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