Skip to content

Commit a3251e6

Browse files
devin-ai-integration[bot]Joe Petrich
andcommitted
[ENG-xxx] Fix mock KMS client public key encoding
Co-Authored-By: Joe Petrich <[email protected]>
1 parent 4f3343e commit a3251e6

File tree

1 file changed

+8
-27
lines changed

1 file changed

+8
-27
lines changed

kms_signer_test.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/ecdsa"
66
"crypto/elliptic"
7+
"crypto/x509"
78
"encoding/asn1"
89
"encoding/hex"
910
"encoding/pem"
@@ -47,40 +48,20 @@ func newMockKMSSigner(t *testing.T) *KMSSigner {
4748
}
4849

4950
func (m *mockKMSClient) GetPublicKey(ctx context.Context, req *kmspb.GetPublicKeyRequest) (*kmspb.PublicKey, error) {
50-
spki := struct {
51-
Algorithm struct {
52-
Algorithm asn1.ObjectIdentifier
53-
Parameters asn1.ObjectIdentifier
54-
}
55-
PublicKey asn1.BitString
56-
}{
57-
Algorithm: struct {
58-
Algorithm asn1.ObjectIdentifier
59-
Parameters asn1.ObjectIdentifier
60-
}{
61-
Algorithm: asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1},
62-
Parameters: asn1.ObjectIdentifier{1, 3, 132, 0, 10},
63-
},
64-
}
65-
66-
pubKeyBytes := crypto.FromECDSAPub(&m.privateKey.PublicKey)
67-
spki.PublicKey = asn1.BitString{
68-
Bytes: pubKeyBytes,
69-
BitLength: 8 * len(pubKeyBytes),
70-
}
71-
72-
der, err := asn1.Marshal(spki)
51+
52+
x509EncodedPub, err := x509.MarshalPKIXPublicKey(&m.privateKey.PublicKey)
7353
if err != nil {
74-
return nil, fmt.Errorf("failed to marshal SPKI: %w", err)
54+
return nil, fmt.Errorf("failed to marshal public key to X.509: %w", err)
7555
}
76-
56+
7757
pemBytes := pem.EncodeToMemory(&pem.Block{
7858
Type: "PUBLIC KEY",
79-
Bytes: der,
59+
Bytes: x509EncodedPub,
8060
})
81-
61+
8262
return &kmspb.PublicKey{
8363
Pem: string(pemBytes),
64+
Algorithm: kmspb.CryptoKeyVersion_EC_SIGN_SECP256K1_SHA256,
8465
}, nil
8566
}
8667

0 commit comments

Comments
 (0)