-
Notifications
You must be signed in to change notification settings - Fork 21
/
key_pkcs8.go
392 lines (318 loc) · 11.3 KB
/
key_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package sm9
import (
"errors"
"reflect"
"math/big"
"encoding/asn1"
"crypto/x509/pkix"
"golang.org/x/crypto/cryptobyte"
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
)
var (
oidSM9 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 302}
oidSM9Sign = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 302, 1}
oidSM9Enc = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 302, 3}
)
// pkcs8
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
}
// pkcs8 attribute info
type pkcs8Attribute struct {
Id asn1.ObjectIdentifier
Values []asn1.RawValue `asn1:"set"`
}
type sm9PrivateKey struct {
PrivateKey asn1.RawValue
PublicKey asn1.RawValue
}
// pkixPublicKey reflects a PKIX public key structure. See SubjectPublicKeyInfo
// in RFC 3280.
type pkixPublicKey struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}
func ParsePublicKey(der []byte) (key any, err error) {
var pubkey pkixPublicKey
if _, err := asn1.Unmarshal(der, &pubkey); err != nil {
return nil, err
}
if !reflect.DeepEqual(pubkey.Algo.Algorithm, oidSM9) {
return nil, errors.New("x509: not sm2 elliptic curve")
}
params := pubkey.Algo.Parameters.FullBytes
pubOID := new(asn1.ObjectIdentifier)
_, err = asn1.Unmarshal(params, pubOID)
if err != nil {
return
}
var bytes []byte
switch {
case oidSM9Sign.Equal(*pubOID):
input := cryptobyte.String(pubkey.BitString.Bytes)
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
return nil, errors.New("sm9: invalid sign user public key asn1 data")
}
key, err = NewSignMasterPublicKey(bytes)
return
case oidSM9Enc.Equal(*pubOID):
input := cryptobyte.String(pubkey.BitString.Bytes)
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
return nil, errors.New("sm9: invalid encrypt user public key asn1 data")
}
key, err = NewEncryptMasterPublicKey(bytes)
return
}
return nil, errors.New("not support yet")
}
func MarshalPublicKey(key any) ([]byte, error) {
var r pkixPublicKey
var algo pkix.AlgorithmIdentifier
var pubBytes []byte
var oidBytes []byte
var err error
switch k := key.(type) {
case *SignMasterPublicKey:
pubBytes = k.Mpk.MarshalUncompressed()
oidBytes, err = asn1.Marshal(oidSM9Sign)
case *EncryptMasterPublicKey:
pubBytes = k.Mpk.MarshalUncompressed()
oidBytes, err = asn1.Marshal(oidSM9Enc)
default:
return nil, errors.New("sm9: no support key algo")
}
if err != nil {
return nil, errors.New("sm9: failed to marshal algo param: " + err.Error())
}
algo.Algorithm = oidSM9
algo.Parameters.Class = 0
algo.Parameters.Tag = 6
algo.Parameters.IsCompound = false
algo.Parameters.FullBytes = oidBytes
var b cryptobyte.Builder
b.AddASN1BitString(pubBytes)
pub, err := b.Bytes()
if err != nil {
return nil, err
}
r.Algo = algo
r.BitString = asn1.BitString{
Bytes: pub,
}
return asn1.Marshal(r)
}
// =============
func ParsePrivateKey(der []byte) (any, error) {
var privKey pkcs8
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
return nil, err
}
if privKey.Algo.Algorithm.Equal(oidSM9) ||
privKey.Algo.Algorithm.Equal(oidSM9Sign) ||
privKey.Algo.Algorithm.Equal(oidSM9Enc) {
return parsePrivateKey(privKey)
}
return nil, errors.New("sm9: unknown private key algorithm")
}
func MarshalPrivateKey(key any) ([]byte, error) {
switch k := key.(type) {
case *SignPrivateKey:
return marshalSignPrivateKey(k)
case *EncryptPrivateKey:
return marshalEncPrivateKey(k)
case *SignMasterPrivateKey:
return marshalSignMasterPrivateKey(k)
case *EncryptMasterPrivateKey:
return marshalEncMasterPrivateKey(k)
}
return nil, errors.New("key error")
}
// =============
func parsePrivateKey(privKey pkcs8) (key any, err error) {
var bytes []byte
var inner cryptobyte.String
var pubBytes []byte
switch {
case privKey.Algo.Algorithm.Equal(oidSM9Sign):
input := cryptobyte.String(privKey.PrivateKey)
if !input.ReadASN1(&inner, cryptobyte_asn1.SEQUENCE) ||
!input.Empty() ||
!inner.ReadASN1BitStringAsBytes(&bytes) {
return nil, errors.New("sm9: invalid sign user private key asn1 data")
}
if !inner.Empty() && (!inner.ReadASN1BitStringAsBytes(&pubBytes) || !inner.Empty()) {
return nil, errors.New("sm9: invalid sign user private key asn1 data")
}
priBytes := append(bytes, pubBytes...)
key, err = NewSignPrivateKey(priBytes)
return
case privKey.Algo.Algorithm.Equal(oidSM9Enc):
input := cryptobyte.String(privKey.PrivateKey)
if !input.ReadASN1(&inner, cryptobyte_asn1.SEQUENCE) ||
!input.Empty() ||
!inner.ReadASN1BitStringAsBytes(&bytes) {
return nil, errors.New("sm9: invalid encrypt user private key asn1 data")
}
if !inner.Empty() && (!inner.ReadASN1BitStringAsBytes(&pubBytes) || !inner.Empty()) {
return nil, errors.New("sm9: invalid encrypt user private key asn1 data")
}
priBytes := append(bytes, pubBytes...)
key, err = NewEncryptPrivateKey(priBytes)
return
case privKey.Algo.Algorithm.Equal(oidSM9):
bytes := privKey.Algo.Parameters.FullBytes
priOID := new(asn1.ObjectIdentifier)
_, err = asn1.Unmarshal(bytes, priOID)
if err != nil {
return
}
d := &big.Int{}
switch {
case oidSM9Sign.Equal(*priOID):
input := cryptobyte.String(privKey.PrivateKey)
if !input.ReadASN1(&inner, cryptobyte_asn1.SEQUENCE) ||
!input.Empty() ||
!inner.ReadASN1Integer(d) {
return nil, errors.New("sm9: invalid sign master private key asn1 data")
}
if !inner.Empty() && (!inner.ReadASN1BitStringAsBytes(&pubBytes) || !inner.Empty()) {
return nil, errors.New("sm9: invalid sign master private key asn1 data")
}
key, err = NewSignMasterPrivateKey(d.Bytes())
return
case oidSM9Enc.Equal(*priOID):
input := cryptobyte.String(privKey.PrivateKey)
if !input.ReadASN1(&inner, cryptobyte_asn1.SEQUENCE) ||
!input.Empty() ||
!inner.ReadASN1Integer(d) {
return nil, errors.New("sm9: invalid encrypt master private key asn1 data")
}
if !inner.Empty() && (!inner.ReadASN1BitStringAsBytes(&pubBytes) || !inner.Empty()) {
return nil, errors.New("sm9: invalid encrypt master private key asn1 data")
}
key, err = NewEncryptMasterPrivateKey(d.Bytes())
return
}
}
return nil, errors.New("not support yet")
}
// =============
func marshalSignPrivateKey(k *SignPrivateKey) ([]byte, error) {
var privKey pkcs8
privKey.Algo = pkix.AlgorithmIdentifier{
Algorithm: oidSM9Sign,
Parameters: asn1.NullRawValue,
}
var b cryptobyte.Builder
b.AddASN1BitString(k.Sk.MarshalUncompressed())
privans1, err := b.Bytes()
if err != nil {
return nil, err
}
var pub cryptobyte.Builder
pub.AddASN1BitString(k.Mpk.MarshalUncompressed())
pubasn1, err := pub.Bytes()
if err != nil {
return nil, err
}
key := sm9PrivateKey{}
key.PrivateKey.FullBytes = privans1
key.PublicKey.FullBytes = pubasn1
if privKey.PrivateKey, err = asn1.Marshal(key); err != nil {
return nil, errors.New("sm9: failed to marshal sm9 sign private key while building PKCS#8: " + err.Error())
}
return asn1.Marshal(privKey)
}
func marshalEncPrivateKey(k *EncryptPrivateKey) ([]byte, error) {
var privKey pkcs8
privKey.Algo = pkix.AlgorithmIdentifier{
Algorithm: oidSM9Enc,
Parameters: asn1.NullRawValue,
}
var b cryptobyte.Builder
b.AddASN1BitString(k.Sk.MarshalUncompressed())
privans1, err := b.Bytes()
if err != nil {
return nil, err
}
var pub cryptobyte.Builder
pub.AddASN1BitString(k.Mpk.MarshalUncompressed())
pubasn1, err := pub.Bytes()
if err != nil {
return nil, err
}
key := sm9PrivateKey{}
key.PrivateKey.FullBytes = privans1
key.PublicKey.FullBytes = pubasn1
if privKey.PrivateKey, err = asn1.Marshal(key); err != nil {
return nil, errors.New("sm9: failed to marshal sm9 encrypt private key while building PKCS#8: " + err.Error())
}
return asn1.Marshal(privKey)
}
func marshalSignMasterPrivateKey(k *SignMasterPrivateKey) ([]byte, error) {
var privKey pkcs8
oidBytes, err := asn1.Marshal(oidSM9Sign)
if err != nil {
return nil, errors.New("sm9: failed to marshal SM9 OID: " + err.Error())
}
privKey.Algo = pkix.AlgorithmIdentifier{
Algorithm: oidSM9,
Parameters: asn1.RawValue{
FullBytes: oidBytes,
},
}
var pri cryptobyte.Builder
pri.AddASN1BigInt(k.D)
privans1, err := pri.Bytes()
if err != nil {
return nil, err
}
var pub cryptobyte.Builder
pub.AddASN1BitString(k.Mpk.MarshalUncompressed())
pubasn1, err := pub.Bytes()
if err != nil {
return nil, err
}
key := sm9PrivateKey{}
key.PrivateKey.FullBytes = privans1
key.PublicKey.FullBytes = pubasn1
if privKey.PrivateKey, err = asn1.Marshal(key); err != nil {
return nil, errors.New("sm9: failed to marshal sm9 sign master private key while building PKCS#8: " + err.Error())
}
return asn1.Marshal(privKey)
}
func marshalEncMasterPrivateKey(k *EncryptMasterPrivateKey) ([]byte, error) {
var privKey pkcs8
oidBytes, err := asn1.Marshal(oidSM9Enc)
if err != nil {
return nil, errors.New("sm9: failed to marshal SM9 OID: " + err.Error())
}
privKey.Algo = pkix.AlgorithmIdentifier{
Algorithm: oidSM9,
Parameters: asn1.RawValue{
FullBytes: oidBytes,
},
}
var pri cryptobyte.Builder
pri.AddASN1BigInt(k.D)
privans1, err := pri.Bytes()
if err != nil {
return nil, err
}
var pub cryptobyte.Builder
pub.AddASN1BitString(k.Mpk.MarshalUncompressed())
pubasn1, err := pub.Bytes()
if err != nil {
return nil, err
}
key := sm9PrivateKey{}
key.PrivateKey.FullBytes = privans1
key.PublicKey.FullBytes = pubasn1
if privKey.PrivateKey, err = asn1.Marshal(key); err != nil {
return nil, errors.New("sm9: failed to marshal sm9 encrypt master private key while building PKCS#8: " + err.Error())
}
return asn1.Marshal(privKey)
}