-
Notifications
You must be signed in to change notification settings - Fork 2
/
p12gen.go
236 lines (174 loc) · 4.29 KB
/
p12gen.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package p12gen
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"github.com/chr4/pwgen"
"github.com/gogap/p12gen/pkcs12"
)
type CA struct {
name string
certs []*x509.Certificate
key []byte
}
type PasswordGenerateFunc func() string
type CallbackFunc func(err error, caName string, certTemplate *x509.Certificate, p12Data []byte, p12Password string)
func (p *CA) Count() int {
return len(p.certs)
}
type P12Gen struct {
caList map[string]*CA
pwdGen PasswordGenerateFunc
}
func NewP12Gen() *P12Gen {
return &P12Gen{
caList: make(map[string]*CA),
pwdGen: func() string { return pwgen.AlphaNum(20) },
}
}
func (p *P12Gen) WithPwdGenerator(fn PasswordGenerateFunc) {
p.pwdGen = fn
}
func (p *P12Gen) LoadCA(name string, certData, keyData []byte) (err error) {
if _, exist := p.caList[name]; exist {
err = fmt.Errorf("%s already loaded", name)
return
}
certs, err := loadCerts(certData)
if err != nil {
return
}
ca := &CA{
name: name,
certs: certs,
key: keyData,
}
if ca.Count() == 0 {
err = fmt.Errorf("ca of %s's count is zero", name)
return
}
p.caList[name] = ca
return
}
func (p *P12Gen) CreateCertificate(caName, caPassword string, certTemplate *x509.Certificate) (p12Data []byte, p12Pwd string, err error) {
ca, exist := p.caList[caName]
if !exist {
err = fmt.Errorf("ca of %s not exist", caName)
return
}
pemBlock, _ := pem.Decode(ca.key)
var keyData = pemBlock.Bytes
if x509.IsEncryptedPEMBlock(pemBlock) {
keyData, err = x509.DecryptPEMBlock(pemBlock, []byte(caPassword))
if err != nil {
return
}
}
caKey, err := parsePrivateKey(keyData)
if err != nil {
return
}
if err != nil {
return
}
certKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return
}
cert, err := x509.CreateCertificate(rand.Reader, certTemplate, ca.certs[0], &certKey.PublicKey, caKey)
if err != nil {
return
}
x509Cert, err := x509.ParseCertificate(cert)
if err != nil {
return
}
pwd := ""
if p.pwdGen != nil {
pwd = p.pwdGen()
}
p12Data, err = pkcs12.Encode(rand.Reader, certKey, x509Cert, nil, pwd)
if err != nil {
return
}
p12Pwd = pwd
return
}
func (p *P12Gen) CreateCertificateAsync(caName, caPassword string, certTemplate *x509.Certificate, callbacks ...CallbackFunc) {
go func(caName, caPassword string, certTemplate *x509.Certificate, callbacks ...CallbackFunc) {
p12Data, p12Password, err := p.CreateCertificate(caName, caPassword, certTemplate)
for i := 0; i < len(callbacks); i++ {
callbacks[i](err, caName, certTemplate, p12Data, p12Password)
}
}(caName, caPassword, certTemplate, callbacks...)
}
func PEMToP12(certData, keyData []byte, keyPassword, p12Password string) (p12Data []byte, err error) {
certs, err := loadCerts(certData)
if err != nil {
return
}
if len(certs) == 0 {
err = errors.New("none cert parsed")
return
}
keyPEMBlock, _ := pem.Decode(keyData)
if keyPEMBlock == nil || len(keyPEMBlock.Bytes) == 0 {
err = errors.New("parse key file data failure")
return
}
keyBytes := keyPEMBlock.Bytes
if x509.IsEncryptedPEMBlock(keyPEMBlock) {
keyBytes, err = x509.DecryptPEMBlock(keyPEMBlock, []byte(keyPassword))
if err != nil {
return
}
}
privateKey, err := parsePrivateKey(keyBytes)
if err != nil {
return
}
p12Data, err = pkcs12.Encode(rand.Reader, privateKey, certs[0], nil, p12Password)
if err != nil {
return
}
return
}
func loadCerts(data []byte) (certs []*x509.Certificate, err error) {
var pemDatas []byte
for {
block, rest := pem.Decode(data)
pemDatas = append(pemDatas, block.Bytes...)
if len(rest) == 0 {
break
}
data = rest
}
parsedCerts, err := x509.ParseCertificates(pemDatas)
if err != nil {
return
}
certs = parsedCerts
return
}
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
switch key := key.(type) {
case *rsa.PrivateKey, *ecdsa.PrivateKey:
return key, nil
default:
return nil, errors.New("p12gen: found unknown private key type in PKCS#8 wrapping")
}
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, errors.New("p12gen: failed to parse private key")
}