|
0 |
/*
|
|
1 |
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
2 |
*
|
|
3 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
4 |
* this file except in compliance with the License. You can obtain a copy
|
|
5 |
* in the file LICENSE in the source distribution or at
|
|
6 |
* https://www.openssl.org/source/license.html
|
|
7 |
*/
|
|
8 |
|
|
9 |
#include <stdio.h>
|
|
10 |
#include <string.h>
|
|
11 |
|
|
12 |
#include <openssl/rand.h>
|
|
13 |
#include <openssl/asn1t.h>
|
|
14 |
#include "internal/numbers.h"
|
|
15 |
#include "testutil.h"
|
|
16 |
|
|
17 |
#ifdef __GNUC__
|
|
18 |
# pragma GCC diagnostic ignored "-Wunused-function"
|
|
19 |
#endif
|
|
20 |
#ifdef __clang__
|
|
21 |
# pragma clang diagnostic ignored "-Wunused-function"
|
|
22 |
#endif
|
|
23 |
|
|
24 |
/* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
|
|
25 |
static unsigned char t_invalid_zero[] = {
|
|
26 |
0x30, 0x02, /* SEQUENCE tag + length */
|
|
27 |
0x02, 0x00 /* INTEGER tag + length */
|
|
28 |
};
|
|
29 |
|
|
30 |
#if OPENSSL_API_COMPAT < 0x10200000L
|
|
31 |
/* LONG case ************************************************************* */
|
|
32 |
|
|
33 |
typedef struct {
|
|
34 |
long test_long;
|
|
35 |
} ASN1_LONG_DATA;
|
|
36 |
|
|
37 |
ASN1_SEQUENCE(ASN1_LONG_DATA) = {
|
|
38 |
ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
|
|
39 |
} static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
|
|
40 |
|
|
41 |
IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
|
|
42 |
IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
|
|
43 |
|
|
44 |
static int test_long(void)
|
|
45 |
{
|
|
46 |
const unsigned char *p = t_invalid_zero;
|
|
47 |
ASN1_LONG_DATA *dectst =
|
|
48 |
d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
|
|
49 |
|
|
50 |
if (dectst == NULL)
|
|
51 |
return 0; /* Fail */
|
|
52 |
|
|
53 |
ASN1_LONG_DATA_free(dectst);
|
|
54 |
return 1;
|
|
55 |
}
|
|
56 |
#endif
|
|
57 |
|
|
58 |
/* INT32 case ************************************************************* */
|
|
59 |
|
|
60 |
typedef struct {
|
|
61 |
int32_t test_int32;
|
|
62 |
} ASN1_INT32_DATA;
|
|
63 |
|
|
64 |
ASN1_SEQUENCE(ASN1_INT32_DATA) = {
|
|
65 |
ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
|
|
66 |
} static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
|
|
67 |
|
|
68 |
IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
|
|
69 |
IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
|
|
70 |
|
|
71 |
static int test_int32(void)
|
|
72 |
{
|
|
73 |
const unsigned char *p = t_invalid_zero;
|
|
74 |
ASN1_INT32_DATA *dectst =
|
|
75 |
d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
|
|
76 |
|
|
77 |
if (dectst == NULL)
|
|
78 |
return 0; /* Fail */
|
|
79 |
|
|
80 |
ASN1_INT32_DATA_free(dectst);
|
|
81 |
return 1;
|
|
82 |
}
|
|
83 |
|
|
84 |
/* UINT32 case ************************************************************* */
|
|
85 |
|
|
86 |
typedef struct {
|
|
87 |
uint32_t test_uint32;
|
|
88 |
} ASN1_UINT32_DATA;
|
|
89 |
|
|
90 |
ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
|
|
91 |
ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
|
|
92 |
} static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
|
|
93 |
|
|
94 |
IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
|
|
95 |
IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
|
|
96 |
|
|
97 |
static int test_uint32(void)
|
|
98 |
{
|
|
99 |
const unsigned char *p = t_invalid_zero;
|
|
100 |
ASN1_UINT32_DATA *dectst =
|
|
101 |
d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
|
|
102 |
|
|
103 |
if (dectst == NULL)
|
|
104 |
return 0; /* Fail */
|
|
105 |
|
|
106 |
ASN1_UINT32_DATA_free(dectst);
|
|
107 |
return 1;
|
|
108 |
}
|
|
109 |
|
|
110 |
/* INT64 case ************************************************************* */
|
|
111 |
|
|
112 |
typedef struct {
|
|
113 |
int64_t test_int64;
|
|
114 |
} ASN1_INT64_DATA;
|
|
115 |
|
|
116 |
ASN1_SEQUENCE(ASN1_INT64_DATA) = {
|
|
117 |
ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
|
|
118 |
} static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
|
|
119 |
|
|
120 |
IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
|
|
121 |
IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
|
|
122 |
|
|
123 |
static int test_int64(void)
|
|
124 |
{
|
|
125 |
const unsigned char *p = t_invalid_zero;
|
|
126 |
ASN1_INT64_DATA *dectst =
|
|
127 |
d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
|
|
128 |
|
|
129 |
if (dectst == NULL)
|
|
130 |
return 0; /* Fail */
|
|
131 |
|
|
132 |
ASN1_INT64_DATA_free(dectst);
|
|
133 |
return 1;
|
|
134 |
}
|
|
135 |
|
|
136 |
/* UINT64 case ************************************************************* */
|
|
137 |
|
|
138 |
typedef struct {
|
|
139 |
uint64_t test_uint64;
|
|
140 |
} ASN1_UINT64_DATA;
|
|
141 |
|
|
142 |
ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
|
|
143 |
ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
|
|
144 |
} static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
|
|
145 |
|
|
146 |
IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
|
|
147 |
IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
|
|
148 |
|
|
149 |
static int test_uint64(void)
|
|
150 |
{
|
|
151 |
const unsigned char *p = t_invalid_zero;
|
|
152 |
ASN1_UINT64_DATA *dectst =
|
|
153 |
d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
|
|
154 |
|
|
155 |
if (dectst == NULL)
|
|
156 |
return 0; /* Fail */
|
|
157 |
|
|
158 |
ASN1_UINT64_DATA_free(dectst);
|
|
159 |
return 1;
|
|
160 |
}
|
|
161 |
|
|
162 |
int setup_tests(void)
|
|
163 |
{
|
|
164 |
#if OPENSSL_API_COMPAT < 0x10200000L
|
|
165 |
ADD_TEST(test_long);
|
|
166 |
#endif
|
|
167 |
ADD_TEST(test_int32);
|
|
168 |
ADD_TEST(test_uint32);
|
|
169 |
ADD_TEST(test_int64);
|
|
170 |
ADD_TEST(test_uint64);
|
|
171 |
return 1;
|
|
172 |
}
|