-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca.go
118 lines (102 loc) · 2.9 KB
/
ca.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
package proxy
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/binary"
"encoding/pem"
"io"
"math/big"
"time"
)
func GenerateRandomKey(length int) []byte {
k := make([]byte, length)
if _, err := io.ReadFull(rand.Reader, k); err != nil {
return nil
}
return k
}
func CreateRootCertificate(certPEM, privPEM io.Writer) error {
keyID := GenerateRandomKey(20)
cert := &x509.Certificate{
SerialNumber: big.NewInt(int64(binary.BigEndian.Uint64(GenerateRandomKey(16)))),
Subject: pkix.Name{
Organization: []string{"Proxy Inc"},
CommonName: "Proxy Global Root CA",
},
SubjectKeyId: keyID,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
BasicConstraintsValid: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
AuthorityKeyId: keyID,
}
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
certBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &privKey.PublicKey, privKey)
if err != nil {
return err
}
if err := pem.Encode(certPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
}); err != nil {
return err
}
if err := pem.Encode(privPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privKey),
}); err != nil {
return err
}
return nil
}
func CreateSubCertificate(parentCertPEM, parentPrivPEM []byte, certPEM, privPEM io.Writer, servername string) error {
pemToBytes := func(data []byte) []byte {
p, _ := pem.Decode(data)
return p.Bytes
}
parentCert, err := x509.ParseCertificate(pemToBytes(parentCertPEM))
if err != nil {
return err
}
parenPrivKey, err := x509.ParsePKCS1PrivateKey(pemToBytes(parentPrivPEM))
if err != nil {
return err
}
cert := &x509.Certificate{
SerialNumber: big.NewInt(1658),
Subject: pkix.Name{CommonName: servername},
DNSNames: []string{servername},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
AuthorityKeyId: parentCert.SubjectKeyId,
IsCA: false,
BasicConstraintsValid: false,
}
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
certBytes, err := x509.CreateCertificate(rand.Reader, cert, parentCert, &privKey.PublicKey, parenPrivKey)
if err != nil {
return err
}
if err := pem.Encode(certPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
}); err != nil {
return err
}
return pem.Encode(privPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privKey),
})
}