Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* 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 Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 57 | #if !defined(__STDC_FORMAT_MACROS) |
| 58 | #define __STDC_FORMAT_MACROS |
| 59 | #endif |
| 60 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | #include <openssl/obj.h> |
| 62 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 63 | #include <inttypes.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | #include <limits.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 65 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
David Benjamin | 8a5825e | 2014-08-30 21:33:54 -0400 | [diff] [blame] | 75 | #include "obj_dat.h" |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 76 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 78 | |
| 79 | static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 80 | /* These globals are protected by |global_added_lock|. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 81 | static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL; |
| 82 | static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL; |
| 83 | static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL; |
| 84 | static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL; |
| 85 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 86 | static struct CRYPTO_STATIC_MUTEX global_next_nid_lock = |
| 87 | CRYPTO_STATIC_MUTEX_INIT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 88 | static unsigned global_next_nid = NUM_NID; |
| 89 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 90 | static int obj_next_nid(void) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 91 | int ret; |
| 92 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 93 | CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 94 | ret = global_next_nid++; |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 95 | CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | ASN1_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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 116 | OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | 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 Braithwaite | 9626f26 | 2015-04-20 18:07:32 -0700 | [diff] [blame] | 143 | if (sn == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | 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 | |
| 156 | err: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 157 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
David Benjamin | d8b65c8 | 2015-04-22 16:09:09 -0400 | [diff] [blame] | 158 | OPENSSL_free(ln); |
| 159 | OPENSSL_free(sn); |
| 160 | OPENSSL_free(data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | OPENSSL_free(r); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | int 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 Benjamin | efd8eb3 | 2015-05-26 17:57:41 -0400 | [diff] [blame] | 175 | /* 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | * unsigned int in the array. */ |
| 178 | static int obj_cmp(const void *key, const void *element) { |
David Benjamin | efd8eb3 | 2015-05-26 17:57:41 -0400 | [diff] [blame] | 179 | unsigned nid = *((const unsigned*) element); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | const ASN1_OBJECT *a = key; |
| 181 | const ASN1_OBJECT *b = &kObjects[nid]; |
| 182 | |
David Benjamin | efd8eb3 | 2015-05-26 17:57:41 -0400 | [diff] [blame] | 183 | if (a->length < b->length) { |
| 184 | return -1; |
| 185 | } else if (a->length > b->length) { |
| 186 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | } |
| 188 | return memcmp(a->data, b->data, a->length); |
| 189 | } |
| 190 | |
| 191 | int 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 Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 202 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | 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 Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 208 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | return match->nid; |
| 210 | } |
| 211 | } |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 212 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 213 | |
| 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 | |
| 222 | int OBJ_cbs2nid(const CBS *cbs) { |
David Benjamin | 22edd87 | 2016-08-04 21:38:40 +0000 | [diff] [blame] | 223 | if (CBS_len(cbs) > INT_MAX) { |
| 224 | return NID_undef; |
| 225 | } |
| 226 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | ASN1_OBJECT obj; |
| 228 | memset(&obj, 0, sizeof(obj)); |
| 229 | obj.data = CBS_data(cbs); |
David Benjamin | 22edd87 | 2016-08-04 21:38:40 +0000 | [diff] [blame] | 230 | obj.length = (int)CBS_len(cbs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | |
| 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. */ |
| 238 | static 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 | |
| 245 | int OBJ_sn2nid(const char *short_name) { |
| 246 | const unsigned int *nid_ptr; |
| 247 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 248 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | 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 Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 255 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | return match->nid; |
| 257 | } |
| 258 | } |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 259 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
| 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. */ |
| 272 | static 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 | |
| 279 | int OBJ_ln2nid(const char *long_name) { |
| 280 | const unsigned int *nid_ptr; |
| 281 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 282 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | 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 Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 289 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | return match->nid; |
| 291 | } |
| 292 | } |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 293 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
| 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 | |
| 303 | int 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 Langley | eeb9f49 | 2014-08-06 16:29:56 -0700 | [diff] [blame] | 313 | OPENSSL_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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | const 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 Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 335 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | 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 Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 342 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 343 | return match; |
| 344 | } |
| 345 | } |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 346 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | |
| 348 | err: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 349 | OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | const 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 | |
| 362 | const 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 | |
| 371 | ASN1_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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 400 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | 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 Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 417 | static 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 422 | } |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 423 | return (int)ret; |
| 424 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 426 | static 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | } |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 433 | 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 | |
| 450 | static 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 | |
| 456 | int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, |
Adam Langley | fbe3a7b | 2016-08-09 21:25:45 -0700 | [diff] [blame] | 457 | int always_return_oid) { |
Martin Kreichgauer | 909232d | 2016-08-09 13:53:36 -0700 | [diff] [blame] | 458 | /* 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 Langley | fbe3a7b | 2016-08-09 21:25:45 -0700 | [diff] [blame] | 464 | if (!always_return_oid) { |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 465 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 470 | } |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 471 | if (name != NULL) { |
| 472 | return strlcpy_int(out, name, out_len); |
| 473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 477 | CBB cbb; |
| 478 | if (!CBB_init(&cbb, 32)) { |
| 479 | goto err; |
| 480 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 482 | CBS cbs; |
| 483 | CBS_init(&cbs, obj->data, obj->length); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 485 | /* 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | } |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 496 | } else if (!add_decimal(&cbb, v / 40) || |
| 497 | !CBB_add_u8(&cbb, '.') || |
| 498 | !add_decimal(&cbb, v % 40)) { |
| 499 | goto err; |
| 500 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 502 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 510 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 520 | |
| 521 | err: |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 522 | CBB_cleanup(&cbb); |
| 523 | if (out_len > 0) { |
| 524 | out[0] = '\0'; |
| 525 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | return -1; |
| 527 | } |
| 528 | |
| 529 | static uint32_t hash_nid(const ASN1_OBJECT *obj) { |
| 530 | return obj->nid; |
| 531 | } |
| 532 | |
| 533 | static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 534 | return a->nid - b->nid; |
| 535 | } |
| 536 | |
| 537 | static uint32_t hash_data(const ASN1_OBJECT *obj) { |
| 538 | return OPENSSL_hash32(obj->data, obj->length); |
| 539 | } |
| 540 | |
| 541 | static 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 | |
| 549 | static uint32_t hash_short_name(const ASN1_OBJECT *obj) { |
| 550 | return lh_strhash(obj->sn); |
| 551 | } |
| 552 | |
| 553 | static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 554 | return strcmp(a->sn, b->sn); |
| 555 | } |
| 556 | |
| 557 | static uint32_t hash_long_name(const ASN1_OBJECT *obj) { |
| 558 | return lh_strhash(obj->ln); |
| 559 | } |
| 560 | |
| 561 | static 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. */ |
| 567 | static 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 Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 574 | CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 575 | 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 Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 597 | CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | |
| 599 | return ok; |
| 600 | } |
| 601 | |
| 602 | int 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 615 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | 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 | |
| 635 | err: |
David Benjamin | d8b65c8 | 2015-04-22 16:09:09 -0400 | [diff] [blame] | 636 | ASN1_OBJECT_free(op); |
| 637 | OPENSSL_free(buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | |
| 639 | return ret; |
| 640 | } |