-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign.go
219 lines (175 loc) · 6.38 KB
/
sign.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
package main
import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"os"
"time"
)
func getHelpSign() string {
return `Usage: simpleca sign <class> [--name=<name>] [--altname=<altname>] [--with=<ca name>]
Sign a key (generate a certificate). Note that the name of the key will be the CommonName in the certificate.
Available classes:
root sign a root CA (most certainly without any option to have a self-signed root CA)
intermediate sign an intermediate CA public key
client sign a client public key
--name string
(optional) The name of the key to sign (only needed if you gave a custom name to your key - which you probably
should have done).
--altname string
(optional) The additional DNS name this certificate will include. You can provide this parameter multiple times.
--with string
(optional) Sign the key with the given object (this should be the name of an intermediate CA for signing a client
key, or "root" if you want to sign an intermediate CA). Omit this option to self-sign the given key.`
}
func sign(state *State, conf Conf, class, with, keyName string, altNames []string) error {
var err error
switch class {
case "root":
if keyName == "" {
keyName = "root"
}
case "intermediate":
if keyName == "" {
keyName = "intermediate"
}
case "client":
if keyName == "" {
keyName = "client"
}
default:
return errors.New("can't sign a " + class)
}
var privKey, pubKey interface{}
var keyInState *Element
var ok bool
keyInState, ok = (*state).get(class, keyName)
if !ok {
return errors.New("key " + keyName + " is not known")
}
privKey, pubKey, err = loadPrivKey((*keyInState).Type, (*keyInState).Path)
var cert []byte
var certStruct *x509.Certificate
var serial *big.Int
// If we are signing a client key, this will tell the user about the fullchain file
var additionalMessage string
serial, err = rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(159), nil))
if err != nil {
return err
}
if with == "" {
// Self-signed certificate
if class == "client" {
certStruct = getCertForClient(serial, conf.CertificateDuration, keyName, altNames, conf.Organization, conf.Country, conf.Locality)
} else {
certStruct = getCertForCA(serial, conf.CertificateDuration, keyName, altNames, conf.Organization, conf.Country, conf.Locality)
}
cert, err = x509.CreateCertificate(rand.Reader, certStruct, certStruct, pubKey, privKey)
if err != nil {
return err
}
} else {
var withElement *Element
// Retrieve the element first from intermediate CAs, else from the root CA
withElement, ok = (*state).get("intermediate", with)
if !ok {
withElement, ok = (*state).get("root", with)
if !ok {
return errors.New("can't find a CA named " + with)
}
}
var withPrivKey interface{}
var withCertificatePem *pem.Block
var withCertificateX509 *x509.Certificate
// Load the keys
withPrivKey, _, err = loadPrivKey((*withElement).Type, (*withElement).Path)
if err != nil {
return err
}
withCertificatePem, withCertificateX509, err = loadCertificate((*withElement).Path)
if err != nil {
return err
}
if class == "client" {
certStruct = getCertForClient(serial, conf.CertificateDuration, keyName, altNames, conf.Organization, conf.Country, conf.Locality)
} else {
certStruct = getCertForCA(serial, conf.CertificateDuration, keyName, altNames, conf.Organization, conf.Country, conf.Locality)
}
cert, err = x509.CreateCertificate(rand.Reader, certStruct, withCertificateX509, pubKey, withPrivKey)
if err != nil {
return err
}
// If this is a client key, create the full chain too
if class == "client" {
var fullchainCertPath = getFullCertPath((*keyInState).Path)
fullchainCertFile, err := os.OpenFile(fullchainCertPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer fullchainCertFile.Close()
pem.Encode(fullchainCertFile, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
pem.Encode(fullchainCertFile, withCertificatePem)
additionalMessage = "A full chain certificate file is also available at " + fullchainCertPath
}
}
var certPath string = getCertPath((*keyInState).Path)
certFile, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer certFile.Close()
pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
// Save the serial number
el, ok := (*state).get(class, keyName)
if !ok {
return errors.New("Can't save serial number for " + class + " key " + keyName)
}
(*el).SerialNumber = (*serial).String()
fmt.Println(keyName + " key signed, certificate available in " + certPath)
if additionalMessage != "" {
fmt.Println(additionalMessage)
}
return nil
}
func getCertForCA(serial *big.Int, duration int, commonName string, subjectAltNames []string, organization, country, locality string) *x509.Certificate {
// The DNSNames field should contain all names (the CommonName should not be used)
var dnsNames = append([]string{commonName}, subjectAltNames...)
return &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
Organization: []string{organization},
Country: []string{country},
Locality: []string{locality},
CommonName: commonName,
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(0, duration, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
DNSNames: dnsNames,
}
}
func getCertForClient(serial *big.Int, duration int, commonName string, subjectAltNames []string, organization, country, locality string) *x509.Certificate {
// The DNSNames field should contain all names (the CommonName should not be used)
var dnsNames = append([]string{commonName}, subjectAltNames...)
return &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
Organization: []string{organization},
Country: []string{country},
Locality: []string{locality},
CommonName: commonName,
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(0, duration, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
DNSNames: dnsNames,
}
}