Files
swift-nio-ssl/Sources/NIOOpenSSL/SSLInit.swift
T
Cory Benfield 1bf8870915 Support OpenSSL 1.1 and later. (#20)
Motivation:

Currently there are no Swift.org distributions of Swift for Linux that
link against an OpenSSL 1.1 release, but with the recent release of
Ubuntu 18.04, the next LTS release of Ubuntu, it seems reasonable that
at some point such a release will exist.

Given that OpenSSL 1.1 is substantially better than OpenSSL 1.0, we
should endeavour to allow our users to take advantage of this new support
and compile against this newer OpenSSL.

*Unfortunately*, OpenSSL's specific source change is not well-tolerated
by Swift code. There are a number of reasons for this, mostly explained
in https://forums.swift.org/t/opaque-pointers-in-swift/6875. The end
result of this is that we require substantial, wide-ranging changes to the
implementation in order to successfully compile against both OpenSSL
versions 1.0.X and 1.1.X.

This patch contains those wide ranging modifications. The most profound
of them is to use the compiler to essentially convert between arbitrary
pointer types without writing the names of those types down.
Unfortunately this totally escapes the Swift type system, which is
substantially less than ideal. However, in the current mode where opaque
C structures are unityped in Swift it is really the best we can do.

Modifications:

- Supported converting all pointer types to OpaquePointer.
- Added conversions absolutely everywhere.
- Fixed a few problems with callbacks.
- Moved OpenSSL initialization to C to support both macros and
  functions.
- Made locking callbacks optional.
- Fixed tests that relied on OpenSSL 1.0.2 behaviour of ALPN failure
  to now check OpenSSL version before they assume an outcome.

Result:

Compile against OpenSSL 1.1.0 at the very least.
2018-09-13 09:27:35 +02:00

39 lines
1.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CNIOOpenSSL
/// Initialize OpenSSL. Note that this function IS NOT THREAD SAFE, and so must be called inside
/// either an explicit or implicit dispatch_once.
func initializeOpenSSL() -> Bool {
// Initializing OpenSSL is made up of two steps. First, we need to load all the various crypto bits.
CNIOOpenSSL_InitializeOpenSSL()
// Now we need to set up our custom BIO.
setupBIO()
return true
}
/// Called to initialize our custom OpenSSL BIO.
private func setupBIO() {
/// If the BIO is already initialized, don't do it again.
guard CNIOOpenSSL_ByteBufferBIOType == 0 else {
return
}
CNIOOpenSSL_initByteBufferBIO(openSSLBIOWriteFunc, openSSLBIOReadFunc,
openSSLBIOPutsFunc, openSSLBIOGetsFunc,
openSSLBIOCtrlFunc, openSSLBIOCreateFunc,
openSSLBIODestroyFunc)
}