@@ -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+ }
0 commit comments