Skip to content

Commit 34161a2

Browse files
committed
save/load certificate and key to/from file
1 parent d686f24 commit 34161a2

1 file changed

Lines changed: 61 additions & 25 deletions

File tree

‎controlbox.go‎

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"log"
1010
"os"
11+
"path/filepath"
1112
"slices"
1213
"strconv"
1314
"sync"
@@ -56,34 +57,54 @@ type controlbox struct {
5657
mutex sync.Mutex
5758
}
5859

59-
func (h *controlbox) run() {
60-
var err error
61-
var certificate tls.Certificate
62-
63-
if len(os.Args) == 4 {
64-
certificate, err = tls.LoadX509KeyPair(os.Args[2], os.Args[3])
65-
if err != nil {
66-
usage()
67-
log.Fatal(err)
68-
}
69-
} else {
70-
certificate, err = cert.CreateCertificate("Demo", "Demo", "DE", "Demo-Unit-01")
71-
if err != nil {
72-
log.Fatal(err)
60+
func loadOrCreateCertificate(dir string) (tls.Certificate, error) {
61+
crtPath := filepath.Join(dir, "cb.crt")
62+
keyPath := filepath.Join(dir, "cb.key")
63+
64+
// If both files exist → load
65+
if _, err := os.Stat(crtPath); err == nil {
66+
if _, err := os.Stat(keyPath); err == nil {
67+
return tls.LoadX509KeyPair(crtPath, keyPath)
7368
}
69+
}
7470

75-
pemdata := pem.EncodeToMemory(&pem.Block{
76-
Type: "CERTIFICATE",
77-
Bytes: certificate.Certificate[0],
78-
})
79-
fmt.Println(string(pemdata))
71+
// Otherwise create new certificate
72+
certTLS, err := cert.CreateCertificate("Demo", "Demo", "DE", "Demo-Unit-01")
73+
if err != nil {
74+
return tls.Certificate{}, err
75+
}
8076

81-
b, err := x509.MarshalECPrivateKey(certificate.PrivateKey.(*ecdsa.PrivateKey))
82-
if err != nil {
83-
log.Fatal(err)
84-
}
85-
pemdata = pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: b})
86-
fmt.Println(string(pemdata))
77+
// Write certificate
78+
certPEM := pem.EncodeToMemory(&pem.Block{
79+
Type: "CERTIFICATE",
80+
Bytes: certTLS.Certificate[0],
81+
})
82+
if err := os.WriteFile(crtPath, certPEM, 0644); err != nil {
83+
return tls.Certificate{}, err
84+
}
85+
86+
// Write private key
87+
privKey := certTLS.PrivateKey.(*ecdsa.PrivateKey)
88+
keyBytes, err := x509.MarshalECPrivateKey(privKey)
89+
if err != nil {
90+
return tls.Certificate{}, err
91+
}
92+
93+
keyPEM := pem.EncodeToMemory(&pem.Block{
94+
Type: "EC PRIVATE KEY",
95+
Bytes: keyBytes,
96+
})
97+
if err := os.WriteFile(keyPath, keyPEM, 0600); err != nil {
98+
return tls.Certificate{}, err
99+
}
100+
101+
return certTLS, nil
102+
}
103+
104+
func (h *controlbox) run() {
105+
if len(os.Args) < 2 || len(os.Args) > 3 {
106+
fmt.Println("Usage: controlbox <port> [cert-directory]")
107+
os.Exit(1)
87108
}
88109

89110
port, err := strconv.Atoi(os.Args[1])
@@ -92,6 +113,21 @@ func (h *controlbox) run() {
92113
log.Fatal(err)
93114
}
94115

116+
certDir := "."
117+
if len(os.Args) == 3 {
118+
certDir = os.Args[2]
119+
}
120+
121+
certDir, err = filepath.Abs(certDir)
122+
if err != nil {
123+
log.Fatal(err)
124+
}
125+
126+
certificate, err := loadOrCreateCertificate(certDir)
127+
if err != nil {
128+
log.Fatal(err)
129+
}
130+
95131
vendorCode := "Demo"
96132
deviceBrand := "Demo"
97133
deviceModel := "ControlBox"

0 commit comments

Comments
 (0)