@@ -29,10 +29,6 @@ const ()
29
29
var (
30
30
refreshMutex = & sync.Mutex {}
31
31
x509Certificate x509.Certificate
32
-
33
- rootCAs * x509.CertPool
34
- clientCAs * x509.CertPool
35
- clientAuth * tls.ClientAuthType
36
32
)
37
33
38
34
type KMS struct {
@@ -45,6 +41,7 @@ type KMS struct {
45
41
KeyRing string
46
42
Key string
47
43
KeyVersion string
44
+ x509Certificate * x509.Certificate
48
45
ECCRawOutput bool // for ECC keys, output raw signatures. If false, signature is ans1 formatted
49
46
SignatureAlgorithm x509.SignatureAlgorithm
50
47
}
@@ -59,27 +56,27 @@ func NewKMSCrypto(conf *KMS) (KMS, error) {
59
56
}
60
57
61
58
if conf .ProjectId == "" {
62
- return KMS {}, fmt .Errorf ("ProjectID cannot be null" )
59
+ return KMS {}, fmt .Errorf ("projectID cannot be null" )
63
60
}
64
61
65
62
ctx := context .Background ()
66
63
parentName := fmt .Sprintf ("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s" , conf .ProjectId , conf .LocationId , conf .KeyRing , conf .Key , conf .KeyVersion )
67
64
68
65
kmsClient , err := cloudkms .NewKeyManagementClient (ctx )
69
66
if err != nil {
70
- return KMS {}, fmt .Errorf ("Error getting kms client %v" , err )
67
+ return KMS {}, fmt .Errorf ("error getting kms client %v" , err )
71
68
}
72
69
defer kmsClient .Close ()
73
70
74
71
dresp , err := kmsClient .GetPublicKey (ctx , & kmspb.GetPublicKeyRequest {Name : parentName })
75
72
if err != nil {
76
- return KMS {}, fmt .Errorf ("Error getting GetPublicKey %v" , err )
73
+ return KMS {}, fmt .Errorf ("error getting GetPublicKey %v" , err )
77
74
}
78
75
pubKeyBlock , _ := pem .Decode ([]byte (dresp .Pem ))
79
76
80
77
conf .publicKey , err = x509 .ParsePKIXPublicKey (pubKeyBlock .Bytes )
81
78
if err != nil {
82
- return KMS {}, fmt .Errorf ("Error parsing PublicKey %v" , err )
79
+ return KMS {}, fmt .Errorf ("error parsing PublicKey %v" , err )
83
80
}
84
81
85
82
return * conf , nil
@@ -95,23 +92,26 @@ func (t KMS) TLSCertificate() (tls.Certificate, error) {
95
92
return tls.Certificate {}, fmt .Errorf ("public X509 certificate not specified" )
96
93
}
97
94
98
- pubPEM , err := os .ReadFile (t .PublicKeyFile )
99
- if err != nil {
100
- return tls.Certificate {}, fmt .Errorf ("unable to read keys %v" , err )
101
- }
102
- block , _ := pem .Decode ([]byte (pubPEM ))
103
- if block == nil {
104
- return tls.Certificate {}, fmt .Errorf ("failed to parse PEM block containing the public key" )
105
- }
106
- pub , err := x509 .ParseCertificate (block .Bytes )
107
- if err != nil {
108
- return tls.Certificate {}, fmt .Errorf ("failed to parse public key: %v " , err )
95
+ if t .x509Certificate == nil {
96
+ pubPEM , err := os .ReadFile (t .PublicKeyFile )
97
+ if err != nil {
98
+ return tls.Certificate {}, fmt .Errorf ("unable to read keys %v" , err )
99
+ }
100
+ block , _ := pem .Decode ([]byte (pubPEM ))
101
+ if block == nil {
102
+ return tls.Certificate {}, fmt .Errorf ("failed to parse PEM block containing the public key" )
103
+ }
104
+ pub , err := x509 .ParseCertificate (block .Bytes )
105
+ if err != nil {
106
+ return tls.Certificate {}, fmt .Errorf ("failed to parse public key: %v " , err )
107
+ }
108
+ t .x509Certificate = pub
109
109
}
110
- x509Certificate = * pub
110
+
111
111
var privKey crypto.PrivateKey = t
112
112
return tls.Certificate {
113
113
PrivateKey : privKey ,
114
- Leaf : & x509Certificate ,
114
+ Leaf : t . x509Certificate ,
115
115
Certificate : [][]byte {x509Certificate .Raw },
116
116
}, nil
117
117
}
0 commit comments