@@ -16,7 +16,6 @@ import (
16
16
"crypto/x509/pkix"
17
17
"encoding/pem"
18
18
"fmt"
19
- "io/ioutil"
20
19
"math/big"
21
20
"net"
22
21
"os"
@@ -134,20 +133,20 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {
134
133
return & template , nil
135
134
}
136
135
137
- func generateCertificates (path * paths.Path ) {
138
- path .Join ("ca.cert.pem" ).Remove ()
139
- path .Join ("ca.key.pem" ).Remove ()
140
- path .Join ("cert.pem" ).Remove ()
141
- path .Join ("key.pem" ).Remove ()
136
+ func generateCertificates (certsDir * paths.Path ) {
137
+ certsDir .Join ("ca.cert.pem" ).Remove ()
138
+ certsDir .Join ("ca.key.pem" ).Remove ()
139
+ certsDir .Join ("cert.pem" ).Remove ()
140
+ certsDir .Join ("key.pem" ).Remove ()
142
141
143
142
// Create the key for the certification authority
144
143
caKey , err := generateKey ("P256" )
145
144
if err != nil {
146
145
log .Error (err .Error ())
147
146
os .Exit (1 )
148
147
}
149
- keyOutPath := path .Join ("ca.key.pem" ).String ()
150
- keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
148
+ keyOutPath := certsDir .Join ("ca.key.pem" ).String ()
149
+ keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 ) // Save key with user-only permission 0600
151
150
if err != nil {
152
151
log .Error (err .Error ())
153
152
os .Exit (1 )
@@ -158,27 +157,26 @@ func generateCertificates(path *paths.Path) {
158
157
159
158
// Create the certification authority
160
159
caTemplate , err := generateSingleCertificate (true )
161
-
162
160
if err != nil {
163
161
log .Error (err .Error ())
164
162
os .Exit (1 )
165
163
}
166
164
167
165
derBytes , _ := x509 .CreateCertificate (rand .Reader , caTemplate , caTemplate , publicKey (caKey ), caKey )
168
166
169
- certOutPath := path .Join ("ca.cert.pem" ). String ( )
170
- certOut , err := os .Create (certOutPath )
167
+ caCertOutPath := certsDir .Join ("ca.cert.pem" )
168
+ caCertOut , err := caCertOutPath .Create ()
171
169
if err != nil {
172
170
log .Error (err .Error ())
173
171
os .Exit (1 )
174
172
}
175
- pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
176
- certOut .Close ()
177
- log .Printf ("written %s" , certOutPath )
173
+ pem .Encode (caCertOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
174
+ caCertOut .Close ()
175
+ log .Printf ("written %s" , caCertOutPath )
178
176
179
- filePath := path .Join ("ca.cert.cer" ). String ( )
180
- ioutil .WriteFile (filePath , derBytes , 0644 )
181
- log .Printf ("written %s" , filePath )
177
+ caCertPath := certsDir .Join ("ca.cert.cer" )
178
+ caCertPath .WriteFile (derBytes )
179
+ log .Printf ("written %s" , caCertPath )
182
180
183
181
// Create the key for the final certificate
184
182
key , err := generateKey ("P256" )
@@ -187,8 +185,8 @@ func generateCertificates(path *paths.Path) {
187
185
os .Exit (1 )
188
186
}
189
187
190
- keyOutPath = path .Join ("key.pem" ).String ()
191
- keyOut , err = os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
188
+ keyOutPath = certsDir .Join ("key.pem" ).String ()
189
+ keyOut , err = os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 ) // Save key with user-only permission 0600
192
190
if err != nil {
193
191
log .Error (err .Error ())
194
192
os .Exit (1 )
@@ -199,28 +197,26 @@ func generateCertificates(path *paths.Path) {
199
197
200
198
// Create the final certificate
201
199
template , err := generateSingleCertificate (false )
202
-
203
200
if err != nil {
204
201
log .Error (err .Error ())
205
202
os .Exit (1 )
206
203
}
207
204
208
205
derBytes , _ = x509 .CreateCertificate (rand .Reader , template , caTemplate , publicKey (key ), caKey )
209
206
210
- certOutPath = path .Join ("cert.pem" ).String ()
211
- certOut , err = os .Create (certOutPath )
207
+ certOutPath := certsDir .Join ("cert.pem" ).String ()
208
+ certOut , err : = os .Create (certOutPath )
212
209
if err != nil {
213
210
log .Error (err .Error ())
214
211
os .Exit (1 )
215
212
}
216
213
pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
217
- certOut .Close ()
218
- log .Printf ("written %s" , certOutPath )
214
+ caCertOut .Close ()
215
+ log .Printf ("written %s" , caCertOutPath )
219
216
220
- certPath := path .Join ("cert.cer" ). String ( )
221
- ioutil .WriteFile (certPath , derBytes , 0644 )
217
+ certPath := certsDir .Join ("cert.cer" )
218
+ certPath .WriteFile (derBytes )
222
219
log .Printf ("written %s" , certPath )
223
-
224
220
}
225
221
226
222
func certHandler (c * gin.Context ) {
@@ -239,10 +235,10 @@ func deleteCertHandler(c *gin.Context) {
239
235
}
240
236
241
237
// DeleteCertificates will delete the certificates
242
- func DeleteCertificates (path * paths.Path ) {
243
- path .Join ("ca.cert.pem" ).Remove ()
244
- path .Join ("ca.cert.cer" ).Remove ()
245
- path .Join ("ca.key.pem" ).Remove ()
238
+ func DeleteCertificates (certDir * paths.Path ) {
239
+ certDir .Join ("ca.cert.pem" ).Remove ()
240
+ certDir .Join ("ca.cert.cer" ).Remove ()
241
+ certDir .Join ("ca.key.pem" ).Remove ()
246
242
}
247
243
248
244
const noFirefoxTemplateHTML = `<!DOCTYPE html>
0 commit comments