@@ -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,51 +133,57 @@ 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 )
151
- if err != nil {
152
- log .Error (err .Error ())
153
- os .Exit (1 )
148
+
149
+ {
150
+ keyOutPath := certsDir .Join ("ca.key.pem" ).String ()
151
+ keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 ) // Save key with user-only permission 0600
152
+ if err != nil {
153
+ log .Error (err .Error ())
154
+ os .Exit (1 )
155
+ }
156
+ pem .Encode (keyOut , pemBlockForKey (caKey ))
157
+ keyOut .Close ()
158
+ log .Printf ("written %s" , keyOutPath )
154
159
}
155
- pem .Encode (keyOut , pemBlockForKey (caKey ))
156
- keyOut .Close ()
157
- log .Printf ("written %s" , keyOutPath )
158
160
159
161
// Create the certification authority
160
162
caTemplate , err := generateSingleCertificate (true )
161
-
162
163
if err != nil {
163
164
log .Error (err .Error ())
164
165
os .Exit (1 )
165
166
}
166
167
167
168
derBytes , _ := x509 .CreateCertificate (rand .Reader , caTemplate , caTemplate , publicKey (caKey ), caKey )
168
169
169
- certOutPath := path .Join ("ca.cert.pem" ).String ()
170
- certOut , err := os .Create (certOutPath )
171
- if err != nil {
172
- log .Error (err .Error ())
173
- os .Exit (1 )
170
+ {
171
+ caCertOutPath := certsDir .Join ("ca.cert.pem" )
172
+ caCertOut , err := caCertOutPath .Create ()
173
+ if err != nil {
174
+ log .Error (err .Error ())
175
+ os .Exit (1 )
176
+ }
177
+ pem .Encode (caCertOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
178
+ caCertOut .Close ()
179
+ log .Printf ("written %s" , caCertOutPath )
174
180
}
175
- pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
176
- certOut .Close ()
177
- log .Printf ("written %s" , certOutPath )
178
181
179
- filePath := path .Join ("ca.cert.cer" ).String ()
180
- ioutil .WriteFile (filePath , derBytes , 0644 )
181
- log .Printf ("written %s" , filePath )
182
+ {
183
+ caCertPath := certsDir .Join ("ca.cert.cer" )
184
+ caCertPath .WriteFile (derBytes )
185
+ log .Printf ("written %s" , caCertPath )
186
+ }
182
187
183
188
// Create the key for the final certificate
184
189
key , err := generateKey ("P256" )
@@ -187,40 +192,44 @@ func generateCertificates(path *paths.Path) {
187
192
os .Exit (1 )
188
193
}
189
194
190
- keyOutPath = path .Join ("key.pem" ).String ()
191
- keyOut , err = os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
192
- if err != nil {
193
- log .Error (err .Error ())
194
- os .Exit (1 )
195
+ {
196
+ keyOutPath := certsDir .Join ("key.pem" ).String ()
197
+ keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 ) // Save key with user-only permission 0600
198
+ if err != nil {
199
+ log .Error (err .Error ())
200
+ os .Exit (1 )
201
+ }
202
+ pem .Encode (keyOut , pemBlockForKey (key ))
203
+ keyOut .Close ()
204
+ log .Printf ("written %s" , keyOutPath )
195
205
}
196
- pem .Encode (keyOut , pemBlockForKey (key ))
197
- keyOut .Close ()
198
- log .Printf ("written %s" , keyOutPath )
199
206
200
207
// Create the final certificate
201
208
template , err := generateSingleCertificate (false )
202
-
203
209
if err != nil {
204
210
log .Error (err .Error ())
205
211
os .Exit (1 )
206
212
}
207
213
208
214
derBytes , _ = x509 .CreateCertificate (rand .Reader , template , caTemplate , publicKey (key ), caKey )
209
215
210
- certOutPath = path .Join ("cert.pem" ).String ()
211
- certOut , err = os .Create (certOutPath )
212
- if err != nil {
213
- log .Error (err .Error ())
214
- os .Exit (1 )
216
+ {
217
+ certOutPath := certsDir .Join ("cert.pem" ).String ()
218
+ certOut , err := os .Create (certOutPath )
219
+ if err != nil {
220
+ log .Error (err .Error ())
221
+ os .Exit (1 )
222
+ }
223
+ pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
224
+ certOut .Close ()
225
+ log .Printf ("written %s" , certOutPath )
215
226
}
216
- pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
217
- certOut .Close ()
218
- log .Printf ("written %s" , certOutPath )
219
-
220
- certPath := path .Join ("cert.cer" ).String ()
221
- ioutil .WriteFile (certPath , derBytes , 0644 )
222
- log .Printf ("written %s" , certPath )
223
227
228
+ {
229
+ certPath := certsDir .Join ("cert.cer" )
230
+ certPath .WriteFile (derBytes )
231
+ log .Printf ("written %s" , certPath )
232
+ }
224
233
}
225
234
226
235
func certHandler (c * gin.Context ) {
@@ -239,10 +248,10 @@ func deleteCertHandler(c *gin.Context) {
239
248
}
240
249
241
250
// 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 ()
251
+ func DeleteCertificates (certDir * paths.Path ) {
252
+ certDir .Join ("ca.cert.pem" ).Remove ()
253
+ certDir .Join ("ca.cert.cer" ).Remove ()
254
+ certDir .Join ("ca.key.pem" ).Remove ()
246
255
}
247
256
248
257
const noFirefoxTemplateHTML = `<!DOCTYPE html>
0 commit comments