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