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.
### 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.
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>
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.
## 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
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>
* 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>
### 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.
* 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>
* 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
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>
* Add support for CRLF line endings to `PEMDocument`
* Remove `lineEnding` as an argument and instead detect line ending automatically.
* Fix description
* Refactor
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>
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`.
* 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
### 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
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.
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.
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.