Skip to content
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

Actually disable hostname validation on Apple platforms when asked #502

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ extension SSLConnection {
preconditionFailure("This callback should only be used if we are using the system-default trust.")
}

let expectedHostname = self.validateHostnames ? self.expectedHostname : nil

// This force-unwrap is safe as we must have decided if we're a client or a server before validation.
var trust: SecTrust? = nil
var result: OSStatus
let policy = SecPolicyCreateSSL(self.role! == .client, self.expectedHostname as CFString?)
let policy = SecPolicyCreateSSL(self.role! == .client, expectedHostname as CFString?)
result = SecTrustCreateWithCertificates(peerCertificates as CFArray, policy, &trust)
guard result == errSecSuccess, let actualTrust = trust else {
throw NIOSSLError.unableToValidateCertificate
Expand Down
62 changes: 62 additions & 0 deletions Tests/NIOSSLTests/SecurityFrameworkVerificationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,68 @@ final class SecurityFrameworkVerificationTests: XCTestCase {
#endif
}

func testDefaultVerificationCanValidateHostname() throws {
#if canImport(Darwin)
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

let p = group.next().makePromise(of: NIOSSLVerificationResult.self)
let context = try NIOSSLContext(configuration: .makeClientConfiguration())
let connection = context.createConnection()!
connection.setConnectState()
connection.expectedHostname = "www.apple.com"

connection.performSecurityFrameworkValidation(promise: p, peerCertificates: Self.appleComCertChain)
let result = try p.futureResult.wait()

XCTAssertEqual(result, .certificateVerified)
#endif
}

func testDefaultVerificationFailsOnInvalidHostname() throws {
#if canImport(Darwin)
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

let p = group.next().makePromise(of: NIOSSLVerificationResult.self)
let context = try NIOSSLContext(configuration: .makeClientConfiguration())
let connection = context.createConnection()!
connection.setConnectState()
connection.expectedHostname = "www.swift-nio.io"

connection.performSecurityFrameworkValidation(promise: p, peerCertificates: Self.appleComCertChain)
let result = try p.futureResult.wait()

XCTAssertEqual(result, .failed)
#endif
}

func testDefaultVerificationIgnoresHostnamesWhenConfiguredTo() throws {
#if canImport(Darwin)
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

let p = group.next().makePromise(of: NIOSSLVerificationResult.self)
var configuration = TLSConfiguration.makeClientConfiguration()
configuration.certificateVerification = .noHostnameVerification
let context = try NIOSSLContext(configuration: configuration)
let connection = context.createConnection()!
connection.setConnectState()
connection.expectedHostname = "www.swift-nio.io"

connection.performSecurityFrameworkValidation(promise: p, peerCertificates: Self.appleComCertChain)
let result = try p.futureResult.wait()

XCTAssertEqual(result, .certificateVerified)
#endif
}

func testDefaultVerificationPlusAdditionalCanUseAdditionalRoot() throws {
#if canImport(Darwin)
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Expand Down
Loading