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