-
Notifications
You must be signed in to change notification settings - Fork 21
/
pkcs8.go
132 lines (106 loc) · 3.2 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
package pkcs8pbe
import (
"io"
"fmt"
"errors"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
)
// 加密接口
type PEMCipher interface {
// oid
OID() asn1.ObjectIdentifier
// 值大小
KeySize() int
// 加密, 返回: [加密后数据, 参数, error]
Encrypt(key, plaintext []byte) ([]byte, []byte, error)
// 解密
Decrypt(key, params, ciphertext []byte) ([]byte, error)
}
var ciphers = make(map[string]func() PEMCipher)
// 添加加密
func AddCipher(oid asn1.ObjectIdentifier, cipher func() PEMCipher) {
ciphers[oid.String()] = cipher
}
// 结构体数据可以查看以下文档
// 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 PEMCipher,
) (*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) (PEMCipher, []byte, error) {
oid := encryptionScheme.Algorithm.String()
cipher, ok := ciphers[oid]
if !ok {
return nil, nil, fmt.Errorf("pkcs8: unsupported cipher (OID: %s)", oid)
}
newCipher := cipher()
params := encryptionScheme.Parameters.FullBytes
return newCipher, params, nil
}