]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/asn1_decode_test.c
Make sure we use the libctx when creating an EVP_PKEY_CTX in libssl
[thirdparty/openssl.git] / test / asn1_decode_test.c
1 /*
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 #endif
21 #ifdef __clang__
22 # pragma clang diagnostic ignored "-Wunused-function"
23 #endif
24
25 /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
26 static unsigned char t_invalid_zero[] = {
27 0x30, 0x02, /* SEQUENCE tag + length */
28 0x02, 0x00 /* INTEGER tag + length */
29 };
30
31 #ifndef OPENSSL_NO_DEPRECATED_3_0
32 /* LONG case ************************************************************* */
33
34 typedef struct {
35 long test_long;
36 } ASN1_LONG_DATA;
37
38 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
39 ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
40 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
41
42 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
43 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
44
45 static int test_long(void)
46 {
47 const unsigned char *p = t_invalid_zero;
48 ASN1_LONG_DATA *dectst =
49 d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
50
51 if (dectst == NULL)
52 return 0; /* Fail */
53
54 ASN1_LONG_DATA_free(dectst);
55 return 1;
56 }
57 #endif
58
59 /* INT32 case ************************************************************* */
60
61 typedef struct {
62 int32_t test_int32;
63 } ASN1_INT32_DATA;
64
65 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
66 ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
67 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
68
69 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
70 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
71
72 static int test_int32(void)
73 {
74 const unsigned char *p = t_invalid_zero;
75 ASN1_INT32_DATA *dectst =
76 d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
77
78 if (dectst == NULL)
79 return 0; /* Fail */
80
81 ASN1_INT32_DATA_free(dectst);
82 return 1;
83 }
84
85 /* UINT32 case ************************************************************* */
86
87 typedef struct {
88 uint32_t test_uint32;
89 } ASN1_UINT32_DATA;
90
91 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
92 ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
93 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
94
95 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
96 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
97
98 static int test_uint32(void)
99 {
100 const unsigned char *p = t_invalid_zero;
101 ASN1_UINT32_DATA *dectst =
102 d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
103
104 if (dectst == NULL)
105 return 0; /* Fail */
106
107 ASN1_UINT32_DATA_free(dectst);
108 return 1;
109 }
110
111 /* INT64 case ************************************************************* */
112
113 typedef struct {
114 int64_t test_int64;
115 } ASN1_INT64_DATA;
116
117 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
118 ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
119 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
120
121 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
122 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
123
124 static int test_int64(void)
125 {
126 const unsigned char *p = t_invalid_zero;
127 ASN1_INT64_DATA *dectst =
128 d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
129
130 if (dectst == NULL)
131 return 0; /* Fail */
132
133 ASN1_INT64_DATA_free(dectst);
134 return 1;
135 }
136
137 /* UINT64 case ************************************************************* */
138
139 typedef struct {
140 uint64_t test_uint64;
141 } ASN1_UINT64_DATA;
142
143 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
144 ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
145 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
146
147 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
148 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
149
150 static int test_uint64(void)
151 {
152 const unsigned char *p = t_invalid_zero;
153 ASN1_UINT64_DATA *dectst =
154 d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
155
156 if (dectst == NULL)
157 return 0; /* Fail */
158
159 ASN1_UINT64_DATA_free(dectst);
160 return 1;
161 }
162
163 int setup_tests(void)
164 {
165 #ifndef OPENSSL_NO_DEPRECATED_3_0
166 ADD_TEST(test_long);
167 #endif
168 ADD_TEST(test_int32);
169 ADD_TEST(test_uint32);
170 ADD_TEST(test_int64);
171 ADD_TEST(test_uint64);
172 return 1;
173 }