aryan-25 52f83a14ee Fix optional certificate verification test case (#578)
Motivation:

The
[`testMacOSConnectionFailsIfServerVerificationOptionalAndPeerPresentsUntrustedCert`](https://github.com/apple/swift-nio-ssl/blob/3f337058ccd7243c4cac7911477d8ad4c598d4da/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift#L2603)
test case in `NIOSSLIntegrationTest` verifies that the handshake fails
when the server's certificate verification mode is set to
[`.optionalVerification`](https://github.com/apple/swift-nio-ssl/blob/3f337058ccd7243c4cac7911477d8ad4c598d4da/Sources/NIOSSL/TLSConfiguration.swift#L199)
and the client presents an untrusted certificate chain.

In that test case, there is a `defer` block in which the client channel
is closed:

https://github.com/apple/swift-nio-ssl/blob/3f337058ccd7243c4cac7911477d8ad4c598d4da/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift#L2650-L2652

However, since the handshake will fail in this test case, the
`doDecodeData` method will close the client channel (line 630):

https://github.com/apple/swift-nio-ssl/blob/3f337058ccd7243c4cac7911477d8ad4c598d4da/Sources/NIOSSL/NIOSSLHandler.swift#L626-L634

If the client channel has already closed before the test case calls `try
clientChannel.close().wait()`, then a
[`ChannelError.alreadyClosed`](https://github.com/apple/swift-nio/blob/32e3d863795292c49e1aa3dc1f72a31802e74a1f/Sources/NIOCore/Channel.swift#L377)
will be thrown, which will result in the `XCTAssertNoThrow` failing.

We should instead be waiting for the client channel's `closeFuture` at
the end of the test case.

Modifications:

Removed the `defer` block containing the call to `try
clientChannel.close().wait()`, and instead added a call to `try
clientChannel.closeFuture.wait()` at the end of the test case.

Result:

The
`testMacOSConnectionFailsIfServerVerificationOptionalAndPeerPresentsUntrustedCert`
test case is no longer flaky.
2026-04-23 13:38:05 +01:00
2026-04-07 12:07:47 +00:00
2026-04-07 12:07:47 +00:00
2024-11-15 09:52:50 +00:00
2020-03-23 16:26:05 +00:00
2024-10-25 09:32:23 +01:00
2023-06-06 09:06:58 -07:00
2018-01-24 12:13:14 +00:00
2018-01-24 12:13:14 +00:00
2019-02-25 16:52:26 +00:00
2026-04-07 12:07:47 +00:00
2021-03-09 11:14:07 +00:00

SwiftNIO SSL

SwiftNIO SSL is a Swift package that contains an implementation of TLS based on BoringSSL. This package allows users of SwiftNIO to write protocol clients and servers that use TLS to secure data in flight.

The name is inspired primarily by the names of the library this package uses (BoringSSL), and not because we don't know the name of the protocol. We know the protocol is TLS!

To get started, check out the API docs.

Using SwiftNIO SSL

SwiftNIO SSL provides two ChannelHandlers to use to secure a data stream: the NIOSSLClientHandler and the NIOSSLServerHandler. Each of these can be added to a Channel to secure the communications on that channel.

Additionally, we provide a number of low-level primitives for configuring your TLS connections. These will be shown below.

To secure a server connection, you will need a X.509 certificate chain in a file (either PEM or DER, but PEM is far easier), and the associated private key for the leaf certificate. These objects can then be wrapped up in a TLSConfiguration object that is used to initialize the ChannelHandler.

For example:

let configuration = TLSConfiguration.makeServerConfiguration(
    certificateChain: try NIOSSLCertificate.fromPEMFile("cert.pem").map { .certificate($0) },
    privateKey: try .privateKey(.init(file: "key.pem", format: .pem))
)
let sslContext = try NIOSSLContext(configuration: configuration)

let server = ServerBootstrap(group: group)
    .childChannelInitializer { channel in
        // important: The handler must be initialized _inside_ the `childChannelInitializer`
        let handler = NIOSSLServerHandler(context: sslContext)

        [...]
        channel.pipeline.syncOperations.addHandler(handler)
        [...]
    }

For clients, it is a bit simpler as there is no need to have a certificate chain or private key (though clients may have these things). Setup for clients may be done like this:

let configuration = TLSConfiguration.makeClientConfiguration()
let sslContext = try NIOSSLContext(configuration: configuration)

let client = ClientBootstrap(group: group)
    .channelInitializer { channel in
        // important: The handler must be initialized _inside_ the `channelInitializer`
        let handler = try NIOSSLClientHandler(context: sslContext)

        [...]
        channel.pipeline.syncOperations.addHandler(handler)
        [...]
    }

The most recent versions of SwiftNIO SSL support Swift 5.7 and newer. The minimum Swift version supported by SwiftNIO SSL releases are detailed below:

SwiftNIO SSL Minimum Swift Version
2.0.0 ..< 2.14.0 5.0
2.14.0 ..< 2.19.0 5.2
2.19.0 ..< 2.23.0 5.4
2.23.0 ..< 2.23.2 5.5.2
2.23.2 ..< 2.26.0 5.6
2.26.0 ..< 2.27.0 5.7
2.27.0 ..< 2.29.3 5.8
2.29.3 ..< 2.31.0 5.9
2.31.0 ..< 2.35.0 5.10
2.35.0 ..< 2.37.0 6.0
2.37.0 ..< 6.1
S
Description
Mirror of github.com/apple/swift-nio-ssl
Readme Apache-2.0
19 MiB
0 Stars 1 Watchers 0 Forks
Languages
Assembly 33.1%
C++ 31.1%
C 26.5%
Swift 6.6%
NASL 1.8%
Other 0.9%