Commit Graph
233 Commits
Author SHA1 Message Date
aryan-25 2f797305c1 Add bridge between Certificate and Security.SecCertificate (#182)
* 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.
1.5.0
2024-07-02 08:31:19 -07:00
Alex HoppenandDavid Nadoba 6bd8beeeb8 Update swift-syntax link to swiftlang/swift-syntax (#181)
Co-authored-by: David Nadoba <d_nadoba@apple.com>
2024-06-25 07:58:22 -07: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
Przemysław Lenart be1264c502 Add missing import statement. (#180) 2024-06-20 16:09:06 +01:00
Neil Jones 939a6cc484 add support for riscv64 (#179) 2024-06-17 21:26:37 +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
1.4.0
2024-06-07 16:27:37 +01:00
hamzahrmalikandCory Benfield d93756ba92 Allow OneOfPolicies and AllOfPolicies to take throwing policy builders, to match Verifier (#173)
* 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>
2024-05-17 10:04:40 -07:00
Nicolas Bachschmidt 6f2dc4affc Use case-insensitive comparison when validating cert domain name (#175) 2024-05-15 13:58:23 -07:00
hamzahrmalik 83640c8097 Add OneOfPolicies and AllOfPolicies (#172)
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
1.3.0
2024-05-01 16:48:25 +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
David Nadoba 3b760c831b Make testCertificateDescription independent of system date formatting settings (#170) 2024-04-08 09:10:34 -07: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
David Nadoba 5194b38d36 Fix Sendable warnings from latest main compiler (#166)
* Fix `Sendable` warnings on main

* remove `@preconcurrency` from `import _CryptoExtras`

* move documentation to extensions
2024-03-21 18:14:07 +00:00
David Nadoba d71e4c4261 CountryName should be a PrintableString RDNA (#165) 2024-03-21 16:38:30 +01:00
George Barnett 074848243e Raise minimum Swift version to 5.8 (#164) 2024-03-11 08:52:35 +00:00
Saleem AbdulrasoolandYim Lee f53dbc4512 build: add support to vendor dependencies (#158)
* 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>
2024-01-31 15:08:37 -08:00
Si Beaumont 000be99464 Add missing availability guards in tests (#159) 2023-12-15 08:25:02 -08:00
David NadobaandCory Benfield bc566f8884 Simplify the internal representation of RDN.Attribute.Value (#156)
* 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>
1.2.0
2023-12-05 16:15:19 -08:00
David Nadoba 66143b83e0 Move ExpressibleByArrayLiteral conformance of _TinyArray to the declaring module (#157)
Motivation

Nightly compilers start complaining about retroactive conformances.

Modifications

- move conformance to declaring module
- optimise code to not allocate

Result

no warning, optimal generated code for:

```swift
func foo1(int: Int) -> _TinyArray<Int> {
    [int]
}
```

compiles down to:
```
output.foo1(int: Swift.Int) -> output._TinyArray<Swift.Int>:
        mov     rax, rdi
        xor     edx, edx
        ret
```

https://godbolt.org/z/a91fPe6or
2023-12-05 18:19:37 +00:00
Saleem AbdulrasoolandCory Benfield 12e07730f9 build: support building in Debug mode on Windows (#153)
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>
1.1.0
2023-12-01 05:13:59 -08: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
Saleem Abdulrasool 0369bc0313 build: simplify build rules (NFC) (#152)
Swift currently requires 3.19.x to build.  Bump the CMake minimum
version to 3.19 to allow us to remove some workarounds from earlier
releases.
2023-11-28 14:51:42 +00: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 3956075d19 Align swift-format configuration with other projects (#150)
* Align `swift-format` configuration with other projects

No code change. We already followed these rules.

* UseSynthesizedInitializer still needs to be set to false

The fix for https://github.com/apple/swift-format/issues/591 is not in `509.0.0` but only in `main`: https://github.com/apple/swift-format/commit/b896e0f8c74dd6e0d9780cd4a086714637be6acc
2023-10-16 15:37:22 +01:00
Yim Lee b6de7e7235 Remove note from README about API instability (#149)
I think we can remove that note now that the library has 1.0.0 tag.
2023-10-13 08:58:41 +01:00
Rick Newton-Rogers efc568c52c Add CI pipeline for Swift 5.10 (#145)
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
2023-10-04 17:42:02 +01:00
Franz Busch 01d7664523 Rename TinyArray.append benchmark (#142) 1.0.1 2023-09-25 10:11:14 -07:00
David Nadoba eddfe84a03 Remove Xcode specific benchmark documentation (#141)
It is now included in the official docs: https://github.com/ordo-one/package-benchmark/commit/070dee37abe72c0a3d7e0feff5dca3effd66b541
1.0.0
2023-09-25 02:22:19 -07:00
David Nadoba aa83add828 Update to swift-asn1 1.0.0 (#140)
and use `.git` suffix consistently for dependencies
2023-09-25 02:12:38 -07:00
David Nadoba 1cd75260e0 Update swift-format to 509.0.0 (#139) 2023-09-25 01:19:25 -07:00
David Nadoba fa95d35a65 Parse all WebPKI trust roots in Benchmarks (#138)
* Parse all WebPKI trust roots in Benchmarks

* swift-format
2023-09-25 08:09:22 +01:00
David Nadoba 2876baab8a Update allowed swift-crypto version range to 2.5.0..<4.0.0 (#137) 2023-09-22 16:00:34 +02:00
David Nadoba 7815247199 Adopt package-benchmark (#125)
* Add `package-benchmark`

* use local dependencies

* split into multiple targets

* fix benchmark plugin

* refactor file structure

* self review

* Fix review comment

* Apply review feedback

* use `scaledIterations`

* don’t require the parent folder to be named `swift-certificates`

* install jemalloc

* use new `metricP90AbsoluteThresholds` export format

* update metrics

* use automated threshold import

* merge main and update allocation counters

* adopt `swift-format` for benchmarks

* add license headers

* use latest threshold file format

* delete old integration tests

* use released version

* revork folder structure

and add BlackHole module

* crash in debug configuration

* swift-format

* update baselines

* Move to single target

* update swift-format script

* enable arc for `Verifier`

* update to `package-benchmark` `1.11.0`

* update to `package-benchmark` `1.11.1`

* update benchmark metrics

* update metrics captured

* include release count

* move `.benchmarkBaselines` to nested `.gitignore`

* swift-format

* Fix `.gitignore`

* Add documentation

* change wording

* remove version number

* remove benchmarks folder

* add uppercase Benchmarks folder

* fix formatting script

* update readme with new uppercase folder name

* update Benchmarks folder name in docker compose

* drop allocation limits
2023-09-21 14:17:49 +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
Alexander Cyon 216a8a1166 Fix typos in Sources and Tests (#133)
* Fix typos in Sources and Tests

* Address PR review comments

* Second attempt to rectify tab instead of spaces.
2023-09-04 10:34:25 +01:00
David Nadoba ef10b819c7 Add Certificate.PrivateKey description (#132)
* Add `Certificate.PrivateKey` description

and update `Certificate.PublicKey` description to disambiguate from private keys.

* secure enclave is only a thing on darwin

* update tests
1.0.0-beta.1
2023-08-25 08:32:40 +01:00
David Nadoba 708eb05b1f Use beta release of swift-asn1 (#131)
* Use beta release of `swift-asn1`

* update swift-asn1 in integeration tests as well

* allocation have improved 🥳
2023-08-24 17:27:47 +01:00
Stefana-Ioana DrancaandDavid Nadoba 44a0c84fce Make PublicKey DERImplicitlyTaggable (#129)
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>
2023-08-24 15:09:43 +01:00
David NadobaandCory Benfield 0a44251615 Fix preference of root certificates (#127)
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>
2023-08-24 06:32:50 -07:00
David Nadoba 26832755b5 Refactor ASN1UTF8StringView to ASN1TaggedStringView and use it for printable strings (#128)
* Refactor `ASN1UTF8StringView` to `ASN1TaggedStringView` and use it for `printable` strings

* run swift-format

* fix comments
2023-08-24 14:13:58 +01:00
David Nadoba 92ffb4827b Add PolicyFailureReason to support lazy failure reason string interpolation (#126)
* Add `PolicyFailureReason` to support lazy failure reason string interpolation

* fix allocation test compilation

* update allocation counters
2023-08-21 20:01:19 +01:00
David Nadoba b69a931b43 remove deprecated API (#123) 2023-08-21 06:32:51 -07:00
David Nadoba 088a22943d Use valid ASN1ObjectIdentifier in tests (#124)
### 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
2023-08-18 02:23:30 -07:00
David Nadoba 1442890101 Allow RelativeDistinguishedNameConvertible to fail gracefully (#122) 2023-08-16 13:01:49 +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 98e0f85457 Do not count self-issued certificates against maxPathLength (#119)
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>
2023-08-10 03:58:44 -07:00
David Nadoba 21a8721886 Reject v1 Certificates with extensions (#118)
v1 certificates do not support extensions and any v1 certificate we encounter with extensions can’t be used to verify a chain.
2023-08-10 11:43:21 +01:00
David Nadoba 3c30a4a095 Use Certificate.description instead of dump for debugging flaky tests (#117) 2023-08-09 11:19:27 +01:00
David Nadoba 619756c18f Add custom String representation to Certificate and related types (#116)
* `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
2023-08-09 08:30:09 +01:00