Skip to content

Commit b2e6e97

Browse files
committed
add ecds signature verification
1 parent ef0d83c commit b2e6e97

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

Sources/WebAuthn/CredentialPublicKey.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ struct EC2PublicKey: PublicKey {
8484
/// A byte string 32 bytes in length that holds the y coordinate of the key.
8585
let yCoordinate: [UInt8]
8686

87+
var rawRepresentation: [UInt8] { xCoordinate + yCoordinate }
88+
8789
init(publicKeyObject: CBOR, algorithm: COSEAlgorithmIdentifier) throws {
8890
self.algorithm = algorithm
8991

@@ -108,7 +110,6 @@ struct EC2PublicKey: PublicKey {
108110
}
109111

110112
func getString() throws -> String {
111-
let rawRepresentation = xCoordinate + yCoordinate
112113
switch algorithm {
113114
case .algES256:
114115
return try P256.Signing.PublicKey(rawRepresentation: rawRepresentation).pemRepresentation
@@ -122,7 +123,28 @@ struct EC2PublicKey: PublicKey {
122123
}
123124

124125
func verify(signature: Data, data: Data) throws {
125-
fatalError("Not implemented")
126+
switch algorithm {
127+
case .algES256:
128+
let ecdsaSignature = try P256.Signing.ECDSASignature(derRepresentation: signature)
129+
guard try P256.Signing.PublicKey(rawRepresentation: rawRepresentation)
130+
.isValidSignature(ecdsaSignature, for: data) else {
131+
throw WebAuthnError.badRequestData
132+
}
133+
case .algES384:
134+
let ecdsaSignature = try P384.Signing.ECDSASignature(derRepresentation: signature)
135+
guard try P384.Signing.PublicKey(rawRepresentation: rawRepresentation)
136+
.isValidSignature(ecdsaSignature, for: data) else {
137+
throw WebAuthnError.badRequestData
138+
}
139+
case .algES512:
140+
let ecdsaSignature = try P521.Signing.ECDSASignature(derRepresentation: signature)
141+
guard try P521.Signing.PublicKey(rawRepresentation: rawRepresentation)
142+
.isValidSignature(ecdsaSignature, for: data) else {
143+
throw WebAuthnError.badRequestData
144+
}
145+
default:
146+
throw WebAuthnError.unsupportedCOSEAlgorithm
147+
}
126148
}
127149
}
128150

0 commit comments

Comments
 (0)