Skip to content

Commit

Permalink
fix(JWT): ES256K PEM handling
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-frade-iohk committed Mar 9, 2023
1 parent fe2a464 commit adf5a6d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Sources/SwiftJWT/ES256K.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class ES256KSigner: SignerAlgorithm {
// send utf8 encoded `header.claims` to BlueECC for signing
private func sign(_ data: Data) throws -> Data {
guard let keyString = String(data: key, encoding: .utf8) else {
throw JWTError.invalidPrivateKey
throw JWTError.invalidUTF8Data
}
let keyData = try stripKeyFromPEM(pem: keyString)
let privateKey = try secp256k1
.Signing
.PrivateKey(rawRepresentation: key)
.PrivateKey(rawRepresentation: keyData)

let signedData = try privateKey.ecdsa.signature(for: data)
return signedData.rawRepresentation
Expand Down Expand Up @@ -65,6 +66,10 @@ class ES256KVerifier: VerifierAlgorithm {
// Send the base64URLencoded signature and `header.claims` to BlueECC for verification.
private func verify(signature: Data, for data: Data) -> Bool {
do {
guard let keyString = String(data: key, encoding: .utf8) else {
throw JWTError.invalidUTF8Data
}
let keyData = try stripKeyFromPEM(pem: keyString)
let format: secp256k1.Format
switch key[0] {
case 0x02, 0x03:
Expand All @@ -90,3 +95,15 @@ class ES256KVerifier: VerifierAlgorithm {
}
}
}

private func stripKeyFromPEM(pem: String) throws -> Data {
let strippedKey = String(pem.filter { !" \n\t\r".contains($0) })
let pemComponents = strippedKey.components(separatedBy: "-----")
guard pemComponents.count == 5 else {
throw JWTError.missingPEMHeaders
}
guard let der = Data(base64Encoded: pemComponents[2]) else {
throw JWTError.missingPEMHeaders
}
return der
}

0 comments on commit adf5a6d

Please sign in to comment.