Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CRLF line endings to PEMDocument #68

Merged
merged 4 commits into from
Oct 9, 2024

Conversation

aryan-25
Copy link
Contributor

Motivation:

As per RFC 1421, an encoded PEM message is divided into text lines of 64 characters each:

To represent the encapsulated text of a PEM message, the encoding function's output is delimited into text lines (using local conventions), with each line except the last containing exactly 64 printable characters and the final line containing 64 or fewer printable characters.

We currently only support parsing PEM strings with LF line endings. We would like to add support for PEM strings containing CRLF line endings too (GitHub Issue #66).

Modifications:

Added an additional argument lineEnding to PEMDocument's initializers and functions. This argument defaults to LF.

Result:

PEM strings containing CRLF line endings can be used to construct a PEMDocument.

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! However, I think we should consider taking a different tack.

Rather than ask the user which line-ending is in use when we parse, we should detect it ourselves. As we know what the start of the PEM document looks like, we can work out what the line ending format will be. Bonus points for us if we can tolerate both line endings mixed arbitrarily, but worst-case we should look for the first line ending and work it out from there.

As a sidebar, I don't think we need support for serializing in both formats: we can confidently just serialize with LF line ending only.

@@ -218,6 +218,25 @@ struct LazyPEMDocument {
}

extension Substring.UTF8View {
/// Checks whether `self` starts with a new line character.
/// - Returns: `true` if `self` starts with a new line character; otherwise, `false`.
fileprivate func startsWithNewLine() -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fileprivate func startsWithNewLine() -> Bool {
fileprivate var startsWithNewLine(): Bool {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed startsWithNewLine entirely since there are no longer any references to it

Sources/SwiftASN1/Basic ASN1 Types/PEMDocument.swift Outdated Show resolved Hide resolved
)
suffix: "-----"
),
self[beginDiscriminatorSuffix.upperBound...].startsWithNewLine(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is redundant: the guard let below covers it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed this and just keeping the call to offsetNewLine in the line below.

Sources/SwiftASN1/Basic ASN1 Types/PEMDocument.swift Outdated Show resolved Hide resolved
// The current line cannot contain any "\n" and the end index must be a new line.
guard message[..<expectedNewLineIndex].firstIndex(of: UInt8(ascii: "\n")) == nil,
message[expectedNewLineIndex...].startsWithNewLine(),
let nextLineStart = message[expectedNewLineIndex...].offsetNewLine()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard let also supersedes the other call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, like above.

@Lukasa Lukasa added the semver/patch No public API change. label Oct 9, 2024
@Lukasa
Copy link
Contributor

Lukasa commented Oct 9, 2024

Nice patch, thanks! ✨

@Lukasa Lukasa merged commit a6cdd4d into apple:main Oct 9, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants