Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Fix checks for D.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsha committed Dec 5, 2018
1 parent 25e77d5 commit b56d11b
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ func (key rawJSONWebKey) ecPublicKey() (*ecdsa.PublicKey, error) {
return nil, errors.New("square/go-jose: invalid EC key, missing x/y values")
}

// The length of this octet string MUST be the full size of a coordinate for
// the curve specified in the "crv" parameter.
// https://tools.ietf.org/html/rfc7518#section-6.2.1.2
if curveSize(curve) != len(key.X.data) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for x")
}
Expand Down Expand Up @@ -527,6 +530,9 @@ func (key rawJSONWebKey) ecPrivateKey() (*ecdsa.PrivateKey, error) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key, missing x/y/d values")
}

// The length of this octet string MUST be the full size of a coordinate for
// the curve specified in the "crv" parameter.
// https://tools.ietf.org/html/rfc7518#section-6.2.1.2
if curveSize(curve) != len(key.X.data) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for x")
}
Expand All @@ -535,7 +541,8 @@ func (key rawJSONWebKey) ecPrivateKey() (*ecdsa.PrivateKey, error) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for y")
}

if curveSize(curve) != len(key.D.data) {
// https://tools.ietf.org/html/rfc7518#section-6.2.2.1
if dSize(curve) != len(key.D.data) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key, wrong length for d")
}

Expand Down Expand Up @@ -566,19 +573,24 @@ func fromEcPrivateKey(ec *ecdsa.PrivateKey) (*rawJSONWebKey, error) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key")
}

// Calculating the size of D:
// The length of this octet string MUST be ceiling(log-base-2(n)/8)
// octets (where n is the order of the curve).
// https://tools.ietf.org/html/rfc7518#section-6.2.2.1
order := ec.PublicKey.Curve.Params().P
raw.D = newFixedSizeBuffer(ec.D.Bytes(), dSize(ec.PublicKey.Curve))

return raw, nil
}

// dSize returns the size in octets for the "d" member of an elliptic curve
// private key.
// The length of this octet string MUST be ceiling(log-base-2(n)/8)
// octets (where n is the order of the curve).
// https://tools.ietf.org/html/rfc7518#section-6.2.2.1
func dSize(curve elliptic.Curve) int {
order := curve.Params().P
bitLen := order.BitLen()
size := bitLen / 8
if bitLen%8 != 0 {
size = size + 1
}
raw.D = newFixedSizeBuffer(ec.D.Bytes(), size)

return raw, nil
return size
}

func fromSymmetricKey(key []byte) (*rawJSONWebKey, error) {
Expand Down

0 comments on commit b56d11b

Please sign in to comment.