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