fn pem_decrypt(pemblock: &PemBlock, passphrase: Option<&[u8]>) -> OsshResult<Vec<u8>> {
    let mut encrypted = false;
    for entry in &pemblock.headers {
        if let HeaderEntry::ProcType(ver, proctype) = entry {
            if *proctype == ProcTypeType::ENCRYPTED && *ver == 4 {
                encrypted = true;
            } else {
                return Err(ErrorKind::UnsupportType.into());
            }
        }
    }
    if encrypted {
        let mut decrypted = None;
        for entry in &pemblock.headers {
            if let HeaderEntry::DEKInfo(algo, iv) = entry {
                if let Some(pass) = passphrase {
                    let ciph = match algo {
                        RFC1423Algorithm::DES_EDE3_CBC => Cipher::TDes_Cbc,
                        RFC1423Algorithm::AES_128_CBC => Cipher::Aes128_Cbc,
                        RFC1423Algorithm::AES_192_CBC => Cipher::Aes192_Cbc,
                        RFC1423Algorithm::AES_256_CBC => Cipher::Aes256_Cbc,
                        _ => return Err(ErrorKind::UnsupportCipher.into()),
                    };
                    let key = openssl_kdf(
                        pass,
                        iv.as_slice().try_into()?,
                        &mut md5::Md5::default(),
                        ciph.key_len(),
                        1,
                    )?;
                    decrypted = Some(ciph.decrypt(&pemblock.data, &key, iv)?);
                } else {
                    return Err(ErrorKind::IncorrectPass.into());
                }
                break;
            }
        }
        if let Some(data) = decrypted {
            Ok(data)
        } else {
            Err(ErrorKind::InvalidPemFormat.into())
        }
    } else {
        Ok(pemblock.data.clone())
    }
}
/// Self experimental implementation for decrypting OpenSSL PEM format
Self experimental implementation for decrypting OpenSSL PEM format
Self experimental implementation for decrypting OpenSSL PEM format
fn pem_decrypt(pemblock: &PemBlock, passphrase: Option<&[u8]>) -> OsshResult<Vec<u8>> {
    let mut encrypted = false;
    for entry in &pemblock.headers {
        if let HeaderEntry::ProcType(ver, proctype) = entry {
            if *proctype == ProcTypeType::ENCRYPTED && *ver == 4 {
                encrypted = true;
            } else {
                return Err(ErrorKind::UnsupportType.into());
            }
        }
    }
    if encrypted {
        let mut decrypted = None;
        for entry in &pemblock.headers {
            if let HeaderEntry::DEKInfo(algo, iv) = entry {
                if let Some(pass) = passphrase {
                    let ciph = match algo {
                        RFC1423Algorithm::DES_EDE3_CBC => Cipher::TDes_Cbc,
                        RFC1423Algorithm::AES_128_CBC => Cipher::Aes128_Cbc,
                        RFC1423Algorithm::AES_192_CBC => Cipher::Aes192_Cbc,
                        RFC1423Algorithm::AES_256_CBC => Cipher::Aes256_Cbc,
                        _ => return Err(ErrorKind::UnsupportCipher.into()),
                    };
                    let key = openssl_kdf(
                        pass,
                        iv.as_slice().try_into()?,
                        &mut md5::Md5::default(),
                        ciph.key_len(),
                        1,
                    )?;
                    decrypted = Some(ciph.decrypt(&pemblock.data, &key, iv)?);
                } else {
                    return Err(ErrorKind::IncorrectPass.into());
                }
                break;
            }
        }
        if let Some(data) = decrypted {
            Ok(data)
        } else {
            Err(ErrorKind::InvalidPemFormat.into())
        }
    } else {
        Ok(pemblock.data.clone())
    }
}
fn pem_decrypt ( pemblock : & PemBlock , passphrase : Option < & [ u8 ] > ) -> OsshResult < Vec < u8 > > { let mut encrypted = false ; for entry in & pemblock . headers { if let HeaderEntry :: ProcType ( ver , proctype ) = entry { if * proctype == ProcTypeType :: ENCRYPTED && * ver == 4 { encrypted = true ; } else { return Err ( ErrorKind :: UnsupportType . into ( ) ) ; } } } if encrypted { let mut decrypted = None ; for entry in & pemblock . headers { if let HeaderEntry :: DEKInfo ( algo , iv ) = entry { if let Some ( pass ) = passphrase { let ciph = match algo { RFC1423Algorithm :: DES_EDE3_CBC => Cipher :: TDes_Cbc , RFC1423Algorithm :: AES_128_CBC => Cipher :: Aes128_Cbc , RFC1423Algorithm :: AES_192_CBC => Cipher :: Aes192_Cbc , RFC1423Algorithm :: AES_256_CBC => Cipher :: Aes256_Cbc , _ => return Err ( ErrorKind :: UnsupportCipher . into ( ) ) , } ; let key = openssl_kdf ( pass , iv . as_slice ( ) . try_into ( ) ? , & mut md5 :: Md5 :: default ( ) , ciph . key_len ( ) , 1 , ) ? ; decrypted = Some ( ciph . decrypt ( & pemblock . data , & key , iv ) ? ) ; } else { return Err ( ErrorKind :: IncorrectPass . into ( ) ) ; } break ; } } if let Some ( data ) = decrypted { Ok ( data ) } else { Err ( ErrorKind :: InvalidPemFormat . into ( ) ) } } else { Ok ( pemblock . data . clone ( ) ) } }
Self experimental implementation for decrypting OpenSSL PEM format
Self experimental implementation for decrypting OpenSSL PEM format
pemblock
&PemBlock
passphrase
Option<&[u8]>
pemblock
&PemBlock
passphrase
Option<&[u8]>