Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check EC point is on curve at Verifier instantiation #116

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions testdata/sign1-sign-0000.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"key": {
"kty": "EC",
"crv": "P-256",
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM"
"x": "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",
"y": "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0",
"d": "jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI"
},
"alg": "ES256",
"sign1::sign": {
Expand All @@ -32,4 +32,4 @@
},
"fixedOutputLength": 32
}
}
}
3 changes: 3 additions & 0 deletions verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func NewVerifier(alg Algorithm, key crypto.PublicKey) (Verifier, error) {
if !ok {
return nil, fmt.Errorf("%v: %w", alg, ErrAlgorithmMismatch)
}
if !vk.Curve.IsOnCurve(vk.X, vk.Y) {
shizhMSFT marked this conversation as resolved.
Show resolved Hide resolved
return nil, errors.New("public key point is not on curve")
}
return &ecdsaVerifier{
alg: alg,
key: vk,
Expand Down
29 changes: 29 additions & 0 deletions verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@ package cose
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"math/big"
"reflect"
"testing"
)

func mustBase64ToBigInt(s string) *big.Int {
val, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return new(big.Int).SetBytes(val)
}

func generateBogusECKey() *ecdsa.PublicKey {
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
// x-coord is not on curve p-256
X: mustBase64ToBigInt("MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqx7D4"),
Y: mustBase64ToBigInt("4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM"),
}
}

func TestNewVerifier(t *testing.T) {
// generate ecdsa key
ecdsaKey := generateTestECDSAKey(t).Public().(*ecdsa.PublicKey)
Expand All @@ -25,6 +45,9 @@ func TestNewVerifier(t *testing.T) {
rsaKeyLowEntropy = &key.PublicKey
}

// craft an EC public key with the x-coord not on curve
ecdsaKeyPointNotOnCurve := generateBogusECKey()

// run tests
tests := []struct {
name string
Expand Down Expand Up @@ -88,6 +111,12 @@ func TestNewVerifier(t *testing.T) {
alg: 0,
wantErr: true,
},
{
thomas-fossati marked this conversation as resolved.
Show resolved Hide resolved
name: "bogus ecdsa public key (point not on curve)",
alg: AlgorithmES256,
key: ecdsaKeyPointNotOnCurve,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down