-
Notifications
You must be signed in to change notification settings - Fork 0
/
acert.go
299 lines (254 loc) · 7.85 KB
/
acert.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"net"
"net/mail"
"net/url"
"strings"
"time"
)
// AcertOptions holds extra configuration options used
// when generating private keys and building certificates.
type AcertOptions struct {
// Certificate
Days int
// Path length is used for certificate chaining
// https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.9
PathLenConstraint int
// Private key
Algorithm string
Bits int
}
// Acert is the primary object used to perform PKI operations.
type Acert struct {
// Configuration
Hosts []string
Options AcertOptions
// Inputs
RootPrivateKey crypto.PrivateKey
RootCertificate x509.Certificate
IntermediateCertificate x509.Certificate
Subject pkix.Name
// Outputs
PrivateKey crypto.PrivateKey
PublicKey crypto.PublicKey
Certificate x509.Certificate
Request x509.CertificateRequest
}
// BuildCertificate builds a PKI certificate
func (a *Acert) BuildCertificate(isCa bool) []byte {
if a.Request.Raw != nil {
// Initialize certificate from signing request
a.DecorateCertificateFromRequest()
a.PublicKey = a.Request.PublicKey
} else {
// Parse configured hosts
a.Certificate.Subject = a.Subject
a.ParseSubjectAlternativeNames()
// Require private key for request
a.requirePrivateKey()
a.PublicKey = a.PrivateKey.(crypto.Signer).Public()
}
// Try to set a common name if one is not set
if a.Subject.CommonName == "" && len(a.Hosts) > 0 {
a.Subject.CommonName = a.Hosts[0]
}
// Other certificate properties
a.GenerateSerialNumber()
a.Certificate.NotBefore = now
a.Certificate.IsCA = isCa
// Add expiration date based on the configured number of days
if a.Options.Days > 0 {
if a.Options.Days > 825 {
log("Warning: iOS and macOS certificates must have a validity period of 825 days or fewer")
log("Reference: https://support.apple.com/en-us/HT210176")
}
a.Certificate.NotAfter = now.Add(time.Hour * 24 * time.Duration(a.Options.Days))
}
// Key usage
if isCa {
a.Certificate.BasicConstraintsValid = true
a.Certificate.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageCRLSign
} else {
a.Certificate.KeyUsage = x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
if len(a.Certificate.IPAddresses) > 0 || len(a.Certificate.DNSNames) > 0 || len(a.Certificate.URIs) > 0 {
a.Certificate.ExtKeyUsage = append(a.Certificate.ExtKeyUsage, x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth)
}
// Email protection
if len(a.Certificate.EmailAddresses) > 0 {
a.Certificate.ExtKeyUsage = append(a.Certificate.ExtKeyUsage, x509.ExtKeyUsageEmailProtection)
}
}
// Path length for certificate chaining
if a.Options.PathLenConstraint > 0 {
a.Certificate.MaxPathLen = a.Options.PathLenConstraint
a.Certificate.MaxPathLenZero = false
} else {
a.Certificate.MaxPathLenZero = true
}
// Create self-signed certificate if root is not set
if a.RootCertificate.SerialNumber == nil || a.RootPrivateKey == nil {
a.RootCertificate = a.Certificate
a.RootPrivateKey = a.PrivateKey
}
// Build certificate
certificateBytes, err := x509.CreateCertificate(rand.Reader, &a.Certificate, &a.RootCertificate, a.PublicKey, a.RootPrivateKey)
if err != nil {
panic(err)
}
return certificateBytes
}
// BuildCertificateRequest generates a certificate signing request
func (a *Acert) BuildCertificateRequest() []byte {
// Require private key for request
a.requirePrivateKey()
// Build request template
a.ParseSubjectAlternativeNames()
a.Request.Subject = a.Subject
a.Request.DNSNames = a.Certificate.DNSNames
a.Request.IPAddresses = a.Certificate.IPAddresses
a.Request.EmailAddresses = a.Certificate.EmailAddresses
a.Request.URIs = a.Certificate.URIs
// Build certificate signing request
csr, err := x509.CreateCertificateRequest(rand.Reader, &a.Request, a.PrivateKey)
if err != nil {
panic(err)
}
return csr
}
// DecorateCertificateFromRequest populates an x509 certificate
// with values from a certificate signing request.
func (a *Acert) DecorateCertificateFromRequest() {
a.Certificate.Subject = a.Request.Subject
a.Certificate.Extensions = a.Request.Extensions
a.Certificate.ExtraExtensions = a.Request.ExtraExtensions
// SAN
for _, value := range a.Request.DNSNames {
a.Hosts = append(a.Hosts, value)
a.Certificate.DNSNames = append(a.Certificate.DNSNames, value)
}
for _, value := range a.Request.IPAddresses {
a.Hosts = append(a.Hosts, value.String())
a.Certificate.IPAddresses = append(a.Certificate.IPAddresses, value)
}
for _, value := range a.Request.EmailAddresses {
a.Hosts = append(a.Hosts, value)
a.Certificate.EmailAddresses = append(a.Certificate.EmailAddresses, value)
}
for _, value := range a.Request.URIs {
a.Hosts = append(a.Hosts, value.Host)
a.Certificate.URIs = append(a.Certificate.URIs, value)
}
}
// GenerateKey builds a key with the specified algorithm.
// The number of bits can be specified for the default RSA algorithm.
//
// Valid algorithm names are
// ed25519
// ecdsa-p224
// ecdsa-p256
// ecdsa-p384
// ecdsa-p521
// rsa
func (a *Acert) GenerateKey(algorithm string, bits int) {
var (
// Parse algorithm
kind = strings.Split(strings.ToLower(strings.TrimSpace(algorithm)), "-")
err error
privateKey crypto.PrivateKey
)
switch kind[0] {
case "ed25519":
_, privateKey, err = ed25519.GenerateKey(rand.Reader)
case "ecdsa":
// Get curve type
name := ""
if len(kind) > 1 {
name = kind[1]
}
var curve elliptic.Curve
switch name {
case "p224":
curve = elliptic.P224()
case "p384":
curve = elliptic.P384()
case "p521":
curve = elliptic.P521()
default:
curve = elliptic.P256()
}
privateKey, err = ecdsa.GenerateKey(curve, rand.Reader)
default:
privateKey, err = rsa.GenerateKey(rand.Reader, bits)
}
if err != nil {
panic(err)
}
a.PrivateKey = privateKey
}
// GenerateSerialNumber creates a random serial number.
// This function is used to generate serial numbers for x509 certificates.
func (a *Acert) GenerateSerialNumber() {
limit := new(big.Int).Lsh(big.NewInt(1), 128)
serial, err := rand.Int(rand.Reader, limit)
if err != nil {
panic(err)
}
a.Certificate.SerialNumber = serial
}
// ParseSanHosts takes a string array and
// returns a new x509.Certificate populated with parsed
// subject alternative name values
func (a *Acert) ParseSubjectAlternativeNames() {
for _, value := range a.Hosts {
if ip := net.ParseIP(value); ip != nil {
a.Certificate.IPAddresses = append(a.Certificate.IPAddresses, ip)
} else if email, err := mail.ParseAddress(value); err == nil && email.Address == value {
a.Certificate.EmailAddresses = append(a.Certificate.EmailAddresses, email.Address)
} else if uri, err := url.Parse(value); err == nil && uri.Scheme != "" && uri.Host != "" {
a.Certificate.URIs = append(a.Certificate.URIs, uri)
} else {
a.Certificate.DNSNames = append(a.Certificate.DNSNames, value)
}
}
}
// Require a private key to be set
func (a *Acert) requirePrivateKey() {
if a.PrivateKey == nil {
a.GenerateKey(a.Options.Algorithm, a.Options.Bits)
}
}
// Verify validates a certificate root, chain and/or host names
func (a *Acert) Verify() error {
var err error
options := x509.VerifyOptions{}
// Add root certificate
if a.RootCertificate.SerialNumber != nil {
rootPool := x509.NewCertPool()
rootPool.AddCert(&a.RootCertificate)
options.Roots = rootPool
}
// Add intermediate certificate
if a.IntermediateCertificate.SerialNumber != nil {
intermediatePool := x509.NewCertPool()
intermediatePool.AddCert(&a.IntermediateCertificate)
options.Intermediates = intermediatePool
}
if len(a.Hosts) > 0 {
for _, host := range a.Hosts {
options.DNSName = host
_, err = a.Certificate.Verify(options)
}
} else {
_, err = a.Certificate.Verify(options)
}
return err
}