* X509: make the code more portable
`CoreFoundation` is not available on all platforms. It never was meant
to be available on Linux, but was accidentally pushed into the release
and is now stuck. Windows intentionally did not make this module
visible. With the future work with the Foundation rewrite
`CoreFoundation` will disappear in more locations.
Windows does not support `inet_pton` except unless the string is
explicitly ANSI and not UTF-8. We must use the proper spelling of
`InetPtonW` which takes a UTF-16 string.
The import of `CoreFoundation` was more for the dependency on the C
library as a side-effect. On Windows, the networking functions are
provided by a separate user space library rather than the C library.
This is now highlighted in the imports.
* Clean up indentation
* Re-add CoreFoundation on Linux
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
macOS CMake build is failing with error:
```
/Users/ec2-user/jenkins/workspace/swift-package-manager-PR-macos-smoke-test/branch-main/swift-certificates/Sources/X509/CertificatePrivateKey.swift:158:32: error: cannot find type 'SecureEnclave' in scope
case secureEnclaveP256(SecureEnclave.P256.Signing.PrivateKey)
^~~~~~~~~~~~~
ninja: build stopped: subcommand failed.
```
https://ci.swift.org/job/swift-package-manager-PR-macos-smoke-test/2207/console
Modifications:
Introduce compiler directive `INCLUDE_SECURE_ENCLAVE_SUPPORT` to control when
to include Secure Enclave related code. It is set in SwiftPM build (`Package.swift`)
but not CMake.
There are some use-cases where interrogating the structure of a CMS
signature is necessary. This patch adds an SPI for looking at a CMS
signature.
As with the rest of our CMS operations, we're not willing to commit to
an API shape yet, so this patch doesn't provide one. Instead, users need
to opt-in to using the unstable API.
* OCSP soft failure mode
Soft failure mode allows to only trust OCSP responses once the responders certificate is fully verified. If the OCSP fails at any other step e.g. the request could not complete or decoding failed we still meet the OCSP policy in soft failure mode. This should generally be the default unless otherwise required.
* fix review comments
In many circumstances it may be useful to extract the underlying public
key from a Certificate.PublicKey. This patch adds support for optionally
unwrapping the key into the various base types.
In some PKI applications private keys should be kept in the
most secure manner possible. The SecureEnclave provides the most
secure repository and signing capabilities on Apple hardware. These
changes allow applications to use SecureEnclave managed P256 private
keys which can be referenced in a keychain while the cryptographic
material and signing operations never leave the SecureEnclave.
The following key initializer is added to the api.
Certificate.PrivateKey(SecureEnclave.P256.Signing.PrivateKey())
Our current basic constraints checking policy requires the presence of
the basic constraints extension to be a CA. That's too aggressive in the
face of the ongoing persistence of v1 certs, both in the root store and
in the wider ecosystem of devices. We can safely tolerate v1
certificates in almost all cases.
In general, disabling expiry checking is not a useful thing to do: it
weakens security posture for no reason. In some testing use-cases or
unusual product cases, however, it's valuable to do this. This patch
adds support for ripping out expiry checking.
We had an issue with name constraints where we lost a layer of SEQUENCE
in the parse and serialization. That meant we couldn't parse real name
constraints.
This patch modifies the ECDSA signature representation internally to
avoid eagerly creating the Crypto type. This is necessary because we
can't actually know what key created the signature when we parse it.
As a result we have to defer actually doing that until we actually get a
key type, at which point we can create the underlying key type.
This is a bit less efficient for us overall, but it solves a long-tail
problem in the WebPKI that is more likely to increase in frequency over
time.
* Add support for policing critical extensions
We are required to ensure that we do not validate chains that contain
certificates with extensions marked critical that we do not understand.
Because we allow pluggable policies which can handle arbitrary
extensions, we need a way to ask verifier policy objects what extensions
they can handle.
This patch adds support for policies to express that set, as well as for
the verifier to ensure that certificates that have critical extensions
we cannot understand are not used for chain building. This is integrated
early into chain building to reject unsatisfiable chains, and works
transparently.
This PR also updates the tests to meet this new standard, including
applying the RFC 5280 policy liberally throughout the test suite.
* Don't forget the leaf!
* Rename property
* Remove Sendable
This patch ties together the various checks we have already written into
a single NameConstraintsPolicy, and attaches it to the RFC5280Policy.
This includes defining the logic for traversing names, as well as for
managing the excluded and permitted subtrees.
Unfortunately, this patch is giant. I wish I could do more about it, but
fundamentally the majority of the code it adds is tests, and those tests
are all necessary.
Just to round things out we can add directory name constraints. There's
nominally a fairly complex algorithm for validating directory name
constraints, but most implementations don't bother, instead falling back
to direct matches.
This is the last of the major constraint types we intend to support
initially in name constraints. Mostly URIs leverage the domain name
support, but we do have a need to parse URIs in order to extract their
host component.
`Certificate.SerialNumber.init()` could produce a number where the first byte(s) could be zero(s). We strip them during encoding and will not and can’t reconstruct them after decoding. Therefore a serial number might no longer be equal to itself after a de/serialisation roundtrip. We have observed this behaviour to cause problems in the OCSP test suite with random test failures. This fixes it.
This updates Swift Crypto to 2.4.0 and fixes RSA key serialization on
all platforms. It also adds a defensive check that we only accept
appropriately-formatted RSA keys, and some regression tests.
This PR adds support for handling domain name constraints. Specifically,
it adds the infrastructure for doing so, but doesn't wire it up into a
useful policy. This will follow in a later PR.
When we decode a certificate, we produce an in-memory representation.
This representation is something we are capable of encoding. If we
re-encode that directly, we can produce an output that is different to
the one we injested. This happens when there are objects that can be
canonicalised, such as replacing an explicit ASN1 NULL with an absent
field where both are considered equivalent.
This patch rewrites the code to ensure that we preserve the bytes of the
certificate that we deserialized. This allows us to write them back
unchanged, ensuring that certificates can round-trip through this
library without modification.
A side-effect of this change is that we need to essentially encode the
certificate on construction whenever we build them in memory. That's not
a big deal, but it is a bit of an annoyance.
This is the first of a series of PRs that adds support for RFC 5280
policies as a single verifier policy. For now we take the low hanging
fruit, expiry.
This patch adds support for basic CMS operations. We aren't sure that
the API surface here is right, so for now we're going to provide this
under an SPI guard so that we can make any amendments we might need.
When building against a build tree artifact for Foundation, we need to add the build tree artifacts of dispatch as that is an exposed dependency for Foundation.
Motivation
We omitted RSA signing in the initial implementation. We should support
it.
As part of this, we need to decouple the idea that we know what the
digest algorithm is based solely on the key type. There's no particular
reason not to support ECDSA with SHA256 when using a P521 key, so we
need to tolerate that as well.
Modifications
- Explicitly pass the signature algorithm to our various cryptographic
operations.
- Add a Digest type to handle the various digest types we create.
- Add tests for the various cross products.
Results
RSA signing works, as do weirder flavours of ECDSA.