/src/libressl/crypto/asn1/a_object.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: a_object.c,v 1.48 2022/05/13 16:32:10 tb Exp $ */ |
2 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | | * All rights reserved. |
4 | | * |
5 | | * This package is an SSL implementation written |
6 | | * by Eric Young (eay@cryptsoft.com). |
7 | | * The implementation was written so as to conform with Netscapes SSL. |
8 | | * |
9 | | * This library is free for commercial and non-commercial use as long as |
10 | | * the following conditions are aheared to. The following conditions |
11 | | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | | * included with this distribution is covered by the same copyright terms |
14 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | | * |
16 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | | * the code are not to be removed. |
18 | | * If this package is used in a product, Eric Young should be given attribution |
19 | | * as the author of the parts of the library used. |
20 | | * This can be in the form of a textual message at program startup or |
21 | | * in documentation (online or textual) provided with the package. |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions |
25 | | * are met: |
26 | | * 1. Redistributions of source code must retain the copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * 2. Redistributions in binary form must reproduce the above copyright |
29 | | * notice, this list of conditions and the following disclaimer in the |
30 | | * documentation and/or other materials provided with the distribution. |
31 | | * 3. All advertising materials mentioning features or use of this software |
32 | | * must display the following acknowledgement: |
33 | | * "This product includes cryptographic software written by |
34 | | * Eric Young (eay@cryptsoft.com)" |
35 | | * The word 'cryptographic' can be left out if the rouines from the library |
36 | | * being used are not cryptographic related :-). |
37 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | | * the apps directory (application code) you must include an acknowledgement: |
39 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | | * SUCH DAMAGE. |
52 | | * |
53 | | * The licence and distribution terms for any publically available version or |
54 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | | * copied and put under another distribution licence |
56 | | * [including the GNU Public Licence.] |
57 | | */ |
58 | | |
59 | | #include <limits.h> |
60 | | #include <stdio.h> |
61 | | #include <string.h> |
62 | | |
63 | | #include <openssl/asn1.h> |
64 | | #include <openssl/asn1t.h> |
65 | | #include <openssl/err.h> |
66 | | #include <openssl/buffer.h> |
67 | | #include <openssl/objects.h> |
68 | | |
69 | | #include "asn1_locl.h" |
70 | | |
71 | | const ASN1_ITEM ASN1_OBJECT_it = { |
72 | | .itype = ASN1_ITYPE_PRIMITIVE, |
73 | | .utype = V_ASN1_OBJECT, |
74 | | .sname = "ASN1_OBJECT", |
75 | | }; |
76 | | |
77 | | ASN1_OBJECT * |
78 | | ASN1_OBJECT_new(void) |
79 | 28.9k | { |
80 | 28.9k | ASN1_OBJECT *a; |
81 | | |
82 | 28.9k | if ((a = calloc(1, sizeof(ASN1_OBJECT))) == NULL) { |
83 | 0 | ASN1error(ERR_R_MALLOC_FAILURE); |
84 | 0 | return (NULL); |
85 | 0 | } |
86 | 28.9k | a->flags = ASN1_OBJECT_FLAG_DYNAMIC; |
87 | | |
88 | 28.9k | return a; |
89 | 28.9k | } |
90 | | |
91 | | void |
92 | | ASN1_OBJECT_free(ASN1_OBJECT *a) |
93 | 61.2k | { |
94 | 61.2k | if (a == NULL) |
95 | 38 | return; |
96 | 61.2k | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { |
97 | 8.82k | free((void *)a->sn); |
98 | 8.82k | free((void *)a->ln); |
99 | 8.82k | a->sn = a->ln = NULL; |
100 | 8.82k | } |
101 | 61.2k | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { |
102 | 28.9k | freezero((void *)a->data, a->length); |
103 | 28.9k | a->data = NULL; |
104 | 28.9k | a->length = 0; |
105 | 28.9k | } |
106 | 61.2k | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) |
107 | 28.9k | free(a); |
108 | 61.2k | } |
109 | | |
110 | | ASN1_OBJECT * |
111 | | ASN1_OBJECT_create(int nid, unsigned char *data, int len, |
112 | | const char *sn, const char *ln) |
113 | 0 | { |
114 | 0 | ASN1_OBJECT o; |
115 | |
|
116 | 0 | o.sn = sn; |
117 | 0 | o.ln = ln; |
118 | 0 | o.data = data; |
119 | 0 | o.nid = nid; |
120 | 0 | o.length = len; |
121 | 0 | o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
122 | 0 | ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
123 | 0 | return (OBJ_dup(&o)); |
124 | 0 | } |
125 | | |
126 | | static int |
127 | | oid_add_arc(CBB *cbb, uint64_t arc) |
128 | 0 | { |
129 | 0 | int started = 0; |
130 | 0 | uint8_t val; |
131 | 0 | int i; |
132 | |
|
133 | 0 | for (i = (sizeof(arc) * 8) / 7; i >= 0; i--) { |
134 | 0 | val = (arc >> (i * 7)) & 0x7f; |
135 | 0 | if (!started && i != 0 && val == 0) |
136 | 0 | continue; |
137 | 0 | if (i > 0) |
138 | 0 | val |= 0x80; |
139 | 0 | if (!CBB_add_u8(cbb, val)) |
140 | 0 | return 0; |
141 | 0 | started = 1; |
142 | 0 | } |
143 | | |
144 | 0 | return 1; |
145 | 0 | } |
146 | | |
147 | | static int |
148 | | oid_parse_arc(CBS *cbs, uint64_t *out_arc) |
149 | 79.7k | { |
150 | 79.7k | uint64_t arc = 0; |
151 | 79.7k | uint8_t val; |
152 | | |
153 | 99.1k | do { |
154 | 99.1k | if (!CBS_get_u8(cbs, &val)) |
155 | 24 | return 0; |
156 | 99.0k | if (arc == 0 && val == 0x80) |
157 | 12 | return 0; |
158 | 99.0k | if (out_arc != NULL && arc > (UINT64_MAX >> 7)) |
159 | 46 | return 0; |
160 | 99.0k | arc = (arc << 7) | (val & 0x7f); |
161 | 99.0k | } while (val & 0x80); |
162 | | |
163 | 79.6k | if (out_arc != NULL) |
164 | 2.16k | *out_arc = arc; |
165 | | |
166 | 79.6k | return 1; |
167 | 79.7k | } |
168 | | |
169 | | static int |
170 | | oid_add_arc_txt(CBB *cbb, uint64_t arc, int first) |
171 | 2.89k | { |
172 | 2.89k | const char *fmt = ".%llu"; |
173 | 2.89k | char s[22]; /* Digits in decimal representation of 2^64-1, plus '.' and NUL. */ |
174 | 2.89k | int n; |
175 | | |
176 | 2.89k | if (first) |
177 | 727 | fmt = "%llu"; |
178 | 2.89k | n = snprintf(s, sizeof(s), fmt, (unsigned long long)arc); |
179 | 2.89k | if (n < 0 || (size_t)n >= sizeof(s)) |
180 | 0 | return 0; |
181 | 2.89k | if (!CBB_add_bytes(cbb, s, n)) |
182 | 0 | return 0; |
183 | | |
184 | 2.89k | return 1; |
185 | 2.89k | } |
186 | | |
187 | | static int |
188 | | oid_parse_arc_txt(CBS *cbs, uint64_t *out_arc, char *separator, int first) |
189 | 0 | { |
190 | 0 | uint64_t arc = 0; |
191 | 0 | int digits = 0; |
192 | 0 | uint8_t val; |
193 | |
|
194 | 0 | if (!first) { |
195 | 0 | if (!CBS_get_u8(cbs, &val)) |
196 | 0 | return 0; |
197 | 0 | if ((*separator == 0 && val != '.' && val != ' ') || |
198 | 0 | (*separator != 0 && val != *separator)) { |
199 | 0 | ASN1error(ASN1_R_INVALID_SEPARATOR); |
200 | 0 | return 0; |
201 | 0 | } |
202 | 0 | *separator = val; |
203 | 0 | } |
204 | | |
205 | 0 | while (CBS_len(cbs) > 0) { |
206 | 0 | if (!CBS_peek_u8(cbs, &val)) |
207 | 0 | return 0; |
208 | 0 | if (val == '.' || val == ' ') |
209 | 0 | break; |
210 | | |
211 | 0 | if (!CBS_get_u8(cbs, &val)) |
212 | 0 | return 0; |
213 | 0 | if (val < '0' || val > '9') { |
214 | | /* For the first arc we treat this as the separator. */ |
215 | 0 | if (first) { |
216 | 0 | ASN1error(ASN1_R_INVALID_SEPARATOR); |
217 | 0 | return 0; |
218 | 0 | } |
219 | 0 | ASN1error(ASN1_R_INVALID_DIGIT); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 0 | val -= '0'; |
223 | |
|
224 | 0 | if (digits > 0 && arc == 0 && val == 0) { |
225 | 0 | ASN1error(ASN1_R_INVALID_NUMBER); |
226 | 0 | return 0; |
227 | 0 | } |
228 | 0 | digits++; |
229 | |
|
230 | 0 | if (arc > UINT64_MAX / 10) { |
231 | 0 | ASN1error(ASN1_R_TOO_LONG); |
232 | 0 | return 0; |
233 | 0 | } |
234 | 0 | arc = arc * 10 + val; |
235 | 0 | } |
236 | | |
237 | 0 | if (digits < 1) { |
238 | 0 | ASN1error(ASN1_R_INVALID_NUMBER); |
239 | 0 | return 0; |
240 | 0 | } |
241 | | |
242 | 0 | *out_arc = arc; |
243 | |
|
244 | 0 | return 1; |
245 | 0 | } |
246 | | |
247 | | static int |
248 | | a2c_ASN1_OBJECT_internal(CBB *cbb, CBS *cbs) |
249 | 0 | { |
250 | 0 | uint64_t arc, si1, si2; |
251 | 0 | char separator = 0; |
252 | |
|
253 | 0 | if (!oid_parse_arc_txt(cbs, &si1, &separator, 1)) |
254 | 0 | return 0; |
255 | | |
256 | 0 | if (CBS_len(cbs) == 0) { |
257 | 0 | ASN1error(ASN1_R_MISSING_SECOND_NUMBER); |
258 | 0 | return 0; |
259 | 0 | } |
260 | | |
261 | 0 | if (!oid_parse_arc_txt(cbs, &si2, &separator, 0)) |
262 | 0 | return 0; |
263 | | |
264 | | /* |
265 | | * X.690 section 8.19 - the first two subidentifiers are encoded as |
266 | | * (x * 40) + y, with x being limited to [0,1,2]. The second |
267 | | * subidentifier cannot exceed 39 for x < 2. |
268 | | */ |
269 | 0 | if (si1 > 2) { |
270 | 0 | ASN1error(ASN1_R_FIRST_NUM_TOO_LARGE); |
271 | 0 | return 0; |
272 | 0 | } |
273 | 0 | if ((si1 < 2 && si2 >= 40) || si2 > UINT64_MAX - si1 * 40) { |
274 | 0 | ASN1error(ASN1_R_SECOND_NUMBER_TOO_LARGE); |
275 | 0 | return 0; |
276 | 0 | } |
277 | 0 | arc = si1 * 40 + si2; |
278 | |
|
279 | 0 | if (!oid_add_arc(cbb, arc)) |
280 | 0 | return 0; |
281 | | |
282 | 0 | while (CBS_len(cbs) > 0) { |
283 | 0 | if (!oid_parse_arc_txt(cbs, &arc, &separator, 0)) |
284 | 0 | return 0; |
285 | 0 | if (!oid_add_arc(cbb, arc)) |
286 | 0 | return 0; |
287 | 0 | } |
288 | | |
289 | 0 | return 1; |
290 | 0 | } |
291 | | |
292 | | static int |
293 | | c2a_ASN1_OBJECT(CBS *cbs, CBB *cbb) |
294 | 758 | { |
295 | 758 | uint64_t arc, si1, si2; |
296 | | |
297 | | /* |
298 | | * X.690 section 8.19 - the first two subidentifiers are encoded as |
299 | | * (x * 40) + y, with x being limited to [0,1,2]. |
300 | | */ |
301 | 758 | if (!oid_parse_arc(cbs, &arc)) |
302 | 31 | return 0; |
303 | 727 | if ((si1 = arc / 40) > 2) |
304 | 101 | si1 = 2; |
305 | 727 | si2 = arc - si1 * 40; |
306 | | |
307 | 727 | if (!oid_add_arc_txt(cbb, si1, 1)) |
308 | 0 | return 0; |
309 | 727 | if (!oid_add_arc_txt(cbb, si2, 0)) |
310 | 0 | return 0; |
311 | | |
312 | 2.16k | while (CBS_len(cbs) > 0) { |
313 | 1.45k | if (!oid_parse_arc(cbs, &arc)) |
314 | 15 | return 0; |
315 | 1.43k | if (!oid_add_arc_txt(cbb, arc, 0)) |
316 | 0 | return 0; |
317 | 1.43k | } |
318 | | |
319 | | /* NUL terminate. */ |
320 | 712 | if (!CBB_add_u8(cbb, 0)) |
321 | 0 | return 0; |
322 | | |
323 | 712 | return 1; |
324 | 712 | } |
325 | | |
326 | | int |
327 | | a2d_ASN1_OBJECT(unsigned char *out, int out_len, const char *in, int in_len) |
328 | 0 | { |
329 | 0 | uint8_t *data = NULL; |
330 | 0 | size_t data_len; |
331 | 0 | CBS cbs; |
332 | 0 | CBB cbb; |
333 | 0 | int ret = 0; |
334 | |
|
335 | 0 | memset(&cbb, 0, sizeof(cbb)); |
336 | |
|
337 | 0 | if (in_len == -1) |
338 | 0 | in_len = strlen(in); |
339 | 0 | if (in_len <= 0) |
340 | 0 | goto err; |
341 | | |
342 | 0 | CBS_init(&cbs, in, in_len); |
343 | |
|
344 | 0 | if (!CBB_init(&cbb, 0)) |
345 | 0 | goto err; |
346 | 0 | if (!a2c_ASN1_OBJECT_internal(&cbb, &cbs)) |
347 | 0 | goto err; |
348 | 0 | if (!CBB_finish(&cbb, &data, &data_len)) |
349 | 0 | goto err; |
350 | | |
351 | 0 | if (data_len > INT_MAX) |
352 | 0 | goto err; |
353 | | |
354 | 0 | if (out != NULL) { |
355 | 0 | if (out_len <= 0 || (size_t)out_len < data_len) { |
356 | 0 | ASN1error(ASN1_R_BUFFER_TOO_SMALL); |
357 | 0 | goto err; |
358 | 0 | } |
359 | 0 | memcpy(out, data, data_len); |
360 | 0 | } |
361 | | |
362 | 0 | ret = (int)data_len; |
363 | |
|
364 | 0 | err: |
365 | 0 | CBB_cleanup(&cbb); |
366 | 0 | free(data); |
367 | |
|
368 | 0 | return ret; |
369 | 0 | } |
370 | | |
371 | | static int |
372 | | i2t_ASN1_OBJECT_oid(const ASN1_OBJECT *aobj, CBB *cbb) |
373 | 758 | { |
374 | 758 | CBS cbs; |
375 | | |
376 | 758 | CBS_init(&cbs, aobj->data, aobj->length); |
377 | | |
378 | 758 | return c2a_ASN1_OBJECT(&cbs, cbb); |
379 | 758 | } |
380 | | |
381 | | static int |
382 | | i2t_ASN1_OBJECT_name(const ASN1_OBJECT *aobj, CBB *cbb, const char **out_name) |
383 | 758 | { |
384 | 758 | const char *name; |
385 | 758 | int nid; |
386 | | |
387 | 758 | *out_name = NULL; |
388 | | |
389 | 758 | if ((nid = OBJ_obj2nid(aobj)) == NID_undef) |
390 | 758 | return 0; |
391 | | |
392 | 0 | if ((name = OBJ_nid2ln(nid)) == NULL) |
393 | 0 | name = OBJ_nid2sn(nid); |
394 | 0 | if (name == NULL) |
395 | 0 | return 0; |
396 | | |
397 | 0 | *out_name = name; |
398 | |
|
399 | 0 | if (!CBB_add_bytes(cbb, name, strlen(name))) |
400 | 0 | return 0; |
401 | | |
402 | | /* NUL terminate. */ |
403 | 0 | if (!CBB_add_u8(cbb, 0)) |
404 | 0 | return 0; |
405 | | |
406 | 0 | return 1; |
407 | 0 | } |
408 | | |
409 | | static int |
410 | | i2t_ASN1_OBJECT_cbb(const ASN1_OBJECT *aobj, CBB *cbb, int no_name) |
411 | 758 | { |
412 | 758 | const char *name; |
413 | | |
414 | 758 | if (!no_name) { |
415 | 758 | if (i2t_ASN1_OBJECT_name(aobj, cbb, &name)) |
416 | 0 | return 1; |
417 | 758 | if (name != NULL) |
418 | 0 | return 0; |
419 | 758 | } |
420 | 758 | return i2t_ASN1_OBJECT_oid(aobj, cbb); |
421 | 758 | } |
422 | | |
423 | | int |
424 | | i2t_ASN1_OBJECT_internal(const ASN1_OBJECT *aobj, char *buf, int buf_len, int no_name) |
425 | 758 | { |
426 | 758 | uint8_t *data = NULL; |
427 | 758 | size_t data_len; |
428 | 758 | CBB cbb; |
429 | 758 | int ret = 0; |
430 | | |
431 | 758 | if (buf_len < 0) |
432 | 0 | return 0; |
433 | 758 | if (buf_len > 0) |
434 | 758 | buf[0] = '\0'; |
435 | | |
436 | 758 | if (!CBB_init(&cbb, 0)) |
437 | 0 | goto err; |
438 | 758 | if (!i2t_ASN1_OBJECT_cbb(aobj, &cbb, no_name)) |
439 | 46 | goto err; |
440 | 712 | if (!CBB_finish(&cbb, &data, &data_len)) |
441 | 0 | goto err; |
442 | | |
443 | 712 | ret = strlcpy(buf, data, buf_len); |
444 | 758 | err: |
445 | 758 | CBB_cleanup(&cbb); |
446 | 758 | free(data); |
447 | | |
448 | 758 | return ret; |
449 | 712 | } |
450 | | |
451 | | int |
452 | | i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *aobj) |
453 | 758 | { |
454 | 758 | return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, 0); |
455 | 758 | } |
456 | | |
457 | | ASN1_OBJECT * |
458 | | t2i_ASN1_OBJECT_internal(const char *oid) |
459 | 0 | { |
460 | 0 | ASN1_OBJECT *aobj = NULL; |
461 | 0 | uint8_t *data = NULL; |
462 | 0 | size_t data_len; |
463 | 0 | CBB cbb; |
464 | 0 | CBS cbs; |
465 | |
|
466 | 0 | memset(&cbb, 0, sizeof(cbb)); |
467 | |
|
468 | 0 | CBS_init(&cbs, oid, strlen(oid)); |
469 | |
|
470 | 0 | if (!CBB_init(&cbb, 0)) |
471 | 0 | goto err; |
472 | 0 | if (!a2c_ASN1_OBJECT_internal(&cbb, &cbs)) |
473 | 0 | goto err; |
474 | 0 | if (!CBB_finish(&cbb, &data, &data_len)) |
475 | 0 | goto err; |
476 | | |
477 | 0 | if (data_len > INT_MAX) |
478 | 0 | goto err; |
479 | | |
480 | 0 | if ((aobj = ASN1_OBJECT_new()) == NULL) |
481 | 0 | goto err; |
482 | | |
483 | 0 | aobj->data = data; |
484 | 0 | aobj->length = (int)data_len; |
485 | 0 | aobj->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
486 | 0 | data = NULL; |
487 | |
|
488 | 0 | err: |
489 | 0 | CBB_cleanup(&cbb); |
490 | 0 | free(data); |
491 | |
|
492 | 0 | return aobj; |
493 | 0 | } |
494 | | |
495 | | int |
496 | | i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *aobj) |
497 | 0 | { |
498 | 0 | uint8_t *data = NULL; |
499 | 0 | size_t data_len; |
500 | 0 | CBB cbb; |
501 | 0 | int ret = -1; |
502 | |
|
503 | 0 | if (aobj == NULL || aobj->data == NULL) |
504 | 0 | return BIO_write(bp, "NULL", 4); |
505 | | |
506 | 0 | if (!CBB_init(&cbb, 0)) |
507 | 0 | goto err; |
508 | 0 | if (!i2t_ASN1_OBJECT_cbb(aobj, &cbb, 0)) { |
509 | 0 | ret = BIO_write(bp, "<INVALID>", 9); |
510 | 0 | goto err; |
511 | 0 | } |
512 | 0 | if (!CBB_finish(&cbb, &data, &data_len)) |
513 | 0 | goto err; |
514 | | |
515 | 0 | ret = BIO_write(bp, data, strlen(data)); |
516 | |
|
517 | 0 | err: |
518 | 0 | CBB_cleanup(&cbb); |
519 | 0 | free(data); |
520 | |
|
521 | 0 | return ret; |
522 | 0 | } |
523 | | |
524 | | int |
525 | | c2i_ASN1_OBJECT_cbs(ASN1_OBJECT **out_aobj, CBS *content) |
526 | 20.1k | { |
527 | 20.1k | ASN1_OBJECT *aobj = NULL; |
528 | 20.1k | uint8_t *data = NULL; |
529 | 20.1k | size_t data_len; |
530 | 20.1k | CBS cbs; |
531 | | |
532 | 20.1k | if (out_aobj == NULL) |
533 | 0 | goto err; |
534 | | |
535 | 20.1k | if (*out_aobj != NULL) { |
536 | 19.3k | ASN1_OBJECT_free(*out_aobj); |
537 | 19.3k | *out_aobj = NULL; |
538 | 19.3k | } |
539 | | |
540 | | /* Parse and validate OID encoding per X.690 8.19.2. */ |
541 | 20.1k | CBS_dup(content, &cbs); |
542 | 20.1k | if (CBS_len(&cbs) == 0) { |
543 | 2 | ASN1error(ASN1_R_INVALID_OBJECT_ENCODING); |
544 | 2 | goto err; |
545 | 2 | } |
546 | 97.5k | while (CBS_len(&cbs) > 0) { |
547 | 77.5k | if (!oid_parse_arc(&cbs, NULL)) { |
548 | 36 | ASN1error(ASN1_R_INVALID_OBJECT_ENCODING); |
549 | 36 | goto err; |
550 | 36 | } |
551 | 77.5k | } |
552 | | |
553 | 20.0k | if (!CBS_stow(content, &data, &data_len)) |
554 | 0 | goto err; |
555 | | |
556 | 20.0k | if (data_len > INT_MAX) |
557 | 0 | goto err; |
558 | | |
559 | 20.0k | if ((aobj = ASN1_OBJECT_new()) == NULL) |
560 | 0 | goto err; |
561 | | |
562 | 20.0k | aobj->data = data; |
563 | 20.0k | aobj->length = (int)data_len; /* XXX - change length to size_t. */ |
564 | 20.0k | aobj->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
565 | | |
566 | 20.0k | *out_aobj = aobj; |
567 | | |
568 | 20.0k | return 1; |
569 | | |
570 | 38 | err: |
571 | 38 | ASN1_OBJECT_free(aobj); |
572 | 38 | free(data); |
573 | | |
574 | 38 | return 0; |
575 | 20.0k | } |
576 | | |
577 | | ASN1_OBJECT * |
578 | | c2i_ASN1_OBJECT(ASN1_OBJECT **out_aobj, const unsigned char **pp, long len) |
579 | 0 | { |
580 | 0 | ASN1_OBJECT *aobj = NULL; |
581 | 0 | CBS content; |
582 | |
|
583 | 0 | if (out_aobj != NULL) { |
584 | 0 | ASN1_OBJECT_free(*out_aobj); |
585 | 0 | *out_aobj = NULL; |
586 | 0 | } |
587 | |
|
588 | 0 | if (len < 0) { |
589 | 0 | ASN1error(ASN1_R_LENGTH_ERROR); |
590 | 0 | return NULL; |
591 | 0 | } |
592 | | |
593 | 0 | CBS_init(&content, *pp, len); |
594 | |
|
595 | 0 | if (!c2i_ASN1_OBJECT_cbs(&aobj, &content)) |
596 | 0 | return NULL; |
597 | | |
598 | 0 | *pp = CBS_data(&content); |
599 | |
|
600 | 0 | if (out_aobj != NULL) |
601 | 0 | *out_aobj = aobj; |
602 | |
|
603 | 0 | return aobj; |
604 | 0 | } |
605 | | |
606 | | int |
607 | | i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) |
608 | 0 | { |
609 | 0 | unsigned char *p; |
610 | 0 | int objsize; |
611 | |
|
612 | 0 | if ((a == NULL) || (a->data == NULL)) |
613 | 0 | return (0); |
614 | | |
615 | 0 | objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT); |
616 | 0 | if (pp == NULL) |
617 | 0 | return objsize; |
618 | | |
619 | 0 | p = *pp; |
620 | 0 | ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
621 | 0 | memcpy(p, a->data, a->length); |
622 | 0 | p += a->length; |
623 | |
|
624 | 0 | *pp = p; |
625 | 0 | return (objsize); |
626 | 0 | } |
627 | | |
628 | | ASN1_OBJECT * |
629 | | d2i_ASN1_OBJECT(ASN1_OBJECT **out_aobj, const unsigned char **pp, long length) |
630 | 0 | { |
631 | 0 | ASN1_OBJECT *aobj = NULL; |
632 | 0 | uint32_t tag_number; |
633 | 0 | CBS cbs, content; |
634 | |
|
635 | 0 | if (out_aobj != NULL) { |
636 | 0 | ASN1_OBJECT_free(*out_aobj); |
637 | 0 | *out_aobj = NULL; |
638 | 0 | } |
639 | |
|
640 | 0 | if (length < 0) { |
641 | 0 | ASN1error(ASN1_R_LENGTH_ERROR); |
642 | 0 | return NULL; |
643 | 0 | } |
644 | | |
645 | 0 | CBS_init(&cbs, *pp, length); |
646 | |
|
647 | 0 | if (!asn1_get_primitive(&cbs, 0, &tag_number, &content)) { |
648 | 0 | ASN1error(ASN1_R_BAD_OBJECT_HEADER); |
649 | 0 | return NULL; |
650 | 0 | } |
651 | 0 | if (tag_number != V_ASN1_OBJECT) { |
652 | 0 | ASN1error(ASN1_R_EXPECTING_AN_OBJECT); |
653 | 0 | return NULL; |
654 | 0 | } |
655 | | |
656 | 0 | if (!c2i_ASN1_OBJECT_cbs(&aobj, &content)) |
657 | 0 | return NULL; |
658 | | |
659 | 0 | *pp = CBS_data(&cbs); |
660 | |
|
661 | 0 | if (out_aobj != NULL) |
662 | 0 | *out_aobj = aobj; |
663 | |
|
664 | 0 | return aobj; |
665 | 0 | } |