-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (140 loc) · 4.56 KB
/
main.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
package main
import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"crypto/sha256"
"encoding/asn1"
"encoding/pem"
"github.com/jessevdk/go-flags"
"io/ioutil"
"log"
"github.com/google/uuid"
"math/big"
"net"
"os"
"time"
)
var options struct {
Validity int64 `short:"v" long:"validity" description:"Certificate validity period (days)" default:"90"`
KeyFile string `short:"k" long:"key" description:"CA Private key, PEM format" required:"true"`
EmailAddress []string `short:"E" long:"email" description:"Email Address" required:"true"`
Hosts []string `short:"H" long:"hosts" description:"DNS name or IP addresse"`
Country []string `short:"C" long:"country" description:"Country"`
Province []string `short:"P" long:"province" description:"Province"`
Locality []string `short:"L" long:"locality" description:"Locality"`
OrganizationalUnit []string `short:"U" long:"organisational-unit" description:"Organizational Unit"`
Organization []string `short:"O" long:"organisation" description:"Organization"`
CommonName string `short:"N" long:"common-name" description:"Common Name" required:"true"`
CrlUri []string `short:"d" long:"crl-distribution" description:"CRL Distribution URI" required:"false"`
CaUri []string `short:"i" long:"ca-issuers-distribution" description:"CA Issuer Chains (p7c)" required:"false"`
}
// OID of email address.
var oidEmailAddress = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
func main() {
// Parse flags.
_, err := flags.Parse(&options)
if err != nil {
os.Exit(1)
}
// Read keyfile
raw, err := ioutil.ReadFile(options.KeyFile)
if err != nil {
log.Fatalf("failed to read key file: %s", err)
}
// Parse PEM in keyfile
keyPem, _ := pem.Decode([]byte(raw))
if keyPem == nil {
log.Fatalf("failed to parse PEM key")
}
// Parse key from PEM.
key, err := x509.ParseECPrivateKey(keyPem.Bytes)
if err != nil {
log.Fatalf("failed to read key file: %s", err)
}
// Certificate validity period starts now.
notBefore := time.Now().UTC()
// Work out certificate expiry.
duration := time.Duration(options.Validity*24) * time.Hour
notAfter := notBefore.Add(duration)
// Make up a random serial number.
uuidVal, err := uuid.NewRandom()
if err != nil {
log.Fatalf("failed to generate uuid: %s", err)
}
uuidBytes,err := uuidVal.MarshalBinary()
if err != nil {
log.Fatalf("failed to generate serial: %s", err)
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serial := serialNumberLimit.SetBytes(uuidBytes)
// Create the certificate subject based on options.
subject := pkix.Name{}
if len(options.Country) > 0 {
subject.Country = options.Country
}
if len(options.Province) > 0 {
subject.Province = options.Province
}
if len(options.Locality) > 0 {
subject.Locality = options.Locality
}
if len(options.OrganizationalUnit) > 0 {
subject.OrganizationalUnit =
options.OrganizationalUnit
}
if len(options.Organization) > 0 {
subject.Organization =
options.Organization
}
if options.CommonName != "" {
subject.CommonName = options.CommonName
}
// Create populated certificate template
template := x509.Certificate{
SignatureAlgorithm: x509.ECDSAWithSHA512,
SerialNumber: serial,
Subject: subject,
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageCertSign |
x509.KeyUsageDigitalSignature |
x509.KeyUsageCRLSign,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{},
BasicConstraintsValid: true,
EmailAddresses: options.EmailAddress,
}
// Add optional hosts containing IP address and DNS names.
if len(options.Hosts) > 0 {
for _, h := range options.Hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses =
append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
}
pkix, err := x509.MarshalPKIXPublicKey(key.Public())
if err != nil {
log.Fatalf("failed to get Public Key bytes: %s", err)
}
pkixSum := sha256.Sum256(pkix)
template.SubjectKeyId = pkixSum[:]
if len(options.CrlUri) > 0 {
template.CRLDistributionPoints = options.CrlUri
}
if len(options.CaUri) > 0 {
template.IssuingCertificateURL = options.CaUri
}
// Sign the certificate.
derBytes, err := x509.CreateCertificate(rand.Reader, &template,
&template, &key.PublicKey, key)
if err != nil {
log.Fatalf("Failed to create certificate: %s", err)
}
// Output certificate in PEM format.
pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
os.Exit(0)
}