Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/obj/obj.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/obj.h>
5
6 #ifndef __STDC_FORMAT_MACROS
7 #define __STDC_FORMAT_MACROS
8 #endif
9 #include <inttypes.h>
10
11 #include <limits.h>
12 #include <string.h>
13
14 #include <openssl/asn1.h>
15 #include <openssl/bytestring.h>
16 #include <openssl/err.h>
17 #include <openssl/lhash.h>
18 #include <openssl/mem.h>
19 #include <openssl/thread.h>
20
21 #include "../asn1/internal.h"
22 #include "../internal.h"
23 #include "../lhash/internal.h"
24
25 // obj_data.h must be included after the definition of |ASN1_OBJECT|.
26 #include "obj_dat.h"
27
28
29 DEFINE_LHASH_OF(ASN1_OBJECT)
30
31 static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT;
32 // These globals are protected by |global_added_lock|.
33 static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL;
34 static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL;
35 static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL;
36 static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL;
37
38 static struct CRYPTO_STATIC_MUTEX global_next_nid_lock =
39 CRYPTO_STATIC_MUTEX_INIT;
40 static unsigned global_next_nid = NUM_NID;
41
42 static int obj_next_nid(void) {
43 int ret;
44
45 CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock);
46 ret = global_next_nid++;
47 CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock);
48
49 return ret;
50 }
51
52 ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) {
53 ASN1_OBJECT *r;
54 unsigned char *data = NULL;
55 char *sn = NULL, *ln = NULL;
56
57 if (o == NULL) {
58 return NULL;
59 }
60
61 if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
62 // TODO(fork): this is a little dangerous.
63 return (ASN1_OBJECT *)o;
64 }
65
66 r = ASN1_OBJECT_new();
67 if (r == NULL) {
68 OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB);
69 return NULL;
70 }
71 r->ln = r->sn = NULL;
72
73 // once data is attached to an object, it remains const
74 r->data = OPENSSL_memdup(o->data, o->length);
75 if (o->length != 0 && r->data == NULL) {
76 goto err;
77 }
78
79 r->length = o->length;
80 r->nid = o->nid;
81
82 if (o->ln != NULL) {
83 ln = OPENSSL_strdup(o->ln);
84 if (ln == NULL) {
85 goto err;
86 }
87 }
88
89 if (o->sn != NULL) {
90 sn = OPENSSL_strdup(o->sn);
91 if (sn == NULL) {
92 goto err;
93 }
94 }
95
96 r->sn = sn;
97 r->ln = ln;
98
99 r->flags =
100 o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
101 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
102 return r;
103
104 err:
105 OPENSSL_free(ln);
106 OPENSSL_free(sn);
107 OPENSSL_free(data);
108 OPENSSL_free(r);
109 return NULL;
110 }
111
112 int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
113 if (a->length < b->length) {
114 return -1;
115 } else if (a->length > b->length) {
116 return 1;
117 }
118 return OPENSSL_memcmp(a->data, b->data, a->length);
119 }
120
121 const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj) {
122 if (obj == NULL) {
123 return NULL;
124 }
125
126 return obj->data;
127 }
128
129 size_t OBJ_length(const ASN1_OBJECT *obj) {
130 if (obj == NULL || obj->length < 0) {
131 return 0;
132 }
133
134 return (size_t)obj->length;
135 }
136
137 static const ASN1_OBJECT *get_builtin_object(int nid) {
138 // |NID_undef| is stored separately, so all the indices are off by one. The
139 // caller of this function must have a valid built-in, non-undef NID.
140 BSSL_CHECK(nid > 0 && nid < NUM_NID);
141 return &kObjects[nid - 1];
142 }
143
144 // obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is
145 // an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
146 // unsigned int in the array.
147 static int obj_cmp(const void *key, const void *element) {
148 uint16_t nid = *((const uint16_t *)element);
149 return OBJ_cmp(key, get_builtin_object(nid));
150 }
151
152 int OBJ_obj2nid(const ASN1_OBJECT *obj) {
153 if (obj == NULL) {
154 return NID_undef;
155 }
156
157 if (obj->nid != 0) {
158 return obj->nid;
159 }
160
161 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
162 if (global_added_by_data != NULL) {
163 ASN1_OBJECT *match;
164
165 match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj);
166 if (match != NULL) {
167 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
168 return match->nid;
169 }
170 }
171 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
172
173 const uint16_t *nid_ptr =
174 bsearch(obj, kNIDsInOIDOrder, OPENSSL_ARRAY_SIZE(kNIDsInOIDOrder),
175 sizeof(kNIDsInOIDOrder[0]), obj_cmp);
176 if (nid_ptr == NULL) {
177 return NID_undef;
178 }
179
180 return get_builtin_object(*nid_ptr)->nid;
181 }
182
183 int OBJ_cbs2nid(const CBS *cbs) {
184 if (CBS_len(cbs) > INT_MAX) {
185 return NID_undef;
186 }
187
188 ASN1_OBJECT obj;
189 OPENSSL_memset(&obj, 0, sizeof(obj));
190 obj.data = CBS_data(cbs);
191 obj.length = (int)CBS_len(cbs);
192
193 return OBJ_obj2nid(&obj);
194 }
195
196 // short_name_cmp is called to search the kNIDsInShortNameOrder array. The
197 // |key| argument is name that we're looking for and |element| is a pointer to
198 // an unsigned int in the array.
199 static int short_name_cmp(const void *key, const void *element) {
200 const char *name = (const char *)key;
201 uint16_t nid = *((const uint16_t *)element);
202
203 return strcmp(name, get_builtin_object(nid)->sn);
204 }
205
206 int OBJ_sn2nid(const char *short_name) {
207 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
208 if (global_added_by_short_name != NULL) {
209 ASN1_OBJECT *match, template;
210
211 template.sn = short_name;
212 match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template);
213 if (match != NULL) {
214 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
215 return match->nid;
216 }
217 }
218 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
219
220 const uint16_t *nid_ptr =
221 bsearch(short_name, kNIDsInShortNameOrder,
222 OPENSSL_ARRAY_SIZE(kNIDsInShortNameOrder),
223 sizeof(kNIDsInShortNameOrder[0]), short_name_cmp);
224 if (nid_ptr == NULL) {
225 return NID_undef;
226 }
227
228 return get_builtin_object(*nid_ptr)->nid;
229 }
230
231 // long_name_cmp is called to search the kNIDsInLongNameOrder array. The
232 // |key| argument is name that we're looking for and |element| is a pointer to
233 // an unsigned int in the array.
234 static int long_name_cmp(const void *key, const void *element) {
235 const char *name = (const char *)key;
236 uint16_t nid = *((const uint16_t *)element);
237
238 return strcmp(name, get_builtin_object(nid)->ln);
239 }
240
241 int OBJ_ln2nid(const char *long_name) {
242 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
243 if (global_added_by_long_name != NULL) {
244 ASN1_OBJECT *match, template;
245
246 template.ln = long_name;
247 match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template);
248 if (match != NULL) {
249 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
250 return match->nid;
251 }
252 }
253 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
254
255 const uint16_t *nid_ptr = bsearch(
256 long_name, kNIDsInLongNameOrder, OPENSSL_ARRAY_SIZE(kNIDsInLongNameOrder),
257 sizeof(kNIDsInLongNameOrder[0]), long_name_cmp);
258 if (nid_ptr == NULL) {
259 return NID_undef;
260 }
261
262 return get_builtin_object(*nid_ptr)->nid;
263 }
264
265 int OBJ_txt2nid(const char *s) {
266 ASN1_OBJECT *obj;
267 int nid;
268
269 obj = OBJ_txt2obj(s, 0 /* search names */);
270 nid = OBJ_obj2nid(obj);
271 ASN1_OBJECT_free(obj);
272 return nid;
273 }
274
275 OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) {
276 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
277 CBB oid;
278
279 if (obj == NULL ||
280 !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) ||
281 !CBB_add_bytes(&oid, obj->data, obj->length) ||
282 !CBB_flush(out)) {
283 return 0;
284 }
285
286 return 1;
287 }
288
289 const ASN1_OBJECT *OBJ_get_undef(void) {
290 static const ASN1_OBJECT kUndef = {
291 /*sn=*/SN_undef,
292 /*ln=*/LN_undef,
293 /*nid=*/NID_undef,
294 /*length=*/0,
295 /*data=*/NULL,
296 /*flags=*/0,
297 };
298 return &kUndef;
299 }
300
301 ASN1_OBJECT *OBJ_nid2obj(int nid) {
302 if (nid == NID_undef) {
303 return (ASN1_OBJECT *)OBJ_get_undef();
304 }
305
306 if (nid > 0 && nid < NUM_NID) {
307 const ASN1_OBJECT *obj = get_builtin_object(nid);
308 if (nid != NID_undef && obj->nid == NID_undef) {
309 goto err;
310 }
311 return (ASN1_OBJECT *)obj;
312 }
313
314 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
315 if (global_added_by_nid != NULL) {
316 ASN1_OBJECT *match, template;
317
318 template.nid = nid;
319 match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
320 if (match != NULL) {
321 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
322 return match;
323 }
324 }
325 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
326
327 err:
328 OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID);
329 return NULL;
330 }
331
332 const char *OBJ_nid2sn(int nid) {
333 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
334 if (obj == NULL) {
335 return NULL;
336 }
337
338 return obj->sn;
339 }
340
341 const char *OBJ_nid2ln(int nid) {
342 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
343 if (obj == NULL) {
344 return NULL;
345 }
346
347 return obj->ln;
348 }
349
350 static ASN1_OBJECT *create_object_with_text_oid(int (*get_nid)(void),
351 const char *oid,
352 const char *short_name,
353 const char *long_name) {
354 uint8_t *buf;
355 size_t len;
356 CBB cbb;
357 if (!CBB_init(&cbb, 32) ||
358 !CBB_add_asn1_oid_from_text(&cbb, oid, strlen(oid)) ||
359 !CBB_finish(&cbb, &buf, &len)) {
360 OPENSSL_PUT_ERROR(OBJ, OBJ_R_INVALID_OID_STRING);
361 CBB_cleanup(&cbb);
362 return NULL;
363 }
364
365 ASN1_OBJECT *ret = ASN1_OBJECT_create(get_nid ? get_nid() : NID_undef, buf,
366 len, short_name, long_name);
367 OPENSSL_free(buf);
368 return ret;
369 }
370
371 ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
372 if (!dont_search_names) {
373 int nid = OBJ_sn2nid(s);
374 if (nid == NID_undef) {
375 nid = OBJ_ln2nid(s);
376 }
377
378 if (nid != NID_undef) {
379 return OBJ_nid2obj(nid);
380 }
381 }
382
383 return create_object_with_text_oid(NULL, s, NULL, NULL);
384 }
385
386 static int strlcpy_int(char *dst, const char *src, int dst_size) {
387 size_t ret = OPENSSL_strlcpy(dst, src, dst_size < 0 ? 0 : (size_t)dst_size);
388 if (ret > INT_MAX) {
389 OPENSSL_PUT_ERROR(OBJ, ERR_R_OVERFLOW);
390 return -1;
391 }
392 return (int)ret;
393 }
394
395 int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj,
396 int always_return_oid) {
397 // Python depends on the empty OID successfully encoding as the empty
398 // string.
399 if (obj == NULL || obj->length == 0) {
400 return strlcpy_int(out, "", out_len);
401 }
402
403 if (!always_return_oid) {
404 int nid = OBJ_obj2nid(obj);
405 if (nid != NID_undef) {
406 const char *name = OBJ_nid2ln(nid);
407 if (name == NULL) {
408 name = OBJ_nid2sn(nid);
409 }
410 if (name != NULL) {
411 return strlcpy_int(out, name, out_len);
412 }
413 }
414 }
415
416 CBS cbs;
417 CBS_init(&cbs, obj->data, obj->length);
418 char *txt = CBS_asn1_oid_to_text(&cbs);
419 if (txt == NULL) {
420 if (out_len > 0) {
421 out[0] = '\0';
422 }
423 return -1;
424 }
425
426 int ret = strlcpy_int(out, txt, out_len);
427 OPENSSL_free(txt);
428 return ret;
429 }
430
431 static uint32_t hash_nid(const ASN1_OBJECT *obj) {
432 return obj->nid;
433 }
434
435 static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
436 return a->nid - b->nid;
437 }
438
439 static uint32_t hash_data(const ASN1_OBJECT *obj) {
440 return OPENSSL_hash32(obj->data, obj->length);
441 }
442
443 static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
444 return OPENSSL_strhash(obj->sn);
445 }
446
447 static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
448 return strcmp(a->sn, b->sn);
449 }
450
451 static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
452 return OPENSSL_strhash(obj->ln);
453 }
454
455 static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
456 return strcmp(a->ln, b->ln);
457 }
458
459 // obj_add_object inserts |obj| into the various global hashes for run-time
460 // added objects. It returns one on success or zero otherwise.
461 static int obj_add_object(ASN1_OBJECT *obj) {
462 obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
463 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
464
465 CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock);
466 if (global_added_by_nid == NULL) {
467 global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
468 }
469 if (global_added_by_data == NULL) {
470 global_added_by_data = lh_ASN1_OBJECT_new(hash_data, OBJ_cmp);
471 }
472 if (global_added_by_short_name == NULL) {
473 global_added_by_short_name =
474 lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
475 }
476 if (global_added_by_long_name == NULL) {
477 global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
478 }
479
480 int ok = 0;
481 if (global_added_by_nid == NULL ||
482 global_added_by_data == NULL ||
483 global_added_by_short_name == NULL ||
484 global_added_by_long_name == NULL) {
485 goto err;
486 }
487
488 // We don't pay attention to |old_object| (which contains any previous object
489 // that was evicted from the hashes) because we don't have a reference count
490 // on ASN1_OBJECT values. Also, we should never have duplicates nids and so
491 // should always have objects in |global_added_by_nid|.
492 ASN1_OBJECT *old_object;
493 ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
494 if (obj->length != 0 && obj->data != NULL) {
495 ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
496 }
497 if (obj->sn != NULL) {
498 ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
499 }
500 if (obj->ln != NULL) {
501 ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
502 }
503
504 err:
505 CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock);
506 return ok;
507 }
508
509 int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
510 ASN1_OBJECT *op =
511 create_object_with_text_oid(obj_next_nid, oid, short_name, long_name);
512 if (op == NULL ||
513 !obj_add_object(op)) {
514 return NID_undef;
515 }
516 return op->nid;
517 }
518
519 void OBJ_cleanup(void) {}