-
Notifications
You must be signed in to change notification settings - Fork 1
/
PEM_decrypt.go
297 lines (276 loc) 路 6.45 KB
/
PEM_decrypt.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/md5"
"encoding/hex"
"encoding/pem"
"errors"
"io"
"strings"
"github.com/RyuaNerin/go-krypto/aria"
"github.com/emmansun/gmsm/sm4"
"github.com/pedroalbanese/anubis"
"github.com/pedroalbanese/camellia"
"github.com/pedroalbanese/cast5"
"github.com/pedroalbanese/go-idea"
"github.com/pedroalbanese/go-krcrypt"
"github.com/pedroalbanese/kuznechik"
)
type PEMCipher int
const (
_ PEMCipher = iota
PEMCipherDES
PEMCipher3DES
PEMCipherAES128
PEMCipherAES192
PEMCipherAES256
PEMCipherSM4
PEMCipherARIA128
PEMCipherARIA192
PEMCipherARIA256
PEMCipherCAMELLIA128
PEMCipherCAMELLIA192
PEMCipherCAMELLIA256
PEMCipherIDEA
PEMCipherSEED
PEMCipherGOST
PEMCipherCAST
PEMCipherANUBIS
)
type rfc1423Algo struct {
cipher PEMCipher
name string
cipherFunc func(key []byte) (cipher.Block, error)
keySize int
blockSize int
}
var rfc1423Algos = []rfc1423Algo{{
cipher: PEMCipherGOST,
name: "GRASSHOPPER-CBC",
cipherFunc: kuznechik.NewCipher,
keySize: 32,
blockSize: kuznechik.BlockSize,
}, {
cipher: PEMCipherDES,
name: "DES-CBC",
cipherFunc: des.NewCipher,
keySize: 8,
blockSize: des.BlockSize,
}, {
cipher: PEMCipher3DES,
name: "DES-EDE3-CBC",
cipherFunc: des.NewTripleDESCipher,
keySize: 24,
blockSize: des.BlockSize,
}, {
cipher: PEMCipherAES128,
name: "AES-128-CBC",
cipherFunc: aes.NewCipher,
keySize: 16,
blockSize: aes.BlockSize,
}, {
cipher: PEMCipherAES192,
name: "AES-192-CBC",
cipherFunc: aes.NewCipher,
keySize: 24,
blockSize: aes.BlockSize,
}, {
cipher: PEMCipherAES256,
name: "AES-256-CBC",
cipherFunc: aes.NewCipher,
keySize: 32,
blockSize: aes.BlockSize,
}, {
cipher: PEMCipherSM4,
name: "SM4-CBC",
cipherFunc: sm4.NewCipher,
keySize: 16,
blockSize: sm4.BlockSize,
}, {
cipher: PEMCipherARIA128,
name: "ARIA-128-CBC",
cipherFunc: aria.NewCipher,
keySize: 16,
blockSize: aria.BlockSize,
}, {
cipher: PEMCipherARIA192,
name: "ARIA-192-CBC",
cipherFunc: aria.NewCipher,
keySize: 24,
blockSize: aria.BlockSize,
}, {
cipher: PEMCipherARIA256,
name: "ARIA-256-CBC",
cipherFunc: aria.NewCipher,
keySize: 32,
blockSize: aria.BlockSize,
}, {
cipher: PEMCipherCAMELLIA128,
name: "CAMELLIA-128-CBC",
cipherFunc: camellia.NewCipher,
keySize: 16,
blockSize: camellia.BlockSize,
}, {
cipher: PEMCipherCAMELLIA192,
name: "CAMELLIA-192-CBC",
cipherFunc: camellia.NewCipher,
keySize: 24,
blockSize: camellia.BlockSize,
}, {
cipher: PEMCipherCAMELLIA256,
name: "CAMELLIA-256-CBC",
cipherFunc: camellia.NewCipher,
keySize: 32,
blockSize: camellia.BlockSize,
}, {
cipher: PEMCipherIDEA,
name: "IDEA-CBC",
cipherFunc: idea.NewCipher,
keySize: 16,
blockSize: 8,
}, {
cipher: PEMCipherSEED,
name: "SEED-CBC",
cipherFunc: krcrypt.NewSEED,
keySize: 16,
blockSize: 16,
}, {
cipher: PEMCipherSEED,
name: "SEED-CBC",
cipherFunc: krcrypt.NewSEED,
keySize: 16,
blockSize: 16,
}, {
cipher: PEMCipherCAST,
name: "CAST-CBC",
cipherFunc: cast5.NewCAST,
keySize: 16,
blockSize: 8,
}, {
cipher: PEMCipherANUBIS,
name: "ANUBIS-CBC",
cipherFunc: anubis.New,
keySize: 16,
blockSize: 16,
},
}
func (c rfc1423Algo) deriveKey(password, salt []byte) []byte {
hash := md5.New()
out := make([]byte, c.keySize)
var digest []byte
for i := 0; i < len(out); i += len(digest) {
hash.Reset()
hash.Write(digest)
hash.Write(password)
hash.Write(salt)
digest = hash.Sum(digest[:0])
copy(out[i:], digest)
}
return out
}
func IsEncryptedPEMBlock(b *pem.Block) bool {
_, ok := b.Headers["DEK-Info"]
return ok
}
var IncorrectPasswordError = errors.New("x509: decryption password incorrect")
func DecryptPEMBlock(b *pem.Block, password []byte) ([]byte, error) {
dek, ok := b.Headers["DEK-Info"]
if !ok {
return nil, errors.New("x509: no DEK-Info header in block")
}
idx := strings.Index(dek, ",")
if idx == -1 {
return nil, errors.New("x509: malformed DEK-Info header")
}
mode, hexIV := dek[:idx], dek[idx+1:]
ciph := cipherByName(mode)
if ciph == nil {
return nil, errors.New("x509: unknown encryption mode")
}
iv, err := hex.DecodeString(hexIV)
if err != nil {
return nil, err
}
if len(iv) != ciph.blockSize {
return nil, errors.New("x509: incorrect IV size")
}
key := ciph.deriveKey(password, iv[:8])
block, err := ciph.cipherFunc(key)
if err != nil {
return nil, err
}
if len(b.Bytes)%block.BlockSize() != 0 {
return nil, errors.New("x509: encrypted PEM data is not a multiple of the block size")
}
data := make([]byte, len(b.Bytes))
dec := cipher.NewCBCDecrypter(block, iv)
dec.CryptBlocks(data, b.Bytes)
dlen := len(data)
if dlen == 0 || dlen%ciph.blockSize != 0 {
return nil, errors.New("x509: invalid padding")
}
last := int(data[dlen-1])
if dlen < last {
return nil, IncorrectPasswordError
}
if last == 0 || last > ciph.blockSize {
return nil, IncorrectPasswordError
}
for _, val := range data[dlen-last:] {
if int(val) != last {
return nil, IncorrectPasswordError
}
}
return data[:dlen-last], nil
}
func EncryptPEMBlock(rand io.Reader, blockType string, data, password []byte, alg PEMCipher) (*pem.Block, error) {
ciph := cipherByKey(alg)
if ciph == nil {
return nil, errors.New("x509: unknown encryption mode")
}
iv := make([]byte, ciph.blockSize)
if _, err := io.ReadFull(rand, iv); err != nil {
return nil, errors.New("x509: cannot generate IV: " + err.Error())
}
key := ciph.deriveKey(password, iv[:8])
block, err := ciph.cipherFunc(key)
if err != nil {
return nil, err
}
enc := cipher.NewCBCEncrypter(block, iv)
pad := ciph.blockSize - len(data)%ciph.blockSize
encrypted := make([]byte, len(data), len(data)+pad)
copy(encrypted, data)
for i := 0; i < pad; i++ {
encrypted = append(encrypted, byte(pad))
}
enc.CryptBlocks(encrypted, encrypted)
return &pem.Block{
Type: blockType,
Headers: map[string]string{
"Proc-Type": "4,ENCRYPTED",
"DEK-Info": ciph.name + "," + hex.EncodeToString(iv),
},
Bytes: encrypted,
}, nil
}
func cipherByName(name string) *rfc1423Algo {
for i := range rfc1423Algos {
alg := &rfc1423Algos[i]
if alg.name == name {
return alg
}
}
return nil
}
func cipherByKey(key PEMCipher) *rfc1423Algo {
for i := range rfc1423Algos {
alg := &rfc1423Algos[i]
if alg.cipher == key {
return alg
}
}
return nil
}