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

Commit

Permalink
feat: enable ed25519 in jwkkid.BuildJWK()
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Burlacu <[email protected]>
  • Loading branch information
Filip Burlacu committed Dec 19, 2022
1 parent e290a6c commit 3edf7e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pkg/doc/util/jwkkid/kid_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func CreateKID(keyBytes []byte, kt kms.KeyType) (string, error) {

return x25519KID, nil
case kms.ED25519Type: // go-jose JWK thumbprint of Ed25519 has a bug, manually build it and build its resulting KID.
// TODO remove `case kms.ED25519Type` when go-jose fixes Ed25519 JWK thumbprint.
// Also remove `createED25519KID(keyBytes []byte)` function further below.
ed25519KID, err := createED25519KID(keyBytes)
if err != nil {
return "", fmt.Errorf("createKID: %w", err)
Expand Down Expand Up @@ -182,7 +184,7 @@ func curveSize(crv elliptic.Curve) int {
}

// BuildJWK builds a go jose JWK from keyBytes with key type kt.
func BuildJWK(keyBytes []byte, kt kms.KeyType) (*jwk.JWK, error) {
func BuildJWK(keyBytes []byte, kt kms.KeyType) (*jwk.JWK, error) { //nolint: gocyclo
var (
j *jwk.JWK
err error
Expand All @@ -194,13 +196,11 @@ func BuildJWK(keyBytes []byte, kt kms.KeyType) (*jwk.JWK, error) {
if err != nil {
return nil, fmt.Errorf("buildJWK: failed to build JWK from ecdsa DER key: %w", err)
}
// TODO remove `case kms.ED25519Type` in CreateKID() and uncomment below case when go-jose fixes Ed25519
// JWK thumbprint. Also remove `createED25519KID(keyBytes []byte)` function further below.
// case kms.ED25519Type:
// j, err = jwksupport.JWKFromKey(ed25519.PublicKey(keyBytes))
// if err != nil {
// return nil, fmt.Errorf("buildJWK: failed to build JWK from ed25519 key: %w", err)
// }
case kms.ED25519Type:
j, err = jwksupport.JWKFromKey(ed25519.PublicKey(keyBytes))
if err != nil {
return nil, fmt.Errorf("buildJWK: failed to build JWK from ed25519 key: %w", err)
}
case kms.ECDSAP256TypeIEEEP1363, kms.ECDSAP384TypeIEEEP1363, kms.ECDSAP521TypeIEEEP1363, kms.ECDSASecp256k1IEEEP1363:
c := getCurveByKMSKeyType(kt)
x, y := elliptic.Unmarshal(c, keyBytes)
Expand Down
11 changes: 11 additions & 0 deletions pkg/doc/util/jwkkid/kid_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ func TestBuildJWKX25519(t *testing.T) {
})
}

func TestBuildJWK_Ed25519(t *testing.T) {
t.Run("success", func(t *testing.T) {
pubKey, _, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)

jwk, err := BuildJWK(pubKey, kms.ED25519Type)
require.NoError(t, err)
require.NotNil(t, jwk)
})
}

func TestCreateED25519KID_Failure(t *testing.T) {
key := &cryptoapi.PublicKey{
Curve: "Ed25519",
Expand Down

0 comments on commit 3edf7e0

Please sign in to comment.