VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1j/crypto/asn1/asn1_lib.c@ 88461

Last change on this file since 88461 was 87984, checked in by vboxsync, 4 years ago

openssl-1.1.1j: Applied and adjusted our OpenSSL changes to 1.1.1j. bugref:9963

File size: 9.1 KB
Line 
1/*
2 * Copyright 1995-2020 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_local.h"
15
16static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
17 long max);
18static void asn1_put_length(unsigned char **pp, int length);
19
20static int _asn1_check_infinite_end(const unsigned char **p, long len)
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}
33
34int ASN1_check_infinite_end(unsigned char **p, long len)
35{
36 return _asn1_check_infinite_end((const unsigned char **)p, len);
37}
38
39int ASN1_const_check_infinite_end(const unsigned char **p, long len)
40{
41 return _asn1_check_infinite_end(p, len);
42}
43
44int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
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;
84 if (!asn1_get_length(&p, &inf, plength, max))
85 goto err;
86
87 if (inf && !(ret & V_ASN1_CONSTRUCTED))
88 goto err;
89
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/*
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 */
112static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
113 long max)
114{
115 const unsigned char *p = *pp;
116 unsigned long ret = 0;
117 int i;
118
119 if (max-- < 1)
120 return 0;
121 if (*p == 0x80) {
122 *inf = 1;
123 p++;
124 } else {
125 *inf = 0;
126 i = *p & 0x7f;
127 if (*p++ & 0x80) {
128 if (max < i + 1)
129 return 0;
130 /* Skip leading zeroes */
131 while (i > 0 && *p == 0) {
132 p++;
133 i--;
134 }
135 if (i > (int)sizeof(long))
136 return 0;
137 while (i > 0) {
138 ret <<= 8;
139 ret |= *p++;
140 i--;
141 }
142 if (ret > LONG_MAX)
143 return 0;
144 } else
145 ret = i;
146 }
147 *pp = p;
148 *rl = (long)ret;
149 return 1;
150}
151
152/*
153 * class 0 is constructed constructed == 2 for indefinite length constructed
154 */
155void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
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}
184
185int ASN1_put_eoc(unsigned char **pp)
186{
187 unsigned char *p = *pp;
188 *p++ = 0;
189 *p++ = 0;
190 *pp = p;
191 return 2;
192}
193
194static void asn1_put_length(unsigned char **pp, int length)
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}
214
215int ASN1_object_size(int constructed, int length, int tag)
216{
217 int ret = 1;
218 if (length < 0)
219 return -1;
220 if (tag >= 31) {
221 while (tag > 0) {
222 tag >>= 7;
223 ret++;
224 }
225 }
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 }
236 }
237 }
238 if (ret >= INT_MAX - length)
239 return -1;
240 return ret + length;
241}
242
243int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
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;
250 /* Copy flags but preserve embed value */
251 dst->flags &= ASN1_STRING_FLAG_EMBED;
252 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
253 return 1;
254}
255
256ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
257{
258 ASN1_STRING *ret;
259 if (!str)
260 return NULL;
261 ret = ASN1_STRING_new();
262 if (ret == NULL)
263 return NULL;
264 if (!ASN1_STRING_copy(ret, str)) {
265 ASN1_STRING_free(ret);
266 return NULL;
267 }
268 return ret;
269}
270
271int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
272{
273 unsigned char *c;
274 const char *data = _data;
275 size_t len;
276
277 if (len_in < 0) {
278 if (data == NULL)
279 return 0;
280 len = strlen(data);
281 } else {
282 len = (size_t)len_in;
283 }
284 /*
285 * Verify that the length fits within an integer for assignment to
286 * str->length below. The additional 1 is subtracted to allow for the
287 * '\0' terminator even though this isn't strictly necessary.
288 */
289 if (len > INT_MAX - 1) {
290 ASN1err(0, ASN1_R_TOO_LARGE);
291 return 0;
292 }
293 if ((size_t)str->length <= len || str->data == NULL) {
294 c = str->data;
295 str->data = OPENSSL_realloc(c, len + 1);
296 if (str->data == NULL) {
297 ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
298 str->data = c;
299 return 0;
300 }
301 }
302 str->length = len;
303 if (data != NULL) {
304 memcpy(str->data, data, len);
305 /* an allowance for strings :-) */
306 str->data[len] = '\0';
307 }
308 return 1;
309}
310
311void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
312{
313 OPENSSL_free(str->data);
314 str->data = data;
315 str->length = len;
316}
317
318ASN1_STRING *ASN1_STRING_new(void)
319{
320 return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
321}
322
323ASN1_STRING *ASN1_STRING_type_new(int type)
324{
325 ASN1_STRING *ret;
326
327 ret = OPENSSL_zalloc(sizeof(*ret));
328 if (ret == NULL) {
329 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
330 return NULL;
331 }
332 ret->type = type;
333 return ret;
334}
335
336void asn1_string_embed_free(ASN1_STRING *a, int embed)
337{
338 if (a == NULL)
339 return;
340 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
341 OPENSSL_free(a->data);
342 if (embed == 0)
343 OPENSSL_free(a);
344}
345
346void ASN1_STRING_free(ASN1_STRING *a)
347{
348 if (a == NULL)
349 return;
350 asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
351}
352
353void ASN1_STRING_clear_free(ASN1_STRING *a)
354{
355 if (a == NULL)
356 return;
357 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
358 OPENSSL_cleanse(a->data, a->length);
359 ASN1_STRING_free(a);
360}
361
362int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
363{
364 int i;
365
366 i = (a->length - b->length);
367 if (i == 0) {
368 i = memcmp(a->data, b->data, a->length);
369 if (i == 0)
370 return a->type - b->type;
371 else
372 return i;
373 } else
374 return i;
375}
376
377int ASN1_STRING_length(const ASN1_STRING *x)
378{
379 return x->length;
380}
381
382void ASN1_STRING_length_set(ASN1_STRING *x, int len)
383{
384 x->length = len;
385}
386
387int ASN1_STRING_type(const ASN1_STRING *x)
388{
389 return x->type;
390}
391
392const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
393{
394 return x->data;
395}
396
397# if OPENSSL_API_COMPAT < 0x10100000L
398unsigned char *ASN1_STRING_data(ASN1_STRING *x)
399{
400 return x->data;
401}
402#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette