xref: /freebsd/crypto/openssl/test/asn1_encode_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 "internal/numbers.h"
16*e0c4386eSCy Schubert #include "testutil.h"
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #ifdef __GNUC__
19*e0c4386eSCy Schubert # pragma GCC diagnostic ignored "-Wunused-function"
20*e0c4386eSCy Schubert # pragma GCC diagnostic ignored "-Wformat"
21*e0c4386eSCy Schubert #endif
22*e0c4386eSCy Schubert #ifdef __clang__
23*e0c4386eSCy Schubert # pragma clang diagnostic ignored "-Wunused-function"
24*e0c4386eSCy Schubert # pragma clang diagnostic ignored "-Wformat"
25*e0c4386eSCy Schubert #endif
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert /***** Custom test data ******************************************************/
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert /*
30*e0c4386eSCy Schubert  * We conduct tests with these arrays for every type we try out.
31*e0c4386eSCy Schubert  * You will find the expected results together with the test structures
32*e0c4386eSCy Schubert  * for each type, further down.
33*e0c4386eSCy Schubert  */
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert static unsigned char t_zero[] = {
36*e0c4386eSCy Schubert     0x00
37*e0c4386eSCy Schubert };
38*e0c4386eSCy Schubert static unsigned char t_one[] = {
39*e0c4386eSCy Schubert     0x01
40*e0c4386eSCy Schubert };
41*e0c4386eSCy Schubert static unsigned char t_one_neg[] = {
42*e0c4386eSCy Schubert     0xff
43*e0c4386eSCy Schubert };
44*e0c4386eSCy Schubert static unsigned char t_minus_256[] = {
45*e0c4386eSCy Schubert     0xff, 0x00
46*e0c4386eSCy Schubert };
47*e0c4386eSCy Schubert static unsigned char t_longundef[] = {
48*e0c4386eSCy Schubert     0x7f, 0xff, 0xff, 0xff
49*e0c4386eSCy Schubert };
50*e0c4386eSCy Schubert static unsigned char t_9bytes_1[] = {
51*e0c4386eSCy Schubert     0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
52*e0c4386eSCy Schubert };
53*e0c4386eSCy Schubert static unsigned char t_8bytes_1[] = {
54*e0c4386eSCy Schubert     0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
55*e0c4386eSCy Schubert };
56*e0c4386eSCy Schubert static unsigned char t_8bytes_2[] = {
57*e0c4386eSCy Schubert     0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
58*e0c4386eSCy Schubert };
59*e0c4386eSCy Schubert static unsigned char t_8bytes_3_pad[] = {
60*e0c4386eSCy Schubert     0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
61*e0c4386eSCy Schubert };
62*e0c4386eSCy Schubert static unsigned char t_8bytes_4_neg[] = {
63*e0c4386eSCy Schubert     0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
64*e0c4386eSCy Schubert };
65*e0c4386eSCy Schubert static unsigned char t_8bytes_5_negpad[] = {
66*e0c4386eSCy Schubert     0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67*e0c4386eSCy Schubert };
68*e0c4386eSCy Schubert 
69*e0c4386eSCy Schubert /* 32-bit long */
70*e0c4386eSCy Schubert static unsigned char t_5bytes_1[] = {
71*e0c4386eSCy Schubert     0x01, 0xff, 0xff, 0xff, 0xff
72*e0c4386eSCy Schubert };
73*e0c4386eSCy Schubert static unsigned char t_4bytes_1[] = {
74*e0c4386eSCy Schubert     0x00, 0x80, 0x00, 0x00, 0x00
75*e0c4386eSCy Schubert };
76*e0c4386eSCy Schubert /* We make the last byte 0xfe to avoid a clash with ASN1_LONG_UNDEF */
77*e0c4386eSCy Schubert static unsigned char t_4bytes_2[] = {
78*e0c4386eSCy Schubert     0x7f, 0xff, 0xff, 0xfe
79*e0c4386eSCy Schubert };
80*e0c4386eSCy Schubert static unsigned char t_4bytes_3_pad[] = {
81*e0c4386eSCy Schubert     0x00, 0x7f, 0xff, 0xff, 0xfe
82*e0c4386eSCy Schubert };
83*e0c4386eSCy Schubert static unsigned char t_4bytes_4_neg[] = {
84*e0c4386eSCy Schubert     0x80, 0x00, 0x00, 0x00
85*e0c4386eSCy Schubert };
86*e0c4386eSCy Schubert static unsigned char t_4bytes_5_negpad[] = {
87*e0c4386eSCy Schubert     0xff, 0x80, 0x00, 0x00, 0x00
88*e0c4386eSCy Schubert };
89*e0c4386eSCy Schubert 
90*e0c4386eSCy Schubert typedef struct {
91*e0c4386eSCy Schubert     unsigned char *bytes1;
92*e0c4386eSCy Schubert     size_t nbytes1;
93*e0c4386eSCy Schubert     unsigned char *bytes2;
94*e0c4386eSCy Schubert     size_t nbytes2;
95*e0c4386eSCy Schubert } TEST_CUSTOM_DATA;
96*e0c4386eSCy Schubert #define CUSTOM_DATA(v)                          \
97*e0c4386eSCy Schubert     { v, sizeof(v), t_one, sizeof(t_one) },     \
98*e0c4386eSCy Schubert     { t_one, sizeof(t_one), v, sizeof(v) }
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert static TEST_CUSTOM_DATA test_custom_data[] = {
101*e0c4386eSCy Schubert     CUSTOM_DATA(t_zero),
102*e0c4386eSCy Schubert     CUSTOM_DATA(t_longundef),
103*e0c4386eSCy Schubert     CUSTOM_DATA(t_one),
104*e0c4386eSCy Schubert     CUSTOM_DATA(t_one_neg),
105*e0c4386eSCy Schubert     CUSTOM_DATA(t_minus_256),
106*e0c4386eSCy Schubert     CUSTOM_DATA(t_9bytes_1),
107*e0c4386eSCy Schubert     CUSTOM_DATA(t_8bytes_1),
108*e0c4386eSCy Schubert     CUSTOM_DATA(t_8bytes_2),
109*e0c4386eSCy Schubert     CUSTOM_DATA(t_8bytes_3_pad),
110*e0c4386eSCy Schubert     CUSTOM_DATA(t_8bytes_4_neg),
111*e0c4386eSCy Schubert     CUSTOM_DATA(t_8bytes_5_negpad),
112*e0c4386eSCy Schubert     CUSTOM_DATA(t_5bytes_1),
113*e0c4386eSCy Schubert     CUSTOM_DATA(t_4bytes_1),
114*e0c4386eSCy Schubert     CUSTOM_DATA(t_4bytes_2),
115*e0c4386eSCy Schubert     CUSTOM_DATA(t_4bytes_3_pad),
116*e0c4386eSCy Schubert     CUSTOM_DATA(t_4bytes_4_neg),
117*e0c4386eSCy Schubert     CUSTOM_DATA(t_4bytes_5_negpad),
118*e0c4386eSCy Schubert };
119*e0c4386eSCy Schubert 
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert /***** Type specific test data ***********************************************/
122*e0c4386eSCy Schubert 
123*e0c4386eSCy Schubert /*
124*e0c4386eSCy Schubert  * First, a few utility things that all type specific data can use, or in some
125*e0c4386eSCy Schubert  * cases, MUST use.
126*e0c4386eSCy Schubert  */
127*e0c4386eSCy Schubert 
128*e0c4386eSCy Schubert /*
129*e0c4386eSCy Schubert  * For easy creation of arrays of expected data.  These macros correspond to
130*e0c4386eSCy Schubert  * the uses of CUSTOM_DATA above.
131*e0c4386eSCy Schubert  */
132*e0c4386eSCy Schubert #define CUSTOM_EXPECTED_SUCCESS(num, znum)      \
133*e0c4386eSCy Schubert     { 0xff, num, 1 },                           \
134*e0c4386eSCy Schubert     { 0xff, 1, znum }
135*e0c4386eSCy Schubert #define CUSTOM_EXPECTED_FAILURE                 \
136*e0c4386eSCy Schubert     { 0, 0, 0 },                                \
137*e0c4386eSCy Schubert     { 0, 0, 0 }
138*e0c4386eSCy Schubert 
139*e0c4386eSCy Schubert /*
140*e0c4386eSCy Schubert  * A structure to collect all test information in.  There MUST be one instance
141*e0c4386eSCy Schubert  * of this for each test
142*e0c4386eSCy Schubert  */
143*e0c4386eSCy Schubert typedef int i2d_fn(void *a, unsigned char **pp);
144*e0c4386eSCy Schubert typedef void *d2i_fn(void **a, unsigned char **pp, long length);
145*e0c4386eSCy Schubert typedef void ifree_fn(void *a);
146*e0c4386eSCy Schubert typedef struct {
147*e0c4386eSCy Schubert     ASN1_ITEM_EXP *asn1_type;
148*e0c4386eSCy Schubert     const char *name;
149*e0c4386eSCy Schubert     int skip;                    /* 1 if this package should be skipped */
150*e0c4386eSCy Schubert 
151*e0c4386eSCy Schubert     /* An array of structures to compare decoded custom data with */
152*e0c4386eSCy Schubert     void *encode_expectations;
153*e0c4386eSCy Schubert     size_t encode_expectations_size;
154*e0c4386eSCy Schubert     size_t encode_expectations_elem_size;
155*e0c4386eSCy Schubert 
156*e0c4386eSCy Schubert     /*
157*e0c4386eSCy Schubert      * An array of structures that are encoded into a DER blob, which is
158*e0c4386eSCy Schubert      * then decoded, and result gets compared with the original.
159*e0c4386eSCy Schubert      */
160*e0c4386eSCy Schubert     void *encdec_data;
161*e0c4386eSCy Schubert     size_t encdec_data_size;
162*e0c4386eSCy Schubert     size_t encdec_data_elem_size;
163*e0c4386eSCy Schubert 
164*e0c4386eSCy Schubert     /* The i2d function to use with this type */
165*e0c4386eSCy Schubert     i2d_fn *i2d;
166*e0c4386eSCy Schubert     /* The d2i function to use with this type */
167*e0c4386eSCy Schubert     d2i_fn *d2i;
168*e0c4386eSCy Schubert     /* Function to free a decoded structure */
169*e0c4386eSCy Schubert     ifree_fn *ifree;
170*e0c4386eSCy Schubert } TEST_PACKAGE;
171*e0c4386eSCy Schubert 
172*e0c4386eSCy Schubert /* To facilitate the creation of an encdec_data array */
173*e0c4386eSCy Schubert #define ENCDEC_DATA(num, znum)                  \
174*e0c4386eSCy Schubert     { 0xff, num, 1 }, { 0xff, 1, znum }
175*e0c4386eSCy Schubert #define ENCDEC_ARRAY(max, zmax, min, zmin)      \
176*e0c4386eSCy Schubert     ENCDEC_DATA(max,zmax),                      \
177*e0c4386eSCy Schubert     ENCDEC_DATA(min,zmin),                      \
178*e0c4386eSCy Schubert     ENCDEC_DATA(1, 1),                          \
179*e0c4386eSCy Schubert     ENCDEC_DATA(-1, -1),                        \
180*e0c4386eSCy Schubert     ENCDEC_DATA(0, ASN1_LONG_UNDEF)
181*e0c4386eSCy Schubert 
182*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
183*e0c4386eSCy Schubert /***** LONG ******************************************************************/
184*e0c4386eSCy Schubert 
185*e0c4386eSCy Schubert typedef struct {
186*e0c4386eSCy Schubert     /* If decoding is expected to succeed, set this to 1, otherwise 0 */
187*e0c4386eSCy Schubert     ASN1_BOOLEAN success;
188*e0c4386eSCy Schubert     long test_long;
189*e0c4386eSCy Schubert     long test_zlong;
190*e0c4386eSCy Schubert } ASN1_LONG_DATA;
191*e0c4386eSCy Schubert 
192*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_LONG_DATA) = {
193*e0c4386eSCy Schubert     ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_BOOLEAN),
194*e0c4386eSCy Schubert     ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
195*e0c4386eSCy Schubert     ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
196*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
197*e0c4386eSCy Schubert 
198*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
199*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
200*e0c4386eSCy Schubert 
201*e0c4386eSCy Schubert static ASN1_LONG_DATA long_expected_32bit[] = {
202*e0c4386eSCy Schubert     /* The following should fail on the second because it's the default */
203*e0c4386eSCy Schubert     { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
204*e0c4386eSCy Schubert     { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
205*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
206*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
207*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
208*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
209*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
210*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_2 */
211*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad */
212*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_4_neg */
213*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad */
214*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_5bytes_1 */
215*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_1 (too large positive) */
216*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
217*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
218*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
219*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_5_negpad (illegal padding) */
220*e0c4386eSCy Schubert };
221*e0c4386eSCy Schubert static ASN1_LONG_DATA long_encdec_data_32bit[] = {
222*e0c4386eSCy Schubert     ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN),
223*e0c4386eSCy Schubert     /* Check that default numbers fail */
224*e0c4386eSCy Schubert     { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
225*e0c4386eSCy Schubert };
226*e0c4386eSCy Schubert 
227*e0c4386eSCy Schubert static TEST_PACKAGE long_test_package_32bit = {
228*e0c4386eSCy Schubert     ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4,
229*e0c4386eSCy Schubert     long_expected_32bit,
230*e0c4386eSCy Schubert     sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
231*e0c4386eSCy Schubert     long_encdec_data_32bit,
232*e0c4386eSCy Schubert     sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]),
233*e0c4386eSCy Schubert     (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
234*e0c4386eSCy Schubert     (ifree_fn *)ASN1_LONG_DATA_free
235*e0c4386eSCy Schubert };
236*e0c4386eSCy Schubert 
237*e0c4386eSCy Schubert static ASN1_LONG_DATA long_expected_64bit[] = {
238*e0c4386eSCy Schubert     /* The following should fail on the second because it's the default */
239*e0c4386eSCy Schubert     { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
240*e0c4386eSCy Schubert     { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
241*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
242*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
243*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
244*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
245*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
246*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
247*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad (illegal padding) */
248*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */
249*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad (illegal padding) */
250*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */
251*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */
252*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
253*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
254*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
255*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_5_negpad (illegal padding) */
256*e0c4386eSCy Schubert };
257*e0c4386eSCy Schubert static ASN1_LONG_DATA long_encdec_data_64bit[] = {
258*e0c4386eSCy Schubert     ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN),
259*e0c4386eSCy Schubert     /* Check that default numbers fail */
260*e0c4386eSCy Schubert     { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
261*e0c4386eSCy Schubert };
262*e0c4386eSCy Schubert 
263*e0c4386eSCy Schubert static TEST_PACKAGE long_test_package_64bit = {
264*e0c4386eSCy Schubert     ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8,
265*e0c4386eSCy Schubert     long_expected_64bit,
266*e0c4386eSCy Schubert     sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
267*e0c4386eSCy Schubert     long_encdec_data_64bit,
268*e0c4386eSCy Schubert     sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]),
269*e0c4386eSCy Schubert     (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
270*e0c4386eSCy Schubert     (ifree_fn *)ASN1_LONG_DATA_free
271*e0c4386eSCy Schubert };
272*e0c4386eSCy Schubert #endif
273*e0c4386eSCy Schubert 
274*e0c4386eSCy Schubert /***** INT32 *****************************************************************/
275*e0c4386eSCy Schubert 
276*e0c4386eSCy Schubert typedef struct {
277*e0c4386eSCy Schubert     ASN1_BOOLEAN success;
278*e0c4386eSCy Schubert     int32_t test_int32;
279*e0c4386eSCy Schubert     int32_t test_zint32;
280*e0c4386eSCy Schubert } ASN1_INT32_DATA;
281*e0c4386eSCy Schubert 
282*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_INT32_DATA) = {
283*e0c4386eSCy Schubert     ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_BOOLEAN),
284*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
285*e0c4386eSCy Schubert     ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
286*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
287*e0c4386eSCy Schubert 
288*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
289*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
290*e0c4386eSCy Schubert 
291*e0c4386eSCy Schubert static ASN1_INT32_DATA int32_expected[] = {
292*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
293*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
294*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
295*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
296*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
297*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
298*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
299*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_2 */
300*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad */
301*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_4_neg */
302*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad */
303*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_5bytes_1 */
304*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_1 (too large positive) */
305*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
306*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
307*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
308*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_5_negpad (illegal padding) */
309*e0c4386eSCy Schubert };
310*e0c4386eSCy Schubert static ASN1_INT32_DATA int32_encdec_data[] = {
311*e0c4386eSCy Schubert     ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
312*e0c4386eSCy Schubert };
313*e0c4386eSCy Schubert 
314*e0c4386eSCy Schubert static TEST_PACKAGE int32_test_package = {
315*e0c4386eSCy Schubert     ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0,
316*e0c4386eSCy Schubert     int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
317*e0c4386eSCy Schubert     int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
318*e0c4386eSCy Schubert     (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
319*e0c4386eSCy Schubert     (ifree_fn *)ASN1_INT32_DATA_free
320*e0c4386eSCy Schubert };
321*e0c4386eSCy Schubert 
322*e0c4386eSCy Schubert /***** UINT32 ****************************************************************/
323*e0c4386eSCy Schubert 
324*e0c4386eSCy Schubert typedef struct {
325*e0c4386eSCy Schubert     ASN1_BOOLEAN success;
326*e0c4386eSCy Schubert     uint32_t test_uint32;
327*e0c4386eSCy Schubert     uint32_t test_zuint32;
328*e0c4386eSCy Schubert } ASN1_UINT32_DATA;
329*e0c4386eSCy Schubert 
330*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
331*e0c4386eSCy Schubert     ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_BOOLEAN),
332*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
333*e0c4386eSCy Schubert     ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
334*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
335*e0c4386eSCy Schubert 
336*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
337*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
338*e0c4386eSCy Schubert 
339*e0c4386eSCy Schubert static ASN1_UINT32_DATA uint32_expected[] = {
340*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
341*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
342*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
343*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_one_neg (illegal negative value) */
344*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_minus_256 (illegal negative value) */
345*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
346*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 */
347*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_2 */
348*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad */
349*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_4_neg */
350*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad */
351*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_5bytes_1 */
352*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
353*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
354*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
355*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_4_neg (illegal negative value) */
356*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_5_negpad (illegal padding) */
357*e0c4386eSCy Schubert };
358*e0c4386eSCy Schubert static ASN1_UINT32_DATA uint32_encdec_data[] = {
359*e0c4386eSCy Schubert     ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0),
360*e0c4386eSCy Schubert };
361*e0c4386eSCy Schubert 
362*e0c4386eSCy Schubert static TEST_PACKAGE uint32_test_package = {
363*e0c4386eSCy Schubert     ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0,
364*e0c4386eSCy Schubert     uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
365*e0c4386eSCy Schubert     uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
366*e0c4386eSCy Schubert     (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
367*e0c4386eSCy Schubert     (ifree_fn *)ASN1_UINT32_DATA_free
368*e0c4386eSCy Schubert };
369*e0c4386eSCy Schubert 
370*e0c4386eSCy Schubert /***** INT64 *****************************************************************/
371*e0c4386eSCy Schubert 
372*e0c4386eSCy Schubert typedef struct {
373*e0c4386eSCy Schubert     ASN1_BOOLEAN success;
374*e0c4386eSCy Schubert     int64_t test_int64;
375*e0c4386eSCy Schubert     int64_t test_zint64;
376*e0c4386eSCy Schubert } ASN1_INT64_DATA;
377*e0c4386eSCy Schubert 
378*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_INT64_DATA) = {
379*e0c4386eSCy Schubert     ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_BOOLEAN),
380*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
381*e0c4386eSCy Schubert     ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
382*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
383*e0c4386eSCy Schubert 
384*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
385*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
386*e0c4386eSCy Schubert 
387*e0c4386eSCy Schubert static ASN1_INT64_DATA int64_expected[] = {
388*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
389*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
390*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
391*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
392*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
393*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
394*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_1 (too large positive) */
395*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
396*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad (illegal padding) */
397*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
398*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad (illegal padding) */
399*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */
400*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
401*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
402*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
403*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
404*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_5_negpad (illegal padding) */
405*e0c4386eSCy Schubert };
406*e0c4386eSCy Schubert static ASN1_INT64_DATA int64_encdec_data[] = {
407*e0c4386eSCy Schubert     ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN),
408*e0c4386eSCy Schubert     ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
409*e0c4386eSCy Schubert };
410*e0c4386eSCy Schubert 
411*e0c4386eSCy Schubert static TEST_PACKAGE int64_test_package = {
412*e0c4386eSCy Schubert     ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0,
413*e0c4386eSCy Schubert     int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
414*e0c4386eSCy Schubert     int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
415*e0c4386eSCy Schubert     (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
416*e0c4386eSCy Schubert     (ifree_fn *)ASN1_INT64_DATA_free
417*e0c4386eSCy Schubert };
418*e0c4386eSCy Schubert 
419*e0c4386eSCy Schubert /***** UINT64 ****************************************************************/
420*e0c4386eSCy Schubert 
421*e0c4386eSCy Schubert typedef struct {
422*e0c4386eSCy Schubert     ASN1_BOOLEAN success;
423*e0c4386eSCy Schubert     uint64_t test_uint64;
424*e0c4386eSCy Schubert     uint64_t test_zuint64;
425*e0c4386eSCy Schubert } ASN1_UINT64_DATA;
426*e0c4386eSCy Schubert 
427*e0c4386eSCy Schubert ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
428*e0c4386eSCy Schubert     ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_BOOLEAN),
429*e0c4386eSCy Schubert     ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
430*e0c4386eSCy Schubert     ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
431*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
432*e0c4386eSCy Schubert 
433*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
434*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
435*e0c4386eSCy Schubert 
436*e0c4386eSCy Schubert static ASN1_UINT64_DATA uint64_expected[] = {
437*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
438*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
439*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
440*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_one_neg (illegal negative value) */
441*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_minus_256 (illegal negative value) */
442*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_9bytes_1 */
443*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
444*e0c4386eSCy Schubert                                  /* t_8bytes_1 */
445*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
446*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_3_pad */
447*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_4_neg */
448*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_8bytes_5_negpad */
449*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */
450*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
451*e0c4386eSCy Schubert     CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
452*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_3_pad (illegal padding) */
453*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_4_neg (illegal negative value) */
454*e0c4386eSCy Schubert     CUSTOM_EXPECTED_FAILURE,     /* t_4bytes_5_negpad (illegal padding) */
455*e0c4386eSCy Schubert };
456*e0c4386eSCy Schubert static ASN1_UINT64_DATA uint64_encdec_data[] = {
457*e0c4386eSCy Schubert     ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0),
458*e0c4386eSCy Schubert };
459*e0c4386eSCy Schubert 
460*e0c4386eSCy Schubert static TEST_PACKAGE uint64_test_package = {
461*e0c4386eSCy Schubert     ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0,
462*e0c4386eSCy Schubert     uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
463*e0c4386eSCy Schubert     uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
464*e0c4386eSCy Schubert     (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
465*e0c4386eSCy Schubert     (ifree_fn *)ASN1_UINT64_DATA_free
466*e0c4386eSCy Schubert };
467*e0c4386eSCy Schubert 
468*e0c4386eSCy Schubert /***** General testing functions *********************************************/
469*e0c4386eSCy Schubert 
470*e0c4386eSCy Schubert 
471*e0c4386eSCy Schubert /* Template structure to map onto any test data structure */
472*e0c4386eSCy Schubert typedef struct {
473*e0c4386eSCy Schubert     ASN1_BOOLEAN success;
474*e0c4386eSCy Schubert     unsigned char bytes[1];       /* In reality, there's more */
475*e0c4386eSCy Schubert } EXPECTED;
476*e0c4386eSCy Schubert 
477*e0c4386eSCy Schubert /*
478*e0c4386eSCy Schubert  * do_decode returns a tristate:
479*e0c4386eSCy Schubert  *
480*e0c4386eSCy Schubert  *      -1      Couldn't decode
481*e0c4386eSCy Schubert  *      0       decoded structure wasn't what was expected (failure)
482*e0c4386eSCy Schubert  *      1       decoded structure was what was expected (success)
483*e0c4386eSCy Schubert  */
do_decode(unsigned char * bytes,long nbytes,const EXPECTED * expected,size_t expected_size,const TEST_PACKAGE * package)484*e0c4386eSCy Schubert static int do_decode(unsigned char *bytes, long nbytes,
485*e0c4386eSCy Schubert                      const EXPECTED *expected, size_t expected_size,
486*e0c4386eSCy Schubert                      const TEST_PACKAGE *package)
487*e0c4386eSCy Schubert {
488*e0c4386eSCy Schubert     EXPECTED *enctst = NULL;
489*e0c4386eSCy Schubert     const unsigned char *start;
490*e0c4386eSCy Schubert     int ret = 0;
491*e0c4386eSCy Schubert 
492*e0c4386eSCy Schubert     start = bytes;
493*e0c4386eSCy Schubert     enctst = package->d2i(NULL, &bytes, nbytes);
494*e0c4386eSCy Schubert     if (enctst == NULL) {
495*e0c4386eSCy Schubert         if (expected->success == 0) {
496*e0c4386eSCy Schubert             ret = 1;
497*e0c4386eSCy Schubert             ERR_clear_error();
498*e0c4386eSCy Schubert         } else {
499*e0c4386eSCy Schubert             ret = -1;
500*e0c4386eSCy Schubert         }
501*e0c4386eSCy Schubert     } else {
502*e0c4386eSCy Schubert         if (start + nbytes == bytes
503*e0c4386eSCy Schubert             && memcmp(enctst, expected, expected_size) == 0)
504*e0c4386eSCy Schubert             ret = 1;
505*e0c4386eSCy Schubert         else
506*e0c4386eSCy Schubert             ret = 0;
507*e0c4386eSCy Schubert     }
508*e0c4386eSCy Schubert 
509*e0c4386eSCy Schubert     package->ifree(enctst);
510*e0c4386eSCy Schubert     return ret;
511*e0c4386eSCy Schubert }
512*e0c4386eSCy Schubert 
513*e0c4386eSCy Schubert /*
514*e0c4386eSCy Schubert  * do_encode returns a tristate:
515*e0c4386eSCy Schubert  *
516*e0c4386eSCy Schubert  *      -1      Couldn't encode
517*e0c4386eSCy Schubert  *      0       encoded DER wasn't what was expected (failure)
518*e0c4386eSCy Schubert  *      1       encoded DER was what was expected (success)
519*e0c4386eSCy Schubert  */
do_encode(EXPECTED * input,const unsigned char * expected,size_t expected_len,const TEST_PACKAGE * package)520*e0c4386eSCy Schubert static int do_encode(EXPECTED *input,
521*e0c4386eSCy Schubert                      const unsigned char *expected, size_t expected_len,
522*e0c4386eSCy Schubert                      const TEST_PACKAGE *package)
523*e0c4386eSCy Schubert {
524*e0c4386eSCy Schubert     unsigned char *data = NULL;
525*e0c4386eSCy Schubert     int len;
526*e0c4386eSCy Schubert     int ret = 0;
527*e0c4386eSCy Schubert 
528*e0c4386eSCy Schubert     len = package->i2d(input, &data);
529*e0c4386eSCy Schubert     if (len < 0)
530*e0c4386eSCy Schubert         return -1;
531*e0c4386eSCy Schubert 
532*e0c4386eSCy Schubert     if ((size_t)len != expected_len
533*e0c4386eSCy Schubert         || memcmp(data, expected, expected_len) != 0) {
534*e0c4386eSCy Schubert         if (input->success == 0) {
535*e0c4386eSCy Schubert             ret = 1;
536*e0c4386eSCy Schubert             ERR_clear_error();
537*e0c4386eSCy Schubert         } else {
538*e0c4386eSCy Schubert             ret = 0;
539*e0c4386eSCy Schubert         }
540*e0c4386eSCy Schubert     } else {
541*e0c4386eSCy Schubert         ret = 1;
542*e0c4386eSCy Schubert     }
543*e0c4386eSCy Schubert 
544*e0c4386eSCy Schubert     OPENSSL_free(data);
545*e0c4386eSCy Schubert     return ret;
546*e0c4386eSCy Schubert }
547*e0c4386eSCy Schubert 
548*e0c4386eSCy Schubert /* Do an encode/decode round trip */
do_enc_dec(EXPECTED * bytes,long nbytes,const TEST_PACKAGE * package)549*e0c4386eSCy Schubert static int do_enc_dec(EXPECTED *bytes, long nbytes,
550*e0c4386eSCy Schubert                       const TEST_PACKAGE *package)
551*e0c4386eSCy Schubert {
552*e0c4386eSCy Schubert     unsigned char *data = NULL;
553*e0c4386eSCy Schubert     int len;
554*e0c4386eSCy Schubert     int ret = 0;
555*e0c4386eSCy Schubert     void *p = bytes;
556*e0c4386eSCy Schubert 
557*e0c4386eSCy Schubert     len = package->i2d(p, &data);
558*e0c4386eSCy Schubert     if (len < 0)
559*e0c4386eSCy Schubert         return -1;
560*e0c4386eSCy Schubert 
561*e0c4386eSCy Schubert     ret = do_decode(data, len, bytes, nbytes, package);
562*e0c4386eSCy Schubert     OPENSSL_free(data);
563*e0c4386eSCy Schubert     return ret;
564*e0c4386eSCy Schubert }
565*e0c4386eSCy Schubert 
der_encode_length(size_t len,unsigned char ** pp)566*e0c4386eSCy Schubert static size_t der_encode_length(size_t len, unsigned char **pp)
567*e0c4386eSCy Schubert {
568*e0c4386eSCy Schubert     size_t lenbytes;
569*e0c4386eSCy Schubert 
570*e0c4386eSCy Schubert     OPENSSL_assert(len < 0x8000);
571*e0c4386eSCy Schubert     if (len > 255)
572*e0c4386eSCy Schubert         lenbytes = 3;
573*e0c4386eSCy Schubert     else if (len > 127)
574*e0c4386eSCy Schubert         lenbytes = 2;
575*e0c4386eSCy Schubert     else
576*e0c4386eSCy Schubert         lenbytes = 1;
577*e0c4386eSCy Schubert 
578*e0c4386eSCy Schubert     if (pp != NULL) {
579*e0c4386eSCy Schubert         if (lenbytes == 1) {
580*e0c4386eSCy Schubert             *(*pp)++ = (unsigned char)len;
581*e0c4386eSCy Schubert         } else {
582*e0c4386eSCy Schubert             *(*pp)++ = (unsigned char)(lenbytes - 1);
583*e0c4386eSCy Schubert             if (lenbytes == 2) {
584*e0c4386eSCy Schubert                 *(*pp)++ = (unsigned char)(0x80 | len);
585*e0c4386eSCy Schubert             } else {
586*e0c4386eSCy Schubert                 *(*pp)++ = (unsigned char)(0x80 | (len >> 8));
587*e0c4386eSCy Schubert                 *(*pp)++ = (unsigned char)(len);
588*e0c4386eSCy Schubert             }
589*e0c4386eSCy Schubert         }
590*e0c4386eSCy Schubert     }
591*e0c4386eSCy Schubert     return lenbytes;
592*e0c4386eSCy Schubert }
593*e0c4386eSCy Schubert 
make_custom_der(const TEST_CUSTOM_DATA * custom_data,unsigned char ** encoding,int explicit_default)594*e0c4386eSCy Schubert static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data,
595*e0c4386eSCy Schubert                               unsigned char **encoding, int explicit_default)
596*e0c4386eSCy Schubert {
597*e0c4386eSCy Schubert     size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes;
598*e0c4386eSCy Schubert     const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff };
599*e0c4386eSCy Schubert     unsigned char *p = NULL;
600*e0c4386eSCy Schubert     size_t i;
601*e0c4386eSCy Schubert 
602*e0c4386eSCy Schubert     /*
603*e0c4386eSCy Schubert      * The first item is just an INTEGER tag, INTEGER length and INTEGER content
604*e0c4386eSCy Schubert      */
605*e0c4386eSCy Schubert     firstbytes =
606*e0c4386eSCy Schubert         1 + der_encode_length(custom_data->nbytes1, NULL)
607*e0c4386eSCy Schubert         + custom_data->nbytes1;
608*e0c4386eSCy Schubert 
609*e0c4386eSCy Schubert     for (i = custom_data->nbytes2; i > 0; i--) {
610*e0c4386eSCy Schubert         if (custom_data->bytes2[i - 1] != '\0')
611*e0c4386eSCy Schubert             break;
612*e0c4386eSCy Schubert     }
613*e0c4386eSCy Schubert     if (explicit_default || i > 0) {
614*e0c4386eSCy Schubert         /*
615*e0c4386eSCy Schubert          * The second item is an explicit tag, content length, INTEGER tag,
616*e0c4386eSCy Schubert          * INTEGER length, INTEGER bytes
617*e0c4386eSCy Schubert          */
618*e0c4386eSCy Schubert         secondbytesinner =
619*e0c4386eSCy Schubert             1 + der_encode_length(custom_data->nbytes2, NULL)
620*e0c4386eSCy Schubert             + custom_data->nbytes2;
621*e0c4386eSCy Schubert         secondbytes =
622*e0c4386eSCy Schubert             1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner;
623*e0c4386eSCy Schubert     }
624*e0c4386eSCy Schubert 
625*e0c4386eSCy Schubert     /*
626*e0c4386eSCy Schubert      * The whole sequence is the sequence tag, content length, BOOLEAN true
627*e0c4386eSCy Schubert      * (copied from t_true), the first (firstbytes) and second (secondbytes)
628*e0c4386eSCy Schubert      * items
629*e0c4386eSCy Schubert      */
630*e0c4386eSCy Schubert     seqbytes =
631*e0c4386eSCy Schubert         1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL)
632*e0c4386eSCy Schubert         + sizeof(t_true) + firstbytes + secondbytes;
633*e0c4386eSCy Schubert 
634*e0c4386eSCy Schubert     *encoding = p = OPENSSL_malloc(seqbytes);
635*e0c4386eSCy Schubert     if (*encoding == NULL)
636*e0c4386eSCy Schubert         return 0;
637*e0c4386eSCy Schubert 
638*e0c4386eSCy Schubert     /* Sequence tag */
639*e0c4386eSCy Schubert     *p++ = 0x30;
640*e0c4386eSCy Schubert     der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p);
641*e0c4386eSCy Schubert 
642*e0c4386eSCy Schubert     /* ASN1_BOOLEAN TRUE */
643*e0c4386eSCy Schubert     memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */
644*e0c4386eSCy Schubert     p += sizeof(t_true);
645*e0c4386eSCy Schubert 
646*e0c4386eSCy Schubert     /* First INTEGER item (non-optional) */
647*e0c4386eSCy Schubert     *p++ = V_ASN1_INTEGER;
648*e0c4386eSCy Schubert     der_encode_length(custom_data->nbytes1, &p);
649*e0c4386eSCy Schubert     memcpy(p, custom_data->bytes1, custom_data->nbytes1);
650*e0c4386eSCy Schubert     p += custom_data->nbytes1;
651*e0c4386eSCy Schubert 
652*e0c4386eSCy Schubert     if (secondbytes > 0) {
653*e0c4386eSCy Schubert         /* Second INTEGER item (optional) */
654*e0c4386eSCy Schubert         /* Start with the explicit optional tag */
655*e0c4386eSCy Schubert         *p++ = 0xa0;
656*e0c4386eSCy Schubert         der_encode_length(secondbytesinner, &p);
657*e0c4386eSCy Schubert         *p++ = V_ASN1_INTEGER;
658*e0c4386eSCy Schubert         der_encode_length(custom_data->nbytes2, &p);
659*e0c4386eSCy Schubert         memcpy(p, custom_data->bytes2, custom_data->nbytes2);
660*e0c4386eSCy Schubert         p += custom_data->nbytes2;
661*e0c4386eSCy Schubert     }
662*e0c4386eSCy Schubert 
663*e0c4386eSCy Schubert     OPENSSL_assert(seqbytes == (size_t)(p - *encoding));
664*e0c4386eSCy Schubert 
665*e0c4386eSCy Schubert     return seqbytes;
666*e0c4386eSCy Schubert }
667*e0c4386eSCy Schubert 
668*e0c4386eSCy Schubert /* Attempt to decode a custom encoding of the test structure */
do_decode_custom(const TEST_CUSTOM_DATA * custom_data,const EXPECTED * expected,size_t expected_size,const TEST_PACKAGE * package)669*e0c4386eSCy Schubert static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
670*e0c4386eSCy Schubert                             const EXPECTED *expected, size_t expected_size,
671*e0c4386eSCy Schubert                             const TEST_PACKAGE *package)
672*e0c4386eSCy Schubert {
673*e0c4386eSCy Schubert     unsigned char *encoding = NULL;
674*e0c4386eSCy Schubert     /*
675*e0c4386eSCy Schubert      * We force the defaults to be explicitly encoded to make sure we test
676*e0c4386eSCy Schubert      * for defaults that shouldn't be present (i.e. we check for failure)
677*e0c4386eSCy Schubert      */
678*e0c4386eSCy Schubert     size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
679*e0c4386eSCy Schubert     int ret;
680*e0c4386eSCy Schubert 
681*e0c4386eSCy Schubert     if (encoding_length == 0)
682*e0c4386eSCy Schubert         return -1;
683*e0c4386eSCy Schubert 
684*e0c4386eSCy Schubert     ret = do_decode(encoding, encoding_length, expected, expected_size,
685*e0c4386eSCy Schubert                     package);
686*e0c4386eSCy Schubert     OPENSSL_free(encoding);
687*e0c4386eSCy Schubert 
688*e0c4386eSCy Schubert     return ret;
689*e0c4386eSCy Schubert }
690*e0c4386eSCy Schubert 
691*e0c4386eSCy Schubert /* Attempt to encode the test structure and compare it to custom DER */
do_encode_custom(EXPECTED * input,const TEST_CUSTOM_DATA * custom_data,const TEST_PACKAGE * package)692*e0c4386eSCy Schubert static int do_encode_custom(EXPECTED *input,
693*e0c4386eSCy Schubert                             const TEST_CUSTOM_DATA *custom_data,
694*e0c4386eSCy Schubert                             const TEST_PACKAGE *package)
695*e0c4386eSCy Schubert {
696*e0c4386eSCy Schubert     unsigned char *expected = NULL;
697*e0c4386eSCy Schubert     size_t expected_length = make_custom_der(custom_data, &expected, 0);
698*e0c4386eSCy Schubert     int ret;
699*e0c4386eSCy Schubert 
700*e0c4386eSCy Schubert     if (expected_length == 0)
701*e0c4386eSCy Schubert         return -1;
702*e0c4386eSCy Schubert 
703*e0c4386eSCy Schubert     ret = do_encode(input, expected, expected_length, package);
704*e0c4386eSCy Schubert     OPENSSL_free(expected);
705*e0c4386eSCy Schubert 
706*e0c4386eSCy Schubert     return ret;
707*e0c4386eSCy Schubert }
708*e0c4386eSCy Schubert 
do_print_item(const TEST_PACKAGE * package)709*e0c4386eSCy Schubert static int do_print_item(const TEST_PACKAGE *package)
710*e0c4386eSCy Schubert {
711*e0c4386eSCy Schubert #define DATA_BUF_SIZE 256
712*e0c4386eSCy Schubert     const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
713*e0c4386eSCy Schubert     ASN1_VALUE *o;
714*e0c4386eSCy Schubert     int ret;
715*e0c4386eSCy Schubert 
716*e0c4386eSCy Schubert     OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
717*e0c4386eSCy Schubert     if ((o = OPENSSL_malloc(DATA_BUF_SIZE)) == NULL)
718*e0c4386eSCy Schubert         return 0;
719*e0c4386eSCy Schubert 
720*e0c4386eSCy Schubert     (void)RAND_bytes((unsigned char*)o,
721*e0c4386eSCy Schubert                      (int)package->encode_expectations_elem_size);
722*e0c4386eSCy Schubert     ret = ASN1_item_print(bio_err, o, 0, i, NULL);
723*e0c4386eSCy Schubert     OPENSSL_free(o);
724*e0c4386eSCy Schubert 
725*e0c4386eSCy Schubert     return ret;
726*e0c4386eSCy Schubert }
727*e0c4386eSCy Schubert 
728*e0c4386eSCy Schubert 
test_intern(const TEST_PACKAGE * package)729*e0c4386eSCy Schubert static int test_intern(const TEST_PACKAGE *package)
730*e0c4386eSCy Schubert {
731*e0c4386eSCy Schubert     unsigned int i;
732*e0c4386eSCy Schubert     size_t nelems;
733*e0c4386eSCy Schubert     int fail = 0;
734*e0c4386eSCy Schubert 
735*e0c4386eSCy Schubert     if (package->skip)
736*e0c4386eSCy Schubert         return 1;
737*e0c4386eSCy Schubert 
738*e0c4386eSCy Schubert     /* Do decode_custom checks */
739*e0c4386eSCy Schubert     nelems = package->encode_expectations_size
740*e0c4386eSCy Schubert         / package->encode_expectations_elem_size;
741*e0c4386eSCy Schubert     OPENSSL_assert(nelems ==
742*e0c4386eSCy Schubert                    sizeof(test_custom_data) / sizeof(test_custom_data[0]));
743*e0c4386eSCy Schubert     for (i = 0; i < nelems; i++) {
744*e0c4386eSCy Schubert         size_t pos = i * package->encode_expectations_elem_size;
745*e0c4386eSCy Schubert         EXPECTED *expected
746*e0c4386eSCy Schubert             = (EXPECTED *)&((unsigned char *)package->encode_expectations)[pos];
747*e0c4386eSCy Schubert 
748*e0c4386eSCy Schubert         switch (do_encode_custom(expected, &test_custom_data[i], package)) {
749*e0c4386eSCy Schubert         case -1:
750*e0c4386eSCy Schubert             if (expected->success) {
751*e0c4386eSCy Schubert                 TEST_error("Failed custom encode round trip %u of %s",
752*e0c4386eSCy Schubert                            i, package->name);
753*e0c4386eSCy Schubert                 TEST_openssl_errors();
754*e0c4386eSCy Schubert                 fail++;
755*e0c4386eSCy Schubert             }
756*e0c4386eSCy Schubert             break;
757*e0c4386eSCy Schubert         case 0:
758*e0c4386eSCy Schubert             TEST_error("Custom encode round trip %u of %s mismatch",
759*e0c4386eSCy Schubert                        i, package->name);
760*e0c4386eSCy Schubert             TEST_openssl_errors();
761*e0c4386eSCy Schubert             fail++;
762*e0c4386eSCy Schubert             break;
763*e0c4386eSCy Schubert         case 1:
764*e0c4386eSCy Schubert             break;
765*e0c4386eSCy Schubert         default:
766*e0c4386eSCy Schubert             OPENSSL_die("do_encode_custom() return unknown value",
767*e0c4386eSCy Schubert                         __FILE__, __LINE__);
768*e0c4386eSCy Schubert         }
769*e0c4386eSCy Schubert         switch (do_decode_custom(&test_custom_data[i], expected,
770*e0c4386eSCy Schubert                                  package->encode_expectations_elem_size,
771*e0c4386eSCy Schubert                                  package)) {
772*e0c4386eSCy Schubert         case -1:
773*e0c4386eSCy Schubert             if (expected->success) {
774*e0c4386eSCy Schubert                 TEST_error("Failed custom decode round trip %u of %s",
775*e0c4386eSCy Schubert                            i, package->name);
776*e0c4386eSCy Schubert                 TEST_openssl_errors();
777*e0c4386eSCy Schubert                 fail++;
778*e0c4386eSCy Schubert             }
779*e0c4386eSCy Schubert             break;
780*e0c4386eSCy Schubert         case 0:
781*e0c4386eSCy Schubert             TEST_error("Custom decode round trip %u of %s mismatch",
782*e0c4386eSCy Schubert                        i, package->name);
783*e0c4386eSCy Schubert             TEST_openssl_errors();
784*e0c4386eSCy Schubert             fail++;
785*e0c4386eSCy Schubert             break;
786*e0c4386eSCy Schubert         case 1:
787*e0c4386eSCy Schubert             break;
788*e0c4386eSCy Schubert         default:
789*e0c4386eSCy Schubert             OPENSSL_die("do_decode_custom() return unknown value",
790*e0c4386eSCy Schubert                         __FILE__, __LINE__);
791*e0c4386eSCy Schubert         }
792*e0c4386eSCy Schubert     }
793*e0c4386eSCy Schubert 
794*e0c4386eSCy Schubert     /* Do enc_dec checks */
795*e0c4386eSCy Schubert     nelems = package->encdec_data_size / package->encdec_data_elem_size;
796*e0c4386eSCy Schubert     for (i = 0; i < nelems; i++) {
797*e0c4386eSCy Schubert         size_t pos = i * package->encdec_data_elem_size;
798*e0c4386eSCy Schubert         EXPECTED *expected
799*e0c4386eSCy Schubert             = (EXPECTED *)&((unsigned char *)package->encdec_data)[pos];
800*e0c4386eSCy Schubert 
801*e0c4386eSCy Schubert         switch (do_enc_dec(expected, package->encdec_data_elem_size, package)) {
802*e0c4386eSCy Schubert         case -1:
803*e0c4386eSCy Schubert             if (expected->success) {
804*e0c4386eSCy Schubert                 TEST_error("Failed encode/decode round trip %u of %s",
805*e0c4386eSCy Schubert                            i, package->name);
806*e0c4386eSCy Schubert                 TEST_openssl_errors();
807*e0c4386eSCy Schubert                 fail++;
808*e0c4386eSCy Schubert             }
809*e0c4386eSCy Schubert             break;
810*e0c4386eSCy Schubert         case 0:
811*e0c4386eSCy Schubert             TEST_error("Encode/decode round trip %u of %s mismatch",
812*e0c4386eSCy Schubert                        i, package->name);
813*e0c4386eSCy Schubert             fail++;
814*e0c4386eSCy Schubert             break;
815*e0c4386eSCy Schubert         case 1:
816*e0c4386eSCy Schubert             break;
817*e0c4386eSCy Schubert         default:
818*e0c4386eSCy Schubert             OPENSSL_die("do_enc_dec() return unknown value",
819*e0c4386eSCy Schubert                         __FILE__, __LINE__);
820*e0c4386eSCy Schubert         }
821*e0c4386eSCy Schubert     }
822*e0c4386eSCy Schubert 
823*e0c4386eSCy Schubert     if (!do_print_item(package)) {
824*e0c4386eSCy Schubert         TEST_error("Printing of %s failed", package->name);
825*e0c4386eSCy Schubert         TEST_openssl_errors();
826*e0c4386eSCy Schubert         fail++;
827*e0c4386eSCy Schubert     }
828*e0c4386eSCy Schubert 
829*e0c4386eSCy Schubert     return fail == 0;
830*e0c4386eSCy Schubert }
831*e0c4386eSCy Schubert 
832*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
test_long_32bit(void)833*e0c4386eSCy Schubert static int test_long_32bit(void)
834*e0c4386eSCy Schubert {
835*e0c4386eSCy Schubert     return test_intern(&long_test_package_32bit);
836*e0c4386eSCy Schubert }
837*e0c4386eSCy Schubert 
test_long_64bit(void)838*e0c4386eSCy Schubert static int test_long_64bit(void)
839*e0c4386eSCy Schubert {
840*e0c4386eSCy Schubert     return test_intern(&long_test_package_64bit);
841*e0c4386eSCy Schubert }
842*e0c4386eSCy Schubert #endif
843*e0c4386eSCy Schubert 
test_int32(void)844*e0c4386eSCy Schubert static int test_int32(void)
845*e0c4386eSCy Schubert {
846*e0c4386eSCy Schubert     return test_intern(&int32_test_package);
847*e0c4386eSCy Schubert }
848*e0c4386eSCy Schubert 
test_uint32(void)849*e0c4386eSCy Schubert static int test_uint32(void)
850*e0c4386eSCy Schubert {
851*e0c4386eSCy Schubert     return test_intern(&uint32_test_package);
852*e0c4386eSCy Schubert }
853*e0c4386eSCy Schubert 
test_int64(void)854*e0c4386eSCy Schubert static int test_int64(void)
855*e0c4386eSCy Schubert {
856*e0c4386eSCy Schubert     return test_intern(&int64_test_package);
857*e0c4386eSCy Schubert }
858*e0c4386eSCy Schubert 
test_uint64(void)859*e0c4386eSCy Schubert static int test_uint64(void)
860*e0c4386eSCy Schubert {
861*e0c4386eSCy Schubert     return test_intern(&uint64_test_package);
862*e0c4386eSCy Schubert }
863*e0c4386eSCy Schubert 
864*e0c4386eSCy Schubert typedef struct {
865*e0c4386eSCy Schubert     ASN1_STRING *invalidDirString;
866*e0c4386eSCy Schubert } INVALIDTEMPLATE;
867*e0c4386eSCy Schubert 
868*e0c4386eSCy Schubert ASN1_SEQUENCE(INVALIDTEMPLATE) = {
869*e0c4386eSCy Schubert     /*
870*e0c4386eSCy Schubert      * DirectoryString is a CHOICE type so it must use explicit tagging -
871*e0c4386eSCy Schubert      * but we deliberately use implicit here, which makes this template invalid.
872*e0c4386eSCy Schubert      */
873*e0c4386eSCy Schubert     ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
874*e0c4386eSCy Schubert } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
875*e0c4386eSCy Schubert 
876*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
877*e0c4386eSCy Schubert IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
878*e0c4386eSCy Schubert 
879*e0c4386eSCy Schubert static int test_invalid_template(void)
880*e0c4386eSCy Schubert {
881*e0c4386eSCy Schubert     INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new();
882*e0c4386eSCy Schubert     int ret;
883*e0c4386eSCy Schubert 
884*e0c4386eSCy Schubert     if (!TEST_ptr(temp))
885*e0c4386eSCy Schubert         return 0;
886*e0c4386eSCy Schubert 
887*e0c4386eSCy Schubert     ret = i2d_INVALIDTEMPLATE(temp, NULL);
888*e0c4386eSCy Schubert 
889*e0c4386eSCy Schubert     INVALIDTEMPLATE_free(temp);
890*e0c4386eSCy Schubert 
891*e0c4386eSCy Schubert     /* We expect the i2d operation to fail */
892*e0c4386eSCy Schubert     return ret < 0;
893*e0c4386eSCy Schubert }
894*e0c4386eSCy Schubert 
895*e0c4386eSCy Schubert 
setup_tests(void)896*e0c4386eSCy Schubert int setup_tests(void)
897*e0c4386eSCy Schubert {
898*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
899*e0c4386eSCy Schubert     ADD_TEST(test_long_32bit);
900*e0c4386eSCy Schubert     ADD_TEST(test_long_64bit);
901*e0c4386eSCy Schubert #endif
902*e0c4386eSCy Schubert     ADD_TEST(test_int32);
903*e0c4386eSCy Schubert     ADD_TEST(test_uint32);
904*e0c4386eSCy Schubert     ADD_TEST(test_int64);
905*e0c4386eSCy Schubert     ADD_TEST(test_uint64);
906*e0c4386eSCy Schubert     ADD_TEST(test_invalid_template);
907*e0c4386eSCy Schubert     return 1;
908*e0c4386eSCy Schubert }
909