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 | |
| 57 | #include <openssl/obj.h> |
| 58 | |
| 59 | #include <limits.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 60 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | |
| 62 | #include <openssl/asn1.h> |
| 63 | #include <openssl/buf.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 | |
David Benjamin | 8a5825e | 2014-08-30 21:33:54 -0400 | [diff] [blame] | 70 | #include "obj_dat.h" |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 71 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 72 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 73 | |
| 74 | static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 75 | /* These globals are protected by |global_added_lock|. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 76 | static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL; |
| 77 | static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL; |
| 78 | static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL; |
| 79 | static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL; |
| 80 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 81 | static struct CRYPTO_STATIC_MUTEX global_next_nid_lock = |
| 82 | CRYPTO_STATIC_MUTEX_INIT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 83 | static unsigned global_next_nid = NUM_NID; |
| 84 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 85 | static int obj_next_nid(void) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 86 | int ret; |
| 87 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 88 | CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | ret = global_next_nid++; |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 90 | CRYPTO_STATIC_MUTEX_unlock(&global_next_nid_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) { |
| 96 | ASN1_OBJECT *r; |
| 97 | unsigned char *data = NULL; |
| 98 | char *sn = NULL, *ln = NULL; |
| 99 | |
| 100 | if (o == NULL) { |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { |
| 105 | /* TODO(fork): this is a little dangerous. */ |
| 106 | return (ASN1_OBJECT *)o; |
| 107 | } |
| 108 | |
| 109 | r = ASN1_OBJECT_new(); |
| 110 | if (r == NULL) { |
| 111 | OPENSSL_PUT_ERROR(OBJ, OBJ_dup, ERR_R_ASN1_LIB); |
| 112 | return NULL; |
| 113 | } |
| 114 | r->ln = r->sn = NULL; |
| 115 | |
| 116 | data = OPENSSL_malloc(o->length); |
| 117 | if (data == NULL) { |
| 118 | goto err; |
| 119 | } |
| 120 | if (o->data != NULL) { |
| 121 | memcpy(data, o->data, o->length); |
| 122 | } |
| 123 | |
| 124 | /* once data is attached to an object, it remains const */ |
| 125 | r->data = data; |
| 126 | r->length = o->length; |
| 127 | r->nid = o->nid; |
| 128 | |
| 129 | if (o->ln != NULL) { |
| 130 | ln = OPENSSL_strdup(o->ln); |
| 131 | if (ln == NULL) { |
| 132 | goto err; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (o->sn != NULL) { |
| 137 | sn = OPENSSL_strdup(o->sn); |
Matt Braithwaite | 9626f26 | 2015-04-20 18:07:32 -0700 | [diff] [blame] | 138 | if (sn == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | goto err; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | r->sn = sn; |
| 144 | r->ln = ln; |
| 145 | |
| 146 | r->flags = |
| 147 | o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
| 148 | ASN1_OBJECT_FLAG_DYNAMIC_DATA); |
| 149 | return r; |
| 150 | |
| 151 | err: |
| 152 | OPENSSL_PUT_ERROR(OBJ, OBJ_dup, ERR_R_MALLOC_FAILURE); |
| 153 | if (ln != NULL) { |
| 154 | OPENSSL_free(ln); |
| 155 | } |
| 156 | if (sn != NULL) { |
| 157 | OPENSSL_free(sn); |
| 158 | } |
| 159 | if (data != NULL) { |
| 160 | OPENSSL_free(data); |
| 161 | } |
| 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 memcmp(a->data, b->data, a->length); |
| 174 | } |
| 175 | |
| 176 | /* nids_cmp is called to search the kNIDsInOIDOrder array. The |key| argument |
| 177 | * is an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an |
| 178 | * unsigned int in the array. */ |
| 179 | static int obj_cmp(const void *key, const void *element) { |
| 180 | int j; |
| 181 | unsigned nid = *((unsigned*) element); |
| 182 | const ASN1_OBJECT *a = key; |
| 183 | const ASN1_OBJECT *b = &kObjects[nid]; |
| 184 | |
| 185 | j = a->length - b->length; |
| 186 | if (j) { |
| 187 | return j; |
| 188 | } |
| 189 | return memcmp(a->data, b->data, a->length); |
| 190 | } |
| 191 | |
| 192 | int OBJ_obj2nid(const ASN1_OBJECT *obj) { |
| 193 | const unsigned int *nid_ptr; |
| 194 | |
| 195 | if (obj == NULL) { |
| 196 | return NID_undef; |
| 197 | } |
| 198 | |
| 199 | if (obj->nid != 0) { |
| 200 | return obj->nid; |
| 201 | } |
| 202 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 203 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 204 | if (global_added_by_data != NULL) { |
| 205 | ASN1_OBJECT *match; |
| 206 | |
| 207 | match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj); |
| 208 | if (match != NULL) { |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 209 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | return match->nid; |
| 211 | } |
| 212 | } |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 213 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
| 215 | nid_ptr = bsearch(obj, kNIDsInOIDOrder, NUM_OBJ, sizeof(unsigned), obj_cmp); |
| 216 | if (nid_ptr == NULL) { |
| 217 | return NID_undef; |
| 218 | } |
| 219 | |
| 220 | return kObjects[*nid_ptr].nid; |
| 221 | } |
| 222 | |
| 223 | int OBJ_cbs2nid(const CBS *cbs) { |
| 224 | ASN1_OBJECT obj; |
| 225 | memset(&obj, 0, sizeof(obj)); |
| 226 | obj.data = CBS_data(cbs); |
| 227 | obj.length = CBS_len(cbs); |
| 228 | |
| 229 | return OBJ_obj2nid(&obj); |
| 230 | } |
| 231 | |
| 232 | /* short_name_cmp is called to search the kNIDsInShortNameOrder array. The |
| 233 | * |key| argument is name that we're looking for and |element| is a pointer to |
| 234 | * an unsigned int in the array. */ |
| 235 | static int short_name_cmp(const void *key, const void *element) { |
| 236 | const char *name = (const char *) key; |
| 237 | unsigned nid = *((unsigned*) element); |
| 238 | |
| 239 | return strcmp(name, kObjects[nid].sn); |
| 240 | } |
| 241 | |
| 242 | int OBJ_sn2nid(const char *short_name) { |
| 243 | const unsigned int *nid_ptr; |
| 244 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 245 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 246 | if (global_added_by_short_name != NULL) { |
| 247 | ASN1_OBJECT *match, template; |
| 248 | |
| 249 | template.sn = short_name; |
| 250 | match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template); |
| 251 | if (match != NULL) { |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 252 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 253 | return match->nid; |
| 254 | } |
| 255 | } |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 256 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
| 258 | nid_ptr = bsearch(short_name, kNIDsInShortNameOrder, NUM_SN, sizeof(unsigned), short_name_cmp); |
| 259 | if (nid_ptr == NULL) { |
| 260 | return NID_undef; |
| 261 | } |
| 262 | |
| 263 | return kObjects[*nid_ptr].nid; |
| 264 | } |
| 265 | |
| 266 | /* long_name_cmp is called to search the kNIDsInLongNameOrder array. The |
| 267 | * |key| argument is name that we're looking for and |element| is a pointer to |
| 268 | * an unsigned int in the array. */ |
| 269 | static int long_name_cmp(const void *key, const void *element) { |
| 270 | const char *name = (const char *) key; |
| 271 | unsigned nid = *((unsigned*) element); |
| 272 | |
| 273 | return strcmp(name, kObjects[nid].ln); |
| 274 | } |
| 275 | |
| 276 | int OBJ_ln2nid(const char *long_name) { |
| 277 | const unsigned int *nid_ptr; |
| 278 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 279 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | if (global_added_by_long_name != NULL) { |
| 281 | ASN1_OBJECT *match, template; |
| 282 | |
| 283 | template.ln = long_name; |
| 284 | match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template); |
| 285 | if (match != NULL) { |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 286 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | return match->nid; |
| 288 | } |
| 289 | } |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 290 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | |
| 292 | nid_ptr = bsearch(long_name, kNIDsInLongNameOrder, NUM_LN, sizeof(unsigned), long_name_cmp); |
| 293 | if (nid_ptr == NULL) { |
| 294 | return NID_undef; |
| 295 | } |
| 296 | |
| 297 | return kObjects[*nid_ptr].nid; |
| 298 | } |
| 299 | |
| 300 | int OBJ_txt2nid(const char *s) { |
| 301 | ASN1_OBJECT *obj; |
| 302 | int nid; |
| 303 | |
| 304 | obj = OBJ_txt2obj(s, 0 /* search names */); |
| 305 | nid = OBJ_obj2nid(obj); |
| 306 | ASN1_OBJECT_free(obj); |
| 307 | return nid; |
| 308 | } |
| 309 | |
Adam Langley | eeb9f49 | 2014-08-06 16:29:56 -0700 | [diff] [blame] | 310 | OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) { |
| 311 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
| 312 | CBB oid; |
| 313 | |
| 314 | if (obj == NULL || |
| 315 | !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) || |
| 316 | !CBB_add_bytes(&oid, obj->data, obj->length) || |
| 317 | !CBB_flush(out)) { |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | return 1; |
| 322 | } |
| 323 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | const ASN1_OBJECT *OBJ_nid2obj(int nid) { |
| 325 | if (nid >= 0 && nid < NUM_NID) { |
| 326 | if (nid != NID_undef && kObjects[nid].nid == NID_undef) { |
| 327 | goto err; |
| 328 | } |
| 329 | return &kObjects[nid]; |
| 330 | } |
| 331 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 332 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | if (global_added_by_nid != NULL) { |
| 334 | ASN1_OBJECT *match, template; |
| 335 | |
| 336 | template.nid = nid; |
| 337 | match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template); |
| 338 | if (match != NULL) { |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 339 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 340 | return match; |
| 341 | } |
| 342 | } |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 343 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | |
| 345 | err: |
| 346 | OPENSSL_PUT_ERROR(OBJ, OBJ_nid2obj, OBJ_R_UNKNOWN_NID); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | const char *OBJ_nid2sn(int nid) { |
| 351 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
| 352 | if (obj == NULL) { |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | return obj->sn; |
| 357 | } |
| 358 | |
| 359 | const char *OBJ_nid2ln(int nid) { |
| 360 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
| 361 | if (obj == NULL) { |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | return obj->ln; |
| 366 | } |
| 367 | |
| 368 | ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) { |
| 369 | int nid = NID_undef; |
| 370 | ASN1_OBJECT *op = NULL; |
| 371 | unsigned char *buf; |
| 372 | unsigned char *p; |
| 373 | const unsigned char *bufp; |
| 374 | int contents_len, total_len; |
| 375 | |
| 376 | if (!dont_search_names) { |
| 377 | nid = OBJ_sn2nid(s); |
| 378 | if (nid == NID_undef) { |
| 379 | nid = OBJ_ln2nid(s); |
| 380 | } |
| 381 | |
| 382 | if (nid != NID_undef) { |
| 383 | return (ASN1_OBJECT*) OBJ_nid2obj(nid); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* Work out size of content octets */ |
| 388 | contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1); |
| 389 | if (contents_len <= 0) { |
| 390 | return NULL; |
| 391 | } |
| 392 | /* Work out total size */ |
| 393 | total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT); |
| 394 | |
| 395 | buf = OPENSSL_malloc(total_len); |
| 396 | if (buf == NULL) { |
| 397 | OPENSSL_PUT_ERROR(OBJ, OBJ_txt2obj, ERR_R_MALLOC_FAILURE); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | p = buf; |
| 402 | /* Write out tag+length */ |
| 403 | ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
| 404 | /* Write out contents */ |
| 405 | a2d_ASN1_OBJECT(p, contents_len, s, -1); |
| 406 | |
| 407 | bufp = buf; |
| 408 | op = d2i_ASN1_OBJECT(NULL, &bufp, total_len); |
| 409 | OPENSSL_free(buf); |
| 410 | |
| 411 | return op; |
| 412 | } |
| 413 | |
| 414 | int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, int dont_return_name) { |
| 415 | int i, n = 0, len, nid, first, use_bn; |
| 416 | BIGNUM *bl; |
| 417 | unsigned long l; |
| 418 | const unsigned char *p; |
| 419 | char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2]; |
| 420 | |
| 421 | if (out && out_len > 0) { |
| 422 | out[0] = 0; |
| 423 | } |
| 424 | |
| 425 | if (obj == NULL || obj->data == NULL) { |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | if (!dont_return_name && (nid = OBJ_obj2nid(obj)) != NID_undef) { |
| 430 | const char *s; |
| 431 | s = OBJ_nid2ln(nid); |
| 432 | if (s == NULL) { |
| 433 | s = OBJ_nid2sn(nid); |
| 434 | } |
| 435 | if (s) { |
| 436 | if (out) { |
| 437 | BUF_strlcpy(out, s, out_len); |
| 438 | } |
| 439 | return strlen(s); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | len = obj->length; |
| 444 | p = obj->data; |
| 445 | |
| 446 | first = 1; |
| 447 | bl = NULL; |
| 448 | |
| 449 | while (len > 0) { |
| 450 | l = 0; |
| 451 | use_bn = 0; |
| 452 | for (;;) { |
| 453 | unsigned char c = *p++; |
| 454 | len--; |
| 455 | if (len == 0 && (c & 0x80)) { |
| 456 | goto err; |
| 457 | } |
| 458 | if (use_bn) { |
| 459 | if (!BN_add_word(bl, c & 0x7f)) { |
| 460 | goto err; |
| 461 | } |
| 462 | } else { |
| 463 | l |= c & 0x7f; |
| 464 | } |
| 465 | if (!(c & 0x80)) { |
| 466 | break; |
| 467 | } |
| 468 | if (!use_bn && (l > (ULONG_MAX >> 7L))) { |
| 469 | if (!bl && !(bl = BN_new())) { |
| 470 | goto err; |
| 471 | } |
| 472 | if (!BN_set_word(bl, l)) { |
| 473 | goto err; |
| 474 | } |
| 475 | use_bn = 1; |
| 476 | } |
| 477 | if (use_bn) { |
| 478 | if (!BN_lshift(bl, bl, 7)) { |
| 479 | goto err; |
| 480 | } |
| 481 | } else { |
| 482 | l <<= 7L; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (first) { |
| 487 | first = 0; |
| 488 | if (l >= 80) { |
| 489 | i = 2; |
| 490 | if (use_bn) { |
| 491 | if (!BN_sub_word(bl, 80)) { |
| 492 | goto err; |
| 493 | } |
| 494 | } else { |
| 495 | l -= 80; |
| 496 | } |
| 497 | } else { |
| 498 | i = (int)(l / 40); |
| 499 | l -= (long)(i * 40); |
| 500 | } |
| 501 | if (out && out_len > 1) { |
| 502 | *out++ = i + '0'; |
| 503 | *out = '0'; |
| 504 | out_len--; |
| 505 | } |
| 506 | n++; |
| 507 | } |
| 508 | |
| 509 | if (use_bn) { |
| 510 | char *bndec; |
| 511 | bndec = BN_bn2dec(bl); |
| 512 | if (!bndec) { |
| 513 | goto err; |
| 514 | } |
| 515 | i = strlen(bndec); |
| 516 | if (out) { |
| 517 | if (out_len > 1) { |
| 518 | *out++ = '.'; |
| 519 | *out = 0; |
| 520 | out_len--; |
| 521 | } |
| 522 | BUF_strlcpy(out, bndec, out_len); |
| 523 | if (i > out_len) { |
| 524 | out += out_len; |
| 525 | out_len = 0; |
| 526 | } else { |
| 527 | out += i; |
| 528 | out_len -= i; |
| 529 | } |
| 530 | } |
| 531 | n++; |
| 532 | n += i; |
| 533 | OPENSSL_free(bndec); |
| 534 | } else { |
| 535 | BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l); |
| 536 | i = strlen(tbuf); |
| 537 | if (out && out_len > 0) { |
| 538 | BUF_strlcpy(out, tbuf, out_len); |
| 539 | if (i > out_len) { |
| 540 | out += out_len; |
| 541 | out_len = 0; |
| 542 | } else { |
| 543 | out += i; |
| 544 | out_len -= i; |
| 545 | } |
| 546 | } |
| 547 | n += i; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (bl) { |
| 552 | BN_free(bl); |
| 553 | } |
| 554 | return n; |
| 555 | |
| 556 | err: |
| 557 | if (bl) { |
| 558 | BN_free(bl); |
| 559 | } |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | static uint32_t hash_nid(const ASN1_OBJECT *obj) { |
| 564 | return obj->nid; |
| 565 | } |
| 566 | |
| 567 | static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 568 | return a->nid - b->nid; |
| 569 | } |
| 570 | |
| 571 | static uint32_t hash_data(const ASN1_OBJECT *obj) { |
| 572 | return OPENSSL_hash32(obj->data, obj->length); |
| 573 | } |
| 574 | |
| 575 | static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 576 | int i = a->length - b->length; |
| 577 | if (i) { |
| 578 | return i; |
| 579 | } |
| 580 | return memcmp(a->data, b->data, a->length); |
| 581 | } |
| 582 | |
| 583 | static uint32_t hash_short_name(const ASN1_OBJECT *obj) { |
| 584 | return lh_strhash(obj->sn); |
| 585 | } |
| 586 | |
| 587 | static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 588 | return strcmp(a->sn, b->sn); |
| 589 | } |
| 590 | |
| 591 | static uint32_t hash_long_name(const ASN1_OBJECT *obj) { |
| 592 | return lh_strhash(obj->ln); |
| 593 | } |
| 594 | |
| 595 | static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 596 | return strcmp(a->ln, b->ln); |
| 597 | } |
| 598 | |
| 599 | /* obj_add_object inserts |obj| into the various global hashes for run-time |
| 600 | * added objects. It returns one on success or zero otherwise. */ |
| 601 | static int obj_add_object(ASN1_OBJECT *obj) { |
| 602 | int ok; |
| 603 | ASN1_OBJECT *old_object; |
| 604 | |
| 605 | obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
| 606 | ASN1_OBJECT_FLAG_DYNAMIC_DATA); |
| 607 | |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 608 | CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 609 | if (global_added_by_nid == NULL) { |
| 610 | global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid); |
| 611 | global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data); |
| 612 | global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name); |
| 613 | global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name); |
| 614 | } |
| 615 | |
| 616 | /* We don't pay attention to |old_object| (which contains any previous object |
| 617 | * that was evicted from the hashes) because we don't have a reference count |
| 618 | * on ASN1_OBJECT values. Also, we should never have duplicates nids and so |
| 619 | * should always have objects in |global_added_by_nid|. */ |
| 620 | |
| 621 | ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj); |
| 622 | if (obj->length != 0 && obj->data != NULL) { |
| 623 | ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj); |
| 624 | } |
| 625 | if (obj->sn != NULL) { |
| 626 | ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj); |
| 627 | } |
| 628 | if (obj->ln != NULL) { |
| 629 | ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj); |
| 630 | } |
Adam Langley | ba3bef9 | 2015-04-13 11:04:20 -0700 | [diff] [blame] | 631 | CRYPTO_STATIC_MUTEX_unlock(&global_added_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 632 | |
| 633 | return ok; |
| 634 | } |
| 635 | |
| 636 | int OBJ_create(const char *oid, const char *short_name, const char *long_name) { |
| 637 | int ret = NID_undef; |
| 638 | ASN1_OBJECT *op = NULL; |
| 639 | unsigned char *buf = NULL; |
| 640 | int len; |
| 641 | |
| 642 | len = a2d_ASN1_OBJECT(NULL, 0, oid, -1); |
| 643 | if (len <= 0) { |
| 644 | goto err; |
| 645 | } |
| 646 | |
| 647 | buf = OPENSSL_malloc(len); |
| 648 | if (buf == NULL) { |
| 649 | OPENSSL_PUT_ERROR(OBJ, OBJ_create, ERR_R_MALLOC_FAILURE); |
| 650 | goto err; |
| 651 | } |
| 652 | |
| 653 | len = a2d_ASN1_OBJECT(buf, len, oid, -1); |
| 654 | if (len == 0) { |
| 655 | goto err; |
| 656 | } |
| 657 | |
| 658 | op = (ASN1_OBJECT *)ASN1_OBJECT_create(obj_next_nid(), buf, len, short_name, |
| 659 | long_name); |
| 660 | if (op == NULL) { |
| 661 | goto err; |
| 662 | } |
| 663 | |
| 664 | if (obj_add_object(op)) { |
| 665 | ret = op->nid; |
| 666 | } |
| 667 | op = NULL; |
| 668 | |
| 669 | err: |
| 670 | if (op != NULL) { |
| 671 | ASN1_OBJECT_free(op); |
| 672 | } |
| 673 | if (buf != NULL) { |
| 674 | OPENSSL_free(buf); |
| 675 | } |
| 676 | |
| 677 | return ret; |
| 678 | } |