-
Notifications
You must be signed in to change notification settings - Fork 24
/
pkcs8.go
258 lines (210 loc) · 6.26 KB
/
pkcs8.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package dsa
import (
"fmt"
"errors"
"math/big"
"crypto/dsa"
"crypto/x509/pkix"
"encoding/asn1"
"golang.org/x/crypto/cryptobyte"
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
)
var (
// dsa 公钥 oid
oidPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
)
// dsa Parameters
type dsaAlgorithmParameters struct {
P, Q, G *big.Int
}
// 私钥 - 包装
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
}
// 公钥 - 包装
type pkixPublicKey struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}
// 公钥信息 - 解析
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}
var (
defaultPKCS8Key = NewPKCS8Key()
)
/**
* dsa pkcs8 密钥
*
* @create 2022-3-19
* @author deatil
*/
type PKCS8Key struct {}
// 构造函数
func NewPKCS8Key() PKCS8Key {
return PKCS8Key{}
}
// PKCS8 包装公钥
func (this PKCS8Key) MarshalPublicKey(key *dsa.PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier
var err error
// 创建数据
paramBytes, err := asn1.Marshal(dsaAlgorithmParameters{
P: key.P,
Q: key.Q,
G: key.G,
})
if err != nil {
return nil, errors.New("dsa: failed to marshal algo param: " + err.Error())
}
publicKeyAlgorithm.Algorithm = oidPublicKeyDSA
publicKeyAlgorithm.Parameters.FullBytes = paramBytes
var yInt cryptobyte.Builder
yInt.AddASN1BigInt(key.Y)
publicKeyBytes, err = yInt.Bytes()
if err != nil {
return nil, errors.New("dsa: failed to builder PrivateKey: " + err.Error())
}
pkix := pkixPublicKey{
Algo: publicKeyAlgorithm,
BitString: asn1.BitString{
Bytes: publicKeyBytes,
BitLength: 8 * len(publicKeyBytes),
},
}
return asn1.Marshal(pkix)
}
// PKCS8 包装公钥
func MarshalPKCS8PublicKey(pub *dsa.PublicKey) ([]byte, error) {
return defaultPKCS8Key.MarshalPublicKey(pub)
}
// PKCS8 解析公钥
func (this PKCS8Key) ParsePublicKey(der []byte) (*dsa.PublicKey, error) {
var pki publicKeyInfo
rest, err := asn1.Unmarshal(der, &pki)
if err != nil {
return nil, err
}
if len(rest) > 0 {
return nil, asn1.SyntaxError{Msg: "trailing data"}
}
algoEq := pki.Algorithm.Algorithm.Equal(oidPublicKeyDSA)
if !algoEq {
return nil, errors.New("dsa: unknown public key algorithm")
}
// 解析
keyData := &pki
yDer := cryptobyte.String(keyData.PublicKey.RightAlign())
y := new(big.Int)
if !yDer.ReadASN1Integer(y) {
return nil, errors.New("dsa: invalid DSA public key")
}
pub := &dsa.PublicKey{
Y: y,
Parameters: dsa.Parameters{
P: new(big.Int),
Q: new(big.Int),
G: new(big.Int),
},
}
paramsDer := cryptobyte.String(keyData.Algorithm.Parameters.FullBytes)
if !paramsDer.ReadASN1(¶msDer, cryptobyte_asn1.SEQUENCE) ||
!paramsDer.ReadASN1Integer(pub.P) ||
!paramsDer.ReadASN1Integer(pub.Q) ||
!paramsDer.ReadASN1Integer(pub.G) {
return nil, errors.New("dsa: invalid DSA parameters")
}
if pub.Y.Sign() <= 0 || pub.P.Sign() <= 0 ||
pub.Q.Sign() <= 0 || pub.G.Sign() <= 0 {
return nil, errors.New("dsa: zero or negative DSA parameter")
}
return pub, nil
}
// PKCS8 解析公钥
func ParsePKCS8PublicKey(derBytes []byte) (*dsa.PublicKey, error) {
return defaultPKCS8Key.ParsePublicKey(derBytes)
}
// ====================
// PKCS8 包装私钥
func (this PKCS8Key) MarshalPrivateKey(key *dsa.PrivateKey) ([]byte, error) {
var privKey pkcs8
// params
paramBytes, err := asn1.Marshal(dsaAlgorithmParameters{
P: key.P,
Q: key.Q,
G: key.G,
})
if err != nil {
return nil, errors.New("dsa: failed to marshal algo param: " + err.Error())
}
privKey.Algo = pkix.AlgorithmIdentifier{
Algorithm: oidPublicKeyDSA,
Parameters: asn1.RawValue{
FullBytes: paramBytes,
},
}
var xInt cryptobyte.Builder
xInt.AddASN1BigInt(key.X)
privateKeyBytes, err := xInt.Bytes()
if err != nil {
return nil, errors.New("dsa: failed to builder PrivateKey: " + err.Error())
}
privKey.PrivateKey = privateKeyBytes
return asn1.Marshal(privKey)
}
// PKCS8 包装私钥
func MarshalPKCS8PrivateKey(key *dsa.PrivateKey) ([]byte, error) {
return defaultPKCS8Key.MarshalPrivateKey(key)
}
// PKCS8 解析私钥
func (this PKCS8Key) ParsePrivateKey(der []byte) (key *dsa.PrivateKey, err error) {
var privKey pkcs8
_, err = asn1.Unmarshal(der, &privKey)
if err != nil {
return nil, err
}
if !privKey.Algo.Algorithm.Equal(oidPublicKeyDSA) {
return nil, fmt.Errorf("dsa: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algo.Algorithm)
}
xDer := cryptobyte.String(string(privKey.PrivateKey))
x := new(big.Int)
if !xDer.ReadASN1Integer(x) {
return nil, errors.New("dsa: invalid DSA public key")
}
priv := &dsa.PrivateKey{
PublicKey: dsa.PublicKey{
Parameters: dsa.Parameters{
P: new(big.Int),
Q: new(big.Int),
G: new(big.Int),
},
Y: new(big.Int),
},
X: x,
}
// 找出 p,q,g 数据
paramsDer := cryptobyte.String(privKey.Algo.Parameters.FullBytes)
if !paramsDer.ReadASN1(¶msDer, cryptobyte_asn1.SEQUENCE) ||
!paramsDer.ReadASN1Integer(priv.P) ||
!paramsDer.ReadASN1Integer(priv.Q) ||
!paramsDer.ReadASN1Integer(priv.G) {
return nil, errors.New("dsa: invalid DSA parameters")
}
// 算出 Y 值
priv.Y.Exp(priv.G, x, priv.P)
if priv.Y.Sign() <= 0 || priv.P.Sign() <= 0 ||
priv.Q.Sign() <= 0 || priv.G.Sign() <= 0 {
return nil, errors.New("dsa: zero or negative DSA parameter")
}
return priv, nil
}
// PKCS8 解析私钥
func ParsePKCS8PrivateKey(derBytes []byte) (key *dsa.PrivateKey, err error) {
return defaultPKCS8Key.ParsePrivateKey(derBytes)
}