-
Notifications
You must be signed in to change notification settings - Fork 125
Add NIOTransportServices TLS Configuration options #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
Sources/AsyncHTTPClient/NIOTransportServices/TSTLSConfiguration.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(Network) | ||
|
||
import Network | ||
|
||
extension tls_protocol_version_t { | ||
var sslProtocol: SSLProtocol { | ||
switch self { | ||
case .TLSv10: | ||
return .tlsProtocol1 | ||
case .TLSv11: | ||
return .tlsProtocol11 | ||
case .TLSv12: | ||
return .tlsProtocol12 | ||
case .TLSv13: | ||
return .tlsProtocol13 | ||
case .DTLSv10: | ||
return .dtlsProtocol1 | ||
case .DTLSv12: | ||
return .dtlsProtocol12 | ||
@unknown default: | ||
return .tlsProtocol1 | ||
} | ||
} | ||
} | ||
|
||
/// Certificate verification modes. | ||
public enum TSCertificateVerification { | ||
/// All certificate verification disabled. | ||
case none | ||
|
||
/// Certificates will be validated against the trust store and checked | ||
/// against the hostname of the service we are contacting. | ||
case fullVerification | ||
} | ||
|
||
/// TLS configuration for NIO Transport Services | ||
public struct TSTLSConfiguration { | ||
/// The minimum TLS version to allow in negotiation. Defaults to tlsv1. | ||
public var minimumTLSVersion: tls_protocol_version_t | ||
|
||
/// The maximum TLS version to allow in negotiation. If nil, there is no upper limit. Defaults to nil. | ||
public var maximumTLSVersion: tls_protocol_version_t? | ||
|
||
/// The trust roots to use to validate certificates. This only needs to be provided if you intend to validate | ||
/// certificates. | ||
public var trustRoots: [SecCertificate]? | ||
|
||
/// The identity associated with the leaf certificate. | ||
public var clientIdentity: SecIdentity? | ||
|
||
/// The application protocols to use in the connection. | ||
public var applicationProtocols: [String] | ||
|
||
/// Whether to verify remote certificates. | ||
public var certificateVerification: TSCertificateVerification | ||
|
||
/// Initialize TSTLSConfiguration | ||
public init( | ||
minimumTLSVersion: tls_protocol_version_t = .TLSv10, | ||
maximumTLSVersion: tls_protocol_version_t? = nil, | ||
trustRoots: [SecCertificate]? = nil, | ||
clientIdentity: SecIdentity? = nil, | ||
applicationProtocols: [String] = [], | ||
certificateVerification: TSCertificateVerification = .fullVerification | ||
) { | ||
self.minimumTLSVersion = minimumTLSVersion | ||
self.maximumTLSVersion = maximumTLSVersion | ||
self.trustRoots = trustRoots | ||
self.clientIdentity = clientIdentity | ||
self.applicationProtocols = applicationProtocols | ||
self.certificateVerification = certificateVerification | ||
} | ||
} | ||
|
||
@available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) | ||
extension TSTLSConfiguration { | ||
func getNWProtocolTLSOptions() -> NWProtocolTLS.Options { | ||
let options = NWProtocolTLS.Options() | ||
|
||
// minimum TLS protocol | ||
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) { | ||
sec_protocol_options_set_min_tls_protocol_version(options.securityProtocolOptions, self.minimumTLSVersion) | ||
} else { | ||
sec_protocol_options_set_tls_min_version(options.securityProtocolOptions, self.minimumTLSVersion.sslProtocol) | ||
} | ||
|
||
// maximum TLS protocol | ||
if let maximumTLSVersion = self.maximumTLSVersion { | ||
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) { | ||
sec_protocol_options_set_max_tls_protocol_version(options.securityProtocolOptions, maximumTLSVersion) | ||
} else { | ||
sec_protocol_options_set_tls_max_version(options.securityProtocolOptions, maximumTLSVersion.sslProtocol) | ||
} | ||
} | ||
|
||
// set client identity | ||
if let clientIdentity = self.clientIdentity, let secClientIdentity = sec_identity_create(clientIdentity) { | ||
sec_protocol_options_set_local_identity(options.securityProtocolOptions, secClientIdentity) | ||
} | ||
|
||
// add application protocols | ||
self.applicationProtocols.forEach { | ||
sec_protocol_options_add_tls_application_protocol(options.securityProtocolOptions, $0) | ||
} | ||
|
||
if self.certificateVerification != .fullVerification || self.trustRoots != nil { | ||
// add verify block to control certificate verification | ||
sec_protocol_options_set_verify_block( | ||
options.securityProtocolOptions, | ||
{ _, sec_trust, sec_protocol_verify_complete in | ||
guard self.certificateVerification != .none else { | ||
sec_protocol_verify_complete(true) | ||
return | ||
} | ||
|
||
let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue() | ||
if let trustRootCertificates = trustRoots { | ||
SecTrustSetAnchorCertificates(trust, trustRootCertificates as CFArray) | ||
} | ||
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) { | ||
SecTrustEvaluateAsyncWithError(trust, Self.tlsDispatchQueue) { _, result, _ in | ||
sec_protocol_verify_complete(result) | ||
} | ||
} else { | ||
SecTrustEvaluateAsync(trust, Self.tlsDispatchQueue) { _, result in | ||
switch result { | ||
case .proceed, .unspecified: | ||
sec_protocol_verify_complete(true) | ||
default: | ||
sec_protocol_verify_complete(false) | ||
} | ||
} | ||
} | ||
}, Self.tlsDispatchQueue | ||
) | ||
} | ||
return options | ||
} | ||
|
||
/// Dispatch queue used by Network framework TLS to control certificate verification | ||
static var tlsDispatchQueue = DispatchQueue(label: "TSTLSConfiguration") | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment looks wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can shim most of it.
SecCertificate
can be created from the DER data of aNIOSSLCertificate
.SecIdentity
is the problem case. It requires a p12 or a private key already in the keychain and currently there is no way to generate this from the combination ofNIOSSLPrivateKey
andNIOSSLCertificate
unless I missed something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, SecIdentity is rough.
Perhaps this is the moment we encourage AHC to create its own representation of a TLSConfiguration that can abstract over some of these better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we expose BoringSSL pkcs#12 generation code in NIOSSL? If we have this, we can probably generate a SecIdentity from a TLSConfiguration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, though I don't love doing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want a common structure for both NIOSSL and NIOTransportServices TLS setup I can't see any other way around supplying a p12 generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think I agree. I'd like to know what some other folks prefer here: @artemredkin @ktoso?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think common structure would be preferable over implementing an additional abstraction over NIOSSL and NIOTransportServices. I wonder, can that structure live in NIOTS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@artemredkin I believe with p12 generation from a NIOSSL private key and certificate chain we can use
TLSConfiguration
to cover most situations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really want it to at this early stage.