1 | /* |
2 | * Copyright 1995-2018 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 "crypto/ctype.h" |
12 | #include <limits.h> |
13 | #include "internal/cryptlib.h" |
14 | #include <openssl/lhash.h> |
15 | #include <openssl/asn1.h> |
16 | #include "crypto/objects.h" |
17 | #include <openssl/bn.h> |
18 | #include "crypto/asn1.h" |
19 | #include "obj_local.h" |
20 | |
21 | /* obj_dat.h is generated from objects.h by obj_dat.pl */ |
22 | #include "obj_dat.h" |
23 | |
24 | DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); |
25 | DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); |
26 | DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); |
27 | |
28 | #define ADDED_DATA 0 |
29 | #define ADDED_SNAME 1 |
30 | #define ADDED_LNAME 2 |
31 | #define ADDED_NID 3 |
32 | |
33 | struct added_obj_st { |
34 | int type; |
35 | ASN1_OBJECT *obj; |
36 | }; |
37 | |
38 | static int new_nid = NUM_NID; |
39 | static LHASH_OF(ADDED_OBJ) *added = NULL; |
40 | |
41 | static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b) |
42 | { |
43 | return strcmp((*a)->sn, nid_objs[*b].sn); |
44 | } |
45 | |
46 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); |
47 | |
48 | static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b) |
49 | { |
50 | return strcmp((*a)->ln, nid_objs[*b].ln); |
51 | } |
52 | |
53 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); |
54 | |
55 | static unsigned long added_obj_hash(const ADDED_OBJ *ca) |
56 | { |
57 | const ASN1_OBJECT *a; |
58 | int i; |
59 | unsigned long ret = 0; |
60 | unsigned char *p; |
61 | |
62 | a = ca->obj; |
63 | switch (ca->type) { |
64 | case ADDED_DATA: |
65 | ret = a->length << 20L; |
66 | p = (unsigned char *)a->data; |
67 | for (i = 0; i < a->length; i++) |
68 | ret ^= p[i] << ((i * 3) % 24); |
69 | break; |
70 | case ADDED_SNAME: |
71 | ret = OPENSSL_LH_strhash(a->sn); |
72 | break; |
73 | case ADDED_LNAME: |
74 | ret = OPENSSL_LH_strhash(a->ln); |
75 | break; |
76 | case ADDED_NID: |
77 | ret = a->nid; |
78 | break; |
79 | default: |
80 | /* abort(); */ |
81 | return 0; |
82 | } |
83 | ret &= 0x3fffffffL; |
84 | ret |= ((unsigned long)ca->type) << 30L; |
85 | return ret; |
86 | } |
87 | |
88 | static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb) |
89 | { |
90 | ASN1_OBJECT *a, *b; |
91 | int i; |
92 | |
93 | i = ca->type - cb->type; |
94 | if (i) |
95 | return i; |
96 | a = ca->obj; |
97 | b = cb->obj; |
98 | switch (ca->type) { |
99 | case ADDED_DATA: |
100 | i = (a->length - b->length); |
101 | if (i) |
102 | return i; |
103 | return memcmp(a->data, b->data, (size_t)a->length); |
104 | case ADDED_SNAME: |
105 | if (a->sn == NULL) |
106 | return -1; |
107 | else if (b->sn == NULL) |
108 | return 1; |
109 | else |
110 | return strcmp(a->sn, b->sn); |
111 | case ADDED_LNAME: |
112 | if (a->ln == NULL) |
113 | return -1; |
114 | else if (b->ln == NULL) |
115 | return 1; |
116 | else |
117 | return strcmp(a->ln, b->ln); |
118 | case ADDED_NID: |
119 | return a->nid - b->nid; |
120 | default: |
121 | /* abort(); */ |
122 | return 0; |
123 | } |
124 | } |
125 | |
126 | static int init_added(void) |
127 | { |
128 | if (added != NULL) |
129 | return 1; |
130 | added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp); |
131 | return added != NULL; |
132 | } |
133 | |
134 | static void cleanup1_doall(ADDED_OBJ *a) |
135 | { |
136 | a->obj->nid = 0; |
137 | a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC | |
138 | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
139 | } |
140 | |
141 | static void cleanup2_doall(ADDED_OBJ *a) |
142 | { |
143 | a->obj->nid++; |
144 | } |
145 | |
146 | static void cleanup3_doall(ADDED_OBJ *a) |
147 | { |
148 | if (--a->obj->nid == 0) |
149 | ASN1_OBJECT_free(a->obj); |
150 | OPENSSL_free(a); |
151 | } |
152 | |
153 | void obj_cleanup_int(void) |
154 | { |
155 | if (added == NULL) |
156 | return; |
157 | lh_ADDED_OBJ_set_down_load(added, 0); |
158 | lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */ |
159 | lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */ |
160 | lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */ |
161 | lh_ADDED_OBJ_free(added); |
162 | added = NULL; |
163 | } |
164 | |
165 | int OBJ_new_nid(int num) |
166 | { |
167 | int i; |
168 | |
169 | i = new_nid; |
170 | new_nid += num; |
171 | return i; |
172 | } |
173 | |
174 | int OBJ_add_object(const ASN1_OBJECT *obj) |
175 | { |
176 | ASN1_OBJECT *o; |
177 | ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop; |
178 | int i; |
179 | |
180 | if (added == NULL) |
181 | if (!init_added()) |
182 | return 0; |
183 | if ((o = OBJ_dup(obj)) == NULL) |
184 | goto err; |
185 | if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) |
186 | goto err2; |
187 | if ((o->length != 0) && (obj->data != NULL)) |
188 | if ((ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) |
189 | goto err2; |
190 | if (o->sn != NULL) |
191 | if ((ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) |
192 | goto err2; |
193 | if (o->ln != NULL) |
194 | if ((ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) |
195 | goto err2; |
196 | |
197 | for (i = ADDED_DATA; i <= ADDED_NID; i++) { |
198 | if (ao[i] != NULL) { |
199 | ao[i]->type = i; |
200 | ao[i]->obj = o; |
201 | aop = lh_ADDED_OBJ_insert(added, ao[i]); |
202 | /* memory leak, but should not normally matter */ |
203 | OPENSSL_free(aop); |
204 | } |
205 | } |
206 | o->flags &= |
207 | ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
208 | ASN1_OBJECT_FLAG_DYNAMIC_DATA); |
209 | |
210 | return o->nid; |
211 | err2: |
212 | OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE); |
213 | err: |
214 | for (i = ADDED_DATA; i <= ADDED_NID; i++) |
215 | OPENSSL_free(ao[i]); |
216 | ASN1_OBJECT_free(o); |
217 | return NID_undef; |
218 | } |
219 | |
220 | ASN1_OBJECT *OBJ_nid2obj(int n) |
221 | { |
222 | ADDED_OBJ ad, *adp; |
223 | ASN1_OBJECT ob; |
224 | |
225 | if ((n >= 0) && (n < NUM_NID)) { |
226 | if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) { |
227 | OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID); |
228 | return NULL; |
229 | } |
230 | return (ASN1_OBJECT *)&(nid_objs[n]); |
231 | } |
232 | |
233 | /* Make sure we've loaded config before checking for any "added" objects */ |
234 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
235 | |
236 | if (added == NULL) |
237 | return NULL; |
238 | |
239 | ad.type = ADDED_NID; |
240 | ad.obj = &ob; |
241 | ob.nid = n; |
242 | adp = lh_ADDED_OBJ_retrieve(added, &ad); |
243 | if (adp != NULL) |
244 | return adp->obj; |
245 | |
246 | OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID); |
247 | return NULL; |
248 | } |
249 | |
250 | const char *OBJ_nid2sn(int n) |
251 | { |
252 | ADDED_OBJ ad, *adp; |
253 | ASN1_OBJECT ob; |
254 | |
255 | if ((n >= 0) && (n < NUM_NID)) { |
256 | if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) { |
257 | OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID); |
258 | return NULL; |
259 | } |
260 | return nid_objs[n].sn; |
261 | } |
262 | |
263 | /* Make sure we've loaded config before checking for any "added" objects */ |
264 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
265 | |
266 | if (added == NULL) |
267 | return NULL; |
268 | |
269 | ad.type = ADDED_NID; |
270 | ad.obj = &ob; |
271 | ob.nid = n; |
272 | adp = lh_ADDED_OBJ_retrieve(added, &ad); |
273 | if (adp != NULL) |
274 | return adp->obj->sn; |
275 | |
276 | OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID); |
277 | return NULL; |
278 | } |
279 | |
280 | const char *OBJ_nid2ln(int n) |
281 | { |
282 | ADDED_OBJ ad, *adp; |
283 | ASN1_OBJECT ob; |
284 | |
285 | if ((n >= 0) && (n < NUM_NID)) { |
286 | if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) { |
287 | OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID); |
288 | return NULL; |
289 | } |
290 | return nid_objs[n].ln; |
291 | } |
292 | |
293 | /* Make sure we've loaded config before checking for any "added" objects */ |
294 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
295 | |
296 | if (added == NULL) |
297 | return NULL; |
298 | |
299 | ad.type = ADDED_NID; |
300 | ad.obj = &ob; |
301 | ob.nid = n; |
302 | adp = lh_ADDED_OBJ_retrieve(added, &ad); |
303 | if (adp != NULL) |
304 | return adp->obj->ln; |
305 | |
306 | OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID); |
307 | return NULL; |
308 | } |
309 | |
310 | static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp) |
311 | { |
312 | int j; |
313 | const ASN1_OBJECT *a = *ap; |
314 | const ASN1_OBJECT *b = &nid_objs[*bp]; |
315 | |
316 | j = (a->length - b->length); |
317 | if (j) |
318 | return j; |
319 | if (a->length == 0) |
320 | return 0; |
321 | return memcmp(a->data, b->data, a->length); |
322 | } |
323 | |
324 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); |
325 | |
326 | int OBJ_obj2nid(const ASN1_OBJECT *a) |
327 | { |
328 | const unsigned int *op; |
329 | ADDED_OBJ ad, *adp; |
330 | |
331 | if (a == NULL) |
332 | return NID_undef; |
333 | if (a->nid != 0) |
334 | return a->nid; |
335 | |
336 | if (a->length == 0) |
337 | return NID_undef; |
338 | |
339 | /* Make sure we've loaded config before checking for any "added" objects */ |
340 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
341 | |
342 | if (added != NULL) { |
343 | ad.type = ADDED_DATA; |
344 | ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */ |
345 | adp = lh_ADDED_OBJ_retrieve(added, &ad); |
346 | if (adp != NULL) |
347 | return adp->obj->nid; |
348 | } |
349 | op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ); |
350 | if (op == NULL) |
351 | return NID_undef; |
352 | return nid_objs[*op].nid; |
353 | } |
354 | |
355 | /* |
356 | * Convert an object name into an ASN1_OBJECT if "noname" is not set then |
357 | * search for short and long names first. This will convert the "dotted" form |
358 | * into an object: unlike OBJ_txt2nid it can be used with any objects, not |
359 | * just registered ones. |
360 | */ |
361 | |
362 | ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) |
363 | { |
364 | int nid = NID_undef; |
365 | ASN1_OBJECT *op; |
366 | unsigned char *buf; |
367 | unsigned char *p; |
368 | const unsigned char *cp; |
369 | int i, j; |
370 | |
371 | if (!no_name) { |
372 | if (((nid = OBJ_sn2nid(s)) != NID_undef) || |
373 | ((nid = OBJ_ln2nid(s)) != NID_undef)) |
374 | return OBJ_nid2obj(nid); |
375 | } |
376 | |
377 | /* Work out size of content octets */ |
378 | i = a2d_ASN1_OBJECT(NULL, 0, s, -1); |
379 | if (i <= 0) { |
380 | /* Don't clear the error */ |
381 | /* |
382 | * ERR_clear_error(); |
383 | */ |
384 | return NULL; |
385 | } |
386 | /* Work out total size */ |
387 | j = ASN1_object_size(0, i, V_ASN1_OBJECT); |
388 | if (j < 0) |
389 | return NULL; |
390 | |
391 | if ((buf = OPENSSL_malloc(j)) == NULL) { |
392 | OBJerr(OBJ_F_OBJ_TXT2OBJ, ERR_R_MALLOC_FAILURE); |
393 | return NULL; |
394 | } |
395 | |
396 | p = buf; |
397 | /* Write out tag+length */ |
398 | ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
399 | /* Write out contents */ |
400 | a2d_ASN1_OBJECT(p, i, s, -1); |
401 | |
402 | cp = buf; |
403 | op = d2i_ASN1_OBJECT(NULL, &cp, j); |
404 | OPENSSL_free(buf); |
405 | return op; |
406 | } |
407 | |
408 | int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) |
409 | { |
410 | int i, n = 0, len, nid, first, use_bn; |
411 | BIGNUM *bl; |
412 | unsigned long l; |
413 | const unsigned char *p; |
414 | char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2]; |
415 | |
416 | /* Ensure that, at every state, |buf| is NUL-terminated. */ |
417 | if (buf && buf_len > 0) |
418 | buf[0] = '\0'; |
419 | |
420 | if ((a == NULL) || (a->data == NULL)) |
421 | return 0; |
422 | |
423 | if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) { |
424 | const char *s; |
425 | s = OBJ_nid2ln(nid); |
426 | if (s == NULL) |
427 | s = OBJ_nid2sn(nid); |
428 | if (s) { |
429 | if (buf) |
430 | OPENSSL_strlcpy(buf, s, buf_len); |
431 | n = strlen(s); |
432 | return n; |
433 | } |
434 | } |
435 | |
436 | len = a->length; |
437 | p = a->data; |
438 | |
439 | first = 1; |
440 | bl = NULL; |
441 | |
442 | while (len > 0) { |
443 | l = 0; |
444 | use_bn = 0; |
445 | for (;;) { |
446 | unsigned char c = *p++; |
447 | len--; |
448 | if ((len == 0) && (c & 0x80)) |
449 | goto err; |
450 | if (use_bn) { |
451 | if (!BN_add_word(bl, c & 0x7f)) |
452 | goto err; |
453 | } else |
454 | l |= c & 0x7f; |
455 | if (!(c & 0x80)) |
456 | break; |
457 | if (!use_bn && (l > (ULONG_MAX >> 7L))) { |
458 | if (bl == NULL && (bl = BN_new()) == NULL) |
459 | goto err; |
460 | if (!BN_set_word(bl, l)) |
461 | goto err; |
462 | use_bn = 1; |
463 | } |
464 | if (use_bn) { |
465 | if (!BN_lshift(bl, bl, 7)) |
466 | goto err; |
467 | } else |
468 | l <<= 7L; |
469 | } |
470 | |
471 | if (first) { |
472 | first = 0; |
473 | if (l >= 80) { |
474 | i = 2; |
475 | if (use_bn) { |
476 | if (!BN_sub_word(bl, 80)) |
477 | goto err; |
478 | } else |
479 | l -= 80; |
480 | } else { |
481 | i = (int)(l / 40); |
482 | l -= (long)(i * 40); |
483 | } |
484 | if (buf && (buf_len > 1)) { |
485 | *buf++ = i + '0'; |
486 | *buf = '\0'; |
487 | buf_len--; |
488 | } |
489 | n++; |
490 | } |
491 | |
492 | if (use_bn) { |
493 | char *bndec; |
494 | bndec = BN_bn2dec(bl); |
495 | if (!bndec) |
496 | goto err; |
497 | i = strlen(bndec); |
498 | if (buf) { |
499 | if (buf_len > 1) { |
500 | *buf++ = '.'; |
501 | *buf = '\0'; |
502 | buf_len--; |
503 | } |
504 | OPENSSL_strlcpy(buf, bndec, buf_len); |
505 | if (i > buf_len) { |
506 | buf += buf_len; |
507 | buf_len = 0; |
508 | } else { |
509 | buf += i; |
510 | buf_len -= i; |
511 | } |
512 | } |
513 | n++; |
514 | n += i; |
515 | OPENSSL_free(bndec); |
516 | } else { |
517 | BIO_snprintf(tbuf, sizeof(tbuf), ".%lu" , l); |
518 | i = strlen(tbuf); |
519 | if (buf && (buf_len > 0)) { |
520 | OPENSSL_strlcpy(buf, tbuf, buf_len); |
521 | if (i > buf_len) { |
522 | buf += buf_len; |
523 | buf_len = 0; |
524 | } else { |
525 | buf += i; |
526 | buf_len -= i; |
527 | } |
528 | } |
529 | n += i; |
530 | l = 0; |
531 | } |
532 | } |
533 | |
534 | BN_free(bl); |
535 | return n; |
536 | |
537 | err: |
538 | BN_free(bl); |
539 | return -1; |
540 | } |
541 | |
542 | int OBJ_txt2nid(const char *s) |
543 | { |
544 | ASN1_OBJECT *obj; |
545 | int nid; |
546 | obj = OBJ_txt2obj(s, 0); |
547 | nid = OBJ_obj2nid(obj); |
548 | ASN1_OBJECT_free(obj); |
549 | return nid; |
550 | } |
551 | |
552 | int OBJ_ln2nid(const char *s) |
553 | { |
554 | ASN1_OBJECT o; |
555 | const ASN1_OBJECT *oo = &o; |
556 | ADDED_OBJ ad, *adp; |
557 | const unsigned int *op; |
558 | |
559 | /* Make sure we've loaded config before checking for any "added" objects */ |
560 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
561 | |
562 | o.ln = s; |
563 | if (added != NULL) { |
564 | ad.type = ADDED_LNAME; |
565 | ad.obj = &o; |
566 | adp = lh_ADDED_OBJ_retrieve(added, &ad); |
567 | if (adp != NULL) |
568 | return adp->obj->nid; |
569 | } |
570 | op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN); |
571 | if (op == NULL) |
572 | return NID_undef; |
573 | return nid_objs[*op].nid; |
574 | } |
575 | |
576 | int OBJ_sn2nid(const char *s) |
577 | { |
578 | ASN1_OBJECT o; |
579 | const ASN1_OBJECT *oo = &o; |
580 | ADDED_OBJ ad, *adp; |
581 | const unsigned int *op; |
582 | |
583 | /* Make sure we've loaded config before checking for any "added" objects */ |
584 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
585 | |
586 | o.sn = s; |
587 | if (added != NULL) { |
588 | ad.type = ADDED_SNAME; |
589 | ad.obj = &o; |
590 | adp = lh_ADDED_OBJ_retrieve(added, &ad); |
591 | if (adp != NULL) |
592 | return adp->obj->nid; |
593 | } |
594 | op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN); |
595 | if (op == NULL) |
596 | return NID_undef; |
597 | return nid_objs[*op].nid; |
598 | } |
599 | |
600 | const void *OBJ_bsearch_(const void *key, const void *base, int num, int size, |
601 | int (*cmp) (const void *, const void *)) |
602 | { |
603 | return OBJ_bsearch_ex_(key, base, num, size, cmp, 0); |
604 | } |
605 | |
606 | const void *OBJ_bsearch_ex_(const void *key, const void *base, int num, |
607 | int size, |
608 | int (*cmp) (const void *, const void *), |
609 | int flags) |
610 | { |
611 | const char *p = ossl_bsearch(key, base, num, size, cmp, flags); |
612 | |
613 | #ifdef CHARSET_EBCDIC |
614 | /* |
615 | * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I |
616 | * don't have perl (yet), we revert to a *LINEAR* search when the object |
617 | * wasn't found in the binary search. |
618 | */ |
619 | if (p == NULL) { |
620 | const char *base_ = base; |
621 | int l, h, i = 0, c = 0; |
622 | |
623 | for (i = 0; i < num; ++i) { |
624 | p = &(base_[i * size]); |
625 | c = (*cmp) (key, p); |
626 | if (c == 0 |
627 | || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))) |
628 | return p; |
629 | } |
630 | } |
631 | #endif |
632 | return p; |
633 | } |
634 | |
635 | /* |
636 | * Parse a BIO sink to create some extra oid's objects. |
637 | * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN> |
638 | */ |
639 | int OBJ_create_objects(BIO *in) |
640 | { |
641 | char buf[512]; |
642 | int i, num = 0; |
643 | char *o, *s, *l = NULL; |
644 | |
645 | for (;;) { |
646 | s = o = NULL; |
647 | i = BIO_gets(in, buf, 512); |
648 | if (i <= 0) |
649 | return num; |
650 | buf[i - 1] = '\0'; |
651 | if (!ossl_isalnum(buf[0])) |
652 | return num; |
653 | o = s = buf; |
654 | while (ossl_isdigit(*s) || *s == '.') |
655 | s++; |
656 | if (*s != '\0') { |
657 | *(s++) = '\0'; |
658 | while (ossl_isspace(*s)) |
659 | s++; |
660 | if (*s == '\0') { |
661 | s = NULL; |
662 | } else { |
663 | l = s; |
664 | while (*l != '\0' && !ossl_isspace(*l)) |
665 | l++; |
666 | if (*l != '\0') { |
667 | *(l++) = '\0'; |
668 | while (ossl_isspace(*l)) |
669 | l++; |
670 | if (*l == '\0') { |
671 | l = NULL; |
672 | } |
673 | } else { |
674 | l = NULL; |
675 | } |
676 | } |
677 | } else { |
678 | s = NULL; |
679 | } |
680 | if (*o == '\0') |
681 | return num; |
682 | if (!OBJ_create(o, s, l)) |
683 | return num; |
684 | num++; |
685 | } |
686 | } |
687 | |
688 | int OBJ_create(const char *oid, const char *sn, const char *ln) |
689 | { |
690 | ASN1_OBJECT *tmpoid = NULL; |
691 | int ok = 0; |
692 | |
693 | /* Check to see if short or long name already present */ |
694 | if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef) |
695 | || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) { |
696 | OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS); |
697 | return 0; |
698 | } |
699 | |
700 | /* Convert numerical OID string to an ASN1_OBJECT structure */ |
701 | tmpoid = OBJ_txt2obj(oid, 1); |
702 | if (tmpoid == NULL) |
703 | return 0; |
704 | |
705 | /* If NID is not NID_undef then object already exists */ |
706 | if (OBJ_obj2nid(tmpoid) != NID_undef) { |
707 | OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS); |
708 | goto err; |
709 | } |
710 | |
711 | tmpoid->nid = OBJ_new_nid(1); |
712 | tmpoid->sn = (char *)sn; |
713 | tmpoid->ln = (char *)ln; |
714 | |
715 | ok = OBJ_add_object(tmpoid); |
716 | |
717 | tmpoid->sn = NULL; |
718 | tmpoid->ln = NULL; |
719 | |
720 | err: |
721 | ASN1_OBJECT_free(tmpoid); |
722 | return ok; |
723 | } |
724 | |
725 | size_t OBJ_length(const ASN1_OBJECT *obj) |
726 | { |
727 | if (obj == NULL) |
728 | return 0; |
729 | return obj->length; |
730 | } |
731 | |
732 | const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj) |
733 | { |
734 | if (obj == NULL) |
735 | return NULL; |
736 | return obj->data; |
737 | } |
738 | |