summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/cryptlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/cryptlib.c')
-rw-r--r--src/lib/libcrypto/cryptlib.c226
1 files changed, 106 insertions, 120 deletions
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c
index 8c68623828..e63bbe8dba 100644
--- a/src/lib/libcrypto/cryptlib.c
+++ b/src/lib/libcrypto/cryptlib.c
@@ -1,57 +1,4 @@
1/* crypto/cryptlib.c */ 1/* crypto/cryptlib.c */
2/* ====================================================================
3 * Copyright (c) 1998-2003 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) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56 * All rights reserved. 3 * All rights reserved.
57 * 4 *
@@ -108,13 +55,11 @@
108 * copied and put under another distribution licence 55 * copied and put under another distribution licence
109 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
110 */ 57 */
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 58
59#include <stdio.h>
60#include <string.h>
117#include "cryptlib.h" 61#include "cryptlib.h"
62#include <openssl/crypto.h>
118#include <openssl/safestack.h> 63#include <openssl/safestack.h>
119 64
120#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) 65#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
@@ -125,7 +70,7 @@ DECLARE_STACK_OF(CRYPTO_dynlock)
125IMPLEMENT_STACK_OF(CRYPTO_dynlock) 70IMPLEMENT_STACK_OF(CRYPTO_dynlock)
126 71
127/* real #defines in crypto.h, keep these upto date */ 72/* real #defines in crypto.h, keep these upto date */
128static const char* const lock_names[CRYPTO_NUM_LOCKS] = 73static const char* lock_names[CRYPTO_NUM_LOCKS] =
129 { 74 {
130 "<<ERROR>>", 75 "<<ERROR>>",
131 "err", 76 "err",
@@ -159,14 +104,10 @@ static const char* const lock_names[CRYPTO_NUM_LOCKS] =
159 "dynlock", 104 "dynlock",
160 "engine", 105 "engine",
161 "ui", 106 "ui",
162 "ecdsa", 107 "hwcrhk", /* This is a HACK which will disappear in 0.9.8 */
163 "ec", 108 "fips",
164 "ecdh", 109 "fips2",
165 "bn", 110#if CRYPTO_NUM_LOCKS != 35
166 "ec_pre_comp",
167 "store",
168 "comp",
169#if CRYPTO_NUM_LOCKS != 39
170# error "Inconsistency between crypto.h and cryptlib.c" 111# error "Inconsistency between crypto.h and cryptlib.c"
171#endif 112#endif
172 }; 113 };
@@ -277,7 +218,7 @@ int CRYPTO_get_new_dynlockid(void)
277 else 218 else
278 /* If we found a place with a NULL pointer, put our pointer 219 /* If we found a place with a NULL pointer, put our pointer
279 in it. */ 220 in it. */
280 (void)sk_CRYPTO_dynlock_set(dyn_locks,i,pointer); 221 sk_CRYPTO_dynlock_set(dyn_locks,i,pointer);
281 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); 222 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
282 223
283 if (i == -1) 224 if (i == -1)
@@ -319,7 +260,7 @@ void CRYPTO_destroy_dynlockid(int i)
319#endif 260#endif
320 if (pointer->references <= 0) 261 if (pointer->references <= 0)
321 { 262 {
322 (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL); 263 sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
323 } 264 }
324 else 265 else
325 pointer = NULL; 266 pointer = NULL;
@@ -539,48 +480,9 @@ const char *CRYPTO_get_lock_name(int type)
539 return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS)); 480 return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));
540 } 481 }
541 482
542#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ 483int OPENSSL_NONPIC_relocated=0;
543 defined(__INTEL__) || \
544 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64)
545
546unsigned long OPENSSL_ia32cap_P=0;
547unsigned long *OPENSSL_ia32cap_loc(void) { return &OPENSSL_ia32cap_P; }
548
549#if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
550#define OPENSSL_CPUID_SETUP
551void OPENSSL_cpuid_setup(void)
552{ static int trigger=0;
553 unsigned long OPENSSL_ia32_cpuid(void);
554 char *env;
555 484
556 if (trigger) return; 485#if defined(_WIN32) && defined(_WINDLL)
557
558 trigger=1;
559 if ((env=getenv("OPENSSL_ia32cap")))
560 OPENSSL_ia32cap_P = strtoul(env,NULL,0)|(1<<10);
561 else
562 OPENSSL_ia32cap_P = OPENSSL_ia32_cpuid()|(1<<10);
563 /*
564 * |(1<<10) sets a reserved bit to signal that variable
565 * was initialized already... This is to avoid interference
566 * with cpuid snippets in ELF .init segment.
567 */
568}
569#endif
570
571#else
572unsigned long *OPENSSL_ia32cap_loc(void) { return NULL; }
573#endif
574int OPENSSL_NONPIC_relocated = 0;
575#if !defined(OPENSSL_CPUID_SETUP)
576void OPENSSL_cpuid_setup(void) {}
577#endif
578
579#if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
580#ifdef __CYGWIN__
581/* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
582#include <windows.h>
583#endif
584 486
585/* All we really need to do is remove the 'error' state when a thread 487/* All we really need to do is remove the 'error' state when a thread
586 * detaches */ 488 * detaches */
@@ -591,7 +493,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
591 switch(fdwReason) 493 switch(fdwReason)
592 { 494 {
593 case DLL_PROCESS_ATTACH: 495 case DLL_PROCESS_ATTACH:
594 OPENSSL_cpuid_setup();
595#if defined(_WIN32_WINNT) 496#if defined(_WIN32_WINNT)
596 { 497 {
597 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL; 498 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL;
@@ -620,11 +521,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
620 } 521 }
621#endif 522#endif
622 523
623#if defined(_WIN32) && !defined(__CYGWIN__) 524#if defined(_WIN32)
624#include <tchar.h> 525#include <tchar.h>
625 526
626#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 527#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
627int OPENSSL_isservice(void) 528static int IsService(void)
628{ HWINSTA h; 529{ HWINSTA h;
629 DWORD len; 530 DWORD len;
630 WCHAR *name; 531 WCHAR *name;
@@ -661,15 +562,12 @@ int OPENSSL_isservice(void)
661#endif 562#endif
662 else return 0; 563 else return 0;
663} 564}
664#else
665int OPENSSL_isservice(void) { return 0; }
666#endif 565#endif
667 566
668void OPENSSL_showfatal (const char *fmta,...) 567void OPENSSL_showfatal (const char *fmta,...)
669{ va_list ap; 568{ va_list ap;
670 TCHAR buf[256]; 569 TCHAR buf[256];
671 const TCHAR *fmt; 570 const TCHAR *fmt;
672#ifdef STD_ERROR_HANDLE /* what a dirty trick! */
673 HANDLE h; 571 HANDLE h;
674 572
675 if ((h=GetStdHandle(STD_ERROR_HANDLE)) != NULL && 573 if ((h=GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
@@ -680,7 +578,6 @@ void OPENSSL_showfatal (const char *fmta,...)
680 va_end (ap); 578 va_end (ap);
681 return; 579 return;
682 } 580 }
683#endif
684 581
685 if (sizeof(TCHAR)==sizeof(char)) 582 if (sizeof(TCHAR)==sizeof(char))
686 fmt=(const TCHAR *)fmta; 583 fmt=(const TCHAR *)fmta;
@@ -726,7 +623,7 @@ void OPENSSL_showfatal (const char *fmta,...)
726 623
727#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 624#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
728 /* this -------------v--- guards NT-specific calls */ 625 /* this -------------v--- guards NT-specific calls */
729 if (GetVersion() < 0x80000000 && OPENSSL_isservice()) 626 if (GetVersion() < 0x80000000 && IsService())
730 { HANDLE h = RegisterEventSource(0,_T("OPENSSL")); 627 { HANDLE h = RegisterEventSource(0,_T("OPENSSL"));
731 const TCHAR *pmsg=buf; 628 const TCHAR *pmsg=buf;
732 ReportEvent(h,EVENTLOG_ERROR_TYPE,0,0,0,1,0,&pmsg,0); 629 ReportEvent(h,EVENTLOG_ERROR_TYPE,0,0,0,1,0,&pmsg,0);
@@ -734,7 +631,21 @@ void OPENSSL_showfatal (const char *fmta,...)
734 } 631 }
735 else 632 else
736#endif 633#endif
737 MessageBox (NULL,buf,_T("OpenSSL: FATAL"),MB_OK|MB_ICONSTOP); 634 { MSGBOXPARAMS m;
635
636 m.cbSize = sizeof(m);
637 m.hwndOwner = NULL;
638 m.lpszCaption = _T("OpenSSL: FATAL");
639 m.dwStyle = MB_OK;
640 m.hInstance = NULL;
641 m.lpszIcon = IDI_ERROR;
642 m.dwContextHelpId = 0;
643 m.lpfnMsgBoxCallback = NULL;
644 m.dwLanguageId = MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US);
645 m.lpszText = buf;
646
647 MessageBoxIndirect (&m);
648 }
738} 649}
739#else 650#else
740void OPENSSL_showfatal (const char *fmta,...) 651void OPENSSL_showfatal (const char *fmta,...)
@@ -744,7 +655,6 @@ void OPENSSL_showfatal (const char *fmta,...)
744 vfprintf (stderr,fmta,ap); 655 vfprintf (stderr,fmta,ap);
745 va_end (ap); 656 va_end (ap);
746} 657}
747int OPENSSL_isservice (void) { return 0; }
748#endif 658#endif
749 659
750void OpenSSLDie(const char *file,int line,const char *assertion) 660void OpenSSLDie(const char *file,int line,const char *assertion)
@@ -756,3 +666,79 @@ void OpenSSLDie(const char *file,int line,const char *assertion)
756 } 666 }
757 667
758void *OPENSSL_stderr(void) { return stderr; } 668void *OPENSSL_stderr(void) { return stderr; }
669
670#ifdef OPENSSL_FIPS
671
672void fips_w_lock(void) { CRYPTO_w_lock(CRYPTO_LOCK_FIPS); }
673void fips_w_unlock(void) { CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); }
674void fips_r_lock(void) { CRYPTO_r_lock(CRYPTO_LOCK_FIPS); }
675void fips_r_unlock(void) { CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); }
676
677static int fips_started = 0;
678static unsigned long fips_thread = 0;
679
680void fips_set_started(void)
681 {
682 fips_started = 1;
683 }
684
685int fips_is_started(void)
686 {
687 return fips_started;
688 }
689
690int fips_is_owning_thread(void)
691 {
692 int ret = 0;
693
694 if (fips_is_started())
695 {
696 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
697 if (fips_thread != 0 && fips_thread == CRYPTO_thread_id())
698 ret = 1;
699 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
700 }
701 return ret;
702 }
703
704int fips_set_owning_thread(void)
705 {
706 int ret = 0;
707
708 if (fips_is_started())
709 {
710 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
711 if (fips_thread == 0)
712 {
713 fips_thread = CRYPTO_thread_id();
714 ret = 1;
715 }
716 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
717 }
718 return ret;
719 }
720
721int fips_clear_owning_thread(void)
722 {
723 int ret = 0;
724
725 if (fips_is_started())
726 {
727 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
728 if (fips_thread == CRYPTO_thread_id())
729 {
730 fips_thread = 0;
731 ret = 1;
732 }
733 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
734 }
735 return ret;
736 }
737
738unsigned char *fips_signature_witness(void)
739 {
740 extern unsigned char FIPS_signature[];
741 return FIPS_signature;
742 }
743#endif /* OPENSSL_FIPS */
744