@@ -107,7 +107,18 @@ func (ca *Cert) GenerateCustomX509(
107
107
template * x509.Certificate ,
108
108
) * Cert {
109
109
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 )
111
122
112
123
return & Cert {
113
124
CertPath : certPath ,
@@ -124,16 +135,16 @@ func createCert(
124
135
template , caCert * x509.Certificate ,
125
136
caKey * rsa.PrivateKey ,
126
137
) (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" )
130
140
131
141
if caKey == nil {
132
142
caKey = key
133
143
}
134
144
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
+ }
137
148
138
149
signedCert , err := x509 .CreateCertificate (rand .Reader , template , caCert , & key .PublicKey , caKey )
139
150
assertive .ErrorIsNil (testing , err , "certificate creation should succeed" )
@@ -144,16 +155,17 @@ func createCert(
144
155
}
145
156
146
157
data .Temp ().Dir (dir )
147
- certPath = data .Temp ().Path (dir , serial .String ()+ ".cert" )
148
- keyPath = data .Temp ().Path (dir , serial .String ()+ ".key" )
149
158
150
159
data .Temp ().SaveToWriter (func (writer io.Writer ) error {
151
160
return pem .Encode (writer , & pem.Block {Type : "RSA PRIVATE KEY" , Bytes : x509 .MarshalPKCS1PrivateKey (key )})
152
- }, keyPath )
161
+ }, dir , serial . String () + ".key" )
153
162
154
163
data .Temp ().SaveToWriter (func (writer io.Writer ) error {
155
164
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" )
157
169
158
170
return key , certPath , keyPath
159
171
}
0 commit comments