1 /* $OpenBSD: obj_dat.c,v 1.83 2024/01/13 11:55:31 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 <ctype.h> 60 #include <limits.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 65 #include <openssl/opensslconf.h> 66 67 #include <openssl/asn1.h> 68 #include <openssl/bn.h> 69 #include <openssl/err.h> 70 #include <openssl/lhash.h> 71 #include <openssl/objects.h> 72 73 #include "asn1_local.h" 74 75 /* obj_dat.h is generated from objects.h by obj_dat.pl */ 76 #include "obj_dat.h" 77 78 #define ADDED_DATA 0 79 #define ADDED_SNAME 1 80 #define ADDED_LNAME 2 81 #define ADDED_NID 3 82 83 typedef struct added_obj_st { 84 int type; 85 ASN1_OBJECT *obj; 86 } ADDED_OBJ; 87 DECLARE_LHASH_OF(ADDED_OBJ); 88 89 static int new_nid = NUM_NID; 90 static LHASH_OF(ADDED_OBJ) *added = NULL; 91 92 static unsigned long 93 added_obj_hash(const ADDED_OBJ *ca) 94 { 95 const ASN1_OBJECT *a; 96 int i; 97 unsigned long ret = 0; 98 unsigned char *p; 99 100 a = ca->obj; 101 switch (ca->type) { 102 case ADDED_DATA: 103 ret = a->length << 20L; 104 p = (unsigned char *)a->data; 105 for (i = 0; i < a->length; i++) 106 ret ^= p[i] << ((i * 3) % 24); 107 break; 108 case ADDED_SNAME: 109 ret = lh_strhash(a->sn); 110 break; 111 case ADDED_LNAME: 112 ret = lh_strhash(a->ln); 113 break; 114 case ADDED_NID: 115 ret = a->nid; 116 break; 117 default: 118 return 0; 119 } 120 ret &= 0x3fffffffL; 121 ret |= ca->type << 30L; 122 return (ret); 123 } 124 static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ) 125 126 static int 127 added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb) 128 { 129 const ASN1_OBJECT *a, *b; 130 int cmp; 131 132 if ((cmp = ca->type - cb->type) != 0) 133 return cmp; 134 135 a = ca->obj; 136 b = cb->obj; 137 switch (ca->type) { 138 case ADDED_DATA: 139 return OBJ_cmp(a, b); 140 case ADDED_SNAME: 141 if (a->sn == NULL) 142 return -1; 143 if (b->sn == NULL) 144 return 1; 145 return strcmp(a->sn, b->sn); 146 case ADDED_LNAME: 147 if (a->ln == NULL) 148 return -1; 149 if (b->ln == NULL) 150 return 1; 151 return strcmp(a->ln, b->ln); 152 case ADDED_NID: 153 return a->nid - b->nid; 154 default: 155 return 0; 156 } 157 } 158 static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ) 159 160 static void 161 cleanup1_doall(ADDED_OBJ *a) 162 { 163 a->obj->nid = 0; 164 a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC | 165 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | 166 ASN1_OBJECT_FLAG_DYNAMIC_DATA; 167 } 168 169 static void cleanup2_doall(ADDED_OBJ *a) 170 { 171 a->obj->nid++; 172 } 173 174 static void 175 cleanup3_doall(ADDED_OBJ *a) 176 { 177 if (--a->obj->nid == 0) 178 ASN1_OBJECT_free(a->obj); 179 free(a); 180 } 181 182 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ) 183 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ) 184 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ) 185 186 /* The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting 187 * to use freed up OIDs. If necessary the actual freeing up of OIDs is 188 * delayed. 189 */ 190 191 int obj_cleanup_defer = 0; 192 193 void 194 OBJ_cleanup(void) 195 { 196 if (obj_cleanup_defer) { 197 obj_cleanup_defer = 2; 198 return; 199 } 200 if (added == NULL) 201 return; 202 lh_ADDED_OBJ_down_load(added) = 0; 203 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */ 204 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */ 205 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */ 206 lh_ADDED_OBJ_free(added); 207 added = NULL; 208 } 209 LCRYPTO_ALIAS(OBJ_cleanup); 210 211 int 212 OBJ_new_nid(int num) 213 { 214 int i; 215 216 i = new_nid; 217 new_nid += num; 218 return (i); 219 } 220 LCRYPTO_ALIAS(OBJ_new_nid); 221 222 int 223 OBJ_add_object(const ASN1_OBJECT *obj) 224 { 225 ASN1_OBJECT *o = NULL; 226 ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop; 227 int i; 228 229 if (added == NULL) 230 added = lh_ADDED_OBJ_new(); 231 if (added == NULL) 232 goto err; 233 if (obj == NULL || obj->nid == NID_undef) 234 goto err; 235 if ((o = OBJ_dup(obj)) == NULL) 236 goto err; 237 if (!(ao[ADDED_NID] = malloc(sizeof(ADDED_OBJ)))) 238 goto err2; 239 if ((o->length != 0) && (obj->data != NULL)) 240 if (!(ao[ADDED_DATA] = malloc(sizeof(ADDED_OBJ)))) 241 goto err2; 242 if (o->sn != NULL) 243 if (!(ao[ADDED_SNAME] = malloc(sizeof(ADDED_OBJ)))) 244 goto err2; 245 if (o->ln != NULL) 246 if (!(ao[ADDED_LNAME] = malloc(sizeof(ADDED_OBJ)))) 247 goto err2; 248 249 for (i = ADDED_DATA; i <= ADDED_NID; i++) { 250 if (ao[i] != NULL) { 251 ao[i]->type = i; 252 ao[i]->obj = o; 253 aop = lh_ADDED_OBJ_insert(added, ao[i]); 254 /* memory leak, but should not normally matter */ 255 free(aop); 256 } 257 } 258 o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | 259 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | 260 ASN1_OBJECT_FLAG_DYNAMIC_DATA); 261 262 return (o->nid); 263 264 err2: 265 OBJerror(ERR_R_MALLOC_FAILURE); 266 err: 267 for (i = ADDED_DATA; i <= ADDED_NID; i++) 268 free(ao[i]); 269 ASN1_OBJECT_free(o); 270 return (NID_undef); 271 } 272 LCRYPTO_ALIAS(OBJ_add_object); 273 274 ASN1_OBJECT * 275 OBJ_nid2obj(int nid) 276 { 277 if (nid >= 0 && nid < NUM_NID) { 278 if (nid == NID_undef || nid_objs[nid].nid != NID_undef) 279 return (ASN1_OBJECT *)&nid_objs[nid]; 280 281 goto unknown; 282 } 283 284 /* XXX - locking. */ 285 if (added != NULL) { 286 ASN1_OBJECT aobj = { 287 .nid = nid, 288 }; 289 ADDED_OBJ needle = { 290 .type = ADDED_NID, 291 .obj = &aobj, 292 }; 293 ADDED_OBJ *found; 294 295 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 296 return found->obj; 297 } 298 299 unknown: 300 OBJerror(OBJ_R_UNKNOWN_NID); 301 302 return NULL; 303 } 304 LCRYPTO_ALIAS(OBJ_nid2obj); 305 306 const char * 307 OBJ_nid2sn(int nid) 308 { 309 ASN1_OBJECT *aobj; 310 311 if ((aobj = OBJ_nid2obj(nid)) == NULL) 312 return NULL; 313 314 return aobj->sn; 315 } 316 LCRYPTO_ALIAS(OBJ_nid2sn); 317 318 const char * 319 OBJ_nid2ln(int nid) 320 { 321 ASN1_OBJECT *aobj; 322 323 if ((aobj = OBJ_nid2obj(nid)) == NULL) 324 return NULL; 325 326 return aobj->ln; 327 } 328 LCRYPTO_ALIAS(OBJ_nid2ln); 329 330 static int 331 obj_objs_cmp(const void *aobj, const void *b) 332 { 333 const unsigned int *nid = b; 334 335 OPENSSL_assert(*nid < NUM_NID); 336 337 return OBJ_cmp(aobj, &nid_objs[*nid]); 338 } 339 340 int 341 OBJ_obj2nid(const ASN1_OBJECT *aobj) 342 { 343 const unsigned int *nid; 344 345 if (aobj == NULL || aobj->length == 0) 346 return NID_undef; 347 348 if (aobj->nid != NID_undef) 349 return aobj->nid; 350 351 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ 352 if (added != NULL) { 353 ADDED_OBJ needle = { 354 .type = ADDED_DATA, 355 .obj = (ASN1_OBJECT *)aobj, 356 }; 357 ADDED_OBJ *found; 358 359 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 360 return found->obj->nid; 361 } 362 363 /* obj_objs holds built-in obj NIDs in ascending OBJ_cmp() order. */ 364 nid = bsearch(aobj, obj_objs, NUM_OBJ, sizeof(unsigned int), obj_objs_cmp); 365 if (nid != NULL) 366 return *nid; 367 368 return NID_undef; 369 } 370 LCRYPTO_ALIAS(OBJ_obj2nid); 371 372 static int 373 sn_objs_cmp(const void *sn, const void *b) 374 { 375 const unsigned int *nid = b; 376 377 OPENSSL_assert(*nid < NUM_NID); 378 379 return strcmp(sn, nid_objs[*nid].sn); 380 } 381 382 int 383 OBJ_sn2nid(const char *sn) 384 { 385 const unsigned int *nid; 386 387 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ 388 if (added != NULL) { 389 ASN1_OBJECT aobj = { 390 .sn = sn, 391 }; 392 ADDED_OBJ needle = { 393 .type = ADDED_SNAME, 394 .obj = &aobj, 395 }; 396 ADDED_OBJ *found; 397 398 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 399 return found->obj->nid; 400 } 401 402 /* sn_objs holds NIDs in ascending alphabetical order of SN. */ 403 nid = bsearch(sn, sn_objs, NUM_SN, sizeof(unsigned int), sn_objs_cmp); 404 if (nid != NULL) 405 return *nid; 406 407 return NID_undef; 408 } 409 LCRYPTO_ALIAS(OBJ_sn2nid); 410 411 static int 412 ln_objs_cmp(const void *ln, const void *b) 413 { 414 const unsigned int *nid = b; 415 416 OPENSSL_assert(*nid < NUM_NID); 417 418 return strcmp(ln, nid_objs[*nid].ln); 419 } 420 421 int 422 OBJ_ln2nid(const char *ln) 423 { 424 const unsigned int *nid; 425 426 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ 427 if (added != NULL) { 428 ASN1_OBJECT aobj = { 429 .ln = ln, 430 }; 431 ADDED_OBJ needle = { 432 .type = ADDED_LNAME, 433 .obj = &aobj, 434 }; 435 ADDED_OBJ *found; 436 437 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 438 return found->obj->nid; 439 } 440 441 /* ln_objs holds NIDs in ascending alphabetical order of LN. */ 442 nid = bsearch(ln, ln_objs, NUM_LN, sizeof(unsigned int), ln_objs_cmp); 443 if (nid != NULL) 444 return *nid; 445 446 return NID_undef; 447 } 448 LCRYPTO_ALIAS(OBJ_ln2nid); 449 450 const void * 451 OBJ_bsearch_(const void *key, const void *base, int num, int size, 452 int (*cmp)(const void *, const void *)) 453 { 454 return OBJ_bsearch_ex_(key, base, num, size, cmp, 0); 455 } 456 LCRYPTO_ALIAS(OBJ_bsearch_); 457 458 const void * 459 OBJ_bsearch_ex_(const void *key, const void *base_, int num, int size, 460 int (*cmp)(const void *, const void *), int flags) 461 { 462 const char *base = base_; 463 int l, h, i = 0, c = 0; 464 const char *p = NULL; 465 466 if (num == 0) 467 return (NULL); 468 l = 0; 469 h = num; 470 while (l < h) { 471 i = (l + h) / 2; 472 p = &(base[i * size]); 473 c = (*cmp)(key, p); 474 if (c < 0) 475 h = i; 476 else if (c > 0) 477 l = i + 1; 478 else 479 break; 480 } 481 if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)) 482 p = NULL; 483 else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) { 484 while (i > 0 && (*cmp)(key, &(base[(i - 1) * size])) == 0) 485 i--; 486 p = &(base[i * size]); 487 } 488 return (p); 489 } 490 491 /* Convert an object name into an ASN1_OBJECT 492 * if "noname" is not set then search for short and long names first. 493 * This will convert the "dotted" form into an object: unlike OBJ_txt2nid 494 * it can be used with any objects, not just registered ones. 495 */ 496 497 ASN1_OBJECT * 498 OBJ_txt2obj(const char *s, int no_name) 499 { 500 int nid; 501 502 if (!no_name) { 503 if ((nid = OBJ_sn2nid(s)) != NID_undef || 504 (nid = OBJ_ln2nid(s)) != NID_undef) 505 return OBJ_nid2obj(nid); 506 } 507 508 return t2i_ASN1_OBJECT_internal(s); 509 } 510 LCRYPTO_ALIAS(OBJ_txt2obj); 511 512 int 513 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *aobj, int no_name) 514 { 515 return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, no_name); 516 } 517 LCRYPTO_ALIAS(OBJ_obj2txt); 518 519 int 520 OBJ_txt2nid(const char *s) 521 { 522 ASN1_OBJECT *obj; 523 int nid; 524 525 obj = OBJ_txt2obj(s, 0); 526 nid = OBJ_obj2nid(obj); 527 ASN1_OBJECT_free(obj); 528 return nid; 529 } 530 LCRYPTO_ALIAS(OBJ_txt2nid); 531 532 int 533 OBJ_create_objects(BIO *in) 534 { 535 char buf[512]; 536 int i, num = 0; 537 char *o, *s, *l = NULL; 538 539 for (;;) { 540 s = o = NULL; 541 i = BIO_gets(in, buf, 512); 542 if (i <= 0) 543 return (num); 544 buf[i - 1] = '\0'; 545 if (!isalnum((unsigned char)buf[0])) 546 return (num); 547 o = s=buf; 548 while (isdigit((unsigned char)*s) || (*s == '.')) 549 s++; 550 if (*s != '\0') { 551 *(s++) = '\0'; 552 while (isspace((unsigned char)*s)) 553 s++; 554 if (*s == '\0') 555 s = NULL; 556 else { 557 l = s; 558 while ((*l != '\0') && 559 !isspace((unsigned char)*l)) 560 l++; 561 if (*l != '\0') { 562 *(l++) = '\0'; 563 while (isspace((unsigned char)*l)) 564 l++; 565 if (*l == '\0') 566 l = NULL; 567 } else 568 l = NULL; 569 } 570 } else 571 s = NULL; 572 if ((o == NULL) || (*o == '\0')) 573 return (num); 574 if (!OBJ_create(o, s, l)) 575 return (num); 576 num++; 577 } 578 /* return(num); */ 579 } 580 LCRYPTO_ALIAS(OBJ_create_objects); 581 582 int 583 OBJ_create(const char *oid, const char *sn, const char *ln) 584 { 585 ASN1_OBJECT *aobj = NULL; 586 unsigned char *buf = NULL; 587 int len, nid; 588 int ret = 0; 589 590 if ((len = a2d_ASN1_OBJECT(NULL, 0, oid, -1)) <= 0) 591 goto err; 592 593 if ((buf = calloc(1, len)) == NULL) { 594 OBJerror(ERR_R_MALLOC_FAILURE); 595 goto err; 596 } 597 598 if ((len = a2d_ASN1_OBJECT(buf, len, oid, -1)) == 0) 599 goto err; 600 601 nid = OBJ_new_nid(1); 602 if ((aobj = ASN1_OBJECT_create(nid, buf, len, sn, ln)) == NULL) 603 goto err; 604 605 ret = OBJ_add_object(aobj); 606 607 err: 608 ASN1_OBJECT_free(aobj); 609 free(buf); 610 611 return ret; 612 } 613 LCRYPTO_ALIAS(OBJ_create); 614 615 size_t 616 OBJ_length(const ASN1_OBJECT *obj) 617 { 618 if (obj == NULL) 619 return 0; 620 621 if (obj->length < 0) 622 return 0; 623 624 return obj->length; 625 } 626 LCRYPTO_ALIAS(OBJ_length); 627 628 const unsigned char * 629 OBJ_get0_data(const ASN1_OBJECT *obj) 630 { 631 if (obj == NULL) 632 return NULL; 633 634 return obj->data; 635 } 636 LCRYPTO_ALIAS(OBJ_get0_data); 637