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