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