]>
Commit | Line | Data |
---|---|---|
7bf7a6d0 MTL |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
b6f94dbe | 3 | * |
7bf7a6d0 MTL |
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 | |
b2d0e06f MG |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <limits.h> | |
7bf7a6d0 | 12 | #include "internal/cryptlib.h" |
b2d0e06f | 13 | #include <openssl/asn1.h> |
7bf7a6d0 | 14 | #include "asn1_locl.h" |
b2d0e06f | 15 | |
d3819813 | 16 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
62f0afa2 | 17 | long max); |
b2d0e06f | 18 | static void asn1_put_length(unsigned char **pp, int length); |
b2d0e06f MG |
19 | |
20 | static int _asn1_check_infinite_end(const unsigned char **p, long len) | |
d3819813 MTL |
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 | } | |
b2d0e06f MG |
33 | |
34 | int ASN1_check_infinite_end(unsigned char **p, long len) | |
d3819813 MTL |
35 | { |
36 | return _asn1_check_infinite_end((const unsigned char **)p, len); | |
37 | } | |
b2d0e06f MG |
38 | |
39 | int ASN1_const_check_infinite_end(const unsigned char **p, long len) | |
d3819813 MTL |
40 | { |
41 | return _asn1_check_infinite_end(p, len); | |
42 | } | |
b2d0e06f MG |
43 | |
44 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, | |
d3819813 MTL |
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; | |
62f0afa2 | 84 | if (!asn1_get_length(&p, &inf, plength, max)) |
d3819813 MTL |
85 | goto err; |
86 | ||
87 | if (inf && !(ret & V_ASN1_CONSTRUCTED)) | |
88 | goto err; | |
5cbe75a3 | 89 | |
d3819813 MTL |
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, | |
62f0afa2 | 106 | long max) |
d3819813 MTL |
107 | { |
108 | const unsigned char *p = *pp; | |
109 | unsigned long ret = 0; | |
62f0afa2 | 110 | unsigned long i; |
d3819813 MTL |
111 | |
112 | if (max-- < 1) | |
62f0afa2 | 113 | return 0; |
d3819813 MTL |
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) { | |
7bf7a6d0 MTL |
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)) | |
d3819813 | 130 | return 0; |
d3819813 MTL |
131 | while (i-- > 0) { |
132 | ret <<= 8L; | |
133 | ret |= *(p++); | |
d3819813 MTL |
134 | } |
135 | } else | |
136 | ret = i; | |
137 | } | |
138 | if (ret > LONG_MAX) | |
139 | return 0; | |
140 | *pp = p; | |
141 | *rl = (long)ret; | |
62f0afa2 | 142 | return 1; |
d3819813 MTL |
143 | } |
144 | ||
145 | /* | |
146 | * class 0 is constructed constructed == 2 for indefinite length constructed | |
147 | */ | |
b2d0e06f | 148 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
d3819813 MTL |
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 | } | |
b2d0e06f MG |
177 | |
178 | int ASN1_put_eoc(unsigned char **pp) | |
d3819813 MTL |
179 | { |
180 | unsigned char *p = *pp; | |
181 | *p++ = 0; | |
182 | *p++ = 0; | |
183 | *pp = p; | |
184 | return 2; | |
185 | } | |
b2d0e06f MG |
186 | |
187 | static void asn1_put_length(unsigned char **pp, int length) | |
d3819813 MTL |
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 | } | |
b2d0e06f MG |
207 | |
208 | int ASN1_object_size(int constructed, int length, int tag) | |
d3819813 | 209 | { |
f4173af1 MTL |
210 | int ret = 1; |
211 | if (length < 0) | |
212 | return -1; | |
d3819813 MTL |
213 | if (tag >= 31) { |
214 | while (tag > 0) { | |
215 | tag >>= 7; | |
216 | ret++; | |
217 | } | |
218 | } | |
f4173af1 MTL |
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 | } | |
d3819813 MTL |
229 | } |
230 | } | |
f4173af1 MTL |
231 | if (ret >= INT_MAX - length) |
232 | return -1; | |
233 | return ret + length; | |
d3819813 MTL |
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; | |
7bf7a6d0 MTL |
243 | /* Copy flags but preserve embed value */ |
244 | dst->flags &= ASN1_STRING_FLAG_EMBED; | |
245 | dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED; | |
d3819813 MTL |
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(); | |
7bf7a6d0 | 255 | if (ret == NULL) |
d3819813 MTL |
256 | return NULL; |
257 | if (!ASN1_STRING_copy(ret, str)) { | |
258 | ASN1_STRING_free(ret); | |
259 | return NULL; | |
260 | } | |
261 | return ret; | |
262 | } | |
b2d0e06f MG |
263 | |
264 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) | |
d3819813 MTL |
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 | } | |
f4173af1 | 275 | if ((str->length <= len) || (str->data == NULL)) { |
d3819813 | 276 | c = str->data; |
7bf7a6d0 | 277 | str->data = OPENSSL_realloc(c, len + 1); |
d3819813 MTL |
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 | } | |
b2d0e06f MG |
292 | |
293 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) | |
d3819813 | 294 | { |
7bf7a6d0 | 295 | OPENSSL_free(str->data); |
d3819813 MTL |
296 | str->data = data; |
297 | str->length = len; | |
298 | } | |
b2d0e06f MG |
299 | |
300 | ASN1_STRING *ASN1_STRING_new(void) | |
d3819813 MTL |
301 | { |
302 | return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); | |
303 | } | |
b2d0e06f MG |
304 | |
305 | ASN1_STRING *ASN1_STRING_type_new(int type) | |
d3819813 MTL |
306 | { |
307 | ASN1_STRING *ret; | |
308 | ||
7bf7a6d0 | 309 | ret = OPENSSL_zalloc(sizeof(*ret)); |
d3819813 MTL |
310 | if (ret == NULL) { |
311 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); | |
312 | return (NULL); | |
313 | } | |
d3819813 | 314 | ret->type = type; |
d3819813 MTL |
315 | return (ret); |
316 | } | |
b2d0e06f | 317 | |
7bf7a6d0 | 318 | void asn1_string_embed_free(ASN1_STRING *a, int embed) |
f4173af1 MTL |
319 | { |
320 | if (a == NULL) | |
321 | return; | |
7bf7a6d0 | 322 | if (!(a->flags & ASN1_STRING_FLAG_NDEF)) |
b6f94dbe | 323 | OPENSSL_free(a->data); |
7bf7a6d0 MTL |
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); | |
d3819813 MTL |
333 | } |
334 | ||
335 | void ASN1_STRING_clear_free(ASN1_STRING *a) | |
336 | { | |
7bf7a6d0 MTL |
337 | if (a == NULL) |
338 | return; | |
339 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) | |
d3819813 MTL |
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 | } | |
b2d0e06f | 358 | |
7bf7a6d0 | 359 | int ASN1_STRING_length(const ASN1_STRING *x) |
d3819813 | 360 | { |
7bf7a6d0 | 361 | return x->length; |
d3819813 | 362 | } |
b2d0e06f | 363 | |
7bf7a6d0 | 364 | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
d3819813 | 365 | { |
7bf7a6d0 | 366 | x->length = len; |
d3819813 | 367 | } |
b2d0e06f | 368 | |
7bf7a6d0 | 369 | int ASN1_STRING_type(const ASN1_STRING *x) |
d3819813 | 370 | { |
7bf7a6d0 | 371 | return x->type; |
d3819813 | 372 | } |
b2d0e06f | 373 | |
7bf7a6d0 | 374 | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) |
d3819813 | 375 | { |
7bf7a6d0 | 376 | return x->data; |
d3819813 MTL |
377 | } |
378 | ||
7bf7a6d0 | 379 | # if OPENSSL_API_COMPAT < 0x10100000L |
d3819813 MTL |
380 | unsigned char *ASN1_STRING_data(ASN1_STRING *x) |
381 | { | |
7bf7a6d0 | 382 | return x->data; |
d3819813 | 383 | } |
7bf7a6d0 | 384 | #endif |