Skip to content

Commit

Permalink
fix: no longer use separate public and private keys in HSM key manager (
Browse files Browse the repository at this point in the history
  • Loading branch information
aarmam authored Mar 2, 2023
1 parent 016569c commit 375bd5a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
12 changes: 2 additions & 10 deletions hsm/manager_hsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (m *KeyManager) GetKeySet(ctx context.Context, set string) (*jose.JSONWebKe
}

func (m *KeyManager) DeleteKey(ctx context.Context, set, kid string) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GetKeySet")
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.DeleteKey")
defer span.End()
attrs := map[string]string{
"set": set,
Expand Down Expand Up @@ -217,7 +217,7 @@ func (m *KeyManager) DeleteKey(ctx context.Context, set, kid string) error {
}

func (m *KeyManager) DeleteKeySet(ctx context.Context, set string) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GetKeySet")
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.DeleteKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
Expand Down Expand Up @@ -361,14 +361,6 @@ func createKeys(key crypto11.Signer, kid, alg, use string) []jose.JSONWebKey {
Certificates: []*x509.Certificate{},
CertificateThumbprintSHA1: []uint8{},
CertificateThumbprintSHA256: []uint8{},
}, {
Algorithm: alg,
Use: use,
Key: key.Public(),
KeyID: kid,
Certificates: []*x509.Certificate{},
CertificateThumbprintSHA1: []uint8{},
CertificateThumbprintSHA256: []uint8{},
}}
}

Expand Down
8 changes: 0 additions & 8 deletions hsm/manager_hsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,13 +883,5 @@ func createJSONWebKeys(keyPair *MockSignerDecrypter, kid string, alg string, use
Certificates: []*x509.Certificate{},
CertificateThumbprintSHA1: []uint8{},
CertificateThumbprintSHA256: []uint8{},
}, {
Algorithm: alg,
Use: use,
Key: keyPair.Public(),
KeyID: kid,
Certificates: []*x509.Certificate{},
CertificateThumbprintSHA1: []uint8{},
CertificateThumbprintSHA256: []uint8{},
}}
}
6 changes: 1 addition & 5 deletions jwk/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ func TestHandlerWellKnown(t *testing.T) {
var known jose.JSONWebKeySet
err = json.NewDecoder(res.Body).Decode(&known)
require.NoError(t, err, "problem in decoding response")
if conf.HSMEnabled() {
require.Len(t, known.Keys, 2)
} else {
require.Len(t, known.Keys, 1)
}
require.Len(t, known.Keys, 1)

knownKey := known.Key("test-id-2")[0]
require.NotNil(t, knownKey, "Could not find key public")
Expand Down
8 changes: 5 additions & 3 deletions jwk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ func ExcludePrivateKeys(set *jose.JSONWebKeySet) *jose.JSONWebKeySet {

func ExcludeOpaquePrivateKeys(set *jose.JSONWebKeySet) *jose.JSONWebKeySet {
keys := new(jose.JSONWebKeySet)
for _, k := range set.Keys {
if _, opaque := k.Key.(jose.OpaqueSigner); !opaque {
keys.Keys = append(keys.Keys, k)
for i := range set.Keys {
if _, opaque := set.Keys[i].Key.(jose.OpaqueSigner); opaque {
keys.Keys = append(keys.Keys, josex.ToPublicKey(&set.Keys[i]))
} else {
keys.Keys = append(keys.Keys, set.Keys[i])
}
}
return keys
Expand Down
7 changes: 6 additions & 1 deletion jwk/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,13 @@ func TestExcludeOpaquePrivateKeys(t *testing.T) {
assert.NoError(t, err)
require.Len(t, opaqueKeys.Keys, 1)
opaqueKeys.Keys[0].Key = cryptosigner.Opaque(opaqueKeys.Keys[0].Key.(*rsa.PrivateKey))

keys := jwk.ExcludeOpaquePrivateKeys(opaqueKeys)
require.Len(t, keys.Keys, 0)

require.Len(t, keys.Keys, 1)
k := keys.Keys[0]
_, isPublic := k.Key.(*rsa.PublicKey)
assert.True(t, isPublic)
}

func TestGetOrGenerateKeys(t *testing.T) {
Expand Down

0 comments on commit 375bd5a

Please sign in to comment.