summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/cryptlib.c
diff options
context:
space:
mode:
authordjm <>2008-09-06 12:17:54 +0000
committerdjm <>2008-09-06 12:17:54 +0000
commit38ce604e3cc97706b876b0525ddff0121115456d (patch)
tree7ccc28afe1789ea3dbedf72365f955d5b8e105b5 /src/lib/libcrypto/cryptlib.c
parent12867252827c8efaa8ddd1fa3b3d6e321e2bcdef (diff)
downloadopenbsd-38ce604e3cc97706b876b0525ddff0121115456d.tar.gz
openbsd-38ce604e3cc97706b876b0525ddff0121115456d.tar.bz2
openbsd-38ce604e3cc97706b876b0525ddff0121115456d.zip
resolve conflicts
Diffstat (limited to 'src/lib/libcrypto/cryptlib.c')
-rw-r--r--src/lib/libcrypto/cryptlib.c226
1 files changed, 120 insertions, 106 deletions
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c
index e63bbe8dba..8c68623828 100644
--- a/src/lib/libcrypto/cryptlib.c
+++ b/src/lib/libcrypto/cryptlib.c
@@ -1,4 +1,57 @@
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 */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 55/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 56 * All rights reserved.
4 * 57 *
@@ -55,11 +108,13 @@
55 * copied and put under another distribution licence 108 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] 109 * [including the GNU Public Licence.]
57 */ 110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * ECDH support in OpenSSL originally developed by
114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115 */
58 116
59#include <stdio.h>
60#include <string.h>
61#include "cryptlib.h" 117#include "cryptlib.h"
62#include <openssl/crypto.h>
63#include <openssl/safestack.h> 118#include <openssl/safestack.h>
64 119
65#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) 120#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
@@ -70,7 +125,7 @@ DECLARE_STACK_OF(CRYPTO_dynlock)
70IMPLEMENT_STACK_OF(CRYPTO_dynlock) 125IMPLEMENT_STACK_OF(CRYPTO_dynlock)
71 126
72/* real #defines in crypto.h, keep these upto date */ 127/* real #defines in crypto.h, keep these upto date */
73static const char* lock_names[CRYPTO_NUM_LOCKS] = 128static const char* const lock_names[CRYPTO_NUM_LOCKS] =
74 { 129 {
75 "<<ERROR>>", 130 "<<ERROR>>",
76 "err", 131 "err",
@@ -104,10 +159,14 @@ static const char* lock_names[CRYPTO_NUM_LOCKS] =
104 "dynlock", 159 "dynlock",
105 "engine", 160 "engine",
106 "ui", 161 "ui",
107 "hwcrhk", /* This is a HACK which will disappear in 0.9.8 */ 162 "ecdsa",
108 "fips", 163 "ec",
109 "fips2", 164 "ecdh",
110#if CRYPTO_NUM_LOCKS != 35 165 "bn",
166 "ec_pre_comp",
167 "store",
168 "comp",
169#if CRYPTO_NUM_LOCKS != 39
111# error "Inconsistency between crypto.h and cryptlib.c" 170# error "Inconsistency between crypto.h and cryptlib.c"
112#endif 171#endif
113 }; 172 };
@@ -218,7 +277,7 @@ int CRYPTO_get_new_dynlockid(void)
218 else 277 else
219 /* If we found a place with a NULL pointer, put our pointer 278 /* If we found a place with a NULL pointer, put our pointer
220 in it. */ 279 in it. */
221 sk_CRYPTO_dynlock_set(dyn_locks,i,pointer); 280 (void)sk_CRYPTO_dynlock_set(dyn_locks,i,pointer);
222 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); 281 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
223 282
224 if (i == -1) 283 if (i == -1)
@@ -260,7 +319,7 @@ void CRYPTO_destroy_dynlockid(int i)
260#endif 319#endif
261 if (pointer->references <= 0) 320 if (pointer->references <= 0)
262 { 321 {
263 sk_CRYPTO_dynlock_set(dyn_locks, i, NULL); 322 (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
264 } 323 }
265 else 324 else
266 pointer = NULL; 325 pointer = NULL;
@@ -480,9 +539,48 @@ const char *CRYPTO_get_lock_name(int type)
480 return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS)); 539 return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));
481 } 540 }
482 541
483int OPENSSL_NONPIC_relocated=0; 542#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
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;
484 555
485#if defined(_WIN32) && defined(_WINDLL) 556 if (trigger) return;
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
486 584
487/* All we really need to do is remove the 'error' state when a thread 585/* All we really need to do is remove the 'error' state when a thread
488 * detaches */ 586 * detaches */
@@ -493,6 +591,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
493 switch(fdwReason) 591 switch(fdwReason)
494 { 592 {
495 case DLL_PROCESS_ATTACH: 593 case DLL_PROCESS_ATTACH:
594 OPENSSL_cpuid_setup();
496#if defined(_WIN32_WINNT) 595#if defined(_WIN32_WINNT)
497 { 596 {
498 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL; 597 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL;
@@ -521,11 +620,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
521 } 620 }
522#endif 621#endif
523 622
524#if defined(_WIN32) 623#if defined(_WIN32) && !defined(__CYGWIN__)
525#include <tchar.h> 624#include <tchar.h>
526 625
527#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 626#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
528static int IsService(void) 627int OPENSSL_isservice(void)
529{ HWINSTA h; 628{ HWINSTA h;
530 DWORD len; 629 DWORD len;
531 WCHAR *name; 630 WCHAR *name;
@@ -562,12 +661,15 @@ static int IsService(void)
562#endif 661#endif
563 else return 0; 662 else return 0;
564} 663}
664#else
665int OPENSSL_isservice(void) { return 0; }
565#endif 666#endif
566 667
567void OPENSSL_showfatal (const char *fmta,...) 668void OPENSSL_showfatal (const char *fmta,...)
568{ va_list ap; 669{ va_list ap;
569 TCHAR buf[256]; 670 TCHAR buf[256];
570 const TCHAR *fmt; 671 const TCHAR *fmt;
672#ifdef STD_ERROR_HANDLE /* what a dirty trick! */
571 HANDLE h; 673 HANDLE h;
572 674
573 if ((h=GetStdHandle(STD_ERROR_HANDLE)) != NULL && 675 if ((h=GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
@@ -578,6 +680,7 @@ void OPENSSL_showfatal (const char *fmta,...)
578 va_end (ap); 680 va_end (ap);
579 return; 681 return;
580 } 682 }
683#endif
581 684
582 if (sizeof(TCHAR)==sizeof(char)) 685 if (sizeof(TCHAR)==sizeof(char))
583 fmt=(const TCHAR *)fmta; 686 fmt=(const TCHAR *)fmta;
@@ -623,7 +726,7 @@ void OPENSSL_showfatal (const char *fmta,...)
623 726
624#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 727#if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
625 /* this -------------v--- guards NT-specific calls */ 728 /* this -------------v--- guards NT-specific calls */
626 if (GetVersion() < 0x80000000 && IsService()) 729 if (GetVersion() < 0x80000000 && OPENSSL_isservice())
627 { HANDLE h = RegisterEventSource(0,_T("OPENSSL")); 730 { HANDLE h = RegisterEventSource(0,_T("OPENSSL"));
628 const TCHAR *pmsg=buf; 731 const TCHAR *pmsg=buf;
629 ReportEvent(h,EVENTLOG_ERROR_TYPE,0,0,0,1,0,&pmsg,0); 732 ReportEvent(h,EVENTLOG_ERROR_TYPE,0,0,0,1,0,&pmsg,0);
@@ -631,21 +734,7 @@ void OPENSSL_showfatal (const char *fmta,...)
631 } 734 }
632 else 735 else
633#endif 736#endif
634 { MSGBOXPARAMS m; 737 MessageBox (NULL,buf,_T("OpenSSL: FATAL"),MB_OK|MB_ICONSTOP);
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 }
649} 738}
650#else 739#else
651void OPENSSL_showfatal (const char *fmta,...) 740void OPENSSL_showfatal (const char *fmta,...)
@@ -655,6 +744,7 @@ void OPENSSL_showfatal (const char *fmta,...)
655 vfprintf (stderr,fmta,ap); 744 vfprintf (stderr,fmta,ap);
656 va_end (ap); 745 va_end (ap);
657} 746}
747int OPENSSL_isservice (void) { return 0; }
658#endif 748#endif
659 749
660void OpenSSLDie(const char *file,int line,const char *assertion) 750void OpenSSLDie(const char *file,int line,const char *assertion)
@@ -666,79 +756,3 @@ void OpenSSLDie(const char *file,int line,const char *assertion)
666 } 756 }
667 757
668void *OPENSSL_stderr(void) { return stderr; } 758void *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