You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
4.9 KiB
240 lines
4.9 KiB
2 years ago
|
package crypto
|
||
3 years ago
|
|
||
|
import (
|
||
|
"crypto/aes"
|
||
|
"crypto/cipher"
|
||
|
"crypto/des"
|
||
|
"crypto/hmac"
|
||
|
"crypto/sha1"
|
||
|
"crypto/sha256"
|
||
|
"encoding/asn1"
|
||
|
"errors"
|
||
|
|
||
|
"golang.org/x/crypto/pbkdf2"
|
||
|
)
|
||
|
|
||
|
var (
|
||
2 years ago
|
errPasswordIsEmpty = errors.New("password is empty")
|
||
|
errDecodeASN1Failed = errors.New("decode ASN1 data failed")
|
||
|
errEncryptedLength = errors.New("length of encrypted password less than block size")
|
||
3 years ago
|
)
|
||
|
|
||
|
type ASN1PBE interface {
|
||
|
Decrypt(globalSalt, masterPwd []byte) (key []byte, err error)
|
||
|
}
|
||
|
|
||
|
func NewASN1PBE(b []byte) (pbe ASN1PBE, err error) {
|
||
|
var (
|
||
3 years ago
|
n nssPBE
|
||
|
m metaPBE
|
||
|
l loginPBE
|
||
3 years ago
|
)
|
||
|
if _, err := asn1.Unmarshal(b, &n); err == nil {
|
||
|
return n, nil
|
||
|
}
|
||
|
if _, err := asn1.Unmarshal(b, &m); err == nil {
|
||
|
return m, nil
|
||
|
}
|
||
|
if _, err := asn1.Unmarshal(b, &l); err == nil {
|
||
|
return l, nil
|
||
|
}
|
||
|
return nil, errDecodeASN1Failed
|
||
|
}
|
||
|
|
||
3 years ago
|
// nssPBE Struct
|
||
2 years ago
|
//
|
||
|
// SEQUENCE (2 elem)
|
||
|
// OBJECT IDENTIFIER
|
||
|
// SEQUENCE (2 elem)
|
||
|
// OCTET STRING (20 byte)
|
||
|
// INTEGER 1
|
||
|
// OCTET STRING (16 byte)
|
||
3 years ago
|
type nssPBE struct {
|
||
|
AlgoAttr struct {
|
||
|
asn1.ObjectIdentifier
|
||
|
SaltAttr struct {
|
||
|
EntrySalt []byte
|
||
|
Len int
|
||
|
}
|
||
|
}
|
||
3 years ago
|
Encrypted []byte
|
||
|
}
|
||
|
|
||
3 years ago
|
func (n nssPBE) Decrypt(globalSalt, masterPwd []byte) (key []byte, err error) {
|
||
3 years ago
|
glmp := append(globalSalt, masterPwd...)
|
||
|
hp := sha1.Sum(glmp)
|
||
3 years ago
|
s := append(hp[:], n.entrySalt()...)
|
||
3 years ago
|
chp := sha1.Sum(s)
|
||
3 years ago
|
pes := paddingZero(n.entrySalt(), 20)
|
||
3 years ago
|
tk := hmac.New(sha1.New, chp[:])
|
||
|
tk.Write(pes)
|
||
3 years ago
|
pes = append(pes, n.entrySalt()...)
|
||
3 years ago
|
k1 := hmac.New(sha1.New, chp[:])
|
||
|
k1.Write(pes)
|
||
3 years ago
|
tkPlus := append(tk.Sum(nil), n.entrySalt()...)
|
||
3 years ago
|
k2 := hmac.New(sha1.New, chp[:])
|
||
|
k2.Write(tkPlus)
|
||
|
k := append(k1.Sum(nil), k2.Sum(nil)...)
|
||
|
iv := k[len(k)-8:]
|
||
2 years ago
|
return des3Decrypt(k[:24], iv, n.encrypted())
|
||
|
}
|
||
|
|
||
|
func (n nssPBE) entrySalt() []byte {
|
||
|
return n.AlgoAttr.SaltAttr.EntrySalt
|
||
|
}
|
||
|
|
||
|
func (n nssPBE) encrypted() []byte {
|
||
|
return n.Encrypted
|
||
3 years ago
|
}
|
||
|
|
||
|
// MetaPBE Struct
|
||
2 years ago
|
//
|
||
|
// SEQUENCE (2 elem)
|
||
|
// OBJECT IDENTIFIER
|
||
|
// SEQUENCE (2 elem)
|
||
|
// SEQUENCE (2 elem)
|
||
|
// OBJECT IDENTIFIER
|
||
|
// SEQUENCE (4 elem)
|
||
|
// OCTET STRING (32 byte)
|
||
|
// INTEGER 1
|
||
|
// INTEGER 32
|
||
|
// SEQUENCE (1 elem)
|
||
|
// OBJECT IDENTIFIER
|
||
|
// SEQUENCE (2 elem)
|
||
|
// OBJECT IDENTIFIER
|
||
|
// OCTET STRING (14 byte)
|
||
|
// OCTET STRING (16 byte)
|
||
3 years ago
|
type metaPBE struct {
|
||
|
AlgoAttr algoAttr
|
||
3 years ago
|
Encrypted []byte
|
||
|
}
|
||
|
|
||
3 years ago
|
type algoAttr struct {
|
||
|
asn1.ObjectIdentifier
|
||
|
Data struct {
|
||
|
Data struct {
|
||
|
asn1.ObjectIdentifier
|
||
|
SlatAttr slatAttr
|
||
|
}
|
||
|
IVData ivAttr
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
type ivAttr struct {
|
||
|
asn1.ObjectIdentifier
|
||
|
IV []byte
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
type slatAttr struct {
|
||
3 years ago
|
EntrySalt []byte
|
||
|
IterationCount int
|
||
|
KeySize int
|
||
3 years ago
|
Algorithm struct {
|
||
|
asn1.ObjectIdentifier
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func (m metaPBE) Decrypt(globalSalt, masterPwd []byte) (key2 []byte, err error) {
|
||
3 years ago
|
k := sha1.Sum(globalSalt)
|
||
3 years ago
|
key := pbkdf2.Key(k[:], m.entrySalt(), m.iterationCount(), m.keySize(), sha256.New)
|
||
2 years ago
|
iv := append([]byte{4, 14}, m.iv()...)
|
||
|
return aes128CBCDecrypt(key, iv, m.encrypted())
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func (m metaPBE) entrySalt() []byte {
|
||
|
return m.AlgoAttr.Data.Data.SlatAttr.EntrySalt
|
||
|
}
|
||
|
|
||
|
func (m metaPBE) iterationCount() int {
|
||
|
return m.AlgoAttr.Data.Data.SlatAttr.IterationCount
|
||
|
}
|
||
|
|
||
|
func (m metaPBE) keySize() int {
|
||
|
return m.AlgoAttr.Data.Data.SlatAttr.KeySize
|
||
|
}
|
||
|
|
||
2 years ago
|
func (m metaPBE) iv() []byte {
|
||
|
return m.AlgoAttr.Data.IVData.IV
|
||
|
}
|
||
|
|
||
|
func (m metaPBE) encrypted() []byte {
|
||
|
return m.Encrypted
|
||
|
}
|
||
|
|
||
3 years ago
|
// loginPBE Struct
|
||
2 years ago
|
//
|
||
|
// OCTET STRING (16 byte)
|
||
|
// SEQUENCE (2 elem)
|
||
|
// OBJECT IDENTIFIER
|
||
|
// OCTET STRING (8 byte)
|
||
|
// OCTET STRING (16 byte)
|
||
3 years ago
|
type loginPBE struct {
|
||
3 years ago
|
CipherText []byte
|
||
3 years ago
|
Data struct {
|
||
|
asn1.ObjectIdentifier
|
||
|
IV []byte
|
||
|
}
|
||
3 years ago
|
Encrypted []byte
|
||
|
}
|
||
|
|
||
3 years ago
|
func (l loginPBE) Decrypt(globalSalt, masterPwd []byte) (key []byte, err error) {
|
||
2 years ago
|
return des3Decrypt(globalSalt, l.iv(), l.encrypted())
|
||
|
}
|
||
|
|
||
|
func (l loginPBE) iv() []byte {
|
||
|
return l.Data.IV
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (l loginPBE) encrypted() []byte {
|
||
|
return l.Encrypted
|
||
3 years ago
|
}
|
||
|
|
||
|
func aes128CBCDecrypt(key, iv, encryptPass []byte) ([]byte, error) {
|
||
|
block, err := aes.NewCipher(key)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
encryptLen := len(encryptPass)
|
||
|
if encryptLen < block.BlockSize() {
|
||
|
return nil, errEncryptedLength
|
||
|
}
|
||
|
|
||
|
dst := make([]byte, encryptLen)
|
||
|
mode := cipher.NewCBCDecrypter(block, iv)
|
||
|
mode.CryptBlocks(dst, encryptPass)
|
||
3 years ago
|
dst = pkcs5UnPadding(dst, block.BlockSize())
|
||
3 years ago
|
return dst, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func pkcs5UnPadding(src []byte, blockSize int) []byte {
|
||
|
n := len(src)
|
||
|
paddingNum := int(src[n-1])
|
||
|
if n < paddingNum || paddingNum > blockSize {
|
||
|
return src
|
||
|
}
|
||
|
return src[:n-paddingNum]
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
// des3Decrypt use for decrypt firefox PBE
|
||
3 years ago
|
func des3Decrypt(key, iv []byte, src []byte) ([]byte, error) {
|
||
|
block, err := des.NewTripleDESCipher(key)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
blockMode := cipher.NewCBCDecrypter(block, iv)
|
||
|
sq := make([]byte, len(src))
|
||
|
blockMode.CryptBlocks(sq, src)
|
||
3 years ago
|
return pkcs5UnPadding(sq, block.BlockSize()), nil
|
||
3 years ago
|
}
|
||
|
|
||
|
func paddingZero(s []byte, l int) []byte {
|
||
|
h := l - len(s)
|
||
|
if h <= 0 {
|
||
|
return s
|
||
|
}
|
||
2 years ago
|
for i := len(s); i < l; i++ {
|
||
|
s = append(s, 0)
|
||
|
}
|
||
|
return s
|
||
3 years ago
|
}
|