1 /* $OpenBSD: eng_list.c,v 1.20 2015/06/19 06:32:43 bcook Exp $ */ 2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 3 * project 2000. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 /* ==================================================================== 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60 * ECDH support in OpenSSL originally developed by 61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 62 */ 63 64 #include <string.h> 65 #include <unistd.h> 66 67 #include <openssl/opensslconf.h> 68 69 #include <openssl/err.h> 70 71 #include "cryptlib.h" 72 #include "eng_int.h" 73 74 /* The linked-list of pointers to engine types. engine_list_head 75 * incorporates an implicit structural reference but engine_list_tail 76 * does not - the latter is a computational niceity and only points 77 * to something that is already pointed to by its predecessor in the 78 * list (or engine_list_head itself). In the same way, the use of the 79 * "prev" pointer in each ENGINE is to save excessive list iteration, 80 * it doesn't correspond to an extra structural reference. Hence, 81 * engine_list_head, and each non-null "next" pointer account for 82 * the list itself assuming exactly 1 structural reference on each 83 * list member. */ 84 static ENGINE *engine_list_head = NULL; 85 static ENGINE *engine_list_tail = NULL; 86 87 /* This cleanup function is only needed internally. If it should be called, we 88 * register it with the "ENGINE_cleanup()" stack to be called during cleanup. */ 89 90 static void 91 engine_list_cleanup(void) 92 { 93 ENGINE *iterator = engine_list_head; 94 95 while (iterator != NULL && ENGINE_remove(iterator)) 96 iterator = engine_list_head; 97 } 98 99 /* These static functions starting with a lower case "engine_" always 100 * take place when CRYPTO_LOCK_ENGINE has been locked up. */ 101 static int 102 engine_list_add(ENGINE *e) 103 { 104 int conflict = 0; 105 ENGINE *iterator = NULL; 106 107 if (e == NULL) { 108 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, 109 ERR_R_PASSED_NULL_PARAMETER); 110 return 0; 111 } 112 iterator = engine_list_head; 113 while (iterator && !conflict) { 114 conflict = (strcmp(iterator->id, e->id) == 0); 115 iterator = iterator->next; 116 } 117 if (conflict) { 118 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, 119 ENGINE_R_CONFLICTING_ENGINE_ID); 120 return 0; 121 } 122 if (engine_list_head == NULL) { 123 /* We are adding to an empty list. */ 124 if (engine_list_tail) { 125 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, 126 ENGINE_R_INTERNAL_LIST_ERROR); 127 return 0; 128 } 129 engine_list_head = e; 130 e->prev = NULL; 131 /* The first time the list allocates, we should register the 132 * cleanup. */ 133 engine_cleanup_add_last(engine_list_cleanup); 134 } else { 135 /* We are adding to the tail of an existing list. */ 136 if ((engine_list_tail == NULL) || 137 (engine_list_tail->next != NULL)) { 138 ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, 139 ENGINE_R_INTERNAL_LIST_ERROR); 140 return 0; 141 } 142 engine_list_tail->next = e; 143 e->prev = engine_list_tail; 144 } 145 /* Having the engine in the list assumes a structural 146 * reference. */ 147 e->struct_ref++; 148 engine_ref_debug(e, 0, 1) 149 /* However it came to be, e is the last item in the list. */ 150 engine_list_tail = e; 151 e->next = NULL; 152 return 1; 153 } 154 155 static int 156 engine_list_remove(ENGINE *e) 157 { 158 ENGINE *iterator; 159 160 if (e == NULL) { 161 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, 162 ERR_R_PASSED_NULL_PARAMETER); 163 return 0; 164 } 165 /* We need to check that e is in our linked list! */ 166 iterator = engine_list_head; 167 while (iterator && (iterator != e)) 168 iterator = iterator->next; 169 if (iterator == NULL) { 170 ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, 171 ENGINE_R_ENGINE_IS_NOT_IN_LIST); 172 return 0; 173 } 174 /* un-link e from the chain. */ 175 if (e->next) 176 e->next->prev = e->prev; 177 if (e->prev) 178 e->prev->next = e->next; 179 /* Correct our head/tail if necessary. */ 180 if (engine_list_head == e) 181 engine_list_head = e->next; 182 if (engine_list_tail == e) 183 engine_list_tail = e->prev; 184 engine_free_util(e, 0); 185 return 1; 186 } 187 188 /* Get the first/last "ENGINE" type available. */ 189 ENGINE * 190 ENGINE_get_first(void) 191 { 192 ENGINE *ret; 193 194 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 195 ret = engine_list_head; 196 if (ret) { 197 ret->struct_ref++; 198 engine_ref_debug(ret, 0, 1) 199 } 200 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 201 return ret; 202 } 203 204 ENGINE * 205 ENGINE_get_last(void) 206 { 207 ENGINE *ret; 208 209 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 210 ret = engine_list_tail; 211 if (ret) { 212 ret->struct_ref++; 213 engine_ref_debug(ret, 0, 1) 214 } 215 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 216 return ret; 217 } 218 219 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */ 220 ENGINE * 221 ENGINE_get_next(ENGINE *e) 222 { 223 ENGINE *ret = NULL; 224 225 if (e == NULL) { 226 ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, 227 ERR_R_PASSED_NULL_PARAMETER); 228 return 0; 229 } 230 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 231 ret = e->next; 232 if (ret) { 233 /* Return a valid structural refernce to the next ENGINE */ 234 ret->struct_ref++; 235 engine_ref_debug(ret, 0, 1) 236 } 237 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 238 /* Release the structural reference to the previous ENGINE */ 239 ENGINE_free(e); 240 return ret; 241 } 242 243 ENGINE * 244 ENGINE_get_prev(ENGINE *e) 245 { 246 ENGINE *ret = NULL; 247 248 if (e == NULL) { 249 ENGINEerr(ENGINE_F_ENGINE_GET_PREV, 250 ERR_R_PASSED_NULL_PARAMETER); 251 return 0; 252 } 253 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 254 ret = e->prev; 255 if (ret) { 256 /* Return a valid structural reference to the next ENGINE */ 257 ret->struct_ref++; 258 engine_ref_debug(ret, 0, 1) 259 } 260 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 261 /* Release the structural reference to the previous ENGINE */ 262 ENGINE_free(e); 263 return ret; 264 } 265 266 /* Add another "ENGINE" type into the list. */ 267 int 268 ENGINE_add(ENGINE *e) 269 { 270 int to_return = 1; 271 272 if (e == NULL) { 273 ENGINEerr(ENGINE_F_ENGINE_ADD, 274 ERR_R_PASSED_NULL_PARAMETER); 275 return 0; 276 } 277 if ((e->id == NULL) || (e->name == NULL)) { 278 ENGINEerr(ENGINE_F_ENGINE_ADD, 279 ENGINE_R_ID_OR_NAME_MISSING); 280 } 281 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 282 if (!engine_list_add(e)) { 283 ENGINEerr(ENGINE_F_ENGINE_ADD, 284 ENGINE_R_INTERNAL_LIST_ERROR); 285 to_return = 0; 286 } 287 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 288 return to_return; 289 } 290 291 /* Remove an existing "ENGINE" type from the array. */ 292 int 293 ENGINE_remove(ENGINE *e) 294 { 295 int to_return = 1; 296 297 if (e == NULL) { 298 ENGINEerr(ENGINE_F_ENGINE_REMOVE, 299 ERR_R_PASSED_NULL_PARAMETER); 300 return 0; 301 } 302 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 303 if (!engine_list_remove(e)) { 304 ENGINEerr(ENGINE_F_ENGINE_REMOVE, 305 ENGINE_R_INTERNAL_LIST_ERROR); 306 to_return = 0; 307 } 308 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 309 return to_return; 310 } 311 312 static void 313 engine_cpy(ENGINE *dest, const ENGINE *src) 314 { 315 dest->id = src->id; 316 dest->name = src->name; 317 #ifndef OPENSSL_NO_RSA 318 dest->rsa_meth = src->rsa_meth; 319 #endif 320 #ifndef OPENSSL_NO_DSA 321 dest->dsa_meth = src->dsa_meth; 322 #endif 323 #ifndef OPENSSL_NO_DH 324 dest->dh_meth = src->dh_meth; 325 #endif 326 #ifndef OPENSSL_NO_ECDH 327 dest->ecdh_meth = src->ecdh_meth; 328 #endif 329 #ifndef OPENSSL_NO_ECDSA 330 dest->ecdsa_meth = src->ecdsa_meth; 331 #endif 332 dest->rand_meth = src->rand_meth; 333 dest->store_meth = src->store_meth; 334 dest->ciphers = src->ciphers; 335 dest->digests = src->digests; 336 dest->pkey_meths = src->pkey_meths; 337 dest->destroy = src->destroy; 338 dest->init = src->init; 339 dest->finish = src->finish; 340 dest->ctrl = src->ctrl; 341 dest->load_privkey = src->load_privkey; 342 dest->load_pubkey = src->load_pubkey; 343 dest->cmd_defns = src->cmd_defns; 344 dest->flags = src->flags; 345 } 346 347 ENGINE * 348 ENGINE_by_id(const char *id) 349 { 350 ENGINE *iterator; 351 352 if (id == NULL) { 353 ENGINEerr(ENGINE_F_ENGINE_BY_ID, 354 ERR_R_PASSED_NULL_PARAMETER); 355 return NULL; 356 } 357 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 358 iterator = engine_list_head; 359 while (iterator && (strcmp(id, iterator->id) != 0)) 360 iterator = iterator->next; 361 if (iterator) { 362 /* We need to return a structural reference. If this is an 363 * ENGINE type that returns copies, make a duplicate - otherwise 364 * increment the existing ENGINE's reference count. */ 365 if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) { 366 ENGINE *cp = ENGINE_new(); 367 if (!cp) 368 iterator = NULL; 369 else { 370 engine_cpy(cp, iterator); 371 iterator = cp; 372 } 373 } else { 374 iterator->struct_ref++; 375 engine_ref_debug(iterator, 0, 1) 376 } 377 } 378 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 379 380 if (iterator == NULL) { 381 ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE); 382 ERR_asprintf_error_data("id=%s", id); 383 } 384 return iterator; 385 } 386 387 int 388 ENGINE_up_ref(ENGINE *e) 389 { 390 if (e == NULL) { 391 ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER); 392 return 0; 393 } 394 CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE); 395 return 1; 396 } 397