diff options
Diffstat (limited to 'src/lib/libcrypto/cryptlib.c')
-rw-r--r-- | src/lib/libcrypto/cryptlib.c | 968 |
1 files changed, 968 insertions, 0 deletions
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c new file mode 100644 index 0000000000..89667d175a --- /dev/null +++ b/src/lib/libcrypto/cryptlib.c | |||
@@ -0,0 +1,968 @@ | |||
1 | /* crypto/cryptlib.c */ | ||
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 "cryptlib.h" | ||
118 | #include <openssl/safestack.h> | ||
119 | |||
120 | #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) | ||
121 | static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */ | ||
122 | #endif | ||
123 | |||
124 | DECLARE_STACK_OF(CRYPTO_dynlock) | ||
125 | |||
126 | /* real #defines in crypto.h, keep these upto date */ | ||
127 | static const char* const lock_names[CRYPTO_NUM_LOCKS] = { | ||
128 | "<<ERROR>>", | ||
129 | "err", | ||
130 | "ex_data", | ||
131 | "x509", | ||
132 | "x509_info", | ||
133 | "x509_pkey", | ||
134 | "x509_crl", | ||
135 | "x509_req", | ||
136 | "dsa", | ||
137 | "rsa", | ||
138 | "evp_pkey", | ||
139 | "x509_store", | ||
140 | "ssl_ctx", | ||
141 | "ssl_cert", | ||
142 | "ssl_session", | ||
143 | "ssl_sess_cert", | ||
144 | "ssl", | ||
145 | "ssl_method", | ||
146 | "rand", | ||
147 | "rand2", | ||
148 | "debug_malloc", | ||
149 | "BIO", | ||
150 | "gethostbyname", | ||
151 | "getservbyname", | ||
152 | "readdir", | ||
153 | "RSA_blinding", | ||
154 | "dh", | ||
155 | "debug_malloc2", | ||
156 | "dso", | ||
157 | "dynlock", | ||
158 | "engine", | ||
159 | "ui", | ||
160 | "ecdsa", | ||
161 | "ec", | ||
162 | "ecdh", | ||
163 | "bn", | ||
164 | "ec_pre_comp", | ||
165 | "store", | ||
166 | "comp", | ||
167 | "fips", | ||
168 | "fips2", | ||
169 | #if CRYPTO_NUM_LOCKS != 41 | ||
170 | # error "Inconsistency between crypto.h and cryptlib.c" | ||
171 | #endif | ||
172 | }; | ||
173 | |||
174 | /* This is for applications to allocate new type names in the non-dynamic | ||
175 | array of lock names. These are numbered with positive numbers. */ | ||
176 | static STACK_OF(OPENSSL_STRING) *app_locks = NULL; | ||
177 | |||
178 | /* For applications that want a more dynamic way of handling threads, the | ||
179 | following stack is used. These are externally numbered with negative | ||
180 | numbers. */ | ||
181 | static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL; | ||
182 | |||
183 | static void (*locking_callback)(int mode, int type, | ||
184 | const char *file, int line) = 0; | ||
185 | static int (*add_lock_callback)(int *pointer, int amount, | ||
186 | int type, const char *file, int line) = 0; | ||
187 | #ifndef OPENSSL_NO_DEPRECATED | ||
188 | static unsigned long (*id_callback)(void) = 0; | ||
189 | #endif | ||
190 | static void (*threadid_callback)(CRYPTO_THREADID *) = 0; | ||
191 | static struct CRYPTO_dynlock_value *(*dynlock_create_callback)( | ||
192 | const char *file, int line) = 0; | ||
193 | static void (*dynlock_lock_callback)(int mode, | ||
194 | struct CRYPTO_dynlock_value *l, const char *file, int line) = 0; | ||
195 | static void (*dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l, | ||
196 | const char *file, int line) = 0; | ||
197 | |||
198 | int | ||
199 | CRYPTO_get_new_lockid(char *name) | ||
200 | { | ||
201 | char *str; | ||
202 | int i; | ||
203 | |||
204 | #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) | ||
205 | /* A hack to make Visual C++ 5.0 work correctly when linking as | ||
206 | * a DLL using /MT. Without this, the application cannot use | ||
207 | * any floating point printf's. | ||
208 | * It also seems to be needed for Visual C 1.5 (win16) */ | ||
209 | SSLeay_MSVC5_hack = (double)name[0]*(double)name[1]; | ||
210 | #endif | ||
211 | |||
212 | if ((app_locks == NULL) && ((app_locks = sk_OPENSSL_STRING_new_null()) == NULL)) { | ||
213 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE); | ||
214 | return (0); | ||
215 | } | ||
216 | if ((str = BUF_strdup(name)) == NULL) { | ||
217 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE); | ||
218 | return (0); | ||
219 | } | ||
220 | i = sk_OPENSSL_STRING_push(app_locks, str); | ||
221 | if (!i) | ||
222 | OPENSSL_free(str); | ||
223 | else | ||
224 | i += CRYPTO_NUM_LOCKS; /* gap of one :-) */ | ||
225 | return (i); | ||
226 | } | ||
227 | |||
228 | int | ||
229 | CRYPTO_num_locks(void) | ||
230 | { | ||
231 | return CRYPTO_NUM_LOCKS; | ||
232 | } | ||
233 | |||
234 | int | ||
235 | CRYPTO_get_new_dynlockid(void) | ||
236 | { | ||
237 | int i = 0; | ||
238 | CRYPTO_dynlock *pointer = NULL; | ||
239 | |||
240 | if (dynlock_create_callback == NULL) { | ||
241 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK); | ||
242 | return (0); | ||
243 | } | ||
244 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
245 | if ((dyn_locks == NULL) | ||
246 | && ((dyn_locks = sk_CRYPTO_dynlock_new_null()) == NULL)) { | ||
247 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
248 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE); | ||
249 | return (0); | ||
250 | } | ||
251 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
252 | |||
253 | pointer = (CRYPTO_dynlock *)OPENSSL_malloc(sizeof(CRYPTO_dynlock)); | ||
254 | if (pointer == NULL) { | ||
255 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE); | ||
256 | return (0); | ||
257 | } | ||
258 | pointer->references = 1; | ||
259 | pointer->data = dynlock_create_callback(__FILE__, __LINE__); | ||
260 | if (pointer->data == NULL) { | ||
261 | OPENSSL_free(pointer); | ||
262 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE); | ||
263 | return (0); | ||
264 | } | ||
265 | |||
266 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
267 | /* First, try to find an existing empty slot */ | ||
268 | i = sk_CRYPTO_dynlock_find(dyn_locks, NULL); | ||
269 | /* If there was none, push, thereby creating a new one */ | ||
270 | if (i == -1) | ||
271 | /* Since sk_push() returns the number of items on the | ||
272 | stack, not the location of the pushed item, we need | ||
273 | to transform the returned number into a position, | ||
274 | by decreasing it. */ | ||
275 | i = sk_CRYPTO_dynlock_push(dyn_locks, pointer) - 1; | ||
276 | else | ||
277 | /* If we found a place with a NULL pointer, put our pointer | ||
278 | in it. */ | ||
279 | (void)sk_CRYPTO_dynlock_set(dyn_locks, i, pointer); | ||
280 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
281 | |||
282 | if (i == -1) { | ||
283 | dynlock_destroy_callback(pointer->data, __FILE__, __LINE__); | ||
284 | OPENSSL_free(pointer); | ||
285 | } else | ||
286 | i += 1; /* to avoid 0 */ | ||
287 | return - i; | ||
288 | } | ||
289 | |||
290 | void | ||
291 | CRYPTO_destroy_dynlockid(int i) | ||
292 | { | ||
293 | CRYPTO_dynlock *pointer = NULL; | ||
294 | if (i) | ||
295 | i = -i - 1; | ||
296 | if (dynlock_destroy_callback == NULL) | ||
297 | return; | ||
298 | |||
299 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
300 | |||
301 | if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks)) { | ||
302 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
303 | return; | ||
304 | } | ||
305 | pointer = sk_CRYPTO_dynlock_value(dyn_locks, i); | ||
306 | if (pointer != NULL) { | ||
307 | --pointer->references; | ||
308 | #ifdef REF_CHECK | ||
309 | if (pointer->references < 0) { | ||
310 | fprintf(stderr, "CRYPTO_destroy_dynlockid, bad reference count\n"); | ||
311 | abort(); | ||
312 | } else | ||
313 | #endif | ||
314 | if (pointer->references <= 0) { | ||
315 | (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL); | ||
316 | } else | ||
317 | pointer = NULL; | ||
318 | } | ||
319 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
320 | |||
321 | if (pointer) { | ||
322 | dynlock_destroy_callback(pointer->data, __FILE__, __LINE__); | ||
323 | OPENSSL_free(pointer); | ||
324 | } | ||
325 | } | ||
326 | |||
327 | struct CRYPTO_dynlock_value | ||
328 | *CRYPTO_get_dynlock_value(int i) | ||
329 | { | ||
330 | CRYPTO_dynlock *pointer = NULL; | ||
331 | if (i) | ||
332 | i = -i - 1; | ||
333 | |||
334 | CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); | ||
335 | |||
336 | if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks)) | ||
337 | pointer = sk_CRYPTO_dynlock_value(dyn_locks, i); | ||
338 | if (pointer) | ||
339 | pointer->references++; | ||
340 | |||
341 | CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); | ||
342 | |||
343 | if (pointer) | ||
344 | return pointer->data; | ||
345 | return NULL; | ||
346 | } | ||
347 | |||
348 | struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))( | ||
349 | const char *file, int line) | ||
350 | { | ||
351 | return (dynlock_create_callback); | ||
352 | } | ||
353 | |||
354 | void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, | ||
355 | struct CRYPTO_dynlock_value *l, const char *file, int line) | ||
356 | { | ||
357 | return (dynlock_lock_callback); | ||
358 | } | ||
359 | |||
360 | void (*CRYPTO_get_dynlock_destroy_callback(void))( | ||
361 | struct CRYPTO_dynlock_value *l, 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 (*CRYPTO_get_locking_callback(void))(int mode, int type, | ||
388 | const char *file, int line) | ||
389 | { | ||
390 | return (locking_callback); | ||
391 | } | ||
392 | |||
393 | int (*CRYPTO_get_add_lock_callback(void))(int *num, int mount, int type, | ||
394 | const char *file, int line) | ||
395 | { | ||
396 | return (add_lock_callback); | ||
397 | } | ||
398 | |||
399 | void | ||
400 | CRYPTO_set_locking_callback(void (*func)(int mode, int type, | ||
401 | const char *file, int line)) | ||
402 | { | ||
403 | /* Calling this here ensures initialisation before any threads | ||
404 | * are started. | ||
405 | */ | ||
406 | OPENSSL_init(); | ||
407 | locking_callback = func; | ||
408 | } | ||
409 | |||
410 | void | ||
411 | CRYPTO_set_add_lock_callback(int (*func)(int *num, int mount, int type, | ||
412 | const char *file, int line)) | ||
413 | { | ||
414 | add_lock_callback = func; | ||
415 | } | ||
416 | |||
417 | /* the memset() here and in set_pointer() seem overkill, but for the sake of | ||
418 | * CRYPTO_THREADID_cmp() this avoids any platform silliness that might cause two | ||
419 | * "equal" THREADID structs to not be memcmp()-identical. */ | ||
420 | void | ||
421 | CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) | ||
422 | { | ||
423 | memset(id, 0, sizeof(*id)); | ||
424 | id->val = val; | ||
425 | } | ||
426 | |||
427 | static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; | ||
428 | void | ||
429 | CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) | ||
430 | { | ||
431 | unsigned char *dest = (void *)&id->val; | ||
432 | unsigned int accum = 0; | ||
433 | unsigned char dnum = sizeof(id->val); | ||
434 | |||
435 | memset(id, 0, sizeof(*id)); | ||
436 | id->ptr = ptr; | ||
437 | if (sizeof(id->val) >= sizeof(id->ptr)) { | ||
438 | /* 'ptr' can be embedded in 'val' without loss of uniqueness */ | ||
439 | id->val = (unsigned long)id->ptr; | ||
440 | return; | ||
441 | } | ||
442 | /* hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a | ||
443 | * linear function over the bytes in 'ptr', the co-efficients of which | ||
444 | * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - | ||
445 | * the starting prime for the sequence varies for each byte of 'val' | ||
446 | * (unique polynomials unless pointers are >64-bit). For added spice, | ||
447 | * the totals accumulate rather than restarting from zero, and the index | ||
448 | * of the 'val' byte is added each time (position dependence). If I was | ||
449 | * a black-belt, I'd scan big-endian pointers in reverse to give | ||
450 | * low-order bits more play, but this isn't crypto and I'd prefer nobody | ||
451 | * mistake it as such. Plus I'm lazy. */ | ||
452 | while (dnum--) { | ||
453 | const unsigned char *src = (void *)&id->ptr; | ||
454 | unsigned char snum = sizeof(id->ptr); | ||
455 | while (snum--) | ||
456 | accum += *(src++) * hash_coeffs[(snum + dnum) & 7]; | ||
457 | accum += dnum; | ||
458 | *(dest++) = accum & 255; | ||
459 | } | ||
460 | } | ||
461 | |||
462 | int | ||
463 | CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *)) | ||
464 | { | ||
465 | if (threadid_callback) | ||
466 | return 0; | ||
467 | threadid_callback = func; | ||
468 | return 1; | ||
469 | } | ||
470 | |||
471 | void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *) | ||
472 | { | ||
473 | return threadid_callback; | ||
474 | } | ||
475 | |||
476 | void | ||
477 | CRYPTO_THREADID_current(CRYPTO_THREADID *id) | ||
478 | { | ||
479 | if (threadid_callback) { | ||
480 | threadid_callback(id); | ||
481 | return; | ||
482 | } | ||
483 | #ifndef OPENSSL_NO_DEPRECATED | ||
484 | /* If the deprecated callback was set, fall back to that */ | ||
485 | if (id_callback) { | ||
486 | CRYPTO_THREADID_set_numeric(id, id_callback()); | ||
487 | return; | ||
488 | } | ||
489 | #endif | ||
490 | /* Else pick a backup */ | ||
491 | #ifdef OPENSSL_SYS_WIN16 | ||
492 | CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentTask()); | ||
493 | #elif defined(OPENSSL_SYS_WIN32) | ||
494 | CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentThreadId()); | ||
495 | #elif defined(OPENSSL_SYS_BEOS) | ||
496 | CRYPTO_THREADID_set_numeric(id, (unsigned long)find_thread(NULL)); | ||
497 | #else | ||
498 | /* For everything else, default to using the address of 'errno' */ | ||
499 | CRYPTO_THREADID_set_pointer(id, (void*)&errno); | ||
500 | #endif | ||
501 | } | ||
502 | |||
503 | int | ||
504 | CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b) | ||
505 | { | ||
506 | return memcmp(a, b, sizeof(*a)); | ||
507 | } | ||
508 | |||
509 | void | ||
510 | CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src) | ||
511 | { | ||
512 | memcpy(dest, src, sizeof(*src)); | ||
513 | } | ||
514 | |||
515 | unsigned long | ||
516 | CRYPTO_THREADID_hash(const CRYPTO_THREADID *id) | ||
517 | { | ||
518 | return id->val; | ||
519 | } | ||
520 | |||
521 | #ifndef OPENSSL_NO_DEPRECATED | ||
522 | unsigned long (*CRYPTO_get_id_callback(void))(void) | ||
523 | { | ||
524 | return (id_callback); | ||
525 | } | ||
526 | |||
527 | void | ||
528 | CRYPTO_set_id_callback(unsigned long (*func)(void)) | ||
529 | { | ||
530 | id_callback = func; | ||
531 | } | ||
532 | |||
533 | unsigned long | ||
534 | CRYPTO_thread_id(void) | ||
535 | { | ||
536 | unsigned long ret = 0; | ||
537 | |||
538 | if (id_callback == NULL) { | ||
539 | #ifdef OPENSSL_SYS_WIN16 | ||
540 | ret = (unsigned long)GetCurrentTask(); | ||
541 | #elif defined(OPENSSL_SYS_WIN32) | ||
542 | ret = (unsigned long)GetCurrentThreadId(); | ||
543 | #elif defined(GETPID_IS_MEANINGLESS) | ||
544 | ret = 1L; | ||
545 | #elif defined(OPENSSL_SYS_BEOS) | ||
546 | ret = (unsigned long)find_thread(NULL); | ||
547 | #else | ||
548 | ret = (unsigned long)getpid(); | ||
549 | #endif | ||
550 | } else | ||
551 | ret = id_callback(); | ||
552 | return (ret); | ||
553 | } | ||
554 | #endif | ||
555 | |||
556 | void | ||
557 | CRYPTO_lock(int mode, int type, const char *file, int line) | ||
558 | { | ||
559 | #ifdef LOCK_DEBUG | ||
560 | { | ||
561 | CRYPTO_THREADID id; | ||
562 | char *rw_text, *operation_text; | ||
563 | |||
564 | if (mode & CRYPTO_LOCK) | ||
565 | operation_text="lock "; | ||
566 | else if (mode & CRYPTO_UNLOCK) | ||
567 | operation_text="unlock"; | ||
568 | else | ||
569 | operation_text="ERROR "; | ||
570 | |||
571 | if (mode & CRYPTO_READ) | ||
572 | rw_text="r"; | ||
573 | else if (mode & CRYPTO_WRITE) | ||
574 | rw_text="w"; | ||
575 | else | ||
576 | rw_text="ERROR"; | ||
577 | |||
578 | CRYPTO_THREADID_current(&id); | ||
579 | fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\n", | ||
580 | CRYPTO_THREADID_hash(&id), rw_text, operation_text, | ||
581 | CRYPTO_get_lock_name(type), file, line); | ||
582 | } | ||
583 | #endif | ||
584 | if (type < 0) { | ||
585 | if (dynlock_lock_callback != NULL) { | ||
586 | struct CRYPTO_dynlock_value *pointer = | ||
587 | CRYPTO_get_dynlock_value(type); | ||
588 | |||
589 | OPENSSL_assert(pointer != NULL); | ||
590 | |||
591 | dynlock_lock_callback(mode, pointer, file, line); | ||
592 | |||
593 | CRYPTO_destroy_dynlockid(type); | ||
594 | } | ||
595 | } else if (locking_callback != NULL) | ||
596 | locking_callback(mode, type, file, line); | ||
597 | } | ||
598 | |||
599 | int | ||
600 | CRYPTO_add_lock(int *pointer, int amount, int type, const char *file, | ||
601 | int line) | ||
602 | { | ||
603 | int ret = 0; | ||
604 | |||
605 | if (add_lock_callback != NULL) { | ||
606 | #ifdef LOCK_DEBUG | ||
607 | int before= *pointer; | ||
608 | #endif | ||
609 | |||
610 | ret = add_lock_callback(pointer, amount, type, file, line); | ||
611 | #ifdef LOCK_DEBUG | ||
612 | { | ||
613 | CRYPTO_THREADID id; | ||
614 | CRYPTO_THREADID_current(&id); | ||
615 | fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n", | ||
616 | CRYPTO_THREADID_hash(&id), before, amount, ret, | ||
617 | CRYPTO_get_lock_name(type), | ||
618 | file, line); | ||
619 | } | ||
620 | #endif | ||
621 | } else { | ||
622 | CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE, type, file, line); | ||
623 | |||
624 | ret= *pointer + amount; | ||
625 | #ifdef LOCK_DEBUG | ||
626 | { | ||
627 | CRYPTO_THREADID id; | ||
628 | CRYPTO_THREADID_current(&id); | ||
629 | fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n", | ||
630 | CRYPTO_THREADID_hash(&id), | ||
631 | *pointer, amount, ret, | ||
632 | CRYPTO_get_lock_name(type), | ||
633 | file, line); | ||
634 | } | ||
635 | #endif | ||
636 | *pointer = ret; | ||
637 | CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE, type, file, line); | ||
638 | } | ||
639 | return (ret); | ||
640 | } | ||
641 | |||
642 | const char | ||
643 | *CRYPTO_get_lock_name(int type) | ||
644 | { | ||
645 | if (type < 0) | ||
646 | return("dynamic"); | ||
647 | else if (type < CRYPTO_NUM_LOCKS) | ||
648 | return (lock_names[type]); | ||
649 | else if (type - CRYPTO_NUM_LOCKS > sk_OPENSSL_STRING_num(app_locks)) | ||
650 | return("ERROR"); | ||
651 | else | ||
652 | return (sk_OPENSSL_STRING_value(app_locks, type - CRYPTO_NUM_LOCKS)); | ||
653 | } | ||
654 | |||
655 | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ | ||
656 | defined(__INTEL__) || \ | ||
657 | defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) | ||
658 | |||
659 | unsigned int OPENSSL_ia32cap_P[2]; | ||
660 | unsigned long | ||
661 | *OPENSSL_ia32cap_loc(void) | ||
662 | { | ||
663 | if (sizeof(long) == 4) | ||
664 | /* | ||
665 | * If 32-bit application pulls address of OPENSSL_ia32cap_P[0] | ||
666 | * clear second element to maintain the illusion that vector | ||
667 | * is 32-bit. | ||
668 | */ | ||
669 | OPENSSL_ia32cap_P[1] = 0; | ||
670 | return (unsigned long *)OPENSSL_ia32cap_P; | ||
671 | } | ||
672 | |||
673 | #if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY) | ||
674 | #define OPENSSL_CPUID_SETUP | ||
675 | #if defined(_WIN32) | ||
676 | typedef unsigned __int64 IA32CAP; | ||
677 | #else | ||
678 | typedef unsigned long long IA32CAP; | ||
679 | #endif | ||
680 | void | ||
681 | OPENSSL_cpuid_setup(void) | ||
682 | { | ||
683 | static int trigger = 0; | ||
684 | IA32CAP OPENSSL_ia32_cpuid(void); | ||
685 | IA32CAP vec; | ||
686 | char *env; | ||
687 | |||
688 | if (trigger) | ||
689 | return; | ||
690 | |||
691 | trigger = 1; | ||
692 | if ((env = getenv("OPENSSL_ia32cap"))) { | ||
693 | int off = (env[0]=='~') ? 1 : 0; | ||
694 | #if defined(_WIN32) | ||
695 | if (!sscanf(env+off, "%I64i", &vec)) vec = strtoul(env+off, NULL, 0); | ||
696 | #else | ||
697 | if (!sscanf(env+off, "%lli",(long long *)&vec)) vec = strtoul(env+off, NULL, 0); | ||
698 | #endif | ||
699 | if (off) | ||
700 | vec = OPENSSL_ia32_cpuid()&~vec; | ||
701 | } else | ||
702 | vec = OPENSSL_ia32_cpuid(); | ||
703 | |||
704 | /* | ||
705 | * |(1<<10) sets a reserved bit to signal that variable | ||
706 | * was initialized already... This is to avoid interference | ||
707 | * with cpuid snippets in ELF .init segment. | ||
708 | */ | ||
709 | OPENSSL_ia32cap_P[0] = (unsigned int)vec|(1 << 10); | ||
710 | OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32); | ||
711 | } | ||
712 | #endif | ||
713 | |||
714 | #else | ||
715 | unsigned long *OPENSSL_ia32cap_loc(void) { return NULL; | ||
716 | } | ||
717 | #endif | ||
718 | int OPENSSL_NONPIC_relocated = 0; | ||
719 | #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) | ||
720 | void | ||
721 | OPENSSL_cpuid_setup(void) {} | ||
722 | #endif | ||
723 | |||
724 | #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL) | ||
725 | #ifdef __CYGWIN__ | ||
726 | /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */ | ||
727 | #include <windows.h> | ||
728 | /* this has side-effect of _WIN32 getting defined, which otherwise | ||
729 | * is mutually exclusive with __CYGWIN__... */ | ||
730 | #endif | ||
731 | |||
732 | /* All we really need to do is remove the 'error' state when a thread | ||
733 | * detaches */ | ||
734 | |||
735 | BOOL WINAPI | ||
736 | DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) | ||
737 | { | ||
738 | switch (fdwReason) { | ||
739 | case DLL_PROCESS_ATTACH: | ||
740 | OPENSSL_cpuid_setup(); | ||
741 | #if defined(_WIN32_WINNT) | ||
742 | { | ||
743 | IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL; | ||
744 | IMAGE_NT_HEADERS *nt_headers; | ||
745 | |||
746 | if (dos_header->e_magic == IMAGE_DOS_SIGNATURE) { | ||
747 | nt_headers = (IMAGE_NT_HEADERS *)((char *)dos_header | ||
748 | + dos_header->e_lfanew); | ||
749 | if (nt_headers->Signature == IMAGE_NT_SIGNATURE && | ||
750 | hinstDLL != (HINSTANCE)(nt_headers->OptionalHeader.ImageBase)) | ||
751 | OPENSSL_NONPIC_relocated = 1; | ||
752 | } | ||
753 | } | ||
754 | #endif | ||
755 | break; | ||
756 | case DLL_THREAD_ATTACH: | ||
757 | break; | ||
758 | case DLL_THREAD_DETACH: | ||
759 | break; | ||
760 | case DLL_PROCESS_DETACH: | ||
761 | break; | ||
762 | } | ||
763 | return (TRUE); | ||
764 | } | ||
765 | #endif | ||
766 | |||
767 | #if defined(_WIN32) && !defined(__CYGWIN__) | ||
768 | #include <tchar.h> | ||
769 | #include <signal.h> | ||
770 | #ifdef __WATCOMC__ | ||
771 | #if defined(_UNICODE) || defined(__UNICODE__) | ||
772 | #define _vsntprintf _vsnwprintf | ||
773 | #else | ||
774 | #define _vsntprintf _vsnprintf | ||
775 | #endif | ||
776 | #endif | ||
777 | #ifdef _MSC_VER | ||
778 | #define alloca _alloca | ||
779 | #endif | ||
780 | |||
781 | #if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 | ||
782 | int | ||
783 | OPENSSL_isservice(void) | ||
784 | { HWINSTA h; | ||
785 | DWORD len; | ||
786 | WCHAR *name; | ||
787 | static union { void *p; | ||
788 | int (*f)(void); | ||
789 | } _OPENSSL_isservice = { NULL }; | ||
790 | |||
791 | if (_OPENSSL_isservice.p == NULL) { | ||
792 | HANDLE h = GetModuleHandle(NULL); | ||
793 | if (h != NULL) | ||
794 | _OPENSSL_isservice.p = GetProcAddress(h, "_OPENSSL_isservice"); | ||
795 | if (_OPENSSL_isservice.p == NULL) | ||
796 | _OPENSSL_isservice.p = (void *) - 1; | ||
797 | } | ||
798 | |||
799 | if (_OPENSSL_isservice.p != (void *) - 1) | ||
800 | return (*_OPENSSL_isservice.f)(); | ||
801 | |||
802 | (void)GetDesktopWindow(); /* return value is ignored */ | ||
803 | |||
804 | h = GetProcessWindowStation(); | ||
805 | if (h == NULL) | ||
806 | return -1; | ||
807 | |||
808 | if (GetUserObjectInformationW (h, UOI_NAME, NULL, 0, &len) || | ||
809 | GetLastError() != ERROR_INSUFFICIENT_BUFFER) | ||
810 | return -1; | ||
811 | |||
812 | if (len > 512) | ||
813 | return -1; | ||
814 | /* paranoia */ | ||
815 | len++, len&=~1; | ||
816 | /* paranoia */ | ||
817 | name = (WCHAR *)alloca(len + sizeof(WCHAR)); | ||
818 | if (!GetUserObjectInformationW (h, UOI_NAME, name, len, &len)) | ||
819 | return -1; | ||
820 | |||
821 | len++, len&=~1; | ||
822 | /* paranoia */ | ||
823 | name[len/sizeof(WCHAR)]=L'\0'; /* paranoia */ | ||
824 | #if 1 | ||
825 | /* This doesn't cover "interactive" services [working with real | ||
826 | * WinSta0's] nor programs started non-interactively by Task | ||
827 | * Scheduler [those are working with SAWinSta]. */ | ||
828 | if (wcsstr(name, L"Service-0x")) return 1; | ||
829 | #else | ||
830 | /* This covers all non-interactive programs such as services. */ | ||
831 | if (!wcsstr(name, L"WinSta0")) return 1; | ||
832 | #endif | ||
833 | else return 0; | ||
834 | } | ||
835 | #else | ||
836 | int OPENSSL_isservice(void) { return 0; | ||
837 | } | ||
838 | #endif | ||
839 | |||
840 | void OPENSSL_showfatal (const char *fmta,...) | ||
841 | { va_list ap; | ||
842 | TCHAR buf[256]; | ||
843 | const TCHAR *fmt; | ||
844 | #ifdef STD_ERROR_HANDLE /* what a dirty trick! */ | ||
845 | HANDLE h; | ||
846 | |||
847 | if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL && | ||
848 | GetFileType(h) != FILE_TYPE_UNKNOWN) | ||
849 | { /* must be console application */ | ||
850 | va_start (ap, fmta); | ||
851 | vfprintf (stderr, fmta, ap); | ||
852 | va_end (ap); | ||
853 | return; | ||
854 | } | ||
855 | #endif | ||
856 | |||
857 | if (sizeof(TCHAR) == sizeof(char)) | ||
858 | fmt = (const TCHAR *)fmta; | ||
859 | else do | ||
860 | { int keepgoing; | ||
861 | size_t len_0 = strlen(fmta) + 1, i; | ||
862 | WCHAR *fmtw; | ||
863 | |||
864 | fmtw = (WCHAR *)alloca(len_0*sizeof(WCHAR)); | ||
865 | if (fmtw == NULL) { | ||
866 | fmt = (const TCHAR *)L"no stack?"; | ||
867 | break; | ||
868 | } | ||
869 | |||
870 | #ifndef OPENSSL_NO_MULTIBYTE | ||
871 | if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0)) | ||
872 | #endif | ||
873 | for (i = 0;i < len_0;i++) fmtw[i] = (WCHAR)fmta[i]; | ||
874 | |||
875 | for (i = 0; i < len_0; i++) | ||
876 | { if (fmtw[i]==L'%') do | ||
877 | { keepgoing = 0; | ||
878 | switch (fmtw[i + 1]) | ||
879 | { case L'0': case L'1': case L'2': case L'3': case L'4': | ||
880 | case L'5': case L'6': case L'7': case L'8': case L'9': | ||
881 | case L'.': case L'*': | ||
882 | case L'-': | ||
883 | i++; | ||
884 | keepgoing = 1; | ||
885 | break; | ||
886 | case L's': | ||
887 | fmtw[i + 1] = L'S'; | ||
888 | break; | ||
889 | case L'S': | ||
890 | fmtw[i + 1] = L's'; | ||
891 | break; | ||
892 | case L'c': | ||
893 | fmtw[i + 1] = L'C'; | ||
894 | break; | ||
895 | case L'C': | ||
896 | fmtw[i + 1] = L'c'; | ||
897 | break; | ||
898 | } | ||
899 | } while (keepgoing); | ||
900 | } | ||
901 | fmt = (const TCHAR *)fmtw; | ||
902 | } while (0); | ||
903 | |||
904 | va_start (ap, fmta); | ||
905 | _vsntprintf (buf, sizeof(buf)/sizeof(TCHAR) - 1, fmt, ap); | ||
906 | buf [sizeof(buf)/sizeof(TCHAR) - 1] = _T('\0'); | ||
907 | va_end (ap); | ||
908 | |||
909 | #if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 | ||
910 | /* this -------------v--- guards NT-specific calls */ | ||
911 | if (check_winnt() && OPENSSL_isservice() > 0) | ||
912 | { HANDLE h = RegisterEventSource(0, _T("OPENSSL")); | ||
913 | const TCHAR *pmsg = buf; | ||
914 | ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 0, 0, 1, 0, &pmsg, 0); | ||
915 | DeregisterEventSource(h); | ||
916 | } else | ||
917 | #endif | ||
918 | MessageBox (NULL, buf, _T("OpenSSL: FATAL"), MB_OK|MB_ICONSTOP); | ||
919 | } | ||
920 | #else | ||
921 | void OPENSSL_showfatal(const char *fmta, ...) | ||
922 | { | ||
923 | va_list ap; | ||
924 | |||
925 | va_start (ap, fmta); | ||
926 | vfprintf (stderr, fmta, ap); | ||
927 | va_end (ap); | ||
928 | } | ||
929 | |||
930 | int OPENSSL_isservice(void) | ||
931 | { | ||
932 | return 0; | ||
933 | } | ||
934 | #endif | ||
935 | |||
936 | void | ||
937 | OpenSSLDie(const char *file, int line, const char *assertion) | ||
938 | { | ||
939 | OPENSSL_showfatal( | ||
940 | "%s(%d): OpenSSL internal error, assertion failed: %s\n", | ||
941 | file, line, assertion); | ||
942 | #if !defined(_WIN32) || defined(__CYGWIN__) | ||
943 | abort(); | ||
944 | #else | ||
945 | /* Win32 abort() customarily shows a dialog, but we just did that... */ | ||
946 | raise(SIGABRT); | ||
947 | _exit(3); | ||
948 | #endif | ||
949 | } | ||
950 | |||
951 | void *OPENSSL_stderr(void) | ||
952 | { | ||
953 | return stderr; | ||
954 | } | ||
955 | |||
956 | int | ||
957 | CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) | ||
958 | { | ||
959 | size_t i; | ||
960 | const unsigned char *a = in_a; | ||
961 | const unsigned char *b = in_b; | ||
962 | unsigned char x = 0; | ||
963 | |||
964 | for (i = 0; i < len; i++) | ||
965 | x |= a[i] ^ b[i]; | ||
966 | |||
967 | return x; | ||
968 | } | ||