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