@@ -14,6 +14,95 @@ import (
1414 "time"
1515)
1616
17+ // LoadCertificateFromFile loads a certificate and private key from PEM files
18+ func LoadCertificateFromFile (certPath , keyPath string ) (* x509.Certificate , * rsa.PrivateKey , error ) {
19+ // Read certificate file
20+ certPEM , err := os .ReadFile (certPath )
21+ if err != nil {
22+ return nil , nil , fmt .Errorf ("failed to read certificate file: %w" , err )
23+ }
24+
25+ // Decode PEM block
26+ certBlock , _ := pem .Decode (certPEM )
27+ if certBlock == nil {
28+ return nil , nil , fmt .Errorf ("failed to decode certificate PEM" )
29+ }
30+
31+ // Parse certificate
32+ cert , err := x509 .ParseCertificate (certBlock .Bytes )
33+ if err != nil {
34+ return nil , nil , fmt .Errorf ("failed to parse certificate: %w" , err )
35+ }
36+
37+ // Read private key file
38+ keyPEM , err := os .ReadFile (keyPath )
39+ if err != nil {
40+ return nil , nil , fmt .Errorf ("failed to read private key file: %w" , err )
41+ }
42+
43+ // Decode PEM block
44+ keyBlock , _ := pem .Decode (keyPEM )
45+ if keyBlock == nil {
46+ return nil , nil , fmt .Errorf ("failed to decode private key PEM" )
47+ }
48+
49+ // Parse private key
50+ privateKey , err := x509 .ParsePKCS1PrivateKey (keyBlock .Bytes )
51+ if err != nil {
52+ return nil , nil , fmt .Errorf ("failed to parse private key: %w" , err )
53+ }
54+
55+ return cert , privateKey , nil
56+ }
57+
58+ // CheckAndLoadOrGenerateRootCertificate checks if root certificate files exist,
59+ // loads them if they do, or generates new ones if they don't
60+ func CheckAndLoadOrGenerateRootCertificate (addThumbPrintToName bool , commonName , country , organization string , strong bool ) (* x509.Certificate , * rsa.PrivateKey , error ) {
61+ certPath := "config/root_cert.pem"
62+ keyPath := "config/root_key.pem"
63+
64+ // Check if both files exist
65+ _ , certErr := os .Stat (certPath )
66+ _ , keyErr := os .Stat (keyPath )
67+
68+ if certErr == nil && keyErr == nil {
69+ // Files exist, try to load them
70+ cert , key , err := LoadCertificateFromFile (certPath , keyPath )
71+ if err == nil {
72+ return cert , key , nil
73+ }
74+ // If loading fails, fall through to generation
75+ fmt .Printf ("Warning: Failed to load existing certificates: %v. Generating new ones...\n " , err )
76+ }
77+
78+ // Files don't exist or loading failed, generate new certificates
79+ return GenerateRootCertificate (addThumbPrintToName , commonName , country , organization , strong )
80+ }
81+
82+ // CheckAndLoadOrGenerateWebServerCertificate checks if web server certificate files exist,
83+ // loads them if they do, or generates new ones if they don't
84+ func CheckAndLoadOrGenerateWebServerCertificate (rootCert CertAndKeyType , addThumbPrintToName bool , commonName , country , organization string , strong bool ) (* x509.Certificate , * rsa.PrivateKey , error ) {
85+ certPath := "config/" + commonName + "_cert.pem"
86+ keyPath := "config/" + commonName + "_key.pem"
87+
88+ // Check if both files exist
89+ _ , certErr := os .Stat (certPath )
90+ _ , keyErr := os .Stat (keyPath )
91+
92+ if certErr == nil && keyErr == nil {
93+ // Files exist, try to load them
94+ cert , key , err := LoadCertificateFromFile (certPath , keyPath )
95+ if err == nil {
96+ return cert , key , nil
97+ }
98+ // If loading fails, fall through to generation
99+ fmt .Printf ("Warning: Failed to load existing certificates: %v. Generating new ones...\n " , err )
100+ }
101+
102+ // Files don't exist or loading failed, generate new certificates
103+ return IssueWebServerCertificate (rootCert , addThumbPrintToName , commonName , country , organization , strong )
104+ }
105+
17106func GenerateRootCertificate (addThumbPrintToName bool , commonName , country , organization string , strong bool ) (* x509.Certificate , * rsa.PrivateKey , error ) {
18107 keyLength := 2048
19108 if strong {
0 commit comments