* Add bridge between Certificate and Security.SecCertificate
* Add documentation for explaining the bridge between `Certificate` and `Security.SecCertificate`. Add `make` prefix to `SecCertificate` factory method.
* 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>
* Allow OneOfPolicies and AllOfPolicies to take throwing policy builders, to match Verifier
* add tests
* avoid api break
* soundness
* fix
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Created a new OneOfPolicyBuilder
This is similar to the existing PolicyBuilder
The difference is that the new OneOfPolicyBuilder requires only one of the child policies to be valid, not necessarily all of them
A new struct, OneOfPolicies implements VerifierPolicy and can be constructed using OneOfPolicyBuilder
This can be used within a larger policy e.g.
let x = Verifier(rootCertificates: .init([])) {
RFC5280Policy(validationTime: .init()) // this must be met
OneOfPolicies {
SomethingPolicy() // one of these 2 must be met
SomethingElsePolicy() // one of these 2 must be met
}
}
The verifyingCriticalExtensions of OneOfPolicies is intersection of the verifyingCriticalExtensions of each of the child policies, because the overall policy will only be guaranteed to verify an extension if every child policy does so, because we don't know which child policies will actually be evaluated (we stop evaluating when any one passes)
OneOfPolicies will only fail if all of the child policies fail. In this case, the failure reason will be a concatenation of all the child failure reasons, with the word 'and' in between them
This PR also introduces AllOfPolicies
This is to allow users to specify a set of policies which must always be set, as a single item within a OneOfPolicies
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.
* build: add support to vendor dependencies
When building swift-crypto outside of the toolchain (e.g. to build LSP),
it is convenient to support vendoring dependencies. Add support for
falling back to local builds of the dependencies when not provided as an
existing build.
* Apply suggestions from code review
Co-authored-by: Yim Lee <yim_lee@apple.com>
---------
Co-authored-by: Yim Lee <yim_lee@apple.com>
* Simplify the internal representation of RDNAttribute.Value
Motivation
The 1.1.0 release included a patch (#154) that supported converting
RDNAttribute values into strings. This patch missed that the `.any`
case may also have strings in it.
While investigating this limitation, I realised that the constructor
function was excessively complex. While it attempted to coerce based
on extra information in the ASN.1 specification, it always fell back
into the `.any` case. This meant that as a practical matter we always
took the same few paths, and so could safely simplify the code.
Modifications
Simplify DER decoding of RDNAttribute.Values.
Introspect the .any cases for UTF8 strings and Printable strings
when converting to String.
Result
Faster construction, more flexible representation.
* normalise `RDN.Value.Storage` representation
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
With this tweak, we should be able to build the package in debug mode
even on Windows. We always use the release mode DLL form of the C/C++
runtime on Windows and can build the Swift code in Debug or Release mode
optimizations.
Co-authored-by: Cory Benfield <lukasa@apple.com>
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.
* 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
Motivation:
Now that Swift 5.9 is GM we should update the CI pipelines
Modifications:
* Add a 5.10 nightly docker compose file
* Update the 5.9 compose file to stop using nightlies
* Update benchmark thresholds
Result:
Remove add CI support for 5.10
* Add `Certificate.PrivateKey` description
and update `Certificate.PublicKey` description to disambiguate from private keys.
* secure enclave is only a thing on darwin
* update tests
Motivation:
PublicKey doesn't conform to DERImplicitlyTaggable which was an oversight during development.
Modifications:
Add conformance to DERImplicitlyTaggable, PEMSerializable, and PEMParsable.
Result:
PublicKey is DERImplicitlyTaggable.
Co-authored-by: David Nadoba <d_nadoba@apple.com>
Motivation
We prefer issuers that have a subject key identifier that matches the authority key identifier of the certificate for which were are searching for an issuer. However, currently we have flipped this sort order.
Modifications
Flip preference order
update test
add test which would previously fail
Result
We actually prefer certificates in the order described above.
Note that this is just a performance improvement.
Co-authored-by: Cory Benfield <lukasa@apple.com>
### Motivation
We have used `ASN1ObjectIdentifier` with just one component in tests but they need at least two components. `swift-asn1` has recently changed the initialiser to enforce this during initialisation time if initialised from a literal. Therefore our tests currently crash.
### Modifications
This PR changes all of our test `ASN1ObjectIdentifier`s to have at least two components.
### Results
Tests no longer crash
Only non-self-issued certificates count against the maxPathLength limit.
RFC Section 4.2.1.9. Basic Constraints:
> [...]
> The pathLenConstraint field is meaningful only if the cA boolean is
> asserted and the key usage extension, if present, asserts the
> keyCertSign bit (Section 4.2.1.3). In this case, it gives the
> maximum number of non-self-issued intermediate certificates that may
> follow this certificate in a valid certification path. (Note: The
> last certificate in the certification path is not an intermediate
> certificate, and is not included in this limit. Usually, the last
> certificate is an end entity certificate, but it can be a CA
> certificate.) A pathLenConstraint of zero indicates that no non-
> self-issued intermediate CA certificates may follow in a valid
> certification path. Where it appears, the pathLenConstraint field
> MUST be greater than or equal to zero. Where pathLenConstraint does
> not appear, no limit is imposed.
> [...]
> https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.9
Co-authored-by: Cory Benfield <lukasa@apple.com>
* `Custom[String|DebugString]Convertible` conformance
* save progress
* Apply new learned rules
debugDescription should be usable in structural display
* remove unused init
* remove unused properties
* Some fixups before review
* change description a bit around and respect RFC 2253
* add addition test
* fix license header and tests
* update allocation counters