]>
Commit | Line | Data |
---|---|---|
b1322259 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
365a2d99 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
f9082268 | 11 | #include <limits.h> |
b39fc560 | 12 | #include "internal/cryptlib.h" |
ec577822 | 13 | #include <openssl/asn1.h> |
706457b7 | 14 | #include "asn1_local.h" |
d02b48c6 | 15 | |
0f113f3e | 16 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
79c7f74d | 17 | long max); |
d02b48c6 | 18 | static void asn1_put_length(unsigned char **pp, int length); |
d02b48c6 | 19 | |
875a644a | 20 | static int _asn1_check_infinite_end(const unsigned char **p, long len) |
0f113f3e MC |
21 | { |
22 | /* | |
23 | * If there is 0 or 1 byte left, the length check should pick things up | |
24 | */ | |
da42c2a3 | 25 | if (len <= 0) { |
1f06acc0 | 26 | return 1; |
da42c2a3 DDO |
27 | } else { |
28 | if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { | |
29 | (*p) += 2; | |
30 | return 1; | |
31 | } | |
0f113f3e | 32 | } |
1f06acc0 | 33 | return 0; |
0f113f3e | 34 | } |
d02b48c6 | 35 | |
6343829a | 36 | int ASN1_check_infinite_end(unsigned char **p, long len) |
0f113f3e MC |
37 | { |
38 | return _asn1_check_infinite_end((const unsigned char **)p, len); | |
39 | } | |
d02b48c6 | 40 | |
6343829a | 41 | int ASN1_const_check_infinite_end(const unsigned char **p, long len) |
0f113f3e MC |
42 | { |
43 | return _asn1_check_infinite_end(p, len); | |
44 | } | |
875a644a | 45 | |
6343829a | 46 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
0f113f3e MC |
47 | int *pclass, long omax) |
48 | { | |
49 | int i, ret; | |
da42c2a3 | 50 | long len; |
0f113f3e MC |
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; | |
da42c2a3 | 64 | len = 0; |
0f113f3e | 65 | while (*p & 0x80) { |
da42c2a3 DDO |
66 | len <<= 7L; |
67 | len |= *(p++) & 0x7f; | |
0f113f3e MC |
68 | if (--max == 0) |
69 | goto err; | |
da42c2a3 | 70 | if (len > (INT_MAX >> 7L)) |
0f113f3e MC |
71 | goto err; |
72 | } | |
da42c2a3 DDO |
73 | len <<= 7L; |
74 | len |= *(p++) & 0x7f; | |
75 | tag = (int)len; | |
0f113f3e MC |
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; | |
79c7f74d | 86 | if (!asn1_get_length(&p, &inf, plength, max)) |
0f113f3e MC |
87 | goto err; |
88 | ||
89 | if (inf && !(ret & V_ASN1_CONSTRUCTED)) | |
90 | goto err; | |
398e99fe | 91 | |
0f113f3e MC |
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; | |
1f06acc0 | 101 | return ret | inf; |
0f113f3e MC |
102 | err: |
103 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG); | |
1f06acc0 | 104 | return 0x80; |
0f113f3e MC |
105 | } |
106 | ||
a9a157e7 P |
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 | */ | |
0f113f3e | 114 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
79c7f74d | 115 | long max) |
0f113f3e MC |
116 | { |
117 | const unsigned char *p = *pp; | |
118 | unsigned long ret = 0; | |
a9a157e7 | 119 | int i; |
0f113f3e MC |
120 | |
121 | if (max-- < 1) | |
79c7f74d | 122 | return 0; |
0f113f3e MC |
123 | if (*p == 0x80) { |
124 | *inf = 1; | |
0f113f3e MC |
125 | p++; |
126 | } else { | |
127 | *inf = 0; | |
128 | i = *p & 0x7f; | |
a9a157e7 P |
129 | if (*p++ & 0x80) { |
130 | if (max < i + 1) | |
0f113f3e MC |
131 | return 0; |
132 | /* Skip leading zeroes */ | |
a9a157e7 | 133 | while (i > 0 && *p == 0) { |
0f113f3e MC |
134 | p++; |
135 | i--; | |
136 | } | |
a9a157e7 | 137 | if (i > (int)sizeof(long)) |
0f113f3e | 138 | return 0; |
a9a157e7 P |
139 | while (i > 0) { |
140 | ret <<= 8; | |
141 | ret |= *p++; | |
142 | i--; | |
0f113f3e | 143 | } |
a9a157e7 P |
144 | if (ret > LONG_MAX) |
145 | return 0; | |
da42c2a3 | 146 | } else { |
0f113f3e | 147 | ret = i; |
da42c2a3 | 148 | } |
0f113f3e | 149 | } |
0f113f3e MC |
150 | *pp = p; |
151 | *rl = (long)ret; | |
79c7f74d | 152 | return 1; |
0f113f3e MC |
153 | } |
154 | ||
155 | /* | |
156 | * class 0 is constructed constructed == 2 for indefinite length constructed | |
157 | */ | |
6343829a | 158 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
0f113f3e MC |
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); | |
da42c2a3 | 166 | if (tag < 31) { |
0f113f3e | 167 | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); |
da42c2a3 | 168 | } else { |
0f113f3e MC |
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 | } | |
d02b48c6 | 187 | |
230fd6b7 | 188 | int ASN1_put_eoc(unsigned char **pp) |
0f113f3e MC |
189 | { |
190 | unsigned char *p = *pp; | |
da42c2a3 | 191 | |
0f113f3e MC |
192 | *p++ = 0; |
193 | *p++ = 0; | |
194 | *pp = p; | |
195 | return 2; | |
196 | } | |
230fd6b7 | 197 | |
6b691a5c | 198 | static void asn1_put_length(unsigned char **pp, int length) |
0f113f3e MC |
199 | { |
200 | unsigned char *p = *pp; | |
da42c2a3 DDO |
201 | int i, len; |
202 | ||
203 | if (length <= 127) { | |
0f113f3e | 204 | *(p++) = (unsigned char)length; |
da42c2a3 DDO |
205 | } else { |
206 | len = length; | |
207 | for (i = 0; len > 0; i++) | |
208 | len >>= 8; | |
0f113f3e | 209 | *(p++) = i | 0x80; |
da42c2a3 | 210 | len = i; |
0f113f3e MC |
211 | while (i-- > 0) { |
212 | p[i] = length & 0xff; | |
213 | length >>= 8; | |
214 | } | |
da42c2a3 | 215 | p += len; |
0f113f3e MC |
216 | } |
217 | *pp = p; | |
218 | } | |
d02b48c6 | 219 | |
6343829a | 220 | int ASN1_object_size(int constructed, int length, int tag) |
0f113f3e | 221 | { |
e9f17097 | 222 | int ret = 1; |
da42c2a3 | 223 | |
e9f17097 DSH |
224 | if (length < 0) |
225 | return -1; | |
0f113f3e MC |
226 | if (tag >= 31) { |
227 | while (tag > 0) { | |
228 | tag >>= 7; | |
229 | ret++; | |
230 | } | |
231 | } | |
e9f17097 DSH |
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 | } | |
0f113f3e MC |
242 | } |
243 | } | |
e9f17097 DSH |
244 | if (ret >= INT_MAX - length) |
245 | return -1; | |
246 | return ret + length; | |
0f113f3e | 247 | } |
d02b48c6 | 248 | |
18f54773 | 249 | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) |
0f113f3e MC |
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; | |
4002da0f DSH |
256 | /* Copy flags but preserve embed value */ |
257 | dst->flags &= ASN1_STRING_FLAG_EMBED; | |
258 | dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED; | |
0f113f3e MC |
259 | return 1; |
260 | } | |
18f54773 | 261 | |
6384e46d | 262 | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) |
0f113f3e MC |
263 | { |
264 | ASN1_STRING *ret; | |
da42c2a3 | 265 | |
0f113f3e MC |
266 | if (!str) |
267 | return NULL; | |
268 | ret = ASN1_STRING_new(); | |
90945fa3 | 269 | if (ret == NULL) |
0f113f3e MC |
270 | return NULL; |
271 | if (!ASN1_STRING_copy(ret, str)) { | |
272 | ASN1_STRING_free(ret); | |
273 | return NULL; | |
274 | } | |
275 | return ret; | |
276 | } | |
d02b48c6 | 277 | |
6343829a | 278 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) |
0f113f3e MC |
279 | { |
280 | unsigned char *c; | |
281 | const char *data = _data; | |
282 | ||
283 | if (len < 0) { | |
284 | if (data == NULL) | |
1f06acc0 | 285 | return 0; |
0f113f3e MC |
286 | else |
287 | len = strlen(data); | |
288 | } | |
a73be798 | 289 | if ((str->length <= len) || (str->data == NULL)) { |
0f113f3e | 290 | c = str->data; |
2d29e2df | 291 | str->data = OPENSSL_realloc(c, len + 1); |
0f113f3e MC |
292 | if (str->data == NULL) { |
293 | ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); | |
294 | str->data = c; | |
1f06acc0 | 295 | return 0; |
0f113f3e MC |
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 | } | |
1f06acc0 | 304 | return 1; |
0f113f3e | 305 | } |
d02b48c6 | 306 | |
6343829a | 307 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) |
0f113f3e | 308 | { |
b548a1f1 | 309 | OPENSSL_free(str->data); |
0f113f3e MC |
310 | str->data = data; |
311 | str->length = len; | |
312 | } | |
399a6f0b | 313 | |
6b691a5c | 314 | ASN1_STRING *ASN1_STRING_new(void) |
0f113f3e | 315 | { |
1f06acc0 | 316 | return ASN1_STRING_type_new(V_ASN1_OCTET_STRING); |
0f113f3e | 317 | } |
d02b48c6 | 318 | |
6b691a5c | 319 | ASN1_STRING *ASN1_STRING_type_new(int type) |
0f113f3e MC |
320 | { |
321 | ASN1_STRING *ret; | |
322 | ||
64b25758 | 323 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
324 | if (ret == NULL) { |
325 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); | |
1f06acc0 | 326 | return NULL; |
0f113f3e | 327 | } |
0f113f3e | 328 | ret->type = type; |
1f06acc0 | 329 | return ret; |
0f113f3e | 330 | } |
d02b48c6 | 331 | |
6215f27a | 332 | void asn1_string_embed_free(ASN1_STRING *a, int embed) |
0f113f3e MC |
333 | { |
334 | if (a == NULL) | |
335 | return; | |
b548a1f1 | 336 | if (!(a->flags & ASN1_STRING_FLAG_NDEF)) |
0f113f3e | 337 | OPENSSL_free(a->data); |
6215f27a | 338 | if (embed == 0) |
47c9a1b5 | 339 | OPENSSL_free(a); |
0f113f3e | 340 | } |
d02b48c6 | 341 | |
6215f27a DSH |
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 | ||
a8ae0891 DSH |
349 | void ASN1_STRING_clear_free(ASN1_STRING *a) |
350 | { | |
0dfb9398 RS |
351 | if (a == NULL) |
352 | return; | |
353 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) | |
a8ae0891 DSH |
354 | OPENSSL_cleanse(a->data, a->length); |
355 | ASN1_STRING_free(a); | |
356 | } | |
357 | ||
6384e46d | 358 | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) |
0f113f3e MC |
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) | |
1f06acc0 | 366 | return a->type - b->type; |
0f113f3e | 367 | else |
1f06acc0 | 368 | return i; |
da42c2a3 | 369 | } else { |
1f06acc0 | 370 | return i; |
da42c2a3 | 371 | } |
0f113f3e | 372 | } |
d02b48c6 | 373 | |
6343829a | 374 | int ASN1_STRING_length(const ASN1_STRING *x) |
0f113f3e | 375 | { |
f422a514 | 376 | return x->length; |
0f113f3e | 377 | } |
08e9c1af | 378 | |
6343829a | 379 | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
0f113f3e | 380 | { |
f422a514 | 381 | x->length = len; |
0f113f3e | 382 | } |
08e9c1af | 383 | |
08275a29 | 384 | int ASN1_STRING_type(const ASN1_STRING *x) |
0f113f3e | 385 | { |
f422a514 | 386 | return x->type; |
0f113f3e MC |
387 | } |
388 | ||
17ebf85a DSH |
389 | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) |
390 | { | |
391 | return x->data; | |
392 | } | |
393 | ||
da42c2a3 | 394 | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
0f113f3e MC |
395 | unsigned char *ASN1_STRING_data(ASN1_STRING *x) |
396 | { | |
f422a514 | 397 | return x->data; |
0f113f3e | 398 | } |
17ebf85a | 399 | #endif |
da42c2a3 DDO |
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); | |
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 | } |