23 Commits
Author SHA1 Message Date
eaa10d47d1 documentation updates (#296)
- **Fix stale DocC disambiguation hash for PrivateKey init**
- **Fix documentation errors, typos, and a bug in
CertificateStore.appending**
- **code example fixes within the existing articles:**
- **Lots of smaller fixes across the board - some copy/pasta errors,
consistency in usage, typos and grammar issues - added documentation
comments where missing to get all public types covered - applied
internal style guidelines for representing the types**

---------

Co-authored-by: David Nadoba <dnadoba@gmail.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Raphael <josephnoir@users.noreply.github.com>
2026-06-15 13:04:58 -07:00
Sergey Cheperis 7d5f6124c9 Pass additionalIntermediateCertificates through to signWithSigningTime (#289)
When `signingTime` was provided to `CMS.sign`, the
`additionalIntermediateCertificates`
parameter was not being forwarded to `signWithSigningTime`, causing
signature verification
to fail for certificates that require an intermediate CA chain.

Fixes #287
2026-01-05 15:43:18 +00:00
Raphael 4b092f1516 Introduce a validated certificate chain type (#272)
### Motivation:

The certificate chain returned by validation has properties that are
currently not expressed in the API. Introducing a type that does so
allows other APIs to build on these properties.

### Modifications:

* Add a new `ValidatedCertificateChain` type and use it as a result type
for certificate validation. Since a validated chain always has at least
a leaf and a root, the type gives direct access to them.
* Deprecate the previous validate function and introduce a replacement.
Update use of the now-deprecated API and duplicate tests to include both
APIs.

### Result:

Certificate validation returns a type that conceptually holds meaning.
2025-09-12 12:49:58 +01:00
Rafael Cepeda c059d9c9d0 Explicit sendable changes (#265)
Performed the necessary changes to enable compiler flag
`-require-explicit-sendable`. Also enabled this flag in macOS CI.
2025-07-31 15:35:39 +01:00
Gus Cairo 9ad2ac038e Add visionOS support (#244) 2025-04-08 13:35:11 +00:00
Gus Cairo 4c305e3205 Add macCatalyst to available platforms (#242) 2025-04-08 13:25:54 +01:00
Gus Cairo bd5987524c Replace platforms from Package manifest with availability guards (#232)
Minimum deployment versions in package manifests can be problematic for
libraries, because changing them or adding them to an existing package
that doesn't already use them forces SemVer breaking majors.

Furthermore, depending on a new library that specifies minimum
deployment targets would force the adopting library to also have to add
these platforms to its manifest (which would in turn force a major).

This PR removes the platforms stanza from the package manifest and
replaces it with availability guards where necessary.
`swift-certificates` needed the platforms stanza because `swift-crypto`
forced it onto its adopters. After
https://github.com/apple/swift-crypto/pull/331, this isn't needed
anymore.
2025-03-10 14:31:15 +00:00
Gus Cairo a65f903b02 Import FoundationEssentials instead of Foundation when available (#227)
`FoundationEssentials` is more lightweight than `Foundation`, and so we
should switch to importing it when possible.
2025-02-24 14:17:11 +00:00
Nicolas BachschmidtandCory Benfield eaed0355dc Add default value for signature algorithm (#221)
Most of the time, the signature algorithm that should be used is
dictated by the type of the private key.

* Ed25519 keys support only one signature algorithm.
* RFC 5753 section 8 recommends that "[the P-256 curve] be used with
SHA-256; the P-384 curve be used with SHA-384; and the P-521 curve be
used with SHA-512".
* RSA keys support 4 signature algorithms. But most people use RSA with
SHA-256 and nobody should use RSA with SHA-1 anymore.

More over, Certificate.PrivateKey is opaque to the user, who may not
know what type of private key they're using and what the appropriate
signature algorithm is.

For those reasons, we add convenience wrappers around methods with a
signature algorithm to provide a reasonable default value.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-01-27 11:03:56 +00:00
Nicolas BachschmidtandCory Benfield 88ced31d9d Use proper hash when computing message digest in signed attributes (#219)
Per [RFC 5652 §
11.2](https://datatracker.ietf.org/doc/html/rfc5652#section-11.2):

"For signed-data, the message digest is computed using the signer's
message digest algorithm."

Currently, when computing the signed attributes' message digest,
`CMS.sign` assumes the digest algorithm is SHA256, which is incorrect.
The behavior of `CMS.isValid` is already correct and doesn't need
updating. I've made `Digest` conform to `Sequence` to facilitate the
conversion/comparison of the message digest.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-01-16 16:41:23 +00:00
Jon C. Thomason d4d564670a Add support for attached signatures (#201) (#206)
* Add support for attached signatures (#201)

* Relay detachment through with signing time

* Factor attachment into generateSignedData

* Adjust test coverage

* Test verifying an attached signature

* Test verifying an attached signature and timestamp

* Test failing detached signature as attached

* Test failing detached timed signature as attached

* Test accepting attached signature as detached

* Test accepting attached timed signature as detached
2024-11-22 10:54:27 +00:00
Micah MooreandCory Benfield 44f2a33ec3 Adds support for including signing time in signedAttrs for CMS signing. (#176)
* Adds support for including signing time in signedAttrs for CMS signing.

Motivation:

Many CMS signing validations require the inclusion of signing time in signedAttrs (for example Apple Wallet's Pass CMS signing validation)

Modifications:

* Adds optional includeSigningTime flag to CMS.sign method (defaults to false)
* When includeSigningTime flag is true: includes signing-time (UTCTime ASN1Type), content-type, and message-digest signedAttrs and signs those instead of content bytes (as specified in RFC 5652 section 5.4)
* Adds contentType ASN1ObjectIdentifier
* Adds CMSSigningError (noTimeZone, invalidSigningDateComponent) for when includeSigningTime is true and unable to get UTC time zone or date components
* Adds tests for valid and invalid CMS signature when including signedAttrs

Result:

Providing the includeSigningTime = true flag to CMS.sign will cause signing-time, content-type, and message-digest signedAttrs to be included in the CMS signature and the signature bytes will represent signing those signedAttrs. Using CMS.sign without the flag (defaulting to false) should leave the previous CMS.sign behavior unchanged.

* Improvements to inclusion of signing time in signedAttrs of CMS signing.

Motivation:

Callers may want to provide their own signingTime (Date). Code improvements for inclusion of singing time in signedAttrs.

Modifications:

* Changes CMS.sign method to take Date? instead of Bool for signingTime so callers can provide their own signingTime Date
* Adds internal method CMS.signWithSigningTime to separate logic for CMS signing with signingTime in signedAttrs
* Uses Date.utcDate transformation to get DateComponents instead of manually constructing them within CMS.signWithSigningTime
* Removes CMSSigningError from CMSOperations as its no longer needed after removal of manual DateComponents construction
* Switches message digest SHA256 from using unsafe bytes to converting Sequence<UInt8> to ArraySlice directly

Result:

CMS.sign callers can now provide signingTime (Date). CMS.sign with signingTime signedAttrs code is improved.

* Adds explicit self to internal signing method calls within CMSOperations.

Motivation:

Conform to standards regaring usage of explicit self for internal methods calls.

Modifications:

* Adds explicit self to signWithSigningTime and serializeSignedData internal methods calls with CMSOperations

Result:

Internal CMSOperations method calls are now compliant to standards.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-06-21 11:38:32 +01:00
David Zech 5afc92197a Add Expanded CMS Support (#151)
* Support BER Encoded CMS
* Support Attached CMS
* Support Signed Attributes CMS
2023-10-25 16:19:41 +01:00
David Nadoba 3c33085404 Support loading trust root CAs on Linux (#136)
* Support loading trust roots on Linux

* Fix linux

* run `swift-format`

* Apply review feedback

* Implement `_TinyArray2`

* fix tests

* Update benchmarks for _TinyArray2

* run swift-format

* use two properties instead of TinyArray2

* remove usage of `_TinyArray2` and rename `insert` to `append`

* remove TinyArray2 and rename TinyArray1 back to just TinyArray

* run swift-format

* fix tests

* Fix review comments
2023-10-18 14:01:10 +01:00
David Nadoba 50ec4771c2 Add the diagnosticCallback to CMS.isValidSignature (#135)
* Allow setting `diagnosticCallback ` for `CMS.isValidSignature`

* swift-format
2023-09-18 15:44:29 +01:00
David Nadoba 45ad9935c7 Adopt apple/swift-format (#121)
* Adopt `apple/swift-format`

* install swift-format

* set `UseSynthesizedInitializer` to `false` until bug is fixed https://github.com/apple/swift-format/issues/591

* fix license header

* Fix license header
2023-08-16 11:17:34 +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
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
Joey 842de7e8ba Documentation - Typos (#56) 2023-04-11 00:45:16 -07: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