@@ -16,14 +16,14 @@ 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"
23
22
"strings"
24
23
"text/template"
25
24
"time"
26
25
26
+ "github.com/arduino/go-paths-helper"
27
27
"github.com/gin-gonic/gin"
28
28
log "github.com/sirupsen/logrus"
29
29
)
@@ -133,12 +133,38 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {
133
133
return & template , nil
134
134
}
135
135
136
- func generateCertificates () {
136
+ // migrateCertificatesGeneratedWithOldAgentVersions checks if certificates generated
137
+ // with an old version of the Agent needs to be migrated to the current certificates
138
+ // directory, and performs the migration if needed.
139
+ func migrateCertificatesGeneratedWithOldAgentVersions (certsDir * paths.Path ) {
140
+ if certsDir .Join ("ca.cert.pem" ).Exist () {
141
+ // The new certificates are already set-up, nothing to do
142
+ return
143
+ }
144
+
145
+ fileList := []string {
146
+ "ca.key.pem" ,
147
+ "ca.cert.pem" ,
148
+ "ca.cert.cer" ,
149
+ "key.pem" ,
150
+ "cert.pem" ,
151
+ "cert.cer" ,
152
+ }
153
+ oldCertsDirPath , _ := os .Executable ()
154
+ oldCertsDir := paths .New (oldCertsDirPath )
155
+ for _ , fileName := range fileList {
156
+ oldCert := oldCertsDir .Join (fileName )
157
+ if oldCert .Exist () {
158
+ oldCert .CopyTo (certsDir .Join (fileName ))
159
+ }
160
+ }
161
+ }
137
162
138
- os .Remove ("ca.cert.pem" )
139
- os .Remove ("ca.key.pem" )
140
- os .Remove ("cert.pem" )
141
- os .Remove ("key.pem" )
163
+ func generateCertificates (certsDir * paths.Path ) {
164
+ certsDir .Join ("ca.cert.pem" ).Remove ()
165
+ certsDir .Join ("ca.key.pem" ).Remove ()
166
+ certsDir .Join ("cert.pem" ).Remove ()
167
+ certsDir .Join ("key.pem" ).Remove ()
142
168
143
169
// Create the key for the certification authority
144
170
caKey , err := generateKey ("P256" )
@@ -147,36 +173,44 @@ func generateCertificates() {
147
173
os .Exit (1 )
148
174
}
149
175
150
- keyOut , err := os .OpenFile ("ca.key.pem" , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
151
- if err != nil {
152
- log .Error (err .Error ())
153
- os .Exit (1 )
176
+ {
177
+ keyOutPath := certsDir .Join ("ca.key.pem" ).String ()
178
+ keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 ) // Save key with user-only permission 0600
179
+ if err != nil {
180
+ log .Error (err .Error ())
181
+ os .Exit (1 )
182
+ }
183
+ pem .Encode (keyOut , pemBlockForKey (caKey ))
184
+ keyOut .Close ()
185
+ log .Printf ("written %s" , keyOutPath )
154
186
}
155
- pem .Encode (keyOut , pemBlockForKey (caKey ))
156
- keyOut .Close ()
157
- log .Println ("written ca.key.pem" )
158
187
159
188
// Create the certification authority
160
189
caTemplate , err := generateSingleCertificate (true )
161
-
162
190
if err != nil {
163
191
log .Error (err .Error ())
164
192
os .Exit (1 )
165
193
}
166
194
167
195
derBytes , _ := x509 .CreateCertificate (rand .Reader , caTemplate , caTemplate , publicKey (caKey ), caKey )
168
196
169
- certOut , err := os .Create ("ca.cert.pem" )
170
- if err != nil {
171
- log .Error (err .Error ())
172
- os .Exit (1 )
197
+ {
198
+ caCertOutPath := certsDir .Join ("ca.cert.pem" )
199
+ caCertOut , err := caCertOutPath .Create ()
200
+ if err != nil {
201
+ log .Error (err .Error ())
202
+ os .Exit (1 )
203
+ }
204
+ pem .Encode (caCertOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
205
+ caCertOut .Close ()
206
+ log .Printf ("written %s" , caCertOutPath )
173
207
}
174
- pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
175
- certOut .Close ()
176
- log .Print ("written ca.cert.pem" )
177
208
178
- ioutil .WriteFile ("ca.cert.cer" , derBytes , 0644 )
179
- log .Print ("written ca.cert.cer" )
209
+ {
210
+ caCertPath := certsDir .Join ("ca.cert.cer" )
211
+ caCertPath .WriteFile (derBytes )
212
+ log .Printf ("written %s" , caCertPath )
213
+ }
180
214
181
215
// Create the key for the final certificate
182
216
key , err := generateKey ("P256" )
@@ -185,37 +219,44 @@ func generateCertificates() {
185
219
os .Exit (1 )
186
220
}
187
221
188
- keyOut , err = os .OpenFile ("key.pem" , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
189
- if err != nil {
190
- log .Error (err .Error ())
191
- os .Exit (1 )
222
+ {
223
+ keyOutPath := certsDir .Join ("key.pem" ).String ()
224
+ keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 ) // Save key with user-only permission 0600
225
+ if err != nil {
226
+ log .Error (err .Error ())
227
+ os .Exit (1 )
228
+ }
229
+ pem .Encode (keyOut , pemBlockForKey (key ))
230
+ keyOut .Close ()
231
+ log .Printf ("written %s" , keyOutPath )
192
232
}
193
- pem .Encode (keyOut , pemBlockForKey (key ))
194
- keyOut .Close ()
195
- log .Println ("written key.pem" )
196
233
197
234
// Create the final certificate
198
235
template , err := generateSingleCertificate (false )
199
-
200
236
if err != nil {
201
237
log .Error (err .Error ())
202
238
os .Exit (1 )
203
239
}
204
240
205
241
derBytes , _ = x509 .CreateCertificate (rand .Reader , template , caTemplate , publicKey (key ), caKey )
206
242
207
- certOut , err = os .Create ("cert.pem" )
208
- if err != nil {
209
- log .Error (err .Error ())
210
- os .Exit (1 )
243
+ {
244
+ certOutPath := certsDir .Join ("cert.pem" ).String ()
245
+ certOut , err := os .Create (certOutPath )
246
+ if err != nil {
247
+ log .Error (err .Error ())
248
+ os .Exit (1 )
249
+ }
250
+ pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
251
+ certOut .Close ()
252
+ log .Printf ("written %s" , certOutPath )
211
253
}
212
- pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
213
- certOut .Close ()
214
- log .Print ("written cert.pem" )
215
-
216
- ioutil .WriteFile ("cert.cer" , derBytes , 0644 )
217
- log .Print ("written cert.cer" )
218
254
255
+ {
256
+ certPath := certsDir .Join ("cert.cer" )
257
+ certPath .WriteFile (derBytes )
258
+ log .Printf ("written %s" , certPath )
259
+ }
219
260
}
220
261
221
262
func certHandler (c * gin.Context ) {
@@ -230,14 +271,14 @@ func certHandler(c *gin.Context) {
230
271
}
231
272
232
273
func deleteCertHandler (c * gin.Context ) {
233
- DeleteCertificates ()
274
+ DeleteCertificates (getCertificatesDir () )
234
275
}
235
276
236
277
// DeleteCertificates will delete the certificates
237
- func DeleteCertificates () {
238
- os . Remove ("ca.cert.pem" )
239
- os . Remove ("ca.cert.cer" )
240
- os . Remove ("ca.key.pem" )
278
+ func DeleteCertificates (certDir * paths. Path ) {
279
+ certDir . Join ("ca.cert.pem" ). Remove ( )
280
+ certDir . Join ("ca.cert.cer" ). Remove ( )
281
+ certDir . Join ("ca.key.pem" ). Remove ( )
241
282
}
242
283
243
284
const noFirefoxTemplateHTML = `<!DOCTYPE html>
0 commit comments