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