Added state file location defintion to the CA configuration. Added more certificate properties to certificate database.

This commit is contained in:
2025-07-28 17:41:41 +02:00
parent e4469fde96
commit b387a016be
4 changed files with 45 additions and 26 deletions

View File

@@ -10,7 +10,6 @@ import (
"fmt"
"math/big"
"os"
"path/filepath"
"time"
)
@@ -26,6 +25,8 @@ type CAState struct {
// CertificateRecord represents a single certificate record in the CA state
type CertificateRecord struct {
Name string `json:"name"`
Subject string `json:"subject"`
Type string `json:"type"`
Issued string `json:"issued"`
Expires string `json:"expires"`
Serial string `json:"serial"`
@@ -33,15 +34,14 @@ type CertificateRecord struct {
RevokeReason int `json:"revokeReason,omitempty"`
}
func caStatePath() string {
return filepath.Join(filepath.Dir(caConfigPath), caConfig.GetStateFileName())
}
// func caStatePath() string {
// return filepath.Join(filepath.Dir(caConfigPath), caConfig.GetStateFileName())
// }
// LoadCAState loads the CA state from a JSON file
func LoadCAState() error {
path := caStatePath()
fmt.Printf("Loading CA state from %s\n", path)
f, err := os.Open(path)
fmt.Printf("Loading CA state from %s\n", caStatePath)
f, err := os.Open(caStatePath)
if err != nil {
return err
}
@@ -56,7 +56,7 @@ func LoadCAState() error {
// SaveCAState saves the CA state to a JSON file
func SaveCAState() error {
caState.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
f, err := os.Create(caStatePath())
f, err := os.Create(caStatePath)
if err != nil {
return err
}
@@ -67,7 +67,7 @@ func SaveCAState() error {
}
// UpdateCAStateAfterIssue updates the CA state JSON after issuing a certificate
func (s *CAState) UpdateCAStateAfterIssue(serialType, basename string, serialNumber any, validity time.Duration) error {
func (s *CAState) UpdateCAStateAfterIssue(serialType, name string, subject string, certType string, serialNumber any, validity time.Duration) error {
if s == nil {
fmt.Fprintf(os.Stderr, "FATAL: CAState is nil in UpdateCAStateAfterIssue. This indicates a programming error.\n")
os.Exit(1)
@@ -84,17 +84,19 @@ func (s *CAState) UpdateCAStateAfterIssue(serialType, basename string, serialNum
default:
serialStr = fmt.Sprintf("%v", serialNumber)
}
s.AddCertificate(basename, issued, expires, serialStr)
s.AddCertificate(name, subject, certType, issued, expires, serialStr)
return nil
}
func (s *CAState) AddCertificate(name, issued, expires, serial string) {
func (s *CAState) AddCertificate(name, subject, certType, issued, expires, serial string) {
if s == nil {
fmt.Fprintf(os.Stderr, "FATAL: CAState is nil in AddCertificate. This indicates a programming error.\n")
os.Exit(1)
}
rec := CertificateRecord{
Name: name,
Subject: subject,
Type: certType,
Issued: issued,
Expires: expires,
Serial: serial,