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