2 # This file adds the code to setup internal mutexes and callback function.
5 # This change was implemented in-house. The issue was brought up to
6 # the upstream engineers, but there was no commitment.
8 --- a/crypto/cryptlib.c 2016-05-03 06:44:42.000000000 -0700
9 +++ b/crypto/cryptlib.c 2016-09-02 08:47:23.640202700 -0700
13 #include <openssl/safestack.h>
16 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
17 static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
20 static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
22 +static pthread_mutex_t solaris_openssl_locks[CRYPTO_NUM_LOCKS];
24 static void (MS_FAR *locking_callback) (int mode, int type,
25 const char *file, int line) = 0;
26 static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
28 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
29 (const char *file, int line))
31 - dynlock_create_callback = func;
33 + * we now setup our own dynamic locking callback, and disallow
34 + * setting of another locking callback.
38 void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
43 - dynlock_lock_callback = func;
45 + * we now setup our own dynamic locking callback, and disallow
46 + * setting of another locking callback.
50 void CRYPTO_set_dynlock_destroy_callback(void (*func)
52 (struct CRYPTO_dynlock_value *l,
53 const char *file, int line))
55 - dynlock_destroy_callback = func;
57 + * we now setup our own dynamic locking callback, and disallow
58 + * setting of another locking callback.
62 void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
64 return (add_lock_callback);
68 + * This is the locking callback function which all applications will be
69 + * using when CRYPTO_lock() is called.
71 +static void solaris_locking_callback(int mode, int type, const char *file,
74 + if (mode & CRYPTO_LOCK) {
75 + (void) pthread_mutex_lock(&solaris_openssl_locks[type]);
77 + (void) pthread_mutex_unlock(&solaris_openssl_locks[type]);
82 + * Implement Solaris's own dynamic locking routines.
84 +static struct CRYPTO_dynlock_value *
85 +solaris_dynlock_create(const char *file, int line)
88 + pthread_mutex_t *dynlock;
90 + dynlock = OPENSSL_malloc(sizeof(pthread_mutex_t));
91 + if (dynlock == NULL) {
95 + ret = pthread_mutex_init(dynlock, NULL);
96 + OPENSSL_assert(ret == 0);
98 + return ((struct CRYPTO_dynlock_value *)dynlock);
102 +solaris_dynlock_lock(int mode, struct CRYPTO_dynlock_value *dynlock,
103 + const char *file, int line)
107 + if (mode & CRYPTO_LOCK) {
108 + ret = pthread_mutex_lock((pthread_mutex_t *)dynlock);
110 + ret = pthread_mutex_unlock((pthread_mutex_t *)dynlock);
113 + OPENSSL_assert(ret == 0);
117 +solaris_dynlock_destroy(struct CRYPTO_dynlock_value *dynlock,
118 + const char *file, int line)
121 + ret = pthread_mutex_destroy((pthread_mutex_t *)dynlock);
122 + OPENSSL_assert(ret == 0);
126 +static void solaris_fork_prep(void)
130 + for (i = 0; i < CRYPTO_NUM_LOCKS; i++) {
131 + (void) pthread_mutex_lock(&solaris_openssl_locks[i]);
135 +static void solaris_fork_post(void)
139 + for (i = CRYPTO_NUM_LOCKS - 1; i >= 0; i--) {
140 + (void) pthread_mutex_unlock(&solaris_openssl_locks[i]);
143 + OPENSSL_assert(dynlock_create_callback == solaris_dynlock_create);
144 + OPENSSL_assert(dynlock_lock_callback == solaris_dynlock_lock);
145 + OPENSSL_assert(dynlock_destroy_callback == solaris_dynlock_destroy);
146 + OPENSSL_assert(locking_callback == solaris_locking_callback);
150 + * This is called by the _init() function to setup locks used by OpenSSL
151 + * and locking callback functions.
154 +solaris_locking_setup()
158 + /* setup the dynlock callback if not already */
159 + if (dynlock_create_callback == NULL) {
160 + dynlock_create_callback = solaris_dynlock_create;
162 + if (dynlock_lock_callback == NULL) {
163 + dynlock_lock_callback = solaris_dynlock_lock;
165 + if (dynlock_destroy_callback == NULL) {
166 + dynlock_destroy_callback = solaris_dynlock_destroy;
168 + if (locking_callback == NULL) {
169 + locking_callback = solaris_locking_callback;
172 + /* initialize locks needed by OpenSSL */
173 + for (i = 0; i < CRYPTO_NUM_LOCKS; i++) {
174 + (void) pthread_mutex_init(&solaris_openssl_locks[i], NULL);
177 + (void) pthread_atfork(solaris_fork_prep, solaris_fork_post, solaris_fork_post);
181 void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
182 const char *file, int line))
188 - locking_callback = func;
191 + * we now setup our own locking callback and mutexes, and disallow
192 + * setting of another locking callback.
196 void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
197 @@ -471,12 +608,10 @@
199 int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
201 - if (threadid_callback)
203 - threadid_callback = func;
205 - FIPS_crypto_threadid_set_callback(func);
208 + * Setting a threadid callback is no longer allowed; the compiled-in
209 + * platform-specific default is always used.
215 CRYPTO_THREADID_set_numeric(id, (unsigned long)find_thread(NULL));
217 - /* For everything else, default to using the address of 'errno' */
218 - CRYPTO_THREADID_set_pointer(id, (void *)&errno);
219 + /* For everything else, default to using pthread_self() */
220 + CRYPTO_THREADID_set_numeric(id, (unsigned long)pthread_self());
226 void CRYPTO_set_id_callback(unsigned long (*func) (void))
228 - id_callback = func;
230 + * Setting a threadid callback is no longer allowed; the compiled-in
231 + * platform-specific default is always used.
235 unsigned long CRYPTO_thread_id(void)
237 # elif defined(OPENSSL_SYS_BEOS)
238 ret = (unsigned long)find_thread(NULL);
240 - ret = (unsigned long)getpid();
241 + ret = (unsigned long)pthread_self();
245 --- openssl-1.0.1f/crypto/cryptlib.h.~1~ Fri Feb 7 10:41:42 2014
246 +++ openssl-1.0.1f/crypto/cryptlib.h Thu Feb 6 16:04:16 2014
248 void *OPENSSL_stderr(void);
249 extern int OPENSSL_NONPIC_relocated;
251 +void solaris_locking_setup();
256 --- openssl-1.0.1f/crypto/sparccpuid.S.~1~ Fri Feb 7 10:41:37 2014
257 +++ openssl-1.0.1f/crypto/sparccpuid.S Thu Feb 6 16:04:14 2014
259 .size _sparcv9_vis1_instrument_bus2,.-_sparcv9_vis1_instrument_bus2
261 .section ".init",#alloc,#execinstr
262 + call solaris_locking_setup
264 call OPENSSL_cpuid_setup
266 --- openssl-1.0.1f/crypto/x86_64cpuid.pl.~1~ Wed Feb 12 13:20:09 2014
267 +++ openssl-1.0.1f/crypto/x86_64cpuid.pl Wed Feb 12 13:21:20 2014
270 .extern OPENSSL_cpuid_setup
271 .hidden OPENSSL_cpuid_setup
272 +.extern solaris_locking_setup
273 +.hidden solaris_locking_setup
275 + call solaris_locking_setup
276 call OPENSSL_cpuid_setup
278 .hidden OPENSSL_ia32cap_P
279 --- openssl-1.0.1f/crypto/x86cpuid.pl.~1~ Wed Feb 12 13:38:03 2014
280 +++ openssl-1.0.1f/crypto/x86cpuid.pl Wed Feb 12 13:38:31 2014
283 &function_end_B("OPENSSL_ia32_rdseed");
285 +&initseg("solaris_locking_setup");
286 &initseg("OPENSSL_cpuid_setup");
288 +&hidden("solaris_locking_setup");
289 &hidden("OPENSSL_cpuid_setup");
290 &hidden("OPENSSL_ia32cap_P");