1 | /* Copyright (C) 1995-1998 Eric Young ([email protected]) |
2 | * All rights reserved. |
3 | * |
4 | * This package is an SSL implementation written |
5 | * by Eric Young ([email protected]). |
6 | * The implementation was written so as to conform with Netscapes SSL. |
7 | * |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions |
10 | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | * included with this distribution is covered by the same copyright terms |
13 | * except that the holder is Tim Hudson ([email protected]). |
14 | * |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. |
17 | * If this package is used in a product, Eric Young should be given attribution |
18 | * as the author of the parts of the library used. |
19 | * This can be in the form of a textual message at program startup or |
20 | * in documentation (online or textual) provided with the package. |
21 | * |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions |
24 | * are met: |
25 | * 1. Redistributions of source code must retain the copyright |
26 | * notice, this list of conditions and the following disclaimer. |
27 | * 2. Redistributions in binary form must reproduce the above copyright |
28 | * notice, this list of conditions and the following disclaimer in the |
29 | * documentation and/or other materials provided with the distribution. |
30 | * 3. All advertising materials mentioning features or use of this software |
31 | * must display the following acknowledgement: |
32 | * "This product includes cryptographic software written by |
33 | * Eric Young ([email protected])" |
34 | * The word 'cryptographic' can be left out if the rouines from the library |
35 | * being used are not cryptographic related :-). |
36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson ([email protected])" |
39 | * |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | * SUCH DAMAGE. |
51 | * |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | * copied and put under another distribution licence |
55 | * [including the GNU Public Licence.] */ |
56 | |
57 | #include <openssl/obj.h> |
58 | |
59 | #include <inttypes.h> |
60 | #include <limits.h> |
61 | #include <string.h> |
62 | |
63 | #include <openssl/asn1.h> |
64 | #include <openssl/bytestring.h> |
65 | #include <openssl/err.h> |
66 | #include <openssl/lhash.h> |
67 | #include <openssl/mem.h> |
68 | #include <openssl/thread.h> |
69 | |
70 | #include "../asn1/internal.h" |
71 | #include "../internal.h" |
72 | #include "../lhash/internal.h" |
73 | |
74 | // obj_data.h must be included after the definition of |ASN1_OBJECT|. |
75 | #include "obj_dat.h" |
76 | |
77 | |
78 | DEFINE_LHASH_OF(ASN1_OBJECT) |
79 | |
80 | static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT; |
81 | // These globals are protected by |global_added_lock|. |
82 | static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL; |
83 | static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL; |
84 | static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL; |
85 | static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL; |
86 | |
87 | static struct CRYPTO_STATIC_MUTEX global_next_nid_lock = |
88 | CRYPTO_STATIC_MUTEX_INIT; |
89 | static unsigned global_next_nid = NUM_NID; |
90 | |
91 | static int obj_next_nid(void) { |
92 | int ret; |
93 | |
94 | CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock); |
95 | ret = global_next_nid++; |
96 | CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock); |
97 | |
98 | return ret; |
99 | } |
100 | |
101 | ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) { |
102 | ASN1_OBJECT *r; |
103 | unsigned char *data = NULL; |
104 | char *sn = NULL, *ln = NULL; |
105 | |
106 | if (o == NULL) { |
107 | return NULL; |
108 | } |
109 | |
110 | if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { |
111 | // TODO(fork): this is a little dangerous. |
112 | return (ASN1_OBJECT *)o; |
113 | } |
114 | |
115 | r = ASN1_OBJECT_new(); |
116 | if (r == NULL) { |
117 | OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB); |
118 | return NULL; |
119 | } |
120 | r->ln = r->sn = NULL; |
121 | |
122 | data = OPENSSL_malloc(o->length); |
123 | if (data == NULL) { |
124 | goto err; |
125 | } |
126 | if (o->data != NULL) { |
127 | OPENSSL_memcpy(data, o->data, o->length); |
128 | } |
129 | |
130 | // once data is attached to an object, it remains const |
131 | r->data = data; |
132 | r->length = o->length; |
133 | r->nid = o->nid; |
134 | |
135 | if (o->ln != NULL) { |
136 | ln = OPENSSL_strdup(o->ln); |
137 | if (ln == NULL) { |
138 | goto err; |
139 | } |
140 | } |
141 | |
142 | if (o->sn != NULL) { |
143 | sn = OPENSSL_strdup(o->sn); |
144 | if (sn == NULL) { |
145 | goto err; |
146 | } |
147 | } |
148 | |
149 | r->sn = sn; |
150 | r->ln = ln; |
151 | |
152 | r->flags = |
153 | o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
154 | ASN1_OBJECT_FLAG_DYNAMIC_DATA); |
155 | return r; |
156 | |
157 | err: |
158 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
159 | OPENSSL_free(ln); |
160 | OPENSSL_free(sn); |
161 | OPENSSL_free(data); |
162 | OPENSSL_free(r); |
163 | return NULL; |
164 | } |
165 | |
166 | int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
167 | int ret; |
168 | |
169 | ret = a->length - b->length; |
170 | if (ret) { |
171 | return ret; |
172 | } |
173 | return OPENSSL_memcmp(a->data, b->data, a->length); |
174 | } |
175 | |
176 | const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj) { |
177 | if (obj == NULL) { |
178 | return NULL; |
179 | } |
180 | |
181 | return obj->data; |
182 | } |
183 | |
184 | size_t OBJ_length(const ASN1_OBJECT *obj) { |
185 | if (obj == NULL || obj->length < 0) { |
186 | return 0; |
187 | } |
188 | |
189 | return (size_t)obj->length; |
190 | } |
191 | |
192 | // obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is |
193 | // an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an |
194 | // unsigned int in the array. |
195 | static int obj_cmp(const void *key, const void *element) { |
196 | uint16_t nid = *((const uint16_t *)element); |
197 | const ASN1_OBJECT *a = key; |
198 | const ASN1_OBJECT *b = &kObjects[nid]; |
199 | |
200 | if (a->length < b->length) { |
201 | return -1; |
202 | } else if (a->length > b->length) { |
203 | return 1; |
204 | } |
205 | return OPENSSL_memcmp(a->data, b->data, a->length); |
206 | } |
207 | |
208 | int OBJ_obj2nid(const ASN1_OBJECT *obj) { |
209 | if (obj == NULL) { |
210 | return NID_undef; |
211 | } |
212 | |
213 | if (obj->nid != 0) { |
214 | return obj->nid; |
215 | } |
216 | |
217 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
218 | if (global_added_by_data != NULL) { |
219 | ASN1_OBJECT *match; |
220 | |
221 | match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj); |
222 | if (match != NULL) { |
223 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
224 | return match->nid; |
225 | } |
226 | } |
227 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
228 | |
229 | const uint16_t *nid_ptr = |
230 | bsearch(obj, kNIDsInOIDOrder, OPENSSL_ARRAY_SIZE(kNIDsInOIDOrder), |
231 | sizeof(kNIDsInOIDOrder[0]), obj_cmp); |
232 | if (nid_ptr == NULL) { |
233 | return NID_undef; |
234 | } |
235 | |
236 | return kObjects[*nid_ptr].nid; |
237 | } |
238 | |
239 | int OBJ_cbs2nid(const CBS *cbs) { |
240 | if (CBS_len(cbs) > INT_MAX) { |
241 | return NID_undef; |
242 | } |
243 | |
244 | ASN1_OBJECT obj; |
245 | OPENSSL_memset(&obj, 0, sizeof(obj)); |
246 | obj.data = CBS_data(cbs); |
247 | obj.length = (int)CBS_len(cbs); |
248 | |
249 | return OBJ_obj2nid(&obj); |
250 | } |
251 | |
252 | // short_name_cmp is called to search the kNIDsInShortNameOrder array. The |
253 | // |key| argument is name that we're looking for and |element| is a pointer to |
254 | // an unsigned int in the array. |
255 | static int short_name_cmp(const void *key, const void *element) { |
256 | const char *name = (const char *)key; |
257 | uint16_t nid = *((const uint16_t *)element); |
258 | |
259 | return strcmp(name, kObjects[nid].sn); |
260 | } |
261 | |
262 | int OBJ_sn2nid(const char *short_name) { |
263 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
264 | if (global_added_by_short_name != NULL) { |
265 | ASN1_OBJECT *match, template; |
266 | |
267 | template.sn = short_name; |
268 | match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template); |
269 | if (match != NULL) { |
270 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
271 | return match->nid; |
272 | } |
273 | } |
274 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
275 | |
276 | const uint16_t *nid_ptr = |
277 | bsearch(short_name, kNIDsInShortNameOrder, |
278 | OPENSSL_ARRAY_SIZE(kNIDsInShortNameOrder), |
279 | sizeof(kNIDsInShortNameOrder[0]), short_name_cmp); |
280 | if (nid_ptr == NULL) { |
281 | return NID_undef; |
282 | } |
283 | |
284 | return kObjects[*nid_ptr].nid; |
285 | } |
286 | |
287 | // long_name_cmp is called to search the kNIDsInLongNameOrder array. The |
288 | // |key| argument is name that we're looking for and |element| is a pointer to |
289 | // an unsigned int in the array. |
290 | static int long_name_cmp(const void *key, const void *element) { |
291 | const char *name = (const char *)key; |
292 | uint16_t nid = *((const uint16_t *)element); |
293 | |
294 | return strcmp(name, kObjects[nid].ln); |
295 | } |
296 | |
297 | int OBJ_ln2nid(const char *long_name) { |
298 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
299 | if (global_added_by_long_name != NULL) { |
300 | ASN1_OBJECT *match, template; |
301 | |
302 | template.ln = long_name; |
303 | match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template); |
304 | if (match != NULL) { |
305 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
306 | return match->nid; |
307 | } |
308 | } |
309 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
310 | |
311 | const uint16_t *nid_ptr = bsearch( |
312 | long_name, kNIDsInLongNameOrder, OPENSSL_ARRAY_SIZE(kNIDsInLongNameOrder), |
313 | sizeof(kNIDsInLongNameOrder[0]), long_name_cmp); |
314 | if (nid_ptr == NULL) { |
315 | return NID_undef; |
316 | } |
317 | |
318 | return kObjects[*nid_ptr].nid; |
319 | } |
320 | |
321 | int OBJ_txt2nid(const char *s) { |
322 | ASN1_OBJECT *obj; |
323 | int nid; |
324 | |
325 | obj = OBJ_txt2obj(s, 0 /* search names */); |
326 | nid = OBJ_obj2nid(obj); |
327 | ASN1_OBJECT_free(obj); |
328 | return nid; |
329 | } |
330 | |
331 | OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) { |
332 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
333 | CBB oid; |
334 | |
335 | if (obj == NULL || |
336 | !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) || |
337 | !CBB_add_bytes(&oid, obj->data, obj->length) || |
338 | !CBB_flush(out)) { |
339 | return 0; |
340 | } |
341 | |
342 | return 1; |
343 | } |
344 | |
345 | ASN1_OBJECT *OBJ_nid2obj(int nid) { |
346 | if (nid >= 0 && nid < NUM_NID) { |
347 | if (nid != NID_undef && kObjects[nid].nid == NID_undef) { |
348 | goto err; |
349 | } |
350 | return (ASN1_OBJECT *)&kObjects[nid]; |
351 | } |
352 | |
353 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
354 | if (global_added_by_nid != NULL) { |
355 | ASN1_OBJECT *match, template; |
356 | |
357 | template.nid = nid; |
358 | match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template); |
359 | if (match != NULL) { |
360 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
361 | return match; |
362 | } |
363 | } |
364 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
365 | |
366 | err: |
367 | OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID); |
368 | return NULL; |
369 | } |
370 | |
371 | const char *OBJ_nid2sn(int nid) { |
372 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
373 | if (obj == NULL) { |
374 | return NULL; |
375 | } |
376 | |
377 | return obj->sn; |
378 | } |
379 | |
380 | const char *OBJ_nid2ln(int nid) { |
381 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
382 | if (obj == NULL) { |
383 | return NULL; |
384 | } |
385 | |
386 | return obj->ln; |
387 | } |
388 | |
389 | static ASN1_OBJECT *create_object_with_text_oid(int (*get_nid)(void), |
390 | const char *oid, |
391 | const char *short_name, |
392 | const char *long_name) { |
393 | uint8_t *buf; |
394 | size_t len; |
395 | CBB cbb; |
396 | if (!CBB_init(&cbb, 32) || |
397 | !CBB_add_asn1_oid_from_text(&cbb, oid, strlen(oid)) || |
398 | !CBB_finish(&cbb, &buf, &len)) { |
399 | OPENSSL_PUT_ERROR(OBJ, OBJ_R_INVALID_OID_STRING); |
400 | CBB_cleanup(&cbb); |
401 | return NULL; |
402 | } |
403 | |
404 | ASN1_OBJECT *ret = ASN1_OBJECT_create(get_nid ? get_nid() : NID_undef, buf, |
405 | len, short_name, long_name); |
406 | OPENSSL_free(buf); |
407 | return ret; |
408 | } |
409 | |
410 | ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) { |
411 | if (!dont_search_names) { |
412 | int nid = OBJ_sn2nid(s); |
413 | if (nid == NID_undef) { |
414 | nid = OBJ_ln2nid(s); |
415 | } |
416 | |
417 | if (nid != NID_undef) { |
418 | return OBJ_nid2obj(nid); |
419 | } |
420 | } |
421 | |
422 | return create_object_with_text_oid(NULL, s, NULL, NULL); |
423 | } |
424 | |
425 | static int strlcpy_int(char *dst, const char *src, int dst_size) { |
426 | size_t ret = OPENSSL_strlcpy(dst, src, dst_size < 0 ? 0 : (size_t)dst_size); |
427 | if (ret > INT_MAX) { |
428 | OPENSSL_PUT_ERROR(OBJ, ERR_R_OVERFLOW); |
429 | return -1; |
430 | } |
431 | return (int)ret; |
432 | } |
433 | |
434 | int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, |
435 | int always_return_oid) { |
436 | // Python depends on the empty OID successfully encoding as the empty |
437 | // string. |
438 | if (obj == NULL || obj->length == 0) { |
439 | return strlcpy_int(out, "" , out_len); |
440 | } |
441 | |
442 | if (!always_return_oid) { |
443 | int nid = OBJ_obj2nid(obj); |
444 | if (nid != NID_undef) { |
445 | const char *name = OBJ_nid2ln(nid); |
446 | if (name == NULL) { |
447 | name = OBJ_nid2sn(nid); |
448 | } |
449 | if (name != NULL) { |
450 | return strlcpy_int(out, name, out_len); |
451 | } |
452 | } |
453 | } |
454 | |
455 | CBS cbs; |
456 | CBS_init(&cbs, obj->data, obj->length); |
457 | char *txt = CBS_asn1_oid_to_text(&cbs); |
458 | if (txt == NULL) { |
459 | if (out_len > 0) { |
460 | out[0] = '\0'; |
461 | } |
462 | return -1; |
463 | } |
464 | |
465 | int ret = strlcpy_int(out, txt, out_len); |
466 | OPENSSL_free(txt); |
467 | return ret; |
468 | } |
469 | |
470 | static uint32_t hash_nid(const ASN1_OBJECT *obj) { |
471 | return obj->nid; |
472 | } |
473 | |
474 | static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
475 | return a->nid - b->nid; |
476 | } |
477 | |
478 | static uint32_t hash_data(const ASN1_OBJECT *obj) { |
479 | return OPENSSL_hash32(obj->data, obj->length); |
480 | } |
481 | |
482 | static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
483 | int i = a->length - b->length; |
484 | if (i) { |
485 | return i; |
486 | } |
487 | return OPENSSL_memcmp(a->data, b->data, a->length); |
488 | } |
489 | |
490 | static uint32_t hash_short_name(const ASN1_OBJECT *obj) { |
491 | return OPENSSL_strhash(obj->sn); |
492 | } |
493 | |
494 | static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
495 | return strcmp(a->sn, b->sn); |
496 | } |
497 | |
498 | static uint32_t hash_long_name(const ASN1_OBJECT *obj) { |
499 | return OPENSSL_strhash(obj->ln); |
500 | } |
501 | |
502 | static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
503 | return strcmp(a->ln, b->ln); |
504 | } |
505 | |
506 | // obj_add_object inserts |obj| into the various global hashes for run-time |
507 | // added objects. It returns one on success or zero otherwise. |
508 | static int obj_add_object(ASN1_OBJECT *obj) { |
509 | int ok; |
510 | ASN1_OBJECT *old_object; |
511 | |
512 | obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
513 | ASN1_OBJECT_FLAG_DYNAMIC_DATA); |
514 | |
515 | CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock); |
516 | if (global_added_by_nid == NULL) { |
517 | global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid); |
518 | global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data); |
519 | global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name); |
520 | global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name); |
521 | } |
522 | |
523 | // We don't pay attention to |old_object| (which contains any previous object |
524 | // that was evicted from the hashes) because we don't have a reference count |
525 | // on ASN1_OBJECT values. Also, we should never have duplicates nids and so |
526 | // should always have objects in |global_added_by_nid|. |
527 | |
528 | ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj); |
529 | if (obj->length != 0 && obj->data != NULL) { |
530 | ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj); |
531 | } |
532 | if (obj->sn != NULL) { |
533 | ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj); |
534 | } |
535 | if (obj->ln != NULL) { |
536 | ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj); |
537 | } |
538 | CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock); |
539 | |
540 | return ok; |
541 | } |
542 | |
543 | int OBJ_create(const char *oid, const char *short_name, const char *long_name) { |
544 | ASN1_OBJECT *op = |
545 | create_object_with_text_oid(obj_next_nid, oid, short_name, long_name); |
546 | if (op == NULL || |
547 | !obj_add_object(op)) { |
548 | return NID_undef; |
549 | } |
550 | return op->nid; |
551 | } |
552 | |
553 | void OBJ_cleanup(void) {} |
554 | |