@@ -6,9 +6,11 @@ import (
6
6
"crypto/ecdsa"
7
7
"crypto/rsa"
8
8
"crypto/sha256"
9
+ "crypto/sha512"
9
10
"crypto/x509"
10
11
"encoding/asn1"
11
12
"encoding/base64"
13
+ "hash"
12
14
"math/big"
13
15
14
16
"github.com/gofrs/uuid"
@@ -220,7 +222,7 @@ type PubKeyCredentialRequestOpts struct {
220
222
func (c * PubKeyCredential ) Verify (clientData []byte , authnData []byte , sig string ) error {
221
223
sigbytes , err := base64 .RawURLEncoding .DecodeString (sig )
222
224
if err != nil {
223
- return errors . Internal
225
+ return E ( EInternal , "unable to decode signature" )
224
226
}
225
227
226
228
clientDataHash := sha256 .Sum256 (clientData )
@@ -231,27 +233,33 @@ func (c *PubKeyCredential) Verify(clientData []byte, authnData []byte, sig strin
231
233
return c .verifySignature (message , sigbytes )
232
234
}
233
235
236
+ var hashers = map [COSEAlgorithmIdentifier ]func () hash.Hash {
237
+ COSEAlgES256 : sha256 .New ,
238
+ COSEAlgEdDSA : sha512 .New ,
239
+ COSEAlgRS256 : sha256 .New ,
240
+ }
241
+
234
242
func (c * PubKeyCredential ) verifySignature (message []byte , sig []byte ) error {
235
243
bytes , err := base64 .RawURLEncoding .DecodeString (c .PubKey )
236
244
if err != nil {
237
- return errors . NewInternal ( l . T ( "Couldn't decode pubkey") )
245
+ return E ( EInvalid , "unable to decode pubkey" )
238
246
}
239
247
240
248
parsed , err := x509 .ParsePKIXPublicKey (bytes )
241
249
242
250
if err != nil {
243
- return errors . NewInternal ( l . T ( "Unable to parse pubkey") )
251
+ return E ( EInvalid , "unable to parse pubkey" )
244
252
}
245
253
246
254
hasher := hashers [COSEAlgorithmIdentifier (c .PubKeyAlg )]
247
255
if hasher == nil {
248
- return errors . NewInternal ( l . T ( "Unsupported hashing algorithm") )
256
+ return E ( EInvalid , "unsupported hashing algorithm" )
249
257
}
250
258
251
259
h := hasher ()
252
260
_ , err = h .Write (message )
253
261
if err != nil {
254
- return errors . NewInternal ( l . T ( "Couldn't hash the data") )
262
+ return E ( EInternal , "unable to hash the data" )
255
263
}
256
264
257
265
digest := h .Sum (nil )
@@ -263,23 +271,22 @@ func (c *PubKeyCredential) verifySignature(message []byte, sig []byte) error {
263
271
}
264
272
var ecdsaSig ecdsaSignature
265
273
if rest , err := asn1 .Unmarshal (sig , & ecdsaSig ); err != nil {
266
- return errors . Internal
274
+ return E ( EInternal )
267
275
} else if len (rest ) != 0 {
268
- return errors . NewBadRequest ( l . T ( "Trailing data after ECDSA signature") )
276
+ return E ( EInvalid , "trailing data after ECDSA signature" )
269
277
}
270
278
if ecdsaSig .R .Sign () <= 0 || ecdsaSig .S .Sign () <= 0 {
271
- return errors . NewBadRequest ( l . T ( "ECDSA signature contained zero or negative values" ) )
279
+ return E ( EInvalid , "ECDSA signature contained zero or negative values" )
272
280
}
273
281
if ! ecdsa .Verify (pk , digest , ecdsaSig .R , ecdsaSig .S ) {
274
- return errors . NewBadRequest ( l . T ( "ECDSA signature verification failed" ) )
282
+ return E ( EInvalid , "ECDSA signature verification failed" )
275
283
}
276
284
case * rsa.PublicKey :
277
285
if err := rsa .VerifyPKCS1v15 (pk , crypto .SHA256 , digest , sig ); err != nil {
278
- return errors . NewBadRequest ( l . T ( "RSA signature verification failed" ) )
286
+ return E ( EInvalid , "RSA signature verification failed" )
279
287
}
280
288
default :
281
- return errors .NewInternal ("Unsupported key type" )
282
-
289
+ return E (EInternal , "unsupported key type" )
283
290
}
284
291
285
292
return nil
0 commit comments