-
Notifications
You must be signed in to change notification settings - Fork 21
/
pkcs8.go
109 lines (89 loc) · 2.71 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
package pkcs8pbe
import (
"io"
"fmt"
"errors"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
)
// 结构体数据可以查看以下文档
// RFC5208 at https://tools.ietf.org/html/rfc5208
// RFC5958 at https://tools.ietf.org/html/rfc5958
type encryptedPrivateKeyInfo struct {
EncryptionAlgorithm pkix.AlgorithmIdentifier
EncryptedData []byte
}
// 加密 PKCS8
func EncryptPKCS8PrivateKey(
rand io.Reader,
blockType string,
data []byte,
password []byte,
cipher Cipher,
) (*pem.Block, error) {
if cipher == nil {
return nil, errors.New("failed to encrypt PEM: unknown cipher")
}
encrypted, marshalledParams, err := cipher.Encrypt(password, data)
if err != nil {
return nil, err
}
encryptionAlgorithm := pkix.AlgorithmIdentifier{
Algorithm: cipher.OID(),
Parameters: asn1.RawValue{
FullBytes: marshalledParams,
},
}
// 生成 ans1 数据
pki := encryptedPrivateKeyInfo{
EncryptionAlgorithm: encryptionAlgorithm,
EncryptedData: encrypted,
}
b, err := asn1.Marshal(pki)
if err != nil {
return nil, errors.New(err.Error() + " error marshaling encrypted key")
}
return &pem.Block{
Type: blockType,
Bytes: b,
}, nil
}
// 解出 PKCS8 密钥
func DecryptPKCS8PrivateKey(data, password []byte) ([]byte, error) {
var pki encryptedPrivateKeyInfo
if _, err := asn1.Unmarshal(data, &pki); err != nil {
return nil, errors.New(err.Error() + " failed to unmarshal private key")
}
cipher, cipherParams, err := parseEncryptionScheme(pki.EncryptionAlgorithm)
if err != nil {
return nil, err
}
encryptedKey := pki.EncryptedData
decryptedKey, err := cipher.Decrypt(password, cipherParams, encryptedKey)
if err != nil {
return nil, err
}
return decryptedKey, nil
}
// 解出 PEM 块
func DecryptPEMBlock(block *pem.Block, password []byte) ([]byte, error) {
if block.Headers["Proc-Type"] == "4,ENCRYPTED" {
return x509.DecryptPEMBlock(block, password)
}
// PKCS#8 header defined in RFC7468 section 11
if block.Type == "ENCRYPTED PRIVATE KEY" {
return DecryptPKCS8PrivateKey(block.Bytes, password)
}
return nil, errors.New("unsupported encrypted PEM")
}
func parseEncryptionScheme(encryptionScheme pkix.AlgorithmIdentifier) (Cipher, []byte, error) {
oid := encryptionScheme.Algorithm.String()
newCipher, err := GetCipher(oid)
if err != nil {
return nil, nil, fmt.Errorf("pkcs8: unsupported cipher (OID: %s)", oid)
}
params := encryptionScheme.Parameters.FullBytes
return newCipher, params, nil
}