/
certhandler.go
246 lines (226 loc) · 6.38 KB
/
certhandler.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
package certificate
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"os"
"time"
"github.com/chrjoh/certificateBar/key"
)
// view remote certificate
// echo |openssl s_client -connect host:443 2>/dev/null | openssl x509 -text
// view and test certificates localy
// openssl x509 -in ca.pem -text
// openssl verify -verbose -CAfile ca.pem client.pem
type Certificate struct {
Id string
Country string
Organization string
OrganizationalUnit string
CommonName string
AlternativeNames []string
Usage []string
CA bool
PrivateKey interface{}
SignatureAlg string
ValidFrom time.Time
ValidTo time.Time
}
func Sign(cert *x509.Certificate, signer *x509.Certificate, certPubKey, signerPrivateKey interface{}) []byte {
derBytes, err := x509.CreateCertificate(rand.Reader, cert, signer, certPubKey, signerPrivateKey)
if err != nil {
log.Println(err)
log.Fatalf("Failed to sign cetificate: %v\n", cert.Subject)
}
return derBytes
}
// NOTE:
//If an SSL certificate has a Subject Alternative Name (SAN) field, then SSL clients are supposed to ignore
//the common name value and seek a match in the SAN list.
//This is why the Cert always repeats the common name as the first SAN in the certificate.
func CreateCertificateTemplate(data Certificate) *x509.Certificate {
pub := key.PublicKey(data.PrivateKey)
subjectKeyId := keyIdentifier(pub)
keyUsage, extKeyUsage := getUsage(data.Usage, data.CA)
cert := &x509.Certificate{
SerialNumber: new(big.Int).SetBytes([]byte(data.Id)),
Subject: pkix.Name{
Country: []string{data.Country},
Organization: []string{data.Organization},
OrganizationalUnit: []string{data.OrganizationalUnit},
},
NotBefore: data.ValidFrom,
NotAfter: data.ValidTo,
SubjectKeyId: subjectKeyId,
BasicConstraintsValid: true,
SignatureAlgorithm: signatureAlgorithm(data.SignatureAlg, data.PrivateKey),
IsCA: data.CA,
ExtKeyUsage: extKeyUsage,
KeyUsage: keyUsage,
}
if data.CommonName != "" {
cert.Subject.CommonName = data.CommonName
}
//TODO: handle alternative ip
if len(data.AlternativeNames) > 0 {
cert.DNSNames = data.AlternativeNames
if !isStringInList(data.CommonName, data.AlternativeNames) {
cert.DNSNames = append(cert.DNSNames, data.CommonName)
}
}
return cert
}
func keyIdentifier(pub interface{}) []byte {
pbyte, _ := key.PublicKeyBitArray(pub)
hasher := sha1.New()
hasher.Write(pbyte)
return hasher.Sum(nil)
}
func signatureAlgorithm(algType string, privateKey interface{}) x509.SignatureAlgorithm {
switch privateKey.(type) {
case *rsa.PrivateKey:
return findRsaSignALg(algType)
case *ecdsa.PrivateKey:
return findEcdsaSignALg(algType)
default:
log.Fatal("Could not find any signature algorithm\n")
return x509.UnknownSignatureAlgorithm
}
}
func findEcdsaSignALg(algType string) x509.SignatureAlgorithm {
switch algType {
case "SHA1":
return x509.ECDSAWithSHA1
case "SHA256":
return x509.ECDSAWithSHA256
case "SHA384":
return x509.ECDSAWithSHA384
case "SHA512":
return x509.ECDSAWithSHA512
default:
return x509.ECDSAWithSHA256
}
}
func findRsaSignALg(algType string) x509.SignatureAlgorithm {
switch algType {
case "SHA1":
return x509.SHA1WithRSA
case "SHA256":
return x509.SHA256WithRSA
case "SHA384":
return x509.SHA384WithRSA
case "SHA512":
return x509.SHA512WithRSA
default:
return x509.SHA256WithRSA
}
}
func isStringInList(value string, list []string) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}
func CheckCertificate(dnsName string, caBytes, interCaBytes, clientBytes []byte) bool {
rootPool := x509.NewCertPool()
rootCert, _ := x509.ParseCertificate(caBytes)
rootPool.AddCert(rootCert)
interCaPool := x509.NewCertPool()
interCerts, _ := x509.ParseCertificates(interCaBytes)
for _, cert := range interCerts {
interCaPool.AddCert(cert)
}
opts := x509.VerifyOptions{
DNSName: dnsName,
Roots: rootPool,
Intermediates: interCaPool,
}
clientCert, _ := x509.ParseCertificate(clientBytes)
_, certErr := clientCert.Verify(opts)
if certErr != nil {
log.Printf("Could not verify certificate: %v\n", clientCert.Subject.CommonName)
log.Println(certErr)
return false
}
log.Println("Certificates verify: OK")
return true
}
/* TODO to be added
# key usage
KeyUsageDigitalSignature
KeyUsageContentCommitment
KeyUsageKeyEncipherment
KeyUsageDataEncipherment
KeyUsageKeyAgreement
KeyUsageCertSign
KeyUsageCRLSign
KeyUsageEncipherOnly
KeyUsageDecipherOnly
# ext key usage
ExtKeyUsageAny
ExtKeyUsageServerAuth
ExtKeyUsageClientAuth
ExtKeyUsageCodeSigning
ExtKeyUsageEmailProtection
ExtKeyUsageIPSECEndSystem
ExtKeyUsageIPSECTunnel
ExtKeyUsageIPSECUser
ExtKeyUsageTimeStamping
ExtKeyUsageOCSPSigning
ExtKeyUsageMicrosoftServerGatedCrypto
ExtKeyUsageNetscapeServerGatedCrypto
*/
func getUsage(usage []string, ca bool) (x509.KeyUsage, []x509.ExtKeyUsage) {
if len(usage) == 0 {
return getDefaultKeyUsage(ca), getDefaultExtKeyUsage(ca)
}
var keyUsage x509.KeyUsage
var extKeyUsage []x509.ExtKeyUsage
for _, key := range usage {
switch key {
case "crlsign":
keyUsage |= x509.KeyUsageCRLSign
case "certsign":
keyUsage |= x509.KeyUsageCertSign
case "encipherment":
keyUsage |= x509.KeyUsageKeyEncipherment
case "signature":
keyUsage |= x509.KeyUsageDigitalSignature
case "clientauth":
extKeyUsage = append(extKeyUsage, x509.ExtKeyUsageClientAuth)
case "serverauth":
extKeyUsage = append(extKeyUsage, x509.ExtKeyUsageServerAuth)
}
}
return keyUsage, extKeyUsage
}
func getDefaultKeyUsage(ca bool) x509.KeyUsage {
if ca {
return x509.KeyUsageCRLSign | x509.KeyUsageCertSign
}
return x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
}
func getDefaultExtKeyUsage(ca bool) []x509.ExtKeyUsage {
if ca {
return []x509.ExtKeyUsage{}
}
return []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}
}
func WritePemToFile(b []byte, fileName string) {
certFile, err := os.Create(fileName)
defer certFile.Close()
if err != nil {
log.Fatalf("Failed to open %s for writing cerificate: %s\n", fileName, err)
}
pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: b})
fmt.Printf("wrote certificate %s to file\n", fileName)
}