-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert.go
208 lines (185 loc) · 5.16 KB
/
cert.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 certgo
import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math/big"
"net"
"net/url"
"time"
"github.com/Alonza0314/cert-go/model"
"github.com/Alonza0314/cert-go/util"
logger "github.com/Alonza0314/logger-go"
)
func signCertificate(cfg model.Certificate) (*x509.Certificate, error) {
logger.Info("signCertificate", "signing certificate")
// check certificate exists
if util.FileExists(cfg.CertFilePath) {
logger.Warn("signCertificate", "certificate already exists")
return nil, errors.New("certificate already exists")
}
// create certificate template
var template *x509.Certificate
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
logger.Error("signCertificate", err.Error())
return nil, err
}
notBefore := time.Now()
notAfter := notBefore.AddDate(cfg.ValidityYears, cfg.ValidityMonth, cfg.ValidityDay)
template = &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{cfg.Organization},
CommonName: cfg.CommonName,
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: cfg.KeyUsage,
ExtKeyUsage: cfg.ExtKeyUsage,
BasicConstraintsValid: true,
IsCA: cfg.IsCA,
DNSNames: cfg.DNSNames,
IPAddresses: func() []net.IP {
ips := make([]net.IP, 0)
for _, ip := range cfg.IPAddresses {
ips = append(ips, net.ParseIP(ip))
}
return ips
}(),
URIs: func() []*url.URL {
uris := make([]*url.URL, 0)
for _, uri := range cfg.URIs {
uris = append(uris, &url.URL{Host: uri})
}
return uris
}(),
}
var certBytes []byte
if cfg.Type == "root" {
// root certificate self-signed
if !util.FileExists(cfg.KeyFilePath) {
logger.Warn("signCertificate", "private key does not exist")
cfg.ParentKey, err = CreatePrivateKey(cfg.KeyFilePath)
if err != nil {
return nil, err
}
}
if cfg.ParentKey == nil {
cfg.ParentKey, err = util.ReadPrivateKey(cfg.KeyFilePath)
if err != nil {
return nil, err
}
}
certBytes, err = x509.CreateCertificate(rand.Reader, template, template, &cfg.ParentKey.PublicKey, cfg.ParentKey)
if err != nil {
logger.Error("signCertificate", err.Error())
return nil, err
}
} else {
// intermediate certificate or end-entity certificate
var csr *x509.CertificateRequest
if !util.FileExists(cfg.CsrFilePath) {
logger.Warn("signCertificate", "CSR file does not exist")
csr, err = CreateCsr(cfg)
if err != nil {
return nil, err
}
}
if csr == nil {
csr, err = util.ReadCsr(cfg.CsrFilePath)
if err != nil {
return nil, err
}
}
if err := csr.CheckSignature(); err != nil {
logger.Error("signCertificate", err.Error())
return nil, err
}
// read parent cert
cfg.ParentCert, err = util.ReadCertificate(cfg.ParentCertPath)
if err != nil {
return nil, err
}
// read parent key
cfg.ParentKey, err = util.ReadPrivateKey(cfg.ParentKeyPath)
if err != nil {
return nil, err
}
// sign certificate with parent certificate
certBytes, err = x509.CreateCertificate(rand.Reader, template, cfg.ParentCert, csr.PublicKey, cfg.ParentKey)
if err != nil {
logger.Error("signCertificate", err.Error())
return nil, err
}
}
// encode certificate to PEM
certPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
if err := util.FileWrite(cfg.CertFilePath, certPEM, 0644); err != nil {
return nil, err
}
// create directory exists
if !util.FileDirExists(cfg.CertFilePath) {
logger.Warn("signCertificate", util.FileDir(cfg.CertFilePath)+" directory not exists, creating...")
if err := util.FileDirCreate(cfg.CertFilePath); err != nil {
return nil, err
}
logger.Info("signCertificate", util.FileDir(cfg.CertFilePath)+" directory created")
}
logger.Info("signCertificate", cfg.Type+" certificate signed")
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
logger.Error("signCertificate", err.Error())
return nil, err
}
return cert, nil
}
func SignRootCertificate(yamlPath string) (*x509.Certificate, error) {
var cfg model.CAConfig
if err := util.ReadYamlFileToStruct(yamlPath, &cfg); err != nil {
return nil, err
}
cert, err := signCertificate(cfg.CA.Root)
if err != nil {
return nil, err
}
return cert, nil
}
func SignIntermediateCertificate(yamlPath string) (*x509.Certificate, error) {
var cfg model.CAConfig
if err := util.ReadYamlFileToStruct(yamlPath, &cfg); err != nil {
return nil, err
}
cert, err := signCertificate(cfg.CA.Intermediate)
if err != nil {
return nil, err
}
return cert, nil
}
func SignServerCertificate(yamlPath string) (*x509.Certificate, error) {
var cfg model.CAConfig
if err := util.ReadYamlFileToStruct(yamlPath, &cfg); err != nil {
return nil, err
}
cert, err := signCertificate(cfg.CA.Server)
if err != nil {
return nil, err
}
return cert, nil
}
func SignClientCertificate(yamlPath string) (*x509.Certificate, error) {
var cfg model.CAConfig
if err := util.ReadYamlFileToStruct(yamlPath, &cfg); err != nil {
return nil, err
}
cert, err := signCertificate(cfg.CA.Client)
if err != nil {
return nil, err
}
return cert, nil
}