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