/src/openssl/crypto/asn1/asn1_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <limits.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/asn1.h> |
14 | | #include "asn1_locl.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 | 0 | { |
22 | 0 | /* |
23 | 0 | * If there is 0 or 1 byte left, the length check should pick things up |
24 | 0 | */ |
25 | 0 | if (len <= 0) |
26 | 0 | return 1; |
27 | 0 | else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { |
28 | 0 | (*p) += 2; |
29 | 0 | return 1; |
30 | 0 | } |
31 | 0 | return 0; |
32 | 0 | } |
33 | | |
34 | | int ASN1_check_infinite_end(unsigned char **p, long len) |
35 | 0 | { |
36 | 0 | return _asn1_check_infinite_end((const unsigned char **)p, len); |
37 | 0 | } |
38 | | |
39 | | int ASN1_const_check_infinite_end(const unsigned char **p, long len) |
40 | 0 | { |
41 | 0 | return _asn1_check_infinite_end(p, len); |
42 | 0 | } |
43 | | |
44 | | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
45 | | int *pclass, long omax) |
46 | 5.24M | { |
47 | 5.24M | int i, ret; |
48 | 5.24M | long l; |
49 | 5.24M | const unsigned char *p = *pp; |
50 | 5.24M | int tag, xclass, inf; |
51 | 5.24M | long max = omax; |
52 | 5.24M | |
53 | 5.24M | if (!max) |
54 | 7 | goto err; |
55 | 5.24M | ret = (*p & V_ASN1_CONSTRUCTED); |
56 | 5.24M | xclass = (*p & V_ASN1_PRIVATE); |
57 | 5.24M | i = *p & V_ASN1_PRIMITIVE_TAG; |
58 | 5.24M | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ |
59 | 21.5k | p++; |
60 | 21.5k | if (--max == 0) |
61 | 2.75k | goto err; |
62 | 18.8k | l = 0; |
63 | 33.0k | while (*p & 0x80) { |
64 | 16.0k | l <<= 7L; |
65 | 16.0k | l |= *(p++) & 0x7f; |
66 | 16.0k | if (--max == 0) |
67 | 894 | goto err; |
68 | 15.1k | if (l > (INT_MAX >> 7L)) |
69 | 841 | goto err; |
70 | 15.1k | } |
71 | 18.8k | l <<= 7L; |
72 | 17.0k | l |= *(p++) & 0x7f; |
73 | 17.0k | tag = (int)l; |
74 | 17.0k | if (--max == 0) |
75 | 638 | goto err; |
76 | 5.22M | } else { |
77 | 5.22M | tag = i; |
78 | 5.22M | p++; |
79 | 5.22M | if (--max == 0) |
80 | 14.4k | goto err; |
81 | 5.22M | } |
82 | 5.22M | *ptag = tag; |
83 | 5.22M | *pclass = xclass; |
84 | 5.22M | if (!asn1_get_length(&p, &inf, plength, max)) |
85 | 5.44k | goto err; |
86 | 5.22M | |
87 | 5.22M | if (inf && !(ret & V_ASN1_CONSTRUCTED)) |
88 | 705 | goto err; |
89 | 5.22M | |
90 | 5.22M | if (*plength > (omax - (p - *pp))) { |
91 | 8.02k | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG); |
92 | 8.02k | /* |
93 | 8.02k | * Set this so that even if things are not long enough the values are |
94 | 8.02k | * set correctly |
95 | 8.02k | */ |
96 | 8.02k | ret |= 0x80; |
97 | 8.02k | } |
98 | 5.22M | *pp = p; |
99 | 5.22M | return ret | inf; |
100 | 25.7k | err: |
101 | 25.7k | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG); |
102 | 25.7k | return 0x80; |
103 | 5.22M | } |
104 | | |
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 | | */ |
112 | | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
113 | | long max) |
114 | 5.22M | { |
115 | 5.22M | const unsigned char *p = *pp; |
116 | 5.22M | unsigned long ret = 0; |
117 | 5.22M | int i; |
118 | 5.22M | |
119 | 5.22M | if (max-- < 1) |
120 | 0 | return 0; |
121 | 5.22M | if (*p == 0x80) { |
122 | 1.13M | *inf = 1; |
123 | 1.13M | p++; |
124 | 4.08M | } else { |
125 | 4.08M | *inf = 0; |
126 | 4.08M | i = *p & 0x7f; |
127 | 4.08M | if (*p++ & 0x80) { |
128 | 224k | if (max < i + 1) |
129 | 2.83k | return 0; |
130 | 222k | /* Skip leading zeroes */ |
131 | 434k | while (i > 0 && *p == 0) { |
132 | 212k | p++; |
133 | 212k | i--; |
134 | 212k | } |
135 | 222k | if (i > (int)sizeof(long)) |
136 | 1.07k | return 0; |
137 | 408k | while (i > 0) { |
138 | 187k | ret <<= 8; |
139 | 187k | ret |= *p++; |
140 | 187k | i--; |
141 | 187k | } |
142 | 221k | if (ret > LONG_MAX) |
143 | 221k | return 0; |
144 | 3.86M | } else |
145 | 3.86M | ret = i; |
146 | 4.08M | } |
147 | 5.22M | *pp = p; |
148 | 5.22M | *rl = (long)ret; |
149 | 5.22M | return 1; |
150 | 5.22M | } |
151 | | |
152 | | /* |
153 | | * class 0 is constructed constructed == 2 for indefinite length constructed |
154 | | */ |
155 | | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
156 | | int xclass) |
157 | 12.7k | { |
158 | 12.7k | unsigned char *p = *pp; |
159 | 12.7k | int i, ttag; |
160 | 12.7k | |
161 | 12.7k | i = (constructed) ? V_ASN1_CONSTRUCTED : 0; |
162 | 12.7k | i |= (xclass & V_ASN1_PRIVATE); |
163 | 12.7k | if (tag < 31) |
164 | 12.7k | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); |
165 | 0 | else { |
166 | 0 | *(p++) = i | V_ASN1_PRIMITIVE_TAG; |
167 | 0 | for (i = 0, ttag = tag; ttag > 0; i++) |
168 | 0 | ttag >>= 7; |
169 | 0 | ttag = i; |
170 | 0 | while (i-- > 0) { |
171 | 0 | p[i] = tag & 0x7f; |
172 | 0 | if (i != (ttag - 1)) |
173 | 0 | p[i] |= 0x80; |
174 | 0 | tag >>= 7; |
175 | 0 | } |
176 | 0 | p += ttag; |
177 | 0 | } |
178 | 12.7k | if (constructed == 2) |
179 | 0 | *(p++) = 0x80; |
180 | 12.7k | else |
181 | 12.7k | asn1_put_length(&p, length); |
182 | 12.7k | *pp = p; |
183 | 12.7k | } |
184 | | |
185 | | int ASN1_put_eoc(unsigned char **pp) |
186 | 0 | { |
187 | 0 | unsigned char *p = *pp; |
188 | 0 | *p++ = 0; |
189 | 0 | *p++ = 0; |
190 | 0 | *pp = p; |
191 | 0 | return 2; |
192 | 0 | } |
193 | | |
194 | | static void asn1_put_length(unsigned char **pp, int length) |
195 | 12.7k | { |
196 | 12.7k | unsigned char *p = *pp; |
197 | 12.7k | int i, l; |
198 | 12.7k | if (length <= 127) |
199 | 4.33k | *(p++) = (unsigned char)length; |
200 | 8.43k | else { |
201 | 8.43k | l = length; |
202 | 19.3k | for (i = 0; l > 0; i++) |
203 | 10.9k | l >>= 8; |
204 | 8.43k | *(p++) = i | 0x80; |
205 | 8.43k | l = i; |
206 | 19.3k | while (i-- > 0) { |
207 | 10.9k | p[i] = length & 0xff; |
208 | 10.9k | length >>= 8; |
209 | 10.9k | } |
210 | 8.43k | p += l; |
211 | 8.43k | } |
212 | 12.7k | *pp = p; |
213 | 12.7k | } |
214 | | |
215 | | int ASN1_object_size(int constructed, int length, int tag) |
216 | 34.0k | { |
217 | 34.0k | int ret = 1; |
218 | 34.0k | if (length < 0) |
219 | 0 | return -1; |
220 | 34.0k | if (tag >= 31) { |
221 | 0 | while (tag > 0) { |
222 | 0 | tag >>= 7; |
223 | 0 | ret++; |
224 | 0 | } |
225 | 0 | } |
226 | 34.0k | if (constructed == 2) { |
227 | 0 | ret += 3; |
228 | 34.0k | } else { |
229 | 34.0k | ret++; |
230 | 34.0k | if (length > 127) { |
231 | 21.0k | int tmplen = length; |
232 | 48.4k | while (tmplen > 0) { |
233 | 27.3k | tmplen >>= 8; |
234 | 27.3k | ret++; |
235 | 27.3k | } |
236 | 21.0k | } |
237 | 34.0k | } |
238 | 34.0k | if (ret >= INT_MAX - length) |
239 | 0 | return -1; |
240 | 34.0k | return ret + length; |
241 | 34.0k | } |
242 | | |
243 | | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) |
244 | 0 | { |
245 | 0 | if (str == NULL) |
246 | 0 | return 0; |
247 | 0 | dst->type = str->type; |
248 | 0 | if (!ASN1_STRING_set(dst, str->data, str->length)) |
249 | 0 | return 0; |
250 | 0 | /* Copy flags but preserve embed value */ |
251 | 0 | dst->flags &= ASN1_STRING_FLAG_EMBED; |
252 | 0 | dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED; |
253 | 0 | return 1; |
254 | 0 | } |
255 | | |
256 | | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) |
257 | 0 | { |
258 | 0 | ASN1_STRING *ret; |
259 | 0 | if (!str) |
260 | 0 | return NULL; |
261 | 0 | ret = ASN1_STRING_new(); |
262 | 0 | if (ret == NULL) |
263 | 0 | return NULL; |
264 | 0 | if (!ASN1_STRING_copy(ret, str)) { |
265 | 0 | ASN1_STRING_free(ret); |
266 | 0 | return NULL; |
267 | 0 | } |
268 | 0 | return ret; |
269 | 0 | } |
270 | | |
271 | | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) |
272 | 983k | { |
273 | 983k | unsigned char *c; |
274 | 983k | const char *data = _data; |
275 | 983k | |
276 | 983k | if (len < 0) { |
277 | 0 | if (data == NULL) |
278 | 0 | return 0; |
279 | 0 | else |
280 | 0 | len = strlen(data); |
281 | 0 | } |
282 | 983k | if ((str->length <= len) || (str->data == NULL)) { |
283 | 983k | c = str->data; |
284 | 983k | str->data = OPENSSL_realloc(c, len + 1); |
285 | 983k | if (str->data == NULL) { |
286 | 0 | ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); |
287 | 0 | str->data = c; |
288 | 0 | return 0; |
289 | 0 | } |
290 | 983k | } |
291 | 983k | str->length = len; |
292 | 983k | if (data != NULL) { |
293 | 672k | memcpy(str->data, data, len); |
294 | 672k | /* an allowance for strings :-) */ |
295 | 672k | str->data[len] = '\0'; |
296 | 672k | } |
297 | 983k | return 1; |
298 | 983k | } |
299 | | |
300 | | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) |
301 | 0 | { |
302 | 0 | OPENSSL_free(str->data); |
303 | 0 | str->data = data; |
304 | 0 | str->length = len; |
305 | 0 | } |
306 | | |
307 | | ASN1_STRING *ASN1_STRING_new(void) |
308 | 0 | { |
309 | 0 | return ASN1_STRING_type_new(V_ASN1_OCTET_STRING); |
310 | 0 | } |
311 | | |
312 | | ASN1_STRING *ASN1_STRING_type_new(int type) |
313 | 1.25M | { |
314 | 1.25M | ASN1_STRING *ret; |
315 | 1.25M | |
316 | 1.25M | ret = OPENSSL_zalloc(sizeof(*ret)); |
317 | 1.25M | if (ret == NULL) { |
318 | 0 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); |
319 | 0 | return NULL; |
320 | 0 | } |
321 | 1.25M | ret->type = type; |
322 | 1.25M | return ret; |
323 | 1.25M | } |
324 | | |
325 | | void asn1_string_embed_free(ASN1_STRING *a, int embed) |
326 | 1.25M | { |
327 | 1.25M | if (a == NULL) |
328 | 1.25M | return; |
329 | 1.25M | if (!(a->flags & ASN1_STRING_FLAG_NDEF)) |
330 | 1.25M | OPENSSL_free(a->data); |
331 | 1.25M | if (embed == 0) |
332 | 1.25M | OPENSSL_free(a); |
333 | 1.25M | } |
334 | | |
335 | | void ASN1_STRING_free(ASN1_STRING *a) |
336 | 594 | { |
337 | 594 | if (a == NULL) |
338 | 594 | return; |
339 | 594 | asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED); |
340 | 594 | } |
341 | | |
342 | | void ASN1_STRING_clear_free(ASN1_STRING *a) |
343 | 0 | { |
344 | 0 | if (a == NULL) |
345 | 0 | return; |
346 | 0 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) |
347 | 0 | OPENSSL_cleanse(a->data, a->length); |
348 | 0 | ASN1_STRING_free(a); |
349 | 0 | } |
350 | | |
351 | | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) |
352 | 0 | { |
353 | 0 | int i; |
354 | 0 |
|
355 | 0 | i = (a->length - b->length); |
356 | 0 | if (i == 0) { |
357 | 0 | i = memcmp(a->data, b->data, a->length); |
358 | 0 | if (i == 0) |
359 | 0 | return a->type - b->type; |
360 | 0 | else |
361 | 0 | return i; |
362 | 0 | } else |
363 | 0 | return i; |
364 | 0 | } |
365 | | |
366 | | int ASN1_STRING_length(const ASN1_STRING *x) |
367 | 17 | { |
368 | 17 | return x->length; |
369 | 17 | } |
370 | | |
371 | | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
372 | 0 | { |
373 | 0 | x->length = len; |
374 | 0 | } |
375 | | |
376 | | int ASN1_STRING_type(const ASN1_STRING *x) |
377 | 0 | { |
378 | 0 | return x->type; |
379 | 0 | } |
380 | | |
381 | | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) |
382 | 17 | { |
383 | 17 | return x->data; |
384 | 17 | } |
385 | | |
386 | | # if OPENSSL_API_COMPAT < 0x10100000L |
387 | | unsigned char *ASN1_STRING_data(ASN1_STRING *x) |
388 | 0 | { |
389 | 0 | return x->data; |
390 | 0 | } |
391 | | #endif |