-
Notifications
You must be signed in to change notification settings - Fork 1
/
csr.go
299 lines (254 loc) · 7.74 KB
/
csr.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 cli
import (
"encoding/json"
"os"
"strings"
"github.com/effective-security/xpki/authority"
"github.com/effective-security/xpki/certutil"
"github.com/effective-security/xpki/csr"
"github.com/effective-security/xpki/x/print"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
// CsrCmd is the parent for CSR command
type CsrCmd struct {
Create CsrCreateCmd `cmd:"" help:"create certificate request"`
GenCert GenCertCmd `cmd:"" help:"create CSR and sign certificate"`
Sign CsrSignCmd `cmd:"" help:"sign certificate"`
}
// CsrCreateCmd specifies flags for Create command
type CsrCreateCmd struct {
CsrProfile string `required:"" help:"file name with CSR profile"`
KeyLabel string `required:"" help:"name for generated key"`
Output string `help:"the optional prefix for output files; if not set, the output will be printed to STDOUT only"`
}
// Run the command
func (a *CsrCreateCmd) Run(ctx *Cli) error {
cryptoprov, defaultCrypto := ctx.CryptoProv()
if cryptoprov == nil {
return errors.Errorf("unsupported command for this crypto provider")
}
prov := csr.NewProvider(defaultCrypto)
csrf, err := ctx.ReadFile(a.CsrProfile)
if err != nil {
return errors.WithMessage(err, "read CSR profile")
}
req := csr.CertificateRequest{
KeyRequest: prov.NewKeyRequest(prefixKeyLabel(a.KeyLabel), "ECDSA", 256, csr.SigningKey),
}
if strings.HasSuffix(a.CsrProfile, "json") {
err = json.Unmarshal(csrf, &req)
} else {
err = yaml.Unmarshal(csrf, &req)
}
if err != nil {
return errors.WithMessage(err, "invalid CSR")
}
var key, csrPEM []byte
csrPEM, key, _, _, err = prov.CreateRequestAndExportKey(&req)
if err != nil {
return errors.WithMessage(err, "process CSR")
}
if a.Output == "" {
print.CertAndKey(ctx.Writer(), key, csrPEM, nil)
} else {
err = saveCert(a.Output, key, csrPEM, nil)
if err != nil {
return err
}
}
return nil
}
// GenCertCmd specifies flags for GenCert command
type GenCertCmd struct {
SelfSign bool `help:"generate self-signed cert"`
CACert string `help:"file name of the signing CA cert"`
CAKey string `help:"file name of the signing CA key"`
CAConfig string `required:"" help:"file name with ca-config"`
CsrProfile string `required:"" help:"file name with CSR profile"`
Profile string `required:"" help:"certificate profile name from CA config"`
KeyLabel string `required:"" help:"name for generated key"`
San []string `help:"Subject Alt Names for generated cert"`
PemInfo bool `help:"Include certificate info in PEM file"`
Output string `help:"the optional prefix for output files; if not set, the output will be printed to STDOUT only"`
}
// Run the command
func (a *GenCertCmd) Run(ctx *Cli) error {
cryptoprov, defaultCrypto := ctx.CryptoProv()
if cryptoprov == nil {
return errors.Errorf("unsupported command for this crypto provider")
}
isscfg := &authority.IssuerConfig{}
if a.SelfSign {
if a.CAKey != "" {
return errors.Errorf("--self-sign can not be used with --ca-key")
}
} else {
if a.CAKey == "" || a.CACert == "" {
return errors.Errorf("CA certificate and key are required")
}
isscfg.CertFile = a.CACert
isscfg.KeyFile = a.CAKey
}
// Load CSR
csrf, err := ctx.ReadFile(a.CsrProfile)
if err != nil {
return errors.WithMessage(err, "read CSR profile")
}
prov := csr.NewProvider(defaultCrypto)
req := csr.CertificateRequest{
KeyRequest: prov.NewKeyRequest(prefixKeyLabel(a.KeyLabel), "ECDSA", 256, csr.SigningKey),
}
if strings.HasSuffix(a.CsrProfile, "json") {
err = json.Unmarshal(csrf, &req)
} else {
err = yaml.Unmarshal(csrf, &req)
}
if err != nil {
return errors.WithMessage(err, "invalid CSR profile")
}
if len(a.San) > 0 {
req.SAN = a.San
}
// Load ca-config
cacfg, err := authority.LoadConfig(a.CAConfig)
if err != nil {
return errors.WithMessage(err, "ca-config")
}
err = cacfg.Validate()
if err != nil {
return errors.WithMessage(err, "invalid ca-config")
}
isscfg.Profiles = cacfg.Profiles
var key, csrPEM, certPEM []byte
if a.SelfSign {
certPEM, csrPEM, key, err = authority.NewRoot(a.Profile,
cacfg,
defaultCrypto, &req)
if err != nil {
return err
}
crt, _ := certutil.ParseFromPEM(certPEM)
pem, _ := certutil.EncodeToPEMString(a.PemInfo, crt)
certPEM = []byte(pem + "\n")
} else {
issuer, err := authority.NewIssuer(isscfg, cryptoprov)
if err != nil {
return errors.WithMessage(err, "create issuer")
}
csrPEM, key, _, _, err = prov.CreateRequestAndExportKey(&req)
if err != nil {
return errors.WithMessage(err, "process CSR")
}
signReq := csr.SignRequest{
Request: string(csrPEM),
Profile: a.Profile,
}
crt, _, err := issuer.Sign(signReq)
if err != nil {
return errors.WithMessage(err, "sign request")
}
pem, _ := certutil.EncodeToPEMString(a.PemInfo, crt)
certPEM = []byte(pem + "\n")
}
if a.Output == "" {
print.CertAndKey(ctx.Writer(), key, csrPEM, certPEM)
} else {
err = saveCert(a.Output, key, csrPEM, certPEM)
if err != nil {
return errors.WithMessagef(err, "unable to save generated files")
}
}
return nil
}
// CsrSignCmd signs certificate request
type CsrSignCmd struct {
Csr string `kong:"arg" required:"" help:"file name with pem-encoded CSR to sign"`
CACert string `required:"" help:"file name of the signing CA cert"`
CAKey string `required:"" help:"file name of the signing CA key"`
CAConfig string `required:"" help:"file name with ca-config"`
Profile string `required:"" help:"certificate profile name from CA config"`
San []string `help:"Subject Alt Names for generated cert"`
AiaURL string `help:"optional AIA to add to the certificate"`
OcspURL string `help:"optional OCSP URL to add to the certificate"`
CrlURL string `help:"optional CRL DP to add to the certificate"`
PemInfo bool `help:"Include certificate info in PEM file"`
Output string `help:"the optional prefix for output files; if not set, the output will be printed to STDOUT only"`
}
// Run the command
func (a *CsrSignCmd) Run(ctx *Cli) error {
cryptoprov, _ := ctx.CryptoProv()
if cryptoprov == nil {
return errors.Errorf("unsupported command for this crypto provider")
}
// Load CSR
csrPEM, err := ctx.ReadFile(a.Csr)
if err != nil {
return errors.WithMessage(err, "read CSR")
}
// Load ca-config
cacfg, err := authority.LoadConfig(a.CAConfig)
if err != nil {
return errors.WithMessage(err, "failed to load CA configuration")
}
err = cacfg.Validate()
if err != nil {
return errors.WithMessage(err, "invalid ca-config")
}
isscfg := &authority.IssuerConfig{
CertFile: a.CACert,
KeyFile: a.CAKey,
AIA: &authority.AIAConfig{
AiaURL: a.AiaURL,
OcspURL: a.OcspURL,
CrlURL: a.CrlURL,
},
Profiles: cacfg.Profiles,
}
issuer, err := authority.NewIssuer(isscfg, cryptoprov)
if err != nil {
return errors.WithMessage(err, "create issuer")
}
signReq := csr.SignRequest{
SAN: a.San,
Request: string(csrPEM),
Profile: a.Profile,
}
crt, _, err := issuer.Sign(signReq)
if err != nil {
return errors.WithMessage(err, "sign request")
}
pem, _ := certutil.EncodeToPEMString(a.PemInfo, crt)
if a.Output == "" {
print.CertAndKey(ctx.Writer(), nil, nil, []byte(pem+"\n"))
} else {
err = saveCert(a.Output, nil, nil, []byte(pem+"\n"))
if err != nil {
return err
}
}
return nil
}
// SaveCert to file
func saveCert(baseName string, key, csrPEM, certPEM []byte) error {
var err error
if len(certPEM) > 0 {
err = os.WriteFile(baseName+".pem", certPEM, 0664)
if err != nil {
return errors.WithStack(err)
}
}
if len(csrPEM) > 0 {
err = os.WriteFile(baseName+".csr", csrPEM, 0664)
if err != nil {
return errors.WithStack(err)
}
}
if len(key) > 0 {
err = os.WriteFile(baseName+".key", key, 0600)
if err != nil {
return errors.WithStack(err)
}
}
return nil
}