summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2014-04-15 13:42:55 +0000
committerjsing <>2014-04-15 13:42:55 +0000
commit69a8d2dcc19aed0d1116ba2ecd81cce5ae736c94 (patch)
treef085d85cb06e6b6e1a1b1d52cced54d9f7d7e5f2 /src
parentc7fdfc89ee30180bf262c4b146c1b6e1030083d6 (diff)
downloadopenbsd-69a8d2dcc19aed0d1116ba2ecd81cce5ae736c94.tar.gz
openbsd-69a8d2dcc19aed0d1116ba2ecd81cce5ae736c94.tar.bz2
openbsd-69a8d2dcc19aed0d1116ba2ecd81cce5ae736c94.zip
First pass at applying KNF to the OpenSSL code, which almost makes it
readable. This pass is whitespace only and can readily be verified using tr and md5.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/arm_arch.h2
-rw-r--r--src/lib/libcrypto/armcap.c70
-rw-r--r--src/lib/libcrypto/buildinf.h8
-rw-r--r--src/lib/libcrypto/cpt_err.c55
-rw-r--r--src/lib/libcrypto/cryptlib.c842
-rw-r--r--src/lib/libcrypto/crypto.h126
-rw-r--r--src/lib/libcrypto/cversion.c55
-rw-r--r--src/lib/libcrypto/ebcdic.c218
-rw-r--r--src/lib/libcrypto/ex_data.c440
-rw-r--r--src/lib/libcrypto/fips_ers.c2
-rw-r--r--src/lib/libcrypto/md32_common.h105
-rw-r--r--src/lib/libcrypto/mem.c443
-rw-r--r--src/lib/libcrypto/mem_clr.c16
-rw-r--r--src/lib/libcrypto/mem_dbg.c643
-rw-r--r--src/lib/libcrypto/o_dir_test.c34
-rw-r--r--src/lib/libcrypto/o_fips.c15
-rw-r--r--src/lib/libcrypto/o_init.c8
-rw-r--r--src/lib/libcrypto/o_str.c38
-rw-r--r--src/lib/libcrypto/o_str.h2
-rw-r--r--src/lib/libcrypto/o_time.c79
-rw-r--r--src/lib/libcrypto/ossl_typ.h10
-rw-r--r--src/lib/libcrypto/ppccap.c112
-rw-r--r--src/lib/libcrypto/s390xcap.c35
-rw-r--r--src/lib/libcrypto/sparcv9cap.c235
-rw-r--r--src/lib/libcrypto/uid.c30
-rw-r--r--src/lib/libssl/src/crypto/arm_arch.h2
-rw-r--r--src/lib/libssl/src/crypto/armcap.c70
-rw-r--r--src/lib/libssl/src/crypto/buildinf.h8
-rw-r--r--src/lib/libssl/src/crypto/cpt_err.c55
-rw-r--r--src/lib/libssl/src/crypto/cryptlib.c842
-rw-r--r--src/lib/libssl/src/crypto/crypto.h126
-rw-r--r--src/lib/libssl/src/crypto/cversion.c55
-rw-r--r--src/lib/libssl/src/crypto/ebcdic.c218
-rw-r--r--src/lib/libssl/src/crypto/ex_data.c440
-rw-r--r--src/lib/libssl/src/crypto/fips_ers.c2
-rw-r--r--src/lib/libssl/src/crypto/md32_common.h105
-rw-r--r--src/lib/libssl/src/crypto/mem.c443
-rw-r--r--src/lib/libssl/src/crypto/mem_clr.c16
-rw-r--r--src/lib/libssl/src/crypto/mem_dbg.c643
-rw-r--r--src/lib/libssl/src/crypto/o_dir_test.c34
-rw-r--r--src/lib/libssl/src/crypto/o_fips.c15
-rw-r--r--src/lib/libssl/src/crypto/o_init.c8
-rw-r--r--src/lib/libssl/src/crypto/o_str.c38
-rw-r--r--src/lib/libssl/src/crypto/o_str.h2
-rw-r--r--src/lib/libssl/src/crypto/o_time.c79
-rw-r--r--src/lib/libssl/src/crypto/ossl_typ.h10
-rw-r--r--src/lib/libssl/src/crypto/ppccap.c112
-rw-r--r--src/lib/libssl/src/crypto/s390xcap.c35
-rw-r--r--src/lib/libssl/src/crypto/sparcv9cap.c235
-rw-r--r--src/lib/libssl/src/crypto/uid.c30
50 files changed, 3668 insertions, 3578 deletions
diff --git a/src/lib/libcrypto/arm_arch.h b/src/lib/libcrypto/arm_arch.h
index 5a83107680..0ccafb2215 100644
--- a/src/lib/libcrypto/arm_arch.h
+++ b/src/lib/libcrypto/arm_arch.h
@@ -43,7 +43,7 @@
43 43
44#if !__ASSEMBLER__ 44#if !__ASSEMBLER__
45extern unsigned int OPENSSL_armcap_P; 45extern unsigned int OPENSSL_armcap_P;
46 46
47#define ARMV7_NEON (1<<0) 47#define ARMV7_NEON (1<<0)
48#define ARMV7_TICK (1<<1) 48#define ARMV7_TICK (1<<1)
49#endif 49#endif
diff --git a/src/lib/libcrypto/armcap.c b/src/lib/libcrypto/armcap.c
index 9abaf396e5..0625587bdd 100644
--- a/src/lib/libcrypto/armcap.c
+++ b/src/lib/libcrypto/armcap.c
@@ -12,7 +12,8 @@ unsigned int OPENSSL_armcap_P;
12static sigset_t all_masked; 12static sigset_t all_masked;
13 13
14static sigjmp_buf ill_jmp; 14static sigjmp_buf ill_jmp;
15static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); } 15 static void ill_handler (int sig) { siglongjmp(ill_jmp, sig);
16}
16 17
17/* 18/*
18 * Following subroutines could have been inlined, but it's not all 19 * Following subroutines could have been inlined, but it's not all
@@ -21,60 +22,61 @@ static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); }
21void _armv7_neon_probe(void); 22void _armv7_neon_probe(void);
22unsigned int _armv7_tick(void); 23unsigned int _armv7_tick(void);
23 24
24unsigned int OPENSSL_rdtsc(void) 25unsigned int
25 { 26OPENSSL_rdtsc(void)
27{
26 if (OPENSSL_armcap_P & ARMV7_TICK) 28 if (OPENSSL_armcap_P & ARMV7_TICK)
27 return _armv7_tick(); 29 return _armv7_tick();
28 else 30 else
29 return 0; 31 return 0;
30 } 32}
31 33
32#if defined(__GNUC__) && __GNUC__>=2 34#if defined(__GNUC__) && __GNUC__>=2
33void OPENSSL_cpuid_setup(void) __attribute__((constructor)); 35void OPENSSL_cpuid_setup(void) __attribute__((constructor));
34#endif 36#endif
35void OPENSSL_cpuid_setup(void) 37
36 { 38void
39OPENSSL_cpuid_setup(void)
40{
37 char *e; 41 char *e;
38 struct sigaction ill_oact,ill_act; 42 struct sigaction ill_oact, ill_act;
39 sigset_t oset; 43 sigset_t oset;
40 static int trigger=0; 44 static int trigger = 0;
41 45
42 if (trigger) return; 46 if (trigger)
43 trigger=1;
44
45 if ((e=getenv("OPENSSL_armcap")))
46 {
47 OPENSSL_armcap_P=strtoul(e,NULL,0);
48 return; 47 return;
49 } 48 trigger = 1;
49
50 if ((e = getenv("OPENSSL_armcap"))) {
51 OPENSSL_armcap_P = strtoul(e, NULL, 0);
52 return;
53 }
50 54
51 sigfillset(&all_masked); 55 sigfillset(&all_masked);
52 sigdelset(&all_masked,SIGILL); 56 sigdelset(&all_masked, SIGILL);
53 sigdelset(&all_masked,SIGTRAP); 57 sigdelset(&all_masked, SIGTRAP);
54 sigdelset(&all_masked,SIGFPE); 58 sigdelset(&all_masked, SIGFPE);
55 sigdelset(&all_masked,SIGBUS); 59 sigdelset(&all_masked, SIGBUS);
56 sigdelset(&all_masked,SIGSEGV); 60 sigdelset(&all_masked, SIGSEGV);
57 61
58 OPENSSL_armcap_P = 0; 62 OPENSSL_armcap_P = 0;
59 63
60 memset(&ill_act,0,sizeof(ill_act)); 64 memset(&ill_act, 0, sizeof(ill_act));
61 ill_act.sa_handler = ill_handler; 65 ill_act.sa_handler = ill_handler;
62 ill_act.sa_mask = all_masked; 66 ill_act.sa_mask = all_masked;
63 67
64 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset); 68 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
65 sigaction(SIGILL,&ill_act,&ill_oact); 69 sigaction(SIGILL, &ill_act, &ill_oact);
66 70
67 if (sigsetjmp(ill_jmp,1) == 0) 71 if (sigsetjmp(ill_jmp, 1) == 0) {
68 {
69 _armv7_neon_probe(); 72 _armv7_neon_probe();
70 OPENSSL_armcap_P |= ARMV7_NEON; 73 OPENSSL_armcap_P |= ARMV7_NEON;
71 } 74 }
72 if (sigsetjmp(ill_jmp,1) == 0) 75 if (sigsetjmp(ill_jmp, 1) == 0) {
73 {
74 _armv7_tick(); 76 _armv7_tick();
75 OPENSSL_armcap_P |= ARMV7_TICK; 77 OPENSSL_armcap_P |= ARMV7_TICK;
76 }
77
78 sigaction (SIGILL,&ill_oact,NULL);
79 sigprocmask(SIG_SETMASK,&oset,NULL);
80 } 78 }
79
80 sigaction (SIGILL, &ill_oact, NULL);
81 sigprocmask(SIG_SETMASK, &oset, NULL);
82}
diff --git a/src/lib/libcrypto/buildinf.h b/src/lib/libcrypto/buildinf.h
index 957f09d5fd..8fdb24cb33 100644
--- a/src/lib/libcrypto/buildinf.h
+++ b/src/lib/libcrypto/buildinf.h
@@ -1,6 +1,6 @@
1#ifndef MK1MF_BUILD 1#ifndef MK1MF_BUILD
2 /* auto-generated by crypto/Makefile for crypto/cversion.c */ 2/* auto-generated by crypto/Makefile for crypto/cversion.c */
3 #define CFLAGS "cc -O" 3#define CFLAGS "cc -O"
4 #define PLATFORM "dist" 4#define PLATFORM "dist"
5 #define DATE "Sat Aug 21 10:52:09 EST 2010" 5#define DATE "Sat Aug 21 10:52:09 EST 2010"
6#endif 6#endif
diff --git a/src/lib/libcrypto/cpt_err.c b/src/lib/libcrypto/cpt_err.c
index 289005f662..a8e94ccd16 100644
--- a/src/lib/libcrypto/cpt_err.c
+++ b/src/lib/libcrypto/cpt_err.c
@@ -68,38 +68,35 @@
68#define ERR_FUNC(func) ERR_PACK(ERR_LIB_CRYPTO,func,0) 68#define ERR_FUNC(func) ERR_PACK(ERR_LIB_CRYPTO,func,0)
69#define ERR_REASON(reason) ERR_PACK(ERR_LIB_CRYPTO,0,reason) 69#define ERR_REASON(reason) ERR_PACK(ERR_LIB_CRYPTO,0,reason)
70 70
71static ERR_STRING_DATA CRYPTO_str_functs[]= 71static ERR_STRING_DATA CRYPTO_str_functs[]= {
72 { 72 {ERR_FUNC(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX), "CRYPTO_get_ex_new_index"},
73{ERR_FUNC(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX), "CRYPTO_get_ex_new_index"}, 73 {ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID), "CRYPTO_get_new_dynlockid"},
74{ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID), "CRYPTO_get_new_dynlockid"}, 74 {ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_LOCKID), "CRYPTO_get_new_lockid"},
75{ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_LOCKID), "CRYPTO_get_new_lockid"}, 75 {ERR_FUNC(CRYPTO_F_CRYPTO_SET_EX_DATA), "CRYPTO_set_ex_data"},
76{ERR_FUNC(CRYPTO_F_CRYPTO_SET_EX_DATA), "CRYPTO_set_ex_data"}, 76 {ERR_FUNC(CRYPTO_F_DEF_ADD_INDEX), "DEF_ADD_INDEX"},
77{ERR_FUNC(CRYPTO_F_DEF_ADD_INDEX), "DEF_ADD_INDEX"}, 77 {ERR_FUNC(CRYPTO_F_DEF_GET_CLASS), "DEF_GET_CLASS"},
78{ERR_FUNC(CRYPTO_F_DEF_GET_CLASS), "DEF_GET_CLASS"}, 78 {ERR_FUNC(CRYPTO_F_FIPS_MODE_SET), "FIPS_mode_set"},
79{ERR_FUNC(CRYPTO_F_FIPS_MODE_SET), "FIPS_mode_set"}, 79 {ERR_FUNC(CRYPTO_F_INT_DUP_EX_DATA), "INT_DUP_EX_DATA"},
80{ERR_FUNC(CRYPTO_F_INT_DUP_EX_DATA), "INT_DUP_EX_DATA"}, 80 {ERR_FUNC(CRYPTO_F_INT_FREE_EX_DATA), "INT_FREE_EX_DATA"},
81{ERR_FUNC(CRYPTO_F_INT_FREE_EX_DATA), "INT_FREE_EX_DATA"}, 81 {ERR_FUNC(CRYPTO_F_INT_NEW_EX_DATA), "INT_NEW_EX_DATA"},
82{ERR_FUNC(CRYPTO_F_INT_NEW_EX_DATA), "INT_NEW_EX_DATA"}, 82 {0, NULL}
83{0,NULL} 83};
84 };
85 84
86static ERR_STRING_DATA CRYPTO_str_reasons[]= 85static ERR_STRING_DATA CRYPTO_str_reasons[]= {
87 { 86 {ERR_REASON(CRYPTO_R_FIPS_MODE_NOT_SUPPORTED), "fips mode not supported"},
88{ERR_REASON(CRYPTO_R_FIPS_MODE_NOT_SUPPORTED),"fips mode not supported"}, 87 {ERR_REASON(CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK), "no dynlock create callback"},
89{ERR_REASON(CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK),"no dynlock create callback"}, 88 {0, NULL}
90{0,NULL} 89};
91 };
92 90
93#endif 91#endif
94 92
95void ERR_load_CRYPTO_strings(void) 93void
96 { 94ERR_load_CRYPTO_strings(void)
95{
97#ifndef OPENSSL_NO_ERR 96#ifndef OPENSSL_NO_ERR
98 97 if (ERR_func_error_string(CRYPTO_str_functs[0].error) == NULL) {
99 if (ERR_func_error_string(CRYPTO_str_functs[0].error) == NULL) 98 ERR_load_strings(0, CRYPTO_str_functs);
100 { 99 ERR_load_strings(0, CRYPTO_str_reasons);
101 ERR_load_strings(0,CRYPTO_str_functs);
102 ERR_load_strings(0,CRYPTO_str_reasons);
103 }
104#endif
105 } 100 }
101#endif
102}
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)
121static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */ 121static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
122#endif 122#endif
123 123
124DECLARE_STACK_OF(CRYPTO_dynlock) 124DECLARE_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 */
127static const char* const lock_names[CRYPTO_NUM_LOCKS] = 127static 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. */
177static STACK_OF(OPENSSL_STRING) *app_locks=NULL; 176static 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. */
182static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL; 181static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
183
184 182
185static void (*locking_callback)(int mode,int type, 183static void (*locking_callback)(int mode, int type,
186 const char *file,int line)=0; 184 const char *file, int line) = 0;
187static int (*add_lock_callback)(int *pointer,int amount, 185static 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
190static unsigned long (*id_callback)(void)=0; 188static unsigned long (*id_callback)(void) = 0;
191#endif 189#endif
192static void (*threadid_callback)(CRYPTO_THREADID *)=0; 190static void (*threadid_callback)(CRYPTO_THREADID *) = 0;
193static struct CRYPTO_dynlock_value *(*dynlock_create_callback) 191static struct CRYPTO_dynlock_value *(*dynlock_create_callback)(
194 (const char *file,int line)=0; 192 const char *file, int line) = 0;
195static void (*dynlock_lock_callback)(int mode, 193static 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;
197static void (*dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l, 195static 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
200int CRYPTO_get_new_lockid(char *name) 198int
201 { 199CRYPTO_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
231int CRYPTO_num_locks(void) 228int
232 { 229CRYPTO_num_locks(void)
230{
233 return CRYPTO_NUM_LOCKS; 231 return CRYPTO_NUM_LOCKS;
234 } 232}
235 233
236int CRYPTO_get_new_dynlockid(void) 234int
237 { 235CRYPTO_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
297void CRYPTO_destroy_dynlockid(int i) 290void
298 { 291CRYPTO_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
340struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i) 327struct 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
360struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void)) 348struct 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
366void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, 354void (*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
372void (*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
378void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func) 360void (*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
384void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode, 366void
385 struct CRYPTO_dynlock_value *l, const char *file, int line)) 367CRYPTO_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
390void CRYPTO_set_dynlock_destroy_callback(void (*func) 373void
391 (struct CRYPTO_dynlock_value *l, const char *file, int line)) 374CRYPTO_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
380void
381CRYPTO_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
397void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file, 387void (*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
403int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type, 393int (*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
409void CRYPTO_set_locking_callback(void (*func)(int mode,int type, 399void
410 const char *file,int line)) 400CRYPTO_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
419void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type, 410void
420 const char *file,int line)) 411CRYPTO_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. */
428void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) 420void
429 { 421CRYPTO_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
434static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; 427static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
435void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) 428void
436 { 429CRYPTO_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
470int CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *)) 462int
471 { 463CRYPTO_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
478void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *) 471void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *)
479 { 472{
480 return threadid_callback; 473 return threadid_callback;
481 } 474}
482 475
483void CRYPTO_THREADID_current(CRYPTO_THREADID *id) 476void
484 { 477CRYPTO_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
511int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b) 503int
512 { 504CRYPTO_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
516void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src) 509void
517 { 510CRYPTO_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
521unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id) 515unsigned long
522 { 516CRYPTO_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
527unsigned long (*CRYPTO_get_id_callback(void))(void) 522unsigned long (*CRYPTO_get_id_callback(void))(void)
528 { 523{
529 return(id_callback); 524 return (id_callback);
530 } 525}
531 526
532void CRYPTO_set_id_callback(unsigned long (*func)(void)) 527void
533 { 528CRYPTO_set_id_callback(unsigned long (*func)(void))
534 id_callback=func; 529{
535 } 530 id_callback = func;
531}
536 532
537unsigned long CRYPTO_thread_id(void) 533unsigned long
538 { 534CRYPTO_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
561void CRYPTO_lock(int mode, int type, const char *file, int line) 556void
562 { 557CRYPTO_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
607int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file, 599int
608 int line) 600CRYPTO_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
652const char *CRYPTO_get_lock_name(int type) 642const 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
668unsigned int OPENSSL_ia32cap_P[2]; 659unsigned int OPENSSL_ia32cap_P[2];
669unsigned long *OPENSSL_ia32cap_loc(void) 660unsigned 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
685typedef unsigned long long IA32CAP; 678typedef unsigned long long IA32CAP;
686#endif 679#endif
687void OPENSSL_cpuid_setup(void) 680void
688{ static int trigger=0; 681OPENSSL_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
719unsigned long *OPENSSL_ia32cap_loc(void) { return NULL; } 715 unsigned long *OPENSSL_ia32cap_loc(void) { return NULL;
716}
720#endif 717#endif
721int OPENSSL_NONPIC_relocated = 0; 718int OPENSSL_NONPIC_relocated = 0;
722#if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) 719#if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
723void OPENSSL_cpuid_setup(void) {} 720void
721OPENSSL_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
737BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, 735BOOL WINAPI
738 LPVOID lpvReserved) 736DllMain(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
786int OPENSSL_isservice(void) 782int
787{ HWINSTA h; 783OPENSSL_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
832int OPENSSL_isservice(void) { return 0; } 836 int OPENSSL_isservice(void) { return 0;
837}
833#endif 838#endif
834 839
835void OPENSSL_showfatal (const char *fmta,...) 840void 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
903void OPENSSL_showfatal (const char *fmta,...) 921void 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
930int OPENSSL_isservice(void)
931{
932 return 0;
909} 933}
910int OPENSSL_isservice (void) { return 0; }
911#endif 934#endif
912 935
913void OpenSSLDie(const char *file,int line,const char *assertion) 936void
914 { 937OpenSSLDie(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
927void *OPENSSL_stderr(void) { return stderr; } 951void *OPENSSL_stderr(void)
952{
953 return stderr;
954}
928 955
929int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) 956int
930 { 957CRYPTO_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}
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
index 76bc5958c0..baeba25b99 100644
--- a/src/lib/libcrypto/crypto.h
+++ b/src/lib/libcrypto/crypto.h
@@ -154,23 +154,22 @@ extern "C" {
154typedef struct crypto_ex_data_st CRYPTO_EX_DATA; 154typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
155/* Called when a new object is created */ 155/* Called when a new object is created */
156typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 156typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
157 int idx, long argl, void *argp); 157 int idx, long argl, void *argp);
158/* Called when an object is free()ed */ 158/* Called when an object is free()ed */
159typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 159typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
160 int idx, long argl, void *argp); 160 int idx, long argl, void *argp);
161/* Called when we need to dup an object */ 161/* Called when we need to dup an object */
162typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, void *from_d, 162typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
163 int idx, long argl, void *argp); 163 void *from_d, int idx, long argl, void *argp);
164#endif 164#endif
165 165
166/* A generic structure to pass assorted data in a expandable way */ 166/* A generic structure to pass assorted data in a expandable way */
167typedef struct openssl_item_st 167typedef struct openssl_item_st {
168 {
169 int code; 168 int code;
170 void *value; /* Not used for flag attributes */ 169 void *value; /* Not used for flag attributes */
171 size_t value_size; /* Max size of value for output, length for input */ 170 size_t value_size; /* Max size of value for output, length for input */
172 size_t *value_length; /* Returned length of value for output */ 171 size_t *value_length; /* Returned length of value for output */
173 } OPENSSL_ITEM; 172} OPENSSL_ITEM;
174 173
175 174
176/* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock 175/* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock
@@ -249,11 +248,10 @@ typedef struct openssl_item_st
249 and deallocate locks in a dynamic fashion. The following typedef 248 and deallocate locks in a dynamic fashion. The following typedef
250 makes this possible in a type-safe manner. */ 249 makes this possible in a type-safe manner. */
251/* struct CRYPTO_dynlock_value has to be defined by the application. */ 250/* struct CRYPTO_dynlock_value has to be defined by the application. */
252typedef struct 251typedef struct {
253 {
254 int references; 252 int references;
255 struct CRYPTO_dynlock_value *data; 253 struct CRYPTO_dynlock_value *data;
256 } CRYPTO_dynlock; 254} CRYPTO_dynlock;
257 255
258 256
259/* The following can be used to detect memory leaks in the SSLeay library. 257/* The following can be used to detect memory leaks in the SSLeay library.
@@ -278,24 +276,22 @@ typedef struct
278/* predec of the BIO type */ 276/* predec of the BIO type */
279typedef struct bio_st BIO_dummy; 277typedef struct bio_st BIO_dummy;
280 278
281struct crypto_ex_data_st 279struct crypto_ex_data_st {
282 {
283 STACK_OF(void) *sk; 280 STACK_OF(void) *sk;
284 int dummy; /* gcc is screwing up this data structure :-( */ 281 int dummy; /* gcc is screwing up this data structure :-( */
285 }; 282};
286DECLARE_STACK_OF(void) 283DECLARE_STACK_OF(void)
287 284
288/* This stuff is basically class callback functions 285/* This stuff is basically class callback functions
289 * The current classes are SSL_CTX, SSL, SSL_SESSION, and a few more */ 286 * The current classes are SSL_CTX, SSL, SSL_SESSION, and a few more */
290 287
291typedef struct crypto_ex_data_func_st 288typedef struct crypto_ex_data_func_st {
292 {
293 long argl; /* Arbitary long */ 289 long argl; /* Arbitary long */
294 void *argp; /* Arbitary void * */ 290 void *argp; /* Arbitary void * */
295 CRYPTO_EX_new *new_func; 291 CRYPTO_EX_new *new_func;
296 CRYPTO_EX_free *free_func; 292 CRYPTO_EX_free *free_func;
297 CRYPTO_EX_dup *dup_func; 293 CRYPTO_EX_dup *dup_func;
298 } CRYPTO_EX_DATA_FUNCS; 294} CRYPTO_EX_DATA_FUNCS;
299 295
300DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS) 296DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
301 297
@@ -392,18 +388,18 @@ int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i);
392int CRYPTO_ex_data_new_class(void); 388int CRYPTO_ex_data_new_class(void);
393/* Within a given class, get/register a new index */ 389/* Within a given class, get/register a new index */
394int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, 390int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
395 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 391 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
396 CRYPTO_EX_free *free_func); 392 CRYPTO_EX_free *free_func);
397/* Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a given 393/* Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a given
398 * class (invokes whatever per-class callbacks are applicable) */ 394 * class (invokes whatever per-class callbacks are applicable) */
399int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad); 395int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
400int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 396int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
401 CRYPTO_EX_DATA *from); 397 CRYPTO_EX_DATA *from);
402void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad); 398void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
403/* Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular index 399/* Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular index
404 * (relative to the class type involved) */ 400 * (relative to the class type involved) */
405int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val); 401int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val);
406void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad,int idx); 402void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx);
407/* This function cleans up all "ex_data" state. It mustn't be called under 403/* This function cleans up all "ex_data" state. It mustn't be called under
408 * potential race-conditions. */ 404 * potential race-conditions. */
409void CRYPTO_cleanup_all_ex_data(void); 405void CRYPTO_cleanup_all_ex_data(void);
@@ -411,22 +407,21 @@ void CRYPTO_cleanup_all_ex_data(void);
411int CRYPTO_get_new_lockid(char *name); 407int CRYPTO_get_new_lockid(char *name);
412 408
413int CRYPTO_num_locks(void); /* return CRYPTO_NUM_LOCKS (shared libs!) */ 409int CRYPTO_num_locks(void); /* return CRYPTO_NUM_LOCKS (shared libs!) */
414void CRYPTO_lock(int mode, int type,const char *file,int line); 410void CRYPTO_lock(int mode, int type, const char *file, int line);
415void CRYPTO_set_locking_callback(void (*func)(int mode,int type, 411void CRYPTO_set_locking_callback(void (*func)(int mode, int type,
416 const char *file,int line)); 412 const char *file, int line));
417void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file, 413void (*CRYPTO_get_locking_callback(void))(int mode, int type,
418 int line); 414 const char *file, int line);
419void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type, 415void CRYPTO_set_add_lock_callback(int (*func)(int *num, int mount, int type,
420 const char *file, int line)); 416 const char *file, int line));
421int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type, 417int (*CRYPTO_get_add_lock_callback(void))(int *num, int mount, int type,
422 const char *file,int line); 418 const char *file, int line);
423 419
424/* Don't use this structure directly. */ 420/* Don't use this structure directly. */
425typedef struct crypto_threadid_st 421typedef struct crypto_threadid_st {
426 {
427 void *ptr; 422 void *ptr;
428 unsigned long val; 423 unsigned long val;
429 } CRYPTO_THREADID; 424} CRYPTO_THREADID;
430/* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */ 425/* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */
431void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val); 426void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val);
432void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr); 427void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
@@ -443,8 +438,8 @@ unsigned long CRYPTO_thread_id(void);
443#endif 438#endif
444 439
445const char *CRYPTO_get_lock_name(int type); 440const char *CRYPTO_get_lock_name(int type);
446int CRYPTO_add_lock(int *pointer,int amount,int type, const char *file, 441int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
447 int line); 442 int line);
448 443
449int CRYPTO_get_new_dynlockid(void); 444int CRYPTO_get_new_dynlockid(void);
450void CRYPTO_destroy_dynlockid(int i); 445void CRYPTO_destroy_dynlockid(int i);
@@ -452,46 +447,43 @@ struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i);
452void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file, int line)); 447void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file, int line));
453void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)); 448void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line));
454void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l, const char *file, int line)); 449void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l, const char *file, int line));
455struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))(const char *file,int line); 450struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))(const char *file, int line);
456void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, struct CRYPTO_dynlock_value *l, const char *file,int line); 451void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line);
457void (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l, const char *file,int line); 452void (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l, const char *file, int line);
458 453
459/* CRYPTO_set_mem_functions includes CRYPTO_set_locked_mem_functions -- 454/* CRYPTO_set_mem_functions includes CRYPTO_set_locked_mem_functions --
460 * call the latter last if you need different functions */ 455 * call the latter last if you need different functions */
461int CRYPTO_set_mem_functions(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *)); 456int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), void (*f)(void *));
462int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*free_func)(void *)); 457int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*free_func)(void *));
463int CRYPTO_set_mem_ex_functions(void *(*m)(size_t,const char *,int), 458int CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int),
464 void *(*r)(void *,size_t,const char *,int), 459 void *(*r)(void *, size_t, const char *, int), void (*f)(void *));
465 void (*f)(void *)); 460int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int),
466int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t,const char *,int), 461 void (*free_func)(void *));
467 void (*free_func)(void *)); 462int CRYPTO_set_mem_debug_functions(
468int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), 463 void (*m)(void *, int, const char *, int, int),
469 void (*r)(void *,void *,int,const char *,int,int), 464 void (*r)(void *, void *, int, const char *, int, int),
470 void (*f)(void *,int), 465 void (*f)(void *, int), void (*so)(long), long (*go)(void));
471 void (*so)(long), 466void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
472 long (*go)(void)); 467 void (**f)(void *));
473void CRYPTO_get_mem_functions(void *(**m)(size_t),void *(**r)(void *, size_t), void (**f)(void *));
474void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)); 468void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *));
475void CRYPTO_get_mem_ex_functions(void *(**m)(size_t,const char *,int), 469void CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int),
476 void *(**r)(void *, size_t,const char *,int), 470 void *(**r)(void *, size_t, const char *, int), void (**f)(void *));
477 void (**f)(void *)); 471void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int),
478void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t,const char *,int), 472 void (**f)(void *));
479 void (**f)(void *)); 473void CRYPTO_get_mem_debug_functions(
480void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), 474 void (**m)(void *, int, const char *, int, int),
481 void (**r)(void *,void *,int,const char *,int,int), 475 void (**r)(void *, void *, int, const char *, int, int),
482 void (**f)(void *,int), 476 void (**f)(void *, int), void (**so)(long), long (**go)(void));
483 void (**so)(long),
484 long (**go)(void));
485 477
486void *CRYPTO_malloc_locked(int num, const char *file, int line); 478void *CRYPTO_malloc_locked(int num, const char *file, int line);
487void CRYPTO_free_locked(void *ptr); 479void CRYPTO_free_locked(void *ptr);
488void *CRYPTO_malloc(int num, const char *file, int line); 480void *CRYPTO_malloc(int num, const char *file, int line);
489char *CRYPTO_strdup(const char *str, const char *file, int line); 481char *CRYPTO_strdup(const char *str, const char *file, int line);
490void CRYPTO_free(void *ptr); 482void CRYPTO_free(void *ptr);
491void *CRYPTO_realloc(void *addr,int num, const char *file, int line); 483void *CRYPTO_realloc(void *addr, int num, const char *file, int line);
492void *CRYPTO_realloc_clean(void *addr,int old_num,int num,const char *file, 484void *CRYPTO_realloc_clean(void *addr, int old_num, int num,
493 int line); 485 const char *file, int line);
494void *CRYPTO_remalloc(void *addr,int num, const char *file, int line); 486void *CRYPTO_remalloc(void *addr, int num, const char *file, int line);
495 487
496void OPENSSL_cleanse(void *ptr, size_t len); 488void OPENSSL_cleanse(void *ptr, size_t len);
497 489
@@ -512,9 +504,9 @@ int CRYPTO_remove_all_info(void);
512 * 0: called before the actual memory allocation has taken place 504 * 0: called before the actual memory allocation has taken place
513 * 1: called after the actual memory allocation has taken place 505 * 1: called after the actual memory allocation has taken place
514 */ 506 */
515void CRYPTO_dbg_malloc(void *addr,int num,const char *file,int line,int before_p); 507void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, int before_p);
516void CRYPTO_dbg_realloc(void *addr1,void *addr2,int num,const char *file,int line,int before_p); 508void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, const char *file, int line, int before_p);
517void CRYPTO_dbg_free(void *addr,int before_p); 509void CRYPTO_dbg_free(void *addr, int before_p);
518/* Tell the debugging code about options. By default, the following values 510/* Tell the debugging code about options. By default, the following values
519 * apply: 511 * apply:
520 * 512 *
@@ -536,7 +528,7 @@ typedef void *CRYPTO_MEM_LEAK_CB(unsigned long, const char *, int, int, void *);
536void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb); 528void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb);
537 529
538/* die if we have to */ 530/* die if we have to */
539void OpenSSLDie(const char *file,int line,const char *assertion); 531void OpenSSLDie(const char *file, int line, const char *assertion);
540#define OPENSSL_assert(e) (void)((e) ? 0 : (OpenSSLDie(__FILE__, __LINE__, #e),1)) 532#define OPENSSL_assert(e) (void)((e) ? 0 : (OpenSSLDie(__FILE__, __LINE__, #e),1))
541 533
542unsigned long *OPENSSL_ia32cap_loc(void); 534unsigned long *OPENSSL_ia32cap_loc(void);
diff --git a/src/lib/libcrypto/cversion.c b/src/lib/libcrypto/cversion.c
index ea9f25fd16..1fe92a4bfa 100644
--- a/src/lib/libcrypto/cversion.c
+++ b/src/lib/libcrypto/cversion.c
@@ -62,56 +62,53 @@
62#include "buildinf.h" 62#include "buildinf.h"
63#endif 63#endif
64 64
65const char *SSLeay_version(int t) 65const char
66 { 66*SSLeay_version(int t)
67{
67 if (t == SSLEAY_VERSION) 68 if (t == SSLEAY_VERSION)
68 return OPENSSL_VERSION_TEXT; 69 return OPENSSL_VERSION_TEXT;
69 if (t == SSLEAY_BUILT_ON) 70 if (t == SSLEAY_BUILT_ON) {
70 {
71#ifdef DATE 71#ifdef DATE
72 static char buf[sizeof(DATE)+11]; 72 static char buf[sizeof(DATE) + 11];
73 73
74 BIO_snprintf(buf,sizeof buf,"built on: %s",DATE); 74 BIO_snprintf(buf, sizeof buf, "built on: %s", DATE);
75 return(buf); 75 return (buf);
76#else 76#else
77 return("built on: date not available"); 77 return("built on: date not available");
78#endif 78#endif
79 } 79 }
80 if (t == SSLEAY_CFLAGS) 80 if (t == SSLEAY_CFLAGS) {
81 {
82#ifdef CFLAGS 81#ifdef CFLAGS
83 static char buf[sizeof(CFLAGS)+11]; 82 static char buf[sizeof(CFLAGS) + 11];
84 83
85 BIO_snprintf(buf,sizeof buf,"compiler: %s",CFLAGS); 84 BIO_snprintf(buf, sizeof buf, "compiler: %s", CFLAGS);
86 return(buf); 85 return (buf);
87#else 86#else
88 return("compiler: information not available"); 87 return("compiler: information not available");
89#endif 88#endif
90 } 89 }
91 if (t == SSLEAY_PLATFORM) 90 if (t == SSLEAY_PLATFORM) {
92 {
93#ifdef PLATFORM 91#ifdef PLATFORM
94 static char buf[sizeof(PLATFORM)+11]; 92 static char buf[sizeof(PLATFORM) + 11];
95 93
96 BIO_snprintf(buf,sizeof buf,"platform: %s", PLATFORM); 94 BIO_snprintf(buf, sizeof buf, "platform: %s", PLATFORM);
97 return(buf); 95 return (buf);
98#else 96#else
99 return("platform: information not available"); 97 return("platform: information not available");
100#endif 98#endif
101 } 99 }
102 if (t == SSLEAY_DIR) 100 if (t == SSLEAY_DIR) {
103 {
104#ifdef OPENSSLDIR 101#ifdef OPENSSLDIR
105 return "OPENSSLDIR: \"" OPENSSLDIR "\""; 102 return "OPENSSLDIR: \"" OPENSSLDIR "\"";
106#else 103#else
107 return "OPENSSLDIR: N/A"; 104 return "OPENSSLDIR: N/A";
108#endif 105#endif
109 }
110 return("not available");
111 }
112
113unsigned long SSLeay(void)
114 {
115 return(SSLEAY_VERSION_NUMBER);
116 } 106 }
107 return("not available");
108}
117 109
110unsigned long
111SSLeay(void)
112{
113 return (SSLEAY_VERSION_NUMBER);
114}
diff --git a/src/lib/libcrypto/ebcdic.c b/src/lib/libcrypto/ebcdic.c
index 43e53bcaf7..2ac26abc72 100644
--- a/src/lib/libcrypto/ebcdic.c
+++ b/src/lib/libcrypto/ebcdic.c
@@ -4,7 +4,7 @@
4 4
5#include <openssl/e_os2.h> 5#include <openssl/e_os2.h>
6#if defined(PEDANTIC) || defined(__DECC) || defined(OPENSSL_SYS_MACOSX) 6#if defined(PEDANTIC) || defined(__DECC) || defined(OPENSSL_SYS_MACOSX)
7static void *dummy=&dummy; 7static void *dummy = &dummy;
8#endif 8#endif
9 9
10#else /*CHARSET_EBCDIC*/ 10#else /*CHARSET_EBCDIC*/
@@ -30,38 +30,38 @@ static void *dummy=&dummy;
30 30
31/* Here's the bijective ebcdic-to-ascii table: */ 31/* Here's the bijective ebcdic-to-ascii table: */
32const unsigned char os_toascii[256] = { 32const unsigned char os_toascii[256] = {
33/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, 33/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
34 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/ 34 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/
35/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, 35/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
36 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/ 36 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/
37/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, 37/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
38 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/ 38 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/
39/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 39/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
40 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/ 40 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/
41/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, 41/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
42 0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/ 42 0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/
43/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, 43/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
44 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/ 44 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/
45/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, 45/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
46 0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/ 46 0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/
47/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, 47/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
48 0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/ 48 0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/
49/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 49/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
50 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/ 50 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/
51/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 51/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
52 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/ 52 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/
53/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 53/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
54 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/ 54 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/
55/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, 55/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
56 0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/ 56 0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/
57/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 57/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
58 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/ 58 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/
59/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 59/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
60 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/ 60 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/
61/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 61/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
62 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/ 62 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/
63/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 63/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
64 0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e /*0123456789.{.}.~*/ 64 0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e /*0123456789.{.}.~*/
65}; 65};
66 66
67 67
@@ -111,38 +111,38 @@ Bijective EBCDIC (character set IBM-1047) to US-ASCII table:
111This table is bijective - there are no ambigous or duplicate characters. 111This table is bijective - there are no ambigous or duplicate characters.
112*/ 112*/
113const unsigned char os_toascii[256] = { 113const unsigned char os_toascii[256] = {
114 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, /* 00-0f: */ 114 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, /* 00-0f: */
115 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */ 115 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
116 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, /* 10-1f: */ 116 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, /* 10-1f: */
117 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */ 117 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
118 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, /* 20-2f: */ 118 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, /* 20-2f: */
119 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /* ................ */ 119 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /* ................ */
120 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, /* 30-3f: */ 120 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, /* 30-3f: */
121 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /* ................ */ 121 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /* ................ */
122 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, /* 40-4f: */ 122 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, /* 40-4f: */
123 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* ...........<(+| */ 123 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* ...........<(+| */
124 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, /* 50-5f: */ 124 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, /* 50-5f: */
125 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, /* &.........!$*);^ */ 125 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, /* &.........!$*);^ */
126 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, /* 60-6f: */ 126 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, /* 60-6f: */
127 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /* -/.........,%_>? */ 127 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /* -/.........,%_>? */
128 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, /* 70-7f: */ 128 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, /* 70-7f: */
129 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /* .........`:#@'=" */ 129 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /* .........`:#@'=" */
130 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 80-8f: */ 130 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 80-8f: */
131 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /* .abcdefghi...... */ 131 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /* .abcdefghi...... */
132 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, /* 90-9f: */ 132 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, /* 90-9f: */
133 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /* .jklmnopqr...... */ 133 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /* .jklmnopqr...... */
134 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, /* a0-af: */ 134 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, /* a0-af: */
135 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, /* .~stuvwxyz...[.. */ 135 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, /* .~stuvwxyz...[.. */
136 0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, /* b0-bf: */ 136 0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, /* b0-bf: */
137 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, /* .............].. */ 137 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, /* .............].. */
138 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* c0-cf: */ 138 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* c0-cf: */
139 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /* {ABCDEFGHI...... */ 139 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /* {ABCDEFGHI...... */
140 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* d0-df: */ 140 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* d0-df: */
141 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, /* }JKLMNOPQR...... */ 141 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, /* }JKLMNOPQR...... */
142 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, /* e0-ef: */ 142 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, /* e0-ef: */
143 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /* \.STUVWXYZ...... */ 143 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /* \.STUVWXYZ...... */
144 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* f0-ff: */ 144 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* f0-ff: */
145 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f /* 0123456789...... */ 145 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f /* 0123456789...... */
146}; 146};
147 147
148 148
@@ -151,38 +151,38 @@ The US-ASCII to EBCDIC (character set IBM-1047) table:
151This table is bijective (no ambiguous or duplicate characters) 151This table is bijective (no ambiguous or duplicate characters)
152*/ 152*/
153const unsigned char os_toebcdic[256] = { 153const unsigned char os_toebcdic[256] = {
154 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, /* 00-0f: */ 154 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, /* 00-0f: */
155 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */ 155 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
156 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, /* 10-1f: */ 156 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, /* 10-1f: */
157 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */ 157 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
158 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, /* 20-2f: */ 158 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, /* 20-2f: */
159 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, /* !"#$%&'()*+,-./ */ 159 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, /* !"#$%&'()*+,-./ */
160 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 30-3f: */ 160 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 30-3f: */
161 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, /* 0123456789:;<=>? */ 161 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, /* 0123456789:;<=>? */
162 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 40-4f: */ 162 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 40-4f: */
163 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, /* @ABCDEFGHIJKLMNO */ 163 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, /* @ABCDEFGHIJKLMNO */
164 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, /* 50-5f: */ 164 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, /* 50-5f: */
165 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, /* PQRSTUVWXYZ[\]^_ */ 165 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, /* PQRSTUVWXYZ[\]^_ */
166 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 60-6f: */ 166 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 60-6f: */
167 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* `abcdefghijklmno */ 167 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* `abcdefghijklmno */
168 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, /* 70-7f: */ 168 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, /* 70-7f: */
169 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, /* pqrstuvwxyz{|}~. */ 169 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, /* pqrstuvwxyz{|}~. */
170 0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08, /* 80-8f: */ 170 0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08, /* 80-8f: */
171 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14, /* ................ */ 171 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14, /* ................ */
172 0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17, /* 90-9f: */ 172 0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17, /* 90-9f: */
173 0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0xff, /* ................ */ 173 0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0xff, /* ................ */
174 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, /* a0-af: */ 174 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, /* a0-af: */
175 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, /* ................ */ 175 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, /* ................ */
176 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, /* b0-bf: */ 176 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, /* b0-bf: */
177 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, /* ................ */ 177 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, /* ................ */
178 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, /* c0-cf: */ 178 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, /* c0-cf: */
179 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, /* ................ */ 179 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, /* ................ */
180 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, /* d0-df: */ 180 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, /* d0-df: */
181 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, /* ................ */ 181 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, /* ................ */
182 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, /* e0-ef: */ 182 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, /* e0-ef: */
183 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* ................ */ 183 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* ................ */
184 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, /* f0-ff: */ 184 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, /* f0-ff: */
185 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf /* ................ */ 185 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf /* ................ */
186}; 186};
187#endif /*_OSD_POSIX*/ 187#endif /*_OSD_POSIX*/
188 188
@@ -195,27 +195,27 @@ const unsigned char os_toebcdic[256] = {
195void * 195void *
196ebcdic2ascii(void *dest, const void *srce, size_t count) 196ebcdic2ascii(void *dest, const void *srce, size_t count)
197{ 197{
198 unsigned char *udest = dest; 198 unsigned char *udest = dest;
199 const unsigned char *usrce = srce; 199 const unsigned char *usrce = srce;
200 200
201 while (count-- != 0) { 201 while (count-- != 0) {
202 *udest++ = os_toascii[*usrce++]; 202 *udest++ = os_toascii[*usrce++];
203 } 203 }
204 204
205 return dest; 205 return dest;
206} 206}
207 207
208void * 208void *
209ascii2ebcdic(void *dest, const void *srce, size_t count) 209ascii2ebcdic(void *dest, const void *srce, size_t count)
210{ 210{
211 unsigned char *udest = dest; 211 unsigned char *udest = dest;
212 const unsigned char *usrce = srce; 212 const unsigned char *usrce = srce;
213 213
214 while (count-- != 0) { 214 while (count-- != 0) {
215 *udest++ = os_toebcdic[*usrce++]; 215 *udest++ = os_toebcdic[*usrce++];
216 } 216 }
217 217
218 return dest; 218 return dest;
219} 219}
220 220
221#endif 221#endif
diff --git a/src/lib/libcrypto/ex_data.c b/src/lib/libcrypto/ex_data.c
index e2bc8298d0..44bad59527 100644
--- a/src/lib/libcrypto/ex_data.c
+++ b/src/lib/libcrypto/ex_data.c
@@ -142,8 +142,7 @@
142#include <openssl/lhash.h> 142#include <openssl/lhash.h>
143 143
144/* What an "implementation of ex_data functionality" looks like */ 144/* What an "implementation of ex_data functionality" looks like */
145struct st_CRYPTO_EX_DATA_IMPL 145struct st_CRYPTO_EX_DATA_IMPL {
146 {
147 /*********************/ 146 /*********************/
148 /* GLOBAL OPERATIONS */ 147 /* GLOBAL OPERATIONS */
149 /* Return a new class index */ 148 /* Return a new class index */
@@ -154,79 +153,83 @@ struct st_CRYPTO_EX_DATA_IMPL
154 /* PER-CLASS OPERATIONS */ 153 /* PER-CLASS OPERATIONS */
155 /* Get a new method index within a class */ 154 /* Get a new method index within a class */
156 int (*cb_get_new_index)(int class_index, long argl, void *argp, 155 int (*cb_get_new_index)(int class_index, long argl, void *argp,
157 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 156 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
158 CRYPTO_EX_free *free_func); 157 CRYPTO_EX_free *free_func);
159 /* Initialise a new CRYPTO_EX_DATA of a given class */ 158 /* Initialise a new CRYPTO_EX_DATA of a given class */
160 int (*cb_new_ex_data)(int class_index, void *obj, 159 int (*cb_new_ex_data)(int class_index, void *obj,
161 CRYPTO_EX_DATA *ad); 160 CRYPTO_EX_DATA *ad);
162 /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */ 161 /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
163 int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to, 162 int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to,
164 CRYPTO_EX_DATA *from); 163 CRYPTO_EX_DATA *from);
165 /* Cleanup a CRYPTO_EX_DATA of a given class */ 164 /* Cleanup a CRYPTO_EX_DATA of a given class */
166 void (*cb_free_ex_data)(int class_index, void *obj, 165 void (*cb_free_ex_data)(int class_index, void *obj,
167 CRYPTO_EX_DATA *ad); 166 CRYPTO_EX_DATA *ad);
168 }; 167};
169 168
170/* The implementation we use at run-time */ 169/* The implementation we use at run-time */
171static const CRYPTO_EX_DATA_IMPL *impl = NULL; 170static const CRYPTO_EX_DATA_IMPL *impl = NULL;
172 171
173/* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg. 172/* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg.
174 * EX_IMPL(get_new_index)(...); */ 173 * EX_IMPL(get_new_index)(...);
174*/
175#define EX_IMPL(a) impl->cb_##a 175#define EX_IMPL(a) impl->cb_##a
176 176
177/* Predeclare the "default" ex_data implementation */ 177/* Predeclare the "default" ex_data implementation */
178static int int_new_class(void); 178static int int_new_class(void);
179static void int_cleanup(void); 179static void int_cleanup(void);
180static int int_get_new_index(int class_index, long argl, void *argp, 180static int int_get_new_index(int class_index, long argl, void *argp,
181 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 181 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
182 CRYPTO_EX_free *free_func); 182 CRYPTO_EX_free *free_func);
183static int int_new_ex_data(int class_index, void *obj, 183static int int_new_ex_data(int class_index, void *obj,
184 CRYPTO_EX_DATA *ad); 184 CRYPTO_EX_DATA *ad);
185static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 185static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
186 CRYPTO_EX_DATA *from); 186 CRYPTO_EX_DATA *from);
187static void int_free_ex_data(int class_index, void *obj, 187static void int_free_ex_data(int class_index, void *obj,
188 CRYPTO_EX_DATA *ad); 188 CRYPTO_EX_DATA *ad);
189static CRYPTO_EX_DATA_IMPL impl_default = 189
190 { 190static CRYPTO_EX_DATA_IMPL impl_default = {
191 int_new_class, 191 int_new_class,
192 int_cleanup, 192 int_cleanup,
193 int_get_new_index, 193 int_get_new_index,
194 int_new_ex_data, 194 int_new_ex_data,
195 int_dup_ex_data, 195 int_dup_ex_data,
196 int_free_ex_data 196 int_free_ex_data
197 }; 197};
198 198
199/* Internal function that checks whether "impl" is set and if not, sets it to 199/* Internal function that checks whether "impl" is set and if not, sets it to
200 * the default. */ 200 * the default. */
201static void impl_check(void) 201static void
202 { 202impl_check(void)
203{
203 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 204 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
204 if(!impl) 205 if (!impl)
205 impl = &impl_default; 206 impl = &impl_default;
206 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 207 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
207 } 208}
208/* A macro wrapper for impl_check that first uses a non-locked test before 209/* A macro wrapper for impl_check that first uses a non-locked test before
209 * invoking the function (which checks again inside a lock). */ 210 * invoking the function (which checks again inside a lock). */
210#define IMPL_CHECK if(!impl) impl_check(); 211#define IMPL_CHECK if(!impl) impl_check();
211 212
212/* API functions to get/set the "ex_data" implementation */ 213/* API functions to get/set the "ex_data" implementation */
213const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void) 214const CRYPTO_EX_DATA_IMPL
214 { 215*CRYPTO_get_ex_data_implementation(void)
216{
215 IMPL_CHECK 217 IMPL_CHECK
216 return impl; 218 return impl;
217 } 219}
218int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i) 220
219 { 221int
222CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
223{
220 int toret = 0; 224 int toret = 0;
221 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 225 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
222 if(!impl) 226 if (!impl) {
223 {
224 impl = i; 227 impl = i;
225 toret = 1; 228 toret = 1;
226 } 229 }
227 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 230 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
228 return toret; 231 return toret;
229 } 232}
230 233
231/****************************************************************************/ 234/****************************************************************************/
232/* Interal (default) implementation of "ex_data" support. API functions are 235/* Interal (default) implementation of "ex_data" support. API functions are
@@ -249,288 +252,283 @@ DECLARE_LHASH_OF(EX_CLASS_ITEM);
249static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL; 252static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL;
250 253
251/* The callbacks required in the "ex_data" hash table */ 254/* The callbacks required in the "ex_data" hash table */
252static unsigned long ex_class_item_hash(const EX_CLASS_ITEM *a) 255static unsigned long
253 { 256ex_class_item_hash(const EX_CLASS_ITEM *a)
257{
254 return a->class_index; 258 return a->class_index;
255 } 259}
260
256static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM) 261static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM)
257 262
258static int ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b) 263static int
259 { 264ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b)
265{
260 return a->class_index - b->class_index; 266 return a->class_index - b->class_index;
261 } 267}
268
262static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM) 269static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM)
263 270
264/* Internal functions used by the "impl_default" implementation to access the 271/* Internal functions used by the "impl_default" implementation to access the
265 * state */ 272 * state */
266 273
267static int ex_data_check(void) 274static int
268 { 275ex_data_check(void)
276{
269 int toret = 1; 277 int toret = 1;
270 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 278 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
271 if(!ex_data 279 if (!ex_data
272 && (ex_data = lh_EX_CLASS_ITEM_new()) == NULL) 280 && (ex_data = lh_EX_CLASS_ITEM_new()) == NULL)
273 toret = 0; 281 toret = 0;
274 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 282 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
275 return toret; 283 return toret;
276 } 284}
277/* This macros helps reduce the locking from repeated checks because the 285/* This macros helps reduce the locking from repeated checks because the
278 * ex_data_check() function checks ex_data again inside a lock. */ 286 * ex_data_check() function checks ex_data again inside a lock. */
279#define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail} 287#define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
280 288
281/* This "inner" callback is used by the callback function that follows it */ 289/* This "inner" callback is used by the callback function that follows it */
282static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs) 290static void
283 { 291def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
292{
284 OPENSSL_free(funcs); 293 OPENSSL_free(funcs);
285 } 294}
286 295
287/* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from 296/* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
288 * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do 297 * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do
289 * any locking. */ 298 * any locking. */
290static void def_cleanup_cb(void *a_void) 299static void
291 { 300def_cleanup_cb(void *a_void)
301{
292 EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void; 302 EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
293 sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb); 303 sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
294 OPENSSL_free(item); 304 OPENSSL_free(item);
295 } 305}
296 306
297/* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a 307/* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a
298 * given class. Handles locking. */ 308 * given class. Handles locking. */
299static EX_CLASS_ITEM *def_get_class(int class_index) 309static EX_CLASS_ITEM
300 { 310*def_get_class(int class_index)
311{
301 EX_CLASS_ITEM d, *p, *gen; 312 EX_CLASS_ITEM d, *p, *gen;
302 EX_DATA_CHECK(return NULL;) 313 EX_DATA_CHECK(return NULL;)
303 d.class_index = class_index; 314 d.class_index = class_index;
304 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 315 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
305 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d); 316 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d);
306 if(!p) 317 if (!p) {
307 {
308 gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM)); 318 gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
309 if(gen) 319 if (gen) {
310 {
311 gen->class_index = class_index; 320 gen->class_index = class_index;
312 gen->meth_num = 0; 321 gen->meth_num = 0;
313 gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null(); 322 gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
314 if(!gen->meth) 323 if (!gen->meth)
315 OPENSSL_free(gen); 324 OPENSSL_free(gen);
316 else 325 else {
317 {
318 /* Because we're inside the ex_data lock, the 326 /* Because we're inside the ex_data lock, the
319 * return value from the insert will be NULL */ 327 * return value from the insert will be NULL */
320 (void)lh_EX_CLASS_ITEM_insert(ex_data, gen); 328 (void)lh_EX_CLASS_ITEM_insert(ex_data, gen);
321 p = gen; 329 p = gen;
322 }
323 } 330 }
324 } 331 }
332 }
325 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 333 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
326 if(!p) 334 if (!p)
327 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS,ERR_R_MALLOC_FAILURE); 335 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
328 return p; 336 return p;
329 } 337}
330 338
331/* Add a new method to the given EX_CLASS_ITEM and return the corresponding 339/* Add a new method to the given EX_CLASS_ITEM and return the corresponding
332 * index (or -1 for error). Handles locking. */ 340 * index (or -1 for error). Handles locking. */
333static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp, 341static int
334 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 342def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
335 CRYPTO_EX_free *free_func) 343 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
336 { 344{
337 int toret = -1; 345 int toret = -1;
338 CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc( 346 CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(
339 sizeof(CRYPTO_EX_DATA_FUNCS)); 347 sizeof(CRYPTO_EX_DATA_FUNCS));
340 if(!a) 348 if (!a) {
341 { 349 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
342 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
343 return -1; 350 return -1;
344 } 351 }
345 a->argl=argl; 352 a->argl = argl;
346 a->argp=argp; 353 a->argp = argp;
347 a->new_func=new_func; 354 a->new_func = new_func;
348 a->dup_func=dup_func; 355 a->dup_func = dup_func;
349 a->free_func=free_func; 356 a->free_func = free_func;
350 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 357 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
351 while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) 358 while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) {
352 { 359 if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) {
353 if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) 360 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
354 {
355 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
356 OPENSSL_free(a); 361 OPENSSL_free(a);
357 goto err; 362 goto err;
358 }
359 } 363 }
364 }
360 toret = item->meth_num++; 365 toret = item->meth_num++;
361 (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a); 366 (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
362err: 367err:
363 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 368 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
364 return toret; 369 return toret;
365 } 370}
366 371
367/**************************************************************/ 372/**************************************************************/
368/* The functions in the default CRYPTO_EX_DATA_IMPL structure */ 373/* The functions in the default CRYPTO_EX_DATA_IMPL structure */
369 374
370static int int_new_class(void) 375static int
371 { 376int_new_class(void)
377{
372 int toret; 378 int toret;
373 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 379 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
374 toret = ex_class++; 380 toret = ex_class++;
375 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 381 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
376 return toret; 382 return toret;
377 } 383}
378 384
379static void int_cleanup(void) 385static void
380 { 386int_cleanup(void)
387{
381 EX_DATA_CHECK(return;) 388 EX_DATA_CHECK(return;)
382 lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb); 389 lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb);
383 lh_EX_CLASS_ITEM_free(ex_data); 390 lh_EX_CLASS_ITEM_free(ex_data);
384 ex_data = NULL; 391 ex_data = NULL;
385 impl = NULL; 392 impl = NULL;
386 } 393}
387 394
388static int int_get_new_index(int class_index, long argl, void *argp, 395static int
389 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 396int_get_new_index(int class_index, long argl, void *argp,
390 CRYPTO_EX_free *free_func) 397 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
391 { 398CRYPTO_EX_free *free_func)
399{
392 EX_CLASS_ITEM *item = def_get_class(class_index); 400 EX_CLASS_ITEM *item = def_get_class(class_index);
393 if(!item) 401 if (!item)
394 return -1; 402 return -1;
395 return def_add_index(item, argl, argp, new_func, dup_func, free_func); 403 return def_add_index(item, argl, argp, new_func, dup_func, free_func);
396 } 404}
397 405
398/* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in 406/* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in
399 * the lock, then using them outside the lock. NB: Thread-safety only applies to 407 * the lock, then using them outside the lock. NB: Thread-safety only applies to
400 * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad' 408 * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad'
401 * itself. */ 409 * itself. */
402static int int_new_ex_data(int class_index, void *obj, 410static int
403 CRYPTO_EX_DATA *ad) 411int_new_ex_data(int class_index, void *obj,
404 { 412 CRYPTO_EX_DATA *ad)
405 int mx,i; 413{
414 int mx, i;
406 void *ptr; 415 void *ptr;
407 CRYPTO_EX_DATA_FUNCS **storage = NULL; 416 CRYPTO_EX_DATA_FUNCS **storage = NULL;
408 EX_CLASS_ITEM *item = def_get_class(class_index); 417 EX_CLASS_ITEM *item = def_get_class(class_index);
409 if(!item) 418 if (!item)
410 /* error is already set */ 419 /* error is already set */
411 return 0; 420 return 0;
412 ad->sk = NULL; 421 ad->sk = NULL;
413 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 422 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
414 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); 423 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
415 if(mx > 0) 424 if (mx > 0) {
416 {
417 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); 425 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
418 if(!storage) 426 if (!storage)
419 goto skip; 427 goto skip;
420 for(i = 0; i < mx; i++) 428 for (i = 0; i < mx; i++)
421 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); 429 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
422 } 430 }
423skip: 431skip:
424 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); 432 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
425 if((mx > 0) && !storage) 433 if ((mx > 0) && !storage) {
426 { 434 CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
427 CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA,ERR_R_MALLOC_FAILURE);
428 return 0; 435 return 0;
429 } 436 }
430 for(i = 0; i < mx; i++) 437 for (i = 0; i < mx; i++) {
431 { 438 if (storage[i] && storage[i]->new_func) {
432 if(storage[i] && storage[i]->new_func)
433 {
434 ptr = CRYPTO_get_ex_data(ad, i); 439 ptr = CRYPTO_get_ex_data(ad, i);
435 storage[i]->new_func(obj,ptr,ad,i, 440 storage[i]->new_func(obj, ptr, ad, i,
436 storage[i]->argl,storage[i]->argp); 441 storage[i]->argl, storage[i]->argp);
437 }
438 } 442 }
439 if(storage) 443 }
444 if (storage)
440 OPENSSL_free(storage); 445 OPENSSL_free(storage);
441 return 1; 446 return 1;
442 } 447}
443 448
444/* Same thread-safety notes as for "int_new_ex_data" */ 449/* Same thread-safety notes as for "int_new_ex_data" */
445static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 450static int
446 CRYPTO_EX_DATA *from) 451int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
447 { 452 CRYPTO_EX_DATA *from)
453{
448 int mx, j, i; 454 int mx, j, i;
449 char *ptr; 455 char *ptr;
450 CRYPTO_EX_DATA_FUNCS **storage = NULL; 456 CRYPTO_EX_DATA_FUNCS **storage = NULL;
451 EX_CLASS_ITEM *item; 457 EX_CLASS_ITEM *item;
452 if(!from->sk) 458 if (!from->sk)
453 /* 'to' should be "blank" which *is* just like 'from' */ 459 /* 'to' should be "blank" which *is* just like 'from' */
454 return 1; 460 return 1;
455 if((item = def_get_class(class_index)) == NULL) 461 if ((item = def_get_class(class_index)) == NULL)
456 return 0; 462 return 0;
457 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 463 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
458 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); 464 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
459 j = sk_void_num(from->sk); 465 j = sk_void_num(from->sk);
460 if(j < mx) 466 if (j < mx)
461 mx = j; 467 mx = j;
462 if(mx > 0) 468 if (mx > 0) {
463 {
464 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); 469 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
465 if(!storage) 470 if (!storage)
466 goto skip; 471 goto skip;
467 for(i = 0; i < mx; i++) 472 for (i = 0; i < mx; i++)
468 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); 473 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
469 } 474 }
470skip: 475skip:
471 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); 476 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
472 if((mx > 0) && !storage) 477 if ((mx > 0) && !storage) {
473 { 478 CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
474 CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA,ERR_R_MALLOC_FAILURE);
475 return 0; 479 return 0;
476 } 480 }
477 for(i = 0; i < mx; i++) 481 for (i = 0; i < mx; i++) {
478 {
479 ptr = CRYPTO_get_ex_data(from, i); 482 ptr = CRYPTO_get_ex_data(from, i);
480 if(storage[i] && storage[i]->dup_func) 483 if (storage[i] && storage[i]->dup_func)
481 storage[i]->dup_func(to,from,&ptr,i, 484 storage[i]->dup_func(to, from, &ptr, i,
482 storage[i]->argl,storage[i]->argp); 485 storage[i]->argl, storage[i]->argp);
483 CRYPTO_set_ex_data(to,i,ptr); 486 CRYPTO_set_ex_data(to, i, ptr);
484 } 487 }
485 if(storage) 488 if (storage)
486 OPENSSL_free(storage); 489 OPENSSL_free(storage);
487 return 1; 490 return 1;
488 } 491}
489 492
490/* Same thread-safety notes as for "int_new_ex_data" */ 493/* Same thread-safety notes as for "int_new_ex_data" */
491static void int_free_ex_data(int class_index, void *obj, 494static void
492 CRYPTO_EX_DATA *ad) 495int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
493 { 496{
494 int mx,i; 497 int mx, i;
495 EX_CLASS_ITEM *item; 498 EX_CLASS_ITEM *item;
496 void *ptr; 499 void *ptr;
497 CRYPTO_EX_DATA_FUNCS **storage = NULL; 500 CRYPTO_EX_DATA_FUNCS **storage = NULL;
498 if((item = def_get_class(class_index)) == NULL) 501 if ((item = def_get_class(class_index)) == NULL)
499 return; 502 return;
500 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 503 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
501 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); 504 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
502 if(mx > 0) 505 if (mx > 0) {
503 {
504 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); 506 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
505 if(!storage) 507 if (!storage)
506 goto skip; 508 goto skip;
507 for(i = 0; i < mx; i++) 509 for (i = 0; i < mx; i++)
508 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); 510 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
509 } 511 }
510skip: 512skip:
511 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); 513 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
512 if((mx > 0) && !storage) 514 if ((mx > 0) && !storage) {
513 { 515 CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
514 CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA,ERR_R_MALLOC_FAILURE);
515 return; 516 return;
517 }
518 for (i = 0; i < mx; i++) {
519 if (storage[i] && storage[i]->free_func) {
520 ptr = CRYPTO_get_ex_data(ad, i);
521 storage[i]->free_func(obj, ptr, ad, i,
522 storage[i]->argl, storage[i]->argp);
516 } 523 }
517 for(i = 0; i < mx; i++) 524 }
518 { 525 if (storage)
519 if(storage[i] && storage[i]->free_func)
520 {
521 ptr = CRYPTO_get_ex_data(ad,i);
522 storage[i]->free_func(obj,ptr,ad,i,
523 storage[i]->argl,storage[i]->argp);
524 }
525 }
526 if(storage)
527 OPENSSL_free(storage); 526 OPENSSL_free(storage);
528 if(ad->sk) 527 if (ad->sk) {
529 {
530 sk_void_free(ad->sk); 528 sk_void_free(ad->sk);
531 ad->sk=NULL; 529 ad->sk = NULL;
532 }
533 } 530 }
531}
534 532
535/********************************************************************/ 533/********************************************************************/
536/* API functions that defer all "state" operations to the "ex_data" 534/* API functions that defer all "state" operations to the "ex_data"
@@ -538,99 +536,101 @@ skip:
538 536
539/* Obtain an index for a new class (not the same as getting a new index within 537/* Obtain an index for a new class (not the same as getting a new index within
540 * an existing class - this is actually getting a new *class*) */ 538 * an existing class - this is actually getting a new *class*) */
541int CRYPTO_ex_data_new_class(void) 539int
542 { 540CRYPTO_ex_data_new_class(void)
541{
543 IMPL_CHECK 542 IMPL_CHECK
544 return EX_IMPL(new_class)(); 543 return EX_IMPL(new_class)();
545 } 544}
546 545
547/* Release all "ex_data" state to prevent memory leaks. This can't be made 546/* Release all "ex_data" state to prevent memory leaks. This can't be made
548 * thread-safe without overhauling a lot of stuff, and shouldn't really be 547 * thread-safe without overhauling a lot of stuff, and shouldn't really be
549 * called under potential race-conditions anyway (it's for program shutdown 548 * called under potential race-conditions anyway (it's for program shutdown
550 * after all). */ 549 * after all). */
551void CRYPTO_cleanup_all_ex_data(void) 550void
552 { 551CRYPTO_cleanup_all_ex_data(void)
552{
553 IMPL_CHECK 553 IMPL_CHECK
554 EX_IMPL(cleanup)(); 554 EX_IMPL(cleanup)();
555 } 555}
556 556
557/* Inside an existing class, get/register a new index. */ 557/* Inside an existing class, get/register a new index. */
558int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, 558int
559 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 559CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
560 CRYPTO_EX_free *free_func) 560 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
561 { 561{
562 int ret = -1; 562 int ret = -1;
563 563
564 IMPL_CHECK 564 IMPL_CHECK
565 ret = EX_IMPL(get_new_index)(class_index, 565 ret = EX_IMPL(get_new_index)(class_index,
566 argl, argp, new_func, dup_func, free_func); 566 argl, argp, new_func, dup_func, free_func);
567 return ret; 567 return ret;
568 } 568}
569 569
570/* Initialise a new CRYPTO_EX_DATA for use in a particular class - including 570/* Initialise a new CRYPTO_EX_DATA for use in a particular class - including
571 * calling new() callbacks for each index in the class used by this variable */ 571 * calling new() callbacks for each index in the class used by this variable */
572int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) 572int
573 { 573CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
574{
574 IMPL_CHECK 575 IMPL_CHECK
575 return EX_IMPL(new_ex_data)(class_index, obj, ad); 576 return EX_IMPL(new_ex_data)(class_index, obj, ad);
576 } 577}
577 578
578/* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for 579/* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for
579 * each index in the class used by this variable */ 580 * each index in the class used by this variable */
580int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 581int
581 CRYPTO_EX_DATA *from) 582CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from)
582 { 583{
583 IMPL_CHECK 584 IMPL_CHECK
584 return EX_IMPL(dup_ex_data)(class_index, to, from); 585 return EX_IMPL(dup_ex_data)(class_index, to, from);
585 } 586}
586 587
587/* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for 588/* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
588 * each index in the class used by this variable */ 589 * each index in the class used by this variable */
589void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) 590void
590 { 591CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
592{
591 IMPL_CHECK 593 IMPL_CHECK
592 EX_IMPL(free_ex_data)(class_index, obj, ad); 594 EX_IMPL(free_ex_data)(class_index, obj, ad);
593 } 595}
594 596
595/* For a given CRYPTO_EX_DATA variable, set the value corresponding to a 597/* For a given CRYPTO_EX_DATA variable, set the value corresponding to a
596 * particular index in the class used by this variable */ 598 * particular index in the class used by this variable */
597int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val) 599int
598 { 600CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
601{
599 int i; 602 int i;
600 603
601 if (ad->sk == NULL) 604 if (ad->sk == NULL) {
602 { 605 if ((ad->sk = sk_void_new_null()) == NULL) {
603 if ((ad->sk=sk_void_new_null()) == NULL) 606 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
604 { 607 return (0);
605 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
606 return(0);
607 }
608 } 608 }
609 i=sk_void_num(ad->sk); 609 }
610 610 i = sk_void_num(ad->sk);
611 while (i <= idx) 611
612 { 612 while (i <= idx) {
613 if (!sk_void_push(ad->sk,NULL)) 613 if (!sk_void_push(ad->sk, NULL)) {
614 { 614 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
615 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE); 615 return (0);
616 return(0);
617 }
618 i++;
619 } 616 }
620 sk_void_set(ad->sk,idx,val); 617 i++;
621 return(1);
622 } 618 }
619 sk_void_set(ad->sk, idx, val);
620 return (1);
621}
623 622
624/* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a 623/* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
625 * particular index in the class used by this variable */ 624 * particular index in the class used by this variable */
626void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) 625void
627 { 626*CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
627{
628 if (ad->sk == NULL) 628 if (ad->sk == NULL)
629 return(0); 629 return (0);
630 else if (idx >= sk_void_num(ad->sk)) 630 else if (idx >= sk_void_num(ad->sk))
631 return(0); 631 return (0);
632 else 632 else
633 return(sk_void_value(ad->sk,idx)); 633 return (sk_void_value(ad->sk, idx));
634 } 634}
635 635
636IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS) 636IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)
diff --git a/src/lib/libcrypto/fips_ers.c b/src/lib/libcrypto/fips_ers.c
index 09f11748f6..1788ed2884 100644
--- a/src/lib/libcrypto/fips_ers.c
+++ b/src/lib/libcrypto/fips_ers.c
@@ -3,5 +3,5 @@
3#ifdef OPENSSL_FIPS 3#ifdef OPENSSL_FIPS
4# include "fips_err.h" 4# include "fips_err.h"
5#else 5#else
6static void *dummy=&dummy; 6static void *dummy = &dummy;
7#endif 7#endif
diff --git a/src/lib/libcrypto/md32_common.h b/src/lib/libcrypto/md32_common.h
index bb7381952a..bfb610e802 100644
--- a/src/lib/libcrypto/md32_common.h
+++ b/src/lib/libcrypto/md32_common.h
@@ -148,7 +148,7 @@
148# if defined(__POWERPC__) 148# if defined(__POWERPC__)
149# define ROTATE(a,n) __rlwinm(a,n,0,31) 149# define ROTATE(a,n) __rlwinm(a,n,0,31)
150# elif defined(__MC68K__) 150# elif defined(__MC68K__)
151 /* Motorola specific tweak. <appro@fy.chalmers.se> */ 151/* Motorola specific tweak. <appro@fy.chalmers.se> */
152# define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) ) 152# define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
153# else 153# else
154# define ROTATE(a,n) __rol(a,n) 154# define ROTATE(a,n) __rol(a,n)
@@ -252,7 +252,7 @@
252#endif 252#endif
253#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) 253#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
254# ifndef B_ENDIAN 254# ifndef B_ENDIAN
255 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */ 255/* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
256# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l) 256# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
257# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l) 257# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
258# endif 258# endif
@@ -279,108 +279,103 @@
279 * Time for some action:-) 279 * Time for some action:-)
280 */ 280 */
281 281
282int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len) 282int
283 { 283HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
284 const unsigned char *data=data_; 284{
285 const unsigned char *data = data_;
285 unsigned char *p; 286 unsigned char *p;
286 HASH_LONG l; 287 HASH_LONG l;
287 size_t n; 288 size_t n;
288 289
289 if (len==0) return 1; 290 if (len == 0)
291 return 1;
290 292
291 l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL; 293 l = (c->Nl + (((HASH_LONG)len) << 3))&0xffffffffUL;
292 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to 294 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
293 * Wei Dai <weidai@eskimo.com> for pointing it out. */ 295 * Wei Dai <weidai@eskimo.com> for pointing it out. */
294 if (l < c->Nl) /* overflow */ 296 if (l < c->Nl) /* overflow */
295 c->Nh++; 297 c->Nh++;
296 c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */ 298 c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */
297 c->Nl=l; 299 c->Nl = l;
298 300
299 n = c->num; 301 n = c->num;
300 if (n != 0) 302 if (n != 0) {
301 { 303 p = (unsigned char *)c->data;
302 p=(unsigned char *)c->data; 304
303 305 if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
304 if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK) 306 memcpy (p + n, data, HASH_CBLOCK - n);
305 { 307 HASH_BLOCK_DATA_ORDER (c, p, 1);
306 memcpy (p+n,data,HASH_CBLOCK-n); 308 n = HASH_CBLOCK - n;
307 HASH_BLOCK_DATA_ORDER (c,p,1); 309 data += n;
308 n = HASH_CBLOCK-n; 310 len -= n;
309 data += n;
310 len -= n;
311 c->num = 0; 311 c->num = 0;
312 memset (p,0,HASH_CBLOCK); /* keep it zeroed */ 312 memset (p,0,HASH_CBLOCK); /* keep it zeroed */
313 } 313 } else {
314 else 314 memcpy (p + n, data, len);
315 {
316 memcpy (p+n,data,len);
317 c->num += (unsigned int)len; 315 c->num += (unsigned int)len;
318 return 1; 316 return 1;
319 }
320 } 317 }
318 }
321 319
322 n = len/HASH_CBLOCK; 320 n = len/HASH_CBLOCK;
323 if (n > 0) 321 if (n > 0) {
324 { 322 HASH_BLOCK_DATA_ORDER (c, data, n);
325 HASH_BLOCK_DATA_ORDER (c,data,n);
326 n *= HASH_CBLOCK; 323 n *= HASH_CBLOCK;
327 data += n; 324 data += n;
328 len -= n; 325 len -= n;
329 } 326 }
330 327
331 if (len != 0) 328 if (len != 0) {
332 {
333 p = (unsigned char *)c->data; 329 p = (unsigned char *)c->data;
334 c->num = (unsigned int)len; 330 c->num = (unsigned int)len;
335 memcpy (p,data,len); 331 memcpy (p, data, len);
336 }
337 return 1;
338 } 332 }
333 return 1;
334}
339 335
340 336
341void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data) 337void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
342 { 338{
343 HASH_BLOCK_DATA_ORDER (c,data,1); 339 HASH_BLOCK_DATA_ORDER (c, data, 1);
344 } 340}
345 341
346 342
347int HASH_FINAL (unsigned char *md, HASH_CTX *c) 343int HASH_FINAL (unsigned char *md, HASH_CTX *c)
348 { 344{
349 unsigned char *p = (unsigned char *)c->data; 345 unsigned char *p = (unsigned char *)c->data;
350 size_t n = c->num; 346 size_t n = c->num;
351 347
352 p[n] = 0x80; /* there is always room for one */ 348 p[n] = 0x80; /* there is always room for one */
353 n++; 349 n++;
354 350
355 if (n > (HASH_CBLOCK-8)) 351 if (n > (HASH_CBLOCK - 8)) {
356 { 352 memset (p + n, 0, HASH_CBLOCK - n);
357 memset (p+n,0,HASH_CBLOCK-n); 353 n = 0;
358 n=0; 354 HASH_BLOCK_DATA_ORDER (c, p, 1);
359 HASH_BLOCK_DATA_ORDER (c,p,1); 355 }
360 } 356 memset (p + n, 0, HASH_CBLOCK - 8 - n);
361 memset (p+n,0,HASH_CBLOCK-8-n);
362 357
363 p += HASH_CBLOCK-8; 358 p += HASH_CBLOCK - 8;
364#if defined(DATA_ORDER_IS_BIG_ENDIAN) 359#if defined(DATA_ORDER_IS_BIG_ENDIAN)
365 (void)HOST_l2c(c->Nh,p); 360 (void)HOST_l2c(c->Nh, p);
366 (void)HOST_l2c(c->Nl,p); 361 (void)HOST_l2c(c->Nl, p);
367#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) 362#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
368 (void)HOST_l2c(c->Nl,p); 363 (void)HOST_l2c(c->Nl, p);
369 (void)HOST_l2c(c->Nh,p); 364 (void)HOST_l2c(c->Nh, p);
370#endif 365#endif
371 p -= HASH_CBLOCK; 366 p -= HASH_CBLOCK;
372 HASH_BLOCK_DATA_ORDER (c,p,1); 367 HASH_BLOCK_DATA_ORDER (c, p, 1);
373 c->num=0; 368 c->num = 0;
374 memset (p,0,HASH_CBLOCK); 369 memset (p, 0, HASH_CBLOCK);
375 370
376#ifndef HASH_MAKE_STRING 371#ifndef HASH_MAKE_STRING
377#error "HASH_MAKE_STRING must be defined!" 372#error "HASH_MAKE_STRING must be defined!"
378#else 373#else
379 HASH_MAKE_STRING(c,md); 374 HASH_MAKE_STRING(c, md);
380#endif 375#endif
381 376
382 return 1; 377 return 1;
383 } 378}
384 379
385#ifndef MD32_REG_T 380#ifndef MD32_REG_T
386#if defined(__alpha) || defined(__sparcv9) || defined(__mips) 381#if defined(__alpha) || defined(__sparcv9) || defined(__mips)
diff --git a/src/lib/libcrypto/mem.c b/src/lib/libcrypto/mem.c
index 55e829dc92..86e5ff1c8e 100644
--- a/src/lib/libcrypto/mem.c
+++ b/src/lib/libcrypto/mem.c
@@ -62,206 +62,230 @@
62#include "cryptlib.h" 62#include "cryptlib.h"
63 63
64 64
65static int allow_customize = 1; /* we provide flexible functions for */ 65static int allow_customize = 1;
66static int allow_customize_debug = 1;/* exchanging memory-related functions at 66/* we provide flexible functions for */
67 * run-time, but this must be done 67static int allow_customize_debug = 1;/* exchanging memory - related functions at
68 * before any blocks are actually 68 * run - time, but this must be done
69 * allocated; or we'll run into huge 69 * before any blocks are actually
70 * problems when malloc/free pairs 70 * allocated; or we'll run into huge
71 * don't match etc. */ 71 * problems when malloc/free pairs
72 72 * don't match etc. */
73
74 73
75/* the following pointers may be changed as long as 'allow_customize' is set */ 74/* the following pointers may be changed as long as 'allow_customize' is set */
76 75
77static void *(*malloc_func)(size_t) = malloc; 76static void *(*malloc_func)(size_t) = malloc;
78static void *default_malloc_ex(size_t num, const char *file, int line) 77static void
79 { return malloc_func(num); } 78*default_malloc_ex(size_t num, const char *file, int line)
80static void *(*malloc_ex_func)(size_t, const char *file, int line) 79{
81 = default_malloc_ex; 80 return malloc_func(num);
82 81}
83static void *(*realloc_func)(void *, size_t)= realloc; 82static void *(*malloc_ex_func)(size_t, const char *file, int line) =
84static void *default_realloc_ex(void *str, size_t num, 83 default_malloc_ex;
85 const char *file, int line) 84
86 { return realloc_func(str,num); } 85static void *(*realloc_func)(void *, size_t) = realloc;
87static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) 86static void
88 = default_realloc_ex; 87*default_realloc_ex(void *str, size_t num, const char *file, int line)
89 88{
90static void (*free_func)(void *) = free; 89 return realloc_func(str, num);
91 90}
92static void *(*malloc_locked_func)(size_t) = malloc; 91static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) =
93static void *default_malloc_locked_ex(size_t num, const char *file, int line) 92 default_realloc_ex;
94 { return malloc_locked_func(num); } 93
95static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) 94static void (*free_func)(void *) = free;
96 = default_malloc_locked_ex; 95
97 96static void *(*malloc_locked_func)(size_t) = malloc;
98static void (*free_locked_func)(void *) = free; 97static void
99 98*default_malloc_locked_ex(size_t num, const char *file, int line)
99{
100 return malloc_locked_func(num);
101}
102static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) =
103 default_malloc_locked_ex;
104
105static void (*free_locked_func)(void *) = free;
100 106
101 107
102/* may be changed as long as 'allow_customize_debug' is set */ 108/* may be changed as long as 'allow_customize_debug' is set */
103/* XXX use correct function pointer types */ 109/* XXX use correct function pointer types */
104#ifdef CRYPTO_MDEBUG 110#ifdef CRYPTO_MDEBUG
105/* use default functions from mem_dbg.c */ 111/* use default functions from mem_dbg.c */
106static void (*malloc_debug_func)(void *,int,const char *,int,int) 112static void (*malloc_debug_func)(void *, int, const char *, int, int) =
107 = CRYPTO_dbg_malloc; 113 CRYPTO_dbg_malloc;
108static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) 114static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) =
109 = CRYPTO_dbg_realloc; 115 CRYPTO_dbg_realloc;
110static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; 116static void (*free_debug_func)(void *, int) = CRYPTO_dbg_free;
111static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; 117static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
112static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; 118static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
113#else 119#else
114/* applications can use CRYPTO_malloc_debug_init() to select above case 120/* applications can use CRYPTO_malloc_debug_init() to select above case
115 * at run-time */ 121 * at run-time */
116static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; 122static void (*malloc_debug_func)(void *, int, const char *, int, int) = NULL;
117static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) 123static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) =
118 = NULL; 124 NULL;
119static void (*free_debug_func)(void *,int) = NULL; 125static void (*free_debug_func)(void *, int) = NULL;
120static void (*set_debug_options_func)(long) = NULL; 126static void (*set_debug_options_func)(long) = NULL;
121static long (*get_debug_options_func)(void) = NULL; 127static long (*get_debug_options_func)(void) = NULL;
122#endif 128#endif
123 129
124int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), 130int
125 void (*f)(void *)) 131CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
126 { 132 void (*f)(void *))
133{
127 /* Dummy call just to ensure OPENSSL_init() gets linked in */ 134 /* Dummy call just to ensure OPENSSL_init() gets linked in */
128 OPENSSL_init(); 135 OPENSSL_init();
129 if (!allow_customize) 136 if (!allow_customize)
130 return 0; 137 return 0;
131 if ((m == 0) || (r == 0) || (f == 0)) 138 if ((m == 0) || (r == 0) || (f == 0))
132 return 0; 139 return 0;
133 malloc_func=m; malloc_ex_func=default_malloc_ex; 140 malloc_func = m;
134 realloc_func=r; realloc_ex_func=default_realloc_ex; 141 malloc_ex_func = default_malloc_ex;
135 free_func=f; 142 realloc_func = r;
136 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; 143 realloc_ex_func = default_realloc_ex;
137 free_locked_func=f; 144 free_func = f;
145 malloc_locked_func = m;
146 malloc_locked_ex_func = default_malloc_locked_ex;
147 free_locked_func = f;
138 return 1; 148 return 1;
139 } 149}
140 150
141int CRYPTO_set_mem_ex_functions( 151int
142 void *(*m)(size_t,const char *,int), 152CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int),
143 void *(*r)(void *, size_t,const char *,int), 153 void *(*r)(void *, size_t, const char *, int), void (*f)(void *))
144 void (*f)(void *)) 154{
145 {
146 if (!allow_customize) 155 if (!allow_customize)
147 return 0; 156 return 0;
148 if ((m == 0) || (r == 0) || (f == 0)) 157 if ((m == 0) || (r == 0) || (f == 0))
149 return 0; 158 return 0;
150 malloc_func=0; malloc_ex_func=m; 159 malloc_func = 0;
151 realloc_func=0; realloc_ex_func=r; 160 malloc_ex_func = m;
152 free_func=f; 161 realloc_func = 0;
153 malloc_locked_func=0; malloc_locked_ex_func=m; 162 realloc_ex_func = r;
154 free_locked_func=f; 163 free_func = f;
164 malloc_locked_func = 0;
165 malloc_locked_ex_func = m;
166 free_locked_func = f;
155 return 1; 167 return 1;
156 } 168}
157 169
158int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) 170int
159 { 171CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
172{
160 if (!allow_customize) 173 if (!allow_customize)
161 return 0; 174 return 0;
162 if ((m == NULL) || (f == NULL)) 175 if ((m == NULL) || (f == NULL))
163 return 0; 176 return 0;
164 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; 177 malloc_locked_func = m;
165 free_locked_func=f; 178 malloc_locked_ex_func = default_malloc_locked_ex;
179 free_locked_func = f;
166 return 1; 180 return 1;
167 } 181}
168 182
169int CRYPTO_set_locked_mem_ex_functions( 183int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int),
170 void *(*m)(size_t,const char *,int), 184 void (*f)(void *))
171 void (*f)(void *)) 185{
172 {
173 if (!allow_customize) 186 if (!allow_customize)
174 return 0; 187 return 0;
175 if ((m == NULL) || (f == NULL)) 188 if ((m == NULL) || (f == NULL))
176 return 0; 189 return 0;
177 malloc_locked_func=0; malloc_locked_ex_func=m; 190 malloc_locked_func = 0;
178 free_func=f; 191 malloc_locked_ex_func = m;
192 free_func = f;
179 return 1; 193 return 1;
180 } 194}
181 195
182int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), 196int
183 void (*r)(void *,void *,int,const char *,int,int), 197CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int),
184 void (*f)(void *,int), 198 void (*r)(void *, void *, int, const char *, int, int),
185 void (*so)(long), 199 void (*f)(void *, int), void (*so)(long), long (*go)(void))
186 long (*go)(void)) 200{
187 {
188 if (!allow_customize_debug) 201 if (!allow_customize_debug)
189 return 0; 202 return 0;
190 OPENSSL_init(); 203 OPENSSL_init();
191 malloc_debug_func=m; 204 malloc_debug_func = m;
192 realloc_debug_func=r; 205 realloc_debug_func = r;
193 free_debug_func=f; 206 free_debug_func = f;
194 set_debug_options_func=so; 207 set_debug_options_func = so;
195 get_debug_options_func=go; 208 get_debug_options_func = go;
196 return 1; 209 return 1;
197 } 210}
198 211
199 212
200void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), 213void
201 void (**f)(void *)) 214CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
202 { 215 void (**f)(void *))
203 if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? 216{
204 malloc_func : 0; 217 if (m != NULL)
205 if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? 218 *m = (malloc_ex_func == default_malloc_ex) ? malloc_func : 0;
206 realloc_func : 0; 219 if (r != NULL)
207 if (f != NULL) *f=free_func; 220 *r = (realloc_ex_func == default_realloc_ex) ? realloc_func : 0;
208 } 221 if (f != NULL)
209 222 *f = free_func;
210void CRYPTO_get_mem_ex_functions( 223}
211 void *(**m)(size_t,const char *,int), 224
212 void *(**r)(void *, size_t,const char *,int), 225void CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int),
213 void (**f)(void *)) 226 void *(**r)(void *, size_t, const char *, int), void (**f)(void *))
214 { 227{
215 if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ? 228 if (m != NULL)
216 malloc_ex_func : 0; 229 *m = (malloc_ex_func != default_malloc_ex) ? malloc_ex_func : 0;
217 if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ? 230 if (r != NULL)
218 realloc_ex_func : 0; 231 *r = (realloc_ex_func != default_realloc_ex) ?
219 if (f != NULL) *f=free_func; 232 realloc_ex_func : 0;
220 } 233 if (f != NULL)
221 234 *f = free_func;
222void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) 235}
223 { 236
224 if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? 237void
225 malloc_locked_func : 0; 238CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
226 if (f != NULL) *f=free_locked_func; 239{
227 } 240 if (m != NULL)
228 241 *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
229void CRYPTO_get_locked_mem_ex_functions( 242 malloc_locked_func : 0;
230 void *(**m)(size_t,const char *,int), 243 if (f != NULL)
231 void (**f)(void *)) 244 *f = free_locked_func;
232 { 245}
233 if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? 246
234 malloc_locked_ex_func : 0; 247void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int),
235 if (f != NULL) *f=free_locked_func; 248 void (**f)(void *))
236 } 249{
237 250 if (m != NULL)
238void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), 251 *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
239 void (**r)(void *,void *,int,const char *,int,int), 252 malloc_locked_ex_func : 0;
240 void (**f)(void *,int), 253 if (f != NULL)
241 void (**so)(long), 254 *f = free_locked_func;
242 long (**go)(void)) 255}
243 { 256
244 if (m != NULL) *m=malloc_debug_func; 257void
245 if (r != NULL) *r=realloc_debug_func; 258CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int),
246 if (f != NULL) *f=free_debug_func; 259 void (**r)(void *, void *, int, const char *, int, int),
247 if (so != NULL) *so=set_debug_options_func; 260 void (**f)(void *, int), void (**so)(long), long (**go)(void))
248 if (go != NULL) *go=get_debug_options_func; 261{
249 } 262 if (m != NULL)
250 263 *m = malloc_debug_func;
251 264 if (r != NULL)
252void *CRYPTO_malloc_locked(int num, const char *file, int line) 265 *r = realloc_debug_func;
253 { 266 if (f != NULL)
267 *f = free_debug_func;
268 if (so != NULL)
269 *so = set_debug_options_func;
270 if (go != NULL)
271 *go = get_debug_options_func;
272}
273
274
275void
276*CRYPTO_malloc_locked(int num, const char *file, int line)
277{
254 void *ret = NULL; 278 void *ret = NULL;
255 279
256 if (num <= 0) return NULL; 280 if (num <= 0)
281 return NULL;
257 282
258 allow_customize = 0; 283 allow_customize = 0;
259 if (malloc_debug_func != NULL) 284 if (malloc_debug_func != NULL) {
260 {
261 allow_customize_debug = 0; 285 allow_customize_debug = 0;
262 malloc_debug_func(NULL, num, file, line, 0); 286 malloc_debug_func(NULL, num, file, line, 0);
263 } 287 }
264 ret = malloc_locked_ex_func(num,file,line); 288 ret = malloc_locked_ex_func(num, file, line);
265#ifdef LEVITTE_DEBUG_MEM 289#ifdef LEVITTE_DEBUG_MEM
266 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); 290 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
267#endif 291#endif
@@ -272,17 +296,18 @@ void *CRYPTO_malloc_locked(int num, const char *file, int line)
272 /* Create a dependency on the value of 'cleanse_ctr' so our memory 296 /* Create a dependency on the value of 'cleanse_ctr' so our memory
273 * sanitisation function can't be optimised out. NB: We only do 297 * sanitisation function can't be optimised out. NB: We only do
274 * this for >2Kb so the overhead doesn't bother us. */ 298 * this for >2Kb so the overhead doesn't bother us. */
275 if(ret && (num > 2048)) 299 if (ret && (num > 2048)) {
276 { extern unsigned char cleanse_ctr; 300 extern unsigned char cleanse_ctr;
277 ((unsigned char *)ret)[0] = cleanse_ctr; 301 ((unsigned char *)ret)[0] = cleanse_ctr;
278 } 302 }
279#endif 303#endif
280 304
281 return ret; 305 return ret;
282 } 306}
283 307
284void CRYPTO_free_locked(void *str) 308void
285 { 309CRYPTO_free_locked(void *str)
310{
286 if (free_debug_func != NULL) 311 if (free_debug_func != NULL)
287 free_debug_func(str, 0); 312 free_debug_func(str, 0);
288#ifdef LEVITTE_DEBUG_MEM 313#ifdef LEVITTE_DEBUG_MEM
@@ -291,21 +316,22 @@ void CRYPTO_free_locked(void *str)
291 free_locked_func(str); 316 free_locked_func(str);
292 if (free_debug_func != NULL) 317 if (free_debug_func != NULL)
293 free_debug_func(NULL, 1); 318 free_debug_func(NULL, 1);
294 } 319}
295 320
296void *CRYPTO_malloc(int num, const char *file, int line) 321void
297 { 322*CRYPTO_malloc(int num, const char *file, int line)
323{
298 void *ret = NULL; 324 void *ret = NULL;
299 325
300 if (num <= 0) return NULL; 326 if (num <= 0)
327 return NULL;
301 328
302 allow_customize = 0; 329 allow_customize = 0;
303 if (malloc_debug_func != NULL) 330 if (malloc_debug_func != NULL) {
304 {
305 allow_customize_debug = 0; 331 allow_customize_debug = 0;
306 malloc_debug_func(NULL, num, file, line, 0); 332 malloc_debug_func(NULL, num, file, line, 0);
307 } 333 }
308 ret = malloc_ex_func(num,file,line); 334 ret = malloc_ex_func(num, file, line);
309#ifdef LEVITTE_DEBUG_MEM 335#ifdef LEVITTE_DEBUG_MEM
310 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); 336 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
311#endif 337#endif
@@ -316,35 +342,39 @@ void *CRYPTO_malloc(int num, const char *file, int line)
316 /* Create a dependency on the value of 'cleanse_ctr' so our memory 342 /* Create a dependency on the value of 'cleanse_ctr' so our memory
317 * sanitisation function can't be optimised out. NB: We only do 343 * sanitisation function can't be optimised out. NB: We only do
318 * this for >2Kb so the overhead doesn't bother us. */ 344 * this for >2Kb so the overhead doesn't bother us. */
319 if(ret && (num > 2048)) 345 if (ret && (num > 2048)) {
320 { extern unsigned char cleanse_ctr; 346 extern unsigned char cleanse_ctr;
321 ((unsigned char *)ret)[0] = cleanse_ctr; 347 ((unsigned char *)ret)[0] = cleanse_ctr;
322 } 348 }
323#endif 349#endif
324 350
325 return ret; 351 return ret;
326 } 352}
327char *CRYPTO_strdup(const char *str, const char *file, int line) 353
328 { 354char
329 size_t len = strlen(str)+1; 355*CRYPTO_strdup(const char *str, const char *file, int line)
356{
357 size_t len = strlen(str) + 1;
330 char *ret = CRYPTO_malloc(len, file, line); 358 char *ret = CRYPTO_malloc(len, file, line);
331 359
332 memcpy(ret, str, len); 360 memcpy(ret, str, len);
333 return ret; 361 return ret;
334 } 362}
335 363
336void *CRYPTO_realloc(void *str, int num, const char *file, int line) 364void
337 { 365*CRYPTO_realloc(void *str, int num, const char *file, int line)
366{
338 void *ret = NULL; 367 void *ret = NULL;
339 368
340 if (str == NULL) 369 if (str == NULL)
341 return CRYPTO_malloc(num, file, line); 370 return CRYPTO_malloc(num, file, line);
342 371
343 if (num <= 0) return NULL; 372 if (num <= 0)
373 return NULL;
344 374
345 if (realloc_debug_func != NULL) 375 if (realloc_debug_func != NULL)
346 realloc_debug_func(str, NULL, num, file, line, 0); 376 realloc_debug_func(str, NULL, num, file, line, 0);
347 ret = realloc_ex_func(str,num,file,line); 377 ret = realloc_ex_func(str, num, file, line);
348#ifdef LEVITTE_DEBUG_MEM 378#ifdef LEVITTE_DEBUG_MEM
349 fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num); 379 fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num);
350#endif 380#endif
@@ -352,44 +382,47 @@ void *CRYPTO_realloc(void *str, int num, const char *file, int line)
352 realloc_debug_func(str, ret, num, file, line, 1); 382 realloc_debug_func(str, ret, num, file, line, 1);
353 383
354 return ret; 384 return ret;
355 } 385}
356 386
357void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, 387void
358 int line) 388*CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
359 { 389int line)
390{
360 void *ret = NULL; 391 void *ret = NULL;
361 392
362 if (str == NULL) 393 if (str == NULL)
363 return CRYPTO_malloc(num, file, line); 394 return CRYPTO_malloc(num, file, line);
364 395
365 if (num <= 0) return NULL; 396 if (num <= 0)
397 return NULL;
366 398
367 /* We don't support shrinking the buffer. Note the memcpy that copies 399 /* We don't support shrinking the buffer. Note the memcpy that copies
368 * |old_len| bytes to the new buffer, below. */ 400 * |old_len| bytes to the new buffer, below. */
369 if (num < old_len) return NULL; 401 if (num < old_len)
402 return NULL;
370 403
371 if (realloc_debug_func != NULL) 404 if (realloc_debug_func != NULL)
372 realloc_debug_func(str, NULL, num, file, line, 0); 405 realloc_debug_func(str, NULL, num, file, line, 0);
373 ret=malloc_ex_func(num,file,line); 406 ret = malloc_ex_func(num, file, line);
374 if(ret) 407 if (ret) {
375 { 408 memcpy(ret, str, old_len);
376 memcpy(ret,str,old_len); 409 OPENSSL_cleanse(str, old_len);
377 OPENSSL_cleanse(str,old_len);
378 free_func(str); 410 free_func(str);
379 } 411 }
380#ifdef LEVITTE_DEBUG_MEM 412#ifdef LEVITTE_DEBUG_MEM
381 fprintf(stderr, 413 fprintf(stderr,
382 "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", 414 "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n",
383 str, ret, num); 415 str, ret, num);
384#endif 416#endif
385 if (realloc_debug_func != NULL) 417 if (realloc_debug_func != NULL)
386 realloc_debug_func(str, ret, num, file, line, 1); 418 realloc_debug_func(str, ret, num, file, line, 1);
387 419
388 return ret; 420 return ret;
389 } 421}
390 422
391void CRYPTO_free(void *str) 423void
392 { 424CRYPTO_free(void *str)
425{
393 if (free_debug_func != NULL) 426 if (free_debug_func != NULL)
394 free_debug_func(str, 0); 427 free_debug_func(str, 0);
395#ifdef LEVITTE_DEBUG_MEM 428#ifdef LEVITTE_DEBUG_MEM
@@ -398,24 +431,28 @@ void CRYPTO_free(void *str)
398 free_func(str); 431 free_func(str);
399 if (free_debug_func != NULL) 432 if (free_debug_func != NULL)
400 free_debug_func(NULL, 1); 433 free_debug_func(NULL, 1);
401 } 434}
402 435
403void *CRYPTO_remalloc(void *a, int num, const char *file, int line) 436void
404 { 437*CRYPTO_remalloc(void *a, int num, const char *file, int line)
405 if (a != NULL) OPENSSL_free(a); 438{
406 a=(char *)OPENSSL_malloc(num); 439 if (a != NULL)
407 return(a); 440 OPENSSL_free(a);
408 } 441 a = (char *)OPENSSL_malloc(num);
409 442 return (a);
410void CRYPTO_set_mem_debug_options(long bits) 443}
411 { 444
445void
446CRYPTO_set_mem_debug_options(long bits)
447{
412 if (set_debug_options_func != NULL) 448 if (set_debug_options_func != NULL)
413 set_debug_options_func(bits); 449 set_debug_options_func(bits);
414 } 450}
415 451
416long CRYPTO_get_mem_debug_options(void) 452long
417 { 453CRYPTO_get_mem_debug_options(void)
454{
418 if (get_debug_options_func != NULL) 455 if (get_debug_options_func != NULL)
419 return get_debug_options_func(); 456 return get_debug_options_func();
420 return 0; 457 return 0;
421 } 458}
diff --git a/src/lib/libcrypto/mem_clr.c b/src/lib/libcrypto/mem_clr.c
index add1f78020..fc108c83b4 100644
--- a/src/lib/libcrypto/mem_clr.c
+++ b/src/lib/libcrypto/mem_clr.c
@@ -61,17 +61,17 @@
61 61
62unsigned char cleanse_ctr = 0; 62unsigned char cleanse_ctr = 0;
63 63
64void OPENSSL_cleanse(void *ptr, size_t len) 64void
65 { 65OPENSSL_cleanse(void *ptr, size_t len)
66{
66 unsigned char *p = ptr; 67 unsigned char *p = ptr;
67 size_t loop = len, ctr = cleanse_ctr; 68 size_t loop = len, ctr = cleanse_ctr;
68 while(loop--) 69 while (loop--) {
69 {
70 *(p++) = (unsigned char)ctr; 70 *(p++) = (unsigned char)ctr;
71 ctr += (17 + ((size_t)p & 0xF)); 71 ctr += (17 + ((size_t)p & 0xF));
72 } 72 }
73 p=memchr(ptr, (unsigned char)ctr, len); 73 p = memchr(ptr, (unsigned char)ctr, len);
74 if(p) 74 if (p)
75 ctr += (63 + (size_t)p); 75 ctr += (63 + (size_t)p);
76 cleanse_ctr = (unsigned char)ctr; 76 cleanse_ctr = (unsigned char)ctr;
77 } 77}
diff --git a/src/lib/libcrypto/mem_dbg.c b/src/lib/libcrypto/mem_dbg.c
index ac793397f1..9324ec4c7d 100644
--- a/src/lib/libcrypto/mem_dbg.c
+++ b/src/lib/libcrypto/mem_dbg.c
@@ -118,7 +118,7 @@
118#include <openssl/bio.h> 118#include <openssl/bio.h>
119#include <openssl/lhash.h> 119#include <openssl/lhash.h>
120 120
121static int mh_mode=CRYPTO_MEM_CHECK_OFF; 121static int mh_mode = CRYPTO_MEM_CHECK_OFF;
122/* The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE 122/* The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE
123 * when the application asks for it (usually after library initialisation 123 * when the application asks for it (usually after library initialisation
124 * for which no book-keeping is desired). 124 * for which no book-keeping is desired).
@@ -136,10 +136,9 @@ static int mh_mode=CRYPTO_MEM_CHECK_OFF;
136static unsigned long order = 0; /* number of memory requests */ 136static unsigned long order = 0; /* number of memory requests */
137 137
138DECLARE_LHASH_OF(MEM); 138DECLARE_LHASH_OF(MEM);
139static LHASH_OF(MEM) *mh=NULL; /* hash-table of memory requests 139static LHASH_OF(MEM) *mh = NULL; /* hash - table of memory requests
140 * (address as key); access requires 140 * (address as key); access requires
141 * MALLOC2 lock */ 141 * MALLOC2 lock */
142
143 142
144typedef struct app_mem_info_st 143typedef struct app_mem_info_st
145/* For application-defined information (static C-string `info') 144/* For application-defined information (static C-string `info')
@@ -149,28 +148,28 @@ typedef struct app_mem_info_st
149 * CRYPTO_pop_info() to pop an entry, 148 * CRYPTO_pop_info() to pop an entry,
150 * CRYPTO_remove_all_info() to pop all entries. 149 * CRYPTO_remove_all_info() to pop all entries.
151 */ 150 */
152 { 151{
153 CRYPTO_THREADID threadid; 152 CRYPTO_THREADID threadid;
154 const char *file; 153 const char *file;
155 int line; 154 int line;
156 const char *info; 155 const char *info;
157 struct app_mem_info_st *next; /* tail of thread's stack */ 156 struct app_mem_info_st *next; /* tail of thread's stack */
158 int references; 157 int references;
159 } APP_INFO; 158} APP_INFO;
160 159
161static void app_info_free(APP_INFO *); 160static void app_info_free(APP_INFO *);
162 161
163DECLARE_LHASH_OF(APP_INFO); 162DECLARE_LHASH_OF(APP_INFO);
164static LHASH_OF(APP_INFO) *amih=NULL; /* hash-table with those 163static LHASH_OF(APP_INFO) *amih = NULL; /* hash - table with those
165 * app_mem_info_st's that are at 164 * app_mem_info_st's that are at
166 * the top of their thread's 165 * the top of their thread's
167 * stack (with `thread' as key); 166 * stack (with `thread' as key);
168 * access requires MALLOC2 167 * access requires MALLOC2
169 * lock */ 168 * lock */
170 169
171typedef struct mem_st 170typedef struct mem_st
172/* memory-block description */ 171/* memory-block description */
173 { 172{
174 void *addr; 173 void *addr;
175 int num; 174 int num;
176 const char *file; 175 const char *file;
@@ -179,47 +178,46 @@ typedef struct mem_st
179 unsigned long order; 178 unsigned long order;
180 time_t time; 179 time_t time;
181 APP_INFO *app_info; 180 APP_INFO *app_info;
182 } MEM; 181} MEM;
183 182
184static long options = /* extra information to be recorded */ 183static long options = /* extra information to be recorded */
185#if defined(CRYPTO_MDEBUG_TIME) || defined(CRYPTO_MDEBUG_ALL) 184#if defined(CRYPTO_MDEBUG_TIME) || defined(CRYPTO_MDEBUG_ALL)
186 V_CRYPTO_MDEBUG_TIME | 185V_CRYPTO_MDEBUG_TIME |
187#endif 186#endif
188#if defined(CRYPTO_MDEBUG_THREAD) || defined(CRYPTO_MDEBUG_ALL) 187#if defined(CRYPTO_MDEBUG_THREAD) || defined(CRYPTO_MDEBUG_ALL)
189 V_CRYPTO_MDEBUG_THREAD | 188V_CRYPTO_MDEBUG_THREAD |
190#endif 189#endif
191 0; 1900;
192 191
193 192
194static unsigned int num_disable = 0; /* num_disable > 0 193static unsigned int num_disable = 0; /* num_disable > 0
195 * iff 194 * iff
196 * mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) 195 * mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE)
197 */ 196 */
198 197
199/* Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this 198/* Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this
200 * case (by the thread named in disabling_thread). 199 * case (by the thread named in disabling_thread).
201 */ 200 */
202static CRYPTO_THREADID disabling_threadid; 201static CRYPTO_THREADID disabling_threadid;
203 202
204static void app_info_free(APP_INFO *inf) 203static void
205 { 204app_info_free(APP_INFO *inf)
206 if (--(inf->references) <= 0) 205{
207 { 206 if (--(inf->references) <= 0) {
208 if (inf->next != NULL) 207 if (inf->next != NULL) {
209 {
210 app_info_free(inf->next); 208 app_info_free(inf->next);
211 }
212 OPENSSL_free(inf);
213 } 209 }
210 OPENSSL_free(inf);
214 } 211 }
212}
215 213
216int CRYPTO_mem_ctrl(int mode) 214int
217 { 215CRYPTO_mem_ctrl(int mode)
218 int ret=mh_mode; 216{
217 int ret = mh_mode;
219 218
220 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); 219 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
221 switch (mode) 220 switch (mode) {
222 {
223 /* for applications (not to be called while multiple threads 221 /* for applications (not to be called while multiple threads
224 * use the library): */ 222 * use the library): */
225 case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */ 223 case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */
@@ -232,14 +230,13 @@ int CRYPTO_mem_ctrl(int mode)
232 or there'll be a lot of confusion */ 230 or there'll be a lot of confusion */
233 break; 231 break;
234 232
235 /* switch off temporarily (for library-internal use): */ 233 /* switch off temporarily (for library-internal use): */
236 case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */ 234 case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */
237 if (mh_mode & CRYPTO_MEM_CHECK_ON) 235 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
238 {
239 CRYPTO_THREADID cur; 236 CRYPTO_THREADID cur;
240 CRYPTO_THREADID_current(&cur); 237 CRYPTO_THREADID_current(&cur);
241 if (!num_disable || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) /* otherwise we already have the MALLOC2 lock */ 238 if (!num_disable || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) /* otherwise we already have the MALLOC2 lock */
242 { 239 {
243 /* Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed while 240 /* Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed while
244 * we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock if 241 * we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock if
245 * somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot release 242 * somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot release
@@ -257,412 +254,402 @@ int CRYPTO_mem_ctrl(int mode)
257 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); 254 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
258 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE; 255 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
259 CRYPTO_THREADID_cpy(&disabling_threadid, &cur); 256 CRYPTO_THREADID_cpy(&disabling_threadid, &cur);
260 }
261 num_disable++;
262 } 257 }
258 num_disable++;
259 }
263 break; 260 break;
264 case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */ 261 case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */
265 if (mh_mode & CRYPTO_MEM_CHECK_ON) 262 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
266 {
267 if (num_disable) /* always true, or something is going wrong */ 263 if (num_disable) /* always true, or something is going wrong */
268 { 264 {
269 num_disable--; 265 num_disable--;
270 if (num_disable == 0) 266 if (num_disable == 0) {
271 {
272 mh_mode|=CRYPTO_MEM_CHECK_ENABLE; 267 mh_mode|=CRYPTO_MEM_CHECK_ENABLE;
273 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); 268 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
274 }
275 } 269 }
276 } 270 }
271 }
277 break; 272 break;
278 273
279 default: 274 default:
280 break; 275 break;
281 }
282 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
283 return(ret);
284 } 276 }
277 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
278 return (ret);
279}
285 280
286int CRYPTO_is_mem_check_on(void) 281int
287 { 282CRYPTO_is_mem_check_on(void)
283{
288 int ret = 0; 284 int ret = 0;
289 285
290 if (mh_mode & CRYPTO_MEM_CHECK_ON) 286 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
291 {
292 CRYPTO_THREADID cur; 287 CRYPTO_THREADID cur;
293 CRYPTO_THREADID_current(&cur); 288 CRYPTO_THREADID_current(&cur);
294 CRYPTO_r_lock(CRYPTO_LOCK_MALLOC); 289 CRYPTO_r_lock(CRYPTO_LOCK_MALLOC);
295 290
296 ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) 291 ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) ||
297 || CRYPTO_THREADID_cmp(&disabling_threadid, &cur); 292 CRYPTO_THREADID_cmp(&disabling_threadid, &cur);
298 293
299 CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC); 294 CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC);
300 } 295 }
301 return(ret); 296 return (ret);
302 } 297}
303 298
304 299
305void CRYPTO_dbg_set_options(long bits) 300void
306 { 301CRYPTO_dbg_set_options(long bits)
302{
307 options = bits; 303 options = bits;
308 } 304}
309 305
310long CRYPTO_dbg_get_options(void) 306long
311 { 307CRYPTO_dbg_get_options(void)
308{
312 return options; 309 return options;
313 } 310}
314 311
315static int mem_cmp(const MEM *a, const MEM *b) 312static int
316 { 313mem_cmp(const MEM *a, const MEM *b)
314{
317#ifdef _WIN64 315#ifdef _WIN64
318 const char *ap=(const char *)a->addr, 316 const char *ap = (const char *)a->addr,
319 *bp=(const char *)b->addr; 317 *bp = (const char *)b->addr;
320 if (ap==bp) return 0; 318 if (ap == bp)
321 else if (ap>bp) return 1; 319 return 0;
322 else return -1; 320 else if (ap > bp) return 1;
321 else return -1;
323#else 322#else
324 return (const char *)a->addr - (const char *)b->addr; 323 return (const char *)a->addr - (const char *)b->addr;
325#endif 324#endif
326 } 325}
326
327static IMPLEMENT_LHASH_COMP_FN(mem, MEM) 327static IMPLEMENT_LHASH_COMP_FN(mem, MEM)
328 328
329static unsigned long mem_hash(const MEM *a) 329static unsigned long
330 { 330mem_hash(const MEM *a)
331{
331 unsigned long ret; 332 unsigned long ret;
332 333
333 ret=(unsigned long)a->addr; 334 ret = (unsigned long)a->addr;
334 335
335 ret=ret*17851+(ret>>14)*7+(ret>>4)*251; 336 ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
336 return(ret); 337 return (ret);
337 } 338}
338static IMPLEMENT_LHASH_HASH_FN(mem, MEM) 339
340static
341IMPLEMENT_LHASH_HASH_FN(mem, MEM)
339 342
340/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */ 343/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */
341static int app_info_cmp(const void *a_void, const void *b_void) 344static int
342 { 345app_info_cmp(const void *a_void, const void *b_void)
346{
343 return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid, 347 return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid,
344 &((const APP_INFO *)b_void)->threadid); 348 &((const APP_INFO *)b_void)->threadid);
345 } 349}
350
346static IMPLEMENT_LHASH_COMP_FN(app_info, APP_INFO) 351static IMPLEMENT_LHASH_COMP_FN(app_info, APP_INFO)
347 352
348static unsigned long app_info_hash(const APP_INFO *a) 353static unsigned long
349 { 354app_info_hash(const APP_INFO *a)
355{
350 unsigned long ret; 356 unsigned long ret;
351 357
352 ret = CRYPTO_THREADID_hash(&a->threadid); 358 ret = CRYPTO_THREADID_hash(&a->threadid);
353 /* This is left in as a "who am I to question legacy?" measure */ 359 /* This is left in as a "who am I to question legacy?" measure */
354 ret=ret*17851+(ret>>14)*7+(ret>>4)*251; 360 ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
355 return(ret); 361 return (ret);
356 } 362}
363
357static IMPLEMENT_LHASH_HASH_FN(app_info, APP_INFO) 364static IMPLEMENT_LHASH_HASH_FN(app_info, APP_INFO)
358 365
359static APP_INFO *pop_info(void) 366static APP_INFO
360 { 367*pop_info(void)
368{
361 APP_INFO tmp; 369 APP_INFO tmp;
362 APP_INFO *ret = NULL; 370 APP_INFO *ret = NULL;
363 371
364 if (amih != NULL) 372 if (amih != NULL) {
365 {
366 CRYPTO_THREADID_current(&tmp.threadid); 373 CRYPTO_THREADID_current(&tmp.threadid);
367 if ((ret=lh_APP_INFO_delete(amih,&tmp)) != NULL) 374 if ((ret = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
368 { 375 APP_INFO *next = ret->next;
369 APP_INFO *next=ret->next;
370 376
371 if (next != NULL) 377 if (next != NULL) {
372 {
373 next->references++; 378 next->references++;
374 (void)lh_APP_INFO_insert(amih,next); 379 (void)lh_APP_INFO_insert(amih, next);
375 } 380 }
376#ifdef LEVITTE_DEBUG_MEM 381#ifdef LEVITTE_DEBUG_MEM
377 if (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid)) 382 if (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid)) {
378 {
379 fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n", 383 fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n",
380 CRYPTO_THREADID_hash(&ret->threadid), 384 CRYPTO_THREADID_hash(&ret->threadid),
381 CRYPTO_THREADID_hash(&tmp.threadid)); 385 CRYPTO_THREADID_hash(&tmp.threadid));
382 abort(); 386 abort();
383 } 387 }
384#endif 388#endif
385 if (--(ret->references) <= 0) 389 if (--(ret->references) <= 0) {
386 {
387 ret->next = NULL; 390 ret->next = NULL;
388 if (next != NULL) 391 if (next != NULL)
389 next->references--; 392 next->references--;
390 OPENSSL_free(ret); 393 OPENSSL_free(ret);
391 }
392 } 394 }
393 } 395 }
394 return(ret);
395 } 396 }
397 return (ret);
398}
396 399
397int CRYPTO_push_info_(const char *info, const char *file, int line) 400int
398 { 401CRYPTO_push_info_(const char *info, const char *file, int line)
402{
399 APP_INFO *ami, *amim; 403 APP_INFO *ami, *amim;
400 int ret=0; 404 int ret = 0;
401 405
402 if (is_MemCheck_on()) 406 if (is_MemCheck_on()) {
403 {
404 MemCheck_off(); /* obtain MALLOC2 lock */ 407 MemCheck_off(); /* obtain MALLOC2 lock */
405 408
406 if ((ami = (APP_INFO *)OPENSSL_malloc(sizeof(APP_INFO))) == NULL) 409 if ((ami = (APP_INFO *)OPENSSL_malloc(sizeof(APP_INFO))) == NULL) {
407 { 410 ret = 0;
408 ret=0;
409 goto err; 411 goto err;
410 } 412 }
411 if (amih == NULL) 413 if (amih == NULL) {
412 { 414 if ((amih = lh_APP_INFO_new()) == NULL) {
413 if ((amih=lh_APP_INFO_new()) == NULL)
414 {
415 OPENSSL_free(ami); 415 OPENSSL_free(ami);
416 ret=0; 416 ret = 0;
417 goto err; 417 goto err;
418 }
419 } 418 }
419 }
420 420
421 CRYPTO_THREADID_current(&ami->threadid); 421 CRYPTO_THREADID_current(&ami->threadid);
422 ami->file=file; 422 ami->file = file;
423 ami->line=line; 423 ami->line = line;
424 ami->info=info; 424 ami->info = info;
425 ami->references=1; 425 ami->references = 1;
426 ami->next=NULL; 426 ami->next = NULL;
427 427
428 if ((amim=lh_APP_INFO_insert(amih,ami)) != NULL) 428 if ((amim = lh_APP_INFO_insert(amih, ami)) != NULL) {
429 {
430#ifdef LEVITTE_DEBUG_MEM 429#ifdef LEVITTE_DEBUG_MEM
431 if (CRYPTO_THREADID_cmp(&ami->threadid, &amim->threadid)) 430 if (CRYPTO_THREADID_cmp(&ami->threadid, &amim->threadid)) {
432 {
433 fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", 431 fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n",
434 CRYPTO_THREADID_hash(&amim->threadid), 432 CRYPTO_THREADID_hash(&amim->threadid),
435 CRYPTO_THREADID_hash(&ami->threadid)); 433 CRYPTO_THREADID_hash(&ami->threadid));
436 abort(); 434 abort();
437 }
438#endif
439 ami->next=amim;
440 } 435 }
441 err: 436#endif
442 MemCheck_on(); /* release MALLOC2 lock */ 437 ami->next = amim;
443 } 438 }
444 439 err:
445 return(ret); 440 MemCheck_on(); /* release MALLOC2 lock */
446 } 441 }
447 442
448int CRYPTO_pop_info(void) 443 return (ret);
449 { 444}
450 int ret=0; 445
446int
447CRYPTO_pop_info(void)
448{
449 int ret = 0;
451 450
452 if (is_MemCheck_on()) /* _must_ be true, or something went severely wrong */ 451 if (is_MemCheck_on()) /* _must_ be true, or something went severely wrong */
453 { 452 {
454 MemCheck_off(); /* obtain MALLOC2 lock */ 453 MemCheck_off(); /* obtain MALLOC2 lock */
455 454
456 ret=(pop_info() != NULL); 455 ret = (pop_info() != NULL);
457 456
458 MemCheck_on(); /* release MALLOC2 lock */ 457 MemCheck_on(); /* release MALLOC2 lock */
459 }
460 return(ret);
461 } 458 }
459 return (ret);
460}
462 461
463int CRYPTO_remove_all_info(void) 462int
464 { 463CRYPTO_remove_all_info(void)
465 int ret=0; 464{
465 int ret = 0;
466 466
467 if (is_MemCheck_on()) /* _must_ be true */ 467 if (is_MemCheck_on()) /* _must_ be true */
468 { 468 {
469 MemCheck_off(); /* obtain MALLOC2 lock */ 469 MemCheck_off(); /* obtain MALLOC2 lock */
470 470
471 while(pop_info() != NULL) 471 while (pop_info() != NULL)
472 ret++; 472 ret++;
473 473
474 MemCheck_on(); /* release MALLOC2 lock */ 474 MemCheck_on(); /* release MALLOC2 lock */
475 }
476 return(ret);
477 } 475 }
476 return (ret);
477}
478 478
479 479
480static unsigned long break_order_num=0; 480static unsigned long break_order_num = 0;
481void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, 481void
482 int before_p) 482CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
483 { 483 int before_p)
484 MEM *m,*mm; 484{
485 APP_INFO tmp,*amim; 485 MEM *m, *mm;
486 APP_INFO tmp, *amim;
486 487
487 switch(before_p & 127) 488 switch (before_p & 127) {
488 {
489 case 0: 489 case 0:
490 break; 490 break;
491 case 1: 491 case 1:
492 if (addr == NULL) 492 if (addr == NULL)
493 break; 493 break;
494 494
495 if (is_MemCheck_on()) 495 if (is_MemCheck_on()) {
496 {
497 MemCheck_off(); /* make sure we hold MALLOC2 lock */ 496 MemCheck_off(); /* make sure we hold MALLOC2 lock */
498 if ((m=(MEM *)OPENSSL_malloc(sizeof(MEM))) == NULL) 497 if ((m = (MEM *)OPENSSL_malloc(sizeof(MEM))) == NULL) {
499 {
500 OPENSSL_free(addr); 498 OPENSSL_free(addr);
501 MemCheck_on(); /* release MALLOC2 lock 499 MemCheck_on(); /* release MALLOC2 lock
502 * if num_disabled drops to 0 */ 500 * if num_disabled drops to 0 */
503 return; 501 return;
504 } 502 }
505 if (mh == NULL) 503 if (mh == NULL) {
506 { 504 if ((mh = lh_MEM_new()) == NULL) {
507 if ((mh=lh_MEM_new()) == NULL)
508 {
509 OPENSSL_free(addr); 505 OPENSSL_free(addr);
510 OPENSSL_free(m); 506 OPENSSL_free(m);
511 addr=NULL; 507 addr = NULL;
512 goto err; 508 goto err;
513 }
514 } 509 }
510 }
515 511
516 m->addr=addr; 512 m->addr = addr;
517 m->file=file; 513 m->file = file;
518 m->line=line; 514 m->line = line;
519 m->num=num; 515 m->num = num;
520 if (options & V_CRYPTO_MDEBUG_THREAD) 516 if (options & V_CRYPTO_MDEBUG_THREAD)
521 CRYPTO_THREADID_current(&m->threadid); 517 CRYPTO_THREADID_current(&m->threadid);
522 else 518 else
523 memset(&m->threadid, 0, sizeof(m->threadid)); 519 memset(&m->threadid, 0, sizeof(m->threadid));
524 520
525 if (order == break_order_num) 521 if (order == break_order_num) {
526 {
527 /* BREAK HERE */ 522 /* BREAK HERE */
528 m->order=order; 523 m->order = order;
529 } 524 }
530 m->order=order++; 525 m->order = order++;
531#ifdef LEVITTE_DEBUG_MEM 526#ifdef LEVITTE_DEBUG_MEM
532 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] %c 0x%p (%d)\n", 527 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] %c 0x%p (%d)\n",
533 m->order, 528 m->order,
534 (before_p & 128) ? '*' : '+', 529 (before_p & 128) ? '*' : '+',
535 m->addr, m->num); 530 m->addr, m->num);
536#endif 531#endif
537 if (options & V_CRYPTO_MDEBUG_TIME) 532 if (options & V_CRYPTO_MDEBUG_TIME)
538 m->time=time(NULL); 533 m->time = time(NULL);
539 else 534 else
540 m->time=0; 535 m->time = 0;
541 536
542 CRYPTO_THREADID_current(&tmp.threadid); 537 CRYPTO_THREADID_current(&tmp.threadid);
543 m->app_info=NULL; 538 m->app_info = NULL;
544 if (amih != NULL 539 if (amih != NULL &&
545 && (amim=lh_APP_INFO_retrieve(amih,&tmp)) != NULL) 540 (amim = lh_APP_INFO_retrieve(amih, &tmp)) != NULL) {
546 {
547 m->app_info = amim; 541 m->app_info = amim;
548 amim->references++; 542 amim->references++;
549 } 543 }
550 544
551 if ((mm=lh_MEM_insert(mh, m)) != NULL) 545 if ((mm = lh_MEM_insert(mh, m)) != NULL) {
552 {
553 /* Not good, but don't sweat it */ 546 /* Not good, but don't sweat it */
554 if (mm->app_info != NULL) 547 if (mm->app_info != NULL) {
555 {
556 mm->app_info->references--; 548 mm->app_info->references--;
557 }
558 OPENSSL_free(mm);
559 } 549 }
560 err: 550 OPENSSL_free(mm);
551 }
552err:
561 MemCheck_on(); /* release MALLOC2 lock 553 MemCheck_on(); /* release MALLOC2 lock
562 * if num_disabled drops to 0 */ 554 * if num_disabled drops to 0 */
563 }
564 break;
565 } 555 }
566 return; 556 break;
567 } 557 }
558 return;
559}
568 560
569void CRYPTO_dbg_free(void *addr, int before_p) 561void
570 { 562CRYPTO_dbg_free(void *addr, int before_p)
571 MEM m,*mp; 563{
564 MEM m, *mp;
572 565
573 switch(before_p) 566 switch (before_p) {
574 {
575 case 0: 567 case 0:
576 if (addr == NULL) 568 if (addr == NULL)
577 break; 569 break;
578 570
579 if (is_MemCheck_on() && (mh != NULL)) 571 if (is_MemCheck_on() && (mh != NULL)) {
580 {
581 MemCheck_off(); /* make sure we hold MALLOC2 lock */ 572 MemCheck_off(); /* make sure we hold MALLOC2 lock */
582 573
583 m.addr=addr; 574 m.addr = addr;
584 mp=lh_MEM_delete(mh,&m); 575 mp = lh_MEM_delete(mh, &m);
585 if (mp != NULL) 576 if (mp != NULL) {
586 {
587#ifdef LEVITTE_DEBUG_MEM 577#ifdef LEVITTE_DEBUG_MEM
588 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] - 0x%p (%d)\n", 578 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] - 0x%p (%d)\n",
589 mp->order, mp->addr, mp->num); 579 mp->order, mp->addr, mp->num);
590#endif 580#endif
591 if (mp->app_info != NULL) 581 if (mp->app_info != NULL)
592 app_info_free(mp->app_info); 582 app_info_free(mp->app_info);
593 OPENSSL_free(mp); 583 OPENSSL_free(mp);
594 } 584 }
595 585
596 MemCheck_on(); /* release MALLOC2 lock 586 MemCheck_on(); /* release MALLOC2 lock
597 * if num_disabled drops to 0 */ 587 * if num_disabled drops to 0 */
598 } 588 }
599 break; 589 break;
600 case 1: 590 case 1:
601 break; 591 break;
602 }
603 } 592 }
593}
604 594
605void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, 595void
606 const char *file, int line, int before_p) 596CRYPTO_dbg_realloc(void *addr1, void *addr2, int num,
607 { 597 const char *file, int line, int before_p)
608 MEM m,*mp; 598{
599 MEM m, *mp;
609 600
610#ifdef LEVITTE_DEBUG_MEM 601#ifdef LEVITTE_DEBUG_MEM
611 fprintf(stderr, "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n", 602 fprintf(stderr, "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n",
612 addr1, addr2, num, file, line, before_p); 603 addr1, addr2, num, file, line, before_p);
613#endif 604#endif
614 605
615 switch(before_p) 606 switch (before_p) {
616 {
617 case 0: 607 case 0:
618 break; 608 break;
619 case 1: 609 case 1:
620 if (addr2 == NULL) 610 if (addr2 == NULL)
621 break; 611 break;
622 612
623 if (addr1 == NULL) 613 if (addr1 == NULL) {
624 {
625 CRYPTO_dbg_malloc(addr2, num, file, line, 128 | before_p); 614 CRYPTO_dbg_malloc(addr2, num, file, line, 128 | before_p);
626 break; 615 break;
627 } 616 }
628 617
629 if (is_MemCheck_on()) 618 if (is_MemCheck_on()) {
630 {
631 MemCheck_off(); /* make sure we hold MALLOC2 lock */ 619 MemCheck_off(); /* make sure we hold MALLOC2 lock */
632 620
633 m.addr=addr1; 621 m.addr = addr1;
634 mp=lh_MEM_delete(mh,&m); 622 mp = lh_MEM_delete(mh, &m);
635 if (mp != NULL) 623 if (mp != NULL) {
636 {
637#ifdef LEVITTE_DEBUG_MEM 624#ifdef LEVITTE_DEBUG_MEM
638 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] * 0x%p (%d) -> 0x%p (%d)\n", 625 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] * 0x%p (%d) -> 0x%p (%d)\n",
639 mp->order, 626 mp->order,
640 mp->addr, mp->num, 627 mp->addr, mp->num,
641 addr2, num); 628 addr2, num);
642#endif 629#endif
643 mp->addr=addr2; 630 mp->addr = addr2;
644 mp->num=num; 631 mp->num = num;
645 (void)lh_MEM_insert(mh,mp); 632 (void)lh_MEM_insert(mh, mp);
646 } 633 }
647 634
648 MemCheck_on(); /* release MALLOC2 lock 635 MemCheck_on(); /* release MALLOC2 lock
649 * if num_disabled drops to 0 */ 636 * if num_disabled drops to 0 */
650 }
651 break;
652 } 637 }
653 return; 638 break;
654 } 639 }
640 return;
641}
655 642
656 643
657typedef struct mem_leak_st 644typedef struct mem_leak_st {
658 {
659 BIO *bio; 645 BIO *bio;
660 int chunks; 646 int chunks;
661 long bytes; 647 long bytes;
662 } MEM_LEAK; 648} MEM_LEAK;
663 649
664static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l) 650static void
665 { 651print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
652{
666 char buf[1024]; 653 char buf[1024];
667 char *bufp = buf; 654 char *bufp = buf;
668 APP_INFO *amip; 655 APP_INFO *amip;
@@ -672,89 +659,83 @@ static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
672 659
673#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf)) 660#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
674 661
675 if(m->addr == (char *)l->bio) 662 if (m->addr == (char *)l->bio)
676 return; 663 return;
677 664
678 if (options & V_CRYPTO_MDEBUG_TIME) 665 if (options & V_CRYPTO_MDEBUG_TIME) {
679 {
680 lcl = localtime(&m->time); 666 lcl = localtime(&m->time);
681 667
682 BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ", 668 BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ",
683 lcl->tm_hour,lcl->tm_min,lcl->tm_sec); 669 lcl->tm_hour, lcl->tm_min, lcl->tm_sec);
684 bufp += strlen(bufp); 670 bufp += strlen(bufp);
685 } 671 }
686 672
687 BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ", 673 BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",
688 m->order,m->file,m->line); 674 m->order, m->file, m->line);
689 bufp += strlen(bufp); 675 bufp += strlen(bufp);
690 676
691 if (options & V_CRYPTO_MDEBUG_THREAD) 677 if (options & V_CRYPTO_MDEBUG_THREAD) {
692 {
693 BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ", 678 BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ",
694 CRYPTO_THREADID_hash(&m->threadid)); 679 CRYPTO_THREADID_hash(&m->threadid));
695 bufp += strlen(bufp); 680 bufp += strlen(bufp);
696 } 681 }
697 682
698 BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%08lX\n", 683 BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%08lX\n",
699 m->num,(unsigned long)m->addr); 684 m->num,(unsigned long)m->addr);
700 bufp += strlen(bufp); 685 bufp += strlen(bufp);
701 686
702 BIO_puts(l->bio,buf); 687 BIO_puts(l->bio, buf);
703 688
704 l->chunks++; 689 l->chunks++;
705 l->bytes+=m->num; 690 l->bytes += m->num;
706 691
707 amip=m->app_info; 692 amip = m->app_info;
708 ami_cnt=0; 693 ami_cnt = 0;
709 if (!amip) 694 if (!amip)
710 return; 695 return;
711 CRYPTO_THREADID_cpy(&ti, &amip->threadid); 696 CRYPTO_THREADID_cpy(&ti, &amip->threadid);
712 697
713 do 698 do {
714 {
715 int buf_len; 699 int buf_len;
716 int info_len; 700 int info_len;
717 701
718 ami_cnt++; 702 ami_cnt++;
719 memset(buf,'>',ami_cnt); 703 memset(buf, '>', ami_cnt);
720 BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt, 704 BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,
721 " thread=%lu, file=%s, line=%d, info=\"", 705 " thread=%lu, file=%s, line=%d, info=\"",
722 CRYPTO_THREADID_hash(&amip->threadid), amip->file, 706 CRYPTO_THREADID_hash(&amip->threadid), amip->file,
723 amip->line); 707 amip->line);
724 buf_len=strlen(buf); 708 buf_len = strlen(buf);
725 info_len=strlen(amip->info); 709 info_len = strlen(amip->info);
726 if (128 - buf_len - 3 < info_len) 710 if (128 - buf_len - 3 < info_len) {
727 {
728 memcpy(buf + buf_len, amip->info, 128 - buf_len - 3); 711 memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
729 buf_len = 128 - 3; 712 buf_len = 128 - 3;
730 } 713 } else {
731 else
732 {
733 BUF_strlcpy(buf + buf_len, amip->info, 714 BUF_strlcpy(buf + buf_len, amip->info,
734 sizeof buf - buf_len); 715 sizeof buf - buf_len);
735 buf_len = strlen(buf); 716 buf_len = strlen(buf);
736 } 717 }
737 BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n"); 718 BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n");
738 719
739 BIO_puts(l->bio,buf); 720 BIO_puts(l->bio, buf);
740 721
741 amip = amip->next; 722 amip = amip->next;
742 } 723 } while (amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti));
743 while(amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti));
744 724
745#ifdef LEVITTE_DEBUG_MEM 725#ifdef LEVITTE_DEBUG_MEM
746 if (amip) 726 if (amip) {
747 {
748 fprintf(stderr, "Thread switch detected in backtrace!!!!\n"); 727 fprintf(stderr, "Thread switch detected in backtrace!!!!\n");
749 abort(); 728 abort();
750 }
751#endif
752 } 729 }
730#endif
731}
753 732
754static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK) 733static
734IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK)
755 735
756void CRYPTO_mem_leaks(BIO *b) 736void
757 { 737CRYPTO_mem_leaks(BIO *b)
738{
758 MEM_LEAK ml; 739 MEM_LEAK ml;
759 740
760 if (mh == NULL && amih == NULL) 741 if (mh == NULL && amih == NULL)
@@ -762,22 +743,19 @@ void CRYPTO_mem_leaks(BIO *b)
762 743
763 MemCheck_off(); /* obtain MALLOC2 lock */ 744 MemCheck_off(); /* obtain MALLOC2 lock */
764 745
765 ml.bio=b; 746 ml.bio = b;
766 ml.bytes=0; 747 ml.bytes = 0;
767 ml.chunks=0; 748 ml.chunks = 0;
768 if (mh != NULL) 749 if (mh != NULL)
769 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), MEM_LEAK, 750 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak),
770 &ml); 751 MEM_LEAK, &ml);
771 if (ml.chunks != 0) 752 if (ml.chunks != 0) {
772 { 753 BIO_printf(b, "%ld bytes leaked in %d chunks\n",
773 BIO_printf(b,"%ld bytes leaked in %d chunks\n", 754 ml.bytes, ml.chunks);
774 ml.bytes,ml.chunks);
775#ifdef CRYPTO_MDEBUG_ABORT 755#ifdef CRYPTO_MDEBUG_ABORT
776 abort(); 756 abort();
777#endif 757#endif
778 } 758 } else {
779 else
780 {
781 /* Make sure that, if we found no leaks, memory-leak debugging itself 759 /* Make sure that, if we found no leaks, memory-leak debugging itself
782 * does not introduce memory leaks (which might irritate 760 * does not introduce memory leaks (which might irritate
783 * external debugging tools). 761 * external debugging tools).
@@ -802,43 +780,43 @@ void CRYPTO_mem_leaks(BIO *b)
802 old_mh_mode = mh_mode; 780 old_mh_mode = mh_mode;
803 mh_mode = CRYPTO_MEM_CHECK_OFF; 781 mh_mode = CRYPTO_MEM_CHECK_OFF;
804 782
805 if (mh != NULL) 783 if (mh != NULL) {
806 {
807 lh_MEM_free(mh); 784 lh_MEM_free(mh);
808 mh = NULL; 785 mh = NULL;
809 } 786 }
810 if (amih != NULL) 787 if (amih != NULL) {
811 { 788 if (lh_APP_INFO_num_items(amih) == 0) {
812 if (lh_APP_INFO_num_items(amih) == 0)
813 {
814 lh_APP_INFO_free(amih); 789 lh_APP_INFO_free(amih);
815 amih = NULL; 790 amih = NULL;
816 }
817 } 791 }
792 }
818 793
819 mh_mode = old_mh_mode; 794 mh_mode = old_mh_mode;
820 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); 795 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
821 }
822 MemCheck_on(); /* release MALLOC2 lock */
823 } 796 }
797 MemCheck_on(); /* release MALLOC2 lock */
798}
824 799
825#ifndef OPENSSL_NO_FP_API 800#ifndef OPENSSL_NO_FP_API
826void CRYPTO_mem_leaks_fp(FILE *fp) 801void
827 { 802CRYPTO_mem_leaks_fp(FILE *fp)
803{
828 BIO *b; 804 BIO *b;
829 805
830 if (mh == NULL) return; 806 if (mh == NULL)
807 return;
831 /* Need to turn off memory checking when allocated BIOs ... especially 808 /* Need to turn off memory checking when allocated BIOs ... especially
832 * as we're creating them at a time when we're trying to check we've not 809 * as we're creating them at a time when we're trying to check we've not
833 * left anything un-free()'d!! */ 810 * left anything un-free()'d!! */
834 MemCheck_off(); 811 MemCheck_off();
835 b = BIO_new(BIO_s_file()); 812 b = BIO_new(BIO_s_file());
836 MemCheck_on(); 813 MemCheck_on();
837 if(!b) return; 814 if (!b)
838 BIO_set_fp(b,fp,BIO_NOCLOSE); 815 return;
816 BIO_set_fp(b, fp, BIO_NOCLOSE);
839 CRYPTO_mem_leaks(b); 817 CRYPTO_mem_leaks(b);
840 BIO_free(b); 818 BIO_free(b);
841 } 819}
842#endif 820#endif
843 821
844 822
@@ -857,18 +835,21 @@ void CRYPTO_mem_leaks_fp(FILE *fp)
857 835
858typedef CRYPTO_MEM_LEAK_CB *PCRYPTO_MEM_LEAK_CB; 836typedef CRYPTO_MEM_LEAK_CB *PCRYPTO_MEM_LEAK_CB;
859 837
860static void cb_leak_doall_arg(const MEM *m, PCRYPTO_MEM_LEAK_CB *cb) 838static void
861 { 839cb_leak_doall_arg(const MEM *m, PCRYPTO_MEM_LEAK_CB *cb)
862 (*cb)(m->order,m->file,m->line,m->num,m->addr); 840{
863 } 841 (*cb)(m->order, m->file, m->line, m->num, m->addr);
842}
864 843
865static IMPLEMENT_LHASH_DOALL_ARG_FN(cb_leak, const MEM, PCRYPTO_MEM_LEAK_CB) 844static IMPLEMENT_LHASH_DOALL_ARG_FN(cb_leak, const MEM, PCRYPTO_MEM_LEAK_CB)
866 845
867void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb) 846void
868 { 847CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb)
869 if (mh == NULL) return; 848{
849 if (mh == NULL)
850 return;
870 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); 851 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
871 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), PCRYPTO_MEM_LEAK_CB, 852 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), PCRYPTO_MEM_LEAK_CB,
872 &cb); 853 &cb);
873 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); 854 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
874 } 855}
diff --git a/src/lib/libcrypto/o_dir_test.c b/src/lib/libcrypto/o_dir_test.c
index 238717c110..2973458a94 100644
--- a/src/lib/libcrypto/o_dir_test.c
+++ b/src/lib/libcrypto/o_dir_test.c
@@ -43,26 +43,24 @@
43#error "No supported platform defined!" 43#error "No supported platform defined!"
44#endif 44#endif
45 45
46int main() 46int
47main()
47{ 48{
48 OPENSSL_DIR_CTX *ctx = NULL; 49 OPENSSL_DIR_CTX *ctx = NULL;
49 const char *result; 50 const char *result;
50 51
51 while((result = OPENSSL_DIR_read(&ctx, CURRDIR)) != NULL) 52 while ((result = OPENSSL_DIR_read(&ctx, CURRDIR)) != NULL) {
52 { 53 printf("%s\n", result);
53 printf("%s\n", result); 54 }
54 }
55 55
56 if (errno) 56 if (errno) {
57 { 57 perror("test_dir");
58 perror("test_dir"); 58 exit(1);
59 exit(1); 59 }
60 }
61 60
62 if (!OPENSSL_DIR_end(&ctx)) 61 if (!OPENSSL_DIR_end(&ctx)) {
63 { 62 perror("test_dir");
64 perror("test_dir"); 63 exit(2);
65 exit(2); 64 }
66 } 65 exit(0);
67 exit(0);
68} 66}
diff --git a/src/lib/libcrypto/o_fips.c b/src/lib/libcrypto/o_fips.c
index f6d1b21855..9c185cfb18 100644
--- a/src/lib/libcrypto/o_fips.c
+++ b/src/lib/libcrypto/o_fips.c
@@ -62,18 +62,20 @@
62#include <openssl/rand.h> 62#include <openssl/rand.h>
63#endif 63#endif
64 64
65int FIPS_mode(void) 65int
66 { 66FIPS_mode(void)
67{
67 OPENSSL_init(); 68 OPENSSL_init();
68#ifdef OPENSSL_FIPS 69#ifdef OPENSSL_FIPS
69 return FIPS_module_mode(); 70 return FIPS_module_mode();
70#else 71#else
71 return 0; 72 return 0;
72#endif 73#endif
73 } 74}
74 75
75int FIPS_mode_set(int r) 76int
76 { 77FIPS_mode_set(int r)
78{
77 OPENSSL_init(); 79 OPENSSL_init();
78#ifdef OPENSSL_FIPS 80#ifdef OPENSSL_FIPS
79#ifndef FIPS_AUTH_USER_PASS 81#ifndef FIPS_AUTH_USER_PASS
@@ -92,5 +94,4 @@ int FIPS_mode_set(int r)
92 CRYPTOerr(CRYPTO_F_FIPS_MODE_SET, CRYPTO_R_FIPS_MODE_NOT_SUPPORTED); 94 CRYPTOerr(CRYPTO_F_FIPS_MODE_SET, CRYPTO_R_FIPS_MODE_NOT_SUPPORTED);
93 return 0; 95 return 0;
94#endif 96#endif
95 } 97}
96
diff --git a/src/lib/libcrypto/o_init.c b/src/lib/libcrypto/o_init.c
index db4cdc443b..07c8e0d694 100644
--- a/src/lib/libcrypto/o_init.c
+++ b/src/lib/libcrypto/o_init.c
@@ -63,8 +63,9 @@
63 * Currently only sets FIPS callbacks 63 * Currently only sets FIPS callbacks
64 */ 64 */
65 65
66void OPENSSL_init(void) 66void
67 { 67OPENSSL_init(void)
68{
68 static int done = 0; 69 static int done = 0;
69 if (done) 70 if (done)
70 return; 71 return;
@@ -78,5 +79,4 @@ void OPENSSL_init(void)
78#if 0 79#if 0
79 fprintf(stderr, "Called OPENSSL_init\n"); 80 fprintf(stderr, "Called OPENSSL_init\n");
80#endif 81#endif
81 } 82}
82
diff --git a/src/lib/libcrypto/o_str.c b/src/lib/libcrypto/o_str.c
index 56104a6c34..c3ab1f8093 100644
--- a/src/lib/libcrypto/o_str.c
+++ b/src/lib/libcrypto/o_str.c
@@ -66,17 +66,18 @@
66# include <strings.h> 66# include <strings.h>
67#endif 67#endif
68 68
69int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n) 69int
70 { 70OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
71{
71#if defined(OPENSSL_IMPLEMENTS_strncasecmp) 72#if defined(OPENSSL_IMPLEMENTS_strncasecmp)
72 while (*str1 && *str2 && n) 73 while (*str1 && *str2 && n) {
73 {
74 int res = toupper(*str1) - toupper(*str2); 74 int res = toupper(*str1) - toupper(*str2);
75 if (res) return res < 0 ? -1 : 1; 75 if (res)
76 return res < 0 ? -1 : 1;
76 str1++; 77 str1++;
77 str2++; 78 str2++;
78 n--; 79 n--;
79 } 80 }
80 if (n == 0) 81 if (n == 0)
81 return 0; 82 return 0;
82 if (*str1) 83 if (*str1)
@@ -90,22 +91,25 @@ int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
90 * defined as well. */ 91 * defined as well. */
91 return strncasecmp(str1, str2, n); 92 return strncasecmp(str1, str2, n);
92#endif 93#endif
93 } 94}
94int OPENSSL_strcasecmp(const char *str1, const char *str2) 95
95 { 96int
97OPENSSL_strcasecmp(const char *str1, const char *str2)
98{
96#if defined(OPENSSL_IMPLEMENTS_strncasecmp) 99#if defined(OPENSSL_IMPLEMENTS_strncasecmp)
97 return OPENSSL_strncasecmp(str1, str2, (size_t)-1); 100 return OPENSSL_strncasecmp(str1, str2, (size_t) - 1);
98#else 101#else
99 return strcasecmp(str1, str2); 102 return strcasecmp(str1, str2);
100#endif 103#endif
101 } 104}
102 105
103int OPENSSL_memcmp(const void *v1,const void *v2,size_t n) 106int
104 { 107OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
105 const unsigned char *c1=v1,*c2=v2; 108{
106 int ret=0; 109 const unsigned char *c1 = v1, *c2 = v2;
110 int ret = 0;
107 111
108 while(n && (ret=*c1-*c2)==0) n--,c1++,c2++; 112 while (n && (ret = *c1 - *c2) == 0) n--, c1++, c2++;
109 113
110 return ret; 114 return ret;
111 } 115}
diff --git a/src/lib/libcrypto/o_str.h b/src/lib/libcrypto/o_str.h
index dfc98494c6..e2a3fde299 100644
--- a/src/lib/libcrypto/o_str.h
+++ b/src/lib/libcrypto/o_str.h
@@ -63,6 +63,6 @@
63 63
64int OPENSSL_strcasecmp(const char *str1, const char *str2); 64int OPENSSL_strcasecmp(const char *str1, const char *str2);
65int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n); 65int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n);
66int OPENSSL_memcmp(const void *p1,const void *p2,size_t n); 66int OPENSSL_memcmp(const void *p1, const void *p2, size_t n);
67 67
68#endif 68#endif
diff --git a/src/lib/libcrypto/o_time.c b/src/lib/libcrypto/o_time.c
index 5fb5d4e6d7..44f7ba3b8c 100644
--- a/src/lib/libcrypto/o_time.c
+++ b/src/lib/libcrypto/o_time.c
@@ -63,14 +63,14 @@
63#include <string.h> 63#include <string.h>
64#include "o_time.h" 64#include "o_time.h"
65 65
66struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result) 66struct tm
67 { 67*OPENSSL_gmtime(const time_t *timer, struct tm *result) {
68 struct tm *ts = NULL; 68 struct tm *ts = NULL;
69 69
70#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS) 70#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
71 /* should return &data, but doesn't on some systems, 71 /* should return &data, but doesn't on some systems,
72 so we don't even look at the return value */ 72 so we don't even look at the return value */
73 gmtime_r(timer,result); 73 gmtime_r(timer, result);
74 ts = result; 74 ts = result;
75#else 75#else
76 ts = gmtime(timer); 76 ts = gmtime(timer);
@@ -81,7 +81,7 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
81 ts = result; 81 ts = result;
82#endif 82#endif
83 return ts; 83 return ts;
84 } 84}
85 85
86/* Take a tm structure and add an offset to it. This avoids any OS issues 86/* Take a tm structure and add an offset to it. This avoids any OS issues
87 * with restricted date types and overflows which cause the year 2038 87 * with restricted date types and overflows which cause the year 2038
@@ -93,29 +93,27 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
93static long date_to_julian(int y, int m, int d); 93static long date_to_julian(int y, int m, int d);
94static void julian_to_date(long jd, int *y, int *m, int *d); 94static void julian_to_date(long jd, int *y, int *m, int *d);
95 95
96int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) 96int
97 { 97OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
98{
98 int offset_hms, offset_day; 99 int offset_hms, offset_day;
99 long time_jd; 100 long time_jd;
100 int time_year, time_month, time_day; 101 int time_year, time_month, time_day;
101 /* split offset into days and day seconds */ 102 /* split offset into days and day seconds */
102 offset_day = offset_sec / SECS_PER_DAY; 103 offset_day = offset_sec / SECS_PER_DAY;
103 /* Avoid sign issues with % operator */ 104 /* Avoid sign issues with % operator */
104 offset_hms = offset_sec - (offset_day * SECS_PER_DAY); 105 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
105 offset_day += off_day; 106 offset_day += off_day;
106 /* Add current time seconds to offset */ 107 /* Add current time seconds to offset */
107 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; 108 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
108 /* Adjust day seconds if overflow */ 109 /* Adjust day seconds if overflow */
109 if (offset_hms >= SECS_PER_DAY) 110 if (offset_hms >= SECS_PER_DAY) {
110 {
111 offset_day++; 111 offset_day++;
112 offset_hms -= SECS_PER_DAY; 112 offset_hms -= SECS_PER_DAY;
113 } 113 } else if (offset_hms < 0) {
114 else if (offset_hms < 0)
115 {
116 offset_day--; 114 offset_day--;
117 offset_hms += SECS_PER_DAY; 115 offset_hms += SECS_PER_DAY;
118 } 116 }
119 117
120 /* Convert date of time structure into a Julian day number. 118 /* Convert date of time structure into a Julian day number.
121 */ 119 */
@@ -150,22 +148,24 @@ int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
150 tm->tm_sec = offset_hms % 60; 148 tm->tm_sec = offset_hms % 60;
151 149
152 return 1; 150 return 1;
153 151
154} 152}
155 153
156/* Convert date to and from julian day 154/* Convert date to and from julian day
157 * Uses Fliegel & Van Flandern algorithm 155 * Uses Fliegel & Van Flandern algorithm
158 */ 156 */
159static long date_to_julian(int y, int m, int d) 157static long
158date_to_julian(int y, int m, int d)
160{ 159{
161 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 + 160 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
162 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - 161 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
163 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + 162 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 +
164 d - 32075; 163 d - 32075;
165} 164}
166 165
167static void julian_to_date(long jd, int *y, int *m, int *d) 166static void
168 { 167julian_to_date(long jd, int *y, int *m, int *d)
168{
169 long L = jd + 68569; 169 long L = jd + 68569;
170 long n = (4 * L) / 146097; 170 long n = (4 * L) / 146097;
171 long i, j; 171 long i, j;
@@ -178,7 +178,7 @@ static void julian_to_date(long jd, int *y, int *m, int *d)
178 L = j / 11; 178 L = j / 11;
179 *m = j + 2 - (12 * L); 179 *m = j + 2 - (12 * L);
180 *y = 100 * (n - 49) + i + L; 180 *y = 100 * (n - 49) + i + L;
181 } 181}
182 182
183#ifdef OPENSSL_TIME_TEST 183#ifdef OPENSSL_TIME_TEST
184 184
@@ -189,20 +189,21 @@ static void julian_to_date(long jd, int *y, int *m, int *d)
189 * trigger the very errors the routines fix. 189 * trigger the very errors the routines fix.
190 */ 190 */
191 191
192int main(int argc, char **argv) 192int
193 { 193main(int argc, char **argv)
194{
194 long offset; 195 long offset;
195 for (offset = 0; offset < 1000000; offset++) 196 for (offset = 0; offset < 1000000; offset++) {
196 {
197 check_time(offset); 197 check_time(offset);
198 check_time(-offset); 198 check_time(-offset);
199 check_time(offset * 1000); 199 check_time(offset * 1000);
200 check_time(-offset * 1000); 200 check_time(-offset * 1000);
201 }
202 } 201 }
202}
203 203
204int check_time(long offset) 204int
205 { 205check_time(long offset)
206{
206 struct tm tm1, tm2; 207 struct tm tm1, tm2;
207 time_t t1, t2; 208 time_t t1, t2;
208 time(&t1); 209 time(&t1);
@@ -211,20 +212,20 @@ int check_time(long offset)
211 OPENSSL_gmtime(&t1, &tm1); 212 OPENSSL_gmtime(&t1, &tm1);
212 OPENSSL_gmtime_adj(&tm1, 0, offset); 213 OPENSSL_gmtime_adj(&tm1, 0, offset);
213 if ((tm1.tm_year == tm2.tm_year) && 214 if ((tm1.tm_year == tm2.tm_year) &&
214 (tm1.tm_mon == tm2.tm_mon) && 215 (tm1.tm_mon == tm2.tm_mon) &&
215 (tm1.tm_mday == tm2.tm_mday) && 216 (tm1.tm_mday == tm2.tm_mday) &&
216 (tm1.tm_hour == tm2.tm_hour) && 217 (tm1.tm_hour == tm2.tm_hour) &&
217 (tm1.tm_min == tm2.tm_min) && 218 (tm1.tm_min == tm2.tm_min) &&
218 (tm1.tm_sec == tm2.tm_sec)) 219 (tm1.tm_sec == tm2.tm_sec))
219 return 1; 220 return 1;
220 fprintf(stderr, "TIME ERROR!!\n"); 221 fprintf(stderr, "TIME ERROR!!\n");
221 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n", 222 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
222 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900, 223 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
223 tm2.tm_hour, tm2.tm_min, tm2.tm_sec); 224 tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
224 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n", 225 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
225 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900, 226 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
226 tm1.tm_hour, tm1.tm_min, tm1.tm_sec); 227 tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
227 return 0; 228 return 0;
228 } 229}
229 230
230#endif 231#endif
diff --git a/src/lib/libcrypto/ossl_typ.h b/src/lib/libcrypto/ossl_typ.h
index ea9227f6f9..ae79bf8ae4 100644
--- a/src/lib/libcrypto/ossl_typ.h
+++ b/src/lib/libcrypto/ossl_typ.h
@@ -182,18 +182,18 @@ typedef struct DIST_POINT_st DIST_POINT;
182typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT; 182typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT;
183typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS; 183typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS;
184 184
185 /* If placed in pkcs12.h, we end up with a circular depency with pkcs7.h */ 185/* If placed in pkcs12.h, we end up with a circular depency with pkcs7.h */
186#define DECLARE_PKCS12_STACK_OF(type) /* Nothing */ 186#define DECLARE_PKCS12_STACK_OF(type) /* Nothing */
187#define IMPLEMENT_PKCS12_STACK_OF(type) /* Nothing */ 187#define IMPLEMENT_PKCS12_STACK_OF(type) /* Nothing */
188 188
189typedef struct crypto_ex_data_st CRYPTO_EX_DATA; 189typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
190/* Callback types for crypto.h */ 190/* Callback types for crypto.h */
191typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 191typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
192 int idx, long argl, void *argp); 192 int idx, long argl, void *argp);
193typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 193typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
194 int idx, long argl, void *argp); 194 int idx, long argl, void *argp);
195typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, void *from_d, 195typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
196 int idx, long argl, void *argp); 196 void *from_d, int idx, long argl, void *argp);
197 197
198typedef struct ocsp_req_ctx_st OCSP_REQ_CTX; 198typedef struct ocsp_req_ctx_st OCSP_REQ_CTX;
199typedef struct ocsp_response_st OCSP_RESPONSE; 199typedef struct ocsp_response_st OCSP_RESPONSE;
diff --git a/src/lib/libcrypto/ppccap.c b/src/lib/libcrypto/ppccap.c
index f71ba66aa3..16cc9c445e 100644
--- a/src/lib/libcrypto/ppccap.c
+++ b/src/lib/libcrypto/ppccap.c
@@ -15,112 +15,110 @@ static int OPENSSL_ppccap_P = 0;
15static sigset_t all_masked; 15static sigset_t all_masked;
16 16
17#ifdef OPENSSL_BN_ASM_MONT 17#ifdef OPENSSL_BN_ASM_MONT
18int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num) 18int
19 { 19bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
20 const BN_ULONG *np, const BN_ULONG *n0, int num)
21{
20 int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); 22 int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
21 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); 23 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
22 24
23 if (sizeof(size_t)==4) 25 if (sizeof(size_t) == 4) {
24 {
25#if (defined(__APPLE__) && defined(__MACH__)) 26#if (defined(__APPLE__) && defined(__MACH__))
26 if (num>=8 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64)) 27 if (num >= 8 && (num&3) == 0 && (OPENSSL_ppccap_P&PPC_FPU64))
27 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num); 28 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
28#else 29#else
29 /* boundary of 32 was experimentally determined on 30 /* boundary of 32 was experimentally determined on
30 Linux 2.6.22, might have to be adjusted on AIX... */ 31 Linux 2.6.22, might have to be adjusted on AIX... */
31 if (num>=32 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64)) 32 if (num >= 32 && (num&3) == 0 && (OPENSSL_ppccap_P&PPC_FPU64)) {
32 {
33 sigset_t oset; 33 sigset_t oset;
34 int ret; 34 int ret;
35 35
36 sigprocmask(SIG_SETMASK,&all_masked,&oset); 36 sigprocmask(SIG_SETMASK, &all_masked, &oset);
37 ret=bn_mul_mont_fpu64(rp,ap,bp,np,n0,num); 37 ret = bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
38 sigprocmask(SIG_SETMASK,&oset,NULL); 38 sigprocmask(SIG_SETMASK, &oset, NULL);
39 39
40 return ret; 40 return ret;
41 }
42#endif
43 } 41 }
44 else if ((OPENSSL_ppccap_P&PPC_FPU64)) 42#endif
43 } else if ((OPENSSL_ppccap_P&PPC_FPU64))
45 /* this is a "must" on POWER6, but run-time detection 44 /* this is a "must" on POWER6, but run-time detection
46 * is not implemented yet... */ 45 * is not implemented yet... */
47 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num); 46 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
48 47
49 return bn_mul_mont_int(rp,ap,bp,np,n0,num); 48 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
50 } 49}
51#endif 50#endif
52 51
53static sigjmp_buf ill_jmp; 52static sigjmp_buf ill_jmp;
54static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); } 53static void ill_handler (int sig)
54{
55 siglongjmp(ill_jmp, sig);
56}
55 57
56void OPENSSL_ppc64_probe(void); 58void OPENSSL_ppc64_probe(void);
57void OPENSSL_altivec_probe(void); 59void OPENSSL_altivec_probe(void);
58 60
59void OPENSSL_cpuid_setup(void) 61void
60 { 62OPENSSL_cpuid_setup(void)
63{
61 char *e; 64 char *e;
62 struct sigaction ill_oact,ill_act; 65 struct sigaction ill_oact, ill_act;
63 sigset_t oset; 66 sigset_t oset;
64 static int trigger=0; 67 static int trigger = 0;
68
69 if (trigger)
70 return;
71 trigger = 1;
65 72
66 if (trigger) return;
67 trigger=1;
68
69 sigfillset(&all_masked); 73 sigfillset(&all_masked);
70 sigdelset(&all_masked,SIGILL); 74 sigdelset(&all_masked, SIGILL);
71 sigdelset(&all_masked,SIGTRAP); 75 sigdelset(&all_masked, SIGTRAP);
72#ifdef SIGEMT 76#ifdef SIGEMT
73 sigdelset(&all_masked,SIGEMT); 77 sigdelset(&all_masked, SIGEMT);
74#endif 78#endif
75 sigdelset(&all_masked,SIGFPE); 79 sigdelset(&all_masked, SIGFPE);
76 sigdelset(&all_masked,SIGBUS); 80 sigdelset(&all_masked, SIGBUS);
77 sigdelset(&all_masked,SIGSEGV); 81 sigdelset(&all_masked, SIGSEGV);
78 82
79 if ((e=getenv("OPENSSL_ppccap"))) 83 if ((e = getenv("OPENSSL_ppccap"))) {
80 { 84 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
81 OPENSSL_ppccap_P=strtoul(e,NULL,0);
82 return; 85 return;
83 } 86 }
84 87
85 OPENSSL_ppccap_P = 0; 88 OPENSSL_ppccap_P = 0;
86 89
87#if defined(_AIX) 90#if defined(_AIX)
88 if (sizeof(size_t)==4 91 if (sizeof(size_t) == 4
89# if defined(_SC_AIX_KERNEL_BITMODE) 92# if defined(_SC_AIX_KERNEL_BITMODE)
90 && sysconf(_SC_AIX_KERNEL_BITMODE)!=64 93 && sysconf(_SC_AIX_KERNEL_BITMODE) != 64
91# endif 94# endif
92 ) 95 )
93 return; 96 return;
94#endif 97#endif
95 98
96 memset(&ill_act,0,sizeof(ill_act)); 99 memset(&ill_act, 0, sizeof(ill_act));
97 ill_act.sa_handler = ill_handler; 100 ill_act.sa_handler = ill_handler;
98 ill_act.sa_mask = all_masked; 101 ill_act.sa_mask = all_masked;
99 102
100 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset); 103 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
101 sigaction(SIGILL,&ill_act,&ill_oact); 104 sigaction(SIGILL, &ill_act, &ill_oact);
102 105
103 if (sizeof(size_t)==4) 106 if (sizeof(size_t) == 4) {
104 { 107 if (sigsetjmp(ill_jmp, 1) == 0) {
105 if (sigsetjmp(ill_jmp,1) == 0)
106 {
107 OPENSSL_ppc64_probe(); 108 OPENSSL_ppc64_probe();
108 OPENSSL_ppccap_P |= PPC_FPU64; 109 OPENSSL_ppccap_P |= PPC_FPU64;
109 }
110 } 110 }
111 else 111 } else {
112 {
113 /* 112 /*
114 * Wanted code detecting POWER6 CPU and setting PPC_FPU64 113 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
115 */ 114 */
116 } 115 }
117 116
118 if (sigsetjmp(ill_jmp,1) == 0) 117 if (sigsetjmp(ill_jmp, 1) == 0) {
119 {
120 OPENSSL_altivec_probe(); 118 OPENSSL_altivec_probe();
121 OPENSSL_ppccap_P |= PPC_ALTIVEC; 119 OPENSSL_ppccap_P |= PPC_ALTIVEC;
122 }
123
124 sigaction (SIGILL,&ill_oact,NULL);
125 sigprocmask(SIG_SETMASK,&oset,NULL);
126 } 120 }
121
122 sigaction (SIGILL, &ill_oact, NULL);
123 sigprocmask(SIG_SETMASK, &oset, NULL);
124}
diff --git a/src/lib/libcrypto/s390xcap.c b/src/lib/libcrypto/s390xcap.c
index f2e94ef47e..77f6260785 100644
--- a/src/lib/libcrypto/s390xcap.c
+++ b/src/lib/libcrypto/s390xcap.c
@@ -7,31 +7,36 @@
7extern unsigned long OPENSSL_s390xcap_P[]; 7extern unsigned long OPENSSL_s390xcap_P[];
8 8
9static sigjmp_buf ill_jmp; 9static sigjmp_buf ill_jmp;
10static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); } 10static void ill_handler (int sig)
11{
12 siglongjmp(ill_jmp, sig);
13}
11 14
12unsigned long OPENSSL_s390x_facilities(void); 15unsigned long OPENSSL_s390x_facilities(void);
13 16
14void OPENSSL_cpuid_setup(void) 17void
15 { 18OPENSSL_cpuid_setup(void)
19{
16 sigset_t oset; 20 sigset_t oset;
17 struct sigaction ill_act,oact; 21 struct sigaction ill_act, oact;
18 22
19 if (OPENSSL_s390xcap_P[0]) return; 23 if (OPENSSL_s390xcap_P[0])
24 return;
20 25
21 OPENSSL_s390xcap_P[0] = 1UL<<(8*sizeof(unsigned long)-1); 26 OPENSSL_s390xcap_P[0] = 1UL << (8*sizeof(unsigned long) - 1);
22 27
23 memset(&ill_act,0,sizeof(ill_act)); 28 memset(&ill_act, 0, sizeof(ill_act));
24 ill_act.sa_handler = ill_handler; 29 ill_act.sa_handler = ill_handler;
25 sigfillset(&ill_act.sa_mask); 30 sigfillset(&ill_act.sa_mask);
26 sigdelset(&ill_act.sa_mask,SIGILL); 31 sigdelset(&ill_act.sa_mask, SIGILL);
27 sigdelset(&ill_act.sa_mask,SIGTRAP); 32 sigdelset(&ill_act.sa_mask, SIGTRAP);
28 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset); 33 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
29 sigaction (SIGILL,&ill_act,&oact); 34 sigaction (SIGILL, &ill_act, &oact);
30 35
31 /* protection against missing store-facility-list-extended */ 36 /* protection against missing store-facility-list-extended */
32 if (sigsetjmp(ill_jmp,1) == 0) 37 if (sigsetjmp(ill_jmp, 1) == 0)
33 OPENSSL_s390x_facilities(); 38 OPENSSL_s390x_facilities();
34 39
35 sigaction (SIGILL,&oact,NULL); 40 sigaction (SIGILL, &oact, NULL);
36 sigprocmask(SIG_SETMASK,&oset,NULL); 41 sigprocmask(SIG_SETMASK, &oset, NULL);
37 } 42}
diff --git a/src/lib/libcrypto/sparcv9cap.c b/src/lib/libcrypto/sparcv9cap.c
index 43b3ac6f81..08f7de3ec8 100644
--- a/src/lib/libcrypto/sparcv9cap.c
+++ b/src/lib/libcrypto/sparcv9cap.c
@@ -12,20 +12,22 @@
12#define SPARCV9_VIS2 (1<<3) /* reserved */ 12#define SPARCV9_VIS2 (1<<3) /* reserved */
13#define SPARCV9_FMADD (1<<4) /* reserved for SPARC64 V */ 13#define SPARCV9_FMADD (1<<4) /* reserved for SPARC64 V */
14 14
15static int OPENSSL_sparcv9cap_P=SPARCV9_TICK_PRIVILEGED; 15static int OPENSSL_sparcv9cap_P = SPARCV9_TICK_PRIVILEGED;
16 16
17int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num) 17int
18 { 18bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
19 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num); 19 const BN_ULONG *np, const BN_ULONG *n0, int num)
20 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num); 20{
21 21 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
22 if (num>=8 && !(num&1) && 22 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
23 (OPENSSL_sparcv9cap_P&(SPARCV9_PREFER_FPU|SPARCV9_VIS1)) == 23
24 (SPARCV9_PREFER_FPU|SPARCV9_VIS1)) 24 if (num >= 8 && !(num & 1) &&
25 return bn_mul_mont_fpu(rp,ap,bp,np,n0,num); 25 (OPENSSL_sparcv9cap_P & (SPARCV9_PREFER_FPU|SPARCV9_VIS1)) ==
26 (SPARCV9_PREFER_FPU|SPARCV9_VIS1))
27 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
26 else 28 else
27 return bn_mul_mont_int(rp,ap,bp,np,n0,num); 29 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
28 } 30}
29 31
30unsigned long _sparcv9_rdtick(void); 32unsigned long _sparcv9_rdtick(void);
31void _sparcv9_vis1_probe(void); 33void _sparcv9_vis1_probe(void);
@@ -33,8 +35,9 @@ unsigned long _sparcv9_vis1_instrument(void);
33void _sparcv9_vis2_probe(void); 35void _sparcv9_vis2_probe(void);
34void _sparcv9_fmadd_probe(void); 36void _sparcv9_fmadd_probe(void);
35 37
36unsigned long OPENSSL_rdtsc(void) 38unsigned long
37 { 39OPENSSL_rdtsc(void)
40{
38 if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED) 41 if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED)
39#if defined(__sun) && defined(__SVR4) 42#if defined(__sun) && defined(__SVR4)
40 return gethrtime(); 43 return gethrtime();
@@ -43,7 +46,7 @@ unsigned long OPENSSL_rdtsc(void)
43#endif 46#endif
44 else 47 else
45 return _sparcv9_rdtick(); 48 return _sparcv9_rdtick();
46 } 49}
47 50
48#if 0 && defined(__sun) && defined(__SVR4) 51#if 0 && defined(__sun) && defined(__SVR4)
49/* This code path is disabled, because of incompatibility of 52/* This code path is disabled, because of incompatibility of
@@ -54,21 +57,22 @@ unsigned long OPENSSL_rdtsc(void)
54#include <libdevinfo.h> 57#include <libdevinfo.h>
55#include <sys/systeminfo.h> 58#include <sys/systeminfo.h>
56 59
57typedef di_node_t (*di_init_t)(const char *,uint_t); 60typedef di_node_t (*di_init_t)(const char *, uint_t);
58typedef void (*di_fini_t)(di_node_t); 61typedef void (*di_fini_t)(di_node_t);
59typedef char * (*di_node_name_t)(di_node_t); 62typedef char * (*di_node_name_t)(di_node_t);
60typedef int (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t)); 63typedef int (*di_walk_node_t)(di_node_t, uint_t, di_node_name_t, int (*)(di_node_t, di_node_name_t));
61 64
62#define DLLINK(h,name) (name=(name##_t)dlsym((h),#name)) 65#define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
63 66
64static int walk_nodename(di_node_t node, di_node_name_t di_node_name) 67static int
65 { 68walk_nodename(di_node_t node, di_node_name_t di_node_name)
69{
66 char *name = (*di_node_name)(node); 70 char *name = (*di_node_name)(node);
67 71
68 /* This is expected to catch all UltraSPARC flavors prior T1 */ 72 /* This is expected to catch all UltraSPARC flavors prior T1 */
69 if (!strcmp (name,"SUNW,UltraSPARC") || 73 if (!strcmp (name, "SUNW, UltraSPARC") ||
70 !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */ 74 !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
71 { 75 {
72 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1; 76 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
73 77
74 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */ 78 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
@@ -76,51 +80,48 @@ static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
76 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 80 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
77 81
78 return DI_WALK_TERMINATE; 82 return DI_WALK_TERMINATE;
79 } 83 }
80 /* This is expected to catch remaining UltraSPARCs, such as T1 */ 84 /* This is expected to catch remaining UltraSPARCs, such as T1 */
81 else if (!strncmp(name,"SUNW,UltraSPARC",15)) 85 else if (!strncmp(name, "SUNW, UltraSPARC", 15)) {
82 {
83 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 86 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
84 87
85 return DI_WALK_TERMINATE; 88 return DI_WALK_TERMINATE;
86 } 89 }
87 90
88 return DI_WALK_CONTINUE; 91 return DI_WALK_CONTINUE;
89 } 92}
90 93
91void OPENSSL_cpuid_setup(void) 94void
92 { 95OPENSSL_cpuid_setup(void)
96{
93 void *h; 97 void *h;
94 char *e,si[256]; 98 char *e, si[256];
95 static int trigger=0; 99 static int trigger = 0;
96 100
97 if (trigger) return; 101 if (trigger)
98 trigger=1; 102 return;
103 trigger = 1;
99 104
100 if ((e=getenv("OPENSSL_sparcv9cap"))) 105 if ((e = getenv("OPENSSL_sparcv9cap"))) {
101 { 106 OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
102 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
103 return; 107 return;
104 } 108 }
105 109
106 if (sysinfo(SI_MACHINE,si,sizeof(si))>0) 110 if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
107 { 111 if (strcmp(si, "sun4v"))
108 if (strcmp(si,"sun4v"))
109 /* FPU is preferred for all CPUs, but US-T1/2 */ 112 /* FPU is preferred for all CPUs, but US-T1/2 */
110 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU; 113 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
111 } 114 }
112 115
113 if (sysinfo(SI_ISALIST,si,sizeof(si))>0) 116 if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
114 { 117 if (strstr(si, "+vis"))
115 if (strstr(si,"+vis"))
116 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1; 118 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
117 if (strstr(si,"+vis2")) 119 if (strstr(si, "+vis2")) {
118 {
119 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2; 120 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
120 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 121 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
121 return; 122 return;
122 }
123 } 123 }
124 }
124#ifdef M_KEEP 125#ifdef M_KEEP
125 /* 126 /*
126 * Solaris libdevinfo.so.1 is effectively incomatible with 127 * Solaris libdevinfo.so.1 is effectively incomatible with
@@ -129,109 +130,115 @@ void OPENSSL_cpuid_setup(void)
129 * free(3LIBMALLOC) called by di_fini. Prior call to 130 * free(3LIBMALLOC) called by di_fini. Prior call to
130 * mallopt(M_KEEP,0) somehow helps... But not always... 131 * mallopt(M_KEEP,0) somehow helps... But not always...
131 */ 132 */
132 if ((h = dlopen(NULL,RTLD_LAZY))) 133 if ((h = dlopen(NULL, RTLD_LAZY))) {
133 { 134 union { void *p;
134 union { void *p; int (*f)(int,int); } sym; 135 int (*f)(int, int);
135 if ((sym.p = dlsym(h,"mallopt"))) (*sym.f)(M_KEEP,0); 136 } sym;
136 dlclose(h); 137 if ((sym.p = dlsym(h, "mallopt"))) (*sym.f)(M_KEEP, 0);
137 } 138 dlclose(h);
139 }
138#endif 140#endif
139 if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do 141 if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
140 { 142 do {
141 di_init_t di_init; 143 di_init_t di_init;
142 di_fini_t di_fini; 144 di_fini_t di_fini;
143 di_walk_node_t di_walk_node; 145 di_walk_node_t di_walk_node;
144 di_node_name_t di_node_name; 146 di_node_name_t di_node_name;
145 di_node_t root_node; 147 di_node_t root_node;
146 148
147 if (!DLLINK(h,di_init)) break; 149 if (!DLLINK(h, di_init))
148 if (!DLLINK(h,di_fini)) break; 150 break;
149 if (!DLLINK(h,di_walk_node)) break; 151 if (!DLLINK(h, di_fini))
150 if (!DLLINK(h,di_node_name)) break; 152 break;
151 153 if (!DLLINK(h, di_walk_node))
152 if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL) 154 break;
153 { 155 if (!DLLINK(h, di_node_name))
154 (*di_walk_node)(root_node,DI_WALK_SIBFIRST, 156 break;
155 di_node_name,walk_nodename); 157
156 (*di_fini)(root_node); 158 if ((root_node = (*di_init)("/", DINFOSUBTREE)) !=
159 DI_NODE_NIL) {
160 (*di_walk_node)(root_node, DI_WALK_SIBFIRST,
161 di_node_name, walk_nodename);
162 (*di_fini)(root_node);
157 } 163 }
158 } while(0); 164 } while (0);
159 165
160 if (h) dlclose(h); 166 if (h)
161 } 167 dlclose(h);
168}
162 169
163#else 170#else
164 171
165static sigjmp_buf common_jmp; 172static sigjmp_buf common_jmp;
166static void common_handler(int sig) { siglongjmp(common_jmp,sig); } 173static void common_handler(int sig)
167 174{
168void OPENSSL_cpuid_setup(void) 175 siglongjmp(common_jmp, sig);
169 { 176}
177
178void
179OPENSSL_cpuid_setup(void)
180{
170 char *e; 181 char *e;
171 struct sigaction common_act,ill_oact,bus_oact; 182 struct sigaction common_act, ill_oact, bus_oact;
172 sigset_t all_masked,oset; 183 sigset_t all_masked, oset;
173 static int trigger=0; 184 static int trigger = 0;
174 185
175 if (trigger) return; 186 if (trigger)
176 trigger=1;
177
178 if ((e=getenv("OPENSSL_sparcv9cap")))
179 {
180 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
181 return; 187 return;
182 } 188 trigger = 1;
189
190 if ((e = getenv("OPENSSL_sparcv9cap"))) {
191 OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
192 return;
193 }
183 194
184 /* Initial value, fits UltraSPARC-I&II... */ 195 /* Initial value, fits UltraSPARC-I&II... */
185 OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU|SPARCV9_TICK_PRIVILEGED; 196 OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU|SPARCV9_TICK_PRIVILEGED;
186 197
187 sigfillset(&all_masked); 198 sigfillset(&all_masked);
188 sigdelset(&all_masked,SIGILL); 199 sigdelset(&all_masked, SIGILL);
189 sigdelset(&all_masked,SIGTRAP); 200 sigdelset(&all_masked, SIGTRAP);
190#ifdef SIGEMT 201#ifdef SIGEMT
191 sigdelset(&all_masked,SIGEMT); 202 sigdelset(&all_masked, SIGEMT);
192#endif 203#endif
193 sigdelset(&all_masked,SIGFPE); 204 sigdelset(&all_masked, SIGFPE);
194 sigdelset(&all_masked,SIGBUS); 205 sigdelset(&all_masked, SIGBUS);
195 sigdelset(&all_masked,SIGSEGV); 206 sigdelset(&all_masked, SIGSEGV);
196 sigprocmask(SIG_SETMASK,&all_masked,&oset); 207 sigprocmask(SIG_SETMASK, &all_masked, &oset);
197 208
198 memset(&common_act,0,sizeof(common_act)); 209 memset(&common_act, 0, sizeof(common_act));
199 common_act.sa_handler = common_handler; 210 common_act.sa_handler = common_handler;
200 common_act.sa_mask = all_masked; 211 common_act.sa_mask = all_masked;
201 212
202 sigaction(SIGILL,&common_act,&ill_oact); 213 sigaction(SIGILL, &common_act, &ill_oact);
203 sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda [on Linux] */ 214 sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda [on Linux] */
204 215
205 if (sigsetjmp(common_jmp,1) == 0) 216 if (sigsetjmp(common_jmp, 1) == 0) {
206 {
207 _sparcv9_rdtick(); 217 _sparcv9_rdtick();
208 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 218 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
209 } 219 }
210 220
211 if (sigsetjmp(common_jmp,1) == 0) 221 if (sigsetjmp(common_jmp, 1) == 0) {
212 {
213 _sparcv9_vis1_probe(); 222 _sparcv9_vis1_probe();
214 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1; 223 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
215 /* detect UltraSPARC-Tx, see sparccpud.S for details... */ 224 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
216 if (_sparcv9_vis1_instrument() >= 12) 225 if (_sparcv9_vis1_instrument() >= 12)
217 OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1|SPARCV9_PREFER_FPU); 226 OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1|SPARCV9_PREFER_FPU);
218 else 227 else {
219 {
220 _sparcv9_vis2_probe(); 228 _sparcv9_vis2_probe();
221 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2; 229 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
222 }
223 } 230 }
231 }
224 232
225 if (sigsetjmp(common_jmp,1) == 0) 233 if (sigsetjmp(common_jmp, 1) == 0) {
226 {
227 _sparcv9_fmadd_probe(); 234 _sparcv9_fmadd_probe();
228 OPENSSL_sparcv9cap_P |= SPARCV9_FMADD; 235 OPENSSL_sparcv9cap_P |= SPARCV9_FMADD;
229 } 236 }
230 237
231 sigaction(SIGBUS,&bus_oact,NULL); 238 sigaction(SIGBUS, &bus_oact, NULL);
232 sigaction(SIGILL,&ill_oact,NULL); 239 sigaction(SIGILL, &ill_oact, NULL);
233 240
234 sigprocmask(SIG_SETMASK,&oset,NULL); 241 sigprocmask(SIG_SETMASK, &oset, NULL);
235 } 242}
236 243
237#endif 244#endif
diff --git a/src/lib/libcrypto/uid.c b/src/lib/libcrypto/uid.c
index b1fd52bada..fcfa05ce0c 100644
--- a/src/lib/libcrypto/uid.c
+++ b/src/lib/libcrypto/uid.c
@@ -60,30 +60,32 @@
60 60
61#include OPENSSL_UNISTD 61#include OPENSSL_UNISTD
62 62
63int OPENSSL_issetugid(void) 63int
64 { 64OPENSSL_issetugid(void)
65{
65 return issetugid(); 66 return issetugid();
66 } 67}
67 68
68#elif defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) 69#elif defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
69 70
70int OPENSSL_issetugid(void) 71int
71 { 72OPENSSL_issetugid(void)
73{
72 return 0; 74 return 0;
73 } 75}
74 76
75#else 77#else
76 78
77#include OPENSSL_UNISTD 79#include OPENSSL_UNISTD
78#include <sys/types.h> 80#include <sys/types.h>
79 81
80int OPENSSL_issetugid(void) 82int
81 { 83OPENSSL_issetugid(void)
82 if (getuid() != geteuid()) return 1; 84{
83 if (getgid() != getegid()) return 1; 85 if (getuid()
86 != geteuid()) return 1;
87 if (getgid()
88 != getegid()) return 1;
84 return 0; 89 return 0;
85 } 90}
86#endif 91#endif
87
88
89
diff --git a/src/lib/libssl/src/crypto/arm_arch.h b/src/lib/libssl/src/crypto/arm_arch.h
index 5a83107680..0ccafb2215 100644
--- a/src/lib/libssl/src/crypto/arm_arch.h
+++ b/src/lib/libssl/src/crypto/arm_arch.h
@@ -43,7 +43,7 @@
43 43
44#if !__ASSEMBLER__ 44#if !__ASSEMBLER__
45extern unsigned int OPENSSL_armcap_P; 45extern unsigned int OPENSSL_armcap_P;
46 46
47#define ARMV7_NEON (1<<0) 47#define ARMV7_NEON (1<<0)
48#define ARMV7_TICK (1<<1) 48#define ARMV7_TICK (1<<1)
49#endif 49#endif
diff --git a/src/lib/libssl/src/crypto/armcap.c b/src/lib/libssl/src/crypto/armcap.c
index 9abaf396e5..0625587bdd 100644
--- a/src/lib/libssl/src/crypto/armcap.c
+++ b/src/lib/libssl/src/crypto/armcap.c
@@ -12,7 +12,8 @@ unsigned int OPENSSL_armcap_P;
12static sigset_t all_masked; 12static sigset_t all_masked;
13 13
14static sigjmp_buf ill_jmp; 14static sigjmp_buf ill_jmp;
15static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); } 15 static void ill_handler (int sig) { siglongjmp(ill_jmp, sig);
16}
16 17
17/* 18/*
18 * Following subroutines could have been inlined, but it's not all 19 * Following subroutines could have been inlined, but it's not all
@@ -21,60 +22,61 @@ static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); }
21void _armv7_neon_probe(void); 22void _armv7_neon_probe(void);
22unsigned int _armv7_tick(void); 23unsigned int _armv7_tick(void);
23 24
24unsigned int OPENSSL_rdtsc(void) 25unsigned int
25 { 26OPENSSL_rdtsc(void)
27{
26 if (OPENSSL_armcap_P & ARMV7_TICK) 28 if (OPENSSL_armcap_P & ARMV7_TICK)
27 return _armv7_tick(); 29 return _armv7_tick();
28 else 30 else
29 return 0; 31 return 0;
30 } 32}
31 33
32#if defined(__GNUC__) && __GNUC__>=2 34#if defined(__GNUC__) && __GNUC__>=2
33void OPENSSL_cpuid_setup(void) __attribute__((constructor)); 35void OPENSSL_cpuid_setup(void) __attribute__((constructor));
34#endif 36#endif
35void OPENSSL_cpuid_setup(void) 37
36 { 38void
39OPENSSL_cpuid_setup(void)
40{
37 char *e; 41 char *e;
38 struct sigaction ill_oact,ill_act; 42 struct sigaction ill_oact, ill_act;
39 sigset_t oset; 43 sigset_t oset;
40 static int trigger=0; 44 static int trigger = 0;
41 45
42 if (trigger) return; 46 if (trigger)
43 trigger=1;
44
45 if ((e=getenv("OPENSSL_armcap")))
46 {
47 OPENSSL_armcap_P=strtoul(e,NULL,0);
48 return; 47 return;
49 } 48 trigger = 1;
49
50 if ((e = getenv("OPENSSL_armcap"))) {
51 OPENSSL_armcap_P = strtoul(e, NULL, 0);
52 return;
53 }
50 54
51 sigfillset(&all_masked); 55 sigfillset(&all_masked);
52 sigdelset(&all_masked,SIGILL); 56 sigdelset(&all_masked, SIGILL);
53 sigdelset(&all_masked,SIGTRAP); 57 sigdelset(&all_masked, SIGTRAP);
54 sigdelset(&all_masked,SIGFPE); 58 sigdelset(&all_masked, SIGFPE);
55 sigdelset(&all_masked,SIGBUS); 59 sigdelset(&all_masked, SIGBUS);
56 sigdelset(&all_masked,SIGSEGV); 60 sigdelset(&all_masked, SIGSEGV);
57 61
58 OPENSSL_armcap_P = 0; 62 OPENSSL_armcap_P = 0;
59 63
60 memset(&ill_act,0,sizeof(ill_act)); 64 memset(&ill_act, 0, sizeof(ill_act));
61 ill_act.sa_handler = ill_handler; 65 ill_act.sa_handler = ill_handler;
62 ill_act.sa_mask = all_masked; 66 ill_act.sa_mask = all_masked;
63 67
64 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset); 68 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
65 sigaction(SIGILL,&ill_act,&ill_oact); 69 sigaction(SIGILL, &ill_act, &ill_oact);
66 70
67 if (sigsetjmp(ill_jmp,1) == 0) 71 if (sigsetjmp(ill_jmp, 1) == 0) {
68 {
69 _armv7_neon_probe(); 72 _armv7_neon_probe();
70 OPENSSL_armcap_P |= ARMV7_NEON; 73 OPENSSL_armcap_P |= ARMV7_NEON;
71 } 74 }
72 if (sigsetjmp(ill_jmp,1) == 0) 75 if (sigsetjmp(ill_jmp, 1) == 0) {
73 {
74 _armv7_tick(); 76 _armv7_tick();
75 OPENSSL_armcap_P |= ARMV7_TICK; 77 OPENSSL_armcap_P |= ARMV7_TICK;
76 }
77
78 sigaction (SIGILL,&ill_oact,NULL);
79 sigprocmask(SIG_SETMASK,&oset,NULL);
80 } 78 }
79
80 sigaction (SIGILL, &ill_oact, NULL);
81 sigprocmask(SIG_SETMASK, &oset, NULL);
82}
diff --git a/src/lib/libssl/src/crypto/buildinf.h b/src/lib/libssl/src/crypto/buildinf.h
index 957f09d5fd..8fdb24cb33 100644
--- a/src/lib/libssl/src/crypto/buildinf.h
+++ b/src/lib/libssl/src/crypto/buildinf.h
@@ -1,6 +1,6 @@
1#ifndef MK1MF_BUILD 1#ifndef MK1MF_BUILD
2 /* auto-generated by crypto/Makefile for crypto/cversion.c */ 2/* auto-generated by crypto/Makefile for crypto/cversion.c */
3 #define CFLAGS "cc -O" 3#define CFLAGS "cc -O"
4 #define PLATFORM "dist" 4#define PLATFORM "dist"
5 #define DATE "Sat Aug 21 10:52:09 EST 2010" 5#define DATE "Sat Aug 21 10:52:09 EST 2010"
6#endif 6#endif
diff --git a/src/lib/libssl/src/crypto/cpt_err.c b/src/lib/libssl/src/crypto/cpt_err.c
index 289005f662..a8e94ccd16 100644
--- a/src/lib/libssl/src/crypto/cpt_err.c
+++ b/src/lib/libssl/src/crypto/cpt_err.c
@@ -68,38 +68,35 @@
68#define ERR_FUNC(func) ERR_PACK(ERR_LIB_CRYPTO,func,0) 68#define ERR_FUNC(func) ERR_PACK(ERR_LIB_CRYPTO,func,0)
69#define ERR_REASON(reason) ERR_PACK(ERR_LIB_CRYPTO,0,reason) 69#define ERR_REASON(reason) ERR_PACK(ERR_LIB_CRYPTO,0,reason)
70 70
71static ERR_STRING_DATA CRYPTO_str_functs[]= 71static ERR_STRING_DATA CRYPTO_str_functs[]= {
72 { 72 {ERR_FUNC(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX), "CRYPTO_get_ex_new_index"},
73{ERR_FUNC(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX), "CRYPTO_get_ex_new_index"}, 73 {ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID), "CRYPTO_get_new_dynlockid"},
74{ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID), "CRYPTO_get_new_dynlockid"}, 74 {ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_LOCKID), "CRYPTO_get_new_lockid"},
75{ERR_FUNC(CRYPTO_F_CRYPTO_GET_NEW_LOCKID), "CRYPTO_get_new_lockid"}, 75 {ERR_FUNC(CRYPTO_F_CRYPTO_SET_EX_DATA), "CRYPTO_set_ex_data"},
76{ERR_FUNC(CRYPTO_F_CRYPTO_SET_EX_DATA), "CRYPTO_set_ex_data"}, 76 {ERR_FUNC(CRYPTO_F_DEF_ADD_INDEX), "DEF_ADD_INDEX"},
77{ERR_FUNC(CRYPTO_F_DEF_ADD_INDEX), "DEF_ADD_INDEX"}, 77 {ERR_FUNC(CRYPTO_F_DEF_GET_CLASS), "DEF_GET_CLASS"},
78{ERR_FUNC(CRYPTO_F_DEF_GET_CLASS), "DEF_GET_CLASS"}, 78 {ERR_FUNC(CRYPTO_F_FIPS_MODE_SET), "FIPS_mode_set"},
79{ERR_FUNC(CRYPTO_F_FIPS_MODE_SET), "FIPS_mode_set"}, 79 {ERR_FUNC(CRYPTO_F_INT_DUP_EX_DATA), "INT_DUP_EX_DATA"},
80{ERR_FUNC(CRYPTO_F_INT_DUP_EX_DATA), "INT_DUP_EX_DATA"}, 80 {ERR_FUNC(CRYPTO_F_INT_FREE_EX_DATA), "INT_FREE_EX_DATA"},
81{ERR_FUNC(CRYPTO_F_INT_FREE_EX_DATA), "INT_FREE_EX_DATA"}, 81 {ERR_FUNC(CRYPTO_F_INT_NEW_EX_DATA), "INT_NEW_EX_DATA"},
82{ERR_FUNC(CRYPTO_F_INT_NEW_EX_DATA), "INT_NEW_EX_DATA"}, 82 {0, NULL}
83{0,NULL} 83};
84 };
85 84
86static ERR_STRING_DATA CRYPTO_str_reasons[]= 85static ERR_STRING_DATA CRYPTO_str_reasons[]= {
87 { 86 {ERR_REASON(CRYPTO_R_FIPS_MODE_NOT_SUPPORTED), "fips mode not supported"},
88{ERR_REASON(CRYPTO_R_FIPS_MODE_NOT_SUPPORTED),"fips mode not supported"}, 87 {ERR_REASON(CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK), "no dynlock create callback"},
89{ERR_REASON(CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK),"no dynlock create callback"}, 88 {0, NULL}
90{0,NULL} 89};
91 };
92 90
93#endif 91#endif
94 92
95void ERR_load_CRYPTO_strings(void) 93void
96 { 94ERR_load_CRYPTO_strings(void)
95{
97#ifndef OPENSSL_NO_ERR 96#ifndef OPENSSL_NO_ERR
98 97 if (ERR_func_error_string(CRYPTO_str_functs[0].error) == NULL) {
99 if (ERR_func_error_string(CRYPTO_str_functs[0].error) == NULL) 98 ERR_load_strings(0, CRYPTO_str_functs);
100 { 99 ERR_load_strings(0, CRYPTO_str_reasons);
101 ERR_load_strings(0,CRYPTO_str_functs);
102 ERR_load_strings(0,CRYPTO_str_reasons);
103 }
104#endif
105 } 100 }
101#endif
102}
diff --git a/src/lib/libssl/src/crypto/cryptlib.c b/src/lib/libssl/src/crypto/cryptlib.c
index 082b2e88ee..89667d175a 100644
--- a/src/lib/libssl/src/crypto/cryptlib.c
+++ b/src/lib/libssl/src/crypto/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)
121static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */ 121static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
122#endif 122#endif
123 123
124DECLARE_STACK_OF(CRYPTO_dynlock) 124DECLARE_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 */
127static const char* const lock_names[CRYPTO_NUM_LOCKS] = 127static 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. */
177static STACK_OF(OPENSSL_STRING) *app_locks=NULL; 176static 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. */
182static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL; 181static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
183
184 182
185static void (*locking_callback)(int mode,int type, 183static void (*locking_callback)(int mode, int type,
186 const char *file,int line)=0; 184 const char *file, int line) = 0;
187static int (*add_lock_callback)(int *pointer,int amount, 185static 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
190static unsigned long (*id_callback)(void)=0; 188static unsigned long (*id_callback)(void) = 0;
191#endif 189#endif
192static void (*threadid_callback)(CRYPTO_THREADID *)=0; 190static void (*threadid_callback)(CRYPTO_THREADID *) = 0;
193static struct CRYPTO_dynlock_value *(*dynlock_create_callback) 191static struct CRYPTO_dynlock_value *(*dynlock_create_callback)(
194 (const char *file,int line)=0; 192 const char *file, int line) = 0;
195static void (*dynlock_lock_callback)(int mode, 193static 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;
197static void (*dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l, 195static 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
200int CRYPTO_get_new_lockid(char *name) 198int
201 { 199CRYPTO_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
231int CRYPTO_num_locks(void) 228int
232 { 229CRYPTO_num_locks(void)
230{
233 return CRYPTO_NUM_LOCKS; 231 return CRYPTO_NUM_LOCKS;
234 } 232}
235 233
236int CRYPTO_get_new_dynlockid(void) 234int
237 { 235CRYPTO_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
297void CRYPTO_destroy_dynlockid(int i) 290void
298 { 291CRYPTO_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
340struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i) 327struct 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
360struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void)) 348struct 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
366void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, 354void (*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
372void (*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
378void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func) 360void (*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
384void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode, 366void
385 struct CRYPTO_dynlock_value *l, const char *file, int line)) 367CRYPTO_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
390void CRYPTO_set_dynlock_destroy_callback(void (*func) 373void
391 (struct CRYPTO_dynlock_value *l, const char *file, int line)) 374CRYPTO_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
380void
381CRYPTO_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
397void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file, 387void (*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
403int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type, 393int (*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
409void CRYPTO_set_locking_callback(void (*func)(int mode,int type, 399void
410 const char *file,int line)) 400CRYPTO_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
419void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type, 410void
420 const char *file,int line)) 411CRYPTO_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. */
428void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) 420void
429 { 421CRYPTO_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
434static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 }; 427static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
435void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) 428void
436 { 429CRYPTO_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
470int CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *)) 462int
471 { 463CRYPTO_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
478void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *) 471void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *)
479 { 472{
480 return threadid_callback; 473 return threadid_callback;
481 } 474}
482 475
483void CRYPTO_THREADID_current(CRYPTO_THREADID *id) 476void
484 { 477CRYPTO_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
511int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b) 503int
512 { 504CRYPTO_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
516void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src) 509void
517 { 510CRYPTO_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
521unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id) 515unsigned long
522 { 516CRYPTO_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
527unsigned long (*CRYPTO_get_id_callback(void))(void) 522unsigned long (*CRYPTO_get_id_callback(void))(void)
528 { 523{
529 return(id_callback); 524 return (id_callback);
530 } 525}
531 526
532void CRYPTO_set_id_callback(unsigned long (*func)(void)) 527void
533 { 528CRYPTO_set_id_callback(unsigned long (*func)(void))
534 id_callback=func; 529{
535 } 530 id_callback = func;
531}
536 532
537unsigned long CRYPTO_thread_id(void) 533unsigned long
538 { 534CRYPTO_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
561void CRYPTO_lock(int mode, int type, const char *file, int line) 556void
562 { 557CRYPTO_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
607int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file, 599int
608 int line) 600CRYPTO_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
652const char *CRYPTO_get_lock_name(int type) 642const 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
668unsigned int OPENSSL_ia32cap_P[2]; 659unsigned int OPENSSL_ia32cap_P[2];
669unsigned long *OPENSSL_ia32cap_loc(void) 660unsigned 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
685typedef unsigned long long IA32CAP; 678typedef unsigned long long IA32CAP;
686#endif 679#endif
687void OPENSSL_cpuid_setup(void) 680void
688{ static int trigger=0; 681OPENSSL_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
719unsigned long *OPENSSL_ia32cap_loc(void) { return NULL; } 715 unsigned long *OPENSSL_ia32cap_loc(void) { return NULL;
716}
720#endif 717#endif
721int OPENSSL_NONPIC_relocated = 0; 718int OPENSSL_NONPIC_relocated = 0;
722#if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) 719#if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
723void OPENSSL_cpuid_setup(void) {} 720void
721OPENSSL_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
737BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, 735BOOL WINAPI
738 LPVOID lpvReserved) 736DllMain(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
786int OPENSSL_isservice(void) 782int
787{ HWINSTA h; 783OPENSSL_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
832int OPENSSL_isservice(void) { return 0; } 836 int OPENSSL_isservice(void) { return 0;
837}
833#endif 838#endif
834 839
835void OPENSSL_showfatal (const char *fmta,...) 840void 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
903void OPENSSL_showfatal (const char *fmta,...) 921void 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
930int OPENSSL_isservice(void)
931{
932 return 0;
909} 933}
910int OPENSSL_isservice (void) { return 0; }
911#endif 934#endif
912 935
913void OpenSSLDie(const char *file,int line,const char *assertion) 936void
914 { 937OpenSSLDie(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
927void *OPENSSL_stderr(void) { return stderr; } 951void *OPENSSL_stderr(void)
952{
953 return stderr;
954}
928 955
929int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) 956int
930 { 957CRYPTO_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}
diff --git a/src/lib/libssl/src/crypto/crypto.h b/src/lib/libssl/src/crypto/crypto.h
index 76bc5958c0..baeba25b99 100644
--- a/src/lib/libssl/src/crypto/crypto.h
+++ b/src/lib/libssl/src/crypto/crypto.h
@@ -154,23 +154,22 @@ extern "C" {
154typedef struct crypto_ex_data_st CRYPTO_EX_DATA; 154typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
155/* Called when a new object is created */ 155/* Called when a new object is created */
156typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 156typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
157 int idx, long argl, void *argp); 157 int idx, long argl, void *argp);
158/* Called when an object is free()ed */ 158/* Called when an object is free()ed */
159typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 159typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
160 int idx, long argl, void *argp); 160 int idx, long argl, void *argp);
161/* Called when we need to dup an object */ 161/* Called when we need to dup an object */
162typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, void *from_d, 162typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
163 int idx, long argl, void *argp); 163 void *from_d, int idx, long argl, void *argp);
164#endif 164#endif
165 165
166/* A generic structure to pass assorted data in a expandable way */ 166/* A generic structure to pass assorted data in a expandable way */
167typedef struct openssl_item_st 167typedef struct openssl_item_st {
168 {
169 int code; 168 int code;
170 void *value; /* Not used for flag attributes */ 169 void *value; /* Not used for flag attributes */
171 size_t value_size; /* Max size of value for output, length for input */ 170 size_t value_size; /* Max size of value for output, length for input */
172 size_t *value_length; /* Returned length of value for output */ 171 size_t *value_length; /* Returned length of value for output */
173 } OPENSSL_ITEM; 172} OPENSSL_ITEM;
174 173
175 174
176/* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock 175/* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock
@@ -249,11 +248,10 @@ typedef struct openssl_item_st
249 and deallocate locks in a dynamic fashion. The following typedef 248 and deallocate locks in a dynamic fashion. The following typedef
250 makes this possible in a type-safe manner. */ 249 makes this possible in a type-safe manner. */
251/* struct CRYPTO_dynlock_value has to be defined by the application. */ 250/* struct CRYPTO_dynlock_value has to be defined by the application. */
252typedef struct 251typedef struct {
253 {
254 int references; 252 int references;
255 struct CRYPTO_dynlock_value *data; 253 struct CRYPTO_dynlock_value *data;
256 } CRYPTO_dynlock; 254} CRYPTO_dynlock;
257 255
258 256
259/* The following can be used to detect memory leaks in the SSLeay library. 257/* The following can be used to detect memory leaks in the SSLeay library.
@@ -278,24 +276,22 @@ typedef struct
278/* predec of the BIO type */ 276/* predec of the BIO type */
279typedef struct bio_st BIO_dummy; 277typedef struct bio_st BIO_dummy;
280 278
281struct crypto_ex_data_st 279struct crypto_ex_data_st {
282 {
283 STACK_OF(void) *sk; 280 STACK_OF(void) *sk;
284 int dummy; /* gcc is screwing up this data structure :-( */ 281 int dummy; /* gcc is screwing up this data structure :-( */
285 }; 282};
286DECLARE_STACK_OF(void) 283DECLARE_STACK_OF(void)
287 284
288/* This stuff is basically class callback functions 285/* This stuff is basically class callback functions
289 * The current classes are SSL_CTX, SSL, SSL_SESSION, and a few more */ 286 * The current classes are SSL_CTX, SSL, SSL_SESSION, and a few more */
290 287
291typedef struct crypto_ex_data_func_st 288typedef struct crypto_ex_data_func_st {
292 {
293 long argl; /* Arbitary long */ 289 long argl; /* Arbitary long */
294 void *argp; /* Arbitary void * */ 290 void *argp; /* Arbitary void * */
295 CRYPTO_EX_new *new_func; 291 CRYPTO_EX_new *new_func;
296 CRYPTO_EX_free *free_func; 292 CRYPTO_EX_free *free_func;
297 CRYPTO_EX_dup *dup_func; 293 CRYPTO_EX_dup *dup_func;
298 } CRYPTO_EX_DATA_FUNCS; 294} CRYPTO_EX_DATA_FUNCS;
299 295
300DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS) 296DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
301 297
@@ -392,18 +388,18 @@ int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i);
392int CRYPTO_ex_data_new_class(void); 388int CRYPTO_ex_data_new_class(void);
393/* Within a given class, get/register a new index */ 389/* Within a given class, get/register a new index */
394int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, 390int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
395 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 391 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
396 CRYPTO_EX_free *free_func); 392 CRYPTO_EX_free *free_func);
397/* Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a given 393/* Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a given
398 * class (invokes whatever per-class callbacks are applicable) */ 394 * class (invokes whatever per-class callbacks are applicable) */
399int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad); 395int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
400int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 396int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
401 CRYPTO_EX_DATA *from); 397 CRYPTO_EX_DATA *from);
402void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad); 398void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
403/* Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular index 399/* Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular index
404 * (relative to the class type involved) */ 400 * (relative to the class type involved) */
405int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val); 401int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val);
406void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad,int idx); 402void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx);
407/* This function cleans up all "ex_data" state. It mustn't be called under 403/* This function cleans up all "ex_data" state. It mustn't be called under
408 * potential race-conditions. */ 404 * potential race-conditions. */
409void CRYPTO_cleanup_all_ex_data(void); 405void CRYPTO_cleanup_all_ex_data(void);
@@ -411,22 +407,21 @@ void CRYPTO_cleanup_all_ex_data(void);
411int CRYPTO_get_new_lockid(char *name); 407int CRYPTO_get_new_lockid(char *name);
412 408
413int CRYPTO_num_locks(void); /* return CRYPTO_NUM_LOCKS (shared libs!) */ 409int CRYPTO_num_locks(void); /* return CRYPTO_NUM_LOCKS (shared libs!) */
414void CRYPTO_lock(int mode, int type,const char *file,int line); 410void CRYPTO_lock(int mode, int type, const char *file, int line);
415void CRYPTO_set_locking_callback(void (*func)(int mode,int type, 411void CRYPTO_set_locking_callback(void (*func)(int mode, int type,
416 const char *file,int line)); 412 const char *file, int line));
417void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file, 413void (*CRYPTO_get_locking_callback(void))(int mode, int type,
418 int line); 414 const char *file, int line);
419void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type, 415void CRYPTO_set_add_lock_callback(int (*func)(int *num, int mount, int type,
420 const char *file, int line)); 416 const char *file, int line));
421int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type, 417int (*CRYPTO_get_add_lock_callback(void))(int *num, int mount, int type,
422 const char *file,int line); 418 const char *file, int line);
423 419
424/* Don't use this structure directly. */ 420/* Don't use this structure directly. */
425typedef struct crypto_threadid_st 421typedef struct crypto_threadid_st {
426 {
427 void *ptr; 422 void *ptr;
428 unsigned long val; 423 unsigned long val;
429 } CRYPTO_THREADID; 424} CRYPTO_THREADID;
430/* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */ 425/* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */
431void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val); 426void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val);
432void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr); 427void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
@@ -443,8 +438,8 @@ unsigned long CRYPTO_thread_id(void);
443#endif 438#endif
444 439
445const char *CRYPTO_get_lock_name(int type); 440const char *CRYPTO_get_lock_name(int type);
446int CRYPTO_add_lock(int *pointer,int amount,int type, const char *file, 441int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
447 int line); 442 int line);
448 443
449int CRYPTO_get_new_dynlockid(void); 444int CRYPTO_get_new_dynlockid(void);
450void CRYPTO_destroy_dynlockid(int i); 445void CRYPTO_destroy_dynlockid(int i);
@@ -452,46 +447,43 @@ struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i);
452void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file, int line)); 447void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file, int line));
453void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)); 448void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line));
454void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l, const char *file, int line)); 449void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l, const char *file, int line));
455struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))(const char *file,int line); 450struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))(const char *file, int line);
456void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, struct CRYPTO_dynlock_value *l, const char *file,int line); 451void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line);
457void (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l, const char *file,int line); 452void (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l, const char *file, int line);
458 453
459/* CRYPTO_set_mem_functions includes CRYPTO_set_locked_mem_functions -- 454/* CRYPTO_set_mem_functions includes CRYPTO_set_locked_mem_functions --
460 * call the latter last if you need different functions */ 455 * call the latter last if you need different functions */
461int CRYPTO_set_mem_functions(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *)); 456int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), void (*f)(void *));
462int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*free_func)(void *)); 457int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*free_func)(void *));
463int CRYPTO_set_mem_ex_functions(void *(*m)(size_t,const char *,int), 458int CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int),
464 void *(*r)(void *,size_t,const char *,int), 459 void *(*r)(void *, size_t, const char *, int), void (*f)(void *));
465 void (*f)(void *)); 460int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int),
466int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t,const char *,int), 461 void (*free_func)(void *));
467 void (*free_func)(void *)); 462int CRYPTO_set_mem_debug_functions(
468int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), 463 void (*m)(void *, int, const char *, int, int),
469 void (*r)(void *,void *,int,const char *,int,int), 464 void (*r)(void *, void *, int, const char *, int, int),
470 void (*f)(void *,int), 465 void (*f)(void *, int), void (*so)(long), long (*go)(void));
471 void (*so)(long), 466void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
472 long (*go)(void)); 467 void (**f)(void *));
473void CRYPTO_get_mem_functions(void *(**m)(size_t),void *(**r)(void *, size_t), void (**f)(void *));
474void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)); 468void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *));
475void CRYPTO_get_mem_ex_functions(void *(**m)(size_t,const char *,int), 469void CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int),
476 void *(**r)(void *, size_t,const char *,int), 470 void *(**r)(void *, size_t, const char *, int), void (**f)(void *));
477 void (**f)(void *)); 471void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int),
478void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t,const char *,int), 472 void (**f)(void *));
479 void (**f)(void *)); 473void CRYPTO_get_mem_debug_functions(
480void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), 474 void (**m)(void *, int, const char *, int, int),
481 void (**r)(void *,void *,int,const char *,int,int), 475 void (**r)(void *, void *, int, const char *, int, int),
482 void (**f)(void *,int), 476 void (**f)(void *, int), void (**so)(long), long (**go)(void));
483 void (**so)(long),
484 long (**go)(void));
485 477
486void *CRYPTO_malloc_locked(int num, const char *file, int line); 478void *CRYPTO_malloc_locked(int num, const char *file, int line);
487void CRYPTO_free_locked(void *ptr); 479void CRYPTO_free_locked(void *ptr);
488void *CRYPTO_malloc(int num, const char *file, int line); 480void *CRYPTO_malloc(int num, const char *file, int line);
489char *CRYPTO_strdup(const char *str, const char *file, int line); 481char *CRYPTO_strdup(const char *str, const char *file, int line);
490void CRYPTO_free(void *ptr); 482void CRYPTO_free(void *ptr);
491void *CRYPTO_realloc(void *addr,int num, const char *file, int line); 483void *CRYPTO_realloc(void *addr, int num, const char *file, int line);
492void *CRYPTO_realloc_clean(void *addr,int old_num,int num,const char *file, 484void *CRYPTO_realloc_clean(void *addr, int old_num, int num,
493 int line); 485 const char *file, int line);
494void *CRYPTO_remalloc(void *addr,int num, const char *file, int line); 486void *CRYPTO_remalloc(void *addr, int num, const char *file, int line);
495 487
496void OPENSSL_cleanse(void *ptr, size_t len); 488void OPENSSL_cleanse(void *ptr, size_t len);
497 489
@@ -512,9 +504,9 @@ int CRYPTO_remove_all_info(void);
512 * 0: called before the actual memory allocation has taken place 504 * 0: called before the actual memory allocation has taken place
513 * 1: called after the actual memory allocation has taken place 505 * 1: called after the actual memory allocation has taken place
514 */ 506 */
515void CRYPTO_dbg_malloc(void *addr,int num,const char *file,int line,int before_p); 507void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, int before_p);
516void CRYPTO_dbg_realloc(void *addr1,void *addr2,int num,const char *file,int line,int before_p); 508void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, const char *file, int line, int before_p);
517void CRYPTO_dbg_free(void *addr,int before_p); 509void CRYPTO_dbg_free(void *addr, int before_p);
518/* Tell the debugging code about options. By default, the following values 510/* Tell the debugging code about options. By default, the following values
519 * apply: 511 * apply:
520 * 512 *
@@ -536,7 +528,7 @@ typedef void *CRYPTO_MEM_LEAK_CB(unsigned long, const char *, int, int, void *);
536void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb); 528void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb);
537 529
538/* die if we have to */ 530/* die if we have to */
539void OpenSSLDie(const char *file,int line,const char *assertion); 531void OpenSSLDie(const char *file, int line, const char *assertion);
540#define OPENSSL_assert(e) (void)((e) ? 0 : (OpenSSLDie(__FILE__, __LINE__, #e),1)) 532#define OPENSSL_assert(e) (void)((e) ? 0 : (OpenSSLDie(__FILE__, __LINE__, #e),1))
541 533
542unsigned long *OPENSSL_ia32cap_loc(void); 534unsigned long *OPENSSL_ia32cap_loc(void);
diff --git a/src/lib/libssl/src/crypto/cversion.c b/src/lib/libssl/src/crypto/cversion.c
index ea9f25fd16..1fe92a4bfa 100644
--- a/src/lib/libssl/src/crypto/cversion.c
+++ b/src/lib/libssl/src/crypto/cversion.c
@@ -62,56 +62,53 @@
62#include "buildinf.h" 62#include "buildinf.h"
63#endif 63#endif
64 64
65const char *SSLeay_version(int t) 65const char
66 { 66*SSLeay_version(int t)
67{
67 if (t == SSLEAY_VERSION) 68 if (t == SSLEAY_VERSION)
68 return OPENSSL_VERSION_TEXT; 69 return OPENSSL_VERSION_TEXT;
69 if (t == SSLEAY_BUILT_ON) 70 if (t == SSLEAY_BUILT_ON) {
70 {
71#ifdef DATE 71#ifdef DATE
72 static char buf[sizeof(DATE)+11]; 72 static char buf[sizeof(DATE) + 11];
73 73
74 BIO_snprintf(buf,sizeof buf,"built on: %s",DATE); 74 BIO_snprintf(buf, sizeof buf, "built on: %s", DATE);
75 return(buf); 75 return (buf);
76#else 76#else
77 return("built on: date not available"); 77 return("built on: date not available");
78#endif 78#endif
79 } 79 }
80 if (t == SSLEAY_CFLAGS) 80 if (t == SSLEAY_CFLAGS) {
81 {
82#ifdef CFLAGS 81#ifdef CFLAGS
83 static char buf[sizeof(CFLAGS)+11]; 82 static char buf[sizeof(CFLAGS) + 11];
84 83
85 BIO_snprintf(buf,sizeof buf,"compiler: %s",CFLAGS); 84 BIO_snprintf(buf, sizeof buf, "compiler: %s", CFLAGS);
86 return(buf); 85 return (buf);
87#else 86#else
88 return("compiler: information not available"); 87 return("compiler: information not available");
89#endif 88#endif
90 } 89 }
91 if (t == SSLEAY_PLATFORM) 90 if (t == SSLEAY_PLATFORM) {
92 {
93#ifdef PLATFORM 91#ifdef PLATFORM
94 static char buf[sizeof(PLATFORM)+11]; 92 static char buf[sizeof(PLATFORM) + 11];
95 93
96 BIO_snprintf(buf,sizeof buf,"platform: %s", PLATFORM); 94 BIO_snprintf(buf, sizeof buf, "platform: %s", PLATFORM);
97 return(buf); 95 return (buf);
98#else 96#else
99 return("platform: information not available"); 97 return("platform: information not available");
100#endif 98#endif
101 } 99 }
102 if (t == SSLEAY_DIR) 100 if (t == SSLEAY_DIR) {
103 {
104#ifdef OPENSSLDIR 101#ifdef OPENSSLDIR
105 return "OPENSSLDIR: \"" OPENSSLDIR "\""; 102 return "OPENSSLDIR: \"" OPENSSLDIR "\"";
106#else 103#else
107 return "OPENSSLDIR: N/A"; 104 return "OPENSSLDIR: N/A";
108#endif 105#endif
109 }
110 return("not available");
111 }
112
113unsigned long SSLeay(void)
114 {
115 return(SSLEAY_VERSION_NUMBER);
116 } 106 }
107 return("not available");
108}
117 109
110unsigned long
111SSLeay(void)
112{
113 return (SSLEAY_VERSION_NUMBER);
114}
diff --git a/src/lib/libssl/src/crypto/ebcdic.c b/src/lib/libssl/src/crypto/ebcdic.c
index 43e53bcaf7..2ac26abc72 100644
--- a/src/lib/libssl/src/crypto/ebcdic.c
+++ b/src/lib/libssl/src/crypto/ebcdic.c
@@ -4,7 +4,7 @@
4 4
5#include <openssl/e_os2.h> 5#include <openssl/e_os2.h>
6#if defined(PEDANTIC) || defined(__DECC) || defined(OPENSSL_SYS_MACOSX) 6#if defined(PEDANTIC) || defined(__DECC) || defined(OPENSSL_SYS_MACOSX)
7static void *dummy=&dummy; 7static void *dummy = &dummy;
8#endif 8#endif
9 9
10#else /*CHARSET_EBCDIC*/ 10#else /*CHARSET_EBCDIC*/
@@ -30,38 +30,38 @@ static void *dummy=&dummy;
30 30
31/* Here's the bijective ebcdic-to-ascii table: */ 31/* Here's the bijective ebcdic-to-ascii table: */
32const unsigned char os_toascii[256] = { 32const unsigned char os_toascii[256] = {
33/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, 33/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
34 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/ 34 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/
35/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, 35/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
36 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/ 36 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/
37/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, 37/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
38 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/ 38 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/
39/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 39/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
40 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/ 40 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/
41/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, 41/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
42 0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/ 42 0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/
43/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, 43/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
44 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/ 44 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/
45/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, 45/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
46 0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/ 46 0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/
47/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, 47/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
48 0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/ 48 0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/
49/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 49/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
50 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/ 50 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/
51/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 51/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
52 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/ 52 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/
53/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 53/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
54 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/ 54 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/
55/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, 55/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
56 0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/ 56 0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/
57/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 57/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
58 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/ 58 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/
59/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 59/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
60 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/ 60 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/
61/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 61/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
62 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/ 62 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/
63/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 63/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
64 0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e /*0123456789.{.}.~*/ 64 0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e /*0123456789.{.}.~*/
65}; 65};
66 66
67 67
@@ -111,38 +111,38 @@ Bijective EBCDIC (character set IBM-1047) to US-ASCII table:
111This table is bijective - there are no ambigous or duplicate characters. 111This table is bijective - there are no ambigous or duplicate characters.
112*/ 112*/
113const unsigned char os_toascii[256] = { 113const unsigned char os_toascii[256] = {
114 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, /* 00-0f: */ 114 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, /* 00-0f: */
115 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */ 115 0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
116 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, /* 10-1f: */ 116 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, /* 10-1f: */
117 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */ 117 0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
118 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, /* 20-2f: */ 118 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, /* 20-2f: */
119 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /* ................ */ 119 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /* ................ */
120 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, /* 30-3f: */ 120 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, /* 30-3f: */
121 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /* ................ */ 121 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /* ................ */
122 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, /* 40-4f: */ 122 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, /* 40-4f: */
123 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* ...........<(+| */ 123 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* ...........<(+| */
124 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, /* 50-5f: */ 124 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, /* 50-5f: */
125 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, /* &.........!$*);^ */ 125 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, /* &.........!$*);^ */
126 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, /* 60-6f: */ 126 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, /* 60-6f: */
127 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /* -/.........,%_>? */ 127 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /* -/.........,%_>? */
128 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, /* 70-7f: */ 128 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, /* 70-7f: */
129 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /* .........`:#@'=" */ 129 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /* .........`:#@'=" */
130 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 80-8f: */ 130 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 80-8f: */
131 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /* .abcdefghi...... */ 131 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /* .abcdefghi...... */
132 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, /* 90-9f: */ 132 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, /* 90-9f: */
133 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /* .jklmnopqr...... */ 133 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /* .jklmnopqr...... */
134 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, /* a0-af: */ 134 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, /* a0-af: */
135 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, /* .~stuvwxyz...[.. */ 135 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, /* .~stuvwxyz...[.. */
136 0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, /* b0-bf: */ 136 0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, /* b0-bf: */
137 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, /* .............].. */ 137 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, /* .............].. */
138 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* c0-cf: */ 138 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* c0-cf: */
139 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /* {ABCDEFGHI...... */ 139 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /* {ABCDEFGHI...... */
140 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* d0-df: */ 140 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* d0-df: */
141 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, /* }JKLMNOPQR...... */ 141 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, /* }JKLMNOPQR...... */
142 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, /* e0-ef: */ 142 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, /* e0-ef: */
143 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /* \.STUVWXYZ...... */ 143 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /* \.STUVWXYZ...... */
144 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* f0-ff: */ 144 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* f0-ff: */
145 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f /* 0123456789...... */ 145 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f /* 0123456789...... */
146}; 146};
147 147
148 148
@@ -151,38 +151,38 @@ The US-ASCII to EBCDIC (character set IBM-1047) table:
151This table is bijective (no ambiguous or duplicate characters) 151This table is bijective (no ambiguous or duplicate characters)
152*/ 152*/
153const unsigned char os_toebcdic[256] = { 153const unsigned char os_toebcdic[256] = {
154 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, /* 00-0f: */ 154 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, /* 00-0f: */
155 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */ 155 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
156 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, /* 10-1f: */ 156 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, /* 10-1f: */
157 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */ 157 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
158 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, /* 20-2f: */ 158 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, /* 20-2f: */
159 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, /* !"#$%&'()*+,-./ */ 159 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, /* !"#$%&'()*+,-./ */
160 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 30-3f: */ 160 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 30-3f: */
161 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, /* 0123456789:;<=>? */ 161 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, /* 0123456789:;<=>? */
162 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 40-4f: */ 162 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 40-4f: */
163 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, /* @ABCDEFGHIJKLMNO */ 163 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, /* @ABCDEFGHIJKLMNO */
164 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, /* 50-5f: */ 164 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, /* 50-5f: */
165 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, /* PQRSTUVWXYZ[\]^_ */ 165 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, /* PQRSTUVWXYZ[\]^_ */
166 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 60-6f: */ 166 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 60-6f: */
167 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* `abcdefghijklmno */ 167 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* `abcdefghijklmno */
168 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, /* 70-7f: */ 168 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, /* 70-7f: */
169 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, /* pqrstuvwxyz{|}~. */ 169 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, /* pqrstuvwxyz{|}~. */
170 0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08, /* 80-8f: */ 170 0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08, /* 80-8f: */
171 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14, /* ................ */ 171 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14, /* ................ */
172 0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17, /* 90-9f: */ 172 0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17, /* 90-9f: */
173 0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0xff, /* ................ */ 173 0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0xff, /* ................ */
174 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, /* a0-af: */ 174 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, /* a0-af: */
175 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, /* ................ */ 175 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, /* ................ */
176 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, /* b0-bf: */ 176 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, /* b0-bf: */
177 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, /* ................ */ 177 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, /* ................ */
178 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, /* c0-cf: */ 178 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, /* c0-cf: */
179 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, /* ................ */ 179 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, /* ................ */
180 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, /* d0-df: */ 180 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, /* d0-df: */
181 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, /* ................ */ 181 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, /* ................ */
182 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, /* e0-ef: */ 182 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, /* e0-ef: */
183 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* ................ */ 183 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* ................ */
184 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, /* f0-ff: */ 184 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, /* f0-ff: */
185 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf /* ................ */ 185 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf /* ................ */
186}; 186};
187#endif /*_OSD_POSIX*/ 187#endif /*_OSD_POSIX*/
188 188
@@ -195,27 +195,27 @@ const unsigned char os_toebcdic[256] = {
195void * 195void *
196ebcdic2ascii(void *dest, const void *srce, size_t count) 196ebcdic2ascii(void *dest, const void *srce, size_t count)
197{ 197{
198 unsigned char *udest = dest; 198 unsigned char *udest = dest;
199 const unsigned char *usrce = srce; 199 const unsigned char *usrce = srce;
200 200
201 while (count-- != 0) { 201 while (count-- != 0) {
202 *udest++ = os_toascii[*usrce++]; 202 *udest++ = os_toascii[*usrce++];
203 } 203 }
204 204
205 return dest; 205 return dest;
206} 206}
207 207
208void * 208void *
209ascii2ebcdic(void *dest, const void *srce, size_t count) 209ascii2ebcdic(void *dest, const void *srce, size_t count)
210{ 210{
211 unsigned char *udest = dest; 211 unsigned char *udest = dest;
212 const unsigned char *usrce = srce; 212 const unsigned char *usrce = srce;
213 213
214 while (count-- != 0) { 214 while (count-- != 0) {
215 *udest++ = os_toebcdic[*usrce++]; 215 *udest++ = os_toebcdic[*usrce++];
216 } 216 }
217 217
218 return dest; 218 return dest;
219} 219}
220 220
221#endif 221#endif
diff --git a/src/lib/libssl/src/crypto/ex_data.c b/src/lib/libssl/src/crypto/ex_data.c
index e2bc8298d0..44bad59527 100644
--- a/src/lib/libssl/src/crypto/ex_data.c
+++ b/src/lib/libssl/src/crypto/ex_data.c
@@ -142,8 +142,7 @@
142#include <openssl/lhash.h> 142#include <openssl/lhash.h>
143 143
144/* What an "implementation of ex_data functionality" looks like */ 144/* What an "implementation of ex_data functionality" looks like */
145struct st_CRYPTO_EX_DATA_IMPL 145struct st_CRYPTO_EX_DATA_IMPL {
146 {
147 /*********************/ 146 /*********************/
148 /* GLOBAL OPERATIONS */ 147 /* GLOBAL OPERATIONS */
149 /* Return a new class index */ 148 /* Return a new class index */
@@ -154,79 +153,83 @@ struct st_CRYPTO_EX_DATA_IMPL
154 /* PER-CLASS OPERATIONS */ 153 /* PER-CLASS OPERATIONS */
155 /* Get a new method index within a class */ 154 /* Get a new method index within a class */
156 int (*cb_get_new_index)(int class_index, long argl, void *argp, 155 int (*cb_get_new_index)(int class_index, long argl, void *argp,
157 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 156 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
158 CRYPTO_EX_free *free_func); 157 CRYPTO_EX_free *free_func);
159 /* Initialise a new CRYPTO_EX_DATA of a given class */ 158 /* Initialise a new CRYPTO_EX_DATA of a given class */
160 int (*cb_new_ex_data)(int class_index, void *obj, 159 int (*cb_new_ex_data)(int class_index, void *obj,
161 CRYPTO_EX_DATA *ad); 160 CRYPTO_EX_DATA *ad);
162 /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */ 161 /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
163 int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to, 162 int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to,
164 CRYPTO_EX_DATA *from); 163 CRYPTO_EX_DATA *from);
165 /* Cleanup a CRYPTO_EX_DATA of a given class */ 164 /* Cleanup a CRYPTO_EX_DATA of a given class */
166 void (*cb_free_ex_data)(int class_index, void *obj, 165 void (*cb_free_ex_data)(int class_index, void *obj,
167 CRYPTO_EX_DATA *ad); 166 CRYPTO_EX_DATA *ad);
168 }; 167};
169 168
170/* The implementation we use at run-time */ 169/* The implementation we use at run-time */
171static const CRYPTO_EX_DATA_IMPL *impl = NULL; 170static const CRYPTO_EX_DATA_IMPL *impl = NULL;
172 171
173/* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg. 172/* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg.
174 * EX_IMPL(get_new_index)(...); */ 173 * EX_IMPL(get_new_index)(...);
174*/
175#define EX_IMPL(a) impl->cb_##a 175#define EX_IMPL(a) impl->cb_##a
176 176
177/* Predeclare the "default" ex_data implementation */ 177/* Predeclare the "default" ex_data implementation */
178static int int_new_class(void); 178static int int_new_class(void);
179static void int_cleanup(void); 179static void int_cleanup(void);
180static int int_get_new_index(int class_index, long argl, void *argp, 180static int int_get_new_index(int class_index, long argl, void *argp,
181 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 181 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
182 CRYPTO_EX_free *free_func); 182 CRYPTO_EX_free *free_func);
183static int int_new_ex_data(int class_index, void *obj, 183static int int_new_ex_data(int class_index, void *obj,
184 CRYPTO_EX_DATA *ad); 184 CRYPTO_EX_DATA *ad);
185static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 185static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
186 CRYPTO_EX_DATA *from); 186 CRYPTO_EX_DATA *from);
187static void int_free_ex_data(int class_index, void *obj, 187static void int_free_ex_data(int class_index, void *obj,
188 CRYPTO_EX_DATA *ad); 188 CRYPTO_EX_DATA *ad);
189static CRYPTO_EX_DATA_IMPL impl_default = 189
190 { 190static CRYPTO_EX_DATA_IMPL impl_default = {
191 int_new_class, 191 int_new_class,
192 int_cleanup, 192 int_cleanup,
193 int_get_new_index, 193 int_get_new_index,
194 int_new_ex_data, 194 int_new_ex_data,
195 int_dup_ex_data, 195 int_dup_ex_data,
196 int_free_ex_data 196 int_free_ex_data
197 }; 197};
198 198
199/* Internal function that checks whether "impl" is set and if not, sets it to 199/* Internal function that checks whether "impl" is set and if not, sets it to
200 * the default. */ 200 * the default. */
201static void impl_check(void) 201static void
202 { 202impl_check(void)
203{
203 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 204 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
204 if(!impl) 205 if (!impl)
205 impl = &impl_default; 206 impl = &impl_default;
206 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 207 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
207 } 208}
208/* A macro wrapper for impl_check that first uses a non-locked test before 209/* A macro wrapper for impl_check that first uses a non-locked test before
209 * invoking the function (which checks again inside a lock). */ 210 * invoking the function (which checks again inside a lock). */
210#define IMPL_CHECK if(!impl) impl_check(); 211#define IMPL_CHECK if(!impl) impl_check();
211 212
212/* API functions to get/set the "ex_data" implementation */ 213/* API functions to get/set the "ex_data" implementation */
213const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void) 214const CRYPTO_EX_DATA_IMPL
214 { 215*CRYPTO_get_ex_data_implementation(void)
216{
215 IMPL_CHECK 217 IMPL_CHECK
216 return impl; 218 return impl;
217 } 219}
218int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i) 220
219 { 221int
222CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
223{
220 int toret = 0; 224 int toret = 0;
221 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 225 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
222 if(!impl) 226 if (!impl) {
223 {
224 impl = i; 227 impl = i;
225 toret = 1; 228 toret = 1;
226 } 229 }
227 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 230 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
228 return toret; 231 return toret;
229 } 232}
230 233
231/****************************************************************************/ 234/****************************************************************************/
232/* Interal (default) implementation of "ex_data" support. API functions are 235/* Interal (default) implementation of "ex_data" support. API functions are
@@ -249,288 +252,283 @@ DECLARE_LHASH_OF(EX_CLASS_ITEM);
249static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL; 252static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL;
250 253
251/* The callbacks required in the "ex_data" hash table */ 254/* The callbacks required in the "ex_data" hash table */
252static unsigned long ex_class_item_hash(const EX_CLASS_ITEM *a) 255static unsigned long
253 { 256ex_class_item_hash(const EX_CLASS_ITEM *a)
257{
254 return a->class_index; 258 return a->class_index;
255 } 259}
260
256static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM) 261static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM)
257 262
258static int ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b) 263static int
259 { 264ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b)
265{
260 return a->class_index - b->class_index; 266 return a->class_index - b->class_index;
261 } 267}
268
262static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM) 269static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM)
263 270
264/* Internal functions used by the "impl_default" implementation to access the 271/* Internal functions used by the "impl_default" implementation to access the
265 * state */ 272 * state */
266 273
267static int ex_data_check(void) 274static int
268 { 275ex_data_check(void)
276{
269 int toret = 1; 277 int toret = 1;
270 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 278 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
271 if(!ex_data 279 if (!ex_data
272 && (ex_data = lh_EX_CLASS_ITEM_new()) == NULL) 280 && (ex_data = lh_EX_CLASS_ITEM_new()) == NULL)
273 toret = 0; 281 toret = 0;
274 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 282 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
275 return toret; 283 return toret;
276 } 284}
277/* This macros helps reduce the locking from repeated checks because the 285/* This macros helps reduce the locking from repeated checks because the
278 * ex_data_check() function checks ex_data again inside a lock. */ 286 * ex_data_check() function checks ex_data again inside a lock. */
279#define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail} 287#define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
280 288
281/* This "inner" callback is used by the callback function that follows it */ 289/* This "inner" callback is used by the callback function that follows it */
282static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs) 290static void
283 { 291def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
292{
284 OPENSSL_free(funcs); 293 OPENSSL_free(funcs);
285 } 294}
286 295
287/* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from 296/* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
288 * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do 297 * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do
289 * any locking. */ 298 * any locking. */
290static void def_cleanup_cb(void *a_void) 299static void
291 { 300def_cleanup_cb(void *a_void)
301{
292 EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void; 302 EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
293 sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb); 303 sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
294 OPENSSL_free(item); 304 OPENSSL_free(item);
295 } 305}
296 306
297/* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a 307/* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a
298 * given class. Handles locking. */ 308 * given class. Handles locking. */
299static EX_CLASS_ITEM *def_get_class(int class_index) 309static EX_CLASS_ITEM
300 { 310*def_get_class(int class_index)
311{
301 EX_CLASS_ITEM d, *p, *gen; 312 EX_CLASS_ITEM d, *p, *gen;
302 EX_DATA_CHECK(return NULL;) 313 EX_DATA_CHECK(return NULL;)
303 d.class_index = class_index; 314 d.class_index = class_index;
304 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 315 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
305 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d); 316 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d);
306 if(!p) 317 if (!p) {
307 {
308 gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM)); 318 gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
309 if(gen) 319 if (gen) {
310 {
311 gen->class_index = class_index; 320 gen->class_index = class_index;
312 gen->meth_num = 0; 321 gen->meth_num = 0;
313 gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null(); 322 gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
314 if(!gen->meth) 323 if (!gen->meth)
315 OPENSSL_free(gen); 324 OPENSSL_free(gen);
316 else 325 else {
317 {
318 /* Because we're inside the ex_data lock, the 326 /* Because we're inside the ex_data lock, the
319 * return value from the insert will be NULL */ 327 * return value from the insert will be NULL */
320 (void)lh_EX_CLASS_ITEM_insert(ex_data, gen); 328 (void)lh_EX_CLASS_ITEM_insert(ex_data, gen);
321 p = gen; 329 p = gen;
322 }
323 } 330 }
324 } 331 }
332 }
325 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 333 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
326 if(!p) 334 if (!p)
327 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS,ERR_R_MALLOC_FAILURE); 335 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
328 return p; 336 return p;
329 } 337}
330 338
331/* Add a new method to the given EX_CLASS_ITEM and return the corresponding 339/* Add a new method to the given EX_CLASS_ITEM and return the corresponding
332 * index (or -1 for error). Handles locking. */ 340 * index (or -1 for error). Handles locking. */
333static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp, 341static int
334 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 342def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
335 CRYPTO_EX_free *free_func) 343 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
336 { 344{
337 int toret = -1; 345 int toret = -1;
338 CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc( 346 CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(
339 sizeof(CRYPTO_EX_DATA_FUNCS)); 347 sizeof(CRYPTO_EX_DATA_FUNCS));
340 if(!a) 348 if (!a) {
341 { 349 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
342 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
343 return -1; 350 return -1;
344 } 351 }
345 a->argl=argl; 352 a->argl = argl;
346 a->argp=argp; 353 a->argp = argp;
347 a->new_func=new_func; 354 a->new_func = new_func;
348 a->dup_func=dup_func; 355 a->dup_func = dup_func;
349 a->free_func=free_func; 356 a->free_func = free_func;
350 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 357 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
351 while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) 358 while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) {
352 { 359 if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) {
353 if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) 360 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
354 {
355 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
356 OPENSSL_free(a); 361 OPENSSL_free(a);
357 goto err; 362 goto err;
358 }
359 } 363 }
364 }
360 toret = item->meth_num++; 365 toret = item->meth_num++;
361 (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a); 366 (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
362err: 367err:
363 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 368 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
364 return toret; 369 return toret;
365 } 370}
366 371
367/**************************************************************/ 372/**************************************************************/
368/* The functions in the default CRYPTO_EX_DATA_IMPL structure */ 373/* The functions in the default CRYPTO_EX_DATA_IMPL structure */
369 374
370static int int_new_class(void) 375static int
371 { 376int_new_class(void)
377{
372 int toret; 378 int toret;
373 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 379 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
374 toret = ex_class++; 380 toret = ex_class++;
375 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); 381 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
376 return toret; 382 return toret;
377 } 383}
378 384
379static void int_cleanup(void) 385static void
380 { 386int_cleanup(void)
387{
381 EX_DATA_CHECK(return;) 388 EX_DATA_CHECK(return;)
382 lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb); 389 lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb);
383 lh_EX_CLASS_ITEM_free(ex_data); 390 lh_EX_CLASS_ITEM_free(ex_data);
384 ex_data = NULL; 391 ex_data = NULL;
385 impl = NULL; 392 impl = NULL;
386 } 393}
387 394
388static int int_get_new_index(int class_index, long argl, void *argp, 395static int
389 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 396int_get_new_index(int class_index, long argl, void *argp,
390 CRYPTO_EX_free *free_func) 397 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
391 { 398CRYPTO_EX_free *free_func)
399{
392 EX_CLASS_ITEM *item = def_get_class(class_index); 400 EX_CLASS_ITEM *item = def_get_class(class_index);
393 if(!item) 401 if (!item)
394 return -1; 402 return -1;
395 return def_add_index(item, argl, argp, new_func, dup_func, free_func); 403 return def_add_index(item, argl, argp, new_func, dup_func, free_func);
396 } 404}
397 405
398/* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in 406/* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in
399 * the lock, then using them outside the lock. NB: Thread-safety only applies to 407 * the lock, then using them outside the lock. NB: Thread-safety only applies to
400 * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad' 408 * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad'
401 * itself. */ 409 * itself. */
402static int int_new_ex_data(int class_index, void *obj, 410static int
403 CRYPTO_EX_DATA *ad) 411int_new_ex_data(int class_index, void *obj,
404 { 412 CRYPTO_EX_DATA *ad)
405 int mx,i; 413{
414 int mx, i;
406 void *ptr; 415 void *ptr;
407 CRYPTO_EX_DATA_FUNCS **storage = NULL; 416 CRYPTO_EX_DATA_FUNCS **storage = NULL;
408 EX_CLASS_ITEM *item = def_get_class(class_index); 417 EX_CLASS_ITEM *item = def_get_class(class_index);
409 if(!item) 418 if (!item)
410 /* error is already set */ 419 /* error is already set */
411 return 0; 420 return 0;
412 ad->sk = NULL; 421 ad->sk = NULL;
413 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 422 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
414 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); 423 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
415 if(mx > 0) 424 if (mx > 0) {
416 {
417 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); 425 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
418 if(!storage) 426 if (!storage)
419 goto skip; 427 goto skip;
420 for(i = 0; i < mx; i++) 428 for (i = 0; i < mx; i++)
421 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); 429 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
422 } 430 }
423skip: 431skip:
424 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); 432 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
425 if((mx > 0) && !storage) 433 if ((mx > 0) && !storage) {
426 { 434 CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
427 CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA,ERR_R_MALLOC_FAILURE);
428 return 0; 435 return 0;
429 } 436 }
430 for(i = 0; i < mx; i++) 437 for (i = 0; i < mx; i++) {
431 { 438 if (storage[i] && storage[i]->new_func) {
432 if(storage[i] && storage[i]->new_func)
433 {
434 ptr = CRYPTO_get_ex_data(ad, i); 439 ptr = CRYPTO_get_ex_data(ad, i);
435 storage[i]->new_func(obj,ptr,ad,i, 440 storage[i]->new_func(obj, ptr, ad, i,
436 storage[i]->argl,storage[i]->argp); 441 storage[i]->argl, storage[i]->argp);
437 }
438 } 442 }
439 if(storage) 443 }
444 if (storage)
440 OPENSSL_free(storage); 445 OPENSSL_free(storage);
441 return 1; 446 return 1;
442 } 447}
443 448
444/* Same thread-safety notes as for "int_new_ex_data" */ 449/* Same thread-safety notes as for "int_new_ex_data" */
445static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 450static int
446 CRYPTO_EX_DATA *from) 451int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
447 { 452 CRYPTO_EX_DATA *from)
453{
448 int mx, j, i; 454 int mx, j, i;
449 char *ptr; 455 char *ptr;
450 CRYPTO_EX_DATA_FUNCS **storage = NULL; 456 CRYPTO_EX_DATA_FUNCS **storage = NULL;
451 EX_CLASS_ITEM *item; 457 EX_CLASS_ITEM *item;
452 if(!from->sk) 458 if (!from->sk)
453 /* 'to' should be "blank" which *is* just like 'from' */ 459 /* 'to' should be "blank" which *is* just like 'from' */
454 return 1; 460 return 1;
455 if((item = def_get_class(class_index)) == NULL) 461 if ((item = def_get_class(class_index)) == NULL)
456 return 0; 462 return 0;
457 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 463 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
458 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); 464 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
459 j = sk_void_num(from->sk); 465 j = sk_void_num(from->sk);
460 if(j < mx) 466 if (j < mx)
461 mx = j; 467 mx = j;
462 if(mx > 0) 468 if (mx > 0) {
463 {
464 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); 469 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
465 if(!storage) 470 if (!storage)
466 goto skip; 471 goto skip;
467 for(i = 0; i < mx; i++) 472 for (i = 0; i < mx; i++)
468 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); 473 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
469 } 474 }
470skip: 475skip:
471 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); 476 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
472 if((mx > 0) && !storage) 477 if ((mx > 0) && !storage) {
473 { 478 CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
474 CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA,ERR_R_MALLOC_FAILURE);
475 return 0; 479 return 0;
476 } 480 }
477 for(i = 0; i < mx; i++) 481 for (i = 0; i < mx; i++) {
478 {
479 ptr = CRYPTO_get_ex_data(from, i); 482 ptr = CRYPTO_get_ex_data(from, i);
480 if(storage[i] && storage[i]->dup_func) 483 if (storage[i] && storage[i]->dup_func)
481 storage[i]->dup_func(to,from,&ptr,i, 484 storage[i]->dup_func(to, from, &ptr, i,
482 storage[i]->argl,storage[i]->argp); 485 storage[i]->argl, storage[i]->argp);
483 CRYPTO_set_ex_data(to,i,ptr); 486 CRYPTO_set_ex_data(to, i, ptr);
484 } 487 }
485 if(storage) 488 if (storage)
486 OPENSSL_free(storage); 489 OPENSSL_free(storage);
487 return 1; 490 return 1;
488 } 491}
489 492
490/* Same thread-safety notes as for "int_new_ex_data" */ 493/* Same thread-safety notes as for "int_new_ex_data" */
491static void int_free_ex_data(int class_index, void *obj, 494static void
492 CRYPTO_EX_DATA *ad) 495int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
493 { 496{
494 int mx,i; 497 int mx, i;
495 EX_CLASS_ITEM *item; 498 EX_CLASS_ITEM *item;
496 void *ptr; 499 void *ptr;
497 CRYPTO_EX_DATA_FUNCS **storage = NULL; 500 CRYPTO_EX_DATA_FUNCS **storage = NULL;
498 if((item = def_get_class(class_index)) == NULL) 501 if ((item = def_get_class(class_index)) == NULL)
499 return; 502 return;
500 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 503 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
501 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); 504 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
502 if(mx > 0) 505 if (mx > 0) {
503 {
504 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); 506 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
505 if(!storage) 507 if (!storage)
506 goto skip; 508 goto skip;
507 for(i = 0; i < mx; i++) 509 for (i = 0; i < mx; i++)
508 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); 510 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
509 } 511 }
510skip: 512skip:
511 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); 513 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
512 if((mx > 0) && !storage) 514 if ((mx > 0) && !storage) {
513 { 515 CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
514 CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA,ERR_R_MALLOC_FAILURE);
515 return; 516 return;
517 }
518 for (i = 0; i < mx; i++) {
519 if (storage[i] && storage[i]->free_func) {
520 ptr = CRYPTO_get_ex_data(ad, i);
521 storage[i]->free_func(obj, ptr, ad, i,
522 storage[i]->argl, storage[i]->argp);
516 } 523 }
517 for(i = 0; i < mx; i++) 524 }
518 { 525 if (storage)
519 if(storage[i] && storage[i]->free_func)
520 {
521 ptr = CRYPTO_get_ex_data(ad,i);
522 storage[i]->free_func(obj,ptr,ad,i,
523 storage[i]->argl,storage[i]->argp);
524 }
525 }
526 if(storage)
527 OPENSSL_free(storage); 526 OPENSSL_free(storage);
528 if(ad->sk) 527 if (ad->sk) {
529 {
530 sk_void_free(ad->sk); 528 sk_void_free(ad->sk);
531 ad->sk=NULL; 529 ad->sk = NULL;
532 }
533 } 530 }
531}
534 532
535/********************************************************************/ 533/********************************************************************/
536/* API functions that defer all "state" operations to the "ex_data" 534/* API functions that defer all "state" operations to the "ex_data"
@@ -538,99 +536,101 @@ skip:
538 536
539/* Obtain an index for a new class (not the same as getting a new index within 537/* Obtain an index for a new class (not the same as getting a new index within
540 * an existing class - this is actually getting a new *class*) */ 538 * an existing class - this is actually getting a new *class*) */
541int CRYPTO_ex_data_new_class(void) 539int
542 { 540CRYPTO_ex_data_new_class(void)
541{
543 IMPL_CHECK 542 IMPL_CHECK
544 return EX_IMPL(new_class)(); 543 return EX_IMPL(new_class)();
545 } 544}
546 545
547/* Release all "ex_data" state to prevent memory leaks. This can't be made 546/* Release all "ex_data" state to prevent memory leaks. This can't be made
548 * thread-safe without overhauling a lot of stuff, and shouldn't really be 547 * thread-safe without overhauling a lot of stuff, and shouldn't really be
549 * called under potential race-conditions anyway (it's for program shutdown 548 * called under potential race-conditions anyway (it's for program shutdown
550 * after all). */ 549 * after all). */
551void CRYPTO_cleanup_all_ex_data(void) 550void
552 { 551CRYPTO_cleanup_all_ex_data(void)
552{
553 IMPL_CHECK 553 IMPL_CHECK
554 EX_IMPL(cleanup)(); 554 EX_IMPL(cleanup)();
555 } 555}
556 556
557/* Inside an existing class, get/register a new index. */ 557/* Inside an existing class, get/register a new index. */
558int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, 558int
559 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, 559CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
560 CRYPTO_EX_free *free_func) 560 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
561 { 561{
562 int ret = -1; 562 int ret = -1;
563 563
564 IMPL_CHECK 564 IMPL_CHECK
565 ret = EX_IMPL(get_new_index)(class_index, 565 ret = EX_IMPL(get_new_index)(class_index,
566 argl, argp, new_func, dup_func, free_func); 566 argl, argp, new_func, dup_func, free_func);
567 return ret; 567 return ret;
568 } 568}
569 569
570/* Initialise a new CRYPTO_EX_DATA for use in a particular class - including 570/* Initialise a new CRYPTO_EX_DATA for use in a particular class - including
571 * calling new() callbacks for each index in the class used by this variable */ 571 * calling new() callbacks for each index in the class used by this variable */
572int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) 572int
573 { 573CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
574{
574 IMPL_CHECK 575 IMPL_CHECK
575 return EX_IMPL(new_ex_data)(class_index, obj, ad); 576 return EX_IMPL(new_ex_data)(class_index, obj, ad);
576 } 577}
577 578
578/* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for 579/* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for
579 * each index in the class used by this variable */ 580 * each index in the class used by this variable */
580int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, 581int
581 CRYPTO_EX_DATA *from) 582CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from)
582 { 583{
583 IMPL_CHECK 584 IMPL_CHECK
584 return EX_IMPL(dup_ex_data)(class_index, to, from); 585 return EX_IMPL(dup_ex_data)(class_index, to, from);
585 } 586}
586 587
587/* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for 588/* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
588 * each index in the class used by this variable */ 589 * each index in the class used by this variable */
589void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) 590void
590 { 591CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
592{
591 IMPL_CHECK 593 IMPL_CHECK
592 EX_IMPL(free_ex_data)(class_index, obj, ad); 594 EX_IMPL(free_ex_data)(class_index, obj, ad);
593 } 595}
594 596
595/* For a given CRYPTO_EX_DATA variable, set the value corresponding to a 597/* For a given CRYPTO_EX_DATA variable, set the value corresponding to a
596 * particular index in the class used by this variable */ 598 * particular index in the class used by this variable */
597int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val) 599int
598 { 600CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
601{
599 int i; 602 int i;
600 603
601 if (ad->sk == NULL) 604 if (ad->sk == NULL) {
602 { 605 if ((ad->sk = sk_void_new_null()) == NULL) {
603 if ((ad->sk=sk_void_new_null()) == NULL) 606 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
604 { 607 return (0);
605 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
606 return(0);
607 }
608 } 608 }
609 i=sk_void_num(ad->sk); 609 }
610 610 i = sk_void_num(ad->sk);
611 while (i <= idx) 611
612 { 612 while (i <= idx) {
613 if (!sk_void_push(ad->sk,NULL)) 613 if (!sk_void_push(ad->sk, NULL)) {
614 { 614 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
615 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE); 615 return (0);
616 return(0);
617 }
618 i++;
619 } 616 }
620 sk_void_set(ad->sk,idx,val); 617 i++;
621 return(1);
622 } 618 }
619 sk_void_set(ad->sk, idx, val);
620 return (1);
621}
623 622
624/* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a 623/* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
625 * particular index in the class used by this variable */ 624 * particular index in the class used by this variable */
626void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) 625void
627 { 626*CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
627{
628 if (ad->sk == NULL) 628 if (ad->sk == NULL)
629 return(0); 629 return (0);
630 else if (idx >= sk_void_num(ad->sk)) 630 else if (idx >= sk_void_num(ad->sk))
631 return(0); 631 return (0);
632 else 632 else
633 return(sk_void_value(ad->sk,idx)); 633 return (sk_void_value(ad->sk, idx));
634 } 634}
635 635
636IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS) 636IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)
diff --git a/src/lib/libssl/src/crypto/fips_ers.c b/src/lib/libssl/src/crypto/fips_ers.c
index 09f11748f6..1788ed2884 100644
--- a/src/lib/libssl/src/crypto/fips_ers.c
+++ b/src/lib/libssl/src/crypto/fips_ers.c
@@ -3,5 +3,5 @@
3#ifdef OPENSSL_FIPS 3#ifdef OPENSSL_FIPS
4# include "fips_err.h" 4# include "fips_err.h"
5#else 5#else
6static void *dummy=&dummy; 6static void *dummy = &dummy;
7#endif 7#endif
diff --git a/src/lib/libssl/src/crypto/md32_common.h b/src/lib/libssl/src/crypto/md32_common.h
index bb7381952a..bfb610e802 100644
--- a/src/lib/libssl/src/crypto/md32_common.h
+++ b/src/lib/libssl/src/crypto/md32_common.h
@@ -148,7 +148,7 @@
148# if defined(__POWERPC__) 148# if defined(__POWERPC__)
149# define ROTATE(a,n) __rlwinm(a,n,0,31) 149# define ROTATE(a,n) __rlwinm(a,n,0,31)
150# elif defined(__MC68K__) 150# elif defined(__MC68K__)
151 /* Motorola specific tweak. <appro@fy.chalmers.se> */ 151/* Motorola specific tweak. <appro@fy.chalmers.se> */
152# define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) ) 152# define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
153# else 153# else
154# define ROTATE(a,n) __rol(a,n) 154# define ROTATE(a,n) __rol(a,n)
@@ -252,7 +252,7 @@
252#endif 252#endif
253#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) 253#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
254# ifndef B_ENDIAN 254# ifndef B_ENDIAN
255 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */ 255/* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
256# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l) 256# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
257# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l) 257# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
258# endif 258# endif
@@ -279,108 +279,103 @@
279 * Time for some action:-) 279 * Time for some action:-)
280 */ 280 */
281 281
282int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len) 282int
283 { 283HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
284 const unsigned char *data=data_; 284{
285 const unsigned char *data = data_;
285 unsigned char *p; 286 unsigned char *p;
286 HASH_LONG l; 287 HASH_LONG l;
287 size_t n; 288 size_t n;
288 289
289 if (len==0) return 1; 290 if (len == 0)
291 return 1;
290 292
291 l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL; 293 l = (c->Nl + (((HASH_LONG)len) << 3))&0xffffffffUL;
292 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to 294 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
293 * Wei Dai <weidai@eskimo.com> for pointing it out. */ 295 * Wei Dai <weidai@eskimo.com> for pointing it out. */
294 if (l < c->Nl) /* overflow */ 296 if (l < c->Nl) /* overflow */
295 c->Nh++; 297 c->Nh++;
296 c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */ 298 c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */
297 c->Nl=l; 299 c->Nl = l;
298 300
299 n = c->num; 301 n = c->num;
300 if (n != 0) 302 if (n != 0) {
301 { 303 p = (unsigned char *)c->data;
302 p=(unsigned char *)c->data; 304
303 305 if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
304 if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK) 306 memcpy (p + n, data, HASH_CBLOCK - n);
305 { 307 HASH_BLOCK_DATA_ORDER (c, p, 1);
306 memcpy (p+n,data,HASH_CBLOCK-n); 308 n = HASH_CBLOCK - n;
307 HASH_BLOCK_DATA_ORDER (c,p,1); 309 data += n;
308 n = HASH_CBLOCK-n; 310 len -= n;
309 data += n;
310 len -= n;
311 c->num = 0; 311 c->num = 0;
312 memset (p,0,HASH_CBLOCK); /* keep it zeroed */ 312 memset (p,0,HASH_CBLOCK); /* keep it zeroed */
313 } 313 } else {
314 else 314 memcpy (p + n, data, len);
315 {
316 memcpy (p+n,data,len);
317 c->num += (unsigned int)len; 315 c->num += (unsigned int)len;
318 return 1; 316 return 1;
319 }
320 } 317 }
318 }
321 319
322 n = len/HASH_CBLOCK; 320 n = len/HASH_CBLOCK;
323 if (n > 0) 321 if (n > 0) {
324 { 322 HASH_BLOCK_DATA_ORDER (c, data, n);
325 HASH_BLOCK_DATA_ORDER (c,data,n);
326 n *= HASH_CBLOCK; 323 n *= HASH_CBLOCK;
327 data += n; 324 data += n;
328 len -= n; 325 len -= n;
329 } 326 }
330 327
331 if (len != 0) 328 if (len != 0) {
332 {
333 p = (unsigned char *)c->data; 329 p = (unsigned char *)c->data;
334 c->num = (unsigned int)len; 330 c->num = (unsigned int)len;
335 memcpy (p,data,len); 331 memcpy (p, data, len);
336 }
337 return 1;
338 } 332 }
333 return 1;
334}
339 335
340 336
341void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data) 337void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
342 { 338{
343 HASH_BLOCK_DATA_ORDER (c,data,1); 339 HASH_BLOCK_DATA_ORDER (c, data, 1);
344 } 340}
345 341
346 342
347int HASH_FINAL (unsigned char *md, HASH_CTX *c) 343int HASH_FINAL (unsigned char *md, HASH_CTX *c)
348 { 344{
349 unsigned char *p = (unsigned char *)c->data; 345 unsigned char *p = (unsigned char *)c->data;
350 size_t n = c->num; 346 size_t n = c->num;
351 347
352 p[n] = 0x80; /* there is always room for one */ 348 p[n] = 0x80; /* there is always room for one */
353 n++; 349 n++;
354 350
355 if (n > (HASH_CBLOCK-8)) 351 if (n > (HASH_CBLOCK - 8)) {
356 { 352 memset (p + n, 0, HASH_CBLOCK - n);
357 memset (p+n,0,HASH_CBLOCK-n); 353 n = 0;
358 n=0; 354 HASH_BLOCK_DATA_ORDER (c, p, 1);
359 HASH_BLOCK_DATA_ORDER (c,p,1); 355 }
360 } 356 memset (p + n, 0, HASH_CBLOCK - 8 - n);
361 memset (p+n,0,HASH_CBLOCK-8-n);
362 357
363 p += HASH_CBLOCK-8; 358 p += HASH_CBLOCK - 8;
364#if defined(DATA_ORDER_IS_BIG_ENDIAN) 359#if defined(DATA_ORDER_IS_BIG_ENDIAN)
365 (void)HOST_l2c(c->Nh,p); 360 (void)HOST_l2c(c->Nh, p);
366 (void)HOST_l2c(c->Nl,p); 361 (void)HOST_l2c(c->Nl, p);
367#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) 362#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
368 (void)HOST_l2c(c->Nl,p); 363 (void)HOST_l2c(c->Nl, p);
369 (void)HOST_l2c(c->Nh,p); 364 (void)HOST_l2c(c->Nh, p);
370#endif 365#endif
371 p -= HASH_CBLOCK; 366 p -= HASH_CBLOCK;
372 HASH_BLOCK_DATA_ORDER (c,p,1); 367 HASH_BLOCK_DATA_ORDER (c, p, 1);
373 c->num=0; 368 c->num = 0;
374 memset (p,0,HASH_CBLOCK); 369 memset (p, 0, HASH_CBLOCK);
375 370
376#ifndef HASH_MAKE_STRING 371#ifndef HASH_MAKE_STRING
377#error "HASH_MAKE_STRING must be defined!" 372#error "HASH_MAKE_STRING must be defined!"
378#else 373#else
379 HASH_MAKE_STRING(c,md); 374 HASH_MAKE_STRING(c, md);
380#endif 375#endif
381 376
382 return 1; 377 return 1;
383 } 378}
384 379
385#ifndef MD32_REG_T 380#ifndef MD32_REG_T
386#if defined(__alpha) || defined(__sparcv9) || defined(__mips) 381#if defined(__alpha) || defined(__sparcv9) || defined(__mips)
diff --git a/src/lib/libssl/src/crypto/mem.c b/src/lib/libssl/src/crypto/mem.c
index 55e829dc92..86e5ff1c8e 100644
--- a/src/lib/libssl/src/crypto/mem.c
+++ b/src/lib/libssl/src/crypto/mem.c
@@ -62,206 +62,230 @@
62#include "cryptlib.h" 62#include "cryptlib.h"
63 63
64 64
65static int allow_customize = 1; /* we provide flexible functions for */ 65static int allow_customize = 1;
66static int allow_customize_debug = 1;/* exchanging memory-related functions at 66/* we provide flexible functions for */
67 * run-time, but this must be done 67static int allow_customize_debug = 1;/* exchanging memory - related functions at
68 * before any blocks are actually 68 * run - time, but this must be done
69 * allocated; or we'll run into huge 69 * before any blocks are actually
70 * problems when malloc/free pairs 70 * allocated; or we'll run into huge
71 * don't match etc. */ 71 * problems when malloc/free pairs
72 72 * don't match etc. */
73
74 73
75/* the following pointers may be changed as long as 'allow_customize' is set */ 74/* the following pointers may be changed as long as 'allow_customize' is set */
76 75
77static void *(*malloc_func)(size_t) = malloc; 76static void *(*malloc_func)(size_t) = malloc;
78static void *default_malloc_ex(size_t num, const char *file, int line) 77static void
79 { return malloc_func(num); } 78*default_malloc_ex(size_t num, const char *file, int line)
80static void *(*malloc_ex_func)(size_t, const char *file, int line) 79{
81 = default_malloc_ex; 80 return malloc_func(num);
82 81}
83static void *(*realloc_func)(void *, size_t)= realloc; 82static void *(*malloc_ex_func)(size_t, const char *file, int line) =
84static void *default_realloc_ex(void *str, size_t num, 83 default_malloc_ex;
85 const char *file, int line) 84
86 { return realloc_func(str,num); } 85static void *(*realloc_func)(void *, size_t) = realloc;
87static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) 86static void
88 = default_realloc_ex; 87*default_realloc_ex(void *str, size_t num, const char *file, int line)
89 88{
90static void (*free_func)(void *) = free; 89 return realloc_func(str, num);
91 90}
92static void *(*malloc_locked_func)(size_t) = malloc; 91static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) =
93static void *default_malloc_locked_ex(size_t num, const char *file, int line) 92 default_realloc_ex;
94 { return malloc_locked_func(num); } 93
95static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) 94static void (*free_func)(void *) = free;
96 = default_malloc_locked_ex; 95
97 96static void *(*malloc_locked_func)(size_t) = malloc;
98static void (*free_locked_func)(void *) = free; 97static void
99 98*default_malloc_locked_ex(size_t num, const char *file, int line)
99{
100 return malloc_locked_func(num);
101}
102static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) =
103 default_malloc_locked_ex;
104
105static void (*free_locked_func)(void *) = free;
100 106
101 107
102/* may be changed as long as 'allow_customize_debug' is set */ 108/* may be changed as long as 'allow_customize_debug' is set */
103/* XXX use correct function pointer types */ 109/* XXX use correct function pointer types */
104#ifdef CRYPTO_MDEBUG 110#ifdef CRYPTO_MDEBUG
105/* use default functions from mem_dbg.c */ 111/* use default functions from mem_dbg.c */
106static void (*malloc_debug_func)(void *,int,const char *,int,int) 112static void (*malloc_debug_func)(void *, int, const char *, int, int) =
107 = CRYPTO_dbg_malloc; 113 CRYPTO_dbg_malloc;
108static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) 114static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) =
109 = CRYPTO_dbg_realloc; 115 CRYPTO_dbg_realloc;
110static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; 116static void (*free_debug_func)(void *, int) = CRYPTO_dbg_free;
111static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; 117static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
112static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; 118static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
113#else 119#else
114/* applications can use CRYPTO_malloc_debug_init() to select above case 120/* applications can use CRYPTO_malloc_debug_init() to select above case
115 * at run-time */ 121 * at run-time */
116static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; 122static void (*malloc_debug_func)(void *, int, const char *, int, int) = NULL;
117static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) 123static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) =
118 = NULL; 124 NULL;
119static void (*free_debug_func)(void *,int) = NULL; 125static void (*free_debug_func)(void *, int) = NULL;
120static void (*set_debug_options_func)(long) = NULL; 126static void (*set_debug_options_func)(long) = NULL;
121static long (*get_debug_options_func)(void) = NULL; 127static long (*get_debug_options_func)(void) = NULL;
122#endif 128#endif
123 129
124int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), 130int
125 void (*f)(void *)) 131CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
126 { 132 void (*f)(void *))
133{
127 /* Dummy call just to ensure OPENSSL_init() gets linked in */ 134 /* Dummy call just to ensure OPENSSL_init() gets linked in */
128 OPENSSL_init(); 135 OPENSSL_init();
129 if (!allow_customize) 136 if (!allow_customize)
130 return 0; 137 return 0;
131 if ((m == 0) || (r == 0) || (f == 0)) 138 if ((m == 0) || (r == 0) || (f == 0))
132 return 0; 139 return 0;
133 malloc_func=m; malloc_ex_func=default_malloc_ex; 140 malloc_func = m;
134 realloc_func=r; realloc_ex_func=default_realloc_ex; 141 malloc_ex_func = default_malloc_ex;
135 free_func=f; 142 realloc_func = r;
136 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; 143 realloc_ex_func = default_realloc_ex;
137 free_locked_func=f; 144 free_func = f;
145 malloc_locked_func = m;
146 malloc_locked_ex_func = default_malloc_locked_ex;
147 free_locked_func = f;
138 return 1; 148 return 1;
139 } 149}
140 150
141int CRYPTO_set_mem_ex_functions( 151int
142 void *(*m)(size_t,const char *,int), 152CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int),
143 void *(*r)(void *, size_t,const char *,int), 153 void *(*r)(void *, size_t, const char *, int), void (*f)(void *))
144 void (*f)(void *)) 154{
145 {
146 if (!allow_customize) 155 if (!allow_customize)
147 return 0; 156 return 0;
148 if ((m == 0) || (r == 0) || (f == 0)) 157 if ((m == 0) || (r == 0) || (f == 0))
149 return 0; 158 return 0;
150 malloc_func=0; malloc_ex_func=m; 159 malloc_func = 0;
151 realloc_func=0; realloc_ex_func=r; 160 malloc_ex_func = m;
152 free_func=f; 161 realloc_func = 0;
153 malloc_locked_func=0; malloc_locked_ex_func=m; 162 realloc_ex_func = r;
154 free_locked_func=f; 163 free_func = f;
164 malloc_locked_func = 0;
165 malloc_locked_ex_func = m;
166 free_locked_func = f;
155 return 1; 167 return 1;
156 } 168}
157 169
158int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) 170int
159 { 171CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
172{
160 if (!allow_customize) 173 if (!allow_customize)
161 return 0; 174 return 0;
162 if ((m == NULL) || (f == NULL)) 175 if ((m == NULL) || (f == NULL))
163 return 0; 176 return 0;
164 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; 177 malloc_locked_func = m;
165 free_locked_func=f; 178 malloc_locked_ex_func = default_malloc_locked_ex;
179 free_locked_func = f;
166 return 1; 180 return 1;
167 } 181}
168 182
169int CRYPTO_set_locked_mem_ex_functions( 183int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int),
170 void *(*m)(size_t,const char *,int), 184 void (*f)(void *))
171 void (*f)(void *)) 185{
172 {
173 if (!allow_customize) 186 if (!allow_customize)
174 return 0; 187 return 0;
175 if ((m == NULL) || (f == NULL)) 188 if ((m == NULL) || (f == NULL))
176 return 0; 189 return 0;
177 malloc_locked_func=0; malloc_locked_ex_func=m; 190 malloc_locked_func = 0;
178 free_func=f; 191 malloc_locked_ex_func = m;
192 free_func = f;
179 return 1; 193 return 1;
180 } 194}
181 195
182int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), 196int
183 void (*r)(void *,void *,int,const char *,int,int), 197CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int),
184 void (*f)(void *,int), 198 void (*r)(void *, void *, int, const char *, int, int),
185 void (*so)(long), 199 void (*f)(void *, int), void (*so)(long), long (*go)(void))
186 long (*go)(void)) 200{
187 {
188 if (!allow_customize_debug) 201 if (!allow_customize_debug)
189 return 0; 202 return 0;
190 OPENSSL_init(); 203 OPENSSL_init();
191 malloc_debug_func=m; 204 malloc_debug_func = m;
192 realloc_debug_func=r; 205 realloc_debug_func = r;
193 free_debug_func=f; 206 free_debug_func = f;
194 set_debug_options_func=so; 207 set_debug_options_func = so;
195 get_debug_options_func=go; 208 get_debug_options_func = go;
196 return 1; 209 return 1;
197 } 210}
198 211
199 212
200void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), 213void
201 void (**f)(void *)) 214CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
202 { 215 void (**f)(void *))
203 if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? 216{
204 malloc_func : 0; 217 if (m != NULL)
205 if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? 218 *m = (malloc_ex_func == default_malloc_ex) ? malloc_func : 0;
206 realloc_func : 0; 219 if (r != NULL)
207 if (f != NULL) *f=free_func; 220 *r = (realloc_ex_func == default_realloc_ex) ? realloc_func : 0;
208 } 221 if (f != NULL)
209 222 *f = free_func;
210void CRYPTO_get_mem_ex_functions( 223}
211 void *(**m)(size_t,const char *,int), 224
212 void *(**r)(void *, size_t,const char *,int), 225void CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int),
213 void (**f)(void *)) 226 void *(**r)(void *, size_t, const char *, int), void (**f)(void *))
214 { 227{
215 if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ? 228 if (m != NULL)
216 malloc_ex_func : 0; 229 *m = (malloc_ex_func != default_malloc_ex) ? malloc_ex_func : 0;
217 if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ? 230 if (r != NULL)
218 realloc_ex_func : 0; 231 *r = (realloc_ex_func != default_realloc_ex) ?
219 if (f != NULL) *f=free_func; 232 realloc_ex_func : 0;
220 } 233 if (f != NULL)
221 234 *f = free_func;
222void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) 235}
223 { 236
224 if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? 237void
225 malloc_locked_func : 0; 238CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
226 if (f != NULL) *f=free_locked_func; 239{
227 } 240 if (m != NULL)
228 241 *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
229void CRYPTO_get_locked_mem_ex_functions( 242 malloc_locked_func : 0;
230 void *(**m)(size_t,const char *,int), 243 if (f != NULL)
231 void (**f)(void *)) 244 *f = free_locked_func;
232 { 245}
233 if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? 246
234 malloc_locked_ex_func : 0; 247void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int),
235 if (f != NULL) *f=free_locked_func; 248 void (**f)(void *))
236 } 249{
237 250 if (m != NULL)
238void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), 251 *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
239 void (**r)(void *,void *,int,const char *,int,int), 252 malloc_locked_ex_func : 0;
240 void (**f)(void *,int), 253 if (f != NULL)
241 void (**so)(long), 254 *f = free_locked_func;
242 long (**go)(void)) 255}
243 { 256
244 if (m != NULL) *m=malloc_debug_func; 257void
245 if (r != NULL) *r=realloc_debug_func; 258CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int),
246 if (f != NULL) *f=free_debug_func; 259 void (**r)(void *, void *, int, const char *, int, int),
247 if (so != NULL) *so=set_debug_options_func; 260 void (**f)(void *, int), void (**so)(long), long (**go)(void))
248 if (go != NULL) *go=get_debug_options_func; 261{
249 } 262 if (m != NULL)
250 263 *m = malloc_debug_func;
251 264 if (r != NULL)
252void *CRYPTO_malloc_locked(int num, const char *file, int line) 265 *r = realloc_debug_func;
253 { 266 if (f != NULL)
267 *f = free_debug_func;
268 if (so != NULL)
269 *so = set_debug_options_func;
270 if (go != NULL)
271 *go = get_debug_options_func;
272}
273
274
275void
276*CRYPTO_malloc_locked(int num, const char *file, int line)
277{
254 void *ret = NULL; 278 void *ret = NULL;
255 279
256 if (num <= 0) return NULL; 280 if (num <= 0)
281 return NULL;
257 282
258 allow_customize = 0; 283 allow_customize = 0;
259 if (malloc_debug_func != NULL) 284 if (malloc_debug_func != NULL) {
260 {
261 allow_customize_debug = 0; 285 allow_customize_debug = 0;
262 malloc_debug_func(NULL, num, file, line, 0); 286 malloc_debug_func(NULL, num, file, line, 0);
263 } 287 }
264 ret = malloc_locked_ex_func(num,file,line); 288 ret = malloc_locked_ex_func(num, file, line);
265#ifdef LEVITTE_DEBUG_MEM 289#ifdef LEVITTE_DEBUG_MEM
266 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); 290 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
267#endif 291#endif
@@ -272,17 +296,18 @@ void *CRYPTO_malloc_locked(int num, const char *file, int line)
272 /* Create a dependency on the value of 'cleanse_ctr' so our memory 296 /* Create a dependency on the value of 'cleanse_ctr' so our memory
273 * sanitisation function can't be optimised out. NB: We only do 297 * sanitisation function can't be optimised out. NB: We only do
274 * this for >2Kb so the overhead doesn't bother us. */ 298 * this for >2Kb so the overhead doesn't bother us. */
275 if(ret && (num > 2048)) 299 if (ret && (num > 2048)) {
276 { extern unsigned char cleanse_ctr; 300 extern unsigned char cleanse_ctr;
277 ((unsigned char *)ret)[0] = cleanse_ctr; 301 ((unsigned char *)ret)[0] = cleanse_ctr;
278 } 302 }
279#endif 303#endif
280 304
281 return ret; 305 return ret;
282 } 306}
283 307
284void CRYPTO_free_locked(void *str) 308void
285 { 309CRYPTO_free_locked(void *str)
310{
286 if (free_debug_func != NULL) 311 if (free_debug_func != NULL)
287 free_debug_func(str, 0); 312 free_debug_func(str, 0);
288#ifdef LEVITTE_DEBUG_MEM 313#ifdef LEVITTE_DEBUG_MEM
@@ -291,21 +316,22 @@ void CRYPTO_free_locked(void *str)
291 free_locked_func(str); 316 free_locked_func(str);
292 if (free_debug_func != NULL) 317 if (free_debug_func != NULL)
293 free_debug_func(NULL, 1); 318 free_debug_func(NULL, 1);
294 } 319}
295 320
296void *CRYPTO_malloc(int num, const char *file, int line) 321void
297 { 322*CRYPTO_malloc(int num, const char *file, int line)
323{
298 void *ret = NULL; 324 void *ret = NULL;
299 325
300 if (num <= 0) return NULL; 326 if (num <= 0)
327 return NULL;
301 328
302 allow_customize = 0; 329 allow_customize = 0;
303 if (malloc_debug_func != NULL) 330 if (malloc_debug_func != NULL) {
304 {
305 allow_customize_debug = 0; 331 allow_customize_debug = 0;
306 malloc_debug_func(NULL, num, file, line, 0); 332 malloc_debug_func(NULL, num, file, line, 0);
307 } 333 }
308 ret = malloc_ex_func(num,file,line); 334 ret = malloc_ex_func(num, file, line);
309#ifdef LEVITTE_DEBUG_MEM 335#ifdef LEVITTE_DEBUG_MEM
310 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); 336 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
311#endif 337#endif
@@ -316,35 +342,39 @@ void *CRYPTO_malloc(int num, const char *file, int line)
316 /* Create a dependency on the value of 'cleanse_ctr' so our memory 342 /* Create a dependency on the value of 'cleanse_ctr' so our memory
317 * sanitisation function can't be optimised out. NB: We only do 343 * sanitisation function can't be optimised out. NB: We only do
318 * this for >2Kb so the overhead doesn't bother us. */ 344 * this for >2Kb so the overhead doesn't bother us. */
319 if(ret && (num > 2048)) 345 if (ret && (num > 2048)) {
320 { extern unsigned char cleanse_ctr; 346 extern unsigned char cleanse_ctr;
321 ((unsigned char *)ret)[0] = cleanse_ctr; 347 ((unsigned char *)ret)[0] = cleanse_ctr;
322 } 348 }
323#endif 349#endif
324 350
325 return ret; 351 return ret;
326 } 352}
327char *CRYPTO_strdup(const char *str, const char *file, int line) 353
328 { 354char
329 size_t len = strlen(str)+1; 355*CRYPTO_strdup(const char *str, const char *file, int line)
356{
357 size_t len = strlen(str) + 1;
330 char *ret = CRYPTO_malloc(len, file, line); 358 char *ret = CRYPTO_malloc(len, file, line);
331 359
332 memcpy(ret, str, len); 360 memcpy(ret, str, len);
333 return ret; 361 return ret;
334 } 362}
335 363
336void *CRYPTO_realloc(void *str, int num, const char *file, int line) 364void
337 { 365*CRYPTO_realloc(void *str, int num, const char *file, int line)
366{
338 void *ret = NULL; 367 void *ret = NULL;
339 368
340 if (str == NULL) 369 if (str == NULL)
341 return CRYPTO_malloc(num, file, line); 370 return CRYPTO_malloc(num, file, line);
342 371
343 if (num <= 0) return NULL; 372 if (num <= 0)
373 return NULL;
344 374
345 if (realloc_debug_func != NULL) 375 if (realloc_debug_func != NULL)
346 realloc_debug_func(str, NULL, num, file, line, 0); 376 realloc_debug_func(str, NULL, num, file, line, 0);
347 ret = realloc_ex_func(str,num,file,line); 377 ret = realloc_ex_func(str, num, file, line);
348#ifdef LEVITTE_DEBUG_MEM 378#ifdef LEVITTE_DEBUG_MEM
349 fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num); 379 fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num);
350#endif 380#endif
@@ -352,44 +382,47 @@ void *CRYPTO_realloc(void *str, int num, const char *file, int line)
352 realloc_debug_func(str, ret, num, file, line, 1); 382 realloc_debug_func(str, ret, num, file, line, 1);
353 383
354 return ret; 384 return ret;
355 } 385}
356 386
357void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, 387void
358 int line) 388*CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
359 { 389int line)
390{
360 void *ret = NULL; 391 void *ret = NULL;
361 392
362 if (str == NULL) 393 if (str == NULL)
363 return CRYPTO_malloc(num, file, line); 394 return CRYPTO_malloc(num, file, line);
364 395
365 if (num <= 0) return NULL; 396 if (num <= 0)
397 return NULL;
366 398
367 /* We don't support shrinking the buffer. Note the memcpy that copies 399 /* We don't support shrinking the buffer. Note the memcpy that copies
368 * |old_len| bytes to the new buffer, below. */ 400 * |old_len| bytes to the new buffer, below. */
369 if (num < old_len) return NULL; 401 if (num < old_len)
402 return NULL;
370 403
371 if (realloc_debug_func != NULL) 404 if (realloc_debug_func != NULL)
372 realloc_debug_func(str, NULL, num, file, line, 0); 405 realloc_debug_func(str, NULL, num, file, line, 0);
373 ret=malloc_ex_func(num,file,line); 406 ret = malloc_ex_func(num, file, line);
374 if(ret) 407 if (ret) {
375 { 408 memcpy(ret, str, old_len);
376 memcpy(ret,str,old_len); 409 OPENSSL_cleanse(str, old_len);
377 OPENSSL_cleanse(str,old_len);
378 free_func(str); 410 free_func(str);
379 } 411 }
380#ifdef LEVITTE_DEBUG_MEM 412#ifdef LEVITTE_DEBUG_MEM
381 fprintf(stderr, 413 fprintf(stderr,
382 "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", 414 "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n",
383 str, ret, num); 415 str, ret, num);
384#endif 416#endif
385 if (realloc_debug_func != NULL) 417 if (realloc_debug_func != NULL)
386 realloc_debug_func(str, ret, num, file, line, 1); 418 realloc_debug_func(str, ret, num, file, line, 1);
387 419
388 return ret; 420 return ret;
389 } 421}
390 422
391void CRYPTO_free(void *str) 423void
392 { 424CRYPTO_free(void *str)
425{
393 if (free_debug_func != NULL) 426 if (free_debug_func != NULL)
394 free_debug_func(str, 0); 427 free_debug_func(str, 0);
395#ifdef LEVITTE_DEBUG_MEM 428#ifdef LEVITTE_DEBUG_MEM
@@ -398,24 +431,28 @@ void CRYPTO_free(void *str)
398 free_func(str); 431 free_func(str);
399 if (free_debug_func != NULL) 432 if (free_debug_func != NULL)
400 free_debug_func(NULL, 1); 433 free_debug_func(NULL, 1);
401 } 434}
402 435
403void *CRYPTO_remalloc(void *a, int num, const char *file, int line) 436void
404 { 437*CRYPTO_remalloc(void *a, int num, const char *file, int line)
405 if (a != NULL) OPENSSL_free(a); 438{
406 a=(char *)OPENSSL_malloc(num); 439 if (a != NULL)
407 return(a); 440 OPENSSL_free(a);
408 } 441 a = (char *)OPENSSL_malloc(num);
409 442 return (a);
410void CRYPTO_set_mem_debug_options(long bits) 443}
411 { 444
445void
446CRYPTO_set_mem_debug_options(long bits)
447{
412 if (set_debug_options_func != NULL) 448 if (set_debug_options_func != NULL)
413 set_debug_options_func(bits); 449 set_debug_options_func(bits);
414 } 450}
415 451
416long CRYPTO_get_mem_debug_options(void) 452long
417 { 453CRYPTO_get_mem_debug_options(void)
454{
418 if (get_debug_options_func != NULL) 455 if (get_debug_options_func != NULL)
419 return get_debug_options_func(); 456 return get_debug_options_func();
420 return 0; 457 return 0;
421 } 458}
diff --git a/src/lib/libssl/src/crypto/mem_clr.c b/src/lib/libssl/src/crypto/mem_clr.c
index add1f78020..fc108c83b4 100644
--- a/src/lib/libssl/src/crypto/mem_clr.c
+++ b/src/lib/libssl/src/crypto/mem_clr.c
@@ -61,17 +61,17 @@
61 61
62unsigned char cleanse_ctr = 0; 62unsigned char cleanse_ctr = 0;
63 63
64void OPENSSL_cleanse(void *ptr, size_t len) 64void
65 { 65OPENSSL_cleanse(void *ptr, size_t len)
66{
66 unsigned char *p = ptr; 67 unsigned char *p = ptr;
67 size_t loop = len, ctr = cleanse_ctr; 68 size_t loop = len, ctr = cleanse_ctr;
68 while(loop--) 69 while (loop--) {
69 {
70 *(p++) = (unsigned char)ctr; 70 *(p++) = (unsigned char)ctr;
71 ctr += (17 + ((size_t)p & 0xF)); 71 ctr += (17 + ((size_t)p & 0xF));
72 } 72 }
73 p=memchr(ptr, (unsigned char)ctr, len); 73 p = memchr(ptr, (unsigned char)ctr, len);
74 if(p) 74 if (p)
75 ctr += (63 + (size_t)p); 75 ctr += (63 + (size_t)p);
76 cleanse_ctr = (unsigned char)ctr; 76 cleanse_ctr = (unsigned char)ctr;
77 } 77}
diff --git a/src/lib/libssl/src/crypto/mem_dbg.c b/src/lib/libssl/src/crypto/mem_dbg.c
index ac793397f1..9324ec4c7d 100644
--- a/src/lib/libssl/src/crypto/mem_dbg.c
+++ b/src/lib/libssl/src/crypto/mem_dbg.c
@@ -118,7 +118,7 @@
118#include <openssl/bio.h> 118#include <openssl/bio.h>
119#include <openssl/lhash.h> 119#include <openssl/lhash.h>
120 120
121static int mh_mode=CRYPTO_MEM_CHECK_OFF; 121static int mh_mode = CRYPTO_MEM_CHECK_OFF;
122/* The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE 122/* The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE
123 * when the application asks for it (usually after library initialisation 123 * when the application asks for it (usually after library initialisation
124 * for which no book-keeping is desired). 124 * for which no book-keeping is desired).
@@ -136,10 +136,9 @@ static int mh_mode=CRYPTO_MEM_CHECK_OFF;
136static unsigned long order = 0; /* number of memory requests */ 136static unsigned long order = 0; /* number of memory requests */
137 137
138DECLARE_LHASH_OF(MEM); 138DECLARE_LHASH_OF(MEM);
139static LHASH_OF(MEM) *mh=NULL; /* hash-table of memory requests 139static LHASH_OF(MEM) *mh = NULL; /* hash - table of memory requests
140 * (address as key); access requires 140 * (address as key); access requires
141 * MALLOC2 lock */ 141 * MALLOC2 lock */
142
143 142
144typedef struct app_mem_info_st 143typedef struct app_mem_info_st
145/* For application-defined information (static C-string `info') 144/* For application-defined information (static C-string `info')
@@ -149,28 +148,28 @@ typedef struct app_mem_info_st
149 * CRYPTO_pop_info() to pop an entry, 148 * CRYPTO_pop_info() to pop an entry,
150 * CRYPTO_remove_all_info() to pop all entries. 149 * CRYPTO_remove_all_info() to pop all entries.
151 */ 150 */
152 { 151{
153 CRYPTO_THREADID threadid; 152 CRYPTO_THREADID threadid;
154 const char *file; 153 const char *file;
155 int line; 154 int line;
156 const char *info; 155 const char *info;
157 struct app_mem_info_st *next; /* tail of thread's stack */ 156 struct app_mem_info_st *next; /* tail of thread's stack */
158 int references; 157 int references;
159 } APP_INFO; 158} APP_INFO;
160 159
161static void app_info_free(APP_INFO *); 160static void app_info_free(APP_INFO *);
162 161
163DECLARE_LHASH_OF(APP_INFO); 162DECLARE_LHASH_OF(APP_INFO);
164static LHASH_OF(APP_INFO) *amih=NULL; /* hash-table with those 163static LHASH_OF(APP_INFO) *amih = NULL; /* hash - table with those
165 * app_mem_info_st's that are at 164 * app_mem_info_st's that are at
166 * the top of their thread's 165 * the top of their thread's
167 * stack (with `thread' as key); 166 * stack (with `thread' as key);
168 * access requires MALLOC2 167 * access requires MALLOC2
169 * lock */ 168 * lock */
170 169
171typedef struct mem_st 170typedef struct mem_st
172/* memory-block description */ 171/* memory-block description */
173 { 172{
174 void *addr; 173 void *addr;
175 int num; 174 int num;
176 const char *file; 175 const char *file;
@@ -179,47 +178,46 @@ typedef struct mem_st
179 unsigned long order; 178 unsigned long order;
180 time_t time; 179 time_t time;
181 APP_INFO *app_info; 180 APP_INFO *app_info;
182 } MEM; 181} MEM;
183 182
184static long options = /* extra information to be recorded */ 183static long options = /* extra information to be recorded */
185#if defined(CRYPTO_MDEBUG_TIME) || defined(CRYPTO_MDEBUG_ALL) 184#if defined(CRYPTO_MDEBUG_TIME) || defined(CRYPTO_MDEBUG_ALL)
186 V_CRYPTO_MDEBUG_TIME | 185V_CRYPTO_MDEBUG_TIME |
187#endif 186#endif
188#if defined(CRYPTO_MDEBUG_THREAD) || defined(CRYPTO_MDEBUG_ALL) 187#if defined(CRYPTO_MDEBUG_THREAD) || defined(CRYPTO_MDEBUG_ALL)
189 V_CRYPTO_MDEBUG_THREAD | 188V_CRYPTO_MDEBUG_THREAD |
190#endif 189#endif
191 0; 1900;
192 191
193 192
194static unsigned int num_disable = 0; /* num_disable > 0 193static unsigned int num_disable = 0; /* num_disable > 0
195 * iff 194 * iff
196 * mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) 195 * mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE)
197 */ 196 */
198 197
199/* Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this 198/* Valid iff num_disable > 0. CRYPTO_LOCK_MALLOC2 is locked exactly in this
200 * case (by the thread named in disabling_thread). 199 * case (by the thread named in disabling_thread).
201 */ 200 */
202static CRYPTO_THREADID disabling_threadid; 201static CRYPTO_THREADID disabling_threadid;
203 202
204static void app_info_free(APP_INFO *inf) 203static void
205 { 204app_info_free(APP_INFO *inf)
206 if (--(inf->references) <= 0) 205{
207 { 206 if (--(inf->references) <= 0) {
208 if (inf->next != NULL) 207 if (inf->next != NULL) {
209 {
210 app_info_free(inf->next); 208 app_info_free(inf->next);
211 }
212 OPENSSL_free(inf);
213 } 209 }
210 OPENSSL_free(inf);
214 } 211 }
212}
215 213
216int CRYPTO_mem_ctrl(int mode) 214int
217 { 215CRYPTO_mem_ctrl(int mode)
218 int ret=mh_mode; 216{
217 int ret = mh_mode;
219 218
220 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); 219 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
221 switch (mode) 220 switch (mode) {
222 {
223 /* for applications (not to be called while multiple threads 221 /* for applications (not to be called while multiple threads
224 * use the library): */ 222 * use the library): */
225 case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */ 223 case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */
@@ -232,14 +230,13 @@ int CRYPTO_mem_ctrl(int mode)
232 or there'll be a lot of confusion */ 230 or there'll be a lot of confusion */
233 break; 231 break;
234 232
235 /* switch off temporarily (for library-internal use): */ 233 /* switch off temporarily (for library-internal use): */
236 case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */ 234 case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */
237 if (mh_mode & CRYPTO_MEM_CHECK_ON) 235 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
238 {
239 CRYPTO_THREADID cur; 236 CRYPTO_THREADID cur;
240 CRYPTO_THREADID_current(&cur); 237 CRYPTO_THREADID_current(&cur);
241 if (!num_disable || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) /* otherwise we already have the MALLOC2 lock */ 238 if (!num_disable || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) /* otherwise we already have the MALLOC2 lock */
242 { 239 {
243 /* Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed while 240 /* Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed while
244 * we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock if 241 * we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock if
245 * somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot release 242 * somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot release
@@ -257,412 +254,402 @@ int CRYPTO_mem_ctrl(int mode)
257 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC); 254 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
258 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE; 255 mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
259 CRYPTO_THREADID_cpy(&disabling_threadid, &cur); 256 CRYPTO_THREADID_cpy(&disabling_threadid, &cur);
260 }
261 num_disable++;
262 } 257 }
258 num_disable++;
259 }
263 break; 260 break;
264 case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */ 261 case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */
265 if (mh_mode & CRYPTO_MEM_CHECK_ON) 262 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
266 {
267 if (num_disable) /* always true, or something is going wrong */ 263 if (num_disable) /* always true, or something is going wrong */
268 { 264 {
269 num_disable--; 265 num_disable--;
270 if (num_disable == 0) 266 if (num_disable == 0) {
271 {
272 mh_mode|=CRYPTO_MEM_CHECK_ENABLE; 267 mh_mode|=CRYPTO_MEM_CHECK_ENABLE;
273 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); 268 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
274 }
275 } 269 }
276 } 270 }
271 }
277 break; 272 break;
278 273
279 default: 274 default:
280 break; 275 break;
281 }
282 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
283 return(ret);
284 } 276 }
277 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
278 return (ret);
279}
285 280
286int CRYPTO_is_mem_check_on(void) 281int
287 { 282CRYPTO_is_mem_check_on(void)
283{
288 int ret = 0; 284 int ret = 0;
289 285
290 if (mh_mode & CRYPTO_MEM_CHECK_ON) 286 if (mh_mode & CRYPTO_MEM_CHECK_ON) {
291 {
292 CRYPTO_THREADID cur; 287 CRYPTO_THREADID cur;
293 CRYPTO_THREADID_current(&cur); 288 CRYPTO_THREADID_current(&cur);
294 CRYPTO_r_lock(CRYPTO_LOCK_MALLOC); 289 CRYPTO_r_lock(CRYPTO_LOCK_MALLOC);
295 290
296 ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) 291 ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE) ||
297 || CRYPTO_THREADID_cmp(&disabling_threadid, &cur); 292 CRYPTO_THREADID_cmp(&disabling_threadid, &cur);
298 293
299 CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC); 294 CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC);
300 } 295 }
301 return(ret); 296 return (ret);
302 } 297}
303 298
304 299
305void CRYPTO_dbg_set_options(long bits) 300void
306 { 301CRYPTO_dbg_set_options(long bits)
302{
307 options = bits; 303 options = bits;
308 } 304}
309 305
310long CRYPTO_dbg_get_options(void) 306long
311 { 307CRYPTO_dbg_get_options(void)
308{
312 return options; 309 return options;
313 } 310}
314 311
315static int mem_cmp(const MEM *a, const MEM *b) 312static int
316 { 313mem_cmp(const MEM *a, const MEM *b)
314{
317#ifdef _WIN64 315#ifdef _WIN64
318 const char *ap=(const char *)a->addr, 316 const char *ap = (const char *)a->addr,
319 *bp=(const char *)b->addr; 317 *bp = (const char *)b->addr;
320 if (ap==bp) return 0; 318 if (ap == bp)
321 else if (ap>bp) return 1; 319 return 0;
322 else return -1; 320 else if (ap > bp) return 1;
321 else return -1;
323#else 322#else
324 return (const char *)a->addr - (const char *)b->addr; 323 return (const char *)a->addr - (const char *)b->addr;
325#endif 324#endif
326 } 325}
326
327static IMPLEMENT_LHASH_COMP_FN(mem, MEM) 327static IMPLEMENT_LHASH_COMP_FN(mem, MEM)
328 328
329static unsigned long mem_hash(const MEM *a) 329static unsigned long
330 { 330mem_hash(const MEM *a)
331{
331 unsigned long ret; 332 unsigned long ret;
332 333
333 ret=(unsigned long)a->addr; 334 ret = (unsigned long)a->addr;
334 335
335 ret=ret*17851+(ret>>14)*7+(ret>>4)*251; 336 ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
336 return(ret); 337 return (ret);
337 } 338}
338static IMPLEMENT_LHASH_HASH_FN(mem, MEM) 339
340static
341IMPLEMENT_LHASH_HASH_FN(mem, MEM)
339 342
340/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */ 343/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */
341static int app_info_cmp(const void *a_void, const void *b_void) 344static int
342 { 345app_info_cmp(const void *a_void, const void *b_void)
346{
343 return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid, 347 return CRYPTO_THREADID_cmp(&((const APP_INFO *)a_void)->threadid,
344 &((const APP_INFO *)b_void)->threadid); 348 &((const APP_INFO *)b_void)->threadid);
345 } 349}
350
346static IMPLEMENT_LHASH_COMP_FN(app_info, APP_INFO) 351static IMPLEMENT_LHASH_COMP_FN(app_info, APP_INFO)
347 352
348static unsigned long app_info_hash(const APP_INFO *a) 353static unsigned long
349 { 354app_info_hash(const APP_INFO *a)
355{
350 unsigned long ret; 356 unsigned long ret;
351 357
352 ret = CRYPTO_THREADID_hash(&a->threadid); 358 ret = CRYPTO_THREADID_hash(&a->threadid);
353 /* This is left in as a "who am I to question legacy?" measure */ 359 /* This is left in as a "who am I to question legacy?" measure */
354 ret=ret*17851+(ret>>14)*7+(ret>>4)*251; 360 ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
355 return(ret); 361 return (ret);
356 } 362}
363
357static IMPLEMENT_LHASH_HASH_FN(app_info, APP_INFO) 364static IMPLEMENT_LHASH_HASH_FN(app_info, APP_INFO)
358 365
359static APP_INFO *pop_info(void) 366static APP_INFO
360 { 367*pop_info(void)
368{
361 APP_INFO tmp; 369 APP_INFO tmp;
362 APP_INFO *ret = NULL; 370 APP_INFO *ret = NULL;
363 371
364 if (amih != NULL) 372 if (amih != NULL) {
365 {
366 CRYPTO_THREADID_current(&tmp.threadid); 373 CRYPTO_THREADID_current(&tmp.threadid);
367 if ((ret=lh_APP_INFO_delete(amih,&tmp)) != NULL) 374 if ((ret = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
368 { 375 APP_INFO *next = ret->next;
369 APP_INFO *next=ret->next;
370 376
371 if (next != NULL) 377 if (next != NULL) {
372 {
373 next->references++; 378 next->references++;
374 (void)lh_APP_INFO_insert(amih,next); 379 (void)lh_APP_INFO_insert(amih, next);
375 } 380 }
376#ifdef LEVITTE_DEBUG_MEM 381#ifdef LEVITTE_DEBUG_MEM
377 if (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid)) 382 if (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid)) {
378 {
379 fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n", 383 fprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\n",
380 CRYPTO_THREADID_hash(&ret->threadid), 384 CRYPTO_THREADID_hash(&ret->threadid),
381 CRYPTO_THREADID_hash(&tmp.threadid)); 385 CRYPTO_THREADID_hash(&tmp.threadid));
382 abort(); 386 abort();
383 } 387 }
384#endif 388#endif
385 if (--(ret->references) <= 0) 389 if (--(ret->references) <= 0) {
386 {
387 ret->next = NULL; 390 ret->next = NULL;
388 if (next != NULL) 391 if (next != NULL)
389 next->references--; 392 next->references--;
390 OPENSSL_free(ret); 393 OPENSSL_free(ret);
391 }
392 } 394 }
393 } 395 }
394 return(ret);
395 } 396 }
397 return (ret);
398}
396 399
397int CRYPTO_push_info_(const char *info, const char *file, int line) 400int
398 { 401CRYPTO_push_info_(const char *info, const char *file, int line)
402{
399 APP_INFO *ami, *amim; 403 APP_INFO *ami, *amim;
400 int ret=0; 404 int ret = 0;
401 405
402 if (is_MemCheck_on()) 406 if (is_MemCheck_on()) {
403 {
404 MemCheck_off(); /* obtain MALLOC2 lock */ 407 MemCheck_off(); /* obtain MALLOC2 lock */
405 408
406 if ((ami = (APP_INFO *)OPENSSL_malloc(sizeof(APP_INFO))) == NULL) 409 if ((ami = (APP_INFO *)OPENSSL_malloc(sizeof(APP_INFO))) == NULL) {
407 { 410 ret = 0;
408 ret=0;
409 goto err; 411 goto err;
410 } 412 }
411 if (amih == NULL) 413 if (amih == NULL) {
412 { 414 if ((amih = lh_APP_INFO_new()) == NULL) {
413 if ((amih=lh_APP_INFO_new()) == NULL)
414 {
415 OPENSSL_free(ami); 415 OPENSSL_free(ami);
416 ret=0; 416 ret = 0;
417 goto err; 417 goto err;
418 }
419 } 418 }
419 }
420 420
421 CRYPTO_THREADID_current(&ami->threadid); 421 CRYPTO_THREADID_current(&ami->threadid);
422 ami->file=file; 422 ami->file = file;
423 ami->line=line; 423 ami->line = line;
424 ami->info=info; 424 ami->info = info;
425 ami->references=1; 425 ami->references = 1;
426 ami->next=NULL; 426 ami->next = NULL;
427 427
428 if ((amim=lh_APP_INFO_insert(amih,ami)) != NULL) 428 if ((amim = lh_APP_INFO_insert(amih, ami)) != NULL) {
429 {
430#ifdef LEVITTE_DEBUG_MEM 429#ifdef LEVITTE_DEBUG_MEM
431 if (CRYPTO_THREADID_cmp(&ami->threadid, &amim->threadid)) 430 if (CRYPTO_THREADID_cmp(&ami->threadid, &amim->threadid)) {
432 {
433 fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n", 431 fprintf(stderr, "CRYPTO_push_info(): previous info has other thread ID (%lu) than the current thread (%lu)!!!!\n",
434 CRYPTO_THREADID_hash(&amim->threadid), 432 CRYPTO_THREADID_hash(&amim->threadid),
435 CRYPTO_THREADID_hash(&ami->threadid)); 433 CRYPTO_THREADID_hash(&ami->threadid));
436 abort(); 434 abort();
437 }
438#endif
439 ami->next=amim;
440 } 435 }
441 err: 436#endif
442 MemCheck_on(); /* release MALLOC2 lock */ 437 ami->next = amim;
443 } 438 }
444 439 err:
445 return(ret); 440 MemCheck_on(); /* release MALLOC2 lock */
446 } 441 }
447 442
448int CRYPTO_pop_info(void) 443 return (ret);
449 { 444}
450 int ret=0; 445
446int
447CRYPTO_pop_info(void)
448{
449 int ret = 0;
451 450
452 if (is_MemCheck_on()) /* _must_ be true, or something went severely wrong */ 451 if (is_MemCheck_on()) /* _must_ be true, or something went severely wrong */
453 { 452 {
454 MemCheck_off(); /* obtain MALLOC2 lock */ 453 MemCheck_off(); /* obtain MALLOC2 lock */
455 454
456 ret=(pop_info() != NULL); 455 ret = (pop_info() != NULL);
457 456
458 MemCheck_on(); /* release MALLOC2 lock */ 457 MemCheck_on(); /* release MALLOC2 lock */
459 }
460 return(ret);
461 } 458 }
459 return (ret);
460}
462 461
463int CRYPTO_remove_all_info(void) 462int
464 { 463CRYPTO_remove_all_info(void)
465 int ret=0; 464{
465 int ret = 0;
466 466
467 if (is_MemCheck_on()) /* _must_ be true */ 467 if (is_MemCheck_on()) /* _must_ be true */
468 { 468 {
469 MemCheck_off(); /* obtain MALLOC2 lock */ 469 MemCheck_off(); /* obtain MALLOC2 lock */
470 470
471 while(pop_info() != NULL) 471 while (pop_info() != NULL)
472 ret++; 472 ret++;
473 473
474 MemCheck_on(); /* release MALLOC2 lock */ 474 MemCheck_on(); /* release MALLOC2 lock */
475 }
476 return(ret);
477 } 475 }
476 return (ret);
477}
478 478
479 479
480static unsigned long break_order_num=0; 480static unsigned long break_order_num = 0;
481void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line, 481void
482 int before_p) 482CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
483 { 483 int before_p)
484 MEM *m,*mm; 484{
485 APP_INFO tmp,*amim; 485 MEM *m, *mm;
486 APP_INFO tmp, *amim;
486 487
487 switch(before_p & 127) 488 switch (before_p & 127) {
488 {
489 case 0: 489 case 0:
490 break; 490 break;
491 case 1: 491 case 1:
492 if (addr == NULL) 492 if (addr == NULL)
493 break; 493 break;
494 494
495 if (is_MemCheck_on()) 495 if (is_MemCheck_on()) {
496 {
497 MemCheck_off(); /* make sure we hold MALLOC2 lock */ 496 MemCheck_off(); /* make sure we hold MALLOC2 lock */
498 if ((m=(MEM *)OPENSSL_malloc(sizeof(MEM))) == NULL) 497 if ((m = (MEM *)OPENSSL_malloc(sizeof(MEM))) == NULL) {
499 {
500 OPENSSL_free(addr); 498 OPENSSL_free(addr);
501 MemCheck_on(); /* release MALLOC2 lock 499 MemCheck_on(); /* release MALLOC2 lock
502 * if num_disabled drops to 0 */ 500 * if num_disabled drops to 0 */
503 return; 501 return;
504 } 502 }
505 if (mh == NULL) 503 if (mh == NULL) {
506 { 504 if ((mh = lh_MEM_new()) == NULL) {
507 if ((mh=lh_MEM_new()) == NULL)
508 {
509 OPENSSL_free(addr); 505 OPENSSL_free(addr);
510 OPENSSL_free(m); 506 OPENSSL_free(m);
511 addr=NULL; 507 addr = NULL;
512 goto err; 508 goto err;
513 }
514 } 509 }
510 }
515 511
516 m->addr=addr; 512 m->addr = addr;
517 m->file=file; 513 m->file = file;
518 m->line=line; 514 m->line = line;
519 m->num=num; 515 m->num = num;
520 if (options & V_CRYPTO_MDEBUG_THREAD) 516 if (options & V_CRYPTO_MDEBUG_THREAD)
521 CRYPTO_THREADID_current(&m->threadid); 517 CRYPTO_THREADID_current(&m->threadid);
522 else 518 else
523 memset(&m->threadid, 0, sizeof(m->threadid)); 519 memset(&m->threadid, 0, sizeof(m->threadid));
524 520
525 if (order == break_order_num) 521 if (order == break_order_num) {
526 {
527 /* BREAK HERE */ 522 /* BREAK HERE */
528 m->order=order; 523 m->order = order;
529 } 524 }
530 m->order=order++; 525 m->order = order++;
531#ifdef LEVITTE_DEBUG_MEM 526#ifdef LEVITTE_DEBUG_MEM
532 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] %c 0x%p (%d)\n", 527 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] %c 0x%p (%d)\n",
533 m->order, 528 m->order,
534 (before_p & 128) ? '*' : '+', 529 (before_p & 128) ? '*' : '+',
535 m->addr, m->num); 530 m->addr, m->num);
536#endif 531#endif
537 if (options & V_CRYPTO_MDEBUG_TIME) 532 if (options & V_CRYPTO_MDEBUG_TIME)
538 m->time=time(NULL); 533 m->time = time(NULL);
539 else 534 else
540 m->time=0; 535 m->time = 0;
541 536
542 CRYPTO_THREADID_current(&tmp.threadid); 537 CRYPTO_THREADID_current(&tmp.threadid);
543 m->app_info=NULL; 538 m->app_info = NULL;
544 if (amih != NULL 539 if (amih != NULL &&
545 && (amim=lh_APP_INFO_retrieve(amih,&tmp)) != NULL) 540 (amim = lh_APP_INFO_retrieve(amih, &tmp)) != NULL) {
546 {
547 m->app_info = amim; 541 m->app_info = amim;
548 amim->references++; 542 amim->references++;
549 } 543 }
550 544
551 if ((mm=lh_MEM_insert(mh, m)) != NULL) 545 if ((mm = lh_MEM_insert(mh, m)) != NULL) {
552 {
553 /* Not good, but don't sweat it */ 546 /* Not good, but don't sweat it */
554 if (mm->app_info != NULL) 547 if (mm->app_info != NULL) {
555 {
556 mm->app_info->references--; 548 mm->app_info->references--;
557 }
558 OPENSSL_free(mm);
559 } 549 }
560 err: 550 OPENSSL_free(mm);
551 }
552err:
561 MemCheck_on(); /* release MALLOC2 lock 553 MemCheck_on(); /* release MALLOC2 lock
562 * if num_disabled drops to 0 */ 554 * if num_disabled drops to 0 */
563 }
564 break;
565 } 555 }
566 return; 556 break;
567 } 557 }
558 return;
559}
568 560
569void CRYPTO_dbg_free(void *addr, int before_p) 561void
570 { 562CRYPTO_dbg_free(void *addr, int before_p)
571 MEM m,*mp; 563{
564 MEM m, *mp;
572 565
573 switch(before_p) 566 switch (before_p) {
574 {
575 case 0: 567 case 0:
576 if (addr == NULL) 568 if (addr == NULL)
577 break; 569 break;
578 570
579 if (is_MemCheck_on() && (mh != NULL)) 571 if (is_MemCheck_on() && (mh != NULL)) {
580 {
581 MemCheck_off(); /* make sure we hold MALLOC2 lock */ 572 MemCheck_off(); /* make sure we hold MALLOC2 lock */
582 573
583 m.addr=addr; 574 m.addr = addr;
584 mp=lh_MEM_delete(mh,&m); 575 mp = lh_MEM_delete(mh, &m);
585 if (mp != NULL) 576 if (mp != NULL) {
586 {
587#ifdef LEVITTE_DEBUG_MEM 577#ifdef LEVITTE_DEBUG_MEM
588 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] - 0x%p (%d)\n", 578 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] - 0x%p (%d)\n",
589 mp->order, mp->addr, mp->num); 579 mp->order, mp->addr, mp->num);
590#endif 580#endif
591 if (mp->app_info != NULL) 581 if (mp->app_info != NULL)
592 app_info_free(mp->app_info); 582 app_info_free(mp->app_info);
593 OPENSSL_free(mp); 583 OPENSSL_free(mp);
594 } 584 }
595 585
596 MemCheck_on(); /* release MALLOC2 lock 586 MemCheck_on(); /* release MALLOC2 lock
597 * if num_disabled drops to 0 */ 587 * if num_disabled drops to 0 */
598 } 588 }
599 break; 589 break;
600 case 1: 590 case 1:
601 break; 591 break;
602 }
603 } 592 }
593}
604 594
605void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, 595void
606 const char *file, int line, int before_p) 596CRYPTO_dbg_realloc(void *addr1, void *addr2, int num,
607 { 597 const char *file, int line, int before_p)
608 MEM m,*mp; 598{
599 MEM m, *mp;
609 600
610#ifdef LEVITTE_DEBUG_MEM 601#ifdef LEVITTE_DEBUG_MEM
611 fprintf(stderr, "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n", 602 fprintf(stderr, "LEVITTE_DEBUG_MEM: --> CRYPTO_dbg_malloc(addr1 = %p, addr2 = %p, num = %d, file = \"%s\", line = %d, before_p = %d)\n",
612 addr1, addr2, num, file, line, before_p); 603 addr1, addr2, num, file, line, before_p);
613#endif 604#endif
614 605
615 switch(before_p) 606 switch (before_p) {
616 {
617 case 0: 607 case 0:
618 break; 608 break;
619 case 1: 609 case 1:
620 if (addr2 == NULL) 610 if (addr2 == NULL)
621 break; 611 break;
622 612
623 if (addr1 == NULL) 613 if (addr1 == NULL) {
624 {
625 CRYPTO_dbg_malloc(addr2, num, file, line, 128 | before_p); 614 CRYPTO_dbg_malloc(addr2, num, file, line, 128 | before_p);
626 break; 615 break;
627 } 616 }
628 617
629 if (is_MemCheck_on()) 618 if (is_MemCheck_on()) {
630 {
631 MemCheck_off(); /* make sure we hold MALLOC2 lock */ 619 MemCheck_off(); /* make sure we hold MALLOC2 lock */
632 620
633 m.addr=addr1; 621 m.addr = addr1;
634 mp=lh_MEM_delete(mh,&m); 622 mp = lh_MEM_delete(mh, &m);
635 if (mp != NULL) 623 if (mp != NULL) {
636 {
637#ifdef LEVITTE_DEBUG_MEM 624#ifdef LEVITTE_DEBUG_MEM
638 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] * 0x%p (%d) -> 0x%p (%d)\n", 625 fprintf(stderr, "LEVITTE_DEBUG_MEM: [%5ld] * 0x%p (%d) -> 0x%p (%d)\n",
639 mp->order, 626 mp->order,
640 mp->addr, mp->num, 627 mp->addr, mp->num,
641 addr2, num); 628 addr2, num);
642#endif 629#endif
643 mp->addr=addr2; 630 mp->addr = addr2;
644 mp->num=num; 631 mp->num = num;
645 (void)lh_MEM_insert(mh,mp); 632 (void)lh_MEM_insert(mh, mp);
646 } 633 }
647 634
648 MemCheck_on(); /* release MALLOC2 lock 635 MemCheck_on(); /* release MALLOC2 lock
649 * if num_disabled drops to 0 */ 636 * if num_disabled drops to 0 */
650 }
651 break;
652 } 637 }
653 return; 638 break;
654 } 639 }
640 return;
641}
655 642
656 643
657typedef struct mem_leak_st 644typedef struct mem_leak_st {
658 {
659 BIO *bio; 645 BIO *bio;
660 int chunks; 646 int chunks;
661 long bytes; 647 long bytes;
662 } MEM_LEAK; 648} MEM_LEAK;
663 649
664static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l) 650static void
665 { 651print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
652{
666 char buf[1024]; 653 char buf[1024];
667 char *bufp = buf; 654 char *bufp = buf;
668 APP_INFO *amip; 655 APP_INFO *amip;
@@ -672,89 +659,83 @@ static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
672 659
673#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf)) 660#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
674 661
675 if(m->addr == (char *)l->bio) 662 if (m->addr == (char *)l->bio)
676 return; 663 return;
677 664
678 if (options & V_CRYPTO_MDEBUG_TIME) 665 if (options & V_CRYPTO_MDEBUG_TIME) {
679 {
680 lcl = localtime(&m->time); 666 lcl = localtime(&m->time);
681 667
682 BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ", 668 BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ",
683 lcl->tm_hour,lcl->tm_min,lcl->tm_sec); 669 lcl->tm_hour, lcl->tm_min, lcl->tm_sec);
684 bufp += strlen(bufp); 670 bufp += strlen(bufp);
685 } 671 }
686 672
687 BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ", 673 BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",
688 m->order,m->file,m->line); 674 m->order, m->file, m->line);
689 bufp += strlen(bufp); 675 bufp += strlen(bufp);
690 676
691 if (options & V_CRYPTO_MDEBUG_THREAD) 677 if (options & V_CRYPTO_MDEBUG_THREAD) {
692 {
693 BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ", 678 BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu, ",
694 CRYPTO_THREADID_hash(&m->threadid)); 679 CRYPTO_THREADID_hash(&m->threadid));
695 bufp += strlen(bufp); 680 bufp += strlen(bufp);
696 } 681 }
697 682
698 BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%08lX\n", 683 BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%08lX\n",
699 m->num,(unsigned long)m->addr); 684 m->num,(unsigned long)m->addr);
700 bufp += strlen(bufp); 685 bufp += strlen(bufp);
701 686
702 BIO_puts(l->bio,buf); 687 BIO_puts(l->bio, buf);
703 688
704 l->chunks++; 689 l->chunks++;
705 l->bytes+=m->num; 690 l->bytes += m->num;
706 691
707 amip=m->app_info; 692 amip = m->app_info;
708 ami_cnt=0; 693 ami_cnt = 0;
709 if (!amip) 694 if (!amip)
710 return; 695 return;
711 CRYPTO_THREADID_cpy(&ti, &amip->threadid); 696 CRYPTO_THREADID_cpy(&ti, &amip->threadid);
712 697
713 do 698 do {
714 {
715 int buf_len; 699 int buf_len;
716 int info_len; 700 int info_len;
717 701
718 ami_cnt++; 702 ami_cnt++;
719 memset(buf,'>',ami_cnt); 703 memset(buf, '>', ami_cnt);
720 BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt, 704 BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,
721 " thread=%lu, file=%s, line=%d, info=\"", 705 " thread=%lu, file=%s, line=%d, info=\"",
722 CRYPTO_THREADID_hash(&amip->threadid), amip->file, 706 CRYPTO_THREADID_hash(&amip->threadid), amip->file,
723 amip->line); 707 amip->line);
724 buf_len=strlen(buf); 708 buf_len = strlen(buf);
725 info_len=strlen(amip->info); 709 info_len = strlen(amip->info);
726 if (128 - buf_len - 3 < info_len) 710 if (128 - buf_len - 3 < info_len) {
727 {
728 memcpy(buf + buf_len, amip->info, 128 - buf_len - 3); 711 memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
729 buf_len = 128 - 3; 712 buf_len = 128 - 3;
730 } 713 } else {
731 else
732 {
733 BUF_strlcpy(buf + buf_len, amip->info, 714 BUF_strlcpy(buf + buf_len, amip->info,
734 sizeof buf - buf_len); 715 sizeof buf - buf_len);
735 buf_len = strlen(buf); 716 buf_len = strlen(buf);
736 } 717 }
737 BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n"); 718 BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n");
738 719
739 BIO_puts(l->bio,buf); 720 BIO_puts(l->bio, buf);
740 721
741 amip = amip->next; 722 amip = amip->next;
742 } 723 } while (amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti));
743 while(amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti));
744 724
745#ifdef LEVITTE_DEBUG_MEM 725#ifdef LEVITTE_DEBUG_MEM
746 if (amip) 726 if (amip) {
747 {
748 fprintf(stderr, "Thread switch detected in backtrace!!!!\n"); 727 fprintf(stderr, "Thread switch detected in backtrace!!!!\n");
749 abort(); 728 abort();
750 }
751#endif
752 } 729 }
730#endif
731}
753 732
754static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK) 733static
734IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK)
755 735
756void CRYPTO_mem_leaks(BIO *b) 736void
757 { 737CRYPTO_mem_leaks(BIO *b)
738{
758 MEM_LEAK ml; 739 MEM_LEAK ml;
759 740
760 if (mh == NULL && amih == NULL) 741 if (mh == NULL && amih == NULL)
@@ -762,22 +743,19 @@ void CRYPTO_mem_leaks(BIO *b)
762 743
763 MemCheck_off(); /* obtain MALLOC2 lock */ 744 MemCheck_off(); /* obtain MALLOC2 lock */
764 745
765 ml.bio=b; 746 ml.bio = b;
766 ml.bytes=0; 747 ml.bytes = 0;
767 ml.chunks=0; 748 ml.chunks = 0;
768 if (mh != NULL) 749 if (mh != NULL)
769 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), MEM_LEAK, 750 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak),
770 &ml); 751 MEM_LEAK, &ml);
771 if (ml.chunks != 0) 752 if (ml.chunks != 0) {
772 { 753 BIO_printf(b, "%ld bytes leaked in %d chunks\n",
773 BIO_printf(b,"%ld bytes leaked in %d chunks\n", 754 ml.bytes, ml.chunks);
774 ml.bytes,ml.chunks);
775#ifdef CRYPTO_MDEBUG_ABORT 755#ifdef CRYPTO_MDEBUG_ABORT
776 abort(); 756 abort();
777#endif 757#endif
778 } 758 } else {
779 else
780 {
781 /* Make sure that, if we found no leaks, memory-leak debugging itself 759 /* Make sure that, if we found no leaks, memory-leak debugging itself
782 * does not introduce memory leaks (which might irritate 760 * does not introduce memory leaks (which might irritate
783 * external debugging tools). 761 * external debugging tools).
@@ -802,43 +780,43 @@ void CRYPTO_mem_leaks(BIO *b)
802 old_mh_mode = mh_mode; 780 old_mh_mode = mh_mode;
803 mh_mode = CRYPTO_MEM_CHECK_OFF; 781 mh_mode = CRYPTO_MEM_CHECK_OFF;
804 782
805 if (mh != NULL) 783 if (mh != NULL) {
806 {
807 lh_MEM_free(mh); 784 lh_MEM_free(mh);
808 mh = NULL; 785 mh = NULL;
809 } 786 }
810 if (amih != NULL) 787 if (amih != NULL) {
811 { 788 if (lh_APP_INFO_num_items(amih) == 0) {
812 if (lh_APP_INFO_num_items(amih) == 0)
813 {
814 lh_APP_INFO_free(amih); 789 lh_APP_INFO_free(amih);
815 amih = NULL; 790 amih = NULL;
816 }
817 } 791 }
792 }
818 793
819 mh_mode = old_mh_mode; 794 mh_mode = old_mh_mode;
820 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); 795 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
821 }
822 MemCheck_on(); /* release MALLOC2 lock */
823 } 796 }
797 MemCheck_on(); /* release MALLOC2 lock */
798}
824 799
825#ifndef OPENSSL_NO_FP_API 800#ifndef OPENSSL_NO_FP_API
826void CRYPTO_mem_leaks_fp(FILE *fp) 801void
827 { 802CRYPTO_mem_leaks_fp(FILE *fp)
803{
828 BIO *b; 804 BIO *b;
829 805
830 if (mh == NULL) return; 806 if (mh == NULL)
807 return;
831 /* Need to turn off memory checking when allocated BIOs ... especially 808 /* Need to turn off memory checking when allocated BIOs ... especially
832 * as we're creating them at a time when we're trying to check we've not 809 * as we're creating them at a time when we're trying to check we've not
833 * left anything un-free()'d!! */ 810 * left anything un-free()'d!! */
834 MemCheck_off(); 811 MemCheck_off();
835 b = BIO_new(BIO_s_file()); 812 b = BIO_new(BIO_s_file());
836 MemCheck_on(); 813 MemCheck_on();
837 if(!b) return; 814 if (!b)
838 BIO_set_fp(b,fp,BIO_NOCLOSE); 815 return;
816 BIO_set_fp(b, fp, BIO_NOCLOSE);
839 CRYPTO_mem_leaks(b); 817 CRYPTO_mem_leaks(b);
840 BIO_free(b); 818 BIO_free(b);
841 } 819}
842#endif 820#endif
843 821
844 822
@@ -857,18 +835,21 @@ void CRYPTO_mem_leaks_fp(FILE *fp)
857 835
858typedef CRYPTO_MEM_LEAK_CB *PCRYPTO_MEM_LEAK_CB; 836typedef CRYPTO_MEM_LEAK_CB *PCRYPTO_MEM_LEAK_CB;
859 837
860static void cb_leak_doall_arg(const MEM *m, PCRYPTO_MEM_LEAK_CB *cb) 838static void
861 { 839cb_leak_doall_arg(const MEM *m, PCRYPTO_MEM_LEAK_CB *cb)
862 (*cb)(m->order,m->file,m->line,m->num,m->addr); 840{
863 } 841 (*cb)(m->order, m->file, m->line, m->num, m->addr);
842}
864 843
865static IMPLEMENT_LHASH_DOALL_ARG_FN(cb_leak, const MEM, PCRYPTO_MEM_LEAK_CB) 844static IMPLEMENT_LHASH_DOALL_ARG_FN(cb_leak, const MEM, PCRYPTO_MEM_LEAK_CB)
866 845
867void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb) 846void
868 { 847CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb)
869 if (mh == NULL) return; 848{
849 if (mh == NULL)
850 return;
870 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2); 851 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
871 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), PCRYPTO_MEM_LEAK_CB, 852 lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), PCRYPTO_MEM_LEAK_CB,
872 &cb); 853 &cb);
873 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); 854 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
874 } 855}
diff --git a/src/lib/libssl/src/crypto/o_dir_test.c b/src/lib/libssl/src/crypto/o_dir_test.c
index 238717c110..2973458a94 100644
--- a/src/lib/libssl/src/crypto/o_dir_test.c
+++ b/src/lib/libssl/src/crypto/o_dir_test.c
@@ -43,26 +43,24 @@
43#error "No supported platform defined!" 43#error "No supported platform defined!"
44#endif 44#endif
45 45
46int main() 46int
47main()
47{ 48{
48 OPENSSL_DIR_CTX *ctx = NULL; 49 OPENSSL_DIR_CTX *ctx = NULL;
49 const char *result; 50 const char *result;
50 51
51 while((result = OPENSSL_DIR_read(&ctx, CURRDIR)) != NULL) 52 while ((result = OPENSSL_DIR_read(&ctx, CURRDIR)) != NULL) {
52 { 53 printf("%s\n", result);
53 printf("%s\n", result); 54 }
54 }
55 55
56 if (errno) 56 if (errno) {
57 { 57 perror("test_dir");
58 perror("test_dir"); 58 exit(1);
59 exit(1); 59 }
60 }
61 60
62 if (!OPENSSL_DIR_end(&ctx)) 61 if (!OPENSSL_DIR_end(&ctx)) {
63 { 62 perror("test_dir");
64 perror("test_dir"); 63 exit(2);
65 exit(2); 64 }
66 } 65 exit(0);
67 exit(0);
68} 66}
diff --git a/src/lib/libssl/src/crypto/o_fips.c b/src/lib/libssl/src/crypto/o_fips.c
index f6d1b21855..9c185cfb18 100644
--- a/src/lib/libssl/src/crypto/o_fips.c
+++ b/src/lib/libssl/src/crypto/o_fips.c
@@ -62,18 +62,20 @@
62#include <openssl/rand.h> 62#include <openssl/rand.h>
63#endif 63#endif
64 64
65int FIPS_mode(void) 65int
66 { 66FIPS_mode(void)
67{
67 OPENSSL_init(); 68 OPENSSL_init();
68#ifdef OPENSSL_FIPS 69#ifdef OPENSSL_FIPS
69 return FIPS_module_mode(); 70 return FIPS_module_mode();
70#else 71#else
71 return 0; 72 return 0;
72#endif 73#endif
73 } 74}
74 75
75int FIPS_mode_set(int r) 76int
76 { 77FIPS_mode_set(int r)
78{
77 OPENSSL_init(); 79 OPENSSL_init();
78#ifdef OPENSSL_FIPS 80#ifdef OPENSSL_FIPS
79#ifndef FIPS_AUTH_USER_PASS 81#ifndef FIPS_AUTH_USER_PASS
@@ -92,5 +94,4 @@ int FIPS_mode_set(int r)
92 CRYPTOerr(CRYPTO_F_FIPS_MODE_SET, CRYPTO_R_FIPS_MODE_NOT_SUPPORTED); 94 CRYPTOerr(CRYPTO_F_FIPS_MODE_SET, CRYPTO_R_FIPS_MODE_NOT_SUPPORTED);
93 return 0; 95 return 0;
94#endif 96#endif
95 } 97}
96
diff --git a/src/lib/libssl/src/crypto/o_init.c b/src/lib/libssl/src/crypto/o_init.c
index db4cdc443b..07c8e0d694 100644
--- a/src/lib/libssl/src/crypto/o_init.c
+++ b/src/lib/libssl/src/crypto/o_init.c
@@ -63,8 +63,9 @@
63 * Currently only sets FIPS callbacks 63 * Currently only sets FIPS callbacks
64 */ 64 */
65 65
66void OPENSSL_init(void) 66void
67 { 67OPENSSL_init(void)
68{
68 static int done = 0; 69 static int done = 0;
69 if (done) 70 if (done)
70 return; 71 return;
@@ -78,5 +79,4 @@ void OPENSSL_init(void)
78#if 0 79#if 0
79 fprintf(stderr, "Called OPENSSL_init\n"); 80 fprintf(stderr, "Called OPENSSL_init\n");
80#endif 81#endif
81 } 82}
82
diff --git a/src/lib/libssl/src/crypto/o_str.c b/src/lib/libssl/src/crypto/o_str.c
index 56104a6c34..c3ab1f8093 100644
--- a/src/lib/libssl/src/crypto/o_str.c
+++ b/src/lib/libssl/src/crypto/o_str.c
@@ -66,17 +66,18 @@
66# include <strings.h> 66# include <strings.h>
67#endif 67#endif
68 68
69int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n) 69int
70 { 70OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
71{
71#if defined(OPENSSL_IMPLEMENTS_strncasecmp) 72#if defined(OPENSSL_IMPLEMENTS_strncasecmp)
72 while (*str1 && *str2 && n) 73 while (*str1 && *str2 && n) {
73 {
74 int res = toupper(*str1) - toupper(*str2); 74 int res = toupper(*str1) - toupper(*str2);
75 if (res) return res < 0 ? -1 : 1; 75 if (res)
76 return res < 0 ? -1 : 1;
76 str1++; 77 str1++;
77 str2++; 78 str2++;
78 n--; 79 n--;
79 } 80 }
80 if (n == 0) 81 if (n == 0)
81 return 0; 82 return 0;
82 if (*str1) 83 if (*str1)
@@ -90,22 +91,25 @@ int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n)
90 * defined as well. */ 91 * defined as well. */
91 return strncasecmp(str1, str2, n); 92 return strncasecmp(str1, str2, n);
92#endif 93#endif
93 } 94}
94int OPENSSL_strcasecmp(const char *str1, const char *str2) 95
95 { 96int
97OPENSSL_strcasecmp(const char *str1, const char *str2)
98{
96#if defined(OPENSSL_IMPLEMENTS_strncasecmp) 99#if defined(OPENSSL_IMPLEMENTS_strncasecmp)
97 return OPENSSL_strncasecmp(str1, str2, (size_t)-1); 100 return OPENSSL_strncasecmp(str1, str2, (size_t) - 1);
98#else 101#else
99 return strcasecmp(str1, str2); 102 return strcasecmp(str1, str2);
100#endif 103#endif
101 } 104}
102 105
103int OPENSSL_memcmp(const void *v1,const void *v2,size_t n) 106int
104 { 107OPENSSL_memcmp(const void *v1, const void *v2, size_t n)
105 const unsigned char *c1=v1,*c2=v2; 108{
106 int ret=0; 109 const unsigned char *c1 = v1, *c2 = v2;
110 int ret = 0;
107 111
108 while(n && (ret=*c1-*c2)==0) n--,c1++,c2++; 112 while (n && (ret = *c1 - *c2) == 0) n--, c1++, c2++;
109 113
110 return ret; 114 return ret;
111 } 115}
diff --git a/src/lib/libssl/src/crypto/o_str.h b/src/lib/libssl/src/crypto/o_str.h
index dfc98494c6..e2a3fde299 100644
--- a/src/lib/libssl/src/crypto/o_str.h
+++ b/src/lib/libssl/src/crypto/o_str.h
@@ -63,6 +63,6 @@
63 63
64int OPENSSL_strcasecmp(const char *str1, const char *str2); 64int OPENSSL_strcasecmp(const char *str1, const char *str2);
65int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n); 65int OPENSSL_strncasecmp(const char *str1, const char *str2, size_t n);
66int OPENSSL_memcmp(const void *p1,const void *p2,size_t n); 66int OPENSSL_memcmp(const void *p1, const void *p2, size_t n);
67 67
68#endif 68#endif
diff --git a/src/lib/libssl/src/crypto/o_time.c b/src/lib/libssl/src/crypto/o_time.c
index 5fb5d4e6d7..44f7ba3b8c 100644
--- a/src/lib/libssl/src/crypto/o_time.c
+++ b/src/lib/libssl/src/crypto/o_time.c
@@ -63,14 +63,14 @@
63#include <string.h> 63#include <string.h>
64#include "o_time.h" 64#include "o_time.h"
65 65
66struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result) 66struct tm
67 { 67*OPENSSL_gmtime(const time_t *timer, struct tm *result) {
68 struct tm *ts = NULL; 68 struct tm *ts = NULL;
69 69
70#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS) 70#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
71 /* should return &data, but doesn't on some systems, 71 /* should return &data, but doesn't on some systems,
72 so we don't even look at the return value */ 72 so we don't even look at the return value */
73 gmtime_r(timer,result); 73 gmtime_r(timer, result);
74 ts = result; 74 ts = result;
75#else 75#else
76 ts = gmtime(timer); 76 ts = gmtime(timer);
@@ -81,7 +81,7 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
81 ts = result; 81 ts = result;
82#endif 82#endif
83 return ts; 83 return ts;
84 } 84}
85 85
86/* Take a tm structure and add an offset to it. This avoids any OS issues 86/* Take a tm structure and add an offset to it. This avoids any OS issues
87 * with restricted date types and overflows which cause the year 2038 87 * with restricted date types and overflows which cause the year 2038
@@ -93,29 +93,27 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
93static long date_to_julian(int y, int m, int d); 93static long date_to_julian(int y, int m, int d);
94static void julian_to_date(long jd, int *y, int *m, int *d); 94static void julian_to_date(long jd, int *y, int *m, int *d);
95 95
96int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) 96int
97 { 97OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
98{
98 int offset_hms, offset_day; 99 int offset_hms, offset_day;
99 long time_jd; 100 long time_jd;
100 int time_year, time_month, time_day; 101 int time_year, time_month, time_day;
101 /* split offset into days and day seconds */ 102 /* split offset into days and day seconds */
102 offset_day = offset_sec / SECS_PER_DAY; 103 offset_day = offset_sec / SECS_PER_DAY;
103 /* Avoid sign issues with % operator */ 104 /* Avoid sign issues with % operator */
104 offset_hms = offset_sec - (offset_day * SECS_PER_DAY); 105 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
105 offset_day += off_day; 106 offset_day += off_day;
106 /* Add current time seconds to offset */ 107 /* Add current time seconds to offset */
107 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; 108 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
108 /* Adjust day seconds if overflow */ 109 /* Adjust day seconds if overflow */
109 if (offset_hms >= SECS_PER_DAY) 110 if (offset_hms >= SECS_PER_DAY) {
110 {
111 offset_day++; 111 offset_day++;
112 offset_hms -= SECS_PER_DAY; 112 offset_hms -= SECS_PER_DAY;
113 } 113 } else if (offset_hms < 0) {
114 else if (offset_hms < 0)
115 {
116 offset_day--; 114 offset_day--;
117 offset_hms += SECS_PER_DAY; 115 offset_hms += SECS_PER_DAY;
118 } 116 }
119 117
120 /* Convert date of time structure into a Julian day number. 118 /* Convert date of time structure into a Julian day number.
121 */ 119 */
@@ -150,22 +148,24 @@ int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
150 tm->tm_sec = offset_hms % 60; 148 tm->tm_sec = offset_hms % 60;
151 149
152 return 1; 150 return 1;
153 151
154} 152}
155 153
156/* Convert date to and from julian day 154/* Convert date to and from julian day
157 * Uses Fliegel & Van Flandern algorithm 155 * Uses Fliegel & Van Flandern algorithm
158 */ 156 */
159static long date_to_julian(int y, int m, int d) 157static long
158date_to_julian(int y, int m, int d)
160{ 159{
161 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 + 160 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
162 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - 161 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
163 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + 162 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 +
164 d - 32075; 163 d - 32075;
165} 164}
166 165
167static void julian_to_date(long jd, int *y, int *m, int *d) 166static void
168 { 167julian_to_date(long jd, int *y, int *m, int *d)
168{
169 long L = jd + 68569; 169 long L = jd + 68569;
170 long n = (4 * L) / 146097; 170 long n = (4 * L) / 146097;
171 long i, j; 171 long i, j;
@@ -178,7 +178,7 @@ static void julian_to_date(long jd, int *y, int *m, int *d)
178 L = j / 11; 178 L = j / 11;
179 *m = j + 2 - (12 * L); 179 *m = j + 2 - (12 * L);
180 *y = 100 * (n - 49) + i + L; 180 *y = 100 * (n - 49) + i + L;
181 } 181}
182 182
183#ifdef OPENSSL_TIME_TEST 183#ifdef OPENSSL_TIME_TEST
184 184
@@ -189,20 +189,21 @@ static void julian_to_date(long jd, int *y, int *m, int *d)
189 * trigger the very errors the routines fix. 189 * trigger the very errors the routines fix.
190 */ 190 */
191 191
192int main(int argc, char **argv) 192int
193 { 193main(int argc, char **argv)
194{
194 long offset; 195 long offset;
195 for (offset = 0; offset < 1000000; offset++) 196 for (offset = 0; offset < 1000000; offset++) {
196 {
197 check_time(offset); 197 check_time(offset);
198 check_time(-offset); 198 check_time(-offset);
199 check_time(offset * 1000); 199 check_time(offset * 1000);
200 check_time(-offset * 1000); 200 check_time(-offset * 1000);
201 }
202 } 201 }
202}
203 203
204int check_time(long offset) 204int
205 { 205check_time(long offset)
206{
206 struct tm tm1, tm2; 207 struct tm tm1, tm2;
207 time_t t1, t2; 208 time_t t1, t2;
208 time(&t1); 209 time(&t1);
@@ -211,20 +212,20 @@ int check_time(long offset)
211 OPENSSL_gmtime(&t1, &tm1); 212 OPENSSL_gmtime(&t1, &tm1);
212 OPENSSL_gmtime_adj(&tm1, 0, offset); 213 OPENSSL_gmtime_adj(&tm1, 0, offset);
213 if ((tm1.tm_year == tm2.tm_year) && 214 if ((tm1.tm_year == tm2.tm_year) &&
214 (tm1.tm_mon == tm2.tm_mon) && 215 (tm1.tm_mon == tm2.tm_mon) &&
215 (tm1.tm_mday == tm2.tm_mday) && 216 (tm1.tm_mday == tm2.tm_mday) &&
216 (tm1.tm_hour == tm2.tm_hour) && 217 (tm1.tm_hour == tm2.tm_hour) &&
217 (tm1.tm_min == tm2.tm_min) && 218 (tm1.tm_min == tm2.tm_min) &&
218 (tm1.tm_sec == tm2.tm_sec)) 219 (tm1.tm_sec == tm2.tm_sec))
219 return 1; 220 return 1;
220 fprintf(stderr, "TIME ERROR!!\n"); 221 fprintf(stderr, "TIME ERROR!!\n");
221 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n", 222 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
222 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900, 223 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
223 tm2.tm_hour, tm2.tm_min, tm2.tm_sec); 224 tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
224 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n", 225 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
225 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900, 226 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
226 tm1.tm_hour, tm1.tm_min, tm1.tm_sec); 227 tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
227 return 0; 228 return 0;
228 } 229}
229 230
230#endif 231#endif
diff --git a/src/lib/libssl/src/crypto/ossl_typ.h b/src/lib/libssl/src/crypto/ossl_typ.h
index ea9227f6f9..ae79bf8ae4 100644
--- a/src/lib/libssl/src/crypto/ossl_typ.h
+++ b/src/lib/libssl/src/crypto/ossl_typ.h
@@ -182,18 +182,18 @@ typedef struct DIST_POINT_st DIST_POINT;
182typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT; 182typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT;
183typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS; 183typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS;
184 184
185 /* If placed in pkcs12.h, we end up with a circular depency with pkcs7.h */ 185/* If placed in pkcs12.h, we end up with a circular depency with pkcs7.h */
186#define DECLARE_PKCS12_STACK_OF(type) /* Nothing */ 186#define DECLARE_PKCS12_STACK_OF(type) /* Nothing */
187#define IMPLEMENT_PKCS12_STACK_OF(type) /* Nothing */ 187#define IMPLEMENT_PKCS12_STACK_OF(type) /* Nothing */
188 188
189typedef struct crypto_ex_data_st CRYPTO_EX_DATA; 189typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
190/* Callback types for crypto.h */ 190/* Callback types for crypto.h */
191typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 191typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
192 int idx, long argl, void *argp); 192 int idx, long argl, void *argp);
193typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, 193typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
194 int idx, long argl, void *argp); 194 int idx, long argl, void *argp);
195typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, void *from_d, 195typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
196 int idx, long argl, void *argp); 196 void *from_d, int idx, long argl, void *argp);
197 197
198typedef struct ocsp_req_ctx_st OCSP_REQ_CTX; 198typedef struct ocsp_req_ctx_st OCSP_REQ_CTX;
199typedef struct ocsp_response_st OCSP_RESPONSE; 199typedef struct ocsp_response_st OCSP_RESPONSE;
diff --git a/src/lib/libssl/src/crypto/ppccap.c b/src/lib/libssl/src/crypto/ppccap.c
index f71ba66aa3..16cc9c445e 100644
--- a/src/lib/libssl/src/crypto/ppccap.c
+++ b/src/lib/libssl/src/crypto/ppccap.c
@@ -15,112 +15,110 @@ static int OPENSSL_ppccap_P = 0;
15static sigset_t all_masked; 15static sigset_t all_masked;
16 16
17#ifdef OPENSSL_BN_ASM_MONT 17#ifdef OPENSSL_BN_ASM_MONT
18int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num) 18int
19 { 19bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
20 const BN_ULONG *np, const BN_ULONG *n0, int num)
21{
20 int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); 22 int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
21 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); 23 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
22 24
23 if (sizeof(size_t)==4) 25 if (sizeof(size_t) == 4) {
24 {
25#if (defined(__APPLE__) && defined(__MACH__)) 26#if (defined(__APPLE__) && defined(__MACH__))
26 if (num>=8 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64)) 27 if (num >= 8 && (num&3) == 0 && (OPENSSL_ppccap_P&PPC_FPU64))
27 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num); 28 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
28#else 29#else
29 /* boundary of 32 was experimentally determined on 30 /* boundary of 32 was experimentally determined on
30 Linux 2.6.22, might have to be adjusted on AIX... */ 31 Linux 2.6.22, might have to be adjusted on AIX... */
31 if (num>=32 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64)) 32 if (num >= 32 && (num&3) == 0 && (OPENSSL_ppccap_P&PPC_FPU64)) {
32 {
33 sigset_t oset; 33 sigset_t oset;
34 int ret; 34 int ret;
35 35
36 sigprocmask(SIG_SETMASK,&all_masked,&oset); 36 sigprocmask(SIG_SETMASK, &all_masked, &oset);
37 ret=bn_mul_mont_fpu64(rp,ap,bp,np,n0,num); 37 ret = bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
38 sigprocmask(SIG_SETMASK,&oset,NULL); 38 sigprocmask(SIG_SETMASK, &oset, NULL);
39 39
40 return ret; 40 return ret;
41 }
42#endif
43 } 41 }
44 else if ((OPENSSL_ppccap_P&PPC_FPU64)) 42#endif
43 } else if ((OPENSSL_ppccap_P&PPC_FPU64))
45 /* this is a "must" on POWER6, but run-time detection 44 /* this is a "must" on POWER6, but run-time detection
46 * is not implemented yet... */ 45 * is not implemented yet... */
47 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num); 46 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
48 47
49 return bn_mul_mont_int(rp,ap,bp,np,n0,num); 48 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
50 } 49}
51#endif 50#endif
52 51
53static sigjmp_buf ill_jmp; 52static sigjmp_buf ill_jmp;
54static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); } 53static void ill_handler (int sig)
54{
55 siglongjmp(ill_jmp, sig);
56}
55 57
56void OPENSSL_ppc64_probe(void); 58void OPENSSL_ppc64_probe(void);
57void OPENSSL_altivec_probe(void); 59void OPENSSL_altivec_probe(void);
58 60
59void OPENSSL_cpuid_setup(void) 61void
60 { 62OPENSSL_cpuid_setup(void)
63{
61 char *e; 64 char *e;
62 struct sigaction ill_oact,ill_act; 65 struct sigaction ill_oact, ill_act;
63 sigset_t oset; 66 sigset_t oset;
64 static int trigger=0; 67 static int trigger = 0;
68
69 if (trigger)
70 return;
71 trigger = 1;
65 72
66 if (trigger) return;
67 trigger=1;
68
69 sigfillset(&all_masked); 73 sigfillset(&all_masked);
70 sigdelset(&all_masked,SIGILL); 74 sigdelset(&all_masked, SIGILL);
71 sigdelset(&all_masked,SIGTRAP); 75 sigdelset(&all_masked, SIGTRAP);
72#ifdef SIGEMT 76#ifdef SIGEMT
73 sigdelset(&all_masked,SIGEMT); 77 sigdelset(&all_masked, SIGEMT);
74#endif 78#endif
75 sigdelset(&all_masked,SIGFPE); 79 sigdelset(&all_masked, SIGFPE);
76 sigdelset(&all_masked,SIGBUS); 80 sigdelset(&all_masked, SIGBUS);
77 sigdelset(&all_masked,SIGSEGV); 81 sigdelset(&all_masked, SIGSEGV);
78 82
79 if ((e=getenv("OPENSSL_ppccap"))) 83 if ((e = getenv("OPENSSL_ppccap"))) {
80 { 84 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
81 OPENSSL_ppccap_P=strtoul(e,NULL,0);
82 return; 85 return;
83 } 86 }
84 87
85 OPENSSL_ppccap_P = 0; 88 OPENSSL_ppccap_P = 0;
86 89
87#if defined(_AIX) 90#if defined(_AIX)
88 if (sizeof(size_t)==4 91 if (sizeof(size_t) == 4
89# if defined(_SC_AIX_KERNEL_BITMODE) 92# if defined(_SC_AIX_KERNEL_BITMODE)
90 && sysconf(_SC_AIX_KERNEL_BITMODE)!=64 93 && sysconf(_SC_AIX_KERNEL_BITMODE) != 64
91# endif 94# endif
92 ) 95 )
93 return; 96 return;
94#endif 97#endif
95 98
96 memset(&ill_act,0,sizeof(ill_act)); 99 memset(&ill_act, 0, sizeof(ill_act));
97 ill_act.sa_handler = ill_handler; 100 ill_act.sa_handler = ill_handler;
98 ill_act.sa_mask = all_masked; 101 ill_act.sa_mask = all_masked;
99 102
100 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset); 103 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
101 sigaction(SIGILL,&ill_act,&ill_oact); 104 sigaction(SIGILL, &ill_act, &ill_oact);
102 105
103 if (sizeof(size_t)==4) 106 if (sizeof(size_t) == 4) {
104 { 107 if (sigsetjmp(ill_jmp, 1) == 0) {
105 if (sigsetjmp(ill_jmp,1) == 0)
106 {
107 OPENSSL_ppc64_probe(); 108 OPENSSL_ppc64_probe();
108 OPENSSL_ppccap_P |= PPC_FPU64; 109 OPENSSL_ppccap_P |= PPC_FPU64;
109 }
110 } 110 }
111 else 111 } else {
112 {
113 /* 112 /*
114 * Wanted code detecting POWER6 CPU and setting PPC_FPU64 113 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
115 */ 114 */
116 } 115 }
117 116
118 if (sigsetjmp(ill_jmp,1) == 0) 117 if (sigsetjmp(ill_jmp, 1) == 0) {
119 {
120 OPENSSL_altivec_probe(); 118 OPENSSL_altivec_probe();
121 OPENSSL_ppccap_P |= PPC_ALTIVEC; 119 OPENSSL_ppccap_P |= PPC_ALTIVEC;
122 }
123
124 sigaction (SIGILL,&ill_oact,NULL);
125 sigprocmask(SIG_SETMASK,&oset,NULL);
126 } 120 }
121
122 sigaction (SIGILL, &ill_oact, NULL);
123 sigprocmask(SIG_SETMASK, &oset, NULL);
124}
diff --git a/src/lib/libssl/src/crypto/s390xcap.c b/src/lib/libssl/src/crypto/s390xcap.c
index f2e94ef47e..77f6260785 100644
--- a/src/lib/libssl/src/crypto/s390xcap.c
+++ b/src/lib/libssl/src/crypto/s390xcap.c
@@ -7,31 +7,36 @@
7extern unsigned long OPENSSL_s390xcap_P[]; 7extern unsigned long OPENSSL_s390xcap_P[];
8 8
9static sigjmp_buf ill_jmp; 9static sigjmp_buf ill_jmp;
10static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); } 10static void ill_handler (int sig)
11{
12 siglongjmp(ill_jmp, sig);
13}
11 14
12unsigned long OPENSSL_s390x_facilities(void); 15unsigned long OPENSSL_s390x_facilities(void);
13 16
14void OPENSSL_cpuid_setup(void) 17void
15 { 18OPENSSL_cpuid_setup(void)
19{
16 sigset_t oset; 20 sigset_t oset;
17 struct sigaction ill_act,oact; 21 struct sigaction ill_act, oact;
18 22
19 if (OPENSSL_s390xcap_P[0]) return; 23 if (OPENSSL_s390xcap_P[0])
24 return;
20 25
21 OPENSSL_s390xcap_P[0] = 1UL<<(8*sizeof(unsigned long)-1); 26 OPENSSL_s390xcap_P[0] = 1UL << (8*sizeof(unsigned long) - 1);
22 27
23 memset(&ill_act,0,sizeof(ill_act)); 28 memset(&ill_act, 0, sizeof(ill_act));
24 ill_act.sa_handler = ill_handler; 29 ill_act.sa_handler = ill_handler;
25 sigfillset(&ill_act.sa_mask); 30 sigfillset(&ill_act.sa_mask);
26 sigdelset(&ill_act.sa_mask,SIGILL); 31 sigdelset(&ill_act.sa_mask, SIGILL);
27 sigdelset(&ill_act.sa_mask,SIGTRAP); 32 sigdelset(&ill_act.sa_mask, SIGTRAP);
28 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset); 33 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
29 sigaction (SIGILL,&ill_act,&oact); 34 sigaction (SIGILL, &ill_act, &oact);
30 35
31 /* protection against missing store-facility-list-extended */ 36 /* protection against missing store-facility-list-extended */
32 if (sigsetjmp(ill_jmp,1) == 0) 37 if (sigsetjmp(ill_jmp, 1) == 0)
33 OPENSSL_s390x_facilities(); 38 OPENSSL_s390x_facilities();
34 39
35 sigaction (SIGILL,&oact,NULL); 40 sigaction (SIGILL, &oact, NULL);
36 sigprocmask(SIG_SETMASK,&oset,NULL); 41 sigprocmask(SIG_SETMASK, &oset, NULL);
37 } 42}
diff --git a/src/lib/libssl/src/crypto/sparcv9cap.c b/src/lib/libssl/src/crypto/sparcv9cap.c
index 43b3ac6f81..08f7de3ec8 100644
--- a/src/lib/libssl/src/crypto/sparcv9cap.c
+++ b/src/lib/libssl/src/crypto/sparcv9cap.c
@@ -12,20 +12,22 @@
12#define SPARCV9_VIS2 (1<<3) /* reserved */ 12#define SPARCV9_VIS2 (1<<3) /* reserved */
13#define SPARCV9_FMADD (1<<4) /* reserved for SPARC64 V */ 13#define SPARCV9_FMADD (1<<4) /* reserved for SPARC64 V */
14 14
15static int OPENSSL_sparcv9cap_P=SPARCV9_TICK_PRIVILEGED; 15static int OPENSSL_sparcv9cap_P = SPARCV9_TICK_PRIVILEGED;
16 16
17int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num) 17int
18 { 18bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
19 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num); 19 const BN_ULONG *np, const BN_ULONG *n0, int num)
20 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num); 20{
21 21 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
22 if (num>=8 && !(num&1) && 22 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
23 (OPENSSL_sparcv9cap_P&(SPARCV9_PREFER_FPU|SPARCV9_VIS1)) == 23
24 (SPARCV9_PREFER_FPU|SPARCV9_VIS1)) 24 if (num >= 8 && !(num & 1) &&
25 return bn_mul_mont_fpu(rp,ap,bp,np,n0,num); 25 (OPENSSL_sparcv9cap_P & (SPARCV9_PREFER_FPU|SPARCV9_VIS1)) ==
26 (SPARCV9_PREFER_FPU|SPARCV9_VIS1))
27 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
26 else 28 else
27 return bn_mul_mont_int(rp,ap,bp,np,n0,num); 29 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
28 } 30}
29 31
30unsigned long _sparcv9_rdtick(void); 32unsigned long _sparcv9_rdtick(void);
31void _sparcv9_vis1_probe(void); 33void _sparcv9_vis1_probe(void);
@@ -33,8 +35,9 @@ unsigned long _sparcv9_vis1_instrument(void);
33void _sparcv9_vis2_probe(void); 35void _sparcv9_vis2_probe(void);
34void _sparcv9_fmadd_probe(void); 36void _sparcv9_fmadd_probe(void);
35 37
36unsigned long OPENSSL_rdtsc(void) 38unsigned long
37 { 39OPENSSL_rdtsc(void)
40{
38 if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED) 41 if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED)
39#if defined(__sun) && defined(__SVR4) 42#if defined(__sun) && defined(__SVR4)
40 return gethrtime(); 43 return gethrtime();
@@ -43,7 +46,7 @@ unsigned long OPENSSL_rdtsc(void)
43#endif 46#endif
44 else 47 else
45 return _sparcv9_rdtick(); 48 return _sparcv9_rdtick();
46 } 49}
47 50
48#if 0 && defined(__sun) && defined(__SVR4) 51#if 0 && defined(__sun) && defined(__SVR4)
49/* This code path is disabled, because of incompatibility of 52/* This code path is disabled, because of incompatibility of
@@ -54,21 +57,22 @@ unsigned long OPENSSL_rdtsc(void)
54#include <libdevinfo.h> 57#include <libdevinfo.h>
55#include <sys/systeminfo.h> 58#include <sys/systeminfo.h>
56 59
57typedef di_node_t (*di_init_t)(const char *,uint_t); 60typedef di_node_t (*di_init_t)(const char *, uint_t);
58typedef void (*di_fini_t)(di_node_t); 61typedef void (*di_fini_t)(di_node_t);
59typedef char * (*di_node_name_t)(di_node_t); 62typedef char * (*di_node_name_t)(di_node_t);
60typedef int (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t)); 63typedef int (*di_walk_node_t)(di_node_t, uint_t, di_node_name_t, int (*)(di_node_t, di_node_name_t));
61 64
62#define DLLINK(h,name) (name=(name##_t)dlsym((h),#name)) 65#define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
63 66
64static int walk_nodename(di_node_t node, di_node_name_t di_node_name) 67static int
65 { 68walk_nodename(di_node_t node, di_node_name_t di_node_name)
69{
66 char *name = (*di_node_name)(node); 70 char *name = (*di_node_name)(node);
67 71
68 /* This is expected to catch all UltraSPARC flavors prior T1 */ 72 /* This is expected to catch all UltraSPARC flavors prior T1 */
69 if (!strcmp (name,"SUNW,UltraSPARC") || 73 if (!strcmp (name, "SUNW, UltraSPARC") ||
70 !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */ 74 !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
71 { 75 {
72 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1; 76 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
73 77
74 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */ 78 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
@@ -76,51 +80,48 @@ static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
76 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 80 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
77 81
78 return DI_WALK_TERMINATE; 82 return DI_WALK_TERMINATE;
79 } 83 }
80 /* This is expected to catch remaining UltraSPARCs, such as T1 */ 84 /* This is expected to catch remaining UltraSPARCs, such as T1 */
81 else if (!strncmp(name,"SUNW,UltraSPARC",15)) 85 else if (!strncmp(name, "SUNW, UltraSPARC", 15)) {
82 {
83 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 86 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
84 87
85 return DI_WALK_TERMINATE; 88 return DI_WALK_TERMINATE;
86 } 89 }
87 90
88 return DI_WALK_CONTINUE; 91 return DI_WALK_CONTINUE;
89 } 92}
90 93
91void OPENSSL_cpuid_setup(void) 94void
92 { 95OPENSSL_cpuid_setup(void)
96{
93 void *h; 97 void *h;
94 char *e,si[256]; 98 char *e, si[256];
95 static int trigger=0; 99 static int trigger = 0;
96 100
97 if (trigger) return; 101 if (trigger)
98 trigger=1; 102 return;
103 trigger = 1;
99 104
100 if ((e=getenv("OPENSSL_sparcv9cap"))) 105 if ((e = getenv("OPENSSL_sparcv9cap"))) {
101 { 106 OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
102 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
103 return; 107 return;
104 } 108 }
105 109
106 if (sysinfo(SI_MACHINE,si,sizeof(si))>0) 110 if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
107 { 111 if (strcmp(si, "sun4v"))
108 if (strcmp(si,"sun4v"))
109 /* FPU is preferred for all CPUs, but US-T1/2 */ 112 /* FPU is preferred for all CPUs, but US-T1/2 */
110 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU; 113 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
111 } 114 }
112 115
113 if (sysinfo(SI_ISALIST,si,sizeof(si))>0) 116 if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
114 { 117 if (strstr(si, "+vis"))
115 if (strstr(si,"+vis"))
116 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1; 118 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
117 if (strstr(si,"+vis2")) 119 if (strstr(si, "+vis2")) {
118 {
119 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2; 120 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
120 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 121 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
121 return; 122 return;
122 }
123 } 123 }
124 }
124#ifdef M_KEEP 125#ifdef M_KEEP
125 /* 126 /*
126 * Solaris libdevinfo.so.1 is effectively incomatible with 127 * Solaris libdevinfo.so.1 is effectively incomatible with
@@ -129,109 +130,115 @@ void OPENSSL_cpuid_setup(void)
129 * free(3LIBMALLOC) called by di_fini. Prior call to 130 * free(3LIBMALLOC) called by di_fini. Prior call to
130 * mallopt(M_KEEP,0) somehow helps... But not always... 131 * mallopt(M_KEEP,0) somehow helps... But not always...
131 */ 132 */
132 if ((h = dlopen(NULL,RTLD_LAZY))) 133 if ((h = dlopen(NULL, RTLD_LAZY))) {
133 { 134 union { void *p;
134 union { void *p; int (*f)(int,int); } sym; 135 int (*f)(int, int);
135 if ((sym.p = dlsym(h,"mallopt"))) (*sym.f)(M_KEEP,0); 136 } sym;
136 dlclose(h); 137 if ((sym.p = dlsym(h, "mallopt"))) (*sym.f)(M_KEEP, 0);
137 } 138 dlclose(h);
139 }
138#endif 140#endif
139 if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do 141 if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
140 { 142 do {
141 di_init_t di_init; 143 di_init_t di_init;
142 di_fini_t di_fini; 144 di_fini_t di_fini;
143 di_walk_node_t di_walk_node; 145 di_walk_node_t di_walk_node;
144 di_node_name_t di_node_name; 146 di_node_name_t di_node_name;
145 di_node_t root_node; 147 di_node_t root_node;
146 148
147 if (!DLLINK(h,di_init)) break; 149 if (!DLLINK(h, di_init))
148 if (!DLLINK(h,di_fini)) break; 150 break;
149 if (!DLLINK(h,di_walk_node)) break; 151 if (!DLLINK(h, di_fini))
150 if (!DLLINK(h,di_node_name)) break; 152 break;
151 153 if (!DLLINK(h, di_walk_node))
152 if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL) 154 break;
153 { 155 if (!DLLINK(h, di_node_name))
154 (*di_walk_node)(root_node,DI_WALK_SIBFIRST, 156 break;
155 di_node_name,walk_nodename); 157
156 (*di_fini)(root_node); 158 if ((root_node = (*di_init)("/", DINFOSUBTREE)) !=
159 DI_NODE_NIL) {
160 (*di_walk_node)(root_node, DI_WALK_SIBFIRST,
161 di_node_name, walk_nodename);
162 (*di_fini)(root_node);
157 } 163 }
158 } while(0); 164 } while (0);
159 165
160 if (h) dlclose(h); 166 if (h)
161 } 167 dlclose(h);
168}
162 169
163#else 170#else
164 171
165static sigjmp_buf common_jmp; 172static sigjmp_buf common_jmp;
166static void common_handler(int sig) { siglongjmp(common_jmp,sig); } 173static void common_handler(int sig)
167 174{
168void OPENSSL_cpuid_setup(void) 175 siglongjmp(common_jmp, sig);
169 { 176}
177
178void
179OPENSSL_cpuid_setup(void)
180{
170 char *e; 181 char *e;
171 struct sigaction common_act,ill_oact,bus_oact; 182 struct sigaction common_act, ill_oact, bus_oact;
172 sigset_t all_masked,oset; 183 sigset_t all_masked, oset;
173 static int trigger=0; 184 static int trigger = 0;
174 185
175 if (trigger) return; 186 if (trigger)
176 trigger=1;
177
178 if ((e=getenv("OPENSSL_sparcv9cap")))
179 {
180 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
181 return; 187 return;
182 } 188 trigger = 1;
189
190 if ((e = getenv("OPENSSL_sparcv9cap"))) {
191 OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
192 return;
193 }
183 194
184 /* Initial value, fits UltraSPARC-I&II... */ 195 /* Initial value, fits UltraSPARC-I&II... */
185 OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU|SPARCV9_TICK_PRIVILEGED; 196 OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU|SPARCV9_TICK_PRIVILEGED;
186 197
187 sigfillset(&all_masked); 198 sigfillset(&all_masked);
188 sigdelset(&all_masked,SIGILL); 199 sigdelset(&all_masked, SIGILL);
189 sigdelset(&all_masked,SIGTRAP); 200 sigdelset(&all_masked, SIGTRAP);
190#ifdef SIGEMT 201#ifdef SIGEMT
191 sigdelset(&all_masked,SIGEMT); 202 sigdelset(&all_masked, SIGEMT);
192#endif 203#endif
193 sigdelset(&all_masked,SIGFPE); 204 sigdelset(&all_masked, SIGFPE);
194 sigdelset(&all_masked,SIGBUS); 205 sigdelset(&all_masked, SIGBUS);
195 sigdelset(&all_masked,SIGSEGV); 206 sigdelset(&all_masked, SIGSEGV);
196 sigprocmask(SIG_SETMASK,&all_masked,&oset); 207 sigprocmask(SIG_SETMASK, &all_masked, &oset);
197 208
198 memset(&common_act,0,sizeof(common_act)); 209 memset(&common_act, 0, sizeof(common_act));
199 common_act.sa_handler = common_handler; 210 common_act.sa_handler = common_handler;
200 common_act.sa_mask = all_masked; 211 common_act.sa_mask = all_masked;
201 212
202 sigaction(SIGILL,&common_act,&ill_oact); 213 sigaction(SIGILL, &common_act, &ill_oact);
203 sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda [on Linux] */ 214 sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda [on Linux] */
204 215
205 if (sigsetjmp(common_jmp,1) == 0) 216 if (sigsetjmp(common_jmp, 1) == 0) {
206 {
207 _sparcv9_rdtick(); 217 _sparcv9_rdtick();
208 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED; 218 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
209 } 219 }
210 220
211 if (sigsetjmp(common_jmp,1) == 0) 221 if (sigsetjmp(common_jmp, 1) == 0) {
212 {
213 _sparcv9_vis1_probe(); 222 _sparcv9_vis1_probe();
214 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1; 223 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
215 /* detect UltraSPARC-Tx, see sparccpud.S for details... */ 224 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
216 if (_sparcv9_vis1_instrument() >= 12) 225 if (_sparcv9_vis1_instrument() >= 12)
217 OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1|SPARCV9_PREFER_FPU); 226 OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1|SPARCV9_PREFER_FPU);
218 else 227 else {
219 {
220 _sparcv9_vis2_probe(); 228 _sparcv9_vis2_probe();
221 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2; 229 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
222 }
223 } 230 }
231 }
224 232
225 if (sigsetjmp(common_jmp,1) == 0) 233 if (sigsetjmp(common_jmp, 1) == 0) {
226 {
227 _sparcv9_fmadd_probe(); 234 _sparcv9_fmadd_probe();
228 OPENSSL_sparcv9cap_P |= SPARCV9_FMADD; 235 OPENSSL_sparcv9cap_P |= SPARCV9_FMADD;
229 } 236 }
230 237
231 sigaction(SIGBUS,&bus_oact,NULL); 238 sigaction(SIGBUS, &bus_oact, NULL);
232 sigaction(SIGILL,&ill_oact,NULL); 239 sigaction(SIGILL, &ill_oact, NULL);
233 240
234 sigprocmask(SIG_SETMASK,&oset,NULL); 241 sigprocmask(SIG_SETMASK, &oset, NULL);
235 } 242}
236 243
237#endif 244#endif
diff --git a/src/lib/libssl/src/crypto/uid.c b/src/lib/libssl/src/crypto/uid.c
index b1fd52bada..fcfa05ce0c 100644
--- a/src/lib/libssl/src/crypto/uid.c
+++ b/src/lib/libssl/src/crypto/uid.c
@@ -60,30 +60,32 @@
60 60
61#include OPENSSL_UNISTD 61#include OPENSSL_UNISTD
62 62
63int OPENSSL_issetugid(void) 63int
64 { 64OPENSSL_issetugid(void)
65{
65 return issetugid(); 66 return issetugid();
66 } 67}
67 68
68#elif defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) 69#elif defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
69 70
70int OPENSSL_issetugid(void) 71int
71 { 72OPENSSL_issetugid(void)
73{
72 return 0; 74 return 0;
73 } 75}
74 76
75#else 77#else
76 78
77#include OPENSSL_UNISTD 79#include OPENSSL_UNISTD
78#include <sys/types.h> 80#include <sys/types.h>
79 81
80int OPENSSL_issetugid(void) 82int
81 { 83OPENSSL_issetugid(void)
82 if (getuid() != geteuid()) return 1; 84{
83 if (getgid() != getegid()) return 1; 85 if (getuid()
86 != geteuid()) return 1;
87 if (getgid()
88 != getegid()) return 1;
84 return 0; 89 return 0;
85 } 90}
86#endif 91#endif
87
88
89