@@ -16,14 +16,14 @@ import (
1616 "crypto/x509/pkix"
1717 "encoding/pem"
1818 "fmt"
19- "io/ioutil"
2019 "math/big"
2120 "net"
2221 "os"
2322 "strings"
2423 "text/template"
2524 "time"
2625
26+ "github.com/arduino/go-paths-helper"
2727 "github.com/gin-gonic/gin"
2828 log "github.com/sirupsen/logrus"
2929)
@@ -133,12 +133,38 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {
133133 return & template , nil
134134}
135135
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+ }
137162
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 ()
142168
143169 // Create the key for the certification authority
144170 caKey , err := generateKey ("P256" )
@@ -147,36 +173,44 @@ func generateCertificates() {
147173 os .Exit (1 )
148174 }
149175
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 )
154186 }
155- pem .Encode (keyOut , pemBlockForKey (caKey ))
156- keyOut .Close ()
157- log .Println ("written ca.key.pem" )
158187
159188 // Create the certification authority
160189 caTemplate , err := generateSingleCertificate (true )
161-
162190 if err != nil {
163191 log .Error (err .Error ())
164192 os .Exit (1 )
165193 }
166194
167195 derBytes , _ := x509 .CreateCertificate (rand .Reader , caTemplate , caTemplate , publicKey (caKey ), caKey )
168196
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 )
173207 }
174- pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
175- certOut .Close ()
176- log .Print ("written ca.cert.pem" )
177208
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+ }
180214
181215 // Create the key for the final certificate
182216 key , err := generateKey ("P256" )
@@ -185,37 +219,44 @@ func generateCertificates() {
185219 os .Exit (1 )
186220 }
187221
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 )
192232 }
193- pem .Encode (keyOut , pemBlockForKey (key ))
194- keyOut .Close ()
195- log .Println ("written key.pem" )
196233
197234 // Create the final certificate
198235 template , err := generateSingleCertificate (false )
199-
200236 if err != nil {
201237 log .Error (err .Error ())
202238 os .Exit (1 )
203239 }
204240
205241 derBytes , _ = x509 .CreateCertificate (rand .Reader , template , caTemplate , publicKey (key ), caKey )
206242
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 )
211253 }
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" )
218254
255+ {
256+ certPath := certsDir .Join ("cert.cer" )
257+ certPath .WriteFile (derBytes )
258+ log .Printf ("written %s" , certPath )
259+ }
219260}
220261
221262func certHandler (c * gin.Context ) {
@@ -230,14 +271,14 @@ func certHandler(c *gin.Context) {
230271}
231272
232273func deleteCertHandler (c * gin.Context ) {
233- DeleteCertificates ()
274+ DeleteCertificates (getCertificatesDir () )
234275}
235276
236277// 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 ( )
241282}
242283
243284const noFirefoxTemplateHTML = `<!DOCTYPE html>
0 commit comments