103 Commits
Author SHA1 Message Date
David NadobaandCory Benfield 9099e78aad Fix OCSP serialisation and parsing tests (#81)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-05-09 11:25:51 +01:00
David NadobaandCory Benfield 147d854c38 Policy Builder (#79)
* experiment with policy builder

* convert tests to use new policy builder

* Add AnyPolicy

* deprecate PolicySet

* revert swift tools version

* code review

* run ./scripts/update_cmakelists.sh

* add tests

* Fix Swift 5.7

* add documentation

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-05-09 11:16:59 +01:00
David Nadoba 922fdbd233 Gracefully fail Extension serialization (#80)
* Gracefully fail extension serialisation

* Add more tests

and remove unused code
2023-05-09 11:02:49 +01:00
Cory Benfield d9a8b6583a Certificate Signing Request (#59)
* Add support for Certificate Signing Requests

This patch adds support for working with Certificate Signing Requests to
swift-certificates.

CSRs are a common feature of X.509 based PKIs. They're used to encapsulate
a number of details about the certificate being requested, and are
typically handed to CAs in exchange for a certificate. Their purpose is
to prove possession of a private key, and optionally some other useful
pieces of data.

This patch provides a complete representation of the CSR type, as well as
semantic types to cover the attributes that can be applied to a CSR.
Additionally, CSR signature validation is also available.

* Code review

* Fix up equality and hashing

* Count matters too

* Comment explaining XOR
2023-05-04 04:08:23 -07:00
Cory Benfield 18b6614b15 Fix signature comparison (#77)
This patch fixes up an issue where a signature produced from raw form
will have a different in-memory representation from the same one
deserialized from the network.
2023-04-27 10:31:20 +01:00
David Nadoba ac767dc442 Support PEM serialisation and parsing of Certificate, Certificate.PublicKey and Certificate.PrivateKey (#71)
* Support PEM serialisation and parsing for `Certificate`, `Certificate.PublicKey` and `Certificate.PrivateKey`

* Fix Linux build

* Fix tests on Linux

and required watchOS version

* run ./scripts/update_cmakelists.sh

* add proper support for private RSA keys

* Fix review comments

* replace `as Any` with `String(describing:)`
2023-04-24 13:20:19 +01:00
Kai ÖzerandCory Benfield 3d58f897b1 Fixed typo (#73)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-04-24 01:50:45 -07:00
Kai ÖzerandCory Benfield 2b03571446 Fixed variable name in documentation code snippet (#76)
The variable name should be `opaqueSanExtension` instead of `opaqueSANExtension`.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-04-24 01:11:51 -07:00
Kai ÖzerandCory Benfield d25064f450 Fixed referenced property name in documentation (#75)
In a certificate chain, the child certificate's issuer is the parent certificate's subject.

Modifications:

Replaced Certificate/issuer with Certificate/subject to fix the meaning.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-04-24 09:06:18 +01:00
Kai Özer b8c3f38102 Fixed typo "the the" (#74)
Removes repeated "the".
2023-04-24 00:55:49 -07:00
Saleem AbdulrasoolandCory Benfield 35a7df2d9d X509: make the code more portable (#72)
* 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>
2023-04-21 18:22:18 +01:00
Lukas Reschke 0121e2492c Fix wrong variable in documentation (#70)
`name` doesn't exist in this context and probably refers to `subjectName` and `issuerName`
2023-04-19 23:24:17 +02:00
Yim Lee 5c05fab7fb Proper fix for 'SecureEnclave' issue in cmake build (#68)
Motivation:
https://github.com/apple/swift-certificates/pull/67 introduced a temporary workaround to unblock cmake build on macOS, but we have a proper fix now (https://github.com/apple/swift-crypto/pull/177).

Modifications:
- Revert https://github.com/apple/swift-certificates/pull/67
- Take new version of `swift-crypto` containing https://github.com/apple/swift-crypto/pull/177
2023-04-19 09:50:54 +01:00
Yim Lee 6ec92354cf Exclude Secure Enclave related code from CMake build (#67)
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.
2023-04-18 10:12:15 +01:00
Cory Benfield 678756a659 Expose a CMSSignature type (#65)
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.
2023-04-13 18:30:47 +01:00
David Nadoba d0c71ebd05 Add API to construct a SubjectKeyIdentifier by hashing a Certificate.PublicKey (#64) 2023-04-13 12:53:20 +01:00
David Nadoba c45f99f9eb Fix Sendable warning in OCSPVerifierPolicy. self.chainMeetsPolicyRequirementsWithoutDeadline (#62)
* Fix `Sendable` warning in `OCSPVerifierPolicy. self.chainMeetsPolicyRequirementsWithoutDeadline`

* Fix non-Darwin platforms
2023-04-13 02:31:00 -07:00
David Nadoba eb0313c109 OCSP soft failure mode (#61)
* 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
2023-04-12 07:44:54 -07:00
Cory Benfield 9d1a62e487 Add support for unwrapping public keys. (#60)
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.
2023-04-12 13:59:17 +01:00
Joey 842de7e8ba Documentation - Typos (#56) 2023-04-11 00:45:16 -07:00
Josh Schlesser 2c41d15b25 Add support for using SecureEnclave.P256 (#55)
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())
2023-04-04 11:30:01 +01:00
Cory Benfield 08f8b2394b Tolerate v1 certs without basic constraints (#53)
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.
2023-03-31 06:52:29 -07:00
Cory Benfield 35a24f4ab9 Add SPI support for disabling expiry checking in RFC5280Policy. (#54)
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.
2023-03-31 14:23:10 +01:00
Cory Benfield 19b1827d92 Fix name constraints parsing (#52)
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.
2023-03-30 06:17:20 -07:00
Cory Benfield 46b2236f9c Tweak the ECDSA signature representation (#50)
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.
2023-03-30 14:07:12 +01:00
David Nadoba 1166febcbf Add leeway to the OCSP time verification (#48) 2023-03-22 08:07:30 -07:00
Cory Benfield f6a1b1b418 Add support for policing critical extensions (#47)
* 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
2023-03-22 07:02:58 -07:00
David Nadoba 4a67b473a1 Build OCSP Response certificate chain and verify signature (#43)
OCSP responses are signed and we must validate the signature. This PR adds support for that.
2023-03-21 09:51:10 -07:00
David Nadoba 7fff85a91e Follow casing from Swift API Design Guidelines (#46)
* Follow casing from Swift API Design Guidelines

* revert ASN.1 definitions back to casing of the specification
2023-03-21 16:39:09 +00:00
Cory Benfield 55105af86b Produce functional NameConstraints (#45)
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.
2023-03-20 10:54:29 +00:00
Cory Benfield 83af4ff3ca Add directory name constraints. (#44)
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.
2023-03-16 15:37:20 +00:00
Cory Benfield 7816361861 Add support for name constraints on URIs (#42)
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.
2023-03-16 13:54:00 +00:00
Cory Benfield 383d0c4c7c Improve our handling of trailing periods. (#41)
We incorrectly considered a trailing period to be a match-all clause.
It's not. Make the code a little less clever to compensate.
2023-03-16 09:21:03 +00:00
Cory Benfield 9b2f1c6797 Add support for IP address constraints. (#38)
This patch adds more partial name constraints support, this time for IP
address constraints.
2023-03-14 17:58:17 +00:00
David Nadoba 88a30b349c Fix Certificate.SerialNumber.init() (#40)
`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.
2023-03-14 17:54:06 +00:00
Cory Benfield 740e2e0bd3 Update Swift Crypto and fix RSA keys (#39)
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.
2023-03-14 14:16:50 +00:00
Cory Benfield 54f7367ea5 Add support for handling domain name constraints. (#37)
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.
2023-03-10 11:23:30 +00:00
Cory Benfield d05bd7819f Add BasicConstraints enforcement to RF5280Policy (#36)
This patch adds support for enforcing BasicConstraints in the RFC 5280
policy.
2023-03-08 17:17:29 +00:00
Cory Benfield 54b9711034 Avoid re-serializing certificates (#35)
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.
2023-03-08 13:30:34 +00:00
Cory Benfield 6410b6928d New files for Cmake 2023-03-07 10:11:20 +00:00
Cory Benfield f0cafaed95 Test that the right policy does the rejecting 2023-03-07 10:09:10 +00:00
Cory Benfield 111a9f9985 Add initial RFC5280 policy.
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.
2023-03-06 14:55:19 +00:00
Cory Benfield 04d4c4fc01 CMake 2023-03-06 13:45:03 +00:00
Cory Benfield 78c495a58d Apply code review feedback 2023-03-06 13:44:54 +00:00
Cory Benfield 5b7666f8ef Support additional intermediates 2023-03-06 13:44:54 +00:00
Cory Benfield d1cd7fe8c2 Provide the signer 2023-03-06 13:44:54 +00:00
Cory Benfield e61c4ecf0a CMS Signing support
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.
2023-03-06 13:44:54 +00:00
David Nadoba 0abd90a675 Merge branch 'main' into cb-leafs-as-roots 2023-03-06 13:11:30 +01:00
Boris Bügling 5dad2bc2ea Fix CMake build (#33)
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.
2023-03-03 17:20:40 -08:00
Boris BüglingandYim Lee 75c7f1c7c8 Fix CMake for X509 (#32)
Co-authored-by: Yim Lee <yim_lee@apple.com>
2023-03-02 17:38:58 -08:00