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

chore: port latest afgo changes #2

Merged
merged 1 commit into from
Aug 23, 2023
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
2 changes: 1 addition & 1 deletion kms/localkms/localkms.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (l *LocalKMS) CreateAndExportPubKeyBytes(kt kmsapi.KeyType, opts ...kmsapi.
// Note: The key handle created is not stored in the KMS, it's only useful to execute the crypto primitive
// associated with it.
func (l *LocalKMS) PubKeyBytesToHandle(pubKey []byte, kt kmsapi.KeyType, opts ...kmsapi.KeyOpts) (interface{}, error) {
return publicKeyBytesToHandle(pubKey, kt, opts...)
return PublicKeyBytesToHandle(pubKey, kt, opts...)
}

// ImportPrivateKey will import privKey into the KMS storage for the given keyType then returns the new key id and
Expand Down
18 changes: 9 additions & 9 deletions kms/localkms/pubkey_export_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestPubKeyExportAndRead(t *testing.T) {
t.Run(tt.tcName, func(t *testing.T) {
exportedKeyBytes, origKH := exportRawPublicKeyBytes(t, tt.keyTemplate, false)

kh, err := publicKeyBytesToHandle(exportedKeyBytes, tt.keyType)
kh, err := PublicKeyBytesToHandle(exportedKeyBytes, tt.keyType)
require.NoError(t, err)
require.NotEmpty(t, kh)

Expand Down Expand Up @@ -179,49 +179,49 @@ func exportRawPublicKeyBytes(t *testing.T, keyTemplate *tinkpb.KeyTemplate, expe

func TestNegativeCases(t *testing.T) {
t.Run("test publicKeyBytesToHandle with empty pubKey", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{}, kms.ECDSAP256TypeIEEEP1363)
kh, err := PublicKeyBytesToHandle([]byte{}, kms.ECDSAP256TypeIEEEP1363)
require.EqualError(t, err, "pubKey is empty")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with empty KeyType", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, "")
kh, err := PublicKeyBytesToHandle([]byte{1}, "")
require.EqualError(t, err, "error getting marshalled proto key: invalid key type")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with bad pubKey and ECDSAP256TypeDER", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, kms.ECDSAP256TypeDER)
kh, err := PublicKeyBytesToHandle([]byte{1}, kms.ECDSAP256TypeDER)
require.EqualError(t, err, "error getting marshalled proto key: asn1: syntax error: truncated tag or length")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with bad pubKey and ECDSAP256TypeIEEEP1363", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, kms.ECDSAP256TypeIEEEP1363)
kh, err := PublicKeyBytesToHandle([]byte{1}, kms.ECDSAP256TypeIEEEP1363)
require.EqualError(t, err, "error getting marshalled proto key: failed to unamrshal public ecdsa key")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with bad pubKey and ECDSAP384TypeDER", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, kms.ECDSAP384TypeDER)
kh, err := PublicKeyBytesToHandle([]byte{1}, kms.ECDSAP384TypeDER)
require.EqualError(t, err, "error getting marshalled proto key: asn1: syntax error: truncated tag or length")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with bad pubKey and ECDSAP384TypeIEEEP1363", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, kms.ECDSAP384TypeIEEEP1363)
kh, err := PublicKeyBytesToHandle([]byte{1}, kms.ECDSAP384TypeIEEEP1363)
require.EqualError(t, err, "error getting marshalled proto key: failed to unamrshal public ecdsa key")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with bad pubKey and ECDSAP521TypeDER", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, kms.ECDSAP521TypeDER)
kh, err := PublicKeyBytesToHandle([]byte{1}, kms.ECDSAP521TypeDER)
require.EqualError(t, err, "error getting marshalled proto key: asn1: syntax error: truncated tag or length")
require.Empty(t, kh)
})

t.Run("test publicKeyBytesToHandle with bad pubKey and ECDSAP521TypeIEEEP1363", func(t *testing.T) {
kh, err := publicKeyBytesToHandle([]byte{1}, kms.ECDSAP521TypeIEEEP1363)
kh, err := PublicKeyBytesToHandle([]byte{1}, kms.ECDSAP521TypeIEEEP1363)
require.EqualError(t, err, "error getting marshalled proto key: failed to unamrshal public ecdsa key")
require.Empty(t, kh)
})
Expand Down
6 changes: 5 additions & 1 deletion kms/localkms/pubkey_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import (
secp256k1subtle "github.com/trustbloc/kms-crypto-go/crypto/tinkcrypto/primitive/secp256k1/subtle"
)

func publicKeyBytesToHandle(pubKey []byte, kt kms.KeyType, opts ...kms.KeyOpts) (*keyset.Handle, error) {
// PublicKeyBytesToHandle will create and return a key handle for pubKey of type kt
// it returns an error if it failed creating the key handle
// Note: The key handle created is not stored in the KMS, it's only useful to execute the crypto primitive
// associated with it.
func PublicKeyBytesToHandle(pubKey []byte, kt kms.KeyType, opts ...kms.KeyOpts) (*keyset.Handle, error) {
if len(pubKey) == 0 {
return nil, fmt.Errorf("pubKey is empty")
}
Expand Down
Loading