Skip to content

Commit 5879915

Browse files
committed
fixed jceks
1 parent c27d2a5 commit 5879915

File tree

14 files changed

+106
-119
lines changed

14 files changed

+106
-119
lines changed

jceks/bks_encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ func (this *BKS) Marshal(password string, opts ...BKSOpts) ([]byte, error) {
532532

533533
salt, err := genRandom(opt.SaltSize)
534534
if err != nil {
535-
return nil, errors.New("failed to generate salt")
535+
return nil, errors.New("go-cryptobin/jceks: failed to generate salt")
536536
}
537537

538538
err = writeBytes(buf, salt)

jceks/bks_entry.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"bytes"
77
"errors"
88
"crypto"
9-
"encoding/asn1"
109
)
1110

1211
type BksDataEntry interface {
@@ -219,15 +218,7 @@ func (this *bksSealedKeyEntry) Decrypt(password string) error {
219218
return errors.New("decrypt EOF")
220219
}
221220

222-
params, err := asn1.Marshal(pbeParam{
223-
Salt: salt,
224-
IterationCount: int(iterationCount),
225-
})
226-
if err != nil {
227-
return errors.New("decrypt marshal error")
228-
}
229-
230-
decrypted, err := CipherSHA1And3DESForBKS.Decrypt([]byte(password), params, encryptedBlob)
221+
decrypted, err := CipherSHA1And3DESForBKS.decrypt([]byte(password), salt, int(iterationCount), encryptedBlob)
231222
if err != nil {
232223
return errors.New("decrypt EOF")
233224
}
@@ -262,24 +253,19 @@ func (this *bksSealedKeyEntry) Encrypt() ([]byte, error) {
262253

263254
plaintext := bksBuf.Bytes()
264255

265-
encrypted, params, err := CipherSHA1And3DESForBKS.Encrypt([]byte(this.password), plaintext)
256+
encrypted, salt, iterationCount, err := CipherSHA1And3DESForBKS.encrypt([]byte(this.password), plaintext)
266257
if err != nil {
267258
return nil, err
268259
}
269260

270-
var param pbeParam
271-
if _, err := asn1.Unmarshal(params, &param); err != nil {
272-
return nil, err
273-
}
274-
275261
buf := bytes.NewBuffer(nil)
276262

277-
err = writeBytes(buf, param.Salt)
263+
err = writeBytes(buf, salt)
278264
if err != nil {
279265
return nil, err
280266
}
281267

282-
err = writeInt32(buf, int32(param.IterationCount))
268+
err = writeInt32(buf, int32(iterationCount))
283269
if err != nil {
284270
return nil, err
285271
}

jceks/cipher_blockcbc.go

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"encoding/asn1"
99
)
1010

11-
// pbe 数据
11+
// pbe parameters
1212
type pbeParam struct {
1313
Salt []byte
1414
IterationCount int
1515
}
1616

17-
// cbc 模式加密
17+
// Cipher Block CBC mode
1818
type CipherBlockCBC struct {
1919
// 对称加密
2020
cipherFunc func(key []byte) (cipher.Block, error)
@@ -34,7 +34,7 @@ type CipherBlockCBC struct {
3434
oid asn1.ObjectIdentifier
3535
}
3636

37-
// 值大小
37+
// Key Size
3838
func (this CipherBlockCBC) KeySize() int {
3939
return this.keySize
4040
}
@@ -44,85 +44,103 @@ func (this CipherBlockCBC) OID() asn1.ObjectIdentifier {
4444
return this.oid
4545
}
4646

47-
// 加密
47+
// with saltSize
48+
func (this CipherBlockCBC) WithSaltSize(saltSize int) CipherBlockCBC {
49+
this.saltSize = saltSize
50+
51+
return this
52+
}
53+
54+
// Encrypt data
4855
func (this CipherBlockCBC) Encrypt(password, plaintext []byte) ([]byte, []byte, error) {
49-
// 加密数据补码
56+
encrypted, salt, iterationCount, err := this.encrypt(password, plaintext)
57+
if err != nil {
58+
return nil, nil, err
59+
}
60+
61+
// Marshal pbe param
62+
paramBytes, err := asn1.Marshal(pbeParam{
63+
Salt: salt,
64+
IterationCount: iterationCount,
65+
})
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
70+
return encrypted, paramBytes, nil
71+
}
72+
73+
// Decrypt data
74+
func (this CipherBlockCBC) Decrypt(password, params, ciphertext []byte) ([]byte, error) {
75+
var param pbeParam
76+
if _, err := asn1.Unmarshal(params, &param); err != nil {
77+
return nil, errors.New("go-cryptobin/jceks: invalid PBE parameters")
78+
}
79+
80+
return this.decrypt(password, param.Salt, param.IterationCount, ciphertext)
81+
}
82+
83+
func (this CipherBlockCBC) encrypt(password, plaintext []byte) (encrypted, salt []byte, iterationCount int, err error) {
84+
// pkcs7 padding
5085
plaintext = pkcs7Padding(plaintext, this.blockSize)
5186

52-
salt, err := genRandom(this.saltSize)
87+
salt, err = genRandom(this.saltSize)
5388
if err != nil {
54-
return nil, nil, errors.New(err.Error() + " failed to generate salt")
89+
err = errors.New("go-cryptobin/jceks: failed to generate salt")
90+
return
5591
}
5692

5793
key, iv := this.derivedKeyFunc(string(password), string(salt), this.iterationCount, this.keySize, this.blockSize, this.hashFunc)
5894
if key == nil && iv == nil {
59-
return nil, nil, fmt.Errorf("unexpected salt length: %d", len(salt))
95+
err = fmt.Errorf("go-cryptobin/jceks: unexpected salt length: %d", len(salt))
96+
return
6097
}
6198

6299
block, err := this.cipherFunc(key)
63100
if err != nil {
64-
return nil, nil, errors.New("pkcs8:" + err.Error() + " failed to create cipher")
101+
err = fmt.Errorf("go-cryptobin/jceks: failed to create cipher: %s", err.Error())
102+
return
65103
}
66104

67105
// 需要保存的加密数据
68-
encrypted := make([]byte, len(plaintext))
106+
encrypted = make([]byte, len(plaintext))
69107

70108
enc := cipher.NewCBCEncrypter(block, iv)
71109
enc.CryptBlocks(encrypted, plaintext)
72110

73-
// 返回数据
74-
paramBytes, err := asn1.Marshal(pbeParam{
75-
Salt: salt,
76-
IterationCount: this.iterationCount,
77-
})
78-
if err != nil {
79-
return nil, nil, err
80-
}
111+
iterationCount = this.iterationCount
81112

82-
return encrypted, paramBytes, nil
113+
return
83114
}
84115

85-
// 解密
86-
func (this CipherBlockCBC) Decrypt(password, params, ciphertext []byte) ([]byte, error) {
87-
var param pbeParam
88-
if _, err := asn1.Unmarshal(params, &param); err != nil {
89-
return nil, errors.New("pkcs8: invalid PBES2 parameters")
90-
}
91-
92-
key, iv := this.derivedKeyFunc(string(password), string(param.Salt), param.IterationCount, this.keySize, this.blockSize, this.hashFunc)
116+
func (this CipherBlockCBC) decrypt(password, salt []byte, iterationCount int, ciphertext []byte) ([]byte, error) {
117+
key, iv := this.derivedKeyFunc(string(password), string(salt), iterationCount, this.keySize, this.blockSize, this.hashFunc)
93118
if key == nil && iv == nil {
94-
return nil, fmt.Errorf("unexpected salt length: %d", len(param.Salt))
119+
return nil, fmt.Errorf("go-cryptobin/jceks: unexpected salt length: %d", len(salt))
95120
}
96121

97122
block, err := this.cipherFunc(key)
98123
if err != nil {
99124
return nil, err
100125
}
101126

102-
// 判断数据是否为填充数据
127+
// check ciphertext length
103128
blockSize := block.BlockSize()
104129
dlen := len(ciphertext)
105130
if dlen == 0 || dlen%blockSize != 0 {
106-
return nil, errors.New("pkcs8: invalid padding")
131+
return nil, errors.New("go-cryptobin/jceks: invalid padding")
107132
}
108133

109134
plaintext := make([]byte, len(ciphertext))
110135

111136
mode := cipher.NewCBCDecrypter(block, iv)
112137
mode.CryptBlocks(plaintext, ciphertext)
113138

114-
// 解析加密数据
139+
// pkcs7 UnPadding
115140
plaintext, err = pkcs7UnPadding(plaintext)
116141
if err != nil {
117142
return nil, err
118143
}
119144

120145
return plaintext, nil
121146
}
122-
123-
// 设置 saltSize
124-
func (this CipherBlockCBC) WithSaltSize(saltSize int) CipherBlockCBC {
125-
this.saltSize = saltSize
126-
127-
return this
128-
}

jceks/cipher_setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var CipherSHA1And3DES = CipherBlockCBC{
4242
oid: oidPbeWithSHA1And3DES,
4343
}
4444

45-
// bks 使用
45+
// for BKS and UBER
4646
var CipherSHA1And3DESForBKS = CipherBlockCBC{
4747
cipherFunc: des.NewTripleDESCipher,
4848
hashFunc: sha1.New,

jceks/jks_encryptkey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func jksDecryptKey(encryptedPKI []byte, passwd []byte) ([]byte, error) {
4141
sha.Write(key)
4242

4343
if subtle.ConstantTimeCompare(check, sha.Sum(nil)) != 1 {
44-
return nil, errors.New("keystore was tampered with or password was incorrect")
44+
return nil, errors.New("go-cryptobin/jceks: keystore was tampered with or password was incorrect")
4545
}
4646

4747
return key, nil

jceks/key.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ParsePKCS8PrivateKey(pkData []byte) (privateKey crypto.PrivateKey, err erro
3232
}
3333
}
3434

35-
return nil, errors.New("jceks: error parsing PKCS#8 private key: " + err.Error())
35+
return nil, errors.New("go-cryptobin/jceks: error parsing PKCS#8 private key: " + err.Error())
3636
}
3737

3838
// 从注册的 key 列表编码证书
@@ -41,7 +41,7 @@ func MarshalPKCS8PrivateKey(privateKey crypto.PrivateKey) ([]byte, error) {
4141

4242
key, ok := keys[keytype]
4343
if !ok {
44-
return nil, errors.New("jceks: unsupported private key type " + keytype)
44+
return nil, errors.New("go-cryptobin/jceks: unsupported private key type " + keytype)
4545
}
4646

4747
return key().MarshalPKCS8PrivateKey(privateKey)
@@ -55,7 +55,7 @@ func ParsePKCS8PublicKey(pkData []byte) (publicKey crypto.PublicKey, err error)
5555
}
5656
}
5757

58-
return nil, errors.New("jceks: error parsing PKCS#8 public key: " + err.Error())
58+
return nil, errors.New("go-cryptobin/jceks: error parsing PKCS#8 public key: " + err.Error())
5959
}
6060

6161
// 从注册的 key 列表编码公钥证书
@@ -64,7 +64,7 @@ func MarshalPKCS8PublicKey(publicKey crypto.PublicKey) ([]byte, error) {
6464

6565
key, ok := keys[keytype]
6666
if !ok {
67-
return nil, errors.New("jceks: unsupported public key type " + keytype)
67+
return nil, errors.New("go-cryptobin/jceks: unsupported public key type " + keytype)
6868
}
6969

7070
return key().MarshalPKCS8PublicKey(publicKey)
@@ -76,7 +76,7 @@ func GetPKCS8PrivateKeyAlgorithm(privateKey crypto.PrivateKey) (string, error) {
7676

7777
key, ok := keys[keytype]
7878
if !ok {
79-
return "", errors.New("jceks: unsupported private key type " + keytype)
79+
return "", errors.New("go-cryptobin/jceks: unsupported private key type " + keytype)
8080
}
8181

8282
return key().Algorithm(), nil
@@ -88,7 +88,7 @@ func GetPKCS8PublicKeyAlgorithm(publicKey crypto.PublicKey) (string, error) {
8888

8989
key, ok := keys[keytype]
9090
if !ok {
91-
return "", errors.New("jceks: unsupported private key type " + keytype)
91+
return "", errors.New("go-cryptobin/jceks: unsupported private key type " + keytype)
9292
}
9393

9494
return key().Algorithm(), nil

jceks/key_dsa.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ type KeyDSA struct {}
1515
func (this KeyDSA) MarshalPKCS8PrivateKey(privateKey crypto.PrivateKey) ([]byte, error) {
1616
priKey, ok := privateKey.(*dsa.PrivateKey)
1717
if !ok {
18-
return nil, errors.New("jceks: private key is err")
18+
return nil, errors.New("go-cryptobin/jceks: private key is err")
1919
}
2020

2121
pkData, err := cryptobin_dsa.MarshalPKCS8PrivateKey(priKey)
2222
if err != nil {
23-
return nil, errors.New("jceks: error encoding PKCS#8 private key: " + err.Error())
23+
return nil, errors.New("go-cryptobin/jceks: error encoding PKCS#8 private key: " + err.Error())
2424
}
2525

2626
return pkData, nil
@@ -30,7 +30,7 @@ func (this KeyDSA) MarshalPKCS8PrivateKey(privateKey crypto.PrivateKey) ([]byte,
3030
func (this KeyDSA) ParsePKCS8PrivateKey(pkData []byte) (crypto.PrivateKey, error) {
3131
privateKey, err := cryptobin_dsa.ParsePKCS8PrivateKey(pkData)
3232
if err != nil {
33-
return nil, errors.New("jceks: error parsing PKCS#8 private key: " + err.Error())
33+
return nil, errors.New("go-cryptobin/jceks: error parsing PKCS#8 private key: " + err.Error())
3434
}
3535

3636
return privateKey, nil
@@ -42,12 +42,12 @@ func (this KeyDSA) ParsePKCS8PrivateKey(pkData []byte) (crypto.PrivateKey, error
4242
func (this KeyDSA) MarshalPKCS8PublicKey(publicKey crypto.PublicKey) ([]byte, error) {
4343
pubKey, ok := publicKey.(*dsa.PublicKey)
4444
if !ok {
45-
return nil, errors.New("jceks: public key is err")
45+
return nil, errors.New("go-cryptobin/jceks: public key is err")
4646
}
4747

4848
pkData, err := cryptobin_dsa.MarshalPKCS8PublicKey(pubKey)
4949
if err != nil {
50-
return nil, errors.New("jceks: error encoding PKCS#8 public key: " + err.Error())
50+
return nil, errors.New("go-cryptobin/jceks: error encoding PKCS#8 public key: " + err.Error())
5151
}
5252

5353
return pkData, nil
@@ -57,7 +57,7 @@ func (this KeyDSA) MarshalPKCS8PublicKey(publicKey crypto.PublicKey) ([]byte, er
5757
func (this KeyDSA) ParsePKCS8PublicKey(pkData []byte) (crypto.PublicKey, error) {
5858
publicKey, err := cryptobin_dsa.ParsePKCS8PublicKey(pkData)
5959
if err != nil {
60-
return nil, errors.New("jceks: error parsing PKCS#8 public key: " + err.Error())
60+
return nil, errors.New("go-cryptobin/jceks: error parsing PKCS#8 public key: " + err.Error())
6161
}
6262

6363
return publicKey, nil

jceks/key_ecdsa.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type KeyEcdsa struct {}
1313
func (this KeyEcdsa) MarshalPKCS8PrivateKey(privateKey crypto.PrivateKey) ([]byte, error) {
1414
pkData, err := x509.MarshalPKCS8PrivateKey(privateKey)
1515
if err != nil {
16-
return nil, errors.New("jceks: error encoding PKCS#8 private key: " + err.Error())
16+
return nil, errors.New("go-cryptobin/jceks: error encoding PKCS#8 private key: " + err.Error())
1717
}
1818

1919
return pkData, nil
@@ -23,7 +23,7 @@ func (this KeyEcdsa) MarshalPKCS8PrivateKey(privateKey crypto.PrivateKey) ([]byt
2323
func (this KeyEcdsa) ParsePKCS8PrivateKey(pkData []byte) (crypto.PrivateKey, error) {
2424
privateKey, err := x509.ParsePKCS8PrivateKey(pkData)
2525
if err != nil {
26-
return nil, errors.New("jceks: error parsing PKCS#8 private key: " + err.Error())
26+
return nil, errors.New("go-cryptobin/jceks: error parsing PKCS#8 private key: " + err.Error())
2727
}
2828

2929
return privateKey, nil
@@ -35,7 +35,7 @@ func (this KeyEcdsa) ParsePKCS8PrivateKey(pkData []byte) (crypto.PrivateKey, err
3535
func (this KeyEcdsa) MarshalPKCS8PublicKey(publicKey crypto.PublicKey) ([]byte, error) {
3636
pkData, err := x509.MarshalPKIXPublicKey(publicKey)
3737
if err != nil {
38-
return nil, errors.New("jceks: error encoding PKCS#8 public key: " + err.Error())
38+
return nil, errors.New("go-cryptobin/jceks: error encoding PKCS#8 public key: " + err.Error())
3939
}
4040

4141
return pkData, nil
@@ -45,7 +45,7 @@ func (this KeyEcdsa) MarshalPKCS8PublicKey(publicKey crypto.PublicKey) ([]byte,
4545
func (this KeyEcdsa) ParsePKCS8PublicKey(pkData []byte) (crypto.PublicKey, error) {
4646
publicKey, err := x509.ParsePKIXPublicKey(pkData)
4747
if err != nil {
48-
return nil, errors.New("jceks: error parsing PKCS#8 public key: " + err.Error())
48+
return nil, errors.New("go-cryptobin/jceks: error parsing PKCS#8 public key: " + err.Error())
4949
}
5050

5151
return publicKey, nil

0 commit comments

Comments
 (0)