Skip to content

Commit c5ba0da

Browse files
Check EC point is on curve at Verifier instantiation (#116)
Signed-off-by: Thomas Fossati <[email protected]>
1 parent 4dbb9a7 commit c5ba0da

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

testdata/sign1-sign-0000.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"key": {
66
"kty": "EC",
77
"crv": "P-256",
8-
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
9-
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
10-
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM"
8+
"x": "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",
9+
"y": "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0",
10+
"d": "jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI"
1111
},
1212
"alg": "ES256",
1313
"sign1::sign": {
@@ -32,4 +32,4 @@
3232
},
3333
"fixedOutputLength": 32
3434
}
35-
}
35+
}

verifier.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func NewVerifier(alg Algorithm, key crypto.PublicKey) (Verifier, error) {
4646
if !ok {
4747
return nil, fmt.Errorf("%v: %w", alg, ErrAlgorithmMismatch)
4848
}
49+
if !vk.Curve.IsOnCurve(vk.X, vk.Y) {
50+
return nil, errors.New("public key point is not on curve")
51+
}
4952
return &ecdsaVerifier{
5053
alg: alg,
5154
key: vk,

verifier_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@ package cose
33
import (
44
"crypto"
55
"crypto/ecdsa"
6+
"crypto/elliptic"
67
"crypto/rand"
78
"crypto/rsa"
9+
"encoding/base64"
10+
"math/big"
811
"reflect"
912
"testing"
1013
)
1114

15+
func mustBase64ToBigInt(s string) *big.Int {
16+
val, err := base64.RawURLEncoding.DecodeString(s)
17+
if err != nil {
18+
panic(err)
19+
}
20+
return new(big.Int).SetBytes(val)
21+
}
22+
23+
func generateBogusECKey() *ecdsa.PublicKey {
24+
return &ecdsa.PublicKey{
25+
Curve: elliptic.P256(),
26+
// x-coord is not on curve p-256
27+
X: mustBase64ToBigInt("MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqx7D4"),
28+
Y: mustBase64ToBigInt("4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM"),
29+
}
30+
}
31+
1232
func TestNewVerifier(t *testing.T) {
1333
// generate ecdsa key
1434
ecdsaKey := generateTestECDSAKey(t).Public().(*ecdsa.PublicKey)
@@ -25,6 +45,9 @@ func TestNewVerifier(t *testing.T) {
2545
rsaKeyLowEntropy = &key.PublicKey
2646
}
2747

48+
// craft an EC public key with the x-coord not on curve
49+
ecdsaKeyPointNotOnCurve := generateBogusECKey()
50+
2851
// run tests
2952
tests := []struct {
3053
name string
@@ -88,6 +111,12 @@ func TestNewVerifier(t *testing.T) {
88111
alg: 0,
89112
wantErr: true,
90113
},
114+
{
115+
name: "bogus ecdsa public key (point not on curve)",
116+
alg: AlgorithmES256,
117+
key: ecdsaKeyPointNotOnCurve,
118+
wantErr: true,
119+
},
91120
}
92121
for _, tt := range tests {
93122
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)