@@ -244,7 +244,7 @@ proc TLSv1_method*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
244
244
# and support SSLv3, TLSv1, TLSv1.1 and TLSv1.2
245
245
# SSLv23_method(), SSLv23_server_method(), SSLv23_client_method() are removed in 1.1.0
246
246
247
- when compileOption(" dynlibOverride" , " ssl" ):
247
+ when compileOption(" dynlibOverride" , " ssl" ) or defined(noOpenSSLHacks) :
248
248
# Static linking
249
249
250
250
when defined(openssl10):
@@ -285,40 +285,59 @@ else:
285
285
proc thisModule(): LibHandle {.inline.} =
286
286
var thisMod {.global.}: LibHandle
287
287
if thisMod.isNil: thisMod = loadLib()
288
+
288
289
result = thisMod
289
290
290
291
proc sslModule(): LibHandle {.inline.} =
291
292
var sslMod {.global.}: LibHandle
292
293
if sslMod.isNil: sslMod = loadLibPattern(DLLSSLName)
294
+
293
295
result = sslMod
294
296
295
- proc sslSym(name: string ): pointer =
296
- var dl = thisModule()
297
- if not dl.isNil:
298
- result = symAddr(dl, name)
297
+ proc sslSymNullable(name: string , alternativeName = " " ): pointer =
298
+ # Load from DLL.
299
+ var sslDynlib = sslModule()
300
+ if not sslDynlib.isNil:
301
+ result = symAddr(sslDynlib, name)
302
+ if result .isNil and alternativeName.len > 0 :
303
+ result = symAddr(sslDynlib, alternativeName)
304
+
305
+ # Attempt to load from current exe.
299
306
if result .isNil:
300
- dl = sslModule()
301
- if not dl.isNil:
302
- result = symAddr(dl, name)
307
+ let thisDynlib = thisModule()
308
+ if thisDynlib.isNil: return nil
309
+ result = symAddr(thisDynlib, name)
310
+ if result .isNil and alternativeName.len > 0 :
311
+ result = symAddr(sslDynlib, alternativeName)
312
+
313
+ proc sslSymThrows(name: string , alternativeName = " " ): pointer =
314
+ result = sslSymNullable(name, alternativeName)
315
+ if result .isNil: raiseInvalidLibrary(name)
303
316
304
317
proc loadPSSLMethod(method1, method2: string ): PSSL_METHOD =
305
318
# # Load <method1> from OpenSSL if available, otherwise <method2>
306
- let m1 = cast [proc (): PSSL_METHOD {.cdecl, gcsafe.}](sslSym(method1))
307
- if not m1.isNil:
308
- return m1()
309
- cast [proc (): PSSL_METHOD {.cdecl, gcsafe.}](sslSym(method2))()
319
+ # #
320
+ let methodSym = sslSymNullable(method1, method2)
321
+ if methodSym.isNil:
322
+ raise newException(LibraryError, " Could not load " & method1 & " nor " & method2)
323
+
324
+ let method2Proc = cast [proc (): PSSL_METHOD {.cdecl, gcsafe.}](methodSym)
325
+ return method2Proc()
310
326
311
327
proc SSL_library_init* (): cint {.discardable.} =
312
328
# # Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0 otherwise
313
329
# # SSL_library_init
314
- let theProc = cast [proc (opts: uint64 , settings: uint8 ): cint {.cdecl.}](sslSym(" OPENSSL_init_ssl" ))
315
- if not theProc.isNil:
316
- return theProc(0 , 0 )
317
- let olderProc = cast [proc (): cint {.cdecl.}](sslSym(" SSL_library_init" ))
330
+ let newInitSym = sslSymNullable(" OPENSSL_init_ssl" )
331
+ if not newInitSym.isNil:
332
+ let newInitProc =
333
+ cast [proc (opts: uint64 , settings: uint8 ): cint {.cdecl.}](newInitSym)
334
+ return newInitProc(0 , 0 )
335
+ let olderProc = cast [proc (): cint {.cdecl.}](sslSymThrows(" SSL_library_init" ))
318
336
if not olderProc.isNil: result = olderProc()
319
337
320
338
proc SSL_load_error_strings* () =
321
- let theProc = cast [proc () {.cdecl.}](sslSym(" SSL_load_error_strings" ))
339
+ # TODO : Are we ignoring this on purpose? SSL GitHub CI fails otherwise.
340
+ let theProc = cast [proc () {.cdecl.}](sslSymNullable(" SSL_load_error_strings" ))
322
341
if not theProc.isNil: theProc()
323
342
324
343
proc SSLv23_client_method * (): PSSL_METHOD =
@@ -343,12 +362,13 @@ else:
343
362
loadPSSLMethod(" TLS_server_method" , " SSLv23_server_method" )
344
363
345
364
proc OpenSSL_add_all_algorithms* () =
346
- let theProc = cast [proc () {.cdecl.}](sslSym(" OPENSSL_add_all_algorithms_conf" ))
365
+ # TODO : Are we ignoring this on purpose? SSL GitHub CI fails otherwise.
366
+ let theProc = cast [proc () {.cdecl.}](sslSymNullable(" OPENSSL_add_all_algorithms_conf" ))
347
367
if not theProc.isNil: theProc()
348
368
349
369
proc getOpenSSLVersion* (): culong =
350
370
# # Return OpenSSL version as unsigned long or 0 if not available
351
- let theProc = cast [proc (): culong {.cdecl.}](sslSym (" OpenSSL_version_num" ))
371
+ let theProc = cast [proc (): culong {.cdecl.}](sslSymNullable (" OpenSSL_version_num" ))
352
372
result =
353
373
if theProc.isNil: 0 .culong
354
374
else : theProc()
0 commit comments