Skip to content

Commit 1a5090a

Browse files
committed
Rebase conflicts
1 parent 0f41e99 commit 1a5090a

4 files changed

Lines changed: 88 additions & 7 deletions

File tree

tests/api/test_asn.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,75 @@ int test_DecodeAltNames_length_underflow(void)
10281028
#endif /* !NO_CERTS && !NO_RSA && !NO_ASN */
10291029
return EXPECT_RESULT();
10301030
}
1031+
1032+
int test_wc_DecodeObjectId(void)
1033+
{
1034+
EXPECT_DECLS;
1035+
1036+
#if defined(HAVE_OID_DECODING) || defined(WOLFSSL_ASN_PRINT)
1037+
{
1038+
/* OID 1.2.840.113549.1.1.11 (sha256WithRSAEncryption)
1039+
* DER encoding: 2a 86 48 86 f7 0d 01 01 0b
1040+
* First byte 0x2a = 42 => arc0 = 42/40 = 1, arc1 = 42%40 = 2
1041+
* Remaining arcs: 840, 113549, 1, 1, 11
1042+
*/
1043+
static const byte oid_sha256rsa[] = {
1044+
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b
1045+
};
1046+
word16 out[MAX_OID_SZ];
1047+
word32 outSz;
1048+
1049+
/* Test 1: Normal decode */
1050+
outSz = MAX_OID_SZ;
1051+
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
1052+
out, &outSz), 0);
1053+
ExpectIntEQ((int)outSz, 7);
1054+
ExpectIntEQ(out[0], 1);
1055+
ExpectIntEQ(out[1], 2);
1056+
ExpectIntEQ(out[2], 840);
1057+
ExpectIntEQ(out[3], (word16)113549); /* truncated to word16 */
1058+
ExpectIntEQ(out[4], 1);
1059+
ExpectIntEQ(out[5], 1);
1060+
ExpectIntEQ(out[6], 11);
1061+
1062+
/* Test 2: NULL args */
1063+
outSz = MAX_OID_SZ;
1064+
ExpectIntEQ(DecodeObjectId(NULL, sizeof(oid_sha256rsa), out, &outSz),
1065+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1066+
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
1067+
out, NULL),
1068+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1069+
1070+
/* Test 3 (Bug 1): outSz=1 must return BUFFER_E, not OOB write.
1071+
* The first OID byte decodes into two arcs, so outSz must be >= 2. */
1072+
outSz = 1;
1073+
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
1074+
out, &outSz),
1075+
WC_NO_ERR_TRACE(BUFFER_E));
1076+
1077+
/* Test 4: outSz=0 must also return BUFFER_E */
1078+
outSz = 0;
1079+
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
1080+
out, &outSz),
1081+
WC_NO_ERR_TRACE(BUFFER_E));
1082+
1083+
/* Test 5: outSz=2 is enough for a single-byte OID (two arcs) */
1084+
{
1085+
static const byte oid_one_byte[] = { 0x2a }; /* 1.2 */
1086+
outSz = 2;
1087+
ExpectIntEQ(DecodeObjectId(oid_one_byte, sizeof(oid_one_byte),
1088+
out, &outSz), 0);
1089+
ExpectIntEQ((int)outSz, 2);
1090+
ExpectIntEQ(out[0], 1);
1091+
ExpectIntEQ(out[1], 2);
1092+
}
1093+
1094+
/* Test 6: Buffer too small for later arcs */
1095+
outSz = 3; /* only room for 3 arcs, but OID has 8 */
1096+
ExpectIntEQ(DecodeObjectId(oid_sha256rsa, sizeof(oid_sha256rsa),
1097+
out, &outSz),
1098+
WC_NO_ERR_TRACE(BUFFER_E));
1099+
}
1100+
#endif /* HAVE_OID_DECODING || WOLFSSL_ASN_PRINT */
1101+
return EXPECT_RESULT();
1102+
}

tests/api/test_asn.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ int test_wc_IndexSequenceOf(void);
3030
int test_wolfssl_local_MatchBaseName(void);
3131
int test_wc_DecodeRsaPssParams(void);
3232
int test_DecodeAltNames_length_underflow(void);
33+
int test_wc_DecodeObjectId(void);
3334

3435
#define TEST_ASN_DECLS \
3536
TEST_DECL_GROUP("asn", test_SetAsymKeyDer), \
3637
TEST_DECL_GROUP("asn", test_GetSetShortInt), \
3738
TEST_DECL_GROUP("asn", test_wc_IndexSequenceOf), \
3839
TEST_DECL_GROUP("asn", test_wolfssl_local_MatchBaseName), \
3940
TEST_DECL_GROUP("asn", test_wc_DecodeRsaPssParams), \
40-
TEST_DECL_GROUP("asn", test_DecodeAltNames_length_underflow)
41+
TEST_DECL_GROUP("asn", test_DecodeAltNames_length_underflow), \
42+
TEST_DECL_GROUP("asn", test_wc_DecodeObjectId)
4143

4244
#endif /* WOLFCRYPT_TEST_ASN_H */

wolfcrypt/src/asn.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6808,15 +6808,18 @@ int DecodeObjectId(const byte* in, word32 inSz, word16* out, word32* outSz)
68086808
t = (t << 7) | (in[x] & 0x7F);
68096809
cnt++;
68106810
if (!(in[x] & 0x80)) {
6811-
if (y >= (int)*outSz) {
6812-
return BUFFER_E;
6813-
}
68146811
if (y == 0) {
6812+
if ((int)*outSz < 2) {
6813+
return BUFFER_E;
6814+
}
68156815
out[0] = (word16)(t / 40);
68166816
out[1] = (word16)(t % 40);
68176817
y = 2;
68186818
}
68196819
else {
6820+
if (y >= (int)*outSz) {
6821+
return BUFFER_E;
6822+
}
68206823
out[y++] = (word16)t;
68216824
}
68226825
t = 0; /* reset tmp */
@@ -6913,7 +6916,7 @@ static int DumpOID(const byte* oidData, word32 oidSz, word32 oid,
69136916
#ifdef HAVE_OID_DECODING
69146917
{
69156918
word16 decOid[MAX_OID_SZ];
6916-
word32 decOidSz = sizeof(decOid);
6919+
word32 decOidSz = MAX_OID_SZ;
69176920
/* Decode the OID into dotted form. */
69186921
ret = DecodeObjectId(oidData, oidSz, decOid, &decOidSz);
69196922
if (ret == 0) {
@@ -24084,7 +24087,7 @@ static int DecodeCertExtensions(DecodedCert* cert)
2408424087
if (isUnknownExt && (cert->unknownExtCallback != NULL ||
2408524088
cert->unknownExtCallbackEx != NULL)) {
2408624089
word16 decOid[MAX_OID_SZ];
24087-
word32 decOidSz = sizeof(decOid);
24090+
word32 decOidSz = MAX_OID_SZ;
2408824091
ret = DecodeObjectId(
2408924092
dataASN[CERTEXTASN_IDX_OID].data.oid.data,
2409024093
dataASN[CERTEXTASN_IDX_OID].data.oid.length,

wolfssl/wolfcrypt/asn.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,10 @@ typedef enum MimeStatus
22472247
#define SetAlgoID wc_SetAlgoID
22482248
#define SetAsymKeyDer wc_SetAsymKeyDer
22492249
#define CalcHashId wc_CalcHashId
2250+
#if defined(HAVE_OID_DECODING) || defined(WOLFSSL_ASN_PRINT) || \
2251+
defined(OPENSSL_ALL)
2252+
#define DecodeObjectId wc_DecodeObjectId
2253+
#endif
22502254
#if defined(WOLFSSL_AKID_NAME) && !defined(GetCAByAKID)
22512255
/* GetCAByAKID() has two implementations, a full implementation in
22522256
* src/ssl.c, and a dummy implementation in wolfcrypt/src/asn.c for
@@ -2484,7 +2488,7 @@ WOLFSSL_LOCAL word32 wc_oid_sum(const byte* input, int length);
24842488
#endif
24852489
#if defined(HAVE_OID_DECODING) || defined(WOLFSSL_ASN_PRINT) || \
24862490
defined(OPENSSL_ALL)
2487-
WOLFSSL_LOCAL int DecodeObjectId(const byte* in, word32 inSz,
2491+
WOLFSSL_TEST_VIS int DecodeObjectId(const byte* in, word32 inSz,
24882492
word16* out, word32* outSz);
24892493
#endif
24902494
WOLFSSL_LOCAL int GetASNObjectId(const byte* input, word32* inOutIdx, int* len,

0 commit comments

Comments
 (0)