xref: /freebsd/crypto/openssl/test/asn1_decode_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert 
13*e0c4386eSCy Schubert #include <openssl/rand.h>
14*e0c4386eSCy Schubert #include <openssl/asn1t.h>
15*e0c4386eSCy Schubert #include <openssl/obj_mac.h>
16*e0c4386eSCy Schubert #include "internal/numbers.h"
17*e0c4386eSCy Schubert #include "testutil.h"
18*e0c4386eSCy Schubert 
19*e0c4386eSCy Schubert #ifdef __GNUC__
20*e0c4386eSCy Schubert # pragma GCC diagnostic ignored "-Wunused-function"
21*e0c4386eSCy Schubert #endif
22*e0c4386eSCy Schubert #ifdef __clang__
23*e0c4386eSCy Schubert # pragma clang diagnostic ignored "-Wunused-function"
24*e0c4386eSCy Schubert #endif
25*e0c4386eSCy Schubert 
26*e0c4386eSCy Schubert /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
27*e0c4386eSCy Schubert static unsigned char t_invalid_zero[] = {
28*e0c4386eSCy Schubert     0x30, 0x02,                  /* SEQUENCE tag + length */
29*e0c4386eSCy Schubert     0x02, 0x00                   /* INTEGER tag + length */
30*e0c4386eSCy Schubert };
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
33*e0c4386eSCy Schubert /* LONG case ************************************************************* */
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert typedef struct {
36*e0c4386eSCy Schubert     long test_long;
37*e0c4386eSCy Schubert } ASN1_LONG_DATA;
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_LONG_DATA) = {
40*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
41*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
42*e0c4386eSCy Schubert 
43*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
44*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
45*e0c4386eSCy Schubert 
46*e0c4386eSCy Schubert static int test_long(void)
47*e0c4386eSCy Schubert {
48*e0c4386eSCy Schubert     const unsigned char *p = t_invalid_zero;
49*e0c4386eSCy Schubert     ASN1_LONG_DATA *dectst =
50*e0c4386eSCy Schubert         d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
51*e0c4386eSCy Schubert 
52*e0c4386eSCy Schubert     if (dectst == NULL)
53*e0c4386eSCy Schubert         return 0;                /* Fail */
54*e0c4386eSCy Schubert 
55*e0c4386eSCy Schubert     ASN1_LONG_DATA_free(dectst);
56*e0c4386eSCy Schubert     return 1;
57*e0c4386eSCy Schubert }
58*e0c4386eSCy Schubert #endif
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert /* INT32 case ************************************************************* */
61*e0c4386eSCy Schubert 
62*e0c4386eSCy Schubert typedef struct {
63*e0c4386eSCy Schubert     int32_t test_int32;
64*e0c4386eSCy Schubert } ASN1_INT32_DATA;
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_INT32_DATA) = {
67*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
68*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
69*e0c4386eSCy Schubert 
70*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
71*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
72*e0c4386eSCy Schubert 
73*e0c4386eSCy Schubert static int test_int32(void)
74*e0c4386eSCy Schubert {
75*e0c4386eSCy Schubert     const unsigned char *p = t_invalid_zero;
76*e0c4386eSCy Schubert     ASN1_INT32_DATA *dectst =
77*e0c4386eSCy Schubert         d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
78*e0c4386eSCy Schubert 
79*e0c4386eSCy Schubert     if (dectst == NULL)
80*e0c4386eSCy Schubert         return 0;                /* Fail */
81*e0c4386eSCy Schubert 
82*e0c4386eSCy Schubert     ASN1_INT32_DATA_free(dectst);
83*e0c4386eSCy Schubert     return 1;
84*e0c4386eSCy Schubert }
85*e0c4386eSCy Schubert 
86*e0c4386eSCy Schubert /* UINT32 case ************************************************************* */
87*e0c4386eSCy Schubert 
88*e0c4386eSCy Schubert typedef struct {
89*e0c4386eSCy Schubert     uint32_t test_uint32;
90*e0c4386eSCy Schubert } ASN1_UINT32_DATA;
91*e0c4386eSCy Schubert 
92*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
93*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
94*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
95*e0c4386eSCy Schubert 
96*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
97*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert static int test_uint32(void)
100*e0c4386eSCy Schubert {
101*e0c4386eSCy Schubert     const unsigned char *p = t_invalid_zero;
102*e0c4386eSCy Schubert     ASN1_UINT32_DATA *dectst =
103*e0c4386eSCy Schubert         d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
104*e0c4386eSCy Schubert 
105*e0c4386eSCy Schubert     if (dectst == NULL)
106*e0c4386eSCy Schubert         return 0;                /* Fail */
107*e0c4386eSCy Schubert 
108*e0c4386eSCy Schubert     ASN1_UINT32_DATA_free(dectst);
109*e0c4386eSCy Schubert     return 1;
110*e0c4386eSCy Schubert }
111*e0c4386eSCy Schubert 
112*e0c4386eSCy Schubert /* INT64 case ************************************************************* */
113*e0c4386eSCy Schubert 
114*e0c4386eSCy Schubert typedef struct {
115*e0c4386eSCy Schubert     int64_t test_int64;
116*e0c4386eSCy Schubert } ASN1_INT64_DATA;
117*e0c4386eSCy Schubert 
118*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_INT64_DATA) = {
119*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
120*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
121*e0c4386eSCy Schubert 
122*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
123*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
124*e0c4386eSCy Schubert 
125*e0c4386eSCy Schubert static int test_int64(void)
126*e0c4386eSCy Schubert {
127*e0c4386eSCy Schubert     const unsigned char *p = t_invalid_zero;
128*e0c4386eSCy Schubert     ASN1_INT64_DATA *dectst =
129*e0c4386eSCy Schubert         d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
130*e0c4386eSCy Schubert 
131*e0c4386eSCy Schubert     if (dectst == NULL)
132*e0c4386eSCy Schubert         return 0;                /* Fail */
133*e0c4386eSCy Schubert 
134*e0c4386eSCy Schubert     ASN1_INT64_DATA_free(dectst);
135*e0c4386eSCy Schubert     return 1;
136*e0c4386eSCy Schubert }
137*e0c4386eSCy Schubert 
138*e0c4386eSCy Schubert /* UINT64 case ************************************************************* */
139*e0c4386eSCy Schubert 
140*e0c4386eSCy Schubert typedef struct {
141*e0c4386eSCy Schubert     uint64_t test_uint64;
142*e0c4386eSCy Schubert } ASN1_UINT64_DATA;
143*e0c4386eSCy Schubert 
144*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
145*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
146*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
147*e0c4386eSCy Schubert 
148*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
149*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
150*e0c4386eSCy Schubert 
151*e0c4386eSCy Schubert static int test_uint64(void)
152*e0c4386eSCy Schubert {
153*e0c4386eSCy Schubert     const unsigned char *p = t_invalid_zero;
154*e0c4386eSCy Schubert     ASN1_UINT64_DATA *dectst =
155*e0c4386eSCy Schubert         d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
156*e0c4386eSCy Schubert 
157*e0c4386eSCy Schubert     if (dectst == NULL)
158*e0c4386eSCy Schubert         return 0;                /* Fail */
159*e0c4386eSCy Schubert 
160*e0c4386eSCy Schubert     ASN1_UINT64_DATA_free(dectst);
161*e0c4386eSCy Schubert     return 1;
162*e0c4386eSCy Schubert }
163*e0c4386eSCy Schubert 
164*e0c4386eSCy Schubert typedef struct {
165*e0c4386eSCy Schubert     ASN1_STRING *invalidDirString;
166*e0c4386eSCy Schubert } INVALIDTEMPLATE;
167*e0c4386eSCy Schubert 
168*e0c4386eSCy Schubert ASN1_SEQUENCE(INVALIDTEMPLATE) = {
169*e0c4386eSCy Schubert     /*
170*e0c4386eSCy Schubert      * DirectoryString is a CHOICE type so it must use explicit tagging -
171*e0c4386eSCy Schubert      * but we deliberately use implicit here, which makes this template invalid.
172*e0c4386eSCy Schubert      */
173*e0c4386eSCy Schubert     ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
174*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
175*e0c4386eSCy Schubert 
176*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
177*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
178*e0c4386eSCy Schubert 
179*e0c4386eSCy Schubert /* Empty sequence for invalid template test */
180*e0c4386eSCy Schubert static unsigned char t_invalid_template[] = {
181*e0c4386eSCy Schubert     0x30, 0x03,                  /* SEQUENCE tag + length */
182*e0c4386eSCy Schubert     0x0c, 0x01, 0x41             /* UTF8String, length 1, "A" */
183*e0c4386eSCy Schubert };
184*e0c4386eSCy Schubert 
test_invalid_template(void)185*e0c4386eSCy Schubert static int test_invalid_template(void)
186*e0c4386eSCy Schubert {
187*e0c4386eSCy Schubert     const unsigned char *p = t_invalid_template;
188*e0c4386eSCy Schubert     INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
189*e0c4386eSCy Schubert                                                sizeof(t_invalid_template));
190*e0c4386eSCy Schubert 
191*e0c4386eSCy Schubert     /* We expect a NULL pointer return */
192*e0c4386eSCy Schubert     if (TEST_ptr_null(tmp))
193*e0c4386eSCy Schubert         return 1;
194*e0c4386eSCy Schubert 
195*e0c4386eSCy Schubert     INVALIDTEMPLATE_free(tmp);
196*e0c4386eSCy Schubert     return 0;
197*e0c4386eSCy Schubert }
198*e0c4386eSCy Schubert 
test_reuse_asn1_object(void)199*e0c4386eSCy Schubert static int test_reuse_asn1_object(void)
200*e0c4386eSCy Schubert {
201*e0c4386eSCy Schubert     static unsigned char cn_der[] = { 0x06, 0x03, 0x55, 0x04, 0x06 };
202*e0c4386eSCy Schubert     static unsigned char oid_der[] = {
203*e0c4386eSCy Schubert         0x06, 0x06, 0x2a, 0x03, 0x04, 0x05, 0x06, 0x07
204*e0c4386eSCy Schubert     };
205*e0c4386eSCy Schubert     int ret = 0;
206*e0c4386eSCy Schubert     ASN1_OBJECT *obj;
207*e0c4386eSCy Schubert     unsigned char const *p = oid_der;
208*e0c4386eSCy Schubert 
209*e0c4386eSCy Schubert     /* Create an object that owns dynamically allocated 'sn' and 'ln' fields */
210*e0c4386eSCy Schubert 
211*e0c4386eSCy Schubert     if (!TEST_ptr(obj = ASN1_OBJECT_create(NID_undef, cn_der, sizeof(cn_der),
212*e0c4386eSCy Schubert                                            "C", "countryName")))
213*e0c4386eSCy Schubert         goto err;
214*e0c4386eSCy Schubert     /* reuse obj - this should not leak sn and ln */
215*e0c4386eSCy Schubert     if (!TEST_ptr(d2i_ASN1_OBJECT(&obj, &p, sizeof(oid_der))))
216*e0c4386eSCy Schubert         goto err;
217*e0c4386eSCy Schubert     ret = 1;
218*e0c4386eSCy Schubert err:
219*e0c4386eSCy Schubert     ASN1_OBJECT_free(obj);
220*e0c4386eSCy Schubert     return ret;
221*e0c4386eSCy Schubert }
222*e0c4386eSCy Schubert 
setup_tests(void)223*e0c4386eSCy Schubert int setup_tests(void)
224*e0c4386eSCy Schubert {
225*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
226*e0c4386eSCy Schubert     ADD_TEST(test_long);
227*e0c4386eSCy Schubert #endif
228*e0c4386eSCy Schubert     ADD_TEST(test_int32);
229*e0c4386eSCy Schubert     ADD_TEST(test_uint32);
230*e0c4386eSCy Schubert     ADD_TEST(test_int64);
231*e0c4386eSCy Schubert     ADD_TEST(test_uint64);
232*e0c4386eSCy Schubert     ADD_TEST(test_invalid_template);
233*e0c4386eSCy Schubert     ADD_TEST(test_reuse_asn1_object);
234*e0c4386eSCy Schubert     return 1;
235*e0c4386eSCy Schubert }
236