-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
127 lines (108 loc) · 3.31 KB
/
generate.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
package cmd
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/chenzhiwei/certctl/pkg/cert"
)
var (
size int
days int
san string
subject string
keyUsage string
extKeyUsage string
noDefaults bool
keyfile string
certfile string
generateLong string = `Generate self-signed certificate.
Examples:
# Generate self-signed certificate
certctl generate --subject "C=CN/ST=Beijing/L=Haidian/O=Any Corp/CN=any.com" \
--san "any.com,*.any.com,localhost,127.0.0.1" \
--key any.com.key --cert any.com.crt \
--days 730 --size 2048
# Set Key Usages and Extended Key usages manaully
certctl generate --subject "C=CN/ST=Beijing/L=Haidian/O=Any Corp/CN=Root CA" \
--nodefault --ku digitalSignature,keyCertSign --eku serverAuth \
--san "any.com,*.any.com,localhost,127.0.0.1" \
--key any.com.key --cert any.com.crt \
--days 730 --size 2048
The list of key usages are:
* digitalSignature
* contentCommitment
* keyEncipherment
* dataEncipherment
* keyAgreement
* keyCertSign
* cRLSign
* encipherOnly
* decipherOnly
The list of extended key usages are:
* any
* serverAuth
* clientAuth
* codeSigning
* emailProtection
* IPSECEndSystem
* IPSECTunnel
* IPSECUser
* timeStamping
* OCSPSigning
* netscapeServerGatedCrypto
* microsoftServerGatedCrypto
* microsoftCommercialCodeSigning
* microsoftKernelCodeSigning
`
generateCmd = &cobra.Command{
Use: "generate",
Aliases: []string{"create", "gen"},
Short: "Generate self-signed certificate",
Long: generateLong,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
if err := runGenerate(); err != nil {
return err
}
return nil
},
}
)
func init() {
generateCmd.Flags().StringVar(&subject, "subject", "", "the certificate subject")
generateCmd.Flags().StringVar(&san, "san", "", "the certificate subject alternate names")
generateCmd.Flags().StringVar(&keyUsage, "ku", "", "the certificate key usage")
generateCmd.Flags().StringVar(&extKeyUsage, "eku", "", "the certificate extended key usage")
generateCmd.Flags().IntVar(&days, "days", 365, "the certificate validation period")
generateCmd.Flags().IntVar(&size, "size", 2048, "the certificate RSA private key size")
generateCmd.Flags().BoolVar(&noDefaults, "nodefault", false, "do not set any default vaules")
generateCmd.Flags().StringVar(&keyfile, "key", "certctl.key", "the output key file")
generateCmd.Flags().StringVar(&certfile, "cert", "certctl.crt", "the output cert file")
generateCmd.Flags().SortFlags = false
generateCmd.MarkFlagRequired("subject")
}
func runGenerate() error {
duration := time.Hour * 24 * time.Duration(days)
if !noDefaults {
keyUsage = "digitalSignature,keyEncipherment"
extKeyUsage = "serverAuth,clientAuth"
}
certInfo, err := cert.NewCertInfo(duration, subject, san, keyUsage, extKeyUsage, false)
if err != nil {
return err
}
certBytes, keyBytes, err := cert.NewCertKey(certInfo, size)
if err != nil {
return err
}
if err := os.WriteFile(keyfile, keyBytes, 0600); err != nil {
return err
}
fmt.Printf("Writing new private key to '%s'\n", keyfile)
if err := os.WriteFile(certfile, certBytes, 0644); err != nil {
return err
}
fmt.Printf("Writing new certificate to '%s'\n", certfile)
return nil
}