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