| |
| |
| |
|
|
| package x509 |
|
|
| |
| |
| |
|
|
| import ( |
| "crypto/aes" |
| "crypto/cipher" |
| "crypto/des" |
| "crypto/md5" |
| "encoding/hex" |
| "encoding/pem" |
| "errors" |
| "io" |
| "strings" |
| ) |
|
|
| type PEMCipher int |
|
|
| |
| const ( |
| _ PEMCipher = iota |
| PEMCipherDES |
| PEMCipher3DES |
| PEMCipherAES128 |
| PEMCipherAES192 |
| PEMCipherAES256 |
| ) |
|
|
| |
| type rfc1423Algo struct { |
| cipher PEMCipher |
| name string |
| cipherFunc func(key []byte) (cipher.Block, error) |
| keySize int |
| blockSize int |
| } |
|
|
| |
| |
| var rfc1423Algos = []rfc1423Algo{{ |
| 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, |
| }, |
| } |
|
|
| |
| |
| |
| 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") |
| } |
|
|
| mode, hexIV, ok := strings.Cut(dek, ",") |
| if !ok { |
| return nil, errors.New("x509: malformed DEK-Info header") |
| } |
|
|
| 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 |
| } |
|
|