mirror of
https://github.com/apple/swift-nio-ssl
synced 2026-11-30 05:28:46 +00:00
make TLSConfiguration.applicationProtocols a [String] (#70)
* add separate encodedApplicationProtocols method to TLSConfiguration * store application protocols in encoded format
This commit is contained in:
1 parent
f531a05167
commit
9d46aef76b
5 files changed
+32
-4
No files matched your search
@@ -6,3 +6,5 @@ Package.pins
|
||||
Package.resolved
|
||||
*.pem
|
||||
/docs
|
||||
DerivedData
|
||||
|
||||
@@ -203,7 +203,7 @@ public final class SSLContext {
|
||||
}
|
||||
|
||||
if configuration.applicationProtocols.count > 0 {
|
||||
try SSLContext.setAlpnProtocols(configuration.applicationProtocols, context: context)
|
||||
try SSLContext.setAlpnProtocols(configuration.encodedApplicationProtocols, context: context)
|
||||
SSLContext.setAlpnCallback(context: context)
|
||||
}
|
||||
|
||||
@@ -248,7 +248,7 @@ public final class SSLContext {
|
||||
}
|
||||
|
||||
fileprivate func alpnSelectCallback(offeredProtocols: UnsafeBufferPointer<UInt8>) -> (index: Int, length: Int)? {
|
||||
for possibility in configuration.applicationProtocols {
|
||||
for possibility in configuration.encodedApplicationProtocols {
|
||||
let match = possibility.withUnsafeBufferPointer {
|
||||
offeredProtocols.locateAlpnIdentifier(identifier: $0)
|
||||
}
|
||||
|
||||
@@ -99,6 +99,12 @@ internal func encodeALPNIdentifier(identifier: String) -> [UInt8] {
|
||||
return encodedIdentifier
|
||||
}
|
||||
|
||||
/// Decodes a string from the wire format of an ALPN identifier. These MUST be correctly
|
||||
/// formatted ALPN identifiers, and so this routine will crash the program if they aren't.
|
||||
internal func decodeALPNIdentifier(identifier: [UInt8]) -> String {
|
||||
return String(decoding: identifier[1..<identifier.count], as: Unicode.ASCII.self)
|
||||
}
|
||||
|
||||
/// Manages configuration of OpenSSL for SwiftNIO programs.
|
||||
///
|
||||
/// OpenSSL has a number of configuration options that are worth setting. This structure allows
|
||||
@@ -134,7 +140,16 @@ public struct TLSConfiguration {
|
||||
/// strings representing the ALPN identifiers of the protocols to negotiate. For clients,
|
||||
/// the protocols will be offered in the order given. For servers, the protocols will be matched
|
||||
/// against the client's offered protocols in order.
|
||||
public var applicationProtocols: [[UInt8]]
|
||||
public var applicationProtocols: [String] {
|
||||
get {
|
||||
return self.encodedApplicationProtocols.map(decodeALPNIdentifier)
|
||||
}
|
||||
set {
|
||||
self.encodedApplicationProtocols = newValue.map(encodeALPNIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
internal var encodedApplicationProtocols: [[UInt8]]
|
||||
|
||||
private init(cipherSuites: String,
|
||||
minimumTLSVersion: TLSVersion,
|
||||
@@ -151,7 +166,8 @@ public struct TLSConfiguration {
|
||||
self.trustRoots = trustRoots
|
||||
self.certificateChain = certificateChain
|
||||
self.privateKey = privateKey
|
||||
self.applicationProtocols = applicationProtocols.map(encodeALPNIdentifier)
|
||||
self.encodedApplicationProtocols = []
|
||||
self.applicationProtocols = applicationProtocols
|
||||
}
|
||||
|
||||
/// Create a TLS configuration for use with server-side contexts.
|
||||
|
||||
@@ -33,6 +33,7 @@ extension TLSConfigurationTest {
|
||||
("testServerCannotValidateClientPostTLS13", testServerCannotValidateClientPostTLS13),
|
||||
("testMutualValidation", testMutualValidation),
|
||||
("testNonexistentFileObject", testNonexistentFileObject),
|
||||
("testComputedApplicationProtocols", testComputedApplicationProtocols),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,4 +258,13 @@ class TLSConfigurationTest: XCTestCase {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
func testComputedApplicationProtocols() throws {
|
||||
var config = TLSConfiguration.forServer(certificateChain: [], privateKey: .file("fake.file"), applicationProtocols: ["http/1.1"])
|
||||
XCTAssertEqual(config.applicationProtocols, ["http/1.1"])
|
||||
XCTAssertEqual(config.encodedApplicationProtocols, [[8, 104, 116, 116, 112, 47, 49, 46, 49]])
|
||||
config.applicationProtocols.insert("h2", at: 0)
|
||||
XCTAssertEqual(config.applicationProtocols, ["h2", "http/1.1"])
|
||||
XCTAssertEqual(config.encodedApplicationProtocols, [[2, 104, 50], [8, 104, 116, 116, 112, 47, 49, 46, 49]])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user