@@ -98,27 +98,44 @@ typedef struct ossl3_library_context {
98
98
OSSL_PROVIDER * default_provider ;
99
99
} ossl3_context_t ;
100
100
101
- static ossl3_context_t * init_ossl3_ctx ()
101
+ static pthread_once_t global_ossl3_ctx_init = PTHREAD_ONCE_INIT ;
102
+ static ossl3_context_t * global_ossl3_ctx = NULL ;
103
+
104
+ static void init_global_ossl3_ctx (void )
102
105
{
103
106
ossl3_context_t * ctx = OPENSSL_malloc (sizeof (ossl3_context_t ));
104
- if (!ctx ) return NULL ;
107
+ if (!ctx ) return ;
105
108
106
109
ctx -> libctx = OSSL_LIB_CTX_new ();
107
110
if (!ctx -> libctx ) {
108
111
OPENSSL_free (ctx );
109
- return NULL ;
112
+ return ;
110
113
}
111
114
112
115
/* Load both legacy and default provider as both may be needed */
113
116
/* if they fail keep going and an error will be raised when we try to
114
117
* fetch the cipher later */
115
118
ctx -> legacy_provider = OSSL_PROVIDER_load (ctx -> libctx , "legacy" );
116
119
ctx -> default_provider = OSSL_PROVIDER_load (ctx -> libctx , "default" );
117
- return ctx ;
120
+ global_ossl3_ctx = ctx ;
121
+ }
122
+
123
+ static ossl3_context_t * get_ossl3_ctx ()
124
+ {
125
+ int ret ;
126
+
127
+ ret = pthread_once (& global_ossl3_ctx_init , init_global_ossl3_ctx );
128
+ if (ret != 0 ) {
129
+ return NULL ;
130
+ }
131
+
132
+ return global_ossl3_ctx ;
118
133
}
119
134
120
- static void free_ossl3_ctx (ossl3_context_t * ctx )
135
+ __attribute__((destructor ))
136
+ static void free_ossl3_ctx ()
121
137
{
138
+ ossl3_context_t * ctx = global_ossl3_ctx ;
122
139
if (ctx == NULL ) return ;
123
140
if (ctx -> legacy_provider ) OSSL_PROVIDER_unload (ctx -> legacy_provider );
124
141
if (ctx -> default_provider ) OSSL_PROVIDER_unload (ctx -> default_provider );
@@ -178,7 +195,7 @@ int MD4_HASH(struct ntlm_buffer *payload,
178
195
EVP_MD * md ;
179
196
int ret ;
180
197
181
- ossl3_ctx = init_ossl3_ctx ();
198
+ ossl3_ctx = get_ossl3_ctx ();
182
199
if (ossl3_ctx == NULL ) {
183
200
ret = ERR_CRYPTO ;
184
201
goto done ;
@@ -193,7 +210,6 @@ int MD4_HASH(struct ntlm_buffer *payload,
193
210
ret = mdx_hash (md , payload , result );
194
211
195
212
done :
196
- free_ossl3_ctx (ossl3_ctx );
197
213
return ret ;
198
214
#else
199
215
return mdx_hash (EVP_md4 (), payload , result );
0 commit comments