### Motivation
`BoringSSLPassphraseCallbackManager.invoke` copied the passphrase into
BoringSSL's buffer with
`CChar($0)`. That is a value-preserving conversion. Where `CChar` is
signed, such as on Apple
platforms, any byte of 0x80 or more traps with "Not enough bits to
represent the passed value".
So any non-ASCII passphrase, for example "Heß", crashed the process
instead of loading the key.
This affects `NIOSSLPrivateKey(file:format:passphraseCallback:)`,
`NIOSSLPrivateKey(bytes:format:passphraseCallback:)`, and the
`NIOSSLContext` passphrase callback.
### Modifications
- Copy each byte with `CChar(truncatingIfNeeded:)`, which keeps the bit
pattern. I used this
instead of `CChar(bitPattern:)`. Both produce the same bits, but
`truncatingIfNeeded` is
generic, so it would also compile if `CChar` were ever `UInt8`.
`UInt8(bitPattern:)` only
takes an `Int8`. Happy to switch to `bitPattern` if you prefer it.
- Add tests:
- Load a key encrypted by the OpenSSL CLI with "Heß" (UTF-8 `48 65 C3
9F`), from memory
and from a file.
- Reject a wrong non-ASCII passphrase.
- Check that the callback copies all byte values 1 to 255 bit for bit,
and handles the
buffer limit.
- Round trip keys written with a multi-byte UTF-8 passphrase and with
the raw one-byte
passphrases `[0x80]` and `[0xFF]`.
### Result
Private keys protected by non-ASCII passphrases now load instead of
crashing.
### Testing
- Before the fix, each new test crashes with the trap above. After the
fix, all pass.
- `swift test` passes on macOS with Swift 6.3.3: 351 tests, 0 failures.
- A strict build with the Linux 6.3 CI flags adds no new errors. The
release build, the
API breakage check (no breaking changes) and `test_01_renegotiation`
also pass locally.
### Before / After
Before (left, error in red): loading a key with the passphrase "Heß"
through `NIOSSLPrivateKey(bytes:format:passphraseCallback:)` crashes
with "Not enough bits to represent the passed value" (exit 133).
After (right, fix in green): the same call loads the key, and it matches
the unencrypted key (exit 0).
https://github.com/user-attachments/assets/33e5a137-9d53-4e62-bf43-feccd56cf139
Fixes #618
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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 |