diff options
| author | cvs2svn <admin@example.com> | 2015-03-08 16:48:48 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2015-03-08 16:48:48 +0000 |
| commit | da1a9ad3a4a867ba6569c05e6fca66d7f296c553 (patch) | |
| tree | 44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/cryptlib.c | |
| parent | 973703db67a8e73d70e63afa8f2cde19da09144d (diff) | |
| download | openbsd-OPENBSD_5_7_BASE.tar.gz openbsd-OPENBSD_5_7_BASE.tar.bz2 openbsd-OPENBSD_5_7_BASE.zip | |
This commit was manufactured by cvs2git to create tag 'OPENBSD_5_7_BASE'.OPENBSD_5_7_BASE
Diffstat (limited to 'src/lib/libcrypto/cryptlib.c')
| -rw-r--r-- | src/lib/libcrypto/cryptlib.c | 714 |
1 files changed, 0 insertions, 714 deletions
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c deleted file mode 100644 index dc92ac89fe..0000000000 --- a/src/lib/libcrypto/cryptlib.c +++ /dev/null | |||
| @@ -1,714 +0,0 @@ | |||
| 1 | /* $OpenBSD: cryptlib.c,v 1.34 2015/01/22 03:56:27 bcook Exp $ */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@openssl.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 56 | * All rights reserved. | ||
| 57 | * | ||
| 58 | * This package is an SSL implementation written | ||
| 59 | * by Eric Young (eay@cryptsoft.com). | ||
| 60 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 61 | * | ||
| 62 | * This library is free for commercial and non-commercial use as long as | ||
| 63 | * the following conditions are aheared to. The following conditions | ||
| 64 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 65 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 66 | * included with this distribution is covered by the same copyright terms | ||
| 67 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 68 | * | ||
| 69 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 70 | * the code are not to be removed. | ||
| 71 | * If this package is used in a product, Eric Young should be given attribution | ||
| 72 | * as the author of the parts of the library used. | ||
| 73 | * This can be in the form of a textual message at program startup or | ||
| 74 | * in documentation (online or textual) provided with the package. | ||
| 75 | * | ||
| 76 | * Redistribution and use in source and binary forms, with or without | ||
| 77 | * modification, are permitted provided that the following conditions | ||
| 78 | * are met: | ||
| 79 | * 1. Redistributions of source code must retain the copyright | ||
| 80 | * notice, this list of conditions and the following disclaimer. | ||
| 81 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 82 | * notice, this list of conditions and the following disclaimer in the | ||
| 83 | * documentation and/or other materials provided with the distribution. | ||
| 84 | * 3. All advertising materials mentioning features or use of this software | ||
| 85 | * must display the following acknowledgement: | ||
| 86 | * "This product includes cryptographic software written by | ||
| 87 | * Eric Young (eay@cryptsoft.com)" | ||
| 88 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 89 | * being used are not cryptographic related :-). | ||
| 90 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 91 | * the apps directory (application code) you must include an acknowledgement: | ||
| 92 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 93 | * | ||
| 94 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 95 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 96 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 97 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 98 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 99 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 100 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 101 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 102 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 103 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 104 | * SUCH DAMAGE. | ||
| 105 | * | ||
| 106 | * The licence and distribution terms for any publically available version or | ||
| 107 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 108 | * copied and put under another distribution licence | ||
| 109 | * [including the GNU Public Licence.] | ||
| 110 | */ | ||
| 111 | /* ==================================================================== | ||
| 112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
| 113 | * ECDH support in OpenSSL originally developed by | ||
| 114 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. | ||
| 115 | */ | ||
| 116 | |||
| 117 | #include <stdarg.h> | ||
| 118 | #include <string.h> | ||
| 119 | #include <unistd.h> | ||
| 120 | |||
| 121 | #include <openssl/opensslconf.h> | ||
| 122 | |||
| 123 | #include <openssl/crypto.h> | ||
| 124 | #include <openssl/buffer.h> | ||
| 125 | #include <openssl/err.h> | ||
| 126 | #include <openssl/safestack.h> | ||
| 127 | #include <openssl/sha.h> | ||
| 128 | |||
| 129 | DECLARE_STACK_OF(CRYPTO_dynlock) | ||
| 130 | |||
| 131 | /* real #defines in crypto.h, keep these upto date */ | ||
| 132 | static const char* const lock_names[CRYPTO_NUM_LOCKS] = { | ||
| 133 | "<<ERROR>>", | ||
| 134 | "err", | ||
| 135 | "ex_data", | ||
| 136 | "x509", | ||
| 137 | "x509_info", | ||
| 138 | "x509_pkey", | ||
| 139 | "x509_crl", | ||
| 140 | "x509_req", | ||
| 141 | "dsa", | ||
| 142 | "rsa", | ||
| 143 | "evp_pkey", | ||
| 144 | "x509_store", | ||
| 145 | "ssl_ctx", | ||
| 146 | "ssl_cert", | ||
| 147 | "ssl_session", | ||
| 148 | "ssl_sess_cert", | ||
| 149 | "ssl", | ||
| 150 | "ssl_method", | ||
| 151 | "rand", | ||
| 152 | "rand2", | ||
| 153 | "debug_malloc", | ||
| 154 | "BIO", | ||
| 155 | "gethostbyname", | ||
| 156 | "getservbyname", | ||
| 157 | "readdir", | ||
| 158 | "RSA_blinding", | ||
| 159 | "dh", | ||
| 160 | "debug_malloc2", | ||
| 161 | "dso", | ||
| 162 | "dynlock", | ||
| 163 | "engine", | ||
| 164 | "ui", | ||
| 165 | "ecdsa", | ||
| 166 | "ec", | ||
| 167 | "ecdh", | ||
| 168 | "bn", | ||
| 169 | "ec_pre_comp", | ||
| 170 | "store", | ||
| 171 | "comp", | ||
| 172 | "fips", | ||
| 173 | "fips2", | ||
| 174 | #if CRYPTO_NUM_LOCKS != 41 | ||
| 175 | # error "Inconsistency between crypto.h and cryptlib.c" | ||
| 176 | #endif | ||
| 177 | }; | ||
| 178 | |||
| 179 | /* This is for applications to allocate new type names in the non-dynamic | ||
| 180 | array of lock names. These are numbered with positive numbers. */ | ||
| 181 | static STACK_OF(OPENSSL_STRING) *app_locks = NULL; | ||
| 182 | |||
| 183 | /* For applications that want a more dynamic way of handling threads, the | ||
| 184 | following stack is used. These are externally numbered with negative | ||
| 185 | numbers. */ | ||
| 186 | static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL; | ||
| 187 | |||
| 188 | static void (*locking_callback)(int mode, int type, | ||
| 189 | const char *file, int line) = 0; | ||
| 190 | static int (*add_lock_callback)(int *pointer, int amount, | ||
| 191 | int type, const char *file, int line) = 0; | ||
| 192 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 193 | static unsigned long (*id_callback)(void) = 0; | ||
| 194 | #endif | ||
| 195 | static void (*threadid_callback)(CRYPTO_THREADID *) = 0; | ||
| 196 | static struct CRYPTO_dynlock_value *(*dynlock_create_callback)( | ||
| 197 | const char *file, int line) = 0; | ||
| 198 | static void (*dynlock_lock_callback)(int mode, | ||
| 199 | struct CRYPTO_dynlock_value *l, const char *file, int line) = 0; | ||
| 200 | static void (*dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l, | ||
| 201 | const char *file, int line) = 0; | ||
| 202 | |||
| 203 | int | ||
| 204 | CRYPTO_get_new_lockid(char *name) | ||
| 205 | { | ||
| 206 | char *str; | ||
| 207 | int i; | ||
| 208 | |||
| 209 | if ((app_locks == NULL) && | ||
| 210 | ((app_locks = sk_OPENSSL_STRING_new_null()) == NULL)) { | ||
| 211 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE); | ||
| 212 | return (0); | ||
| 213 | } | ||
| 214 | if (name == NULL || (str = strdup(name)) == NULL) { | ||
| 215 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE); | ||
| 216 | return (0); | ||
| 217 | } | ||
| 218 | i = sk_OPENSSL_STRING_push(app_locks, str); | ||
| 219 | if (!i) | ||
| 220 | free(str); | ||
| 221 | else | ||
| 222 | i += CRYPTO_NUM_LOCKS; /* gap of one :-) */ | ||
| 223 | return (i); | ||
| 224 | } | ||
| 225 | |||
| 226 | int | ||
| 227 | CRYPTO_num_locks(void) | ||
| 228 | { | ||
| 229 | return CRYPTO_NUM_LOCKS; | ||
| 230 | } | ||
| 231 | |||
| 232 | int | ||
| 233 | CRYPTO_get_new_dynlockid(void) | ||
| 234 | { | ||
| 235 | int i = 0; | ||
| 236 | CRYPTO_dynlock *pointer = NULL; | ||
| 237 | |||
| 238 | if (dynlock_create_callback == NULL) { | ||
| 239 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, | ||
| 240 | CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK); | ||
| 241 | return (0); | ||
| 242 | } | ||
| 243 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
| 244 | if ((dyn_locks == NULL) && | ||
| 245 | ((dyn_locks = sk_CRYPTO_dynlock_new_null()) == NULL)) { | ||
| 246 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
| 247 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, | ||
| 248 | ERR_R_MALLOC_FAILURE); | ||
| 249 | return (0); | ||
| 250 | } | ||
| 251 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
| 252 | |||
| 253 | pointer = malloc(sizeof(CRYPTO_dynlock)); | ||
| 254 | if (pointer == NULL) { | ||
| 255 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, | ||
| 256 | ERR_R_MALLOC_FAILURE); | ||
| 257 | return (0); | ||
| 258 | } | ||
| 259 | pointer->references = 1; | ||
| 260 | pointer->data = dynlock_create_callback(__FILE__, __LINE__); | ||
| 261 | if (pointer->data == NULL) { | ||
| 262 | free(pointer); | ||
| 263 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, | ||
| 264 | ERR_R_MALLOC_FAILURE); | ||
| 265 | return (0); | ||
| 266 | } | ||
| 267 | |||
| 268 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
| 269 | /* First, try to find an existing empty slot */ | ||
| 270 | i = sk_CRYPTO_dynlock_find(dyn_locks, NULL); | ||
| 271 | /* If there was none, push, thereby creating a new one */ | ||
| 272 | if (i == -1) | ||
| 273 | /* Since sk_push() returns the number of items on the | ||
| 274 | stack, not the location of the pushed item, we need | ||
| 275 | to transform the returned number into a position, | ||
| 276 | by decreasing it. */ | ||
| 277 | i = sk_CRYPTO_dynlock_push(dyn_locks, pointer) - 1; | ||
| 278 | else | ||
| 279 | /* If we found a place with a NULL pointer, put our pointer | ||
| 280 | in it. */ | ||
| 281 | (void)sk_CRYPTO_dynlock_set(dyn_locks, i, pointer); | ||
| 282 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
| 283 | |||
| 284 | if (i == -1) { | ||
| 285 | dynlock_destroy_callback(pointer->data, __FILE__, __LINE__); | ||
| 286 | free(pointer); | ||
| 287 | } else | ||
| 288 | i += 1; /* to avoid 0 */ | ||
| 289 | return -i; | ||
| 290 | } | ||
| 291 | |||
| 292 | void | ||
| 293 | CRYPTO_destroy_dynlockid(int i) | ||
| 294 | { | ||
| 295 | CRYPTO_dynlock *pointer = NULL; | ||
| 296 | |||
| 297 | if (i) | ||
| 298 | i = -i - 1; | ||
| 299 | if (dynlock_destroy_callback == NULL) | ||
| 300 | return; | ||
| 301 | |||
| 302 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
| 303 | |||
| 304 | if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks)) { | ||
| 305 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
| 306 | return; | ||
| 307 | } | ||
| 308 | pointer = sk_CRYPTO_dynlock_value(dyn_locks, i); | ||
| 309 | if (pointer != NULL) { | ||
| 310 | --pointer->references; | ||
| 311 | if (pointer->references <= 0) { | ||
| 312 | (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL); | ||
| 313 | } else | ||
| 314 | pointer = NULL; | ||
| 315 | } | ||
| 316 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
| 317 | |||
| 318 | if (pointer) { | ||
| 319 | dynlock_destroy_callback(pointer->data, __FILE__, __LINE__); | ||
| 320 | free(pointer); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | struct CRYPTO_dynlock_value * | ||
| 325 | CRYPTO_get_dynlock_value(int i) | ||
| 326 | { | ||
| 327 | CRYPTO_dynlock *pointer = NULL; | ||
| 328 | |||
| 329 | if (i) | ||
| 330 | i = -i - 1; | ||
| 331 | |||
| 332 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
| 333 | |||
| 334 | if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks)) | ||
| 335 | pointer = sk_CRYPTO_dynlock_value(dyn_locks, i); | ||
| 336 | if (pointer) | ||
| 337 | pointer->references++; | ||
| 338 | |||
| 339 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
| 340 | |||
| 341 | if (pointer) | ||
| 342 | return pointer->data; | ||
| 343 | return NULL; | ||
| 344 | } | ||
| 345 | |||
| 346 | struct CRYPTO_dynlock_value * | ||
| 347 | (*CRYPTO_get_dynlock_create_callback(void))(const char *file, int line) | ||
| 348 | { | ||
| 349 | return (dynlock_create_callback); | ||
| 350 | } | ||
| 351 | |||
| 352 | void | ||
| 353 | (*CRYPTO_get_dynlock_lock_callback(void))(int mode, | ||
| 354 | struct CRYPTO_dynlock_value *l, const char *file, int line) | ||
| 355 | { | ||
| 356 | return (dynlock_lock_callback); | ||
| 357 | } | ||
| 358 | |||
| 359 | void | ||
| 360 | (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l, | ||
| 361 | const char *file, int line) | ||
| 362 | { | ||
| 363 | return (dynlock_destroy_callback); | ||
| 364 | } | ||
| 365 | |||
| 366 | void | ||
| 367 | CRYPTO_set_dynlock_create_callback( | ||
| 368 | struct CRYPTO_dynlock_value *(*func)(const char *file, int line)) | ||
| 369 | { | ||
| 370 | dynlock_create_callback = func; | ||
| 371 | } | ||
| 372 | |||
| 373 | void | ||
| 374 | CRYPTO_set_dynlock_lock_callback(void (*func)(int mode, | ||
| 375 | struct CRYPTO_dynlock_value *l, const char *file, int line)) | ||
| 376 | { | ||
| 377 | dynlock_lock_callback = func; | ||
| 378 | } | ||
| 379 | |||
| 380 | void | ||
| 381 | CRYPTO_set_dynlock_destroy_callback( | ||
| 382 | void (*func)(struct CRYPTO_dynlock_value *l, const char *file, int line)) | ||
| 383 | { | ||
| 384 | dynlock_destroy_callback = func; | ||
| 385 | } | ||
| 386 | |||
| 387 | void | ||
| 388 | (*CRYPTO_get_locking_callback(void))(int mode, int type, const char *file, | ||
| 389 | int line) | ||
| 390 | { | ||
| 391 | return (locking_callback); | ||
| 392 | } | ||
| 393 | |||
| 394 | int | ||
| 395 | (*CRYPTO_get_add_lock_callback(void))(int *num, int mount, int type, | ||
| 396 | const char *file, int line) | ||
| 397 | { | ||
| 398 | return (add_lock_callback); | ||
| 399 | } | ||
| 400 | |||
| 401 | void | ||
| 402 | CRYPTO_set_locking_callback(void (*func)(int mode, int type, | ||
| 403 | const char *file, int line)) | ||
| 404 | { | ||
| 405 | /* Calling this here ensures initialisation before any threads | ||
| 406 | * are started. | ||
| 407 | */ | ||
| 408 | OPENSSL_init(); | ||
| 409 | locking_callback = func; | ||
| 410 | } | ||
| 411 | |||
| 412 | void | ||
| 413 | CRYPTO_set_add_lock_callback(int (*func)(int *num, int mount, int type, | ||
| 414 | const char *file, int line)) | ||
| 415 | { | ||
| 416 | add_lock_callback = func; | ||
| 417 | } | ||
| 418 | |||
| 419 | /* the memset() here and in set_pointer() seem overkill, but for the sake of | ||
| 420 | * CRYPTO_THREADID_cmp() this avoids any platform silliness that might cause two | ||
| 421 | * "equal" THREADID structs to not be memcmp()-identical. */ | ||
| 422 | void | ||
| 423 | CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) | ||
| 424 | { | ||
| 425 | memset(id, 0, sizeof(*id)); | ||
| 426 | id->val = val; | ||
| 427 | } | ||
| 428 | |||
| 429 | void | ||
| 430 | CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) | ||
| 431 | { | ||
| 432 | memset(id, 0, sizeof(*id)); | ||
| 433 | id->ptr = ptr; | ||
| 434 | #if LONG_MAX >= INTPTR_MAX | ||
| 435 | /*s u 'ptr' can be embedded in 'val' without loss of uniqueness */ | ||
| 436 | id->val = (unsigned long)id->ptr; | ||
| 437 | #else | ||
| 438 | { | ||
| 439 | SHA256_CTX ctx; | ||
| 440 | uint8_t results[SHA256_DIGEST_LENGTH]; | ||
| 441 | |||
| 442 | SHA256_Init(&ctx); | ||
| 443 | SHA256_Update(&ctx, (char *)(&id->ptr), sizeof(id->ptr)); | ||
| 444 | SHA256_Final(results, &ctx); | ||
| 445 | memcpy(&id->val, results, sizeof(id->val)); | ||
| 446 | } | ||
| 447 | #endif | ||
| 448 | } | ||
| 449 | |||
| 450 | int | ||
| 451 | CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *)) | ||
| 452 | { | ||
| 453 | if (threadid_callback) | ||
| 454 | return 0; | ||
| 455 | threadid_callback = func; | ||
| 456 | return 1; | ||
| 457 | } | ||
| 458 | |||
| 459 | void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *) | ||
| 460 | { | ||
| 461 | return threadid_callback; | ||
| 462 | } | ||
| 463 | |||
| 464 | void | ||
| 465 | CRYPTO_THREADID_current(CRYPTO_THREADID *id) | ||
| 466 | { | ||
| 467 | if (threadid_callback) { | ||
| 468 | threadid_callback(id); | ||
| 469 | return; | ||
| 470 | } | ||
| 471 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 472 | /* If the deprecated callback was set, fall back to that */ | ||
| 473 | if (id_callback) { | ||
| 474 | CRYPTO_THREADID_set_numeric(id, id_callback()); | ||
| 475 | return; | ||
| 476 | } | ||
| 477 | #endif | ||
| 478 | /* Else pick a backup */ | ||
| 479 | /* For everything else, default to using the address of 'errno' */ | ||
| 480 | CRYPTO_THREADID_set_pointer(id, (void*)&errno); | ||
| 481 | } | ||
| 482 | |||
| 483 | int | ||
| 484 | CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b) | ||
| 485 | { | ||
| 486 | return memcmp(a, b, sizeof(*a)); | ||
| 487 | } | ||
| 488 | |||
| 489 | void | ||
| 490 | CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src) | ||
| 491 | { | ||
| 492 | memcpy(dest, src, sizeof(*src)); | ||
| 493 | } | ||
| 494 | |||
| 495 | unsigned long | ||
| 496 | CRYPTO_THREADID_hash(const CRYPTO_THREADID *id) | ||
| 497 | { | ||
| 498 | return id->val; | ||
| 499 | } | ||
| 500 | |||
| 501 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 502 | unsigned long (*CRYPTO_get_id_callback(void))(void) | ||
| 503 | { | ||
| 504 | return (id_callback); | ||
| 505 | } | ||
| 506 | |||
| 507 | void | ||
| 508 | CRYPTO_set_id_callback(unsigned long (*func)(void)) | ||
| 509 | { | ||
| 510 | id_callback = func; | ||
| 511 | } | ||
| 512 | |||
| 513 | unsigned long | ||
| 514 | CRYPTO_thread_id(void) | ||
| 515 | { | ||
| 516 | unsigned long ret = 0; | ||
| 517 | |||
| 518 | if (id_callback == NULL) { | ||
| 519 | ret = (unsigned long)getpid(); | ||
| 520 | } else | ||
| 521 | ret = id_callback(); | ||
| 522 | return (ret); | ||
| 523 | } | ||
| 524 | #endif | ||
| 525 | |||
| 526 | void | ||
| 527 | CRYPTO_lock(int mode, int type, const char *file, int line) | ||
| 528 | { | ||
| 529 | #ifdef LOCK_DEBUG | ||
| 530 | { | ||
| 531 | CRYPTO_THREADID id; | ||
| 532 | char *rw_text, *operation_text; | ||
| 533 | |||
| 534 | if (mode & CRYPTO_LOCK) | ||
| 535 | operation_text = "lock "; | ||
| 536 | else if (mode & CRYPTO_UNLOCK) | ||
| 537 | operation_text = "unlock"; | ||
| 538 | else | ||
| 539 | operation_text = "ERROR "; | ||
| 540 | |||
| 541 | if (mode & CRYPTO_READ) | ||
| 542 | rw_text = "r"; | ||
| 543 | else if (mode & CRYPTO_WRITE) | ||
| 544 | rw_text = "w"; | ||
| 545 | else | ||
| 546 | rw_text = "ERROR"; | ||
| 547 | |||
| 548 | CRYPTO_THREADID_current(&id); | ||
| 549 | fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\n", | ||
| 550 | CRYPTO_THREADID_hash(&id), rw_text, operation_text, | ||
| 551 | CRYPTO_get_lock_name(type), file, line); | ||
| 552 | } | ||
| 553 | #endif | ||
| 554 | if (type < 0) { | ||
| 555 | if (dynlock_lock_callback != NULL) { | ||
| 556 | struct CRYPTO_dynlock_value *pointer = | ||
| 557 | CRYPTO_get_dynlock_value(type); | ||
| 558 | |||
| 559 | OPENSSL_assert(pointer != NULL); | ||
| 560 | |||
| 561 | dynlock_lock_callback(mode, pointer, file, line); | ||
| 562 | |||
| 563 | CRYPTO_destroy_dynlockid(type); | ||
| 564 | } | ||
| 565 | } else if (locking_callback != NULL) | ||
| 566 | locking_callback(mode, type, file, line); | ||
| 567 | } | ||
| 568 | |||
| 569 | int | ||
| 570 | CRYPTO_add_lock(int *pointer, int amount, int type, const char *file, | ||
| 571 | int line) | ||
| 572 | { | ||
| 573 | int ret = 0; | ||
| 574 | |||
| 575 | if (add_lock_callback != NULL) { | ||
| 576 | #ifdef LOCK_DEBUG | ||
| 577 | int before= *pointer; | ||
| 578 | #endif | ||
| 579 | |||
| 580 | ret = add_lock_callback(pointer, amount, type, file, line); | ||
| 581 | #ifdef LOCK_DEBUG | ||
| 582 | { | ||
| 583 | CRYPTO_THREADID id; | ||
| 584 | CRYPTO_THREADID_current(&id); | ||
| 585 | fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n", | ||
| 586 | CRYPTO_THREADID_hash(&id), before, amount, ret, | ||
| 587 | CRYPTO_get_lock_name(type), | ||
| 588 | file, line); | ||
| 589 | } | ||
| 590 | #endif | ||
| 591 | } else { | ||
| 592 | CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE, type, file, line); | ||
| 593 | |||
| 594 | ret= *pointer + amount; | ||
| 595 | #ifdef LOCK_DEBUG | ||
| 596 | { | ||
| 597 | CRYPTO_THREADID id; | ||
| 598 | CRYPTO_THREADID_current(&id); | ||
| 599 | fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n", | ||
| 600 | CRYPTO_THREADID_hash(&id), *pointer, amount, ret, | ||
| 601 | CRYPTO_get_lock_name(type), file, line); | ||
| 602 | } | ||
| 603 | #endif | ||
| 604 | *pointer = ret; | ||
| 605 | CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE, type, file, line); | ||
| 606 | } | ||
| 607 | return (ret); | ||
| 608 | } | ||
| 609 | |||
| 610 | const char * | ||
| 611 | CRYPTO_get_lock_name(int type) | ||
| 612 | { | ||
| 613 | if (type < 0) | ||
| 614 | return("dynamic"); | ||
| 615 | else if (type < CRYPTO_NUM_LOCKS) | ||
| 616 | return (lock_names[type]); | ||
| 617 | else if (type - CRYPTO_NUM_LOCKS > sk_OPENSSL_STRING_num(app_locks)) | ||
| 618 | return("ERROR"); | ||
| 619 | else | ||
| 620 | return (sk_OPENSSL_STRING_value(app_locks, | ||
| 621 | type - CRYPTO_NUM_LOCKS)); | ||
| 622 | } | ||
| 623 | |||
| 624 | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ | ||
| 625 | defined(__INTEL__) || \ | ||
| 626 | defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) | ||
| 627 | |||
| 628 | unsigned int OPENSSL_ia32cap_P[2]; | ||
| 629 | unsigned long * | ||
| 630 | OPENSSL_ia32cap_loc(void) | ||
| 631 | { | ||
| 632 | if (sizeof(long) == 4) | ||
| 633 | /* | ||
| 634 | * If 32-bit application pulls address of OPENSSL_ia32cap_P[0] | ||
| 635 | * clear second element to maintain the illusion that vector | ||
| 636 | * is 32-bit. | ||
| 637 | */ | ||
| 638 | OPENSSL_ia32cap_P[1] = 0; | ||
| 639 | return (unsigned long *)OPENSSL_ia32cap_P; | ||
| 640 | } | ||
| 641 | |||
| 642 | #if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY) | ||
| 643 | #define OPENSSL_CPUID_SETUP | ||
| 644 | typedef unsigned long long IA32CAP; | ||
| 645 | void | ||
| 646 | OPENSSL_cpuid_setup(void) | ||
| 647 | { | ||
| 648 | static int trigger = 0; | ||
| 649 | IA32CAP OPENSSL_ia32_cpuid(void); | ||
| 650 | IA32CAP vec; | ||
| 651 | |||
| 652 | if (trigger) | ||
| 653 | return; | ||
| 654 | trigger = 1; | ||
| 655 | |||
| 656 | vec = OPENSSL_ia32_cpuid(); | ||
| 657 | |||
| 658 | /* | ||
| 659 | * |(1<<10) sets a reserved bit to signal that variable | ||
| 660 | * was initialized already... This is to avoid interference | ||
| 661 | * with cpuid snippets in ELF .init segment. | ||
| 662 | */ | ||
| 663 | OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10); | ||
| 664 | OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32); | ||
| 665 | } | ||
| 666 | #endif | ||
| 667 | |||
| 668 | #else | ||
| 669 | unsigned long * | ||
| 670 | OPENSSL_ia32cap_loc(void) | ||
| 671 | { | ||
| 672 | return NULL; | ||
| 673 | } | ||
| 674 | #endif | ||
| 675 | |||
| 676 | #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) | ||
| 677 | void | ||
| 678 | OPENSSL_cpuid_setup(void) | ||
| 679 | { | ||
| 680 | } | ||
| 681 | #endif | ||
| 682 | |||
| 683 | static void | ||
| 684 | OPENSSL_showfatal(const char *fmta, ...) | ||
| 685 | { | ||
| 686 | va_list ap; | ||
| 687 | |||
| 688 | va_start(ap, fmta); | ||
| 689 | vfprintf(stderr, fmta, ap); | ||
| 690 | va_end(ap); | ||
| 691 | } | ||
| 692 | |||
| 693 | void | ||
| 694 | OpenSSLDie(const char *file, int line, const char *assertion) | ||
| 695 | { | ||
| 696 | OPENSSL_showfatal( | ||
| 697 | "%s(%d): OpenSSL internal error, assertion failed: %s\n", | ||
| 698 | file, line, assertion); | ||
| 699 | abort(); | ||
| 700 | } | ||
| 701 | |||
| 702 | int | ||
| 703 | CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) | ||
| 704 | { | ||
| 705 | size_t i; | ||
| 706 | const unsigned char *a = in_a; | ||
| 707 | const unsigned char *b = in_b; | ||
| 708 | unsigned char x = 0; | ||
| 709 | |||
| 710 | for (i = 0; i < len; i++) | ||
| 711 | x |= a[i] ^ b[i]; | ||
| 712 | |||
| 713 | return x; | ||
| 714 | } | ||
