86 Commits
Author SHA1 Message Date
Cory Benfield 8d6849435f Correctly require no padding bits in bit strings for signatures (#329)
Motivation

We have a check that requires that the bit strings for Ed25519
signatures don't have any padding bits. That check is good, but the same
check should have been applied to all the signatures, and it wasn't.

There is no particular security risk with this: while it allows moderate
signature malleability (as we accept multiple signatures that have
different bits as equally valid), there is no way we can conceive of to
launch an attack based on this malleability: the signature itself needs
to remain valid. The only note is that the representation differs.

Modifications

Centralise the check and apply it to all the code paths. Make sure the
OCSP code doesn't lose track of whether there was any padding.
Add tests for the OCSP verifier and the signature code directly.

Result

No more signature malleability.
2026-11-20 16:23:44 +00:00
Cory Benfield 80052da803 Allow the 4.0 series of Crypto releases (#275) 2025-10-06 17:16:25 +01:00
Cory Benfield 20c451f1ad Require VerifierPolicy not have isolated conformance (#269)
Motivation

While conceptually it is possible for verifier policies to have isolated
conformance, in practice the way PolicyBuilder is implemented makes that
nearly impossible. Given that the utility of an isolated VerifierPolicy
is very limited, let's just disallow it.

Modifications

Require VerifierPolicy to have a SendableMetatype

Result

Easier to build correct compositions over PolicyBuilder
2025-08-28 10:32:31 +01:00
Cory Benfield 0b5a9f2e84 Re-core the benchmarks (#271)
With the release of swift-crypto 3.13.3, our benchmark numbers should
have improved. Let's lock those in.
2025-07-30 15:14:23 +00:00
Cory Benfield 870f4d5fe5 Skip errors relating to interaction not being allowed (#259) 2025-07-02 08:16:42 -07:00
Cory Benfield 27c6b0fcfa Add support for Ed25519 certificates (#205)
Motivation

While Ed25519 is not available in the WebPKI, there
are a number of contexts where it's an appropriate
signature algorithm for X.509. In those contexts,
we should allow the signature algorithm.

Modifications

Add support for Ed25519 keys and signatures.
Add unit tests

Result

Ed25519 support in the X509 certs
2024-10-30 13:30:13 +00:00
Cory Benfield 1fbb6ef21f Avoid the macOS requirement. (#204) 2024-10-15 13:19:03 -07:00
Cory Benfield 387fb97cf6 Stop implicitly tagging DistinguishedName (#188)
DistinguishedName was incorrectly marked DERImplicitlyTaggable,
but it isn't. DistinguishedName is our name for Name, which is
an untagged choice type. Untagged choice types can never be
implicitly tagged.
2024-08-15 20:11:51 +01:00
Cory Benfield 17ef172a2a Up our scaling factor to kilo (#189)
This should help with our thresholds moving around so much.
2024-08-15 14:56:32 +01:00
Cory Benfield 4688f24281 Our perf thresholds have changed (#178)
* Our perf thresholds have changed

* Apparently local runs don't match

* Stop using retain/release thresholds

* Remove retain/release counts
2024-06-07 16:27:37 +01:00
Cory Benfield 4975de4e97 Add support for validating server hostnames and IP addresses (#171)
Motivation

To make Swift Certificates useful for the Web PKI it's vital
that we can validate that a particular certificate is authoritative
for a specific domain. This requires a new verifier policy.

Modifications

Added ServerIdentityPolicy
Added tests

Result

We can validate the identity of remote servers.
2024-04-22 16:24:19 +01:00
Cory Benfield b4f2d54e0d Remove now-breaking test vector test (#167)
This test no longer passes with Swift Crypto main, as it relies on
RSA 1024 which we now forbid
2024-03-25 11:17:34 -07:00
Cory Benfield 4ff26ad6cc Support converting RDNAttribute values into Strings (#154)
Motivation

RDNAttribute Values are mostly strings, and it's useful to be able
to convert them. This patch adds that support.

Modifications

Add support for converting RDN attribute values into Strings.

Result

Easier to work with distinguished names.
2023-12-01 05:03:37 -08:00
Cory Benfield e37cd21724 We aren't supporting PSS for now. (#93)
RSA PSS isn't really deployed in the WebPKI, and with the recent
interest in post-quantum crypto it seems unlikely a new RSA padding
mode will be specified. Let's not allude to trying to support it.
2023-06-12 06:44:06 -07:00
Cory Benfield 1b747c3295 Remove unnecessary TODO (#89)
This patch removes a TODO requesting validation of an RSA signature, which we
cannot do.
2023-06-01 05:04:20 -07:00
Cory Benfield d66a9f7e23 Remove the "from parts" Certificate constructor. (#88)
The "from parts" Certificate constructor has existed for a while. As noted
in the docstring, it was originally added for testing and validation
purposes. However, it's not very useful for that purpose.

For one thing, many of the constitutuent parts are not actually directly
constructible. For example, one cannot create a Signature type directly.

For another thing, invalid Certificate handling tests probably want to be
able to construct Certificates that are invalid in a number of interesting
ways, and so will want to use the DER representation to do so.

As a result, this intializer is not broadly useful: even this library
doesn't use it for testing. Let's remove it.
2023-05-25 18:13:03 +01:00
Cory Benfield 6874b181f2 Make constructing Dates cheaper (#86)
* Make constructing Dates cheaper

Translating between Date and Time is expensive when we use
DateComponents, because we jump through ICU unnecessarily. We can
move over to standard UTC date math instead, which makes this
process vastly cheaper.

* CMakelists

* Code review
2023-05-25 14:14:56 +01:00
Cory Benfield e37a3e77d6 Not all systems have SEPs, so we can't rely on them. (#87) 2023-05-25 13:24:26 +01:00
Cory Benfield 3ad34c910e Make expiry checking cheaper. (#85)
Currently, certificate verification is severely bottlenecked on
expiry checking. This bottleneck is almost entirely caused by converting
Time objects into Date objects via DateComponents.

We can reduce the cost of this by doing fewer transformations. Instead
of converting all the Time objects to Dates, we can convert a Date to
a GeneralizedTime and then do all comparisons in GeneralizedTime space.

This vastly reduces the cost of cert validation.
2023-05-18 12:50:30 +01:00
Cory Benfield 47d2efae8d Actually validate we can parse the signature. (#82)
We've had a test floating around for a while that was validating that
we parsed signatures properly, but the test never confirmed that
the parsed signature was valid, only that Crypto accepted it.

This patch improves that situation.
2023-05-09 10:15:19 -07: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
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
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
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
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
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
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 30319b615c Merge pull request #34 from Lukasa/cb-expiry
Add initial RFC5280 policy.
2023-03-07 10:15:02 +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 4ac8b1d517 Merge pull request #23 from apple/cb-generate-cms-sigs
CMS Signing support
2023-03-06 14:01: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
Cory Benfield a7735a35f9 Merge pull request #24 from apple/cb-leafs-as-roots
Handle leaves as trust roots
2023-03-06 13:00:56 +00:00
Cory Benfield 2473e5b258 Merge pull request #25 from apple/cb-readme
Add README and collateral
2023-03-01 12:20:42 +00:00
Cory Benfield 229051f3ad Code-review feedback 2023-03-01 11:16:35 +00:00
Cory Benfield 941e966ccf Add README and collateral 2023-03-01 10:35:09 +00:00
Cory Benfield 2afba510e7 Handle leaves as trust roots
In some cases, we will have the leaf certificate in the trust root
store. In this case, we can immediately pass that on to policy
verification.
2023-03-01 10:24:45 +00:00
Cory Benfield c59fbdbf1f Merge pull request #21 from apple/cb-enable-rsa-signing
Enable RSA signing support.
2023-02-13 17:09:40 +00:00
Cory Benfield cd04b642b2 Merge pull request #22 from apple/dn-ocsp-flaky-tests
Add better diagnostics if an OCSP test fails
2023-02-13 11:20:17 +00:00
Cory Benfield b1fa515ddc Merge branch 'main' into dn-ocsp-flaky-tests 2023-02-13 11:18:38 +00:00
Cory Benfield dc9a93f359 Merge branch 'main' into cb-enable-rsa-signing 2023-02-13 08:28:19 +00:00
Cory Benfield 8458a4ff72 Enable RSA signing support.
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.
2023-02-10 16:55:39 +00:00
Cory Benfield dee25004e8 Merge pull request #19 from apple/dn-set-of
Properly support SET OF fields.
2023-02-10 14:12:45 +00:00
Cory Benfield fdd742b243 Merge branch 'main' into dn-set-of 2023-02-09 09:58:41 +00:00
Cory Benfield 4c6e8bbd8d Merge pull request #20 from apple/dn-fix-5.8-warnings
Fix 5.8 warnings
2023-02-09 09:58:29 +00:00
Cory Benfield 0cd2bb5ea5 Merge pull request #17 from apple/cb-remove-warnings
Silence the warnings I accidentally added
2023-02-07 13:49:41 +00:00
Cory Benfield 39aa8e70f8 Merge branch 'main' into cb-remove-warnings 2023-02-07 13:46:39 +00:00
Cory Benfield 749b1c989b Silence the warnings I accidentally added 2023-02-07 13:46:03 +00:00
Cory Benfield c2ca943c2e Merge pull request #16 from apple/cb-un-namespace-extensions
Un-namespace the extensions.
2023-02-07 13:44:09 +00:00
Cory Benfield 53d16600ae Clean up the docs references as well 2023-02-07 13:31:16 +00:00
Cory Benfield 0d95324c80 Un-namespace the extensions.
We've received API review feedback that is critical of us having the
extensions inside a namespace. This patch extracts them from that
namespace.
2023-02-07 11:26:50 +00:00
Cory Benfield aaafa59d48 Merge pull request #15 from apple/cb-new-asn1-error
Update to swift-asn1 new error type
2023-02-06 12:10:27 +00:00
Cory Benfield b408daf0f8 Update to swift-asn1 new error type 2023-02-06 11:02:45 +00:00
Cory Benfield 38147aaae1 Merge pull request #13 from apple/dn-fix-distinguished-name-example
Fix examples
2023-02-01 15:52:36 +00:00
Cory Benfield 19351b0b17 Merge pull request #10 from apple/dn-ocsp-policy
`OCSPVerifierPolicy`
2023-01-27 09:56:24 +00:00
Cory Benfield 6481155f7e Merge pull request #8 from apple/cb-basic-chain-builder
Implement a basic chain builder.
2023-01-25 14:01:43 +00:00
Cory Benfield 389c9c4706 Prefer appending to init 2023-01-24 14:52:34 +00:00
Cory Benfield d2ef4164b1 Merge branch 'main' into cb-basic-chain-builder 2023-01-24 14:47:22 +00:00
Cory Benfield e474326e9d Merge pull request #6 from apple/dn-ocsp-request
OCSPRequest type and subtypes
2023-01-24 14:43:25 +00:00
Cory Benfield 403da4ffd4 Implement a basic chain builder. 2023-01-16 10:37:52 +00:00
Cory Benfield 205cacfea4 Merge pull request #7 from apple/dn-2023
Its 2023 and we should accept new files
2023-01-12 10:55:44 +00:00
Cory Benfield 253da742a3 Merge pull request #1 from apple/dn-ci-preperation
Add docker compose files and scripts for CI
2022-11-28 11:18:16 +00:00
Cory Benfield 88ed33064a Fix missing index check 2022-11-23 13:57:05 +00:00
Cory Benfield 4bcf463188 Initial Verifier spike 2022-11-23 13:56:25 +00:00
Cory Benfield 195d9cda98 Fix RSA keys 2022-11-22 09:27:40 +00:00
Cory Benfield 0042596250 Clean up a typo 2022-11-18 17:00:45 +00:00
Cory Benfield f303709f60 Update to new ASN.1 API 2022-11-18 16:24:04 +00:00
Cory Benfield 30aa69569b Printable representations for stuff 2022-11-17 11:26:04 +00:00
Cory Benfield a3d3629206 Clean up the package name 2022-11-16 15:01:57 +00:00
Cory Benfield 42be2505cc Initial commit 2022-11-16 15:50:52 +01:00