Print this page
4853 illumos-gate is not lint-clean when built with openssl 1.0
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/openssl/libsunw_crypto/cryptlib.c
+++ new/usr/src/lib/openssl/libsunw_crypto/cryptlib.c
1 1 /* crypto/cryptlib.c */
2 2 /* ====================================================================
3 3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
4 4 *
5 5 * Redistribution and use in source and binary forms, with or without
6 6 * modification, are permitted provided that the following conditions
7 7 * are met:
8 8 *
9 9 * 1. Redistributions of source code must retain the above copyright
10 10 * notice, this list of conditions and the following disclaimer.
11 11 *
12 12 * 2. Redistributions in binary form must reproduce the above copyright
13 13 * notice, this list of conditions and the following disclaimer in
14 14 * the documentation and/or other materials provided with the
15 15 * distribution.
16 16 *
17 17 * 3. All advertising materials mentioning features or use of this
18 18 * software must display the following acknowledgment:
19 19 * "This product includes software developed by the OpenSSL Project
20 20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 21 *
22 22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 23 * endorse or promote products derived from this software without
24 24 * prior written permission. For written permission, please contact
25 25 * openssl-core@openssl.org.
26 26 *
27 27 * 5. Products derived from this software may not be called "OpenSSL"
28 28 * nor may "OpenSSL" appear in their names without prior written
29 29 * permission of the OpenSSL Project.
30 30 *
31 31 * 6. Redistributions of any form whatsoever must retain the following
32 32 * acknowledgment:
33 33 * "This product includes software developed by the OpenSSL Project
34 34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 35 *
36 36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 48 * ====================================================================
49 49 *
50 50 * This product includes cryptographic software written by Eric Young
51 51 * (eay@cryptsoft.com). This product includes software written by Tim
52 52 * Hudson (tjh@cryptsoft.com).
53 53 *
54 54 */
55 55 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56 56 * All rights reserved.
57 57 *
58 58 * This package is an SSL implementation written
59 59 * by Eric Young (eay@cryptsoft.com).
60 60 * The implementation was written so as to conform with Netscapes SSL.
61 61 *
62 62 * This library is free for commercial and non-commercial use as long as
63 63 * the following conditions are aheared to. The following conditions
64 64 * apply to all code found in this distribution, be it the RC4, RSA,
65 65 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
66 66 * included with this distribution is covered by the same copyright terms
67 67 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
68 68 *
69 69 * Copyright remains Eric Young's, and as such any Copyright notices in
70 70 * the code are not to be removed.
71 71 * If this package is used in a product, Eric Young should be given attribution
72 72 * as the author of the parts of the library used.
73 73 * This can be in the form of a textual message at program startup or
74 74 * in documentation (online or textual) provided with the package.
75 75 *
76 76 * Redistribution and use in source and binary forms, with or without
77 77 * modification, are permitted provided that the following conditions
78 78 * are met:
79 79 * 1. Redistributions of source code must retain the copyright
80 80 * notice, this list of conditions and the following disclaimer.
81 81 * 2. Redistributions in binary form must reproduce the above copyright
82 82 * notice, this list of conditions and the following disclaimer in the
83 83 * documentation and/or other materials provided with the distribution.
84 84 * 3. All advertising materials mentioning features or use of this software
85 85 * must display the following acknowledgement:
86 86 * "This product includes cryptographic software written by
87 87 * Eric Young (eay@cryptsoft.com)"
88 88 * The word 'cryptographic' can be left out if the rouines from the library
89 89 * being used are not cryptographic related :-).
90 90 * 4. If you include any Windows specific code (or a derivative thereof) from
91 91 * the apps directory (application code) you must include an acknowledgement:
92 92 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
93 93 *
94 94 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
95 95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 97 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
98 98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 104 * SUCH DAMAGE.
105 105 *
106 106 * The licence and distribution terms for any publically available version or
107 107 * derivative of this code cannot be changed. i.e. this code cannot simply be
108 108 * copied and put under another distribution licence
↓ open down ↓ |
108 lines elided |
↑ open up ↑ |
109 109 * [including the GNU Public Licence.]
110 110 */
111 111 /* ====================================================================
112 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 113 * ECDH support in OpenSSL originally developed by
114 114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115 115 */
116 116
117 117 #include "cryptlib.h"
118 118 #include <openssl/safestack.h>
119 +#include <pthread.h>
119 120
120 121 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
121 122 static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */
122 123 #endif
123 124
124 125 DECLARE_STACK_OF(CRYPTO_dynlock)
125 126
126 127 /* real #defines in crypto.h, keep these upto date */
127 128 static const char* const lock_names[CRYPTO_NUM_LOCKS] =
128 129 {
129 130 "<<ERROR>>",
130 131 "err",
131 132 "ex_data",
132 133 "x509",
133 134 "x509_info",
134 135 "x509_pkey",
135 136 "x509_crl",
136 137 "x509_req",
137 138 "dsa",
138 139 "rsa",
139 140 "evp_pkey",
140 141 "x509_store",
141 142 "ssl_ctx",
142 143 "ssl_cert",
143 144 "ssl_session",
144 145 "ssl_sess_cert",
145 146 "ssl",
146 147 "ssl_method",
147 148 "rand",
148 149 "rand2",
149 150 "debug_malloc",
150 151 "BIO",
151 152 "gethostbyname",
152 153 "getservbyname",
153 154 "readdir",
154 155 "RSA_blinding",
155 156 "dh",
156 157 "debug_malloc2",
157 158 "dso",
158 159 "dynlock",
159 160 "engine",
160 161 "ui",
161 162 "ecdsa",
162 163 "ec",
163 164 "ecdh",
164 165 "bn",
165 166 "ec_pre_comp",
166 167 "store",
167 168 "comp",
168 169 "fips",
169 170 "fips2",
170 171 #if CRYPTO_NUM_LOCKS != 41
171 172 # error "Inconsistency between crypto.h and cryptlib.c"
172 173 #endif
173 174 };
↓ open down ↓ |
45 lines elided |
↑ open up ↑ |
174 175
175 176 /* This is for applications to allocate new type names in the non-dynamic
176 177 array of lock names. These are numbered with positive numbers. */
177 178 static STACK_OF(OPENSSL_STRING) *app_locks=NULL;
178 179
179 180 /* For applications that want a more dynamic way of handling threads, the
180 181 following stack is used. These are externally numbered with negative
181 182 numbers. */
182 183 static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL;
183 184
185 +static pthread_mutex_t *illumos_openssl_locks;
184 186
185 187 static void (MS_FAR *locking_callback)(int mode,int type,
186 188 const char *file,int line)=0;
187 189 static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
188 190 int type,const char *file,int line)=0;
189 191 #ifndef OPENSSL_NO_DEPRECATED
190 192 static unsigned long (MS_FAR *id_callback)(void)=0;
191 193 #endif
192 194 static void (MS_FAR *threadid_callback)(CRYPTO_THREADID *)=0;
193 195 static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
194 196 (const char *file,int line)=0;
195 197 static void (MS_FAR *dynlock_lock_callback)(int mode,
196 198 struct CRYPTO_dynlock_value *l, const char *file,int line)=0;
197 199 static void (MS_FAR *dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l,
198 200 const char *file,int line)=0;
199 201
200 202 int CRYPTO_get_new_lockid(char *name)
201 203 {
202 204 char *str;
203 205 int i;
204 206
205 207 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
206 208 /* A hack to make Visual C++ 5.0 work correctly when linking as
207 209 * a DLL using /MT. Without this, the application cannot use
208 210 * any floating point printf's.
209 211 * It also seems to be needed for Visual C 1.5 (win16) */
210 212 SSLeay_MSVC5_hack=(double)name[0]*(double)name[1];
211 213 #endif
212 214
213 215 if ((app_locks == NULL) && ((app_locks=sk_OPENSSL_STRING_new_null()) == NULL))
214 216 {
215 217 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
216 218 return(0);
217 219 }
218 220 if ((str=BUF_strdup(name)) == NULL)
219 221 {
220 222 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
221 223 return(0);
222 224 }
223 225 i=sk_OPENSSL_STRING_push(app_locks,str);
224 226 if (!i)
225 227 OPENSSL_free(str);
226 228 else
227 229 i+=CRYPTO_NUM_LOCKS; /* gap of one :-) */
228 230 return(i);
229 231 }
230 232
231 233 int CRYPTO_num_locks(void)
232 234 {
233 235 return CRYPTO_NUM_LOCKS;
234 236 }
235 237
236 238 int CRYPTO_get_new_dynlockid(void)
237 239 {
238 240 int i = 0;
239 241 CRYPTO_dynlock *pointer = NULL;
240 242
241 243 if (dynlock_create_callback == NULL)
242 244 {
243 245 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
244 246 return(0);
245 247 }
246 248 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
247 249 if ((dyn_locks == NULL)
248 250 && ((dyn_locks=sk_CRYPTO_dynlock_new_null()) == NULL))
249 251 {
250 252 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
251 253 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
252 254 return(0);
253 255 }
254 256 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
255 257
256 258 pointer = (CRYPTO_dynlock *)OPENSSL_malloc(sizeof(CRYPTO_dynlock));
257 259 if (pointer == NULL)
258 260 {
259 261 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
260 262 return(0);
261 263 }
262 264 pointer->references = 1;
263 265 pointer->data = dynlock_create_callback(__FILE__,__LINE__);
264 266 if (pointer->data == NULL)
265 267 {
266 268 OPENSSL_free(pointer);
267 269 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
268 270 return(0);
269 271 }
270 272
271 273 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
272 274 /* First, try to find an existing empty slot */
273 275 i=sk_CRYPTO_dynlock_find(dyn_locks,NULL);
274 276 /* If there was none, push, thereby creating a new one */
275 277 if (i == -1)
276 278 /* Since sk_push() returns the number of items on the
277 279 stack, not the location of the pushed item, we need
278 280 to transform the returned number into a position,
279 281 by decreasing it. */
280 282 i=sk_CRYPTO_dynlock_push(dyn_locks,pointer) - 1;
281 283 else
282 284 /* If we found a place with a NULL pointer, put our pointer
283 285 in it. */
284 286 (void)sk_CRYPTO_dynlock_set(dyn_locks,i,pointer);
285 287 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
286 288
287 289 if (i == -1)
288 290 {
289 291 dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
290 292 OPENSSL_free(pointer);
291 293 }
292 294 else
293 295 i += 1; /* to avoid 0 */
294 296 return -i;
295 297 }
296 298
297 299 void CRYPTO_destroy_dynlockid(int i)
298 300 {
299 301 CRYPTO_dynlock *pointer = NULL;
300 302 if (i)
301 303 i = -i-1;
302 304 if (dynlock_destroy_callback == NULL)
303 305 return;
304 306
305 307 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
306 308
307 309 if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks))
308 310 {
309 311 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
310 312 return;
311 313 }
312 314 pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
313 315 if (pointer != NULL)
314 316 {
315 317 --pointer->references;
316 318 #ifdef REF_CHECK
317 319 if (pointer->references < 0)
318 320 {
319 321 fprintf(stderr,"CRYPTO_destroy_dynlockid, bad reference count\n");
320 322 abort();
321 323 }
322 324 else
323 325 #endif
324 326 if (pointer->references <= 0)
325 327 {
326 328 (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
327 329 }
328 330 else
329 331 pointer = NULL;
330 332 }
331 333 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
332 334
333 335 if (pointer)
334 336 {
335 337 dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
336 338 OPENSSL_free(pointer);
337 339 }
338 340 }
339 341
340 342 struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
341 343 {
342 344 CRYPTO_dynlock *pointer = NULL;
343 345 if (i)
344 346 i = -i-1;
345 347
346 348 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
347 349
348 350 if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
349 351 pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
350 352 if (pointer)
351 353 pointer->references++;
352 354
353 355 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
354 356
355 357 if (pointer)
356 358 return pointer->data;
357 359 return NULL;
358 360 }
359 361
360 362 struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
361 363 (const char *file,int line)
362 364 {
363 365 return(dynlock_create_callback);
364 366 }
365 367
366 368 void (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
367 369 struct CRYPTO_dynlock_value *l, const char *file,int line)
368 370 {
369 371 return(dynlock_lock_callback);
370 372 }
371 373
372 374 void (*CRYPTO_get_dynlock_destroy_callback(void))
373 375 (struct CRYPTO_dynlock_value *l, const char *file,int line)
374 376 {
375 377 return(dynlock_destroy_callback);
376 378 }
377 379
378 380 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
379 381 (const char *file, int line))
380 382 {
381 383 dynlock_create_callback=func;
382 384 }
383 385
384 386 void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode,
385 387 struct CRYPTO_dynlock_value *l, const char *file, int line))
386 388 {
387 389 dynlock_lock_callback=func;
388 390 }
389 391
390 392 void CRYPTO_set_dynlock_destroy_callback(void (*func)
391 393 (struct CRYPTO_dynlock_value *l, const char *file, int line))
392 394 {
393 395 dynlock_destroy_callback=func;
394 396 }
395 397
396 398
397 399 void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file,
398 400 int line)
↓ open down ↓ |
205 lines elided |
↑ open up ↑ |
399 401 {
400 402 return(locking_callback);
401 403 }
402 404
403 405 int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type,
404 406 const char *file,int line)
405 407 {
406 408 return(add_lock_callback);
407 409 }
408 410
411 +/*
412 + * This is the locking callback function which all applications will be
413 + * using when CRYPTO_lock() is called.
414 + */
415 +static void illumos_locking_callback(int mode, int type, const char *file,
416 + int line)
417 + {
418 + if (mode & CRYPTO_LOCK)
419 + {
420 + pthread_mutex_lock(&illumos_openssl_locks[type]);
421 + }
422 + else
423 + {
424 + pthread_mutex_unlock(&illumos_openssl_locks[type]);
425 + }
426 + }
427 +
428 +
429 +/*
430 + * This function is called when a child process is forked to setup its own
431 + * global locking callback function ptr and mutexes.
432 + */
433 +static void illumos_fork_child(void)
434 + {
435 + /*
436 + * clear locking_callback to indicate that locks should
437 + * be reinitialized.
438 + */
439 + locking_callback = NULL;
440 + illumos_locking_setup();
441 + }
442 +
443 +/*
444 + * This function allocates and initializes the global mutex array, and
445 + * sets the locking callback.
446 + */
447 +void illumos_locking_setup()
448 + {
449 + int i;
450 + int num_locks;
451 +
452 + /* locking callback is already setup. Nothing to do */
453 + if (locking_callback != NULL)
454 + {
455 + return;
456 + }
457 +
458 + /*
459 + * Set atfork handler so that child can setup its own mutexes and
460 + * locking callbacks when it is forked
461 + */
462 + (void) pthread_atfork(NULL, NULL, illumos_fork_child);
463 +
464 + /* allocate locks needed by OpenSSL */
465 + num_locks = CRYPTO_num_locks();
466 + illumos_openssl_locks =
467 + OPENSSL_malloc(sizeof (pthread_mutex_t) * num_locks);
468 + if (illumos_openssl_locks == NULL)
469 + {
470 + fprintf(stderr,
471 + "illumos_locking_setup: memory allocation failure.\n");
472 + abort();
473 + }
474 +
475 + /* initialize openssl mutexes */
476 + for (i = 0; i < num_locks; i++)
477 + {
478 + pthread_mutex_init(&illumos_openssl_locks[i], NULL);
479 + }
480 + locking_callback = illumos_locking_callback;
481 +
482 + }
483 +
409 484 void CRYPTO_set_locking_callback(void (*func)(int mode,int type,
410 485 const char *file,int line))
411 486 {
412 487 /* Calling this here ensures initialisation before any threads
413 488 * are started.
414 489 */
415 490 OPENSSL_init();
416 - locking_callback=func;
491 +
492 + /*
493 + * we now setup our own locking callback and mutexes, and disallow
494 + * setting of another locking callback.
495 + */
417 496 }
418 497
419 498 void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type,
420 499 const char *file,int line))
421 500 {
422 501 add_lock_callback=func;
423 502 }
424 503
425 504 /* the memset() here and in set_pointer() seem overkill, but for the sake of
426 505 * CRYPTO_THREADID_cmp() this avoids any platform silliness that might cause two
427 506 * "equal" THREADID structs to not be memcmp()-identical. */
428 507 void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val)
429 508 {
430 509 memset(id, 0, sizeof(*id));
431 510 id->val = val;
432 511 }
433 512
434 513 static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
435 514 void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr)
436 515 {
437 516 unsigned char *dest = (void *)&id->val;
438 517 unsigned int accum = 0;
439 518 unsigned char dnum = sizeof(id->val);
440 519
441 520 memset(id, 0, sizeof(*id));
442 521 id->ptr = ptr;
443 522 if (sizeof(id->val) >= sizeof(id->ptr))
444 523 {
445 524 /* 'ptr' can be embedded in 'val' without loss of uniqueness */
446 525 id->val = (unsigned long)id->ptr;
447 526 return;
448 527 }
449 528 /* hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a
450 529 * linear function over the bytes in 'ptr', the co-efficients of which
451 530 * are a sequence of low-primes (hash_coeffs is an 8-element cycle) -
452 531 * the starting prime for the sequence varies for each byte of 'val'
453 532 * (unique polynomials unless pointers are >64-bit). For added spice,
454 533 * the totals accumulate rather than restarting from zero, and the index
455 534 * of the 'val' byte is added each time (position dependence). If I was
456 535 * a black-belt, I'd scan big-endian pointers in reverse to give
457 536 * low-order bits more play, but this isn't crypto and I'd prefer nobody
458 537 * mistake it as such. Plus I'm lazy. */
459 538 while (dnum--)
460 539 {
461 540 const unsigned char *src = (void *)&id->ptr;
462 541 unsigned char snum = sizeof(id->ptr);
463 542 while (snum--)
464 543 accum += *(src++) * hash_coeffs[(snum + dnum) & 7];
465 544 accum += dnum;
466 545 *(dest++) = accum & 255;
467 546 }
468 547 }
469 548
470 549 int CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *))
471 550 {
472 551 if (threadid_callback)
473 552 return 0;
474 553 threadid_callback = func;
475 554 return 1;
476 555 }
477 556
478 557 void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *)
479 558 {
480 559 return threadid_callback;
481 560 }
482 561
483 562 void CRYPTO_THREADID_current(CRYPTO_THREADID *id)
484 563 {
485 564 if (threadid_callback)
486 565 {
487 566 threadid_callback(id);
488 567 return;
489 568 }
490 569 #ifndef OPENSSL_NO_DEPRECATED
491 570 /* If the deprecated callback was set, fall back to that */
492 571 if (id_callback)
493 572 {
494 573 CRYPTO_THREADID_set_numeric(id, id_callback());
495 574 return;
496 575 }
497 576 #endif
498 577 /* Else pick a backup */
499 578 #ifdef OPENSSL_SYS_WIN16
500 579 CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentTask());
501 580 #elif defined(OPENSSL_SYS_WIN32)
502 581 CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentThreadId());
503 582 #elif defined(OPENSSL_SYS_BEOS)
504 583 CRYPTO_THREADID_set_numeric(id, (unsigned long)find_thread(NULL));
505 584 #else
506 585 /* For everything else, default to using the address of 'errno' */
507 586 CRYPTO_THREADID_set_pointer(id, (void*)&errno);
508 587 #endif
509 588 }
510 589
511 590 int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b)
512 591 {
513 592 return memcmp(a, b, sizeof(*a));
514 593 }
515 594
516 595 void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src)
517 596 {
518 597 memcpy(dest, src, sizeof(*src));
519 598 }
520 599
521 600 unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id)
522 601 {
523 602 return id->val;
524 603 }
525 604
526 605 #ifndef OPENSSL_NO_DEPRECATED
527 606 unsigned long (*CRYPTO_get_id_callback(void))(void)
528 607 {
529 608 return(id_callback);
530 609 }
531 610
532 611 void CRYPTO_set_id_callback(unsigned long (*func)(void))
533 612 {
534 613 id_callback=func;
535 614 }
536 615
537 616 unsigned long CRYPTO_thread_id(void)
538 617 {
539 618 unsigned long ret=0;
540 619
541 620 if (id_callback == NULL)
542 621 {
543 622 #ifdef OPENSSL_SYS_WIN16
544 623 ret=(unsigned long)GetCurrentTask();
545 624 #elif defined(OPENSSL_SYS_WIN32)
546 625 ret=(unsigned long)GetCurrentThreadId();
547 626 #elif defined(GETPID_IS_MEANINGLESS)
548 627 ret=1L;
549 628 #elif defined(OPENSSL_SYS_BEOS)
550 629 ret=(unsigned long)find_thread(NULL);
551 630 #else
552 631 ret=(unsigned long)getpid();
553 632 #endif
554 633 }
555 634 else
556 635 ret=id_callback();
557 636 return(ret);
558 637 }
559 638 #endif
560 639
561 640 void CRYPTO_lock(int mode, int type, const char *file, int line)
562 641 {
563 642 #ifdef LOCK_DEBUG
564 643 {
565 644 CRYPTO_THREADID id;
566 645 char *rw_text,*operation_text;
567 646
568 647 if (mode & CRYPTO_LOCK)
569 648 operation_text="lock ";
570 649 else if (mode & CRYPTO_UNLOCK)
571 650 operation_text="unlock";
572 651 else
573 652 operation_text="ERROR ";
574 653
575 654 if (mode & CRYPTO_READ)
576 655 rw_text="r";
577 656 else if (mode & CRYPTO_WRITE)
578 657 rw_text="w";
579 658 else
580 659 rw_text="ERROR";
581 660
582 661 CRYPTO_THREADID_current(&id);
583 662 fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
584 663 CRYPTO_THREADID_hash(&id), rw_text, operation_text,
585 664 CRYPTO_get_lock_name(type), file, line);
586 665 }
587 666 #endif
588 667 if (type < 0)
589 668 {
590 669 if (dynlock_lock_callback != NULL)
591 670 {
592 671 struct CRYPTO_dynlock_value *pointer
593 672 = CRYPTO_get_dynlock_value(type);
594 673
595 674 OPENSSL_assert(pointer != NULL);
596 675
597 676 dynlock_lock_callback(mode, pointer, file, line);
598 677
599 678 CRYPTO_destroy_dynlockid(type);
600 679 }
601 680 }
602 681 else
603 682 if (locking_callback != NULL)
604 683 locking_callback(mode,type,file,line);
605 684 }
606 685
607 686 int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
608 687 int line)
609 688 {
610 689 int ret = 0;
611 690
612 691 if (add_lock_callback != NULL)
613 692 {
614 693 #ifdef LOCK_DEBUG
615 694 int before= *pointer;
616 695 #endif
617 696
618 697 ret=add_lock_callback(pointer,amount,type,file,line);
619 698 #ifdef LOCK_DEBUG
620 699 {
621 700 CRYPTO_THREADID id;
622 701 CRYPTO_THREADID_current(&id);
623 702 fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
624 703 CRYPTO_THREADID_hash(&id), before,amount,ret,
625 704 CRYPTO_get_lock_name(type),
626 705 file,line);
627 706 }
628 707 #endif
629 708 }
630 709 else
631 710 {
632 711 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);
633 712
634 713 ret= *pointer+amount;
635 714 #ifdef LOCK_DEBUG
636 715 {
637 716 CRYPTO_THREADID id;
638 717 CRYPTO_THREADID_current(&id);
639 718 fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
640 719 CRYPTO_THREADID_hash(&id),
641 720 *pointer,amount,ret,
642 721 CRYPTO_get_lock_name(type),
643 722 file,line);
644 723 }
645 724 #endif
646 725 *pointer=ret;
647 726 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);
648 727 }
649 728 return(ret);
650 729 }
651 730
652 731 const char *CRYPTO_get_lock_name(int type)
653 732 {
654 733 if (type < 0)
655 734 return("dynamic");
656 735 else if (type < CRYPTO_NUM_LOCKS)
657 736 return(lock_names[type]);
658 737 else if (type-CRYPTO_NUM_LOCKS > sk_OPENSSL_STRING_num(app_locks))
659 738 return("ERROR");
660 739 else
661 740 return(sk_OPENSSL_STRING_value(app_locks,type-CRYPTO_NUM_LOCKS));
662 741 }
663 742
664 743 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
665 744 defined(__INTEL__) || \
666 745 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
667 746
668 747 unsigned int OPENSSL_ia32cap_P[2];
669 748 unsigned long *OPENSSL_ia32cap_loc(void)
670 749 { if (sizeof(long)==4)
671 750 /*
672 751 * If 32-bit application pulls address of OPENSSL_ia32cap_P[0]
673 752 * clear second element to maintain the illusion that vector
674 753 * is 32-bit.
675 754 */
676 755 OPENSSL_ia32cap_P[1]=0;
677 756 return (unsigned long *)OPENSSL_ia32cap_P;
678 757 }
679 758
680 759 #if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
681 760 #define OPENSSL_CPUID_SETUP
682 761 #if defined(_WIN32)
683 762 typedef unsigned __int64 IA32CAP;
684 763 #else
685 764 typedef unsigned long long IA32CAP;
686 765 #endif
687 766 void OPENSSL_cpuid_setup(void)
688 767 { static int trigger=0;
689 768 IA32CAP OPENSSL_ia32_cpuid(void);
690 769 IA32CAP vec;
691 770 char *env;
692 771
693 772 if (trigger) return;
694 773
695 774 trigger=1;
696 775 if ((env=getenv("OPENSSL_ia32cap"))) {
697 776 int off = (env[0]=='~')?1:0;
698 777 #if defined(_WIN32)
699 778 if (!sscanf(env+off,"%I64i",&vec)) vec = strtoul(env+off,NULL,0);
700 779 #else
701 780 if (!sscanf(env+off,"%lli",(long long *)&vec)) vec = strtoul(env+off,NULL,0);
702 781 #endif
703 782 if (off) vec = OPENSSL_ia32_cpuid()&~vec;
704 783 }
705 784 else
706 785 vec = OPENSSL_ia32_cpuid();
707 786
708 787 /*
709 788 * |(1<<10) sets a reserved bit to signal that variable
710 789 * was initialized already... This is to avoid interference
711 790 * with cpuid snippets in ELF .init segment.
712 791 */
713 792 OPENSSL_ia32cap_P[0] = (unsigned int)vec|(1<<10);
714 793 OPENSSL_ia32cap_P[1] = (unsigned int)(vec>>32);
715 794 }
716 795 #endif
717 796
718 797 #else
719 798 unsigned long *OPENSSL_ia32cap_loc(void) { return NULL; }
720 799 #endif
721 800 int OPENSSL_NONPIC_relocated = 0;
722 801 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
723 802 void OPENSSL_cpuid_setup(void) {}
724 803 #endif
725 804
726 805 #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
727 806 #ifdef __CYGWIN__
728 807 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
729 808 #include <windows.h>
730 809 /* this has side-effect of _WIN32 getting defined, which otherwise
731 810 * is mutually exclusive with __CYGWIN__... */
732 811 #endif
733 812
734 813 /* All we really need to do is remove the 'error' state when a thread
735 814 * detaches */
736 815
737 816 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
738 817 LPVOID lpvReserved)
739 818 {
740 819 switch(fdwReason)
741 820 {
742 821 case DLL_PROCESS_ATTACH:
743 822 OPENSSL_cpuid_setup();
744 823 #if defined(_WIN32_WINNT)
745 824 {
746 825 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL;
747 826 IMAGE_NT_HEADERS *nt_headers;
748 827
749 828 if (dos_header->e_magic==IMAGE_DOS_SIGNATURE)
750 829 {
751 830 nt_headers = (IMAGE_NT_HEADERS *)((char *)dos_header
752 831 + dos_header->e_lfanew);
753 832 if (nt_headers->Signature==IMAGE_NT_SIGNATURE &&
754 833 hinstDLL!=(HINSTANCE)(nt_headers->OptionalHeader.ImageBase))
755 834 OPENSSL_NONPIC_relocated=1;
756 835 }
757 836 }
758 837 #endif
759 838 break;
760 839 case DLL_THREAD_ATTACH:
761 840 break;
762 841 case DLL_THREAD_DETACH:
763 842 break;
764 843 case DLL_PROCESS_DETACH:
765 844 break;
766 845 }
767 846 return(TRUE);
768 847 }
769 848 #endif
770 849
771 850 #if defined(_WIN32) && !defined(__CYGWIN__)
772 851 #include <tchar.h>
773 852 #include <signal.h>
774 853 #ifdef __WATCOMC__
775 854 #if defined(_UNICODE) || defined(__UNICODE__)
776 855 #define _vsntprintf _vsnwprintf
777 856 #else
778 857 #define _vsntprintf _vsnprintf
779 858 #endif
780 859 #endif
781 860 #ifdef _MSC_VER
782 861 #define alloca _alloca
783 862 #endif
784 863
785 864 #if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
786 865 int OPENSSL_isservice(void)
787 866 { HWINSTA h;
788 867 DWORD len;
789 868 WCHAR *name;
790 869 static union { void *p; int (*f)(void); } _OPENSSL_isservice = { NULL };
791 870
792 871 if (_OPENSSL_isservice.p == NULL) {
793 872 HANDLE h = GetModuleHandle(NULL);
794 873 if (h != NULL)
795 874 _OPENSSL_isservice.p = GetProcAddress(h,"_OPENSSL_isservice");
796 875 if (_OPENSSL_isservice.p == NULL)
797 876 _OPENSSL_isservice.p = (void *)-1;
798 877 }
799 878
800 879 if (_OPENSSL_isservice.p != (void *)-1)
801 880 return (*_OPENSSL_isservice.f)();
802 881
803 882 (void)GetDesktopWindow(); /* return value is ignored */
804 883
805 884 h = GetProcessWindowStation();
806 885 if (h==NULL) return -1;
807 886
808 887 if (GetUserObjectInformationW (h,UOI_NAME,NULL,0,&len) ||
809 888 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
810 889 return -1;
811 890
812 891 if (len>512) return -1; /* paranoia */
813 892 len++,len&=~1; /* paranoia */
814 893 name=(WCHAR *)alloca(len+sizeof(WCHAR));
815 894 if (!GetUserObjectInformationW (h,UOI_NAME,name,len,&len))
816 895 return -1;
817 896
818 897 len++,len&=~1; /* paranoia */
819 898 name[len/sizeof(WCHAR)]=L'\0'; /* paranoia */
820 899 #if 1
821 900 /* This doesn't cover "interactive" services [working with real
822 901 * WinSta0's] nor programs started non-interactively by Task
823 902 * Scheduler [those are working with SAWinSta]. */
824 903 if (wcsstr(name,L"Service-0x")) return 1;
825 904 #else
826 905 /* This covers all non-interactive programs such as services. */
827 906 if (!wcsstr(name,L"WinSta0")) return 1;
828 907 #endif
829 908 else return 0;
830 909 }
831 910 #else
832 911 int OPENSSL_isservice(void) { return 0; }
833 912 #endif
834 913
835 914 void OPENSSL_showfatal (const char *fmta,...)
836 915 { va_list ap;
837 916 TCHAR buf[256];
838 917 const TCHAR *fmt;
839 918 #ifdef STD_ERROR_HANDLE /* what a dirty trick! */
840 919 HANDLE h;
841 920
842 921 if ((h=GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
843 922 GetFileType(h)!=FILE_TYPE_UNKNOWN)
844 923 { /* must be console application */
845 924 va_start (ap,fmta);
846 925 vfprintf (stderr,fmta,ap);
847 926 va_end (ap);
848 927 return;
849 928 }
850 929 #endif
851 930
852 931 if (sizeof(TCHAR)==sizeof(char))
853 932 fmt=(const TCHAR *)fmta;
854 933 else do
855 934 { int keepgoing;
856 935 size_t len_0=strlen(fmta)+1,i;
857 936 WCHAR *fmtw;
858 937
859 938 fmtw = (WCHAR *)alloca(len_0*sizeof(WCHAR));
860 939 if (fmtw == NULL) { fmt=(const TCHAR *)L"no stack?"; break; }
861 940
862 941 #ifndef OPENSSL_NO_MULTIBYTE
863 942 if (!MultiByteToWideChar(CP_ACP,0,fmta,len_0,fmtw,len_0))
864 943 #endif
865 944 for (i=0;i<len_0;i++) fmtw[i]=(WCHAR)fmta[i];
866 945
867 946 for (i=0;i<len_0;i++)
868 947 { if (fmtw[i]==L'%') do
869 948 { keepgoing=0;
870 949 switch (fmtw[i+1])
871 950 { case L'0': case L'1': case L'2': case L'3': case L'4':
872 951 case L'5': case L'6': case L'7': case L'8': case L'9':
873 952 case L'.': case L'*':
874 953 case L'-': i++; keepgoing=1; break;
875 954 case L's': fmtw[i+1]=L'S'; break;
876 955 case L'S': fmtw[i+1]=L's'; break;
877 956 case L'c': fmtw[i+1]=L'C'; break;
878 957 case L'C': fmtw[i+1]=L'c'; break;
879 958 }
880 959 } while (keepgoing);
881 960 }
882 961 fmt = (const TCHAR *)fmtw;
883 962 } while (0);
884 963
885 964 va_start (ap,fmta);
886 965 _vsntprintf (buf,sizeof(buf)/sizeof(TCHAR)-1,fmt,ap);
887 966 buf [sizeof(buf)/sizeof(TCHAR)-1] = _T('\0');
888 967 va_end (ap);
889 968
890 969 #if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
891 970 /* this -------------v--- guards NT-specific calls */
892 971 if (check_winnt() && OPENSSL_isservice() > 0)
893 972 { HANDLE h = RegisterEventSource(0,_T("OPENSSL"));
894 973 const TCHAR *pmsg=buf;
895 974 ReportEvent(h,EVENTLOG_ERROR_TYPE,0,0,0,1,0,&pmsg,0);
896 975 DeregisterEventSource(h);
897 976 }
898 977 else
899 978 #endif
900 979 MessageBox (NULL,buf,_T("OpenSSL: FATAL"),MB_OK|MB_ICONSTOP);
901 980 }
902 981 #else
903 982 void OPENSSL_showfatal (const char *fmta,...)
904 983 { va_list ap;
905 984
906 985 va_start (ap,fmta);
907 986 vfprintf (stderr,fmta,ap);
908 987 va_end (ap);
909 988 }
910 989 int OPENSSL_isservice (void) { return 0; }
911 990 #endif
912 991
913 992 void OpenSSLDie(const char *file,int line,const char *assertion)
914 993 {
915 994 OPENSSL_showfatal(
916 995 "%s(%d): OpenSSL internal error, assertion failed: %s\n",
917 996 file,line,assertion);
918 997 #if !defined(_WIN32) || defined(__CYGWIN__)
919 998 abort();
920 999 #else
921 1000 /* Win32 abort() customarily shows a dialog, but we just did that... */
922 1001 raise(SIGABRT);
923 1002 _exit(3);
924 1003 #endif
925 1004 }
926 1005
927 1006 void *OPENSSL_stderr(void) { return stderr; }
928 1007
929 1008 int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
930 1009 {
931 1010 size_t i;
932 1011 const unsigned char *a = in_a;
933 1012 const unsigned char *b = in_b;
934 1013 unsigned char x = 0;
935 1014
936 1015 for (i = 0; i < len; i++)
937 1016 x |= a[i] ^ b[i];
938 1017
939 1018 return x;
940 1019 }
↓ open down ↓ |
514 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX