Skip to content

Commit

Permalink
feat(jws): support eddsa
Browse files Browse the repository at this point in the history
  • Loading branch information
beatt83 committed Jan 17, 2024
1 parent ca01114 commit 8304835
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Sources/JSONWebSignature/JWK+SigningAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ extension JWK {
default:
throw JWS.JWSError.unsupportedAlgorithm(keyType: keyType.rawValue, algorithm: algorithm, curve: curve.rawValue)
}
case .octetKeyPair:
guard let curve else { throw JWS.JWSError.missingCurve }
switch curve {
case .ed25519:
return SigningAlgorithm.EdDSA
default:
throw JWS.JWSError.unsupportedAlgorithm(
keyType: keyType.rawValue,
algorithm: algorithm,
curve: curve.rawValue
)
}
case .octetSequence:
switch algorithm {
case SigningAlgorithm.HS256.rawValue:
Expand All @@ -63,8 +75,6 @@ extension JWK {
default:
throw JWS.JWSError.unsupportedAlgorithm(keyType: keyType.rawValue, algorithm: algorithm, curve: curve?.rawValue)
}
default:
throw JWS.JWSError.unsupportedAlgorithm(keyType: keyType.rawValue, algorithm: algorithm, curve: curve?.rawValue)
}
}
}
17 changes: 17 additions & 0 deletions Tests/JWSTests/JWSJsonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,21 @@ final class JWSJsonTests: XCTestCase {

XCTAssertThrowsError(try JWS.verify(jwsJson: jws.data(using: .utf8)!, jwk: jwkWithoutKid))
}

func testJsonSerializationOneKeyOnlyEdDSA() throws {
var keyJWK = JWK.testingCurve25519KPair
keyJWK.keyID = "1"

let payload = "{\"iss\":\"joe\",\"exp\":1300819380,\"http://example.com/is_root\":true}"

let jws: Data = try JWS.jsonSerialization(payload: payload.data(using: .utf8)!, keys: [keyJWK])

let jsonSerilization = try JSONDecoder()
.decode(JWSJson<DefaultJWSHeaderImpl, DefaultJWSHeaderImpl>.self, from: jws)

XCTAssertEqual(jsonSerilization.signatures.count, 1)
XCTAssertEqual(try jsonSerilization.signatures.first!.validateAlg(), .EdDSA)
XCTAssertEqual(try jsonSerilization.signatures.first!.getKid(), "1")
XCTAssertTrue(try JWS.verify(jwsJson: jws, jwk: keyJWK))
}
}
5 changes: 5 additions & 0 deletions Tests/JWSTests/Mocks/JWK+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ extension JWK {
return privateKey.jwkRepresentation
}

static var testingCurve25519KPair: JWK {
let privateKey = Curve25519.Signing.PrivateKey()
return privateKey.jwkRepresentation
}

static var testingES256KPair: JWK {
let privateKey = try! secp256k1.Signing.PrivateKey()
return privateKey.jwkRepresentation
Expand Down

0 comments on commit 8304835

Please sign in to comment.