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