VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1l/crypto/asn1/a_object.c@ 91772

最後變更 在這個檔案從91772是 91772,由 vboxsync 提交於 3 年 前

openssl-1.1.1l: Applied and adjusted our OpenSSL changes to 1.1.1l. bugref:10126

檔案大小: 10.3 KB
 
1/*
2 * Copyright 1995-2021 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 "crypto/ctype.h"
13#include "internal/cryptlib.h"
14#include <openssl/buffer.h>
15#include <openssl/asn1.h>
16#include <openssl/objects.h>
17#include <openssl/bn.h>
18#include "crypto/asn1.h"
19#include "asn1_local.h"
20
21int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
22{
23 unsigned char *p, *allocated = NULL;
24 int objsize;
25
26 if ((a == NULL) || (a->data == NULL))
27 return 0;
28
29 objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
30 if (pp == NULL || objsize == -1)
31 return objsize;
32
33 if (*pp == NULL) {
34 if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
35 ASN1err(ASN1_F_I2D_ASN1_OBJECT, ERR_R_MALLOC_FAILURE);
36 return 0;
37 }
38 } else {
39 p = *pp;
40 }
41
42 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
43 memcpy(p, a->data, a->length);
44
45 /*
46 * If a new buffer was allocated, just return it back.
47 * If not, return the incremented buffer pointer.
48 */
49 *pp = allocated != NULL ? allocated : p + a->length;
50 return objsize;
51}
52
53int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
54{
55 int i, first, len = 0, c, use_bn;
56 char ftmp[24], *tmp = ftmp;
57 int tmpsize = sizeof(ftmp);
58 const char *p;
59 unsigned long l;
60 BIGNUM *bl = NULL;
61
62 if (num == 0)
63 return 0;
64 else if (num == -1)
65 num = strlen(buf);
66
67 p = buf;
68 c = *(p++);
69 num--;
70 if ((c >= '0') && (c <= '2')) {
71 first = c - '0';
72 } else {
73 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
74 goto err;
75 }
76
77 if (num <= 0) {
78 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
79 goto err;
80 }
81 c = *(p++);
82 num--;
83 for (;;) {
84 if (num <= 0)
85 break;
86 if ((c != '.') && (c != ' ')) {
87 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
88 goto err;
89 }
90 l = 0;
91 use_bn = 0;
92 for (;;) {
93 if (num <= 0)
94 break;
95 num--;
96 c = *(p++);
97 if ((c == ' ') || (c == '.'))
98 break;
99 if (!ossl_isdigit(c)) {
100 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
101 goto err;
102 }
103 if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
104 use_bn = 1;
105 if (bl == NULL)
106 bl = BN_new();
107 if (bl == NULL || !BN_set_word(bl, l))
108 goto err;
109 }
110 if (use_bn) {
111 if (!BN_mul_word(bl, 10L)
112 || !BN_add_word(bl, c - '0'))
113 goto err;
114 } else
115 l = l * 10L + (long)(c - '0');
116 }
117 if (len == 0) {
118 if ((first < 2) && (l >= 40)) {
119 ASN1err(ASN1_F_A2D_ASN1_OBJECT,
120 ASN1_R_SECOND_NUMBER_TOO_LARGE);
121 goto err;
122 }
123 if (use_bn) {
124 if (!BN_add_word(bl, first * 40))
125 goto err;
126 } else
127 l += (long)first *40;
128 }
129 i = 0;
130 if (use_bn) {
131 int blsize;
132 blsize = BN_num_bits(bl);
133 blsize = (blsize + 6) / 7;
134 if (blsize > tmpsize) {
135 if (tmp != ftmp)
136 OPENSSL_free(tmp);
137 tmpsize = blsize + 32;
138 tmp = OPENSSL_malloc(tmpsize);
139 if (tmp == NULL)
140 goto err;
141 }
142 while (blsize--) {
143 BN_ULONG t = BN_div_word(bl, 0x80L);
144 if (t == (BN_ULONG)-1)
145 goto err;
146 tmp[i++] = (unsigned char)t;
147 }
148 } else {
149
150 for (;;) {
151 tmp[i++] = (unsigned char)l & 0x7f;
152 l >>= 7L;
153 if (l == 0L)
154 break;
155 }
156
157 }
158 if (out != NULL) {
159 if (len + i > olen) {
160 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
161 goto err;
162 }
163 while (--i > 0)
164 out[len++] = tmp[i] | 0x80;
165 out[len++] = tmp[0];
166 } else
167 len += i;
168 }
169 if (tmp != ftmp)
170 OPENSSL_free(tmp);
171 BN_free(bl);
172 return len;
173 err:
174 if (tmp != ftmp)
175 OPENSSL_free(tmp);
176 BN_free(bl);
177 return 0;
178}
179
180int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
181{
182 return OBJ_obj2txt(buf, buf_len, a, 0);
183}
184
185int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
186{
187 char buf[80], *p = buf;
188 int i;
189
190 if ((a == NULL) || (a->data == NULL))
191 return BIO_write(bp, "NULL", 4);
192 i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
193 if (i > (int)(sizeof(buf) - 1)) {
194 if ((p = OPENSSL_malloc(i + 1)) == NULL) {
195 ASN1err(ASN1_F_I2A_ASN1_OBJECT, ERR_R_MALLOC_FAILURE);
196 return -1;
197 }
198 i2t_ASN1_OBJECT(p, i + 1, a);
199 }
200 if (i <= 0) {
201 i = BIO_write(bp, "<INVALID>", 9);
202 i += BIO_dump(bp, (const char *)a->data, a->length);
203 return i;
204 }
205 BIO_write(bp, p, i);
206 if (p != buf)
207 OPENSSL_free(p);
208 return i;
209}
210
211ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
212 long length)
213{
214 const unsigned char *p;
215 long len;
216 int tag, xclass;
217 int inf, i;
218 ASN1_OBJECT *ret = NULL;
219 p = *pp;
220 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
221 if (inf & 0x80) {
222 i = ASN1_R_BAD_OBJECT_HEADER;
223 goto err;
224 }
225
226 if (tag != V_ASN1_OBJECT) {
227 i = ASN1_R_EXPECTING_AN_OBJECT;
228 goto err;
229 }
230 ret = c2i_ASN1_OBJECT(a, &p, len);
231 if (ret)
232 *pp = p;
233 return ret;
234 err:
235 ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
236 return NULL;
237}
238
239ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
240 long len)
241{
242 ASN1_OBJECT *ret = NULL, tobj;
243 const unsigned char *p;
244 unsigned char *data;
245 int i, length;
246
247 /*
248 * Sanity check OID encoding. Need at least one content octet. MSB must
249 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
250 * see: X.690 8.19.2
251 */
252 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
253 p[len - 1] & 0x80) {
254 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
255 return NULL;
256 }
257 /* Now 0 < len <= INT_MAX, so the cast is safe. */
258 length = (int)len;
259 /*
260 * Try to lookup OID in table: these are all valid encodings so if we get
261 * a match we know the OID is valid.
262 */
263 tobj.nid = NID_undef;
264 tobj.data = p;
265 tobj.length = length;
266 tobj.flags = 0;
267 i = OBJ_obj2nid(&tobj);
268 if (i != NID_undef) {
269 /*
270 * Return shared registered OID object: this improves efficiency
271 * because we don't have to return a dynamically allocated OID
272 * and NID lookups can use the cached value.
273 */
274 ret = OBJ_nid2obj(i);
275 if (a) {
276 ASN1_OBJECT_free(*a);
277 *a = ret;
278 }
279 *pp += len;
280 return ret;
281 }
282 for (i = 0; i < length; i++, p++) {
283 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
284 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
285 return NULL;
286 }
287 }
288
289 if ((a == NULL) || ((*a) == NULL) ||
290 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
291 if ((ret = ASN1_OBJECT_new()) == NULL)
292 return NULL;
293 } else {
294 ret = (*a);
295 }
296
297 p = *pp;
298 /* detach data from object */
299 data = (unsigned char *)ret->data;
300 ret->data = NULL;
301 /* once detached we can change it */
302 if ((data == NULL) || (ret->length < length)) {
303 ret->length = 0;
304 OPENSSL_free(data);
305 data = OPENSSL_malloc(length);
306 if (data == NULL) {
307 i = ERR_R_MALLOC_FAILURE;
308 goto err;
309 }
310 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
311 }
312 memcpy(data, p, length);
313 /* If there are dynamic strings, free them here, and clear the flag */
314 if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
315 OPENSSL_free((char *)ret->sn);
316 OPENSSL_free((char *)ret->ln);
317 ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
318 }
319 /* reattach data to object, after which it remains const */
320 ret->data = data;
321 ret->length = length;
322 ret->sn = NULL;
323 ret->ln = NULL;
324 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
325 p += length;
326
327 if (a != NULL)
328 (*a) = ret;
329 *pp = p;
330 return ret;
331 err:
332 ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
333 if ((a == NULL) || (*a != ret))
334 ASN1_OBJECT_free(ret);
335 return NULL;
336}
337
338ASN1_OBJECT *ASN1_OBJECT_new(void)
339{
340 ASN1_OBJECT *ret;
341
342 ret = OPENSSL_zalloc(sizeof(*ret));
343 if (ret == NULL) {
344 ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
345 return NULL;
346 }
347 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
348 return ret;
349}
350
351void ASN1_OBJECT_free(ASN1_OBJECT *a)
352{
353 if (a == NULL)
354 return;
355 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
356#ifndef CONST_STRICT /* disable purely for compile-time strict
357 * const checking. Doing this on a "real"
358 * compile will cause memory leaks */
359 OPENSSL_free((void*)a->sn);
360 OPENSSL_free((void*)a->ln);
361#endif
362 a->sn = a->ln = NULL;
363 }
364 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
365 OPENSSL_free((void*)a->data);
366 a->data = NULL;
367 a->length = 0;
368 }
369 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
370 OPENSSL_free(a);
371}
372
373ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
374 const char *sn, const char *ln)
375{
376 ASN1_OBJECT o;
377
378 o.sn = sn;
379 o.ln = ln;
380 o.data = data;
381 o.nid = nid;
382 o.length = len;
383 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
384 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
385 return OBJ_dup(&o);
386}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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