generated from liatrio/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
154 lines (135 loc) · 4.42 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Package main provides template parsing and certificate generation functionality
// for creating X.509 certificates from JSON templates. It supports both root and
// intermediate certificate creation with configurable properties including key usage,
// extended key usage, and basic constraints.
package main
import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"fmt"
"math/big"
"os"
"time"
)
type CertificateTemplate struct {
Subject struct {
Country []string `json:"country,omitempty"`
Organization []string `json:"organization,omitempty"`
OrganizationalUnit []string `json:"organizationalUnit,omitempty"`
CommonName string `json:"commonName"`
} `json:"subject"`
Issuer struct {
CommonName string `json:"commonName"`
} `json:"issuer"`
NotBefore string `json:"notBefore"`
NotAfter string `json:"notAfter"`
KeyUsage []string `json:"keyUsage"`
ExtKeyUsage []string `json:"extKeyUsage,omitempty"`
BasicConstraints struct {
IsCA bool `json:"isCA"`
MaxPathLen int `json:"maxPathLen"`
} `json:"basicConstraints"`
Extensions []struct {
ID string `json:"id"`
Critical bool `json:"critical"`
Value string `json:"value"`
} `json:"extensions,omitempty"`
}
// ParseTemplate creates an x509 certificate from JSON template
func ParseTemplate(filename string, parent *x509.Certificate) (*x509.Certificate, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading template file: %w", err)
}
var tmpl CertificateTemplate
if err := json.Unmarshal(content, &tmpl); err != nil {
return nil, fmt.Errorf("error parsing template JSON: %w", err)
}
if err := ValidateTemplate(&tmpl, parent); err != nil {
return nil, err
}
return CreateCertificateFromTemplate(&tmpl, parent)
}
func ValidateTemplate(tmpl *CertificateTemplate, parent *x509.Certificate) error {
if tmpl.Subject.CommonName == "" {
return fmt.Errorf("template subject.commonName cannot be empty")
}
if parent == nil && tmpl.Issuer.CommonName == "" {
return fmt.Errorf("template issuer.commonName cannot be empty for root certificate")
}
if tmpl.BasicConstraints.IsCA && len(tmpl.KeyUsage) == 0 {
return fmt.Errorf("CA certificate must specify at least one key usage")
}
if tmpl.BasicConstraints.IsCA {
hasKeyUsageCertSign := false
for _, usage := range tmpl.KeyUsage {
if usage == "certSign" {
hasKeyUsageCertSign = true
break
}
}
if !hasKeyUsageCertSign {
return fmt.Errorf("CA certificate must have certSign key usage")
}
}
return nil
}
func CreateCertificateFromTemplate(tmpl *CertificateTemplate, parent *x509.Certificate) (*x509.Certificate, error) {
notBefore, err := time.Parse(time.RFC3339, tmpl.NotBefore)
if err != nil {
return nil, fmt.Errorf("invalid notBefore time format: %w", err)
}
notAfter, err := time.Parse(time.RFC3339, tmpl.NotAfter)
if err != nil {
return nil, fmt.Errorf("invalid notAfter time format: %w", err)
}
cert := &x509.Certificate{
Subject: pkix.Name{
Country: tmpl.Subject.Country,
Organization: tmpl.Subject.Organization,
OrganizationalUnit: tmpl.Subject.OrganizationalUnit,
CommonName: tmpl.Subject.CommonName,
},
Issuer: func() pkix.Name {
if parent != nil {
return parent.Subject
}
return pkix.Name{CommonName: tmpl.Issuer.CommonName}
}(),
SerialNumber: big.NewInt(time.Now().Unix()),
NotBefore: notBefore,
NotAfter: notAfter,
BasicConstraintsValid: true,
IsCA: tmpl.BasicConstraints.IsCA,
}
if tmpl.BasicConstraints.IsCA {
cert.MaxPathLen = tmpl.BasicConstraints.MaxPathLen
cert.MaxPathLenZero = tmpl.BasicConstraints.MaxPathLen == 0
}
SetKeyUsages(cert, tmpl.KeyUsage)
SetExtKeyUsages(cert, tmpl.ExtKeyUsage)
return cert, nil
}
func SetKeyUsages(cert *x509.Certificate, usages []string) {
for _, usage := range usages {
switch usage {
case "certSign":
cert.KeyUsage |= x509.KeyUsageCertSign
case "crlSign":
cert.KeyUsage |= x509.KeyUsageCRLSign
case "digitalSignature":
cert.KeyUsage |= x509.KeyUsageDigitalSignature
}
}
}
func SetExtKeyUsages(cert *x509.Certificate, usages []string) {
for _, usage := range usages {
switch usage {
case "timeStamping":
cert.ExtKeyUsage = append(cert.ExtKeyUsage, x509.ExtKeyUsageTimeStamping)
case "codeSign":
cert.ExtKeyUsage = append(cert.ExtKeyUsage, x509.ExtKeyUsageCodeSigning)
}
}
}