[331] | 1 | /*
|
---|
| 2 | * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /* ====================================================================
|
---|
| 11 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
|
---|
| 12 | * ECDH support in OpenSSL originally developed by
|
---|
| 13 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | #include "eng_int.h"
|
---|
| 17 |
|
---|
| 18 | /*
|
---|
| 19 | * The linked-list of pointers to engine types. engine_list_head incorporates
|
---|
| 20 | * an implicit structural reference but engine_list_tail does not - the
|
---|
| 21 | * latter is a computational niceity and only points to something that is
|
---|
| 22 | * already pointed to by its predecessor in the list (or engine_list_head
|
---|
| 23 | * itself). In the same way, the use of the "prev" pointer in each ENGINE is
|
---|
| 24 | * to save excessive list iteration, it doesn't correspond to an extra
|
---|
| 25 | * structural reference. Hence, engine_list_head, and each non-null "next"
|
---|
| 26 | * pointer account for the list itself assuming exactly 1 structural
|
---|
| 27 | * reference on each list member.
|
---|
| 28 | */
|
---|
| 29 | static ENGINE *engine_list_head = NULL;
|
---|
| 30 | static ENGINE *engine_list_tail = NULL;
|
---|
| 31 |
|
---|
| 32 | /*
|
---|
| 33 | * This cleanup function is only needed internally. If it should be called,
|
---|
| 34 | * we register it with the "engine_cleanup_int()" stack to be called during
|
---|
| 35 | * cleanup.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | static void engine_list_cleanup(void)
|
---|
| 39 | {
|
---|
| 40 | ENGINE *iterator = engine_list_head;
|
---|
| 41 |
|
---|
| 42 | while (iterator != NULL) {
|
---|
| 43 | ENGINE_remove(iterator);
|
---|
| 44 | iterator = engine_list_head;
|
---|
| 45 | }
|
---|
| 46 | return;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /*
|
---|
| 50 | * These static functions starting with a lower case "engine_" always take
|
---|
| 51 | * place when global_engine_lock has been locked up.
|
---|
| 52 | */
|
---|
| 53 | static int engine_list_add(ENGINE *e)
|
---|
| 54 | {
|
---|
| 55 | int conflict = 0;
|
---|
| 56 | ENGINE *iterator = NULL;
|
---|
| 57 |
|
---|
| 58 | if (e == NULL) {
|
---|
| 59 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 60 | return 0;
|
---|
| 61 | }
|
---|
| 62 | iterator = engine_list_head;
|
---|
| 63 | while (iterator && !conflict) {
|
---|
| 64 | conflict = (strcmp(iterator->id, e->id) == 0);
|
---|
| 65 | iterator = iterator->next;
|
---|
| 66 | }
|
---|
| 67 | if (conflict) {
|
---|
| 68 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
|
---|
| 69 | return 0;
|
---|
| 70 | }
|
---|
| 71 | if (engine_list_head == NULL) {
|
---|
| 72 | /* We are adding to an empty list. */
|
---|
| 73 | if (engine_list_tail) {
|
---|
| 74 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
|
---|
| 75 | return 0;
|
---|
| 76 | }
|
---|
| 77 | engine_list_head = e;
|
---|
| 78 | e->prev = NULL;
|
---|
| 79 | /*
|
---|
| 80 | * The first time the list allocates, we should register the cleanup.
|
---|
| 81 | */
|
---|
| 82 | engine_cleanup_add_last(engine_list_cleanup);
|
---|
| 83 | } else {
|
---|
| 84 | /* We are adding to the tail of an existing list. */
|
---|
| 85 | if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
|
---|
| 86 | ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
|
---|
| 87 | return 0;
|
---|
| 88 | }
|
---|
| 89 | engine_list_tail->next = e;
|
---|
| 90 | e->prev = engine_list_tail;
|
---|
| 91 | }
|
---|
| 92 | /*
|
---|
| 93 | * Having the engine in the list assumes a structural reference.
|
---|
| 94 | */
|
---|
| 95 | e->struct_ref++;
|
---|
| 96 | engine_ref_debug(e, 0, 1);
|
---|
| 97 | /* However it came to be, e is the last item in the list. */
|
---|
| 98 | engine_list_tail = e;
|
---|
| 99 | e->next = NULL;
|
---|
| 100 | return 1;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | static int engine_list_remove(ENGINE *e)
|
---|
| 104 | {
|
---|
| 105 | ENGINE *iterator;
|
---|
| 106 |
|
---|
| 107 | if (e == NULL) {
|
---|
| 108 | ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 109 | return 0;
|
---|
| 110 | }
|
---|
| 111 | /* We need to check that e is in our linked list! */
|
---|
| 112 | iterator = engine_list_head;
|
---|
| 113 | while (iterator && (iterator != e))
|
---|
| 114 | iterator = iterator->next;
|
---|
| 115 | if (iterator == NULL) {
|
---|
| 116 | ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
|
---|
| 117 | ENGINE_R_ENGINE_IS_NOT_IN_LIST);
|
---|
| 118 | return 0;
|
---|
| 119 | }
|
---|
| 120 | /* un-link e from the chain. */
|
---|
| 121 | if (e->next)
|
---|
| 122 | e->next->prev = e->prev;
|
---|
| 123 | if (e->prev)
|
---|
| 124 | e->prev->next = e->next;
|
---|
| 125 | /* Correct our head/tail if necessary. */
|
---|
| 126 | if (engine_list_head == e)
|
---|
| 127 | engine_list_head = e->next;
|
---|
| 128 | if (engine_list_tail == e)
|
---|
| 129 | engine_list_tail = e->prev;
|
---|
| 130 | engine_free_util(e, 0);
|
---|
| 131 | return 1;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /* Get the first/last "ENGINE" type available. */
|
---|
| 135 | ENGINE *ENGINE_get_first(void)
|
---|
| 136 | {
|
---|
| 137 | ENGINE *ret;
|
---|
| 138 |
|
---|
| 139 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
|
---|
| 140 | ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE);
|
---|
| 141 | return NULL;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 145 | ret = engine_list_head;
|
---|
| 146 | if (ret) {
|
---|
| 147 | ret->struct_ref++;
|
---|
| 148 | engine_ref_debug(ret, 0, 1);
|
---|
| 149 | }
|
---|
| 150 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 151 | return ret;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | ENGINE *ENGINE_get_last(void)
|
---|
| 155 | {
|
---|
| 156 | ENGINE *ret;
|
---|
| 157 |
|
---|
| 158 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
|
---|
| 159 | ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE);
|
---|
| 160 | return NULL;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 164 | ret = engine_list_tail;
|
---|
| 165 | if (ret) {
|
---|
| 166 | ret->struct_ref++;
|
---|
| 167 | engine_ref_debug(ret, 0, 1);
|
---|
| 168 | }
|
---|
| 169 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 170 | return ret;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
|
---|
| 174 | ENGINE *ENGINE_get_next(ENGINE *e)
|
---|
| 175 | {
|
---|
| 176 | ENGINE *ret = NULL;
|
---|
| 177 | if (e == NULL) {
|
---|
| 178 | ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 179 | return 0;
|
---|
| 180 | }
|
---|
| 181 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 182 | ret = e->next;
|
---|
| 183 | if (ret) {
|
---|
| 184 | /* Return a valid structural reference to the next ENGINE */
|
---|
| 185 | ret->struct_ref++;
|
---|
| 186 | engine_ref_debug(ret, 0, 1);
|
---|
| 187 | }
|
---|
| 188 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 189 | /* Release the structural reference to the previous ENGINE */
|
---|
| 190 | ENGINE_free(e);
|
---|
| 191 | return ret;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | ENGINE *ENGINE_get_prev(ENGINE *e)
|
---|
| 195 | {
|
---|
| 196 | ENGINE *ret = NULL;
|
---|
| 197 | if (e == NULL) {
|
---|
| 198 | ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 199 | return 0;
|
---|
| 200 | }
|
---|
| 201 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 202 | ret = e->prev;
|
---|
| 203 | if (ret) {
|
---|
| 204 | /* Return a valid structural reference to the next ENGINE */
|
---|
| 205 | ret->struct_ref++;
|
---|
| 206 | engine_ref_debug(ret, 0, 1);
|
---|
| 207 | }
|
---|
| 208 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 209 | /* Release the structural reference to the previous ENGINE */
|
---|
| 210 | ENGINE_free(e);
|
---|
| 211 | return ret;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /* Add another "ENGINE" type into the list. */
|
---|
| 215 | int ENGINE_add(ENGINE *e)
|
---|
| 216 | {
|
---|
| 217 | int to_return = 1;
|
---|
| 218 | if (e == NULL) {
|
---|
| 219 | ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 220 | return 0;
|
---|
| 221 | }
|
---|
| 222 | if ((e->id == NULL) || (e->name == NULL)) {
|
---|
| 223 | ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
|
---|
| 224 | return 0;
|
---|
| 225 | }
|
---|
| 226 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 227 | if (!engine_list_add(e)) {
|
---|
| 228 | ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
|
---|
| 229 | to_return = 0;
|
---|
| 230 | }
|
---|
| 231 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 232 | return to_return;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /* Remove an existing "ENGINE" type from the array. */
|
---|
| 236 | int ENGINE_remove(ENGINE *e)
|
---|
| 237 | {
|
---|
| 238 | int to_return = 1;
|
---|
| 239 | if (e == NULL) {
|
---|
| 240 | ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 241 | return 0;
|
---|
| 242 | }
|
---|
| 243 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 244 | if (!engine_list_remove(e)) {
|
---|
| 245 | ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
|
---|
| 246 | to_return = 0;
|
---|
| 247 | }
|
---|
| 248 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 249 | return to_return;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | static void engine_cpy(ENGINE *dest, const ENGINE *src)
|
---|
| 253 | {
|
---|
| 254 | dest->id = src->id;
|
---|
| 255 | dest->name = src->name;
|
---|
| 256 | #ifndef OPENSSL_NO_RSA
|
---|
| 257 | dest->rsa_meth = src->rsa_meth;
|
---|
| 258 | #endif
|
---|
| 259 | #ifndef OPENSSL_NO_DSA
|
---|
| 260 | dest->dsa_meth = src->dsa_meth;
|
---|
| 261 | #endif
|
---|
| 262 | #ifndef OPENSSL_NO_DH
|
---|
| 263 | dest->dh_meth = src->dh_meth;
|
---|
| 264 | #endif
|
---|
| 265 | #ifndef OPENSSL_NO_EC
|
---|
| 266 | dest->ec_meth = src->ec_meth;
|
---|
| 267 | #endif
|
---|
| 268 | dest->rand_meth = src->rand_meth;
|
---|
| 269 | dest->ciphers = src->ciphers;
|
---|
| 270 | dest->digests = src->digests;
|
---|
| 271 | dest->pkey_meths = src->pkey_meths;
|
---|
| 272 | dest->destroy = src->destroy;
|
---|
| 273 | dest->init = src->init;
|
---|
| 274 | dest->finish = src->finish;
|
---|
| 275 | dest->ctrl = src->ctrl;
|
---|
| 276 | dest->load_privkey = src->load_privkey;
|
---|
| 277 | dest->load_pubkey = src->load_pubkey;
|
---|
| 278 | dest->cmd_defns = src->cmd_defns;
|
---|
| 279 | dest->flags = src->flags;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | ENGINE *ENGINE_by_id(const char *id)
|
---|
| 283 | {
|
---|
| 284 | ENGINE *iterator;
|
---|
| 285 | char *load_dir = NULL;
|
---|
| 286 | if (id == NULL) {
|
---|
| 287 | ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 288 | return NULL;
|
---|
| 289 | }
|
---|
| 290 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
|
---|
| 291 | ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE);
|
---|
| 292 | return NULL;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | CRYPTO_THREAD_write_lock(global_engine_lock);
|
---|
| 296 | iterator = engine_list_head;
|
---|
| 297 | while (iterator && (strcmp(id, iterator->id) != 0))
|
---|
| 298 | iterator = iterator->next;
|
---|
| 299 | if (iterator != NULL) {
|
---|
| 300 | /*
|
---|
| 301 | * We need to return a structural reference. If this is an ENGINE
|
---|
| 302 | * type that returns copies, make a duplicate - otherwise increment
|
---|
| 303 | * the existing ENGINE's reference count.
|
---|
| 304 | */
|
---|
| 305 | if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
|
---|
| 306 | ENGINE *cp = ENGINE_new();
|
---|
| 307 | if (cp == NULL)
|
---|
| 308 | iterator = NULL;
|
---|
| 309 | else {
|
---|
| 310 | engine_cpy(cp, iterator);
|
---|
| 311 | iterator = cp;
|
---|
| 312 | }
|
---|
| 313 | } else {
|
---|
| 314 | iterator->struct_ref++;
|
---|
| 315 | engine_ref_debug(iterator, 0, 1);
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | CRYPTO_THREAD_unlock(global_engine_lock);
|
---|
| 319 | if (iterator != NULL)
|
---|
| 320 | return iterator;
|
---|
| 321 | /*
|
---|
| 322 | * Prevent infinite recursion if we're looking for the dynamic engine.
|
---|
| 323 | */
|
---|
| 324 | if (strcmp(id, "dynamic")) {
|
---|
| 325 | if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
|
---|
| 326 | load_dir = ""ENGINESDIR"";
|
---|
| 327 | iterator = ENGINE_by_id("dynamic");
|
---|
| 328 | if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
|
---|
| 329 | !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
|
---|
| 330 | !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
|
---|
| 331 | load_dir, 0) ||
|
---|
| 332 | !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
|
---|
| 333 | !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
|
---|
| 334 | goto notfound;
|
---|
| 335 | return iterator;
|
---|
| 336 | }
|
---|
| 337 | notfound:
|
---|
| 338 | ENGINE_free(iterator);
|
---|
| 339 | ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
|
---|
| 340 | ERR_add_error_data(2, "id=", id);
|
---|
| 341 | return NULL;
|
---|
| 342 | /* EEK! Experimental code ends */
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | int ENGINE_up_ref(ENGINE *e)
|
---|
| 346 | {
|
---|
| 347 | int i;
|
---|
| 348 | if (e == NULL) {
|
---|
| 349 | ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 350 | return 0;
|
---|
| 351 | }
|
---|
| 352 | CRYPTO_atomic_add(&e->struct_ref, 1, &i, global_engine_lock);
|
---|
| 353 | return 1;
|
---|
| 354 | }
|
---|