@@ -48,22 +48,22 @@ var instanceWithCnSubject = name("myProject:myInstance")
4848// From the cloud-sql-jdbc-socket-factory project:
4949// core/src/test/java/com/google/cloud/sql/core/TestCertificateGenerator.java
5050type TLSCertificates struct {
51- ServerCaKeyPair * rsa. PrivateKey
52- SigningCaKeyPair * rsa. PrivateKey
53- ServerKeyPair * rsa. PrivateKey
54- ServerIntermediateCaKeyPair * rsa. PrivateKey
55- ServerSigningCaKeyPair * rsa. PrivateKey
56- ClientKeyPair * rsa.PrivateKey
57- DomainServerKeyPair * rsa.PrivateKey
58-
59- ServerCaCert * x509. Certificate
60- SigningCaCert * x509.Certificate
61- ServerCert * x509.Certificate
62- ServerIntermediateCaCert * x509.Certificate
63- CasServerCertificate * x509. Certificate
64- CasServerCertificateChain [] * x509. Certificate
65- DomainServerCertificate * x509.Certificate
66- clientCertExpires time. Time
51+ clientCertExpires time. Time
52+ projectName string
53+ instanceName string
54+ sans [] string
55+
56+ serverCaKeyPair * rsa.PrivateKey
57+ serverIntermediateCaKeyPair * rsa.PrivateKey
58+ clientSigningCaKeyPair * rsa. PrivateKey
59+
60+ serverCaCert * x509.Certificate
61+ serverIntermediateCaCert * x509.Certificate
62+ clientSigningCACertificate * x509.Certificate
63+
64+ serverKeyPair * rsa. PrivateKey
65+ serverCert * x509.Certificate
66+ casServerCertificate * x509. Certificate
6767}
6868
6969func mustGenerateKey () * rsa.PrivateKey {
@@ -76,55 +76,13 @@ func mustGenerateKey() *rsa.PrivateKey {
7676
7777// newTLSCertificates creates a new instance of the TLSCertificates.
7878func newTLSCertificates (projectName , instanceName string , sans []string , clientCertExpires time.Time ) * TLSCertificates {
79- oneYear := time .Now ().AddDate (1 , 0 , 0 )
80-
8179 c := & TLSCertificates {
82- clientCertExpires : clientCertExpires ,
83- ServerCaKeyPair : mustGenerateKey (),
84- SigningCaKeyPair : mustGenerateKey (),
85- ServerKeyPair : mustGenerateKey (),
86- ServerIntermediateCaKeyPair : mustGenerateKey (),
87- ServerSigningCaKeyPair : mustGenerateKey (),
88- ClientKeyPair : mustGenerateKey (),
89- DomainServerKeyPair : mustGenerateKey (),
80+ clientCertExpires : clientCertExpires ,
81+ projectName : projectName ,
82+ instanceName : instanceName ,
83+ sans : sans ,
9084 }
91-
92- c .ServerCaCert = mustBuildRootCertificate (serverCaSubject , c .ServerCaKeyPair )
93- c .SigningCaCert = mustBuildRootCertificate (signingCaSubject , c .SigningCaKeyPair )
94-
95- c .ServerCert = mustBuildSignedCertificate (
96- false ,
97- name (projectName + ":" + instanceName ),
98- c .ServerKeyPair ,
99- serverCaSubject ,
100- c .ServerCaKeyPair ,
101- oneYear ,
102- nil )
103-
104- c .ServerIntermediateCaCert =
105- mustBuildSignedCertificate (
106- true ,
107- intermediateCaSubject ,
108- c .ServerIntermediateCaKeyPair ,
109- serverCaSubject ,
110- c .ServerCaKeyPair ,
111- oneYear ,
112- nil )
113-
114- c .CasServerCertificate =
115- mustBuildSignedCertificate (
116- false ,
117- name ("" ),
118- c .ServerKeyPair ,
119- intermediateCaSubject ,
120- c .ServerIntermediateCaKeyPair ,
121- oneYear ,
122- sans )
123-
124- c .CasServerCertificateChain =
125- []* x509.Certificate {
126- c .CasServerCertificate , c .ServerIntermediateCaCert , c .ServerCaCert }
127-
85+ c .RotateCA ()
12886 return c
12987}
13088
@@ -252,7 +210,7 @@ func (ct *TLSCertificates) signWithClientKey(clientKey *rsa.PublicKey) ([]byte,
252210 KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCertSign ,
253211 BasicConstraintsValid : true ,
254212 }
255- certBytes , err := x509 .CreateCertificate (rand .Reader , cert , ct .SigningCaCert , clientKey , ct .SigningCaKeyPair )
213+ certBytes , err := x509 .CreateCertificate (rand .Reader , cert , ct .clientSigningCACertificate , clientKey , ct .clientSigningCaKeyPair )
256214 if err != nil {
257215 return nil , err
258216 }
@@ -273,9 +231,9 @@ func (ct *TLSCertificates) generateServerCertWithCn(cn string) *x509.Certificate
273231 return mustBuildSignedCertificate (
274232 false ,
275233 name (cn ),
276- ct .ServerKeyPair ,
234+ ct .serverKeyPair ,
277235 serverCaSubject ,
278- ct .ServerCaKeyPair ,
236+ ct .serverCaKeyPair ,
279237 time .Now ().Add (1 * time .Hour ), nil )
280238}
281239
@@ -286,16 +244,67 @@ func (ct *TLSCertificates) serverChain(serverCAMode string) []tls.Certificate {
286244 // if this server is running in legacy mode
287245 if serverCAMode == "" || serverCAMode == "GOOGLE_MANAGED_INTERNAL_CA" {
288246 return []tls.Certificate {{
289- Certificate : [][]byte {ct .ServerCert .Raw , ct .ServerCaCert .Raw },
290- PrivateKey : ct .ServerKeyPair ,
291- Leaf : ct .ServerCert ,
247+ Certificate : [][]byte {ct .serverCert .Raw , ct .serverCaCert .Raw },
248+ PrivateKey : ct .serverKeyPair ,
249+ Leaf : ct .serverCert ,
292250 }}
293251 }
294252
295253 return []tls.Certificate {{
296- Certificate : [][]byte {ct .CasServerCertificate .Raw , ct .ServerIntermediateCaCert .Raw , ct .ServerCaCert .Raw },
297- PrivateKey : ct .ServerKeyPair ,
298- Leaf : ct .CasServerCertificate ,
254+ Certificate : [][]byte {ct .casServerCertificate .Raw , ct .serverIntermediateCaCert .Raw , ct .serverCaCert .Raw },
255+ PrivateKey : ct .serverKeyPair ,
256+ Leaf : ct .casServerCertificate ,
299257 }}
300258
301259}
260+ func (ct * TLSCertificates ) ClientCAPool () * x509.CertPool {
261+ clientCa := x509 .NewCertPool ()
262+ clientCa .AddCert (ct .clientSigningCACertificate )
263+ return clientCa
264+ }
265+
266+ func (ct * TLSCertificates ) RotateClientCA () {
267+ ct .clientSigningCaKeyPair = mustGenerateKey ()
268+ ct .clientSigningCACertificate = mustBuildRootCertificate (signingCaSubject , ct .clientSigningCaKeyPair )
269+ }
270+
271+ func (ct * TLSCertificates ) RotateCA () {
272+ oneYear := time .Now ().AddDate (1 , 0 , 0 )
273+ ct .serverCaKeyPair = mustGenerateKey ()
274+ ct .clientSigningCaKeyPair = mustGenerateKey ()
275+ ct .serverKeyPair = mustGenerateKey ()
276+ ct .serverIntermediateCaKeyPair = mustGenerateKey ()
277+
278+ ct .serverCaCert = mustBuildRootCertificate (serverCaSubject , ct .serverCaKeyPair )
279+
280+ ct .serverIntermediateCaCert =
281+ mustBuildSignedCertificate (
282+ true ,
283+ intermediateCaSubject ,
284+ ct .serverIntermediateCaKeyPair ,
285+ serverCaSubject ,
286+ ct .serverCaKeyPair ,
287+ oneYear ,
288+ nil )
289+
290+ ct .casServerCertificate =
291+ mustBuildSignedCertificate (
292+ false ,
293+ name ("" ),
294+ ct .serverKeyPair ,
295+ intermediateCaSubject ,
296+ ct .serverIntermediateCaKeyPair ,
297+ oneYear ,
298+ ct .sans )
299+
300+ ct .serverCert = mustBuildSignedCertificate (
301+ false ,
302+ name (ct .projectName + ":" + ct .instanceName ),
303+ ct .serverKeyPair ,
304+ serverCaSubject ,
305+ ct .serverCaKeyPair ,
306+ oneYear ,
307+ nil )
308+
309+ ct .RotateClientCA ()
310+ }
0 commit comments