Source File
pem_decrypt.go
Belonging Package
crypto/x509
package x509
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,
},
}
var IncorrectPasswordError = errors.New("x509: decryption password incorrect")
func ( *pem.Block, []byte) ([]byte, error) {
, := .Headers["DEK-Info"]
if ! {
return nil, errors.New("x509: no DEK-Info header in block")
}
:= strings.Index(, ",")
if == -1 {
return nil, errors.New("x509: malformed DEK-Info header")
}
, := [:], [+1:]
:= cipherByName()
if == nil {
return nil, errors.New("x509: unknown encryption mode")
}
, := hex.DecodeString()
if != nil {
return nil,
}
if len() != .blockSize {
return nil, errors.New("x509: incorrect IV size")
}
:= len()
if == 0 || %.blockSize != 0 {
return nil, errors.New("x509: invalid padding")
}
:= int([-1])
if < {
return nil, IncorrectPasswordError
}
if == 0 || > .blockSize {
return nil, IncorrectPasswordError
}
for , := range [-:] {
if int() != {
return nil, IncorrectPasswordError
}
}
return [:-], nil
}
:= .deriveKey(, [:8])
, := .cipherFunc()
if != nil {
return nil,
}
:= cipher.NewCBCEncrypter(, )
:= .blockSize - len()%.blockSize
for := 0; < ; ++ {
= append(, byte())
}
.CryptBlocks(, )
return &pem.Block{
Type: ,
Headers: map[string]string{
"Proc-Type": "4,ENCRYPTED",
"DEK-Info": .name + "," + hex.EncodeToString(),
},
Bytes: ,
}, nil
}
func ( string) *rfc1423Algo {
for := range rfc1423Algos {
:= &rfc1423Algos[]
if .name == {
return
}
}
return nil
}
func ( PEMCipher) *rfc1423Algo {
for := range rfc1423Algos {
:= &rfc1423Algos[]
if .cipher == {
return
}
}
return nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |