]>
Commit | Line | Data |
---|---|---|
b1322259 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
b1322259 RS |
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 | |
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> |
6215f27a | 14 | #include "asn1_locl.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 | */ | |
25 | if (len <= 0) | |
1f06acc0 | 26 | return 1; |
0f113f3e MC |
27 | else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { |
28 | (*p) += 2; | |
1f06acc0 | 29 | return 1; |
0f113f3e | 30 | } |
1f06acc0 | 31 | return 0; |
0f113f3e | 32 | } |
d02b48c6 | 33 | |
6343829a | 34 | int ASN1_check_infinite_end(unsigned char **p, long len) |
0f113f3e MC |
35 | { |
36 | return _asn1_check_infinite_end((const unsigned char **)p, len); | |
37 | } | |
d02b48c6 | 38 | |
6343829a | 39 | int ASN1_const_check_infinite_end(const unsigned char **p, long len) |
0f113f3e MC |
40 | { |
41 | return _asn1_check_infinite_end(p, len); | |
42 | } | |
875a644a | 43 | |
6343829a | 44 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
0f113f3e MC |
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; | |
79c7f74d | 84 | if (!asn1_get_length(&p, &inf, plength, max)) |
0f113f3e MC |
85 | goto err; |
86 | ||
87 | if (inf && !(ret & V_ASN1_CONSTRUCTED)) | |
88 | goto err; | |
398e99fe | 89 | |
0f113f3e MC |
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; | |
1f06acc0 | 99 | return ret | inf; |
0f113f3e MC |
100 | err: |
101 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG); | |
1f06acc0 | 102 | return 0x80; |
0f113f3e MC |
103 | } |
104 | ||
a9a157e7 P |
105 | /* |
106 | * Decode a length field. | |
107 | * The short form is a single byte defining a length 0 - 127. | |
108 | * The long form is a byte 0 - 127 with the top bit set and this indicates | |
109 | * the number of following octets that contain the length. These octets | |
110 | * are stored most significant digit first. | |
111 | */ | |
0f113f3e | 112 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
79c7f74d | 113 | long max) |
0f113f3e MC |
114 | { |
115 | const unsigned char *p = *pp; | |
116 | unsigned long ret = 0; | |
a9a157e7 | 117 | int i; |
0f113f3e MC |
118 | |
119 | if (max-- < 1) | |
79c7f74d | 120 | return 0; |
0f113f3e MC |
121 | if (*p == 0x80) { |
122 | *inf = 1; | |
0f113f3e MC |
123 | p++; |
124 | } else { | |
125 | *inf = 0; | |
126 | i = *p & 0x7f; | |
a9a157e7 P |
127 | if (*p++ & 0x80) { |
128 | if (max < i + 1) | |
0f113f3e MC |
129 | return 0; |
130 | /* Skip leading zeroes */ | |
a9a157e7 | 131 | while (i > 0 && *p == 0) { |
0f113f3e MC |
132 | p++; |
133 | i--; | |
134 | } | |
a9a157e7 | 135 | if (i > (int)sizeof(long)) |
0f113f3e | 136 | return 0; |
a9a157e7 P |
137 | while (i > 0) { |
138 | ret <<= 8; | |
139 | ret |= *p++; | |
140 | i--; | |
0f113f3e | 141 | } |
a9a157e7 P |
142 | if (ret > LONG_MAX) |
143 | return 0; | |
0f113f3e MC |
144 | } else |
145 | ret = i; | |
146 | } | |
0f113f3e MC |
147 | *pp = p; |
148 | *rl = (long)ret; | |
79c7f74d | 149 | return 1; |
0f113f3e MC |
150 | } |
151 | ||
152 | /* | |
153 | * class 0 is constructed constructed == 2 for indefinite length constructed | |
154 | */ | |
6343829a | 155 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
0f113f3e MC |
156 | int xclass) |
157 | { | |
158 | unsigned char *p = *pp; | |
159 | int i, ttag; | |
160 | ||
161 | i = (constructed) ? V_ASN1_CONSTRUCTED : 0; | |
162 | i |= (xclass & V_ASN1_PRIVATE); | |
163 | if (tag < 31) | |
164 | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); | |
165 | else { | |
166 | *(p++) = i | V_ASN1_PRIMITIVE_TAG; | |
167 | for (i = 0, ttag = tag; ttag > 0; i++) | |
168 | ttag >>= 7; | |
169 | ttag = i; | |
170 | while (i-- > 0) { | |
171 | p[i] = tag & 0x7f; | |
172 | if (i != (ttag - 1)) | |
173 | p[i] |= 0x80; | |
174 | tag >>= 7; | |
175 | } | |
176 | p += ttag; | |
177 | } | |
178 | if (constructed == 2) | |
179 | *(p++) = 0x80; | |
180 | else | |
181 | asn1_put_length(&p, length); | |
182 | *pp = p; | |
183 | } | |
d02b48c6 | 184 | |
230fd6b7 | 185 | int ASN1_put_eoc(unsigned char **pp) |
0f113f3e MC |
186 | { |
187 | unsigned char *p = *pp; | |
188 | *p++ = 0; | |
189 | *p++ = 0; | |
190 | *pp = p; | |
191 | return 2; | |
192 | } | |
230fd6b7 | 193 | |
6b691a5c | 194 | static void asn1_put_length(unsigned char **pp, int length) |
0f113f3e MC |
195 | { |
196 | unsigned char *p = *pp; | |
197 | int i, l; | |
198 | if (length <= 127) | |
199 | *(p++) = (unsigned char)length; | |
200 | else { | |
201 | l = length; | |
202 | for (i = 0; l > 0; i++) | |
203 | l >>= 8; | |
204 | *(p++) = i | 0x80; | |
205 | l = i; | |
206 | while (i-- > 0) { | |
207 | p[i] = length & 0xff; | |
208 | length >>= 8; | |
209 | } | |
210 | p += l; | |
211 | } | |
212 | *pp = p; | |
213 | } | |
d02b48c6 | 214 | |
6343829a | 215 | int ASN1_object_size(int constructed, int length, int tag) |
0f113f3e | 216 | { |
e9f17097 DSH |
217 | int ret = 1; |
218 | if (length < 0) | |
219 | return -1; | |
0f113f3e MC |
220 | if (tag >= 31) { |
221 | while (tag > 0) { | |
222 | tag >>= 7; | |
223 | ret++; | |
224 | } | |
225 | } | |
e9f17097 DSH |
226 | if (constructed == 2) { |
227 | ret += 3; | |
228 | } else { | |
229 | ret++; | |
230 | if (length > 127) { | |
231 | int tmplen = length; | |
232 | while (tmplen > 0) { | |
233 | tmplen >>= 8; | |
234 | ret++; | |
235 | } | |
0f113f3e MC |
236 | } |
237 | } | |
e9f17097 DSH |
238 | if (ret >= INT_MAX - length) |
239 | return -1; | |
240 | return ret + length; | |
0f113f3e | 241 | } |
d02b48c6 | 242 | |
18f54773 | 243 | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) |
0f113f3e MC |
244 | { |
245 | if (str == NULL) | |
246 | return 0; | |
247 | dst->type = str->type; | |
248 | if (!ASN1_STRING_set(dst, str->data, str->length)) | |
249 | return 0; | |
4002da0f DSH |
250 | /* Copy flags but preserve embed value */ |
251 | dst->flags &= ASN1_STRING_FLAG_EMBED; | |
252 | dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED; | |
0f113f3e MC |
253 | return 1; |
254 | } | |
18f54773 | 255 | |
6384e46d | 256 | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) |
0f113f3e MC |
257 | { |
258 | ASN1_STRING *ret; | |
259 | if (!str) | |
260 | return NULL; | |
261 | ret = ASN1_STRING_new(); | |
90945fa3 | 262 | if (ret == NULL) |
0f113f3e MC |
263 | return NULL; |
264 | if (!ASN1_STRING_copy(ret, str)) { | |
265 | ASN1_STRING_free(ret); | |
266 | return NULL; | |
267 | } | |
268 | return ret; | |
269 | } | |
d02b48c6 | 270 | |
6343829a | 271 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) |
0f113f3e MC |
272 | { |
273 | unsigned char *c; | |
274 | const char *data = _data; | |
275 | ||
276 | if (len < 0) { | |
277 | if (data == NULL) | |
1f06acc0 | 278 | return 0; |
0f113f3e MC |
279 | else |
280 | len = strlen(data); | |
281 | } | |
a73be798 | 282 | if ((str->length <= len) || (str->data == NULL)) { |
0f113f3e | 283 | c = str->data; |
2d29e2df | 284 | str->data = OPENSSL_realloc(c, len + 1); |
0f113f3e MC |
285 | if (str->data == NULL) { |
286 | ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); | |
287 | str->data = c; | |
1f06acc0 | 288 | return 0; |
0f113f3e MC |
289 | } |
290 | } | |
291 | str->length = len; | |
292 | if (data != NULL) { | |
293 | memcpy(str->data, data, len); | |
294 | /* an allowance for strings :-) */ | |
295 | str->data[len] = '\0'; | |
296 | } | |
1f06acc0 | 297 | return 1; |
0f113f3e | 298 | } |
d02b48c6 | 299 | |
6343829a | 300 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) |
0f113f3e | 301 | { |
b548a1f1 | 302 | OPENSSL_free(str->data); |
0f113f3e MC |
303 | str->data = data; |
304 | str->length = len; | |
305 | } | |
399a6f0b | 306 | |
6b691a5c | 307 | ASN1_STRING *ASN1_STRING_new(void) |
0f113f3e | 308 | { |
1f06acc0 | 309 | return ASN1_STRING_type_new(V_ASN1_OCTET_STRING); |
0f113f3e | 310 | } |
d02b48c6 | 311 | |
6b691a5c | 312 | ASN1_STRING *ASN1_STRING_type_new(int type) |
0f113f3e MC |
313 | { |
314 | ASN1_STRING *ret; | |
315 | ||
64b25758 | 316 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
317 | if (ret == NULL) { |
318 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); | |
1f06acc0 | 319 | return NULL; |
0f113f3e | 320 | } |
0f113f3e | 321 | ret->type = type; |
1f06acc0 | 322 | return ret; |
0f113f3e | 323 | } |
d02b48c6 | 324 | |
6215f27a | 325 | void asn1_string_embed_free(ASN1_STRING *a, int embed) |
0f113f3e MC |
326 | { |
327 | if (a == NULL) | |
328 | return; | |
b548a1f1 | 329 | if (!(a->flags & ASN1_STRING_FLAG_NDEF)) |
0f113f3e | 330 | OPENSSL_free(a->data); |
6215f27a | 331 | if (embed == 0) |
47c9a1b5 | 332 | OPENSSL_free(a); |
0f113f3e | 333 | } |
d02b48c6 | 334 | |
6215f27a DSH |
335 | void ASN1_STRING_free(ASN1_STRING *a) |
336 | { | |
337 | if (a == NULL) | |
338 | return; | |
339 | asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED); | |
340 | } | |
341 | ||
a8ae0891 DSH |
342 | void ASN1_STRING_clear_free(ASN1_STRING *a) |
343 | { | |
0dfb9398 RS |
344 | if (a == NULL) |
345 | return; | |
346 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) | |
a8ae0891 DSH |
347 | OPENSSL_cleanse(a->data, a->length); |
348 | ASN1_STRING_free(a); | |
349 | } | |
350 | ||
6384e46d | 351 | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) |
0f113f3e MC |
352 | { |
353 | int i; | |
354 | ||
355 | i = (a->length - b->length); | |
356 | if (i == 0) { | |
357 | i = memcmp(a->data, b->data, a->length); | |
358 | if (i == 0) | |
1f06acc0 | 359 | return a->type - b->type; |
0f113f3e | 360 | else |
1f06acc0 | 361 | return i; |
0f113f3e | 362 | } else |
1f06acc0 | 363 | return i; |
0f113f3e | 364 | } |
d02b48c6 | 365 | |
6343829a | 366 | int ASN1_STRING_length(const ASN1_STRING *x) |
0f113f3e | 367 | { |
f422a514 | 368 | return x->length; |
0f113f3e | 369 | } |
08e9c1af | 370 | |
6343829a | 371 | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
0f113f3e | 372 | { |
f422a514 | 373 | x->length = len; |
0f113f3e | 374 | } |
08e9c1af | 375 | |
08275a29 | 376 | int ASN1_STRING_type(const ASN1_STRING *x) |
0f113f3e | 377 | { |
f422a514 | 378 | return x->type; |
0f113f3e MC |
379 | } |
380 | ||
17ebf85a DSH |
381 | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) |
382 | { | |
383 | return x->data; | |
384 | } | |
385 | ||
fcd2d5a6 | 386 | # if !OPENSSL_API_1_1_0 |
0f113f3e MC |
387 | unsigned char *ASN1_STRING_data(ASN1_STRING *x) |
388 | { | |
f422a514 | 389 | return x->data; |
0f113f3e | 390 | } |
17ebf85a | 391 | #endif |