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