Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 57 | #if !defined(__STDC_FORMAT_MACROS) |
| 58 | #define __STDC_FORMAT_MACROS |
| 59 | #endif |
| 60 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 61 | #include <openssl/obj.h> |
| 62 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 63 | #include <inttypes.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 64 | #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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 76 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 77 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 78 | |
| 79 | static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 80 | // These globals are protected by |global_added_lock|. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 86 | static struct CRYPTO_STATIC_MUTEX global_next_nid_lock = |
| 87 | CRYPTO_STATIC_MUTEX_INIT; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 88 | static unsigned global_next_nid = NUM_NID; |
| 89 | |
| 90 | static int obj_next_nid(void) { |
| 91 | int ret; |
| 92 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 93 | CRYPTO_STATIC_MUTEX_lock_write(&global_next_nid_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 94 | ret = global_next_nid++; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 95 | CRYPTO_STATIC_MUTEX_unlock_write(&global_next_nid_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 110 | // TODO(fork): this is a little dangerous. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 111 | return (ASN1_OBJECT *)o; |
| 112 | } |
| 113 | |
| 114 | r = ASN1_OBJECT_new(); |
| 115 | if (r == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 116 | OPENSSL_PUT_ERROR(OBJ, ERR_R_ASN1_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 126 | OPENSSL_memcpy(data, o->data, o->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 129 | // once data is attached to an object, it remains const |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 143 | if (sn == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 157 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 158 | OPENSSL_free(ln); |
| 159 | OPENSSL_free(sn); |
| 160 | OPENSSL_free(data); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 172 | return OPENSSL_memcmp(a->data, b->data, a->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 175 | const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj) { |
| 176 | if (obj == NULL) { |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | return obj->data; |
| 181 | } |
| 182 | |
| 183 | size_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 Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 191 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 194 | static int obj_cmp(const void *key, const void *element) { |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 195 | unsigned nid = *((const unsigned*) element); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 196 | const ASN1_OBJECT *a = key; |
| 197 | const ASN1_OBJECT *b = &kObjects[nid]; |
| 198 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 199 | if (a->length < b->length) { |
| 200 | return -1; |
| 201 | } else if (a->length > b->length) { |
| 202 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 203 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 204 | return OPENSSL_memcmp(a->data, b->data, a->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | int 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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 218 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 219 | 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 Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 224 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 225 | return match->nid; |
| 226 | } |
| 227 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 228 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 229 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 230 | nid_ptr = bsearch(obj, kNIDsInOIDOrder, OPENSSL_ARRAY_SIZE(kNIDsInOIDOrder), |
| 231 | sizeof(kNIDsInOIDOrder[0]), obj_cmp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | if (nid_ptr == NULL) { |
| 233 | return NID_undef; |
| 234 | } |
| 235 | |
| 236 | return kObjects[*nid_ptr].nid; |
| 237 | } |
| 238 | |
| 239 | int OBJ_cbs2nid(const CBS *cbs) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 240 | if (CBS_len(cbs) > INT_MAX) { |
| 241 | return NID_undef; |
| 242 | } |
| 243 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 244 | ASN1_OBJECT obj; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 245 | OPENSSL_memset(&obj, 0, sizeof(obj)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | obj.data = CBS_data(cbs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 247 | obj.length = (int)CBS_len(cbs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 248 | |
| 249 | return OBJ_obj2nid(&obj); |
| 250 | } |
| 251 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 252 | // short_name_cmp is called to search the kNIDsInShortNameOrder array. The |
| 253 | // |key| argument is name that we're looking for and |element| is a pointer to |
| 254 | // an unsigned int in the array. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | static 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 | |
| 262 | int OBJ_sn2nid(const char *short_name) { |
| 263 | const unsigned int *nid_ptr; |
| 264 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 265 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 266 | 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 Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 272 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 273 | return match->nid; |
| 274 | } |
| 275 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 276 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 277 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 278 | nid_ptr = bsearch(short_name, kNIDsInShortNameOrder, |
| 279 | OPENSSL_ARRAY_SIZE(kNIDsInShortNameOrder), |
| 280 | sizeof(kNIDsInShortNameOrder[0]), short_name_cmp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 281 | if (nid_ptr == NULL) { |
| 282 | return NID_undef; |
| 283 | } |
| 284 | |
| 285 | return kObjects[*nid_ptr].nid; |
| 286 | } |
| 287 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 288 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | static 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 | |
| 298 | int OBJ_ln2nid(const char *long_name) { |
| 299 | const unsigned int *nid_ptr; |
| 300 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 301 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 302 | 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 Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 308 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 309 | return match->nid; |
| 310 | } |
| 311 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 312 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 313 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 314 | nid_ptr = bsearch(long_name, kNIDsInLongNameOrder, |
| 315 | OPENSSL_ARRAY_SIZE(kNIDsInLongNameOrder), |
| 316 | sizeof(kNIDsInLongNameOrder[0]), long_name_cmp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | if (nid_ptr == NULL) { |
| 318 | return NID_undef; |
| 319 | } |
| 320 | |
| 321 | return kObjects[*nid_ptr].nid; |
| 322 | } |
| 323 | |
| 324 | int 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 | |
| 334 | OPENSSL_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 | |
| 348 | const 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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 356 | CRYPTO_STATIC_MUTEX_lock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 357 | 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 Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 363 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 364 | return match; |
| 365 | } |
| 366 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 367 | CRYPTO_STATIC_MUTEX_unlock_read(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 368 | |
| 369 | err: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 370 | OPENSSL_PUT_ERROR(OBJ, OBJ_R_UNKNOWN_NID); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | const 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 | |
| 383 | const 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 | |
| 392 | ASN1_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 Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 411 | // Work out size of content octets |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 412 | contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1); |
| 413 | if (contents_len <= 0) { |
| 414 | return NULL; |
| 415 | } |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 416 | // Work out total size |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 417 | total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT); |
| 418 | |
| 419 | buf = OPENSSL_malloc(total_len); |
| 420 | if (buf == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 421 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | p = buf; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 426 | // Write out tag+length |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 427 | ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 428 | // Write out contents |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 429 | 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 Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 438 | static 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 443 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 444 | return (int)ret; |
| 445 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 446 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 447 | static 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 453 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 454 | if ((v >> (64 - 7)) != 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 455 | // The component is too large. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | if (v == 0 && b == 0x80) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 459 | // The component must be minimally encoded. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | v = (v << 7) | (b & 0x7f); |
| 463 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 464 | // Components end at an octet with the high bit cleared. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 465 | } while (b & 0x80); |
| 466 | |
| 467 | *out = v; |
| 468 | return 1; |
| 469 | } |
| 470 | |
| 471 | static 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 | |
| 477 | int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, |
| 478 | int always_return_oid) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 479 | // Python depends on the empty OID successfully encoding as the empty |
| 480 | // string. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 481 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 491 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 492 | if (name != NULL) { |
| 493 | return strlcpy_int(out, name, out_len); |
| 494 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 498 | CBB cbb; |
| 499 | if (!CBB_init(&cbb, 32)) { |
| 500 | goto err; |
| 501 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 502 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 503 | CBS cbs; |
| 504 | CBS_init(&cbs, obj->data, obj->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 505 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 506 | // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 507 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 516 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 517 | } else if (!add_decimal(&cbb, v / 40) || |
| 518 | !CBB_add_u8(&cbb, '.') || |
| 519 | !add_decimal(&cbb, v % 40)) { |
| 520 | goto err; |
| 521 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 522 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 523 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 531 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 541 | |
| 542 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 543 | CBB_cleanup(&cbb); |
| 544 | if (out_len > 0) { |
| 545 | out[0] = '\0'; |
| 546 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 547 | return -1; |
| 548 | } |
| 549 | |
| 550 | static uint32_t hash_nid(const ASN1_OBJECT *obj) { |
| 551 | return obj->nid; |
| 552 | } |
| 553 | |
| 554 | static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 555 | return a->nid - b->nid; |
| 556 | } |
| 557 | |
| 558 | static uint32_t hash_data(const ASN1_OBJECT *obj) { |
| 559 | return OPENSSL_hash32(obj->data, obj->length); |
| 560 | } |
| 561 | |
| 562 | static 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 Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 567 | return OPENSSL_memcmp(a->data, b->data, a->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static uint32_t hash_short_name(const ASN1_OBJECT *obj) { |
| 571 | return lh_strhash(obj->sn); |
| 572 | } |
| 573 | |
| 574 | static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 575 | return strcmp(a->sn, b->sn); |
| 576 | } |
| 577 | |
| 578 | static uint32_t hash_long_name(const ASN1_OBJECT *obj) { |
| 579 | return lh_strhash(obj->ln); |
| 580 | } |
| 581 | |
| 582 | static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) { |
| 583 | return strcmp(a->ln, b->ln); |
| 584 | } |
| 585 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 586 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 588 | static 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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 595 | CRYPTO_STATIC_MUTEX_lock_write(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 596 | 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 Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 603 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 607 | |
| 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 Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 618 | CRYPTO_STATIC_MUTEX_unlock_write(&global_added_lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 619 | |
| 620 | return ok; |
| 621 | } |
| 622 | |
| 623 | int 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 Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 636 | OPENSSL_PUT_ERROR(OBJ, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 637 | 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 | |
| 656 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 657 | ASN1_OBJECT_free(op); |
| 658 | OPENSSL_free(buf); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 659 | |
| 660 | return ret; |
| 661 | } |