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