1 | /*
|
---|
2 | * Copyright 1995-2016 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 <limits.h>
|
---|
12 | #include "internal/cryptlib.h"
|
---|
13 | #include <openssl/asn1.h>
|
---|
14 | #include "asn1_locl.h"
|
---|
15 |
|
---|
16 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
---|
17 | long max);
|
---|
18 | static void asn1_put_length(unsigned char **pp, int length);
|
---|
19 |
|
---|
20 | static int _asn1_check_infinite_end(const unsigned char **p, long len)
|
---|
21 | {
|
---|
22 | /*
|
---|
23 | * If there is 0 or 1 byte left, the length check should pick things up
|
---|
24 | */
|
---|
25 | if (len <= 0)
|
---|
26 | return (1);
|
---|
27 | else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
|
---|
28 | (*p) += 2;
|
---|
29 | return (1);
|
---|
30 | }
|
---|
31 | return (0);
|
---|
32 | }
|
---|
33 |
|
---|
34 | int ASN1_check_infinite_end(unsigned char **p, long len)
|
---|
35 | {
|
---|
36 | return _asn1_check_infinite_end((const unsigned char **)p, len);
|
---|
37 | }
|
---|
38 |
|
---|
39 | int ASN1_const_check_infinite_end(const unsigned char **p, long len)
|
---|
40 | {
|
---|
41 | return _asn1_check_infinite_end(p, len);
|
---|
42 | }
|
---|
43 |
|
---|
44 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
|
---|
45 | int *pclass, long omax)
|
---|
46 | {
|
---|
47 | int i, ret;
|
---|
48 | long l;
|
---|
49 | const unsigned char *p = *pp;
|
---|
50 | int tag, xclass, inf;
|
---|
51 | long max = omax;
|
---|
52 |
|
---|
53 | if (!max)
|
---|
54 | goto err;
|
---|
55 | ret = (*p & V_ASN1_CONSTRUCTED);
|
---|
56 | xclass = (*p & V_ASN1_PRIVATE);
|
---|
57 | i = *p & V_ASN1_PRIMITIVE_TAG;
|
---|
58 | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
|
---|
59 | p++;
|
---|
60 | if (--max == 0)
|
---|
61 | goto err;
|
---|
62 | l = 0;
|
---|
63 | while (*p & 0x80) {
|
---|
64 | l <<= 7L;
|
---|
65 | l |= *(p++) & 0x7f;
|
---|
66 | if (--max == 0)
|
---|
67 | goto err;
|
---|
68 | if (l > (INT_MAX >> 7L))
|
---|
69 | goto err;
|
---|
70 | }
|
---|
71 | l <<= 7L;
|
---|
72 | l |= *(p++) & 0x7f;
|
---|
73 | tag = (int)l;
|
---|
74 | if (--max == 0)
|
---|
75 | goto err;
|
---|
76 | } else {
|
---|
77 | tag = i;
|
---|
78 | p++;
|
---|
79 | if (--max == 0)
|
---|
80 | goto err;
|
---|
81 | }
|
---|
82 | *ptag = tag;
|
---|
83 | *pclass = xclass;
|
---|
84 | if (!asn1_get_length(&p, &inf, plength, max))
|
---|
85 | goto err;
|
---|
86 |
|
---|
87 | if (inf && !(ret & V_ASN1_CONSTRUCTED))
|
---|
88 | goto err;
|
---|
89 |
|
---|
90 | if (*plength > (omax - (p - *pp))) {
|
---|
91 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
|
---|
92 | /*
|
---|
93 | * Set this so that even if things are not long enough the values are
|
---|
94 | * set correctly
|
---|
95 | */
|
---|
96 | ret |= 0x80;
|
---|
97 | }
|
---|
98 | *pp = p;
|
---|
99 | return (ret | inf);
|
---|
100 | err:
|
---|
101 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
|
---|
102 | return (0x80);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
---|
106 | long max)
|
---|
107 | {
|
---|
108 | const unsigned char *p = *pp;
|
---|
109 | unsigned long ret = 0;
|
---|
110 | unsigned long i;
|
---|
111 |
|
---|
112 | if (max-- < 1)
|
---|
113 | return 0;
|
---|
114 | if (*p == 0x80) {
|
---|
115 | *inf = 1;
|
---|
116 | ret = 0;
|
---|
117 | p++;
|
---|
118 | } else {
|
---|
119 | *inf = 0;
|
---|
120 | i = *p & 0x7f;
|
---|
121 | if (*(p++) & 0x80) {
|
---|
122 | if (max < (long)i + 1)
|
---|
123 | return 0;
|
---|
124 | /* Skip leading zeroes */
|
---|
125 | while (i && *p == 0) {
|
---|
126 | p++;
|
---|
127 | i--;
|
---|
128 | }
|
---|
129 | if (i > sizeof(long))
|
---|
130 | return 0;
|
---|
131 | while (i-- > 0) {
|
---|
132 | ret <<= 8L;
|
---|
133 | ret |= *(p++);
|
---|
134 | }
|
---|
135 | } else
|
---|
136 | ret = i;
|
---|
137 | }
|
---|
138 | if (ret > LONG_MAX)
|
---|
139 | return 0;
|
---|
140 | *pp = p;
|
---|
141 | *rl = (long)ret;
|
---|
142 | return 1;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * class 0 is constructed constructed == 2 for indefinite length constructed
|
---|
147 | */
|
---|
148 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
|
---|
149 | int xclass)
|
---|
150 | {
|
---|
151 | unsigned char *p = *pp;
|
---|
152 | int i, ttag;
|
---|
153 |
|
---|
154 | i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
|
---|
155 | i |= (xclass & V_ASN1_PRIVATE);
|
---|
156 | if (tag < 31)
|
---|
157 | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
|
---|
158 | else {
|
---|
159 | *(p++) = i | V_ASN1_PRIMITIVE_TAG;
|
---|
160 | for (i = 0, ttag = tag; ttag > 0; i++)
|
---|
161 | ttag >>= 7;
|
---|
162 | ttag = i;
|
---|
163 | while (i-- > 0) {
|
---|
164 | p[i] = tag & 0x7f;
|
---|
165 | if (i != (ttag - 1))
|
---|
166 | p[i] |= 0x80;
|
---|
167 | tag >>= 7;
|
---|
168 | }
|
---|
169 | p += ttag;
|
---|
170 | }
|
---|
171 | if (constructed == 2)
|
---|
172 | *(p++) = 0x80;
|
---|
173 | else
|
---|
174 | asn1_put_length(&p, length);
|
---|
175 | *pp = p;
|
---|
176 | }
|
---|
177 |
|
---|
178 | int ASN1_put_eoc(unsigned char **pp)
|
---|
179 | {
|
---|
180 | unsigned char *p = *pp;
|
---|
181 | *p++ = 0;
|
---|
182 | *p++ = 0;
|
---|
183 | *pp = p;
|
---|
184 | return 2;
|
---|
185 | }
|
---|
186 |
|
---|
187 | static void asn1_put_length(unsigned char **pp, int length)
|
---|
188 | {
|
---|
189 | unsigned char *p = *pp;
|
---|
190 | int i, l;
|
---|
191 | if (length <= 127)
|
---|
192 | *(p++) = (unsigned char)length;
|
---|
193 | else {
|
---|
194 | l = length;
|
---|
195 | for (i = 0; l > 0; i++)
|
---|
196 | l >>= 8;
|
---|
197 | *(p++) = i | 0x80;
|
---|
198 | l = i;
|
---|
199 | while (i-- > 0) {
|
---|
200 | p[i] = length & 0xff;
|
---|
201 | length >>= 8;
|
---|
202 | }
|
---|
203 | p += l;
|
---|
204 | }
|
---|
205 | *pp = p;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int ASN1_object_size(int constructed, int length, int tag)
|
---|
209 | {
|
---|
210 | int ret = 1;
|
---|
211 | if (length < 0)
|
---|
212 | return -1;
|
---|
213 | if (tag >= 31) {
|
---|
214 | while (tag > 0) {
|
---|
215 | tag >>= 7;
|
---|
216 | ret++;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | if (constructed == 2) {
|
---|
220 | ret += 3;
|
---|
221 | } else {
|
---|
222 | ret++;
|
---|
223 | if (length > 127) {
|
---|
224 | int tmplen = length;
|
---|
225 | while (tmplen > 0) {
|
---|
226 | tmplen >>= 8;
|
---|
227 | ret++;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | if (ret >= INT_MAX - length)
|
---|
232 | return -1;
|
---|
233 | return ret + length;
|
---|
234 | }
|
---|
235 |
|
---|
236 | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
|
---|
237 | {
|
---|
238 | if (str == NULL)
|
---|
239 | return 0;
|
---|
240 | dst->type = str->type;
|
---|
241 | if (!ASN1_STRING_set(dst, str->data, str->length))
|
---|
242 | return 0;
|
---|
243 | /* Copy flags but preserve embed value */
|
---|
244 | dst->flags &= ASN1_STRING_FLAG_EMBED;
|
---|
245 | dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
|
---|
246 | return 1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
|
---|
250 | {
|
---|
251 | ASN1_STRING *ret;
|
---|
252 | if (!str)
|
---|
253 | return NULL;
|
---|
254 | ret = ASN1_STRING_new();
|
---|
255 | if (ret == NULL)
|
---|
256 | return NULL;
|
---|
257 | if (!ASN1_STRING_copy(ret, str)) {
|
---|
258 | ASN1_STRING_free(ret);
|
---|
259 | return NULL;
|
---|
260 | }
|
---|
261 | return ret;
|
---|
262 | }
|
---|
263 |
|
---|
264 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
|
---|
265 | {
|
---|
266 | unsigned char *c;
|
---|
267 | const char *data = _data;
|
---|
268 |
|
---|
269 | if (len < 0) {
|
---|
270 | if (data == NULL)
|
---|
271 | return (0);
|
---|
272 | else
|
---|
273 | len = strlen(data);
|
---|
274 | }
|
---|
275 | if ((str->length <= len) || (str->data == NULL)) {
|
---|
276 | c = str->data;
|
---|
277 | str->data = OPENSSL_realloc(c, len + 1);
|
---|
278 | if (str->data == NULL) {
|
---|
279 | ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
|
---|
280 | str->data = c;
|
---|
281 | return (0);
|
---|
282 | }
|
---|
283 | }
|
---|
284 | str->length = len;
|
---|
285 | if (data != NULL) {
|
---|
286 | memcpy(str->data, data, len);
|
---|
287 | /* an allowance for strings :-) */
|
---|
288 | str->data[len] = '\0';
|
---|
289 | }
|
---|
290 | return (1);
|
---|
291 | }
|
---|
292 |
|
---|
293 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
|
---|
294 | {
|
---|
295 | OPENSSL_free(str->data);
|
---|
296 | str->data = data;
|
---|
297 | str->length = len;
|
---|
298 | }
|
---|
299 |
|
---|
300 | ASN1_STRING *ASN1_STRING_new(void)
|
---|
301 | {
|
---|
302 | return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
|
---|
303 | }
|
---|
304 |
|
---|
305 | ASN1_STRING *ASN1_STRING_type_new(int type)
|
---|
306 | {
|
---|
307 | ASN1_STRING *ret;
|
---|
308 |
|
---|
309 | ret = OPENSSL_zalloc(sizeof(*ret));
|
---|
310 | if (ret == NULL) {
|
---|
311 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
|
---|
312 | return (NULL);
|
---|
313 | }
|
---|
314 | ret->type = type;
|
---|
315 | return (ret);
|
---|
316 | }
|
---|
317 |
|
---|
318 | void asn1_string_embed_free(ASN1_STRING *a, int embed)
|
---|
319 | {
|
---|
320 | if (a == NULL)
|
---|
321 | return;
|
---|
322 | if (!(a->flags & ASN1_STRING_FLAG_NDEF))
|
---|
323 | OPENSSL_free(a->data);
|
---|
324 | if (embed == 0)
|
---|
325 | OPENSSL_free(a);
|
---|
326 | }
|
---|
327 |
|
---|
328 | void ASN1_STRING_free(ASN1_STRING *a)
|
---|
329 | {
|
---|
330 | if (a == NULL)
|
---|
331 | return;
|
---|
332 | asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
|
---|
333 | }
|
---|
334 |
|
---|
335 | void ASN1_STRING_clear_free(ASN1_STRING *a)
|
---|
336 | {
|
---|
337 | if (a == NULL)
|
---|
338 | return;
|
---|
339 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
|
---|
340 | OPENSSL_cleanse(a->data, a->length);
|
---|
341 | ASN1_STRING_free(a);
|
---|
342 | }
|
---|
343 |
|
---|
344 | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
|
---|
345 | {
|
---|
346 | int i;
|
---|
347 |
|
---|
348 | i = (a->length - b->length);
|
---|
349 | if (i == 0) {
|
---|
350 | i = memcmp(a->data, b->data, a->length);
|
---|
351 | if (i == 0)
|
---|
352 | return (a->type - b->type);
|
---|
353 | else
|
---|
354 | return (i);
|
---|
355 | } else
|
---|
356 | return (i);
|
---|
357 | }
|
---|
358 |
|
---|
359 | int ASN1_STRING_length(const ASN1_STRING *x)
|
---|
360 | {
|
---|
361 | return x->length;
|
---|
362 | }
|
---|
363 |
|
---|
364 | void ASN1_STRING_length_set(ASN1_STRING *x, int len)
|
---|
365 | {
|
---|
366 | x->length = len;
|
---|
367 | }
|
---|
368 |
|
---|
369 | int ASN1_STRING_type(const ASN1_STRING *x)
|
---|
370 | {
|
---|
371 | return x->type;
|
---|
372 | }
|
---|
373 |
|
---|
374 | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
|
---|
375 | {
|
---|
376 | return x->data;
|
---|
377 | }
|
---|
378 |
|
---|
379 | # if OPENSSL_API_COMPAT < 0x10100000L
|
---|
380 | unsigned char *ASN1_STRING_data(ASN1_STRING *x)
|
---|
381 | {
|
---|
382 | return x->data;
|
---|
383 | }
|
---|
384 | #endif
|
---|