Merge pull request #6922 from FedericoCeratto/openssl-1.1.0
Add OpenSSL 1.1.0 support
This commit is contained in:
commit
1256f211f5
3 changed files with 113 additions and 22 deletions
|
|
@ -413,7 +413,7 @@ proc isIpAddress*(address_str: string): bool {.tags: [].} =
|
||||||
|
|
||||||
when defineSsl:
|
when defineSsl:
|
||||||
CRYPTO_malloc_init()
|
CRYPTO_malloc_init()
|
||||||
SslLibraryInit()
|
doAssert SslLibraryInit() == 1
|
||||||
SslLoadErrorStrings()
|
SslLoadErrorStrings()
|
||||||
ErrLoadBioStrings()
|
ErrLoadBioStrings()
|
||||||
OpenSSL_add_all_algorithms()
|
OpenSSL_add_all_algorithms()
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,17 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
## OpenSSL support
|
## OpenSSL support
|
||||||
|
##
|
||||||
|
## When OpenSSL is dynamically linked, the wrapper provides partial forward and backward
|
||||||
|
## compatibility for OpenSSL versions above and below 1.1.0
|
||||||
|
##
|
||||||
|
## OpenSSL can be also statically linked using dynlibOverride:ssl for OpenSSL >= 1.1.0
|
||||||
|
##
|
||||||
|
## Build and test examples:
|
||||||
|
##
|
||||||
|
## .. code-block::
|
||||||
|
## ./bin/nim c -d:ssl -p:. -r tests/untestable/tssl.nim
|
||||||
|
## ./bin/nim c -d:ssl -p:. --dynlibOverride:ssl --passL:-lcrypto --passL:-lssl -r tests/untestable/tssl.nim
|
||||||
|
|
||||||
{.deadCodeElim: on.}
|
{.deadCodeElim: on.}
|
||||||
|
|
||||||
|
|
@ -25,8 +36,8 @@ when useWinVersion:
|
||||||
|
|
||||||
from winlean import SocketHandle
|
from winlean import SocketHandle
|
||||||
else:
|
else:
|
||||||
const
|
const versions = "(.1.1|.38|.39|.41|.43|.10|.1.0.2|.1.0.1|.1.0.0|.0.9.9|.0.9.8|)"
|
||||||
versions = "(|.38|.39|.41|.43|.10|.1.0.2|.1.0.1|.1.0.0|.0.9.9|.0.9.8)"
|
|
||||||
when defined(macosx):
|
when defined(macosx):
|
||||||
const
|
const
|
||||||
DLLSSLName = "libssl" & versions & ".dylib"
|
DLLSSLName = "libssl" & versions & ".dylib"
|
||||||
|
|
@ -140,6 +151,7 @@ const
|
||||||
SSL_OP_NO_SSLv2* = 0x01000000
|
SSL_OP_NO_SSLv2* = 0x01000000
|
||||||
SSL_OP_NO_SSLv3* = 0x02000000
|
SSL_OP_NO_SSLv3* = 0x02000000
|
||||||
SSL_OP_NO_TLSv1* = 0x04000000
|
SSL_OP_NO_TLSv1* = 0x04000000
|
||||||
|
SSL_OP_NO_TLSv1_1* = 0x08000000
|
||||||
SSL_OP_ALL* = 0x000FFFFF
|
SSL_OP_ALL* = 0x000FFFFF
|
||||||
SSL_VERIFY_NONE* = 0x00000000
|
SSL_VERIFY_NONE* = 0x00000000
|
||||||
SSL_VERIFY_PEER* = 0x00000001
|
SSL_VERIFY_PEER* = 0x00000001
|
||||||
|
|
@ -191,16 +203,39 @@ const
|
||||||
|
|
||||||
proc TLSv1_method*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
|
proc TLSv1_method*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
|
|
||||||
when compileOption("dynlibOverride", "ssl"):
|
# TLS_method(), TLS_server_method(), TLS_client_method() are introduced in 1.1.0
|
||||||
proc SSL_library_init*(): cint {.cdecl, dynlib: DLLSSLName, importc, discardable.}
|
# and support SSLv3, TLSv1, TLSv1.1 and TLSv1.2
|
||||||
proc SSL_load_error_strings*() {.cdecl, dynlib: DLLSSLName, importc.}
|
# SSLv23_method(), SSLv23_server_method(), SSLv23_client_method() are removed in 1.1.0
|
||||||
proc SSLv23_client_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
|
||||||
|
|
||||||
proc SSLv23_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
when compileOption("dynlibOverride", "ssl"):
|
||||||
|
# Static linking
|
||||||
|
proc OPENSSL_init_ssl*(opts: uint64, settings: uint8): cint {.cdecl, dynlib: DLLSSLName, importc, discardable.}
|
||||||
|
proc SSL_library_init*(): cint {.discardable.} =
|
||||||
|
## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0
|
||||||
|
return OPENSSL_init_ssl(0.uint64, 0.uint8)
|
||||||
|
|
||||||
|
proc TLS_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
|
proc SSLv23_method*(): PSSL_METHOD =
|
||||||
|
TLS_method()
|
||||||
|
|
||||||
|
proc SSLv23_client_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
proc SSLv2_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
proc SSLv2_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
proc SSLv3_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
proc SSLv3_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
|
|
||||||
template OpenSSL_add_all_algorithms*() = discard
|
template OpenSSL_add_all_algorithms*() = discard
|
||||||
|
|
||||||
|
proc OpenSSL_version_num(): culong {.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
|
|
||||||
|
proc getOpenSSLVersion*(): culong =
|
||||||
|
## Return OpenSSL version as unsigned long
|
||||||
|
OpenSSL_version_num()
|
||||||
|
|
||||||
|
proc SSL_load_error_strings*() =
|
||||||
|
## Removed from OpenSSL 1.1.0
|
||||||
|
# This proc prevents breaking existing code calling SslLoadErrorStrings
|
||||||
|
# Static linking against OpenSSL < 1.1.0 is not supported
|
||||||
|
discard
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Here we're trying to stay compatible with openssl 1.0.* and 1.1.*. Some
|
# Here we're trying to stay compatible with openssl 1.0.* and 1.1.*. Some
|
||||||
# symbols are loaded dynamically and we don't use them if not found.
|
# symbols are loaded dynamically and we don't use them if not found.
|
||||||
|
|
@ -223,38 +258,58 @@ else:
|
||||||
if not dl.isNil:
|
if not dl.isNil:
|
||||||
result = symAddr(dl, name)
|
result = symAddr(dl, name)
|
||||||
|
|
||||||
|
proc loadPSSLMethod(method1, method2: string): PSSL_METHOD =
|
||||||
|
## Load <method1> from OpenSSL if available, otherwise <method2>
|
||||||
|
let m1 = cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym(method1))
|
||||||
|
if not m1.isNil:
|
||||||
|
return m1()
|
||||||
|
cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym(method2))()
|
||||||
|
|
||||||
proc SSL_library_init*(): cint {.discardable.} =
|
proc SSL_library_init*(): cint {.discardable.} =
|
||||||
let theProc = cast[proc(): cint {.cdecl.}](sslSym("SSL_library_init"))
|
## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0 otherwise
|
||||||
if not theProc.isNil: result = theProc()
|
## SSL_library_init
|
||||||
|
let theProc = cast[proc(opts: uint64, settings: uint8): cint {.cdecl.}](sslSym("OPENSSL_init_ssl"))
|
||||||
|
if not theProc.isNil:
|
||||||
|
return theProc(0, 0)
|
||||||
|
let olderProc = cast[proc(): cint {.cdecl.}](sslSym("SSL_library_init"))
|
||||||
|
if not olderProc.isNil: result = olderProc()
|
||||||
|
|
||||||
proc SSL_load_error_strings*() =
|
proc SSL_load_error_strings*() =
|
||||||
let theProc = cast[proc() {.cdecl.}](sslSym("SSL_load_error_strings"))
|
let theProc = cast[proc() {.cdecl.}](sslSym("SSL_load_error_strings"))
|
||||||
if not theProc.isNil: theProc()
|
if not theProc.isNil: theProc()
|
||||||
|
|
||||||
proc SSLv23_client_method*(): PSSL_METHOD =
|
proc SSLv23_client_method*(): PSSL_METHOD =
|
||||||
let theProc = cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym("SSLv23_client_method"))
|
loadPSSLMethod("SSLv23_client_method", "TLS_client_method")
|
||||||
if not theProc.isNil: result = theProc()
|
|
||||||
else: result = TLSv1_method()
|
|
||||||
|
|
||||||
proc SSLv23_method*(): PSSL_METHOD =
|
proc SSLv23_method*(): PSSL_METHOD =
|
||||||
let theProc = cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym("SSLv23_method"))
|
loadPSSLMethod("SSLv23_method", "TLS_method")
|
||||||
if not theProc.isNil: result = theProc()
|
|
||||||
else: result = TLSv1_method()
|
|
||||||
|
|
||||||
proc SSLv2_method*(): PSSL_METHOD =
|
proc SSLv2_method*(): PSSL_METHOD =
|
||||||
let theProc = cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym("SSLv2_method"))
|
loadPSSLMethod("SSLv2_method", "TLS_method")
|
||||||
if not theProc.isNil: result = theProc()
|
|
||||||
else: result = TLSv1_method()
|
|
||||||
|
|
||||||
proc SSLv3_method*(): PSSL_METHOD =
|
proc SSLv3_method*(): PSSL_METHOD =
|
||||||
let theProc = cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym("SSLv3_method"))
|
loadPSSLMethod("SSLv3_method", "TLS_method")
|
||||||
if not theProc.isNil: result = theProc()
|
|
||||||
else: result = TLSv1_method()
|
proc TLS_method*(): PSSL_METHOD =
|
||||||
|
loadPSSLMethod("TLS_method", "SSLv23_method")
|
||||||
|
|
||||||
|
proc TLS_client_method*(): PSSL_METHOD =
|
||||||
|
loadPSSLMethod("TLS_client_method", "SSLv23_client_method")
|
||||||
|
|
||||||
|
proc TLS_server_method*(): PSSL_METHOD =
|
||||||
|
loadPSSLMethod("TLS_server_method", "SSLv23_server_method")
|
||||||
|
|
||||||
proc OpenSSL_add_all_algorithms*() =
|
proc OpenSSL_add_all_algorithms*() =
|
||||||
let theProc = cast[proc() {.cdecl.}](sslSym("OPENSSL_add_all_algorithms_conf"))
|
let theProc = cast[proc() {.cdecl.}](sslSym("OPENSSL_add_all_algorithms_conf"))
|
||||||
if not theProc.isNil: theProc()
|
if not theProc.isNil: theProc()
|
||||||
|
|
||||||
|
proc getOpenSSLVersion*(): culong =
|
||||||
|
## Return OpenSSL version as unsigned long or 0 if not available
|
||||||
|
let theProc = cast[proc(): culong {.cdecl.}](sslSym("OpenSSL_version_num"))
|
||||||
|
result =
|
||||||
|
if theProc.isNil: 0.culong
|
||||||
|
else: theProc()
|
||||||
|
|
||||||
proc ERR_load_BIO_strings*(){.cdecl, dynlib: DLLUtilName, importc.}
|
proc ERR_load_BIO_strings*(){.cdecl, dynlib: DLLUtilName, importc.}
|
||||||
|
|
||||||
proc SSL_new*(context: SslCtx): SslPtr{.cdecl, dynlib: DLLSSLName, importc.}
|
proc SSL_new*(context: SslCtx): SslPtr{.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
|
|
|
||||||
36
tests/untestable/tssl.nim
Normal file
36
tests/untestable/tssl.nim
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
#
|
||||||
|
# Nim - SSL integration tests
|
||||||
|
# (c) Copyright 2017 Nim contributors
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
## Warning: this test performs external networking.
|
||||||
|
##
|
||||||
|
## Test with:
|
||||||
|
## ./bin/nim c -d:ssl -p:. -r tests/untestable/tssl.nim
|
||||||
|
## ./bin/nim c -d:ssl -p:. --dynlibOverride:ssl --passL:-lcrypto --passL:-lssl -r tests/untestable/tssl.nim
|
||||||
|
## The compilation is expected to succeed with any new/old version of OpenSSL,
|
||||||
|
## both with dynamic and static linking.
|
||||||
|
## The "howsmyssl" test is known to fail with OpenSSL < 1.1 due to insecure
|
||||||
|
## cypher suites being used.
|
||||||
|
|
||||||
|
import httpclient, os
|
||||||
|
from strutils import contains, toHex
|
||||||
|
|
||||||
|
from openssl import getOpenSSLVersion
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
echo "version: 0x" & $getOpenSSLVersion().toHex()
|
||||||
|
|
||||||
|
let client = newHttpClient()
|
||||||
|
# hacky SSL check
|
||||||
|
const url = "https://www.howsmyssl.com"
|
||||||
|
let report = client.getContent(url)
|
||||||
|
if not report.contains(">Probably Okay</span>"):
|
||||||
|
let fn = getTempDir() / "sslreport.html"
|
||||||
|
echo "SSL CHECK ERROR, see " & fn
|
||||||
|
writeFile(fn, report)
|
||||||
|
quit(1)
|
||||||
|
|
||||||
|
echo "done"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue