| 1 |
/* |
| 2 |
* Copyright 1995-2026 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 "internal/thread_once.h" |
| 15 |
#include "internal/tsan_assist.h" |
| 16 |
#include <openssl/lhash.h> |
| 17 |
#include <openssl/asn1.h> |
| 18 |
#include "crypto/objects.h" |
| 19 |
#include <openssl/bn.h> |
| 20 |
#include "crypto/asn1.h" |
| 21 |
#include "obj_local.h" |
| 22 |
|
| 23 |
/* obj_dat.h is generated from objects.txt and obj_mac.{num,h} by obj_dat.pl */ |
| 24 |
#include "obj_dat.h" |
| 25 |
|
| 26 |
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); |
| 27 |
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); |
| 28 |
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); |
| 29 |
|
| 30 |
#define ADDED_DATA 0 |
| 31 |
#define ADDED_SNAME 1 |
| 32 |
#define ADDED_LNAME 2 |
| 33 |
#define ADDED_NID 3 |
| 34 |
|
| 35 |
struct added_obj_st { |
| 36 |
int type; |
| 37 |
ASN1_OBJECT *obj; |
| 38 |
}; |
| 39 |
|
| 40 |
static unsigned long added_obj_hash(const ADDED_OBJ *ca); |
| 41 |
static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb); |
| 42 |
static int add_object(const ASN1_OBJECT *obj, int indirect); |
| 43 |
|
| 44 |
static LHASH_OF(ADDED_OBJ) *added = NULL; |
| 45 |
static CRYPTO_RWLOCK *ossl_obj_lock = NULL; |
| 46 |
#ifdef TSAN_REQUIRES_LOCKING |
| 47 |
static CRYPTO_RWLOCK *ossl_obj_nid_lock = NULL; |
| 48 |
#endif |
| 49 |
|
| 50 |
static CRYPTO_ONCE ossl_obj_api_init = CRYPTO_ONCE_STATIC_INIT; |
| 51 |
|
| 52 |
static ossl_inline void objs_free_locks(void) |
| 53 |
{ |
| 54 |
CRYPTO_THREAD_lock_free(ossl_obj_lock); |
| 55 |
ossl_obj_lock = NULL; |
| 56 |
#ifdef TSAN_REQUIRES_LOCKING |
| 57 |
CRYPTO_THREAD_lock_free(ossl_obj_nid_lock); |
| 58 |
ossl_obj_nid_lock = NULL; |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
DEFINE_RUN_ONCE_STATIC(obj_api_initialise) |
| 63 |
{ |
| 64 |
ossl_obj_lock = CRYPTO_THREAD_lock_new(); |
| 65 |
if (ossl_obj_lock == NULL) |
| 66 |
return 0; |
| 67 |
|
| 68 |
#ifdef TSAN_REQUIRES_LOCKING |
| 69 |
ossl_obj_nid_lock = CRYPTO_THREAD_lock_new(); |
| 70 |
if (ossl_obj_nid_lock == NULL) { |
| 71 |
objs_free_locks(); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
#endif |
| 75 |
added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp); |
| 76 |
if (added == NULL) { |
| 77 |
objs_free_locks(); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
static ossl_inline int ossl_init_added_api(void) |
| 85 |
{ |
| 86 |
#ifndef OPENSSL_NO_AUTOLOAD_CONFIG |
| 87 |
/* Make sure we've loaded config before checking for any "added" objects */ |
| 88 |
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
| 89 |
#endif |
| 90 |
return RUN_ONCE(&ossl_obj_api_init, obj_api_initialise); |
| 91 |
} |
| 92 |
|
| 93 |
static ossl_inline int ossl_obj_write_lock(void) |
| 94 |
{ |
| 95 |
if (!ossl_init_added_api()) |
| 96 |
return 0; |
| 97 |
return CRYPTO_THREAD_write_lock(ossl_obj_lock); |
| 98 |
} |
| 99 |
|
| 100 |
static ossl_inline int ossl_obj_read_lock(void) |
| 101 |
{ |
| 102 |
if (!ossl_init_added_api()) |
| 103 |
return 0; |
| 104 |
return CRYPTO_THREAD_read_lock(ossl_obj_lock); |
| 105 |
} |
| 106 |
|
| 107 |
static ossl_inline void ossl_obj_unlock(void) |
| 108 |
{ |
| 109 |
CRYPTO_THREAD_unlock(ossl_obj_lock); |
| 110 |
} |
| 111 |
|
| 112 |
static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b) |
| 113 |
{ |
| 114 |
return strcmp((*a)->sn, nid_objs[*b].sn); |
| 115 |
} |
| 116 |
|
| 117 |
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); |
| 118 |
|
| 119 |
static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b) |
| 120 |
{ |
| 121 |
return strcmp((*a)->ln, nid_objs[*b].ln); |
| 122 |
} |
| 123 |
|
| 124 |
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); |
| 125 |
|
| 126 |
static unsigned long added_obj_hash(const ADDED_OBJ *ca) |
| 127 |
{ |
| 128 |
const ASN1_OBJECT *a; |
| 129 |
int i; |
| 130 |
unsigned long ret = 0; |
| 131 |
unsigned char *p; |
| 132 |
|
| 133 |
a = ca->obj; |
| 134 |
switch (ca->type) { |
| 135 |
case ADDED_DATA: |
| 136 |
ret = (unsigned long)a->length << 20UL; |
| 137 |
p = (unsigned char *)a->data; |
| 138 |
for (i = 0; i < a->length; i++) |
| 139 |
ret ^= p[i] << ((i * 3) % 24); |
| 140 |
break; |
| 141 |
case ADDED_SNAME: |
| 142 |
ret = OPENSSL_LH_strhash(a->sn); |
| 143 |
break; |
| 144 |
case ADDED_LNAME: |
| 145 |
ret = OPENSSL_LH_strhash(a->ln); |
| 146 |
break; |
| 147 |
case ADDED_NID: |
| 148 |
ret = a->nid; |
| 149 |
break; |
| 150 |
default: |
| 151 |
/* abort(); */ |
| 152 |
return 0; |
| 153 |
} |
| 154 |
ret &= 0x3fffffffL; |
| 155 |
ret |= ((unsigned long)ca->type) << 30L; |
| 156 |
return ret; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Compare two ASN1_OBJECTs, including SNAME and LNAME, but not NIDs. |
| 161 |
*/ |
| 162 |
static int obj_equivalent(const ASN1_OBJECT *a, const ASN1_OBJECT *b) |
| 163 |
{ |
| 164 |
return a->length == b->length |
| 165 |
&& memcmp(a->data, b->data, (size_t)a->length) == 0 |
| 166 |
&& (a->sn == NULL) == (b->sn == NULL) |
| 167 |
&& strcmp(a->sn ? a->sn : "", b->sn ? b->sn : "") == 0 |
| 168 |
&& (a->ln == NULL) == (b->ln == NULL) |
| 169 |
&& strcmp(a->ln ? a->ln : "", b->ln ? b->ln : "") == 0; |
| 170 |
} |
| 171 |
|
| 172 |
static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb) |
| 173 |
{ |
| 174 |
ASN1_OBJECT *a, *b; |
| 175 |
int i; |
| 176 |
|
| 177 |
i = ca->type - cb->type; |
| 178 |
if (i) |
| 179 |
return i; |
| 180 |
a = ca->obj; |
| 181 |
b = cb->obj; |
| 182 |
switch (ca->type) { |
| 183 |
case ADDED_DATA: |
| 184 |
i = (a->length - b->length); |
| 185 |
if (i) |
| 186 |
return i; |
| 187 |
return memcmp(a->data, b->data, (size_t)a->length); |
| 188 |
case ADDED_SNAME: |
| 189 |
if (a->sn == NULL) |
| 190 |
return -1; |
| 191 |
else if (b->sn == NULL) |
| 192 |
return 1; |
| 193 |
else |
| 194 |
return strcmp(a->sn, b->sn); |
| 195 |
case ADDED_LNAME: |
| 196 |
if (a->ln == NULL) |
| 197 |
return -1; |
| 198 |
else if (b->ln == NULL) |
| 199 |
return 1; |
| 200 |
else |
| 201 |
return strcmp(a->ln, b->ln); |
| 202 |
case ADDED_NID: |
| 203 |
return a->nid - b->nid; |
| 204 |
default: |
| 205 |
/* abort(); */ |
| 206 |
return 0; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
static void cleanup1_doall(ADDED_OBJ *a) |
| 211 |
{ |
| 212 |
a->obj->nid = 0; |
| 213 |
a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
| 214 |
} |
| 215 |
|
| 216 |
static void cleanup2_doall(ADDED_OBJ *a) |
| 217 |
{ |
| 218 |
a->obj->nid++; |
| 219 |
} |
| 220 |
|
| 221 |
static void cleanup3_doall(ADDED_OBJ *a) |
| 222 |
{ |
| 223 |
if (--a->obj->nid == 0) |
| 224 |
ASN1_OBJECT_free(a->obj); |
| 225 |
OPENSSL_free(a); |
| 226 |
} |
| 227 |
|
| 228 |
void ossl_obj_cleanup_int(void) |
| 229 |
{ |
| 230 |
if (added != NULL) { |
| 231 |
lh_ADDED_OBJ_set_down_load(added, 0); |
| 232 |
lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */ |
| 233 |
lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */ |
| 234 |
lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */ |
| 235 |
lh_ADDED_OBJ_free(added); |
| 236 |
added = NULL; |
| 237 |
} |
| 238 |
objs_free_locks(); |
| 239 |
} |
| 240 |
|
| 241 |
int OBJ_new_nid(int num) |
| 242 |
{ |
| 243 |
static TSAN_QUALIFIER int new_nid = NUM_NID; |
| 244 |
#ifdef TSAN_REQUIRES_LOCKING |
| 245 |
int i; |
| 246 |
|
| 247 |
ossl_obj_write_lock(); |
| 248 |
i = new_nid; |
| 249 |
new_nid += num; |
| 250 |
ossl_obj_unlock(); |
| 251 |
return i; |
| 252 |
#else |
| 253 |
return tsan_add(&new_nid, num); |
| 254 |
#endif |
| 255 |
} |
| 256 |
|
| 257 |
ASN1_OBJECT *OBJ_nid2obj(int n) |
| 258 |
{ |
| 259 |
ADDED_OBJ ad, *adp = NULL; |
| 260 |
ASN1_OBJECT ob; |
| 261 |
|
| 262 |
if (n == NID_undef |
| 263 |
|| (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef)) |
| 264 |
return (ASN1_OBJECT *)&(nid_objs[n]); |
| 265 |
|
| 266 |
ad.type = ADDED_NID; |
| 267 |
ad.obj = &ob; |
| 268 |
ob.nid = n; |
| 269 |
if (!ossl_obj_read_lock()) { |
| 270 |
ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK); |
| 271 |
return NULL; |
| 272 |
} |
| 273 |
adp = lh_ADDED_OBJ_retrieve(added, &ad); |
| 274 |
ossl_obj_unlock(); |
| 275 |
if (adp != NULL) |
| 276 |
return adp->obj; |
| 277 |
|
| 278 |
ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_NID); |
| 279 |
return NULL; |
| 280 |
} |
| 281 |
|
| 282 |
const char *OBJ_nid2sn(int n) |
| 283 |
{ |
| 284 |
ASN1_OBJECT *ob = OBJ_nid2obj(n); |
| 285 |
|
| 286 |
return ob == NULL ? NULL : ob->sn; |
| 287 |
} |
| 288 |
|
| 289 |
const char *OBJ_nid2ln(int n) |
| 290 |
{ |
| 291 |
ASN1_OBJECT *ob = OBJ_nid2obj(n); |
| 292 |
|
| 293 |
return ob == NULL ? NULL : ob->ln; |
| 294 |
} |
| 295 |
|
| 296 |
static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp) |
| 297 |
{ |
| 298 |
int j; |
| 299 |
const ASN1_OBJECT *a = *ap; |
| 300 |
const ASN1_OBJECT *b = &nid_objs[*bp]; |
| 301 |
|
| 302 |
j = (a->length - b->length); |
| 303 |
if (j) |
| 304 |
return j; |
| 305 |
if (a->length == 0) |
| 306 |
return 0; |
| 307 |
return memcmp(a->data, b->data, a->length); |
| 308 |
} |
| 309 |
|
| 310 |
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); |
| 311 |
|
| 312 |
static int ossl_obj_obj2nid(const ASN1_OBJECT *a) |
| 313 |
{ |
| 314 |
int nid = NID_undef; |
| 315 |
const unsigned int *op; |
| 316 |
ADDED_OBJ ad, *adp; |
| 317 |
|
| 318 |
if (a == NULL) |
| 319 |
return NID_undef; |
| 320 |
if (a->nid != NID_undef) |
| 321 |
return a->nid; |
| 322 |
if (a->length == 0) |
| 323 |
return NID_undef; |
| 324 |
|
| 325 |
op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ); |
| 326 |
if (op != NULL) |
| 327 |
return nid_objs[*op].nid; |
| 328 |
if (!ossl_obj_read_lock()) { |
| 329 |
ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK); |
| 330 |
return NID_undef; |
| 331 |
} |
| 332 |
ad.type = ADDED_DATA; |
| 333 |
ad.obj = (ASN1_OBJECT *)a; /* casting away const is harmless here */ |
| 334 |
adp = lh_ADDED_OBJ_retrieve(added, &ad); |
| 335 |
if (adp != NULL) |
| 336 |
nid = adp->obj->nid; |
| 337 |
ossl_obj_unlock(); |
| 338 |
return nid; |
| 339 |
} |
| 340 |
|
| 341 |
/* |
| 342 |
* Convert an object name into an ASN1_OBJECT if "noname" is not set then |
| 343 |
* search for short and long names first. This will convert the "dotted" form |
| 344 |
* into an object: unlike OBJ_txt2nid it can be used with any objects, not |
| 345 |
* just registered ones. |
| 346 |
*/ |
| 347 |
ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) |
| 348 |
{ |
| 349 |
int nid = NID_undef; |
| 350 |
ASN1_OBJECT *op = NULL; |
| 351 |
unsigned char *buf; |
| 352 |
unsigned char *p; |
| 353 |
const unsigned char *cp; |
| 354 |
int i, j; |
| 355 |
|
| 356 |
if (!no_name) { |
| 357 |
if ((nid = OBJ_sn2nid(s)) != NID_undef |
| 358 |
|| (nid = OBJ_ln2nid(s)) != NID_undef) { |
| 359 |
return OBJ_nid2obj(nid); |
| 360 |
} |
| 361 |
if (!ossl_isdigit(*s)) { |
| 362 |
ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_OBJECT_NAME); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/* Work out size of content octets */ |
| 368 |
i = a2d_ASN1_OBJECT(NULL, 0, s, -1); |
| 369 |
if (i <= 0) |
| 370 |
return NULL; |
| 371 |
|
| 372 |
/* Work out total size */ |
| 373 |
j = ASN1_object_size(0, i, V_ASN1_OBJECT); |
| 374 |
if (j < 0) |
| 375 |
return NULL; |
| 376 |
|
| 377 |
if ((buf = OPENSSL_malloc(j)) == NULL) |
| 378 |
return NULL; |
| 379 |
|
| 380 |
p = buf; |
| 381 |
/* Write out tag+length */ |
| 382 |
ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
| 383 |
/* Write out contents */ |
| 384 |
a2d_ASN1_OBJECT(p, i, s, -1); |
| 385 |
|
| 386 |
cp = buf; |
| 387 |
op = d2i_ASN1_OBJECT(NULL, &cp, j); |
| 388 |
OPENSSL_free(buf); |
| 389 |
return op; |
| 390 |
} |
| 391 |
|
| 392 |
int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) |
| 393 |
{ |
| 394 |
int i, n = 0, len, nid, first, use_bn; |
| 395 |
BIGNUM *bl; |
| 396 |
unsigned long l; |
| 397 |
const unsigned char *p; |
| 398 |
char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2]; |
| 399 |
const char *s; |
| 400 |
|
| 401 |
/* Ensure that, at every state, |buf| is NUL-terminated. */ |
| 402 |
if (buf != NULL && buf_len > 0) |
| 403 |
buf[0] = '\0'; |
| 404 |
|
| 405 |
if (a == NULL || a->data == NULL) |
| 406 |
return 0; |
| 407 |
|
| 408 |
if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) { |
| 409 |
s = OBJ_nid2ln(nid); |
| 410 |
if (s == NULL) |
| 411 |
s = OBJ_nid2sn(nid); |
| 412 |
if (s != NULL) { |
| 413 |
if (buf != NULL) |
| 414 |
OPENSSL_strlcpy(buf, s, buf_len); |
| 415 |
return (int)strlen(s); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
len = a->length; |
| 420 |
p = a->data; |
| 421 |
|
| 422 |
first = 1; |
| 423 |
bl = NULL; |
| 424 |
|
| 425 |
/* |
| 426 |
* RFC 2578 (STD 58) says this about OBJECT IDENTIFIERs: |
| 427 |
* |
| 428 |
* > 3.5. OBJECT IDENTIFIER values |
| 429 |
* > |
| 430 |
* > An OBJECT IDENTIFIER value is an ordered list of non-negative |
| 431 |
* > numbers. For the SMIv2, each number in the list is referred to as a |
| 432 |
* > sub-identifier, there are at most 128 sub-identifiers in a value, |
| 433 |
* > and each sub-identifier has a maximum value of 2^32-1 (4294967295 |
| 434 |
* > decimal). |
| 435 |
* |
| 436 |
* So a legitimate OID according to this RFC is at most (32 * 128 / 7), |
| 437 |
* i.e. 586 bytes long. |
| 438 |
* |
| 439 |
* Ref: https://datatracker.ietf.org/doc/html/rfc2578#section-3.5 |
| 440 |
*/ |
| 441 |
if (len > 586) |
| 442 |
goto err; |
| 443 |
|
| 444 |
while (len > 0) { |
| 445 |
l = 0; |
| 446 |
use_bn = 0; |
| 447 |
for (;;) { |
| 448 |
unsigned char c = *p++; |
| 449 |
|
| 450 |
len--; |
| 451 |
if (len == 0 && (c & 0x80) != 0) |
| 452 |
goto err; |
| 453 |
if (use_bn) { |
| 454 |
if (!BN_add_word(bl, c & 0x7f)) |
| 455 |
goto err; |
| 456 |
} else { |
| 457 |
l |= c & 0x7f; |
| 458 |
} |
| 459 |
if ((c & 0x80) == 0) |
| 460 |
break; |
| 461 |
if (!use_bn && l > (ULONG_MAX >> 7L)) { |
| 462 |
if (bl == NULL && (bl = BN_new()) == NULL) |
| 463 |
goto err; |
| 464 |
if (!BN_set_word(bl, l)) |
| 465 |
goto err; |
| 466 |
use_bn = 1; |
| 467 |
} |
| 468 |
if (use_bn) { |
| 469 |
if (!BN_lshift(bl, bl, 7)) |
| 470 |
goto err; |
| 471 |
} else { |
| 472 |
l <<= 7L; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
if (first) { |
| 477 |
first = 0; |
| 478 |
if (l >= 80) { |
| 479 |
i = 2; |
| 480 |
if (use_bn) { |
| 481 |
if (!BN_sub_word(bl, 80)) |
| 482 |
goto err; |
| 483 |
} else { |
| 484 |
l -= 80; |
| 485 |
} |
| 486 |
} else { |
| 487 |
i = (int)(l / 40); |
| 488 |
l -= (long)(i * 40); |
| 489 |
} |
| 490 |
if (buf != NULL && buf_len > 1) { |
| 491 |
*buf++ = i + '0'; |
| 492 |
*buf = '\0'; |
| 493 |
buf_len--; |
| 494 |
} |
| 495 |
n++; |
| 496 |
} |
| 497 |
|
| 498 |
if (use_bn) { |
| 499 |
char *bndec; |
| 500 |
bndec = BN_bn2dec(bl); |
| 501 |
if (!bndec) |
| 502 |
goto err; |
| 503 |
i = (int)strlen(bndec); |
| 504 |
if (buf != NULL) { |
| 505 |
if (buf_len > 1) { |
| 506 |
*buf++ = '.'; |
| 507 |
*buf = '\0'; |
| 508 |
buf_len--; |
| 509 |
} |
| 510 |
OPENSSL_strlcpy(buf, bndec, buf_len); |
| 511 |
if (i > buf_len) { |
| 512 |
buf += buf_len; |
| 513 |
buf_len = 0; |
| 514 |
} else { |
| 515 |
buf += i; |
| 516 |
buf_len -= i; |
| 517 |
} |
| 518 |
} |
| 519 |
n++; |
| 520 |
n += i; |
| 521 |
OPENSSL_free(bndec); |
| 522 |
} else { |
| 523 |
BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l); |
| 524 |
i = (int)strlen(tbuf); |
| 525 |
if (buf && buf_len > 0) { |
| 526 |
OPENSSL_strlcpy(buf, tbuf, buf_len); |
| 527 |
if (i > buf_len) { |
| 528 |
buf += buf_len; |
| 529 |
buf_len = 0; |
| 530 |
} else { |
| 531 |
buf += i; |
| 532 |
buf_len -= i; |
| 533 |
} |
| 534 |
} |
| 535 |
n += i; |
| 536 |
l = 0; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
BN_free(bl); |
| 541 |
return n; |
| 542 |
|
| 543 |
err: |
| 544 |
BN_free(bl); |
| 545 |
return -1; |
| 546 |
} |
| 547 |
|
| 548 |
int OBJ_txt2nid(const char *s) |
| 549 |
{ |
| 550 |
ASN1_OBJECT *obj = OBJ_txt2obj(s, 0); |
| 551 |
int nid = NID_undef; |
| 552 |
|
| 553 |
if (obj != NULL) { |
| 554 |
nid = OBJ_obj2nid(obj); |
| 555 |
ASN1_OBJECT_free(obj); |
| 556 |
} |
| 557 |
return nid; |
| 558 |
} |
| 559 |
|
| 560 |
int OBJ_ln2nid(const char *s) |
| 561 |
{ |
| 562 |
ASN1_OBJECT o; |
| 563 |
const ASN1_OBJECT *oo = &o; |
| 564 |
ADDED_OBJ ad, *adp; |
| 565 |
const unsigned int *op; |
| 566 |
int nid = NID_undef; |
| 567 |
|
| 568 |
o.ln = s; |
| 569 |
op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN); |
| 570 |
if (op != NULL) |
| 571 |
return nid_objs[*op].nid; |
| 572 |
if (!ossl_obj_read_lock()) { |
| 573 |
ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK); |
| 574 |
return NID_undef; |
| 575 |
} |
| 576 |
ad.type = ADDED_LNAME; |
| 577 |
ad.obj = &o; |
| 578 |
adp = lh_ADDED_OBJ_retrieve(added, &ad); |
| 579 |
if (adp != NULL) |
| 580 |
nid = adp->obj->nid; |
| 581 |
ossl_obj_unlock(); |
| 582 |
return nid; |
| 583 |
} |
| 584 |
|
| 585 |
int OBJ_sn2nid(const char *s) |
| 586 |
{ |
| 587 |
ASN1_OBJECT o; |
| 588 |
const ASN1_OBJECT *oo = &o; |
| 589 |
ADDED_OBJ ad, *adp; |
| 590 |
const unsigned int *op; |
| 591 |
int nid = NID_undef; |
| 592 |
|
| 593 |
o.sn = s; |
| 594 |
op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN); |
| 595 |
if (op != NULL) |
| 596 |
return nid_objs[*op].nid; |
| 597 |
if (!ossl_obj_read_lock()) { |
| 598 |
ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK); |
| 599 |
return NID_undef; |
| 600 |
} |
| 601 |
ad.type = ADDED_SNAME; |
| 602 |
ad.obj = &o; |
| 603 |
adp = lh_ADDED_OBJ_retrieve(added, &ad); |
| 604 |
if (adp != NULL) |
| 605 |
nid = adp->obj->nid; |
| 606 |
ossl_obj_unlock(); |
| 607 |
return nid; |
| 608 |
} |
| 609 |
|
| 610 |
const void *OBJ_bsearch_(const void *key, const void *base, int num, int size, |
| 611 |
int (*cmp)(const void *, const void *)) |
| 612 |
{ |
| 613 |
return OBJ_bsearch_ex_(key, base, num, size, cmp, 0); |
| 614 |
} |
| 615 |
|
| 616 |
const void *OBJ_bsearch_ex_(const void *key, const void *base, int num, |
| 617 |
int size, |
| 618 |
int (*cmp)(const void *, const void *), |
| 619 |
int flags) |
| 620 |
{ |
| 621 |
const char *p = ossl_bsearch(key, base, num, size, cmp, flags); |
| 622 |
|
| 623 |
#ifdef CHARSET_EBCDIC |
| 624 |
/* |
| 625 |
* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I |
| 626 |
* don't have perl (yet), we revert to a *LINEAR* search when the object |
| 627 |
* wasn't found in the binary search. |
| 628 |
*/ |
| 629 |
if (p == NULL) { |
| 630 |
const char *base_ = base; |
| 631 |
int i = 0, c = 0; |
| 632 |
const char *p1; |
| 633 |
|
| 634 |
for (i = 0; i < num; ++i) { |
| 635 |
p1 = &(base_[i * size]); |
| 636 |
c = (*cmp)(key, p1); |
| 637 |
if (c == 0 |
| 638 |
|| (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))) |
| 639 |
return p1; |
| 640 |
} |
| 641 |
} |
| 642 |
#endif |
| 643 |
return p; |
| 644 |
} |
| 645 |
|
| 646 |
/* |
| 647 |
* Parse a BIO sink to create some extra oid's objects. |
| 648 |
* Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN> |
| 649 |
*/ |
| 650 |
int OBJ_create_objects(BIO *in) |
| 651 |
{ |
| 652 |
char buf[512]; |
| 653 |
int i, num = 0; |
| 654 |
char *o, *s, *l = NULL; |
| 655 |
|
| 656 |
for (;;) { |
| 657 |
s = o = NULL; |
| 658 |
i = BIO_gets(in, buf, 512); |
| 659 |
if (i <= 0) |
| 660 |
return num; |
| 661 |
buf[i - 1] = '\0'; |
| 662 |
if (!ossl_isalnum(buf[0])) |
| 663 |
return num; |
| 664 |
o = s = buf; |
| 665 |
while (ossl_isdigit(*s) || *s == '.') |
| 666 |
s++; |
| 667 |
if (*s != '\0') { |
| 668 |
*(s++) = '\0'; |
| 669 |
while (ossl_isspace(*s)) |
| 670 |
s++; |
| 671 |
if (*s == '\0') { |
| 672 |
s = NULL; |
| 673 |
} else { |
| 674 |
l = s; |
| 675 |
while (*l != '\0' && !ossl_isspace(*l)) |
| 676 |
l++; |
| 677 |
if (*l != '\0') { |
| 678 |
*(l++) = '\0'; |
| 679 |
while (ossl_isspace(*l)) |
| 680 |
l++; |
| 681 |
if (*l == '\0') { |
| 682 |
l = NULL; |
| 683 |
} |
| 684 |
} else { |
| 685 |
l = NULL; |
| 686 |
} |
| 687 |
} |
| 688 |
} else { |
| 689 |
s = NULL; |
| 690 |
} |
| 691 |
if (*o == '\0') |
| 692 |
return num; |
| 693 |
if (!OBJ_create(o, s, l)) |
| 694 |
return num; |
| 695 |
num++; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
int OBJ_create(const char *oid, const char *sn, const char *ln) |
| 700 |
{ |
| 701 |
ASN1_OBJECT *tmpoid = NULL; |
| 702 |
int ok = NID_undef; |
| 703 |
|
| 704 |
/* With no arguments at all, nothing can be done */ |
| 705 |
if (oid == NULL && sn == NULL && ln == NULL) { |
| 706 |
ERR_raise(ERR_LIB_OBJ, ERR_R_PASSED_INVALID_ARGUMENT); |
| 707 |
return NID_undef; |
| 708 |
} |
| 709 |
|
| 710 |
/* Check to see if short or long name already present */ |
| 711 |
if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef) |
| 712 |
|| (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) { |
| 713 |
ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS); |
| 714 |
return NID_undef; |
| 715 |
} |
| 716 |
|
| 717 |
if (oid != NULL) { |
| 718 |
/* Convert numerical OID string to an ASN1_OBJECT structure */ |
| 719 |
tmpoid = OBJ_txt2obj(oid, 1); |
| 720 |
if (tmpoid == NULL) |
| 721 |
return NID_undef; |
| 722 |
} else { |
| 723 |
/* Create a no-OID ASN1_OBJECT */ |
| 724 |
tmpoid = ASN1_OBJECT_new(); |
| 725 |
if (tmpoid == NULL) { |
| 726 |
ERR_raise(ERR_LIB_OBJ, ERR_R_ASN1_LIB); |
| 727 |
return NID_undef; |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
/* If NID is not NID_undef then object already exists */ |
| 732 |
if (oid != NULL |
| 733 |
&& ossl_obj_obj2nid(tmpoid) != NID_undef) { |
| 734 |
ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS); |
| 735 |
goto err; |
| 736 |
} |
| 737 |
|
| 738 |
tmpoid->nid = NID_undef; |
| 739 |
tmpoid->sn = (char *)sn; |
| 740 |
tmpoid->ln = (char *)ln; |
| 741 |
|
| 742 |
ok = add_object(tmpoid, 1); |
| 743 |
|
| 744 |
tmpoid->sn = NULL; |
| 745 |
tmpoid->ln = NULL; |
| 746 |
|
| 747 |
err: |
| 748 |
ASN1_OBJECT_free(tmpoid); |
| 749 |
return ok; |
| 750 |
} |
| 751 |
|
| 752 |
size_t OBJ_length(const ASN1_OBJECT *obj) |
| 753 |
{ |
| 754 |
if (obj == NULL) |
| 755 |
return 0; |
| 756 |
return obj->length; |
| 757 |
} |
| 758 |
|
| 759 |
const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj) |
| 760 |
{ |
| 761 |
if (obj == NULL) |
| 762 |
return NULL; |
| 763 |
return obj->data; |
| 764 |
} |
| 765 |
|
| 766 |
static int add_object(const ASN1_OBJECT *obj, int indirect) |
| 767 |
{ |
| 768 |
ASN1_OBJECT *o = NULL, *dup = NULL; |
| 769 |
ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop[4]; |
| 770 |
int i, ret = NID_undef, nid = obj->nid; |
| 771 |
|
| 772 |
/* |
| 773 |
* Indirect calls leave the NID unspecified, in which case we generate a |
| 774 |
* fresh NID here. Direct calls via `OBJ_add_object()` must explicitly |
| 775 |
* specify the nid, and we then also check against the compile-time bsearch |
| 776 |
* lists that the indirect calls have checked while holding a read lock. |
| 777 |
*/ |
| 778 |
if (indirect) { |
| 779 |
if (nid != NID_undef |
| 780 |
|| (nid = OBJ_new_nid(1)) < NUM_NID |
| 781 |
|| (o = OBJ_dup(obj)) == NULL) |
| 782 |
return ret; |
| 783 |
o->nid = nid; |
| 784 |
} else if (nid < NUM_NID |
| 785 |
|| (obj->data != NULL |
| 786 |
&& OBJ_bsearch_obj(&obj, obj_objs, NUM_OBJ) != NULL) |
| 787 |
|| (obj->sn != NULL |
| 788 |
&& OBJ_bsearch_sn(&obj, sn_objs, NUM_SN) != NULL) |
| 789 |
|| (obj->ln != NULL |
| 790 |
&& OBJ_bsearch_ln(&obj, ln_objs, NUM_LN) != NULL) |
| 791 |
|| (o = OBJ_dup(obj)) == NULL) { |
| 792 |
return ret; |
| 793 |
} |
| 794 |
|
| 795 |
if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL |
| 796 |
|| (o->length != 0 |
| 797 |
&& obj->data != NULL |
| 798 |
&& (ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) |
| 799 |
|| (o->sn != NULL |
| 800 |
&& (ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL) |
| 801 |
|| (o->ln != NULL |
| 802 |
&& (ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)) |
| 803 |
goto err2; |
| 804 |
|
| 805 |
if (!ossl_obj_write_lock()) { |
| 806 |
ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK); |
| 807 |
goto err2; |
| 808 |
} |
| 809 |
|
| 810 |
for (i = ADDED_DATA; i <= ADDED_NID; i++) { |
| 811 |
if (ao[i] == NULL) |
| 812 |
continue; |
| 813 |
ao[i]->type = i; |
| 814 |
ao[i]->obj = o; |
| 815 |
if ((aop[i] = lh_ADDED_OBJ_retrieve(added, ao[i])) != NULL) |
| 816 |
dup = aop[i]->obj; |
| 817 |
} |
| 818 |
|
| 819 |
if (dup != NULL) { |
| 820 |
/* |
| 821 |
* We found a possible conflict. If the caller did not specify a NID, |
| 822 |
* return NID_undef to signal the conflict. Otherwise, if the NID and |
| 823 |
* parameters are unchanged, return the old NID, else NID_undef to |
| 824 |
* signal the conflict. This ensures that object registrations are |
| 825 |
* immutable. |
| 826 |
* |
| 827 |
* In the future, ideally also return an equivalent existing NID also |
| 828 |
* when the caller did not specify a NID, as in OBJ_create(). |
| 829 |
*/ |
| 830 |
if (obj->nid == dup->nid && obj_equivalent(obj, dup)) |
| 831 |
ret = dup->nid; |
| 832 |
goto err; |
| 833 |
} |
| 834 |
|
| 835 |
for (i = ADDED_DATA; i <= ADDED_NID; i++) { |
| 836 |
if (ao[i] == NULL) |
| 837 |
continue; |
| 838 |
(void)lh_ADDED_OBJ_insert(added, ao[i]); |
| 839 |
if (lh_ADDED_OBJ_error(added)) { |
| 840 |
while (i-- > ADDED_DATA) { |
| 841 |
if (ao[i] != NULL) |
| 842 |
lh_ADDED_OBJ_delete(added, ao[i]); |
| 843 |
} |
| 844 |
ERR_raise(ERR_LIB_OBJ, ERR_R_CRYPTO_LIB); |
| 845 |
goto err; |
| 846 |
} |
| 847 |
} |
| 848 |
o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA); |
| 849 |
|
| 850 |
ossl_obj_unlock(); |
| 851 |
return o->nid; |
| 852 |
|
| 853 |
err: |
| 854 |
ossl_obj_unlock(); |
| 855 |
err2: |
| 856 |
for (i = ADDED_DATA; i <= ADDED_NID; i++) |
| 857 |
OPENSSL_free(ao[i]); |
| 858 |
ASN1_OBJECT_free(o); |
| 859 |
return ret; |
| 860 |
} |
| 861 |
|
| 862 |
int OBJ_add_object(const ASN1_OBJECT *obj) |
| 863 |
{ |
| 864 |
return add_object(obj, 0); |
| 865 |
} |
| 866 |
|
| 867 |
int OBJ_obj2nid(const ASN1_OBJECT *a) |
| 868 |
{ |
| 869 |
return ossl_obj_obj2nid(a); |
| 870 |
} |