forked from cloudflare/cfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfssl_genkey.go
94 lines (76 loc) · 1.7 KB
/
cfssl_genkey.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"github.com/cloudflare/cfssl/csr"
cferr "github.com/cloudflare/cfssl/errors"
"github.com/cloudflare/cfssl/initca"
)
var genkeyUsageText = `cfssl genkey -- generate a new key and CSR
Usage of genkey:
cfssl genkey CSRJSON
Arguments:
CSRJSON: JSON file containing the request
Flags:
`
var genkeyFlags = []string{"initca", "f"}
func genkeyMain(args []string) (err error) {
csrFile, args, err := popFirstArgument(args)
if err != nil {
return
}
csrFileBytes, err := ioutil.ReadFile(csrFile)
if err != nil {
return
}
var req csr.CertificateRequest
err = json.Unmarshal(csrFileBytes, &req)
if err != nil {
return
}
if Config.isCA {
var key, cert []byte
cert, key, err = initca.New(&req)
if err != nil {
return
}
var out = struct {
Key string `json:"key"`
Cert string `json:"cert"`
}{string(key), string(cert)}
var jsonOut []byte
jsonOut, err = json.Marshal(out)
if err != nil {
return
}
fmt.Printf("%s\n", string(jsonOut))
} else {
var key, csrPEM []byte
g := &csr.Generator{validator}
csrPEM, key, err = g.ProcessRequest(&req)
if err != nil {
key = nil
return
}
var out = struct {
Key string `json:"key"`
CSR string `json:"csr"`
}{string(key), string(csrPEM)}
var jsonOut []byte
jsonOut, err = json.Marshal(out)
if err != nil {
return
}
fmt.Printf("%s\n", string(jsonOut))
}
return nil
}
func validator(req *csr.CertificateRequest) error {
if len(req.Hosts) == 0 {
return cferr.New(cferr.PolicyError, cferr.InvalidRequest, errors.New("missing hosts field"))
}
return nil
}
var CLIGenKey = &Command{genkeyUsageText, genkeyFlags, genkeyMain}