After a server certificate change, the regression environment still connects, but released clients all fail: the problem may not be the network, but clients that still accept only the old certificate digest. To catch this kind of failure early on a cloud Mac, the key is not to keep connecting to the server, but to turn the rotation rules into repeatable tests. This article pins the digest of the leaf certificate’s complete DER encoding. It does not cover public key pinning or require changes to the production certificate.
Define the boundaries
A connection must satisfy two conditions: system trust evaluation must pass, and the leaf certificate’s digest must be in the allowed set. The first checks the certificate chain, validity period and target hostname; the second narrows the identities the client accepts. A matching digest must never be used as a reason to skip system trust evaluation and use the credential directly.
Define three certificates: A is the current certificate, B is the next certificate distributed in advance, and C is an unknown certificate. The transition release accepts A and B; the release that retires A accepts only B. Here, “next” must mean the actual certificate prepared for deployment, not an arbitrary backup file.
Self-signed fixtures are only for validating the local pinning policy. They do not establish system trust. Do not add the fixtures to the system trust store or disable trust evaluation just to make the tests pass.
When a complete certificate is renewed, its DER bytes will usually change even if the public key stays the same. If your team chooses this form of pinning, you must plan the allowed set again for every certificate replacement. Rotation guidance for public key pinning does not apply.
Create the fixtures
Run the following commands in a separate working directory. You will need working certificate tools and a Swift toolchain. First run xcrun swift --version and record the toolchain version in the test log. Generate the certificates only in this working directory; private keys must not enter the repository or pipeline artifacts.
set -eu
umask 077
mkdir -p pin-rotation
cd pin-rotation
for name in a b c; do
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout "$name.key" -out "$name.pem" \
-days 2 -subj "/CN=pin-fixture-$name"
openssl x509 -in "$name.pem" -outform DER \
-out "$name.der"
done
rm a.key b.key c.key
These certificates are not configured for real hostname validation, nor do they provide a server for handshake tests. They supply only three valid, distinct sets of certificate bytes. If the test script receives a corrupted file, it must exit with an error, not turn a read failure into “accept all certificates.”
When rerunning the tests across nodes, distribute the same DER files and record their digests. Do not regenerate them on each machine and then compare the digests for equality. Each generation produces new material, so different results are expected.
Run the policy tests
Save the following script as check.swift in the same directory and run xcrun swift check.swift. The script first checks that each input can actually be parsed as a certificate, then computes its digest. It uses exact set membership, not substring matching, digest prefixes or certificate display names.
import Foundation
import Security
import CryptoKit
enum FixtureError: Error {
case invalidCertificate
case assertionFailed(String)
}
func digest(_ file: String) throws -> Data {
let bytes = try Data(contentsOf: URL(fileURLWithPath: file))
guard let cert = SecCertificateCreateWithData(nil, bytes as CFData) else {
throw FixtureError.invalidCertificate
}
let der = SecCertificateCopyData(cert) as Data
return Data(SHA256.hash(data: der))
}
func accepts(
trusted: Bool,
leaf: Data,
allowed: Set<Data>
) -> Bool {
trusted && allowed.contains(leaf)
}
func expect(_ condition: Bool, _ label: String) throws {
guard condition else {
throw FixtureError.assertionFailed(label)
}
}
let a = try digest("a.der")
let b = try digest("b.der")
let c = try digest("c.der")
try expect(Set([a, b, c]).count == 3, "distinct fixtures")
let overlap: Set<Data> = [a, b]
let retired: Set<Data> = [b]
try expect(accepts(trusted: true, leaf: a, allowed: overlap), "current")
try expect(accepts(trusted: true, leaf: b, allowed: overlap), "next")
try expect(!accepts(trusted: true, leaf: c, allowed: overlap), "unknown")
try expect(!accepts(trusted: true, leaf: a, allowed: retired), "retired")
try expect(accepts(trusted: true, leaf: b, allowed: retired), "replacement")
try expect(!accepts(trusted: false, leaf: b, allowed: overlap), "untrusted")
try expect(!accepts(trusted: true, leaf: b, allowed: []), "empty policy")
Normal completion means all assertions passed; any error ends the script with a nonzero exit status. Here, trusted is a test input, not an implementation of system trust evaluation. It verifies the combined rule: if trust evaluation fails, the connection must be rejected even when the digest matches the pinned set.
When integrating this into your project, have the tests and network layer share the same pure policy function rather than maintaining separate copies. A correct test copy paired with an incorrect production branch is one of the most subtle ways this kind of gate can fail.
Verify the handshake
The real network layer must first complete system trust evaluation for the target hostname, then retrieve the leaf certificate and pass it to the pinning policy. Reject the connection if there is no server trust object, the leaf certificate cannot be retrieved, or the allowed set is empty. Other authentication challenges must follow their own handling paths, not be mistakenly routed through the certificate branch.
Handshake acceptance testing must cover at least four cases: trusted with a matching digest, trusted without a matching digest, matching but expired, and matching but with the wrong hostname. The last two require controlled test endpoints and separate client processes. Simply setting the script’s Boolean input to false does not complete those tests.
For each case, record the client version, policy version, expected result and actual error category. A failed connection may end before any application request is sent, so do not classify “the server received no request” as a test that did not run.
Schedule the rotation
First release a client that accepts both A and B, then switch the server certificate, and finally release a client that removes A. At every step, check older clients that are still supported, not just the latest build. If an older version recognizes only A, removing A from the server too early will prevent it from connecting.
Rollback has limits too: once clients that accept only B have been distributed, switching the server straight back to A will cause failures again. Release records should clearly identify compatible client and certificate combinations. Validate the rollback plan before executing it; do not wait for connections to break and then broaden the allowed set on the fly.
The final checks cover rejection of unknown certificates, rejection of an empty set, rejection after failed trust evaluation, and retirement of the old certificate. Archive the script, DER fixtures, policy version and exit status, but not private keys. Only when both the offline policy tests and controlled handshake tests pass is there enough evidence to proceed with the certificate switch.
Frequently asked questions
Does a matching certificate pin replace system trust validation?
No. Pin matching is an additional requirement. The certificate chain, validity period and target hostname must still pass system trust validation.
Will renewing a certificate with the same public key preserve its pin?
Not generally. This workflow hashes the entire DER certificate, not its public key. Add the renewed certificate's digest before rotation and test the overlap window.
Run tests in parallel across multiple nodes
Choose a dedicated physical Mac mini for your workload. Rent by the day, with no renewal when the term ends. Check the model and node before scheduling regression tests.