Skip to content

PEMDocument(pemString:) does not support Windows line endings #66

@natevw

Description

@natevw

When initializing a PEMDocument (or in my case really a swift-certificates Certificate) via a pemString: which comes from a Windows-world service, an error is thrown that it "could not find PEM start marker". The same string piped to openssl x509 is recognized successfully.

I believe the problem is that

let (
beginDiscriminatorPrefix,
beginDiscriminatorInfix,
beginDiscriminatorSuffix
) = self.firstRangesOf(
prefix: "-----BEGIN ",
suffix: "-----\n"
)
looks only for a suffix of specifically "-----\n" and thus does not match on what in my case is "-----\r\n". (There may be more "\n"-dependent logic beyond that point but this is the source of the initial failure.)

The workaround I'm using is to pass in myPemString.replacingOccurences(of: "\r\n", with: "\n") but it would be nice if these libraries could handle line breaks more generically. I'm not sure if this is the most relevant standard but RFC 1421 says:

To represent the encapsulated text of a PEM message, the encoding function's output is delimited into text lines (using local conventions) […]

which I suppose could be argued that the PEM document conventions could to be local to the receiver but in this case my PEM document is using the "local conventions" of the sender :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugFeature doesn't work as expected.size/SSmall task. (A couple of hours of work.)status/needs-designNeeds further discussion and a concrete proposal.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions