blob: c44ffc8817da08d4be4166a82e2f1ba1aed095e1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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
David Benjamin2f8ea542016-08-04 17:21:36 -040057#if !defined(__STDC_FORMAT_MACROS)
58#define __STDC_FORMAT_MACROS
59#endif
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <openssl/obj.h>
62
David Benjamin2f8ea542016-08-04 17:21:36 -040063#include <inttypes.h>
Adam Langley95c29f32014-06-20 12:00:00 -070064#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080065#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070066
67#include <openssl/asn1.h>
68#include <openssl/buf.h>
69#include <openssl/bytestring.h>
70#include <openssl/err.h>
71#include <openssl/lhash.h>
72#include <openssl/mem.h>
73#include <openssl/thread.h>
74
David Benjamin8a5825e2014-08-30 21:33:54 -040075#include "obj_dat.h"
Adam Langleyba3bef92015-04-13 11:04:20 -070076#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070077
Adam Langleyba3bef92015-04-13 11:04:20 -070078
79static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT;
80/* These globals are protected by |global_added_lock|. */
Adam Langley95c29f32014-06-20 12:00:00 -070081static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL;
82static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL;
83static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL;
84static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL;
85
Adam Langleyba3bef92015-04-13 11:04:20 -070086static struct CRYPTO_STATIC_MUTEX global_next_nid_lock =
87 CRYPTO_STATIC_MUTEX_INIT;
Adam Langley95c29f32014-06-20 12:00:00 -070088static unsigned global_next_nid = NUM_NID;
89
David Benjaminc44d2f42014-08-20 16:24:00 -040090static int obj_next_nid(void) {
Adam Langley95c29f32014-06-20 12:00:00 -070091 int ret;
92
Adam Langleyba3bef92015-04-13 11:04:20 -070093 CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock);
Adam Langley95c29f32014-06-20 12:00:00 -070094 ret = global_next_nid++;
David Benjamin29270de2016-05-24 15:28:36 +000095 CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock);
Adam Langley95c29f32014-06-20 12:00:00 -070096
97 return ret;
98}
99
100ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) {
101 ASN1_OBJECT *r;
102 unsigned char *data = NULL;
103 char *sn = NULL, *ln = NULL;
104
105 if (o == NULL) {
106 return NULL;
107 }
108
109 if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
110 /* TODO(fork): this is a little dangerous. */
111 return (ASN1_OBJECT *)o;
112 }
113
114 r = ASN1_OBJECT_new();
115 if (r == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400116 OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700117 return NULL;
118 }
119 r->ln = r->sn = NULL;
120
121 data = OPENSSL_malloc(o->length);
122 if (data == NULL) {
123 goto err;
124 }
125 if (o->data != NULL) {
126 memcpy(data, o->data, o->length);
127 }
128
129 /* once data is attached to an object, it remains const */
130 r->data = data;
131 r->length = o->length;
132 r->nid = o->nid;
133
134 if (o->ln != NULL) {
135 ln = OPENSSL_strdup(o->ln);
136 if (ln == NULL) {
137 goto err;
138 }
139 }
140
141 if (o->sn != NULL) {
142 sn = OPENSSL_strdup(o->sn);
Matt Braithwaite9626f262015-04-20 18:07:32 -0700143 if (sn == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700144 goto err;
145 }
146 }
147
148 r->sn = sn;
149 r->ln = ln;
150
151 r->flags =
152 o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
153 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
154 return r;
155
156err:
David Benjamin3570d732015-06-29 00:28:17 -0400157 OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
David Benjamind8b65c82015-04-22 16:09:09 -0400158 OPENSSL_free(ln);
159 OPENSSL_free(sn);
160 OPENSSL_free(data);
Adam Langley95c29f32014-06-20 12:00:00 -0700161 OPENSSL_free(r);
162 return NULL;
163}
164
165int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
166 int ret;
167
168 ret = a->length - b->length;
169 if (ret) {
170 return ret;
171 }
172 return memcmp(a->data, b->data, a->length);
173}
174
David Benjaminefd8eb32015-05-26 17:57:41 -0400175/* obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is
176 * an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
Adam Langley95c29f32014-06-20 12:00:00 -0700177 * unsigned int in the array. */
178static int obj_cmp(const void *key, const void *element) {
David Benjaminefd8eb32015-05-26 17:57:41 -0400179 unsigned nid = *((const unsigned*) element);
Adam Langley95c29f32014-06-20 12:00:00 -0700180 const ASN1_OBJECT *a = key;
181 const ASN1_OBJECT *b = &kObjects[nid];
182
David Benjaminefd8eb32015-05-26 17:57:41 -0400183 if (a->length < b->length) {
184 return -1;
185 } else if (a->length > b->length) {
186 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700187 }
188 return memcmp(a->data, b->data, a->length);
189}
190
191int OBJ_obj2nid(const ASN1_OBJECT *obj) {
192 const unsigned int *nid_ptr;
193
194 if (obj == NULL) {
195 return NID_undef;
196 }
197
198 if (obj->nid != 0) {
199 return obj->nid;
200 }
201
Adam Langleyba3bef92015-04-13 11:04:20 -0700202 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700203 if (global_added_by_data != NULL) {
204 ASN1_OBJECT *match;
205
206 match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj);
207 if (match != NULL) {
David Benjamin29270de2016-05-24 15:28:36 +0000208 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700209 return match->nid;
210 }
211 }
David Benjamin29270de2016-05-24 15:28:36 +0000212 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700213
214 nid_ptr = bsearch(obj, kNIDsInOIDOrder, NUM_OBJ, sizeof(unsigned), obj_cmp);
215 if (nid_ptr == NULL) {
216 return NID_undef;
217 }
218
219 return kObjects[*nid_ptr].nid;
220}
221
222int OBJ_cbs2nid(const CBS *cbs) {
David Benjamin22edd872016-08-04 21:38:40 +0000223 if (CBS_len(cbs) > INT_MAX) {
224 return NID_undef;
225 }
226
Adam Langley95c29f32014-06-20 12:00:00 -0700227 ASN1_OBJECT obj;
228 memset(&obj, 0, sizeof(obj));
229 obj.data = CBS_data(cbs);
David Benjamin22edd872016-08-04 21:38:40 +0000230 obj.length = (int)CBS_len(cbs);
Adam Langley95c29f32014-06-20 12:00:00 -0700231
232 return OBJ_obj2nid(&obj);
233}
234
235/* short_name_cmp is called to search the kNIDsInShortNameOrder array. The
236 * |key| argument is name that we're looking for and |element| is a pointer to
237 * an unsigned int in the array. */
238static int short_name_cmp(const void *key, const void *element) {
239 const char *name = (const char *) key;
240 unsigned nid = *((unsigned*) element);
241
242 return strcmp(name, kObjects[nid].sn);
243}
244
245int OBJ_sn2nid(const char *short_name) {
246 const unsigned int *nid_ptr;
247
Adam Langleyba3bef92015-04-13 11:04:20 -0700248 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700249 if (global_added_by_short_name != NULL) {
250 ASN1_OBJECT *match, template;
251
252 template.sn = short_name;
253 match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template);
254 if (match != NULL) {
David Benjamin29270de2016-05-24 15:28:36 +0000255 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700256 return match->nid;
257 }
258 }
David Benjamin29270de2016-05-24 15:28:36 +0000259 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700260
261 nid_ptr = bsearch(short_name, kNIDsInShortNameOrder, NUM_SN, sizeof(unsigned), short_name_cmp);
262 if (nid_ptr == NULL) {
263 return NID_undef;
264 }
265
266 return kObjects[*nid_ptr].nid;
267}
268
269/* long_name_cmp is called to search the kNIDsInLongNameOrder array. The
270 * |key| argument is name that we're looking for and |element| is a pointer to
271 * an unsigned int in the array. */
272static int long_name_cmp(const void *key, const void *element) {
273 const char *name = (const char *) key;
274 unsigned nid = *((unsigned*) element);
275
276 return strcmp(name, kObjects[nid].ln);
277}
278
279int OBJ_ln2nid(const char *long_name) {
280 const unsigned int *nid_ptr;
281
Adam Langleyba3bef92015-04-13 11:04:20 -0700282 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700283 if (global_added_by_long_name != NULL) {
284 ASN1_OBJECT *match, template;
285
286 template.ln = long_name;
287 match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template);
288 if (match != NULL) {
David Benjamin29270de2016-05-24 15:28:36 +0000289 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700290 return match->nid;
291 }
292 }
David Benjamin29270de2016-05-24 15:28:36 +0000293 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700294
295 nid_ptr = bsearch(long_name, kNIDsInLongNameOrder, NUM_LN, sizeof(unsigned), long_name_cmp);
296 if (nid_ptr == NULL) {
297 return NID_undef;
298 }
299
300 return kObjects[*nid_ptr].nid;
301}
302
303int OBJ_txt2nid(const char *s) {
304 ASN1_OBJECT *obj;
305 int nid;
306
307 obj = OBJ_txt2obj(s, 0 /* search names */);
308 nid = OBJ_obj2nid(obj);
309 ASN1_OBJECT_free(obj);
310 return nid;
311}
312
Adam Langleyeeb9f492014-08-06 16:29:56 -0700313OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) {
314 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
315 CBB oid;
316
317 if (obj == NULL ||
318 !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) ||
319 !CBB_add_bytes(&oid, obj->data, obj->length) ||
320 !CBB_flush(out)) {
321 return 0;
322 }
323
324 return 1;
325}
326
Adam Langley95c29f32014-06-20 12:00:00 -0700327const ASN1_OBJECT *OBJ_nid2obj(int nid) {
328 if (nid >= 0 && nid < NUM_NID) {
329 if (nid != NID_undef && kObjects[nid].nid == NID_undef) {
330 goto err;
331 }
332 return &kObjects[nid];
333 }
334
Adam Langleyba3bef92015-04-13 11:04:20 -0700335 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700336 if (global_added_by_nid != NULL) {
337 ASN1_OBJECT *match, template;
338
339 template.nid = nid;
340 match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
341 if (match != NULL) {
David Benjamin29270de2016-05-24 15:28:36 +0000342 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700343 return match;
344 }
345 }
David Benjamin29270de2016-05-24 15:28:36 +0000346 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700347
348err:
David Benjamin3570d732015-06-29 00:28:17 -0400349 OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID);
Adam Langley95c29f32014-06-20 12:00:00 -0700350 return NULL;
351}
352
353const char *OBJ_nid2sn(int nid) {
354 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
355 if (obj == NULL) {
356 return NULL;
357 }
358
359 return obj->sn;
360}
361
362const char *OBJ_nid2ln(int nid) {
363 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
364 if (obj == NULL) {
365 return NULL;
366 }
367
368 return obj->ln;
369}
370
371ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
372 int nid = NID_undef;
373 ASN1_OBJECT *op = NULL;
374 unsigned char *buf;
375 unsigned char *p;
376 const unsigned char *bufp;
377 int contents_len, total_len;
378
379 if (!dont_search_names) {
380 nid = OBJ_sn2nid(s);
381 if (nid == NID_undef) {
382 nid = OBJ_ln2nid(s);
383 }
384
385 if (nid != NID_undef) {
386 return (ASN1_OBJECT*) OBJ_nid2obj(nid);
387 }
388 }
389
390 /* Work out size of content octets */
391 contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1);
392 if (contents_len <= 0) {
393 return NULL;
394 }
395 /* Work out total size */
396 total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT);
397
398 buf = OPENSSL_malloc(total_len);
399 if (buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400400 OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700401 return NULL;
402 }
403
404 p = buf;
405 /* Write out tag+length */
406 ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
407 /* Write out contents */
408 a2d_ASN1_OBJECT(p, contents_len, s, -1);
409
410 bufp = buf;
411 op = d2i_ASN1_OBJECT(NULL, &bufp, total_len);
412 OPENSSL_free(buf);
413
414 return op;
415}
416
David Benjamin2f8ea542016-08-04 17:21:36 -0400417static int strlcpy_int(char *dst, const char *src, int dst_size) {
418 size_t ret = BUF_strlcpy(dst, src, dst_size < 0 ? 0 : (size_t)dst_size);
419 if (ret > INT_MAX) {
420 OPENSSL_PUT_ERROR(OBJ, ERR_R_OVERFLOW);
421 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700422 }
David Benjamin2f8ea542016-08-04 17:21:36 -0400423 return (int)ret;
424}
Adam Langley95c29f32014-06-20 12:00:00 -0700425
David Benjamin2f8ea542016-08-04 17:21:36 -0400426static int parse_oid_component(CBS *cbs, uint64_t *out) {
427 uint64_t v = 0;
428 uint8_t b;
429 do {
430 if (!CBS_get_u8(cbs, &b)) {
431 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700432 }
David Benjamin2f8ea542016-08-04 17:21:36 -0400433 if ((v >> (64 - 7)) != 0) {
434 /* The component is too large. */
435 return 0;
436 }
437 if (v == 0 && b == 0x80) {
438 /* The component must be minimally encoded. */
439 return 0;
440 }
441 v = (v << 7) | (b & 0x7f);
442
443 /* Components end at an octet with the high bit cleared. */
444 } while (b & 0x80);
445
446 *out = v;
447 return 1;
448}
449
450static int add_decimal(CBB *out, uint64_t v) {
451 char buf[DECIMAL_SIZE(uint64_t) + 1];
452 BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
453 return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
454}
455
456int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj,
Adam Langleyfbe3a7b2016-08-09 21:25:45 -0700457 int always_return_oid) {
Martin Kreichgauer909232d2016-08-09 13:53:36 -0700458 /* Python depends on the empty OID successfully encoding as the empty
459 * string. */
460 if (obj == NULL || obj->length == 0) {
461 return strlcpy_int(out, "", out_len);
462 }
463
Adam Langleyfbe3a7b2016-08-09 21:25:45 -0700464 if (!always_return_oid) {
David Benjamin2f8ea542016-08-04 17:21:36 -0400465 int nid = OBJ_obj2nid(obj);
466 if (nid != NID_undef) {
467 const char *name = OBJ_nid2ln(nid);
468 if (name == NULL) {
469 name = OBJ_nid2sn(nid);
Adam Langley95c29f32014-06-20 12:00:00 -0700470 }
David Benjamin2f8ea542016-08-04 17:21:36 -0400471 if (name != NULL) {
472 return strlcpy_int(out, name, out_len);
473 }
Adam Langley95c29f32014-06-20 12:00:00 -0700474 }
475 }
476
David Benjamin2f8ea542016-08-04 17:21:36 -0400477 CBB cbb;
478 if (!CBB_init(&cbb, 32)) {
479 goto err;
480 }
Adam Langley95c29f32014-06-20 12:00:00 -0700481
David Benjamin2f8ea542016-08-04 17:21:36 -0400482 CBS cbs;
483 CBS_init(&cbs, obj->data, obj->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin2f8ea542016-08-04 17:21:36 -0400485 /* The first component is 40 * value1 + value2, where value1 is 0, 1, or 2. */
486 uint64_t v;
487 if (!parse_oid_component(&cbs, &v)) {
488 goto err;
489 }
490
491 if (v >= 80) {
492 if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
493 !add_decimal(&cbb, v - 80)) {
494 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700495 }
David Benjamin2f8ea542016-08-04 17:21:36 -0400496 } else if (!add_decimal(&cbb, v / 40) ||
497 !CBB_add_u8(&cbb, '.') ||
498 !add_decimal(&cbb, v % 40)) {
499 goto err;
500 }
Adam Langley95c29f32014-06-20 12:00:00 -0700501
David Benjamin2f8ea542016-08-04 17:21:36 -0400502 while (CBS_len(&cbs) != 0) {
503 if (!parse_oid_component(&cbs, &v) ||
504 !CBB_add_u8(&cbb, '.') ||
505 !add_decimal(&cbb, v)) {
506 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700507 }
508 }
509
David Benjamin2f8ea542016-08-04 17:21:36 -0400510 uint8_t *txt;
511 size_t txt_len;
512 if (!CBB_add_u8(&cbb, '\0') ||
513 !CBB_finish(&cbb, &txt, &txt_len)) {
514 goto err;
515 }
516
517 int ret = strlcpy_int(out, (const char *)txt, out_len);
518 OPENSSL_free(txt);
519 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700520
521err:
David Benjamin2f8ea542016-08-04 17:21:36 -0400522 CBB_cleanup(&cbb);
523 if (out_len > 0) {
524 out[0] = '\0';
525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526 return -1;
527}
528
529static uint32_t hash_nid(const ASN1_OBJECT *obj) {
530 return obj->nid;
531}
532
533static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
534 return a->nid - b->nid;
535}
536
537static uint32_t hash_data(const ASN1_OBJECT *obj) {
538 return OPENSSL_hash32(obj->data, obj->length);
539}
540
541static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
542 int i = a->length - b->length;
543 if (i) {
544 return i;
545 }
546 return memcmp(a->data, b->data, a->length);
547}
548
549static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
550 return lh_strhash(obj->sn);
551}
552
553static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
554 return strcmp(a->sn, b->sn);
555}
556
557static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
558 return lh_strhash(obj->ln);
559}
560
561static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
562 return strcmp(a->ln, b->ln);
563}
564
565/* obj_add_object inserts |obj| into the various global hashes for run-time
566 * added objects. It returns one on success or zero otherwise. */
567static int obj_add_object(ASN1_OBJECT *obj) {
568 int ok;
569 ASN1_OBJECT *old_object;
570
571 obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
572 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
573
Adam Langleyba3bef92015-04-13 11:04:20 -0700574 CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700575 if (global_added_by_nid == NULL) {
576 global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
577 global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
578 global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
579 global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
580 }
581
582 /* We don't pay attention to |old_object| (which contains any previous object
583 * that was evicted from the hashes) because we don't have a reference count
584 * on ASN1_OBJECT values. Also, we should never have duplicates nids and so
585 * should always have objects in |global_added_by_nid|. */
586
587 ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
588 if (obj->length != 0 && obj->data != NULL) {
589 ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
590 }
591 if (obj->sn != NULL) {
592 ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
593 }
594 if (obj->ln != NULL) {
595 ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
596 }
David Benjamin29270de2016-05-24 15:28:36 +0000597 CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700598
599 return ok;
600}
601
602int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
603 int ret = NID_undef;
604 ASN1_OBJECT *op = NULL;
605 unsigned char *buf = NULL;
606 int len;
607
608 len = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
609 if (len <= 0) {
610 goto err;
611 }
612
613 buf = OPENSSL_malloc(len);
614 if (buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400615 OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700616 goto err;
617 }
618
619 len = a2d_ASN1_OBJECT(buf, len, oid, -1);
620 if (len == 0) {
621 goto err;
622 }
623
624 op = (ASN1_OBJECT *)ASN1_OBJECT_create(obj_next_nid(), buf, len, short_name,
625 long_name);
626 if (op == NULL) {
627 goto err;
628 }
629
630 if (obj_add_object(op)) {
631 ret = op->nid;
632 }
633 op = NULL;
634
635err:
David Benjamind8b65c82015-04-22 16:09:09 -0400636 ASN1_OBJECT_free(op);
637 OPENSSL_free(buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700638
639 return ret;
640}