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