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