Eirik Botten Nicolaysen f7daec2a9b Accept every c_rehash collision suffix, not just .0 (#602)
`_isRehashFormat` recognises only a one-digit collision suffix, so
`openssl rehash` links from `.10` upward are not seen as c_rehash links
at all.

### The three barriers

On `main` the name check is:

```swift
guard let lastPathComponent = utf8PathSplitView.last,
    lastPathComponent.count == 10
else { return false }
let filenameParts = lastPathComponent.split(separator: UInt8(ascii: "."))

guard filenameParts.count == 2,
    let filename = filenameParts.first,
    let fileExtension = filenameParts.last,
    fileExtension.count == 1,
    filename.count == 8,
    filename.allSatisfy({ $0.isHexDigit }),
    fileExtension.first == UInt8(ascii: "0")
else { return false }
```

Three things reject a valid link independently:

1. `lastPathComponent.count == 10` — rejects any name longer than
`HHHHHHHH.D` before the extension is examined at all.
2. `fileExtension.count == 1` — rejects any suffix of more than one
digit.
3. `fileExtension.first == UInt8(ascii: "0")` — rejects every suffix
that does not begin with `0`, which for the ids OpenSSL actually writes
means everything except `.0`.

Relaxing any one of them is not enough on its own.

### Consequence

The filter lives in `loadVerifyLocations(_:context:sendCANames:)`, which
calls `_isRehashFormat` over the directory contents when `sendCANames`
is set, loads each surviving link as a PEM certificate, and passes it to
`addCACertificateNameToList` so its CA name reaches the client CA list.
(`addRootCertificate` is a different function; it adds a certificate to
the trust store and does not walk the directory.)

A CA that lost the hash race is therefore skipped, so its name is absent
from the CertificateRequest, and a client holding a certificate from
that CA has no reason to send it. The failure is quiet: nothing errors,
the handshake just proceeds without the client certificate.

### The change

- `fileExtension.allSatisfy({ $0.isDecimalDigit })` replaces the `==
"0"` comparison, mirroring the `isHexDigit` check on the line above.
`isDecimalDigit` is added to the same `UTF8.CodeUnit` extension as
`isHexDigit`, in the same shape, reusing the existing
`asciiZero`/`asciiNine` constants.
- `fileExtension.count == 1` becomes `!fileExtension.isEmpty`. Emptiness
still has to be excluded explicitly, because `allSatisfy` is vacuously
true on an empty collection.
- `lastPathComponent.count == 10` is removed rather than relaxed to `>=
10`. It is redundant once the parts are checked: `filenameParts.count ==
2`, `filename.count == 8` and a non-empty all-digits extension already
pin the name to 8 + 1 + digits. A second, looser length check would only
be another thing to keep in sync.
- The split passes `omittingEmptySubsequences: false`. This is
load-bearing. With the default behaviour `Collection.split` discards
empty pieces, so `.7f44456a.0`, `7f44456a..0` and `7f44456a.0.` collapse
to the same two parts as `7f44456a.0`. The removed `count == 10` check
had been rejecting exactly those three by accident; the explicit split
replaces that accident with an intentional rule, and the reasoning is
recorded at the call site.

No upper bound on the number of digits is imposed. The reasoning, and
the OpenSSL sources it rests on, are in a comment on this PR.

### Tests

`testRehashFormat` already asserts a non-digit extension (`.z`) is
rejected; `.a` is added next to it, since a hex-but-not-decimal
character is the case most likely to regress if someone reaches for
`isHexDigit` here.

`testRehashFormatAcceptsCollisionSuffixes` creates real symlinks and
covers:

- accepted: 0, 1, 9, 10, 123, 255.
- rejected: `.7f44456a.0`, `7f44456a..0`, `7f44456a.0.`, `7f44456a.`,
`7f44456a.0.1` and `7f44456a.r0`.

Every assertion carries a message saying why that name is in the list.
The rejected names are real symlinks to a real certificate, so the name
check is the only thing that can reject them. Only the first three
depend on the explicit split; the comment in the test says what rejects
the other three, so nobody reads the split as decorative.

The existing `getRehashFilename` helper is used unchanged. It formats
with `%08lx.%d` and already produces `.10`, `.123` and `.255` correctly.

One existing assertion changes meaning. `testRehashFormat` builds
`7f44456a.1` for its "in rehash format, but not a symlink" case, and on
`main` that passes for the wrong reason: the name is rejected at the
`"0"` comparison, before the symlink check it is meant to exercise. I
confirmed this by passing a nonexistent path through the function, where
reaching `lstat` is observable as a throw:

| Source | `_isRehashFormat("/etc/ssl/certs/7f44456a.1")` |
| --- | --- |
| `main` | returns `false`, does not throw — rejected at the name check
|
| this PR | throws — the name is accepted and execution reaches the
symlink check |

After this change the assertion exercises what it was written for.

### Verification

Run in `swift:6.2-noble`, the image the Linux 6.2 leg of the shared
matrix names (`apple/swift-nio`, `scripts/generate_matrix.sh` line 78):

```
docker run --rm -v "$PWD":/src -w /src swift:6.2-noble swift test --scratch-path .build-linux
```

| Run | Result |
| --- | --- |
| `apple:main` (03827c1), unmodified | `Executed 339 tests, with 0
failures (0 unexpected)` |
| PR head before this commit, unmodified | `Executed 340 tests, with 0
failures (0 unexpected)` |
| With this change | `Executed 340 tests, with 0 failures (0
unexpected)` |
| With this change, the matrix's 6.2 argument overrides | `Executed 340
tests, with 0 failures (0 unexpected)` |
| New test against `apple:main` source | 5 failures: `.1`, `.9`, `.10`,
`.123`, `.255` |
| New test against this branch's first commit | 3 failures: `.10`,
`.123`, `.255` |
| New test against a variant keeping the default split | 3 failures:
`.7f44456a.0`, `7f44456a..0`, `7f44456a.0.` |

The last three are negative controls, showing the new assertions have
teeth in both directions. `swift format lint` reports only the
pre-existing `OrderedImports` warnings, unchanged from the baseline.

The `.10` case was reported by @stephenlclarke; I reproduced it here
before changing anything.

---

The code in this pull request was written with Claude Fable 5.1. The
verification above and this description were written with Claude Opus 5.
I have reviewed and tested the change, and I am responsible for its
content.
2026-09-07 16:22:32 +01:00
2024-11-15 09:52:50 +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-08-14 18:37:26 +01: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%