@@ -107,7 +107,18 @@ func (ca *Cert) GenerateCustomX509(
107107 template * x509.Certificate ,
108108) * Cert {
109109 silentT := assertive .WithSilentSuccess (helpers .T ())
110- key , certPath , keyPath := createCert (silentT , data , underDirectory , template , ca .cert , ca .key )
110+
111+ var (
112+ cert * x509.Certificate
113+ key * rsa.PrivateKey
114+ )
115+
116+ if ca != nil {
117+ cert = ca .cert
118+ key = ca .key
119+ }
120+
121+ key , certPath , keyPath := createCert (silentT , data , underDirectory , template , cert , key )
111122
112123 return & Cert {
113124 CertPath : certPath ,
@@ -124,16 +135,16 @@ func createCert(
124135 template , caCert * x509.Certificate ,
125136 caKey * rsa.PrivateKey ,
126137) (key * rsa.PrivateKey , certPath , keyPath string ) {
127- if caCert == nil {
128- caCert = template
129- }
138+ key , err := rsa .GenerateKey (rand .Reader , keyLength )
139+ assertive .ErrorIsNil (testing , err , "key generation should succeed" )
130140
131141 if caKey == nil {
132142 caKey = key
133143 }
134144
135- key , err := rsa .GenerateKey (rand .Reader , keyLength )
136- assertive .ErrorIsNil (testing , err , "key generation should succeed" )
145+ if caCert == nil {
146+ caCert = template
147+ }
137148
138149 signedCert , err := x509 .CreateCertificate (rand .Reader , template , caCert , & key .PublicKey , caKey )
139150 assertive .ErrorIsNil (testing , err , "certificate creation should succeed" )
@@ -144,16 +155,17 @@ func createCert(
144155 }
145156
146157 data .Temp ().Dir (dir )
147- certPath = data .Temp ().Path (dir , serial .String ()+ ".cert" )
148- keyPath = data .Temp ().Path (dir , serial .String ()+ ".key" )
149158
150159 data .Temp ().SaveToWriter (func (writer io.Writer ) error {
151160 return pem .Encode (writer , & pem.Block {Type : "RSA PRIVATE KEY" , Bytes : x509 .MarshalPKCS1PrivateKey (key )})
152- }, keyPath )
161+ }, dir , serial . String () + ".key" )
153162
154163 data .Temp ().SaveToWriter (func (writer io.Writer ) error {
155164 return pem .Encode (writer , & pem.Block {Type : "CERTIFICATE" , Bytes : signedCert })
156- }, keyPath )
165+ }, dir , serial .String ()+ ".cert" )
166+
167+ certPath = data .Temp ().Path (dir , serial .String ()+ ".cert" )
168+ keyPath = data .Temp ().Path (dir , serial .String ()+ ".key" )
157169
158170 return key , certPath , keyPath
159171}
0 commit comments