-
Notifications
You must be signed in to change notification settings - Fork 4
/
cert_store.go
99 lines (85 loc) · 3.06 KB
/
cert_store.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"time"
)
// YearIssueTime is an issuance of this year's date on January 1 at midnight.
var YearIssueTime = time.Date(time.Now().Year(), time.January, 1, 0, 0, 0, 0, time.UTC)
// generateSerial generates a random serial number for our issued certificates.
// It is taken from golang std: src/crypto/tls/generate_cert.go
// Direct permalink on GitHub: https://git.io/JyyDw
func generateSerial() *big.Int {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
check(err)
return serialNumber
}
func createCertificates() []byte {
////////////////////////////////////
// Generate root CA //
////////////////////////////////////
rootCert := &x509.Certificate{
SignatureAlgorithm: x509.SHA1WithRSA,
SerialNumber: generateSerial(),
Subject: pkix.Name{
CommonName: "Open Shop Channel CA",
},
NotBefore: YearIssueTime,
NotAfter: YearIssueTime.AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
rootPriv, err := rsa.GenerateKey(rand.Reader, 2048)
check(err)
rootPublic, err := x509.CreateCertificate(rand.Reader, rootCert, rootCert, &rootPriv.PublicKey, rootPriv)
check(err)
////////////////////////////////////
// Issue server TLS certificate //
////////////////////////////////////
// We'll issue a wildcard for our CN and SANs.
// Is this recommended? Absolutely not, but who's to stop us?
issueName := "*." + baseDomain
serverCert := x509.Certificate{
SignatureAlgorithm: x509.SHA1WithRSA,
SerialNumber: generateSerial(),
Subject: pkix.Name{
CommonName: issueName,
},
DNSNames: []string{
issueName,
},
NotBefore: YearIssueTime,
NotAfter: YearIssueTime.AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageKeyAgreement | x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: false,
}
serverPriv, err := rsa.GenerateKey(rand.Reader, 2048)
check(err)
serverPublic, err := x509.CreateCertificate(rand.Reader, &serverCert, rootCert, &serverPriv.PublicKey, rootPriv)
check(err)
////////////////////////////
// Persist certificates //
////////////////////////////
rootCertPem := pemEncode("CERTIFICATE", rootPublic)
rootKeyPem := pemEncode("RSA PRIVATE KEY", x509.MarshalPKCS1PrivateKey(rootPriv))
serverCertPem := pemEncode("CERTIFICATE", serverPublic)
serverKeyPem := pemEncode("RSA PRIVATE KEY", x509.MarshalPKCS1PrivateKey(serverPriv))
writeOut("root.pem", rootCertPem)
writeOut("root.cer", rootPublic)
writeOut("root.key", rootKeyPem)
writeOut("server.pem", serverCertPem)
writeOut("server.key", serverKeyPem)
return rootPublic
}
func pemEncode(typeName string, bytes []byte) []byte {
block := pem.Block{Type: typeName, Bytes: bytes}
return pem.EncodeToMemory(&block)
}