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