forked from txthinking/mad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert.go
139 lines (126 loc) · 2.89 KB
/
cert.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
package mad
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"math/big"
"net"
"os"
"time"
)
type Cert struct {
CaPEM []byte
CaKeyPEM []byte
C *x509.Certificate
CertPEM []byte
KeyPEM []byte
}
func NewCert(caPEM, caKeyPEM []byte, Organization, OrganizationalUnit string) *Cert {
c := &x509.Certificate{
Subject: pkix.Name{
Organization: []string{Organization},
OrganizationalUnit: []string{OrganizationalUnit},
},
NotBefore: time.Date(2019, time.June, 1, 0, 0, 0, 0, time.UTC),
NotAfter: time.Now().AddDate(10, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
BasicConstraintsValid: true,
}
return &Cert{
CaPEM: caPEM,
CaKeyPEM: caKeyPEM,
C: c,
}
}
func (c *Cert) SetIPAddresses(ips []net.IP) {
c.C.IPAddresses = ips
if len(ips) > 0 {
c.C.Subject.CommonName = ips[0].String()
}
}
func (c *Cert) SetDNSNames(domains []string) {
c.C.DNSNames = domains
if len(domains) > 0 {
c.C.Subject.CommonName = domains[0]
}
}
func (c *Cert) Create() error {
tc, err := tls.X509KeyPair(c.CaPEM, c.CaKeyPEM)
if err != nil {
return err
}
ca, err := x509.ParseCertificate(tc.Certificate[0])
if err != nil {
return err
}
p, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
pub := p.Public()
b, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return err
}
var spki struct {
Algorithm pkix.AlgorithmIdentifier
SubjectPublicKey asn1.BitString
}
if _, err := asn1.Unmarshal(b, &spki); err != nil {
return err
}
skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
c.C.SubjectKeyId = skid[:]
sn, err := rand.Int(rand.Reader, big.NewInt(0).Lsh(big.NewInt(1), 128))
if err != nil {
return err
}
c.C.SerialNumber = sn
b, err = x509.CreateCertificate(rand.Reader, c.C, ca, pub, tc.PrivateKey)
if err != nil {
return err
}
c.CertPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: b})
// c.KeyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(p)})
b, err = x509.MarshalPKCS8PrivateKey(p)
if err != nil {
return err
}
c.KeyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: b})
return nil
}
func (c *Cert) Cert() []byte {
return c.CertPEM
}
func (c *Cert) Key() []byte {
return c.KeyPEM
}
func (c *Cert) SaveToFile(cert, key string) error {
f, err := os.Create(cert)
if err != nil {
return err
}
if _, err := f.Write(c.CertPEM); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
f, err = os.Create(key)
if err != nil {
return err
}
if _, err := f.Write(c.KeyPEM); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}