-
Notifications
You must be signed in to change notification settings - Fork 3
/
x509.go
208 lines (174 loc) · 5.25 KB
/
x509.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
package cert
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"math/big"
"net"
"net/mail"
"net/url"
"os"
"os/user"
"path/filepath"
"time"
)
const rootName = "rootCA.pem"
const rootKeyName = "rootCA-key.pem"
var userAndHostname string
func init() {
u, err := user.Current()
if err == nil {
userAndHostname = u.Username + "@"
}
if h, err := os.Hostname(); err == nil {
userAndHostname += h
}
if err == nil && u.Name != "" && u.Name != u.Username {
userAndHostname += " (" + u.Name + ")"
}
}
type Cert struct {
KeyFile, CertFile, P12File string
dir string
caCert *x509.Certificate
caKey crypto.PrivateKey
}
func New(dir string) *Cert {
dir += "/cert"
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
_ = os.Mkdir(dir, 0755)
}
}
return &Cert{
dir: dir,
KeyFile: filepath.Join(dir, "localhost-key.pem"),
CertFile: filepath.Join(dir, "localhost.pem"),
P12File: filepath.Join(dir, "localhost.p12"),
}
}
func (m *Cert) MakeCert() *Cert {
hosts := []string{
"localhost",
"127.0.0.1",
"::1",
}
if m.caKey == nil {
return m
}
priv, _ := generateKey(false)
pub := priv.(crypto.Signer).Public()
// Certificates last for 2 years and 3 months, which is always less than
// 825 days, the limit that macOS/iOS apply to all certificates,
// including custom roots. See https://support.apple.com/en-us/HT210176.
expiration := time.Now().AddDate(2, 3, 0)
tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: pkix.Name{
Organization: []string{"favorX node certificate"},
OrganizationalUnit: []string{userAndHostname},
},
NotBefore: time.Now(), NotAfter: expiration,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
}
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
tpl.IPAddresses = append(tpl.IPAddresses, ip)
} else if email, err := mail.ParseAddress(h); err == nil && email.Address == h {
tpl.EmailAddresses = append(tpl.EmailAddresses, h)
} else if uriName, err := url.Parse(h); err == nil && uriName.Scheme != "" && uriName.Host != "" {
tpl.URIs = append(tpl.URIs, uriName)
} else {
tpl.DNSNames = append(tpl.DNSNames, h)
}
}
if len(tpl.IPAddresses) > 0 || len(tpl.DNSNames) > 0 || len(tpl.URIs) > 0 {
tpl.ExtKeyUsage = append(tpl.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
}
if len(tpl.EmailAddresses) > 0 {
tpl.ExtKeyUsage = append(tpl.ExtKeyUsage, x509.ExtKeyUsageEmailProtection)
}
crt, _ := x509.CreateCertificate(rand.Reader, tpl, m.caCert, pub, m.caKey)
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: crt})
privDER, _ := x509.MarshalPKCS8PrivateKey(priv)
privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})
_ = os.WriteFile(m.CertFile, certPEM, 0644)
_ = os.WriteFile(m.KeyFile, privPEM, 0600)
return m
}
func (m *Cert) LoadCA() *Cert {
rootFile := filepath.Join(m.dir, rootName)
rootKeyFile := filepath.Join(m.dir, rootKeyName)
_, err1 := os.Stat(rootFile)
_, err2 := os.Stat(rootKeyFile)
if err1 != nil || err2 != nil {
newCA(m.dir)
}
certPEMBlock, err := os.ReadFile(rootFile)
if err != nil {
return m
}
certDERBlock, _ := pem.Decode(certPEMBlock)
if certDERBlock == nil || certDERBlock.Type != "CERTIFICATE" {
return m
}
m.caCert, _ = x509.ParseCertificate(certDERBlock.Bytes)
keyPEMBlock, _ := os.ReadFile(rootKeyFile)
keyDERBlock, _ := pem.Decode(keyPEMBlock)
if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
return m
}
m.caKey, _ = x509.ParsePKCS8PrivateKey(keyDERBlock.Bytes)
return m
}
func newCA(dir string) {
caKey, _ := generateKey(true)
pub := caKey.(crypto.Signer).Public()
spkiASN1, _ := x509.MarshalPKIXPublicKey(pub)
var spki struct {
Algorithm pkix.AlgorithmIdentifier
SubjectPublicKey asn1.BitString
}
_, _ = asn1.Unmarshal(spkiASN1, &spki)
skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: pkix.Name{
Organization: []string{"favorX node CA"},
OrganizationalUnit: []string{userAndHostname},
// The CommonName is required by iOS to show the certificate in the
// "Certificate Trust Settings" menu.
CommonName: "favorX " + userAndHostname,
},
SubjectKeyId: skid[:],
NotAfter: time.Now().AddDate(10, 0, 0),
NotBefore: time.Now(),
KeyUsage: x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
MaxPathLenZero: true,
}
caCert, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, pub, caKey)
privDER, _ := x509.MarshalPKCS8PrivateKey(caKey)
_ = os.WriteFile(filepath.Join(dir, rootKeyName), pem.EncodeToMemory(
&pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0400)
_ = os.WriteFile(filepath.Join(dir, rootName), pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: caCert}), 0644)
}
func generateKey(rootCA bool) (crypto.PrivateKey, error) {
if rootCA {
return rsa.GenerateKey(rand.Reader, 3072)
}
return rsa.GenerateKey(rand.Reader, 2048)
}
func randomSerialNumber() *big.Int {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, _ := rand.Int(rand.Reader, serialNumberLimit)
return serialNumber
}