59 Commits
Author SHA1 Message Date
Cory Benfield 917b472dbe Correctly apply the end-of-contents octets to the right level (#134)
Motivation

In #125 we attempted to apply a hardening fix that would avoid allowing
truncation of BER content by applying an end-of-contents octets at the
wrong parse depth to a higher-level object. This fix was incomplete.

Modifications

Re-write the code to handle the end-of-contents octets as part of the
loop instead of attempting to detect it after the fact. Add
defense-in-depth for unexpected nodes.

Results

More robust against issues with end-of-contents octets.
2026-10-08 13:02:26 +01:00
Aryan Shah 3b6410f7de Validate base64 content (#131)
### Motivation

We currently ignore unknown characters when parsing the body of a PEM
document. We should also validate whether the body forms a valid base64
encoded string.

### Modifications

- Repurposed the `checkLineLengthsOfBase64EncodedString()` method to
also return the base64 encoded bytes with the line endings removed.
- Updated `LazyPEMDocument` to now require base64 encoded bytes
_without_ line endings.
- Now that the input doesn't have any line endings, the presence of any
non-base64 characters must mean that the input has invalid characters.
- As such, `LazyPEMDocument/decode()` was updated to no longer pass the
[`.ignoreUnknownCharacters`](https://developer.apple.com/documentation/foundation/nsdata/base64decodingoptions/ignoreunknowncharacters)
option to `Data(base64Encoded:)`.
- This means that when the body contains non-base64 characters,
`Data(base64Encoded:)` returns `nil` (we already throw
`ASN1Error.invalidPEMDocument` when receiving `nil`).
 
### Result

Better validation.
2026-09-09 11:43:22 +01:00
Hashim KhanandCory Benfield d9a5b37470 Throw on truncated BER indefinite length encodings (#127)
Fixes #125

A truncated BER indefinite length constructed value was parsed as
success. The last child was popped and treated as the end of contents
marker, so that child disappeared.

The parser now throws truncatedASN1Field when input ends without those
octets, matching X.690 section 8.1.3.6.1.

## Test plan
Added testRejectsTruncatedBERIndefiniteLengthWithoutEndMarker. Built the
SwiftASN1 product. Full XCTest suite needs Xcode.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-08-13 14:30:28 +00:00
aryan-25 a9a5efd40e Encode signed integers correctly (#124)
Motivation:

Currently, positive signed integers whose minimal big-endian
representation exactly fills N bytes (`neededBits % 8 == 0`) have their
top bit misread as the DER sign bit. `Int64(128)` is therefore encoded
as `[0x80]` and decoded back as `-128`. We should fix this.

Modifications:

- Updated the `FixedWidthInteger.neededBytes` implementation to return
`bytes + 1` for positive signed integers in this case, causing
`IntegerBytesCollection` to emit the required `0x00` sign byte.
- Added associated test cases.

Result:

Positive signed integers whose minimal big-endian representation exactly
fills N bytes (`neededBits % 8 == 0`) are now encoded correctly.
2026-05-27 14:29:30 +01:00
LamTrinh.Dev af3440c753 Fix typo in ASN1ObjectIdentifier BER initializer parameter name. (#120)
## Summary
Fix typo in the internal parameter name of
`ASN1ObjectIdentifier.berEncoded(withIdentifier:)` initializer.

  ## Changes
- Rename parameter `identiifer` → `identifier` in
`ASN1ObjectIdentifier.berEncoded(withIdentifier:)`
(ObjectIdentifier.swift:133)

  ## Impact
- The typo only affected the internal parameter name (external label
`withIdentifier` was correct)
  - No functional changes or breaking changes
  - Improves code readability and maintainability

  ## Testing
  - Existing tests continue to pass
  - No behavioral changes
2026-04-16 20:08:47 +00:00
mcecundaandCory Benfield 560256f684 Added visible string implementation with tests (#113)
Added missing ASN1 type (ASN1VisibleString) with tests. Implementations
is heavily inspired by existing implementation of ASN1PrintableString.
Differs only in validation rules (_isValid method) and encoding
identifier (defaultIdentifier property).

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-02-24 16:15:00 +00:00
Craz1k0ekandBram Kolkman 810496cf12 Fixed BMPString encoding (#111)
* Fixed #110 by using UTF-16 to encode the string literal
* Added a unit test to test the added functionality

Co-authored-by: Bram Kolkman <bramkolkman@thinkerium.com>
2025-12-01 18:07:08 +00:00
Rick Newton-Rogers 6578d7acfc Use centralized CMake checks (#96)
### Motivation:

We now have centrally-hosted CMake checks in the NIO repository which we
should use to reduce maintenance. The new checks also increase coverage
by performing a build with CMake as part of the checks.

### Modifications:

* Use the new workflow
* Commit formatting changes in the CMakeLists files from the new script
* Document how to update the CMakeLists files
* Remove the obsolete scripts

### Result:

Better CI coverage.
2025-03-17 11:15:52 +00:00
Gus Cairo 77d6ab4537 Import FoundationEssentials instead of Foundation when available (#89)
`FoundationEssentials` is more lightweight than `Foundation`, and so we
should switch to importing it when possible.
2025-02-24 11:39:35 +00:00
Cory BenfieldandNicolas Bachschmidt ae33e5941b Merge commit from fork
* Throw error instead of halting when the encoding is invalid

Signed-off-by: Cory Benfield <lukasa@apple.com>

* Avoid extra precondition failure

---------

Signed-off-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Nicolas Bachschmidt <nicolas@nunk.net>
2025-01-14 14:18:46 +00:00
Rick Newton-Rogers d016643222 Migrate to GitHub Actions reusable workflows (2) (#74)
* Prepare repo for new GHA checks

Motivation:

Migrate to GitHub Actions reusable workflows (2)

Modifications:

* Add GHA PR label checks
* Add GHA benchmarks checks
* Satisfy soundness checks

Result:

New soundness checks should be green and ready for adoption

* review comments
2024-10-15 13:04:58 +01:00
Peter AdamsandCory Benfield aeb9c927fd More Sendability annotiations (#72)
Motivation:

Completing annotations helps with coverage for consuming projects.

Modifications:

Mark more things and Sendable or not.

Result:

Consuming libraries more likely to get correct usage.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-10-14 10:49:11 +00:00
aryan-25 a6cdd4d974 Add support for CRLF line endings to PEMDocument (#68)
* Add support for CRLF line endings to `PEMDocument`

* Remove `lineEnding` as an argument and instead detect line ending automatically.

* Fix description

* Refactor
2024-10-09 10:38:02 +01:00
Paul SeligmanandPaul Seligman dd8cacc283 Update code to compile without issue when ExistentialAny enabled (#63)
Motivation:

We should feel confident that we compile clean when built in an environment
that has enabled upcoming Swift features.

When compiling swift-asn1 specifying `.enableUpcomingFeature("ExistentialAny")`
there are some minor warnings emitted by ASN1.swift.

Modifications:

Minor code changes to address surfaced warnings

Result:

Better confidence that we don't break downstreams that compile this code with
these upcoming features enabled.

Co-authored-by: Paul Seligman <pseligman@apple.com>
2024-09-13 23:59:52 -07:00
Clinton Nkwocha 41bee14cff Add rawFractionalSeconds property to GeneralizedTime (#53)
Motivation:

There is a possible overflow when computing a `Double` value for `fractionalSeconds` from an `ArraySlice` of bytes. Hence, access to this original `ArraySlice` is necessary.

Modifications:

- Add `rawFractionalSeconds` property to `GeneralizedTime`.
- Add a new `internal init` to `GeneralizedTime` that accepts `rawFractionalSeconds` instead of `fractionalSeconds` and generates the later from the former.
- Adjust the algorithm for computing `fractionalSeconds`, from a mathematical to a `String` computation. This is due to the precision mismatch between the mathematical computation and the native `Double` computation. This issue, which previously did not surface, now does, due to the newly included round-trip conversion from `fractionalSeconds` to `rawFractionalSeconds`.

Result:

- The `rawFractionalSeconds` property now provides the original `ArraySlice` from which `fractionalSeconds` was computed.
- `GeneralizedTime` can now compute `fractionalSeconds` from the provided `rawFractionalSeconds`.
2024-06-19 08:26:50 -07:00
aryan-25andCory Benfield e61ef9579a Add initializers for ASN1ObjectIdentifier (#56)
* Add initializers for ASN1ObjectIdentifier

* Added comments to public symbols. Updated error name.

* Add new Substring initializer. Refactor UInt Collection initializer.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-06-18 06:18:58 -07:00
David Zech 28d0e0717f Add support for parsing BER (#42) 2023-10-02 14:42:33 +01:00
David Nadoba 12c24ff59d Support multi PEM parsing and speed up PEM parsing in general (#39)
* Multi PEM support

* Multi PEM support

* swift-format

* Apply review feedback
2023-09-18 03:20:51 -07:00
David Nadoba 49557d7f0a Fix docs: constructed is no longer part of ASN1Identifier (#40) 2023-09-15 18:11:12 +01:00
David Nadoba d5636be054 remove outdated TODO (#38) 2023-08-25 10:50:10 +01:00
David Nadoba ef712c4268 Remove deprecated API (#37) 2023-08-22 11:08:52 +01:00
David Nadoba ce1faa434b Make ASN1ObjectIdentifiers internal representation be the encoded form (#36)
* Make `ASN1ObjectIdentifier`s internal representation be the encoded form

* validate during initialisation

* swift-format

* formatting
2023-08-17 15:53:25 +01:00
David Nadoba 43e0170e65 Adopt apple/swift-format (#35)
* default swift-format configuration

* Adopt `apple/swift-format`

* Fix soundness.sh

* Fix linting warnings

* update license header

* Apply review comments
2023-08-11 09:42:48 +01:00
David Nadoba 4758353894 Support macOS 10.14.x/iOS 12.x and older again (#34)
* Support macOS 10.14.x/iOS 12.x and older again

* Revert "Support macOS 10.14.x/iOS 12.x and older again"

This reverts commit 6eeba0327f.

* Erase concrete type manually
2023-07-03 10:25:35 +01:00
David Nadoba 0c33f188a8 Make IntegerBytesCollection conditionally Sendable (#33) 2023-06-30 12:09:52 +01:00
David Nadoba 8dd3d3192f Add method to lazily parse a SET from a ASN1Node (#32)
### Motiviation
We want reduce allocations in swift-certificates and need a way to parse without allocating an intermediate `Array`.

### Modifications
add `DER.lazySet(of:identifier:rootNode)` which returns its result without allocating.

### Result
We can eliminate allocations during parsing
2023-06-29 14:08:10 +01:00
Cory Benfield 748ece52a8 Avoid using stride(from:to:by:).reversed() (#31)
While this is a nice clear construction, it also has to allocate a temporary
Array, as the StrideTo type is not a BidirectionalCollection but a Sequence.
2023-05-24 09:32:27 +01:00
Cory Benfield 0700d170e8 Fix bug in comparing Generalized and UTC Time (#30)
The prior time comparison did not correctly handle wildly different
times. This patch resolves the issue.
2023-05-18 09:53:54 +01:00
Cory Benfield 1c5387a7ff Make GeneralizedTime and UTCTime Comparable. (#29)
In many cases it's useful to be able to place these into a total order.
This patch adds support for ordering them.
2023-05-18 07:31:59 +01:00
David Nadoba c43fef6dc0 Make PEMDocument Hashable (#28)
* Make `PEMDocument` `Hashable`

* Add `Sendable`
2023-04-21 10:36:29 +01:00
David Nadoba a53d9f676c Add PEM support through PEMRepresentable (#26)
* Add proper PEM support through `PEMRepresentable`

* Fix review comment and add more documentation

* Fix review comments

* Remove `allowedPEMDiscriminators`
2023-04-19 09:55:17 +01:00
Joey b3aaa046fa Documentation - Typos (#24) 2023-04-11 00:39:26 -07:00
Dave Poirier 155b8acdb2 Changed scope of PEMDocument to public (#22) 2023-03-23 10:30:15 +00:00
Cory Benfield 8fb2cd7edc Add support for serializing raw bytes.
In rare cases it's necessary for users to be able to do an end-run
around the serializer. This is most common in cases where some bytes
have had a signature calculated over them, and they are being
re-encoded. Even though DER is unambiguous, the ASN.1 objects themselves
may not be, and may permit multiple valid encodings. Allowing the bytes
to be re-serialized as they were preserves the integrity of the
signature.
2023-03-08 09:06:29 +00:00
Saleem Abdulrasool eb470005ae build: add in dispatch search (#18)
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-02 20:25:25 -08:00
David Nadoba 9d9a8a0c35 Fix review comments 2023-02-10 10:34:13 +01:00
David Nadoba 3794ff4734 Add < again 2023-02-09 06:59:35 +01:00
David Nadoba 321a1076ac Fix review comments 2023-02-08 18:09:17 +01:00
David Nadoba e5ebc6fcb6 rename ASN1BytesComparable to ASN1SetElement 2023-02-08 15:45:29 +01:00
David Nadoba 70d18b6847 SET OF de/serialization 2023-02-07 14:19:10 +01:00
Cory Benfield 658011c966 Further code review 2023-02-06 10:41:39 +00:00
Cory Benfield 85d12f6c3b Code review feedback 2023-02-06 10:24:04 +00:00
Cory Benfield 0fa1d76282 Move to struct-backed error pattern 2023-02-03 10:37:10 +00:00
David Nadoba 4363483e72 Add support for implicitly tagged optional fields 2023-02-02 14:49:58 +01:00
Yim Lee 4009d67f25 Add license header check for cmake files 2023-01-25 15:56:37 -08:00
Yim Lee 0f873377cd Set up CMake build 2023-01-25 13:49:19 -08:00
David Nadoba cfeb75ac80 Fix soundness
File extension of the CONTRIBUTORS file is `.txt` not `.md`
2022-11-30 11:17:30 +01:00
David Nadoba 080ce9bdc8 Use correct project name in license header
And add missing header in one file
2022-11-24 22:52:31 +01:00
Cory Benfield eb11d30c1d Remove constructed/primitive from the tag type
Constructed/primitive is an artefact of the encoder, not of the tag, so
we should remove it from the tag type itself.
2022-11-18 15:33:46 +00:00
Cory Benfield 8fb2002b4f Clarify unexpectedFieldType 2022-11-18 14:43:36 +00:00