1 /* $OpenBSD: obj_dat.c,v 1.91 2024/07/14 14:32:45 jsing 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 170 cleanup2_doall(ADDED_OBJ *a) 171 { 172 a->obj->nid++; 173 } 174 175 static void 176 cleanup3_doall(ADDED_OBJ *a) 177 { 178 if (--a->obj->nid == 0) 179 ASN1_OBJECT_free(a->obj); 180 free(a); 181 } 182 183 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ) 184 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ) 185 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ) 186 187 void 188 OBJ_cleanup(void) 189 { 190 if (added == NULL) 191 return; 192 193 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */ 194 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */ 195 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */ 196 lh_ADDED_OBJ_free(added); 197 added = NULL; 198 } 199 LCRYPTO_ALIAS(OBJ_cleanup); 200 201 int 202 OBJ_new_nid(int num) 203 { 204 int i; 205 206 i = new_nid; 207 new_nid += num; 208 return (i); 209 } 210 LCRYPTO_ALIAS(OBJ_new_nid); 211 212 static int 213 OBJ_add_object(const ASN1_OBJECT *obj) 214 { 215 ASN1_OBJECT *o = NULL; 216 ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop; 217 int i; 218 219 if (added == NULL) 220 added = lh_ADDED_OBJ_new(); 221 if (added == NULL) 222 goto err; 223 if (obj == NULL || obj->nid == NID_undef) 224 goto err; 225 if ((o = OBJ_dup(obj)) == NULL) 226 goto err; 227 if (!(ao[ADDED_NID] = malloc(sizeof(ADDED_OBJ)))) 228 goto err2; 229 if ((o->length != 0) && (obj->data != NULL)) 230 if (!(ao[ADDED_DATA] = malloc(sizeof(ADDED_OBJ)))) 231 goto err2; 232 if (o->sn != NULL) 233 if (!(ao[ADDED_SNAME] = malloc(sizeof(ADDED_OBJ)))) 234 goto err2; 235 if (o->ln != NULL) 236 if (!(ao[ADDED_LNAME] = malloc(sizeof(ADDED_OBJ)))) 237 goto err2; 238 239 for (i = ADDED_DATA; i <= ADDED_NID; i++) { 240 if (ao[i] != NULL) { 241 ao[i]->type = i; 242 ao[i]->obj = o; 243 aop = lh_ADDED_OBJ_insert(added, ao[i]); 244 /* memory leak, but should not normally matter */ 245 free(aop); 246 } 247 } 248 o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | 249 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | 250 ASN1_OBJECT_FLAG_DYNAMIC_DATA); 251 252 return (o->nid); 253 254 err2: 255 OBJerror(ERR_R_MALLOC_FAILURE); 256 err: 257 for (i = ADDED_DATA; i <= ADDED_NID; i++) 258 free(ao[i]); 259 ASN1_OBJECT_free(o); 260 return (NID_undef); 261 } 262 263 ASN1_OBJECT * 264 OBJ_nid2obj(int nid) 265 { 266 if (nid >= 0 && nid < NUM_NID) { 267 if (nid == NID_undef || nid_objs[nid].nid != NID_undef) 268 return (ASN1_OBJECT *)&nid_objs[nid]; 269 270 goto unknown; 271 } 272 273 /* XXX - locking. */ 274 if (added != NULL) { 275 ASN1_OBJECT aobj = { 276 .nid = nid, 277 }; 278 ADDED_OBJ needle = { 279 .type = ADDED_NID, 280 .obj = &aobj, 281 }; 282 ADDED_OBJ *found; 283 284 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 285 return found->obj; 286 } 287 288 unknown: 289 OBJerror(OBJ_R_UNKNOWN_NID); 290 291 return NULL; 292 } 293 LCRYPTO_ALIAS(OBJ_nid2obj); 294 295 const char * 296 OBJ_nid2sn(int nid) 297 { 298 ASN1_OBJECT *aobj; 299 300 if ((aobj = OBJ_nid2obj(nid)) == NULL) 301 return NULL; 302 303 return aobj->sn; 304 } 305 LCRYPTO_ALIAS(OBJ_nid2sn); 306 307 const char * 308 OBJ_nid2ln(int nid) 309 { 310 ASN1_OBJECT *aobj; 311 312 if ((aobj = OBJ_nid2obj(nid)) == NULL) 313 return NULL; 314 315 return aobj->ln; 316 } 317 LCRYPTO_ALIAS(OBJ_nid2ln); 318 319 static int 320 obj_objs_cmp(const void *aobj, const void *b) 321 { 322 const unsigned int *nid = b; 323 324 OPENSSL_assert(*nid < NUM_NID); 325 326 return OBJ_cmp(aobj, &nid_objs[*nid]); 327 } 328 329 int 330 OBJ_obj2nid(const ASN1_OBJECT *aobj) 331 { 332 const unsigned int *nid; 333 334 if (aobj == NULL || aobj->length == 0) 335 return NID_undef; 336 337 if (aobj->nid != NID_undef) 338 return aobj->nid; 339 340 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ 341 if (added != NULL) { 342 ADDED_OBJ needle = { 343 .type = ADDED_DATA, 344 .obj = (ASN1_OBJECT *)aobj, 345 }; 346 ADDED_OBJ *found; 347 348 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 349 return found->obj->nid; 350 } 351 352 /* obj_objs holds built-in obj NIDs in ascending OBJ_cmp() order. */ 353 nid = bsearch(aobj, obj_objs, NUM_OBJ, sizeof(unsigned int), obj_objs_cmp); 354 if (nid != NULL) 355 return *nid; 356 357 return NID_undef; 358 } 359 LCRYPTO_ALIAS(OBJ_obj2nid); 360 361 static int 362 sn_objs_cmp(const void *sn, const void *b) 363 { 364 const unsigned int *nid = b; 365 366 OPENSSL_assert(*nid < NUM_NID); 367 368 return strcmp(sn, nid_objs[*nid].sn); 369 } 370 371 int 372 OBJ_sn2nid(const char *sn) 373 { 374 const unsigned int *nid; 375 376 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ 377 if (added != NULL) { 378 ASN1_OBJECT aobj = { 379 .sn = sn, 380 }; 381 ADDED_OBJ needle = { 382 .type = ADDED_SNAME, 383 .obj = &aobj, 384 }; 385 ADDED_OBJ *found; 386 387 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 388 return found->obj->nid; 389 } 390 391 /* sn_objs holds NIDs in ascending alphabetical order of SN. */ 392 nid = bsearch(sn, sn_objs, NUM_SN, sizeof(unsigned int), sn_objs_cmp); 393 if (nid != NULL) 394 return *nid; 395 396 return NID_undef; 397 } 398 LCRYPTO_ALIAS(OBJ_sn2nid); 399 400 static int 401 ln_objs_cmp(const void *ln, const void *b) 402 { 403 const unsigned int *nid = b; 404 405 OPENSSL_assert(*nid < NUM_NID); 406 407 return strcmp(ln, nid_objs[*nid].ln); 408 } 409 410 int 411 OBJ_ln2nid(const char *ln) 412 { 413 const unsigned int *nid; 414 415 /* XXX - locking. OpenSSL 3 moved this after built-in object lookup. */ 416 if (added != NULL) { 417 ASN1_OBJECT aobj = { 418 .ln = ln, 419 }; 420 ADDED_OBJ needle = { 421 .type = ADDED_LNAME, 422 .obj = &aobj, 423 }; 424 ADDED_OBJ *found; 425 426 if ((found = lh_ADDED_OBJ_retrieve(added, &needle)) != NULL) 427 return found->obj->nid; 428 } 429 430 /* ln_objs holds NIDs in ascending alphabetical order of LN. */ 431 nid = bsearch(ln, ln_objs, NUM_LN, sizeof(unsigned int), ln_objs_cmp); 432 if (nid != NULL) 433 return *nid; 434 435 return NID_undef; 436 } 437 LCRYPTO_ALIAS(OBJ_ln2nid); 438 439 /* Convert an object name into an ASN1_OBJECT 440 * if "noname" is not set then search for short and long names first. 441 * This will convert the "dotted" form into an object: unlike OBJ_txt2nid 442 * it can be used with any objects, not just registered ones. 443 */ 444 445 ASN1_OBJECT * 446 OBJ_txt2obj(const char *s, int no_name) 447 { 448 int nid; 449 450 if (!no_name) { 451 if ((nid = OBJ_sn2nid(s)) != NID_undef || 452 (nid = OBJ_ln2nid(s)) != NID_undef) 453 return OBJ_nid2obj(nid); 454 } 455 456 return t2i_ASN1_OBJECT_internal(s); 457 } 458 LCRYPTO_ALIAS(OBJ_txt2obj); 459 460 int 461 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *aobj, int no_name) 462 { 463 return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, no_name); 464 } 465 LCRYPTO_ALIAS(OBJ_obj2txt); 466 467 int 468 OBJ_txt2nid(const char *s) 469 { 470 ASN1_OBJECT *obj; 471 int nid; 472 473 obj = OBJ_txt2obj(s, 0); 474 nid = OBJ_obj2nid(obj); 475 ASN1_OBJECT_free(obj); 476 return nid; 477 } 478 LCRYPTO_ALIAS(OBJ_txt2nid); 479 480 int 481 OBJ_create_objects(BIO *in) 482 { 483 char buf[512]; 484 int i, num = 0; 485 char *o, *s, *l = NULL; 486 487 for (;;) { 488 s = o = NULL; 489 i = BIO_gets(in, buf, 512); 490 if (i <= 0) 491 return (num); 492 buf[i - 1] = '\0'; 493 if (!isalnum((unsigned char)buf[0])) 494 return (num); 495 o = s=buf; 496 while (isdigit((unsigned char)*s) || (*s == '.')) 497 s++; 498 if (*s != '\0') { 499 *(s++) = '\0'; 500 while (isspace((unsigned char)*s)) 501 s++; 502 if (*s == '\0') 503 s = NULL; 504 else { 505 l = s; 506 while ((*l != '\0') && 507 !isspace((unsigned char)*l)) 508 l++; 509 if (*l != '\0') { 510 *(l++) = '\0'; 511 while (isspace((unsigned char)*l)) 512 l++; 513 if (*l == '\0') 514 l = NULL; 515 } else 516 l = NULL; 517 } 518 } else 519 s = NULL; 520 if ((o == NULL) || (*o == '\0')) 521 return (num); 522 if (!OBJ_create(o, s, l)) 523 return (num); 524 num++; 525 } 526 /* return(num); */ 527 } 528 LCRYPTO_ALIAS(OBJ_create_objects); 529 530 int 531 OBJ_create(const char *oid, const char *sn, const char *ln) 532 { 533 ASN1_OBJECT *aobj = NULL; 534 unsigned char *buf = NULL; 535 int len, nid; 536 int ret = 0; 537 538 if ((len = a2d_ASN1_OBJECT(NULL, 0, oid, -1)) <= 0) 539 goto err; 540 541 if ((buf = calloc(1, len)) == NULL) { 542 OBJerror(ERR_R_MALLOC_FAILURE); 543 goto err; 544 } 545 546 if ((len = a2d_ASN1_OBJECT(buf, len, oid, -1)) == 0) 547 goto err; 548 549 nid = OBJ_new_nid(1); 550 if ((aobj = ASN1_OBJECT_create(nid, buf, len, sn, ln)) == NULL) 551 goto err; 552 553 ret = OBJ_add_object(aobj); 554 555 err: 556 ASN1_OBJECT_free(aobj); 557 free(buf); 558 559 return ret; 560 } 561 LCRYPTO_ALIAS(OBJ_create); 562 563 size_t 564 OBJ_length(const ASN1_OBJECT *obj) 565 { 566 if (obj == NULL) 567 return 0; 568 569 if (obj->length < 0) 570 return 0; 571 572 return obj->length; 573 } 574 LCRYPTO_ALIAS(OBJ_length); 575 576 const unsigned char * 577 OBJ_get0_data(const ASN1_OBJECT *obj) 578 { 579 if (obj == NULL) 580 return NULL; 581 582 return obj->data; 583 } 584 LCRYPTO_ALIAS(OBJ_get0_data); 585