|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018-2020 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#if canImport(Network) |
| 16 | + |
| 17 | +import Foundation |
| 18 | +import Network |
| 19 | +import NIOSSL |
| 20 | +import NIOTransportServices |
| 21 | + |
| 22 | +internal extension TLSVersion { |
| 23 | + /// return Network framework TLS protocol version |
| 24 | + var nwTLSProtocolVersion: tls_protocol_version_t { |
| 25 | + switch self { |
| 26 | + case .tlsv1: |
| 27 | + return .TLSv10 |
| 28 | + case .tlsv11: |
| 29 | + return .TLSv11 |
| 30 | + case .tlsv12: |
| 31 | + return .TLSv12 |
| 32 | + case .tlsv13: |
| 33 | + return .TLSv13 |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +@available (macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) |
| 39 | +internal extension TLSConfiguration { |
| 40 | + |
| 41 | + /// Dispatch queue used by Network framework TLS to control certificate verification |
| 42 | + static var tlsDispatchQueue = DispatchQueue(label: "TLSDispatch") |
| 43 | + |
| 44 | + /// create NWProtocolTLS.Options for use with NIOTransportServices from the NIOSSL TLSConfiguration |
| 45 | + /// |
| 46 | + /// - Parameter queue: Dispatch queue to run `sec_protocol_options_set_verify_block` on. |
| 47 | + /// - Returns: Equivalent NWProtocolTLS Options |
| 48 | + func getNWProtocolTLSOptions() -> NWProtocolTLS.Options { |
| 49 | + let options = NWProtocolTLS.Options() |
| 50 | + |
| 51 | + // minimum TLS protocol |
| 52 | + if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) { |
| 53 | + sec_protocol_options_set_min_tls_protocol_version(options.securityProtocolOptions, self.minimumTLSVersion.nwTLSProtocolVersion) |
| 54 | + } |
| 55 | + |
| 56 | + // maximum TLS protocol |
| 57 | + if let maximumTLSVersion = self.maximumTLSVersion { |
| 58 | + if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) { |
| 59 | + sec_protocol_options_set_max_tls_protocol_version(options.securityProtocolOptions, maximumTLSVersion.nwTLSProtocolVersion) |
| 60 | + } else { |
| 61 | + precondition(self.maximumTLSVersion != nil, "TLSConfiguration.maximumTLSVersion is not supported") |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // application protocols |
| 66 | + if self.applicationProtocols.count > 0 { |
| 67 | + preconditionFailure("TLSConfiguration.applicationProtocols is not supported") |
| 68 | + } |
| 69 | + /*for applicationProtocol in self.applicationProtocols { |
| 70 | + applicationProtocol.utf8.withContiguousStorageIfAvailable { buffer in |
| 71 | + guard let opaquePointer = OpaquePointer(buffer.baseAddress) else { return } |
| 72 | + let int8Pointer = UnsafePointer<Int8>(opaquePointer) |
| 73 | + sec_protocol_options_add_tls_application_protocol(options.securityProtocolOptions, int8Pointer) |
| 74 | + } |
| 75 | + }*/ |
| 76 | + |
| 77 | + // the certificate chain |
| 78 | + if self.certificateChain.count > 0 { |
| 79 | + preconditionFailure("TLSConfiguration.certificateChain is not supported") |
| 80 | + } |
| 81 | + |
| 82 | + // cipher suites |
| 83 | + if self.cipherSuites.count > 0 { |
| 84 | + //preconditionFailure("TLSConfiguration.cipherSuites is not supported") |
| 85 | + } |
| 86 | + |
| 87 | + // key log callback |
| 88 | + if let _ = self.keyLogCallback { |
| 89 | + preconditionFailure("TLSConfiguration.keyLogCallback is not supported") |
| 90 | + } |
| 91 | + |
| 92 | + // private key |
| 93 | + if let _ = self.privateKey { |
| 94 | + preconditionFailure("TLSConfiguration.privateKey is not supported") |
| 95 | + } |
| 96 | + |
| 97 | + // renegotiation support key is unsupported |
| 98 | + |
| 99 | + // trust roots |
| 100 | + if let trustRoots = self.trustRoots { |
| 101 | + guard case .default = trustRoots else { |
| 102 | + preconditionFailure("TLSConfiguration.trustRoots != .default is not supported") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + switch self.certificateVerification { |
| 107 | + case .none: |
| 108 | + // add verify block to control certificate verification |
| 109 | + sec_protocol_options_set_verify_block(options.securityProtocolOptions, { (sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in |
| 110 | + //let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue() |
| 111 | + //var error: CFError? |
| 112 | + //if SecTrustEvaluateWithError(trust, &error) { |
| 113 | + sec_protocol_verify_complete(true) |
| 114 | + }, TLSConfiguration.tlsDispatchQueue) |
| 115 | + |
| 116 | + case .noHostnameVerification: |
| 117 | + precondition(self.certificateVerification != .noHostnameVerification, "TLSConfiguration.certificateVerification = .noHostnameVerification is not supported") |
| 118 | + |
| 119 | + case .fullVerification: |
| 120 | + break |
| 121 | + } |
| 122 | + |
| 123 | + return options |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#endif |
0 commit comments