-
Notifications
You must be signed in to change notification settings - Fork 32
/
certs.go
155 lines (139 loc) · 4.83 KB
/
certs.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
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"io/ioutil"
"log"
"math/big"
"time"
)
const path string = "./certs/"
func makeCA(subject *pkix.Name) (*x509.Certificate, *rsa.PrivateKey, error) {
// creating a CA which will be used to sign all of our certificates using the x509 package from the Go Standard Library
caCert := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: *subject,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10*365, 0, 0),
IsCA: true, // <- indicating this certificate is a CA certificate.
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
// generate a private key for the CA
caKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Printf("Generate the CA Private Key error: %v\n", err)
return nil, nil, err
}
// create the CA certificate
caBytes, err := x509.CreateCertificate(rand.Reader, caCert, caCert, &caKey.PublicKey, caKey)
if err != nil {
log.Printf("Create the CA Certificate error: %v\n", err)
return nil, nil, err
}
// Create the CA PEM files
caPEM := new(bytes.Buffer)
pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
if err := ioutil.WriteFile(path + "ca.crt", caPEM.Bytes(), 0644); err != nil {
log.Printf("Write the CA certificate file error: %v\n", err)
return nil, nil, err
}
caPrivKeyPEM := new(bytes.Buffer)
pem.Encode(caPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caKey),
})
if err := ioutil.WriteFile(path + "ca.key", caPEM.Bytes(), 0644); err != nil {
log.Printf("Write the CA certificate file error: %v\n", err)
return nil, nil, err
}
return caCert, caKey, nil
}
func makeCert(caCert *x509.Certificate, caKey *rsa.PrivateKey, subject *pkix.Name, name string) error {
cert := &x509.Certificate{
SerialNumber: big.NewInt(1658),
Subject: *subject,
//IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
DNSNames: []string{"localhost"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 6},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
certKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
log.Printf("Generate the Key error: %v\n", err)
return err
}
certBytes, err := x509.CreateCertificate(rand.Reader, cert, caCert, &certKey.PublicKey, caKey)
if err != nil {
log.Printf("Generate the certificate error: %v\n", err)
return err
}
certPEM := new(bytes.Buffer)
pem.Encode(certPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
if err := ioutil.WriteFile(path + name + ".crt", certPEM.Bytes(), 0644); err != nil {
log.Printf("Write the CA certificate file error: %v\n", err)
return err
}
certKeyPEM := new(bytes.Buffer)
pem.Encode(certKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(certKey),
})
if err := ioutil.WriteFile(path + name + ".key", certKeyPEM.Bytes(), 0644); err != nil {
log.Printf("Write the CA certificate file error: %v\n", err)
return err
}
return nil
}
func main() {
subject := pkix.Name{
Country: []string{"Earth"},
Organization: []string{"CA Company"},
OrganizationalUnit: []string{"Engineering"},
Locality: []string{"Mountain"},
Province: []string{"Asia"},
StreetAddress: []string{"Bridge"},
PostalCode: []string{"123456"},
SerialNumber: "",
CommonName: "CA",
Names: []pkix.AttributeTypeAndValue{},
ExtraNames: []pkix.AttributeTypeAndValue{},
}
caCert, caKey, err := makeCA(&subject)
if err != nil {
log.Fatalf("make CA Certificate error!")
}
log.Println("Create the CA certificate successfully.")
subject.CommonName = "Server"
subject.Organization = []string{"Server Company"}
if err := makeCert(caCert, caKey, &subject, "server"); err != nil {
log.Fatal("make Server Certificate error!")
}
log.Println("Create and Sign the Server certificate successfully.")
subject.CommonName = "Client A"
subject.Organization = []string{"Client A Company"}
if err := makeCert(caCert, caKey, &subject, "client.a"); err != nil {
log.Fatal("make Client A Certificate error!")
}
log.Println("Create and Sign the Client A certificate successfully.")
subject.CommonName = "Client B"
subject.Organization = []string{"Client B Company"}
if err := makeCert(caCert, caKey, &subject, "client.b"); err != nil {
log.Fatal("make Client B Certificate error!")
}
log.Println("Create and Sign the Client B certificate successfully.")
}