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