Skip to content

Commit e68a6ea

Browse files
authored
openssl 3 support no longer opt in + some 1.0 support (#20668)
* Revert "Add OpenSSL 3 support (#19814)" This reverts commit 2dcfd73. * openssl 3 support no longer opt in + some 1.0 support * hopefully fix * maybe fix * final attempt * actual fix hopefully
1 parent 27896ed commit e68a6ea

File tree

4 files changed

+103
-53
lines changed

4 files changed

+103
-53
lines changed

.github/workflows/ci_packages.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ jobs:
4646
valgrind libc6-dbg libblas-dev xorg-dev
4747
- name: 'Install dependencies (macOS)'
4848
if: runner.os == 'macOS'
49-
run: |
50-
brew install boehmgc make sfml gtk+3
49+
run: brew install boehmgc make sfml gtk+3
5150
- name: 'Install dependencies (Windows)'
5251
if: runner.os == 'Windows'
5352
shell: bash
@@ -71,5 +70,4 @@ jobs:
7170

7271
- name: 'koch, Run CI'
7372
shell: bash
74-
run: |
75-
. ci/funs.sh && nimInternalBuildKochAndRunCI
73+
run: . ci/funs.sh && nimInternalBuildKochAndRunCI

changelog.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@
7878

7979
- Removed the `nimIncrSeqV3` define.
8080

81-
- Static linking against OpenSSL versions below 1.1, previously done by
82-
setting `-d:openssl10`, is no longer supported.
83-
8481
- `macros.getImpl` for `const` symbols now returns the full definition node
8582
(as `nnkConstDef`) rather than the AST of the constant value.
8683

@@ -98,7 +95,7 @@
9895
## Standard library additions and changes
9996

10097
[//]: # "Changes:"
101-
- OpenSSL version 3 is now supported by setting either `-d:sslVersion=3` or `-d:useOpenssl3`.
98+
- OpenSSL 3 is now supported.
10299
- `macros.parseExpr` and `macros.parseStmt` now accept an optional
103100
filename argument for more informative errors.
104101
- Module `colors` expanded with missing colors from the CSS color standard.

lib/pure/net.nim

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,8 @@ when defineSsl:
624624
caDir = "", caFile = ""): SslContext =
625625
## Creates an SSL context.
626626
##
627-
## protVersion is currently unsed.
627+
## Protocol version is currently ignored by default and TLS is used.
628+
## With `-d:openssl10`, only SSLv23 and TLSv1 may be used.
628629
##
629630
## There are three options for verify mode:
630631
## `CVerifyNone`: certificates are not verified;
@@ -651,7 +652,19 @@ when defineSsl:
651652
## or using ECDSA:
652653
## - `openssl ecparam -out mykey.pem -name secp256k1 -genkey`
653654
## - `openssl req -new -key mykey.pem -x509 -nodes -days 365 -out mycert.pem`
654-
let mtd = TLS_method()
655+
var mtd: PSSL_METHOD
656+
when defined(openssl10):
657+
case protVersion
658+
of protSSLv23:
659+
mtd = SSLv23_method()
660+
of protSSLv2:
661+
raiseSSLError("SSLv2 is no longer secure and has been deprecated, use protSSLv23")
662+
of protSSLv3:
663+
raiseSSLError("SSLv3 is no longer secure and has been deprecated, use protSSLv23")
664+
of protTLSv1:
665+
mtd = TLSv1_method()
666+
else:
667+
mtd = TLS_method()
655668
if mtd == nil:
656669
raiseSSLError("Failed to create TLS context")
657670
var newCTX = SSL_CTX_new(mtd)

lib/wrappers/openssl.nim

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
## OpenSSL wrapper. Supports OpenSSL >= 1.1.0 dynamically (as default) or statically linked
1111
## using `--dynlibOverride:ssl`.
1212
##
13-
## To use openSSL 3, either set `-d:sslVersion=3` or `-d:useOpenssl3`.
13+
## `-d:sslVersion=1.2.3` can be used to force an SSL version.
14+
## This version must be included in the library name.
15+
## `-d:useOpenssl3` may be set for OpenSSL 3 instead.
16+
##
17+
## There is also limited support for OpenSSL 1.0.x which may require `-d:openssl10`.
1418
##
1519
## Build and test examples:
1620
##
@@ -59,7 +63,7 @@ when sslVersion != "":
5963
from posix import SocketHandle
6064

6165
elif useWinVersion:
62-
when defined(nimOldDlls):
66+
when defined(openssl10) or defined(nimOldDlls):
6367
when defined(cpu64):
6468
const
6569
DLLSSLName* = "(ssleay32|ssleay64).dll"
@@ -276,40 +280,60 @@ proc TLSv1_method*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
276280
# and support SSLv3, TLSv1, TLSv1.1 and TLSv1.2
277281
# SSLv23_method(), SSLv23_server_method(), SSLv23_client_method() are removed in 1.1.0
278282

279-
when compileOption("dynlibOverride", "ssl"):
283+
when compileOption("dynlibOverride", "ssl") or defined(noOpenSSLHacks):
280284
# Static linking
281-
when not useOpenssl3:
285+
286+
when defined(openssl10):
287+
proc SSL_library_init*(): cint {.cdecl, dynlib: DLLSSLName, importc, discardable.}
288+
proc SSL_load_error_strings*() {.cdecl, dynlib: DLLSSLName, importc.}
289+
proc SSLv23_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
290+
proc SSLeay(): culong {.cdecl, dynlib: DLLUtilName, importc.}
291+
292+
proc getOpenSSLVersion*(): culong =
293+
SSLeay()
294+
295+
proc ERR_load_BIO_strings*() {.cdecl, dynlib: DLLUtilName, importc.}
296+
else:
282297
proc OPENSSL_init_ssl*(opts: uint64, settings: uint8): cint {.cdecl, dynlib: DLLSSLName, importc, discardable.}
283298
proc SSL_library_init*(): cint {.discardable.} =
284299
## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0
285300
return OPENSSL_init_ssl(0.uint64, 0.uint8)
286301

287-
proc TLS_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
302+
proc TLS_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
303+
proc SSLv23_method*(): PSSL_METHOD =
304+
TLS_method()
288305

289-
proc OpenSSL_version_num(): culong {.cdecl, dynlib: DLLUtilName, importc.}
306+
proc OpenSSL_version_num(): culong {.cdecl, dynlib: DLLUtilName, importc.}
290307

291-
proc getOpenSSLVersion*(): culong =
292-
## Return OpenSSL version as unsigned long
293-
OpenSSL_version_num()
308+
proc getOpenSSLVersion*(): culong =
309+
## Return OpenSSL version as unsigned long
310+
OpenSSL_version_num()
294311

295-
proc SSL_load_error_strings*() =
296-
## Removed from OpenSSL 1.1.0
297-
# This proc prevents breaking existing code calling SslLoadErrorStrings
298-
# Static linking against OpenSSL < 1.1.0 is not supported
299-
discard
312+
proc SSL_load_error_strings*() =
313+
## Removed from OpenSSL 1.1.0
314+
# This proc prevents breaking existing code calling SslLoadErrorStrings
315+
# Static linking against OpenSSL < 1.1.0 is not supported
316+
discard
300317

301-
when defined(libressl):
318+
proc ERR_load_BIO_strings*() =
319+
discard
320+
321+
when defined(libressl) or defined(openssl10):
302322
proc SSL_state(ssl: SslPtr): cint {.cdecl, dynlib: DLLSSLName, importc.}
303323
proc SSL_in_init*(ssl: SslPtr): cint {.inline.} =
304-
SSl_state(ssl) and SSL_ST_INIT
324+
SSL_state(ssl) and SSL_ST_INIT
305325
else:
306326
proc SSL_in_init*(ssl: SslPtr): cint {.cdecl, dynlib: DLLSSLName, importc.}
307327
proc SSL_CTX_set_ciphersuites*(ctx: SslCtx, str: cstring): cint {.cdecl, dynlib: DLLSSLName, importc.}
308328

309329
template OpenSSL_add_all_algorithms*() = discard
310330

331+
proc SSLv23_client_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
332+
proc SSLv2_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
333+
proc SSLv3_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
334+
311335
else:
312-
# Here we're trying to stay compatible with openssl 1.1.*. Some
336+
# Here we're trying to stay compatible between openssl versions. Some
313337
# symbols are loaded dynamically and we don't use them if not found.
314338
proc thisModule(): LibHandle {.inline.} =
315339
var thisMod {.global.}: LibHandle
@@ -367,29 +391,47 @@ else:
367391
let method2Proc = cast[proc(): PSSL_METHOD {.cdecl, gcsafe, raises: [].}](methodSym)
368392
return method2Proc()
369393

370-
when not useOpenssl3:
371-
proc SSL_library_init*(): cint {.discardable.} =
372-
## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0 otherwise
373-
## SSL_library_init
374-
let newInitSym = sslSymNullable("OPENSSL_init_ssl")
375-
if not newInitSym.isNil:
376-
let newInitProc =
377-
cast[proc(opts: uint64, settings: uint8): cint {.cdecl.}](newInitSym)
378-
return newInitProc(0, 0)
379-
let olderProc = cast[proc(): cint {.cdecl.}](sslSymThrows("SSL_library_init"))
380-
if not olderProc.isNil: result = olderProc()
394+
proc SSL_library_init*(): cint {.discardable.} =
395+
## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0 otherwise
396+
## SSL_library_init
397+
let newInitSym = sslSymNullable("OPENSSL_init_ssl")
398+
if not newInitSym.isNil:
399+
let newInitProc =
400+
cast[proc(opts: uint64, settings: uint8): cint {.cdecl.}](newInitSym)
401+
return newInitProc(0, 0)
402+
let olderProc = cast[proc(): cint {.cdecl.}](sslSymThrows("SSL_library_init"))
403+
if not olderProc.isNil: result = olderProc()
381404

382405
proc SSL_load_error_strings*() =
383406
# TODO: Are we ignoring this on purpose? SSL GitHub CI fails otherwise.
384407
let theProc = cast[proc() {.cdecl.}](sslSymNullable("SSL_load_error_strings"))
385408
if not theProc.isNil: theProc()
386409

410+
proc ERR_load_BIO_strings*() =
411+
let theProc = cast[proc() {.cdecl.}](utilModule().symNullable("ERR_load_BIO_strings"))
412+
if not theProc.isNil: theProc()
413+
414+
proc SSLv23_client_method*(): PSSL_METHOD =
415+
loadPSSLMethod("SSLv23_client_method", "TLS_client_method")
416+
417+
proc SSLv23_method*(): PSSL_METHOD =
418+
loadPSSLMethod("SSLv23_method", "TLS_method")
419+
420+
proc SSLv2_method*(): PSSL_METHOD =
421+
loadPSSLMethod("SSLv2_method", "TLS_method")
422+
387423
proc SSLv3_method*(): PSSL_METHOD =
388424
loadPSSLMethod("SSLv3_method", "TLS_method")
389425

390426
proc TLS_method*(): PSSL_METHOD =
391427
loadPSSLMethod("TLS_method", "SSLv23_method")
392428

429+
proc TLS_client_method*(): PSSL_METHOD =
430+
loadPSSLMethod("TLS_client_method", "SSLv23_client_method")
431+
432+
proc TLS_server_method*(): PSSL_METHOD =
433+
loadPSSLMethod("TLS_server_method", "SSLv23_server_method")
434+
393435
proc OpenSSL_add_all_algorithms*() =
394436
# TODO: Are we ignoring this on purpose? SSL GitHub CI fails otherwise.
395437
let theProc = cast[proc() {.cdecl.}](sslSymNullable("OPENSSL_add_all_algorithms_conf"))
@@ -423,11 +465,6 @@ else:
423465
theProc = cast[typeof(theProc)](sslSymThrows("SSL_CTX_set_ciphersuites"))
424466
theProc(ctx, str)
425467

426-
proc ERR_load_BIO_strings*(){.cdecl, dynlib: DLLUtilName, importc.}
427-
428-
proc TLS_client_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
429-
430-
431468
proc SSL_new*(context: SslCtx): SslPtr{.cdecl, dynlib: DLLSSLName, importc.}
432469
proc SSL_free*(ssl: SslPtr){.cdecl, dynlib: DLLSSLName, importc.}
433470
proc SSL_get_SSL_CTX*(ssl: SslPtr): SslCtx {.cdecl, dynlib: DLLSSLName, importc.}
@@ -535,8 +572,9 @@ const
535572
useNimsAlloc = not defined(nimNoAllocForSSL) and not defined(gcDestructors)
536573

537574
when not useWinVersion and not defined(macosx) and not defined(android) and useNimsAlloc:
538-
proc CRYPTO_set_mem_functions(a,b,c: pointer){.cdecl,
539-
dynlib: DLLUtilName, importc.}
575+
proc CRYPTO_set_mem_functions(a,b,c: pointer) =
576+
let theProc = cast[proc(a,b,c: pointer) {.cdecl.}](utilModule().symNullable("CRYPTO_set_mem_functions"))
577+
if not theProc.isNil: theProc(a, b, c)
540578

541579
proc allocWrapper(size: int): pointer {.cdecl.} = allocShared(size)
542580
proc reallocWrapper(p: pointer; newSize: int): pointer {.cdecl.} =
@@ -547,9 +585,11 @@ when not useWinVersion and not defined(macosx) and not defined(android) and useN
547585
proc deallocWrapper(p: pointer) {.cdecl.} =
548586
if p != nil: deallocShared(p)
549587

550-
proc CRYPTO_malloc_init*() =
551-
when not useWinVersion and not defined(macosx) and not defined(android) and useNimsAlloc:
588+
proc CRYPTO_malloc_init*() =
552589
CRYPTO_set_mem_functions(allocWrapper, reallocWrapper, deallocWrapper)
590+
else:
591+
proc CRYPTO_malloc_init*() =
592+
discard
553593

554594
proc SSL_CTX_ctrl*(ctx: SslCtx, cmd: cint, larg: clong, parg: pointer): clong{.
555595
cdecl, dynlib: DLLSSLName, importc.}
@@ -792,17 +832,19 @@ when defined(nimHasStyleChecks):
792832
# On old openSSL version some of these symbols are not available
793833
when not defined(nimDisableCertificateValidation) and not defined(windows):
794834

795-
# proc SSL_get_peer_certificate*(ssl: SslCtx): PX509 =
796-
# loadPSSLMethod("SSL_get_peer_certificate", "SSL_get1_peer_certificate")
797-
835+
# SSL_get_peer_certificate removed in 3.0
836+
# SSL_get1_peer_certificate added in 3.0
798837
when useOpenssl3:
799838
proc SSL_get1_peer_certificate*(ssl: SslCtx): PX509 {.cdecl, dynlib: DLLSSLName, importc.}
800839
proc SSL_get_peer_certificate*(ssl: SslCtx): PX509 =
801840
SSL_get1_peer_certificate(ssl)
802-
803841
else:
804-
proc SSL_get_peer_certificate*(ssl: SslCtx): PX509 {.cdecl, dynlib: DLLSSLName, importc.}
805-
842+
proc SSL_get_peer_certificate*(ssl: SslCtx): PX509 =
843+
let methodSym = sslSymNullable("SSL_get_peer_certificate", "SSL_get1_peer_certificate")
844+
if methodSym.isNil:
845+
raise newException(LibraryError, "Could not load SSL_get_peer_certificate or SSL_get1_peer_certificate")
846+
let method2Proc = cast[proc(ssl: SslCtx): PX509 {.cdecl, gcsafe, raises: [].}](methodSym)
847+
return method2Proc(ssl)
806848

807849
proc X509_get_subject_name*(a: PX509): PX509_NAME{.cdecl, dynlib: DLLSSLName, importc.}
808850

0 commit comments

Comments
 (0)