blob: 52e265b0a40fbe8b33316bd4174bd8ef6c97e2d3 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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 Benjaminc895d6b2016-08-11 13:26:41 -040057#if !defined(__STDC_FORMAT_MACROS)
58#define __STDC_FORMAT_MACROS
59#endif
60
Adam Langleyd9e397b2015-01-22 14:27:53 -080061#include <openssl/obj.h>
62
David Benjaminc895d6b2016-08-11 13:26:41 -040063#include <inttypes.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080064#include <limits.h>
65#include <string.h>
66
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
75#include "obj_dat.h"
Adam Langleye9ada862015-05-11 17:20:37 -070076#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080077
Adam Langleye9ada862015-05-11 17:20:37 -070078
79static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT;
Robert Sloan8f860b12017-08-28 07:37:06 -070080// These globals are protected by |global_added_lock|.
Adam Langleyd9e397b2015-01-22 14:27:53 -080081static 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 Langleye9ada862015-05-11 17:20:37 -070086static struct CRYPTO_STATIC_MUTEX global_next_nid_lock =
87 CRYPTO_STATIC_MUTEX_INIT;
Adam Langleyd9e397b2015-01-22 14:27:53 -080088static unsigned global_next_nid = NUM_NID;
89
90static int obj_next_nid(void) {
91 int ret;
92
Adam Langleye9ada862015-05-11 17:20:37 -070093 CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -080094 ret = global_next_nid++;
David Benjamind316cba2016-06-02 16:17:39 -040095 CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -080096
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)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700110 // TODO(fork): this is a little dangerous.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111 return (ASN1_OBJECT *)o;
112 }
113
114 r = ASN1_OBJECT_new();
115 if (r == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000116 OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800117 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) {
Robert Sloan69939df2017-01-09 10:53:07 -0800126 OPENSSL_memcpy(data, o->data, o->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800127 }
128
Robert Sloan8f860b12017-08-28 07:37:06 -0700129 // once data is attached to an object, it remains const
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130 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);
Adam Langleye9ada862015-05-11 17:20:37 -0700143 if (sn == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144 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:
Kenny Rootb8494592015-09-25 02:29:14 +0000157 OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
Adam Langleye9ada862015-05-11 17:20:37 -0700158 OPENSSL_free(ln);
159 OPENSSL_free(sn);
160 OPENSSL_free(data);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161 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 }
Robert Sloan69939df2017-01-09 10:53:07 -0800172 return OPENSSL_memcmp(a->data, b->data, a->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800173}
174
Robert Sloan4562e9d2017-10-02 10:26:51 -0700175const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj) {
176 if (obj == NULL) {
177 return NULL;
178 }
179
180 return obj->data;
181}
182
183size_t OBJ_length(const ASN1_OBJECT *obj) {
184 if (obj == NULL || obj->length < 0) {
185 return 0;
186 }
187
188 return (size_t)obj->length;
189}
190
Robert Sloan8f860b12017-08-28 07:37:06 -0700191// obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is
192// an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
193// unsigned int in the array.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194static int obj_cmp(const void *key, const void *element) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700195 unsigned nid = *((const unsigned*) element);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800196 const ASN1_OBJECT *a = key;
197 const ASN1_OBJECT *b = &kObjects[nid];
198
Adam Langleyf4e42722015-06-04 17:45:09 -0700199 if (a->length < b->length) {
200 return -1;
201 } else if (a->length > b->length) {
202 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203 }
Robert Sloan69939df2017-01-09 10:53:07 -0800204 return OPENSSL_memcmp(a->data, b->data, a->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205}
206
207int OBJ_obj2nid(const ASN1_OBJECT *obj) {
208 const unsigned int *nid_ptr;
209
210 if (obj == NULL) {
211 return NID_undef;
212 }
213
214 if (obj->nid != 0) {
215 return obj->nid;
216 }
217
Adam Langleye9ada862015-05-11 17:20:37 -0700218 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219 if (global_added_by_data != NULL) {
220 ASN1_OBJECT *match;
221
222 match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj);
223 if (match != NULL) {
David Benjamind316cba2016-06-02 16:17:39 -0400224 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225 return match->nid;
226 }
227 }
David Benjamind316cba2016-06-02 16:17:39 -0400228 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229
Robert Sloan69939df2017-01-09 10:53:07 -0800230 nid_ptr = bsearch(obj, kNIDsInOIDOrder, OPENSSL_ARRAY_SIZE(kNIDsInOIDOrder),
231 sizeof(kNIDsInOIDOrder[0]), obj_cmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800232 if (nid_ptr == NULL) {
233 return NID_undef;
234 }
235
236 return kObjects[*nid_ptr].nid;
237}
238
239int OBJ_cbs2nid(const CBS *cbs) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400240 if (CBS_len(cbs) > INT_MAX) {
241 return NID_undef;
242 }
243
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244 ASN1_OBJECT obj;
Robert Sloan69939df2017-01-09 10:53:07 -0800245 OPENSSL_memset(&obj, 0, sizeof(obj));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 obj.data = CBS_data(cbs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400247 obj.length = (int)CBS_len(cbs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248
249 return OBJ_obj2nid(&obj);
250}
251
Robert Sloan8f860b12017-08-28 07:37:06 -0700252// 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.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255static int short_name_cmp(const void *key, const void *element) {
256 const char *name = (const char *) key;
257 unsigned nid = *((unsigned*) element);
258
259 return strcmp(name, kObjects[nid].sn);
260}
261
262int OBJ_sn2nid(const char *short_name) {
263 const unsigned int *nid_ptr;
264
Adam Langleye9ada862015-05-11 17:20:37 -0700265 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 if (global_added_by_short_name != NULL) {
267 ASN1_OBJECT *match, template;
268
269 template.sn = short_name;
270 match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template);
271 if (match != NULL) {
David Benjamind316cba2016-06-02 16:17:39 -0400272 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 return match->nid;
274 }
275 }
David Benjamind316cba2016-06-02 16:17:39 -0400276 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277
Robert Sloan69939df2017-01-09 10:53:07 -0800278 nid_ptr = bsearch(short_name, kNIDsInShortNameOrder,
279 OPENSSL_ARRAY_SIZE(kNIDsInShortNameOrder),
280 sizeof(kNIDsInShortNameOrder[0]), short_name_cmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800281 if (nid_ptr == NULL) {
282 return NID_undef;
283 }
284
285 return kObjects[*nid_ptr].nid;
286}
287
Robert Sloan8f860b12017-08-28 07:37:06 -0700288// long_name_cmp is called to search the kNIDsInLongNameOrder array. The
289// |key| argument is name that we're looking for and |element| is a pointer to
290// an unsigned int in the array.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291static int long_name_cmp(const void *key, const void *element) {
292 const char *name = (const char *) key;
293 unsigned nid = *((unsigned*) element);
294
295 return strcmp(name, kObjects[nid].ln);
296}
297
298int OBJ_ln2nid(const char *long_name) {
299 const unsigned int *nid_ptr;
300
Adam Langleye9ada862015-05-11 17:20:37 -0700301 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800302 if (global_added_by_long_name != NULL) {
303 ASN1_OBJECT *match, template;
304
305 template.ln = long_name;
306 match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template);
307 if (match != NULL) {
David Benjamind316cba2016-06-02 16:17:39 -0400308 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800309 return match->nid;
310 }
311 }
David Benjamind316cba2016-06-02 16:17:39 -0400312 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800313
Robert Sloan69939df2017-01-09 10:53:07 -0800314 nid_ptr = bsearch(long_name, kNIDsInLongNameOrder,
315 OPENSSL_ARRAY_SIZE(kNIDsInLongNameOrder),
316 sizeof(kNIDsInLongNameOrder[0]), long_name_cmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317 if (nid_ptr == NULL) {
318 return NID_undef;
319 }
320
321 return kObjects[*nid_ptr].nid;
322}
323
324int OBJ_txt2nid(const char *s) {
325 ASN1_OBJECT *obj;
326 int nid;
327
328 obj = OBJ_txt2obj(s, 0 /* search names */);
329 nid = OBJ_obj2nid(obj);
330 ASN1_OBJECT_free(obj);
331 return nid;
332}
333
334OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) {
335 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
336 CBB oid;
337
338 if (obj == NULL ||
339 !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) ||
340 !CBB_add_bytes(&oid, obj->data, obj->length) ||
341 !CBB_flush(out)) {
342 return 0;
343 }
344
345 return 1;
346}
347
348const ASN1_OBJECT *OBJ_nid2obj(int nid) {
349 if (nid >= 0 && nid < NUM_NID) {
350 if (nid != NID_undef && kObjects[nid].nid == NID_undef) {
351 goto err;
352 }
353 return &kObjects[nid];
354 }
355
Adam Langleye9ada862015-05-11 17:20:37 -0700356 CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800357 if (global_added_by_nid != NULL) {
358 ASN1_OBJECT *match, template;
359
360 template.nid = nid;
361 match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
362 if (match != NULL) {
David Benjamind316cba2016-06-02 16:17:39 -0400363 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364 return match;
365 }
366 }
David Benjamind316cba2016-06-02 16:17:39 -0400367 CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800368
369err:
Kenny Rootb8494592015-09-25 02:29:14 +0000370 OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800371 return NULL;
372}
373
374const char *OBJ_nid2sn(int nid) {
375 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
376 if (obj == NULL) {
377 return NULL;
378 }
379
380 return obj->sn;
381}
382
383const char *OBJ_nid2ln(int nid) {
384 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
385 if (obj == NULL) {
386 return NULL;
387 }
388
389 return obj->ln;
390}
391
392ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
393 int nid = NID_undef;
394 ASN1_OBJECT *op = NULL;
395 unsigned char *buf;
396 unsigned char *p;
397 const unsigned char *bufp;
398 int contents_len, total_len;
399
400 if (!dont_search_names) {
401 nid = OBJ_sn2nid(s);
402 if (nid == NID_undef) {
403 nid = OBJ_ln2nid(s);
404 }
405
406 if (nid != NID_undef) {
407 return (ASN1_OBJECT*) OBJ_nid2obj(nid);
408 }
409 }
410
Robert Sloan8f860b12017-08-28 07:37:06 -0700411 // Work out size of content octets
Adam Langleyd9e397b2015-01-22 14:27:53 -0800412 contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1);
413 if (contents_len <= 0) {
414 return NULL;
415 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700416 // Work out total size
Adam Langleyd9e397b2015-01-22 14:27:53 -0800417 total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT);
418
419 buf = OPENSSL_malloc(total_len);
420 if (buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000421 OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800422 return NULL;
423 }
424
425 p = buf;
Robert Sloan8f860b12017-08-28 07:37:06 -0700426 // Write out tag+length
Adam Langleyd9e397b2015-01-22 14:27:53 -0800427 ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
Robert Sloan8f860b12017-08-28 07:37:06 -0700428 // Write out contents
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 a2d_ASN1_OBJECT(p, contents_len, s, -1);
430
431 bufp = buf;
432 op = d2i_ASN1_OBJECT(NULL, &bufp, total_len);
433 OPENSSL_free(buf);
434
435 return op;
436}
437
David Benjaminc895d6b2016-08-11 13:26:41 -0400438static int strlcpy_int(char *dst, const char *src, int dst_size) {
439 size_t ret = BUF_strlcpy(dst, src, dst_size < 0 ? 0 : (size_t)dst_size);
440 if (ret > INT_MAX) {
441 OPENSSL_PUT_ERROR(OBJ, ERR_R_OVERFLOW);
442 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400444 return (int)ret;
445}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800446
David Benjaminc895d6b2016-08-11 13:26:41 -0400447static int parse_oid_component(CBS *cbs, uint64_t *out) {
448 uint64_t v = 0;
449 uint8_t b;
450 do {
451 if (!CBS_get_u8(cbs, &b)) {
452 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800453 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400454 if ((v >> (64 - 7)) != 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700455 // The component is too large.
David Benjaminc895d6b2016-08-11 13:26:41 -0400456 return 0;
457 }
458 if (v == 0 && b == 0x80) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700459 // The component must be minimally encoded.
David Benjaminc895d6b2016-08-11 13:26:41 -0400460 return 0;
461 }
462 v = (v << 7) | (b & 0x7f);
463
Robert Sloan8f860b12017-08-28 07:37:06 -0700464 // Components end at an octet with the high bit cleared.
David Benjaminc895d6b2016-08-11 13:26:41 -0400465 } while (b & 0x80);
466
467 *out = v;
468 return 1;
469}
470
471static int add_decimal(CBB *out, uint64_t v) {
472 char buf[DECIMAL_SIZE(uint64_t) + 1];
473 BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
474 return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
475}
476
477int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj,
478 int always_return_oid) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700479 // Python depends on the empty OID successfully encoding as the empty
480 // string.
David Benjaminc895d6b2016-08-11 13:26:41 -0400481 if (obj == NULL || obj->length == 0) {
482 return strlcpy_int(out, "", out_len);
483 }
484
485 if (!always_return_oid) {
486 int nid = OBJ_obj2nid(obj);
487 if (nid != NID_undef) {
488 const char *name = OBJ_nid2ln(nid);
489 if (name == NULL) {
490 name = OBJ_nid2sn(nid);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800491 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400492 if (name != NULL) {
493 return strlcpy_int(out, name, out_len);
494 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800495 }
496 }
497
David Benjaminc895d6b2016-08-11 13:26:41 -0400498 CBB cbb;
499 if (!CBB_init(&cbb, 32)) {
500 goto err;
501 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800502
David Benjaminc895d6b2016-08-11 13:26:41 -0400503 CBS cbs;
504 CBS_init(&cbs, obj->data, obj->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800505
Robert Sloan8f860b12017-08-28 07:37:06 -0700506 // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
David Benjaminc895d6b2016-08-11 13:26:41 -0400507 uint64_t v;
508 if (!parse_oid_component(&cbs, &v)) {
509 goto err;
510 }
511
512 if (v >= 80) {
513 if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
514 !add_decimal(&cbb, v - 80)) {
515 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800516 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400517 } else if (!add_decimal(&cbb, v / 40) ||
518 !CBB_add_u8(&cbb, '.') ||
519 !add_decimal(&cbb, v % 40)) {
520 goto err;
521 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800522
David Benjaminc895d6b2016-08-11 13:26:41 -0400523 while (CBS_len(&cbs) != 0) {
524 if (!parse_oid_component(&cbs, &v) ||
525 !CBB_add_u8(&cbb, '.') ||
526 !add_decimal(&cbb, v)) {
527 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800528 }
529 }
530
David Benjaminc895d6b2016-08-11 13:26:41 -0400531 uint8_t *txt;
532 size_t txt_len;
533 if (!CBB_add_u8(&cbb, '\0') ||
534 !CBB_finish(&cbb, &txt, &txt_len)) {
535 goto err;
536 }
537
538 int ret = strlcpy_int(out, (const char *)txt, out_len);
539 OPENSSL_free(txt);
540 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800541
542err:
David Benjaminc895d6b2016-08-11 13:26:41 -0400543 CBB_cleanup(&cbb);
544 if (out_len > 0) {
545 out[0] = '\0';
546 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800547 return -1;
548}
549
550static uint32_t hash_nid(const ASN1_OBJECT *obj) {
551 return obj->nid;
552}
553
554static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
555 return a->nid - b->nid;
556}
557
558static uint32_t hash_data(const ASN1_OBJECT *obj) {
559 return OPENSSL_hash32(obj->data, obj->length);
560}
561
562static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
563 int i = a->length - b->length;
564 if (i) {
565 return i;
566 }
Robert Sloan69939df2017-01-09 10:53:07 -0800567 return OPENSSL_memcmp(a->data, b->data, a->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800568}
569
570static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
571 return lh_strhash(obj->sn);
572}
573
574static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
575 return strcmp(a->sn, b->sn);
576}
577
578static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
579 return lh_strhash(obj->ln);
580}
581
582static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
583 return strcmp(a->ln, b->ln);
584}
585
Robert Sloan8f860b12017-08-28 07:37:06 -0700586// obj_add_object inserts |obj| into the various global hashes for run-time
587// added objects. It returns one on success or zero otherwise.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800588static int obj_add_object(ASN1_OBJECT *obj) {
589 int ok;
590 ASN1_OBJECT *old_object;
591
592 obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
593 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
594
Adam Langleye9ada862015-05-11 17:20:37 -0700595 CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800596 if (global_added_by_nid == NULL) {
597 global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
598 global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
599 global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
600 global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
601 }
602
Robert Sloan8f860b12017-08-28 07:37:06 -0700603 // We don't pay attention to |old_object| (which contains any previous object
604 // that was evicted from the hashes) because we don't have a reference count
605 // on ASN1_OBJECT values. Also, we should never have duplicates nids and so
606 // should always have objects in |global_added_by_nid|.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800607
608 ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
609 if (obj->length != 0 && obj->data != NULL) {
610 ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
611 }
612 if (obj->sn != NULL) {
613 ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
614 }
615 if (obj->ln != NULL) {
616 ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
617 }
David Benjamind316cba2016-06-02 16:17:39 -0400618 CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800619
620 return ok;
621}
622
623int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
624 int ret = NID_undef;
625 ASN1_OBJECT *op = NULL;
626 unsigned char *buf = NULL;
627 int len;
628
629 len = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
630 if (len <= 0) {
631 goto err;
632 }
633
634 buf = OPENSSL_malloc(len);
635 if (buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000636 OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800637 goto err;
638 }
639
640 len = a2d_ASN1_OBJECT(buf, len, oid, -1);
641 if (len == 0) {
642 goto err;
643 }
644
645 op = (ASN1_OBJECT *)ASN1_OBJECT_create(obj_next_nid(), buf, len, short_name,
646 long_name);
647 if (op == NULL) {
648 goto err;
649 }
650
651 if (obj_add_object(op)) {
652 ret = op->nid;
653 }
654 op = NULL;
655
656err:
Adam Langleye9ada862015-05-11 17:20:37 -0700657 ASN1_OBJECT_free(op);
658 OPENSSL_free(buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800659
660 return ret;
661}