/
cert.go
199 lines (187 loc) · 6.71 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
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
package cert
import (
"bytes"
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"io/ioutil"
"math/big"
"math/rand"
"net"
"strconv"
"time"
)
func CreateSignCertToFile(rootCa *x509.Certificate, rootKey *rsa.PrivateKey, domainOrIP string, expireDays int, name string) (err error) {
cert, key, err := CreateSignCert(rootCa, rootKey, domainOrIP, expireDays)
if err != nil {
return
}
err = ioutil.WriteFile(name+".crt", cert, 0755)
if err != nil {
return
}
err = ioutil.WriteFile(name+".key", key, 0755)
return
}
func CreateSignCert(rootCa *x509.Certificate, rootKey *rsa.PrivateKey, domainOrIP string, expireDays int) (certBytes []byte, keyBytes []byte, err error) {
cer := &x509.Certificate{
SerialNumber: big.NewInt(rand.Int63()), //证书序列号
Subject: pkix.Name{
Country: []string{getCountry()},
Organization: []string{domainOrIP},
OrganizationalUnit: []string{domainOrIP},
CommonName: domainOrIP,
},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour * 24 * time.Duration(expireDays)),
BasicConstraintsValid: true,
IsCA: false,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
//KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageDataEncipherment,
EmailAddresses: []string{},
IPAddresses: []net.IP{},
}
if ip := net.ParseIP(domainOrIP); ip != nil {
cer.IPAddresses = append(cer.IPAddresses, ip)
} else {
cer.DNSNames = append(cer.DNSNames, domainOrIP)
}
// cer.IPAddresses = append(cer.IPAddresses, alternateIPs...)
// cer.DNSNames = append(cer.DNSNames, alternateDNS...)
//生成公钥私钥对
priKey, err := rsa.GenerateKey(cryptorand.Reader, 2048)
if err != nil {
return
}
certBytes, err = x509.CreateCertificate(cryptorand.Reader, cer, rootCa, &priKey.PublicKey, rootKey)
if err != nil {
return
}
//编码证书文件和私钥文件
caPem := &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
}
certBytes = pem.EncodeToMemory(caPem)
buf := x509.MarshalPKCS1PrivateKey(priKey)
keyPem := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: buf,
}
keyBytes = pem.EncodeToMemory(keyPem)
return
}
func CreateCaToFile(name, domainOrIP string, expireDays int) (err error) {
ca, key, err := CreateCa(domainOrIP, expireDays)
if err != nil {
return
}
err = ioutil.WriteFile(name+".crt", ca, 0755)
if err != nil {
return
}
err = ioutil.WriteFile(name+".key", key, 0755)
return
}
func CreateCa(organization string, expireDays int) (certBytes []byte, keyBytes []byte, err error) {
priv, err := rsa.GenerateKey(cryptorand.Reader, 2048)
if err != nil {
return nil, nil, err
}
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: organization,
Organization: []string{organization},
OrganizationalUnit: []string{organization},
Country: []string{getCountry()},
},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour * 24 * time.Duration(expireDays)),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
IsCA: true,
}
derBytes, err := x509.CreateCertificate(cryptorand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return nil, nil, err
}
// Generate cert
certBuffer := bytes.Buffer{}
if err := pem.Encode(&certBuffer, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return nil, nil, err
}
// Generate key
keyBuffer := bytes.Buffer{}
if err := pem.Encode(&keyBuffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
return nil, nil, err
}
return certBuffer.Bytes(), keyBuffer.Bytes(), nil
}
func ParseCertAndKeyBytes(certPemFileByes, keyFileBytes []byte) (cert *x509.Certificate, privateKey *rsa.PrivateKey, err error) {
//解析根证书
cert, err = ParseCertBytes(certPemFileByes)
if err != nil {
return
}
//解析私钥
privateKey, err = ParseKeyBytes(keyFileBytes)
return
}
func ParseCertAndKey(certPemFile, keyFile string) (cert *x509.Certificate, privateKey *rsa.PrivateKey, err error) {
//解析根证书
cert, err = ParseCert(certPemFile)
if err != nil {
return
}
//解析私钥
privateKey, err = ParseKey(keyFile)
return
}
func ParseCert(certPemFile string) (cert *x509.Certificate, err error) {
//解析证书
certFile_, err := ioutil.ReadFile(certPemFile)
if err != nil {
return
}
cert, err = ParseCertBytes(certFile_)
return
}
func ParseKey(keyFile string) (key *rsa.PrivateKey, err error) {
//解析证书
keyFile_, err := ioutil.ReadFile(keyFile)
if err != nil {
return
}
key, err = ParseKeyBytes(keyFile_)
return
}
func ParseCertBytes(certPemFileBytes []byte) (cert *x509.Certificate, err error) {
caBlock, _ := pem.Decode(certPemFileBytes)
cert, err = x509.ParseCertificate(caBlock.Bytes)
return
}
func ParseKeyBytes(keyFileBytes []byte) (praKey *rsa.PrivateKey, err error) {
keyBlock, _ := pem.Decode(keyFileBytes)
praKey, err = x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
return
}
func getCountry() string {
CList := []string{"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BR", "BS", "BW", "BY", "BZ", "CA", "CF", "CG", "CH", "CK", "CL", "CM", "CN", "CO", "CR", "CS", "CU", "CY", "CZ", "DE", "DJ", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "ET", "FI", "FJ", "FR", "GA", "GB", "GD", "GE", "GF", "GH", "GI", "GM", "GN", "GR", "GT", "GU", "GY", "HK", "HN", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IR", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KP", "KR", "KT", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "ML", "MM", "MN", "MO", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PR", "PT", "PY", "QA", "RO", "RU", "SA", "SB", "SC", "SD", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TD", "TG", "TH", "TJ", "TM", "TN", "TO", "TR", "TT", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "YE", "YU", "ZA", "ZM", "ZR", "ZW"}
return CList[int(randInt(4))%len(CList)]
}
func randInt(strLen int) int64 {
codes := "123456789"
codeLen := len(codes)
data := make([]byte, strLen)
rand.Seed(time.Now().UnixNano() + rand.Int63() + rand.Int63() + rand.Int63() + rand.Int63())
for i := 0; i < strLen; i++ {
idx := rand.Intn(codeLen)
data[i] = byte(codes[idx])
}
i, _ := strconv.ParseInt(string(data), 10, 64)
return i
}