/// PKCS #12 certificate to DER using openssl command.
PKCS #12 certificate to DER using openssl command.
PKCS #12 certificate to DER using openssl command .
pub(crate) fn pkcs12_to_der(pkcs12: &[u8], passphrase: &str) -> anyhow::Result<(Vec<u8>, Vec<u8>)> {
    let temp_dir = tempdir::TempDir::new("tls-api-der-to-pkcs12").unwrap();
    let cert_pem_file = temp_dir.path().join("cert.pem");
    let pkcs12_file = temp_dir.path().join("cert.pkcs12");
    fs::write(&pkcs12_file, pkcs12)?;
    let output = Command::new("openssl")
        .arg("pkcs12")
        .arg("-nodes")
        .arg("-in")
        .arg(&pkcs12_file)
        .arg("-out")
        .arg(&cert_pem_file)
        .arg("-password")
        .arg(format!("pass:{}", passphrase))
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .output()?;
    if !output.status.success() {
        return Err(crate::CommonError::OpensslCommandFailedToConvert.into());
    }
    let cert_pem = fs::read_to_string(cert_pem_file)?;
    let pems = pem::parse_many(cert_pem);
    let mut certificates: Vec<Vec<u8>> = pems
        .iter()
        .flat_map(|p| match p.tag.as_str() {
            "CERTIFICATE" => Some(p.contents.clone()),
            _ => None,
        })
        .collect();
    let mut keys: Vec<Vec<u8>> = pems
        .iter()
        .flat_map(|p| match p.tag.as_str() {
            "PRIVATE KEY" | "RSA PRIVATE KEY" => Some(p.contents.clone()),
            _ => None,
        })
        .collect();
    if keys.len() != 1 || certificates.len() != 1 {
        return Err(
            crate::CommonError::PemFromPkcs12ContainsNotSingleCertKeyPair(
                pems.iter().map(|p| p.tag.clone()).collect(),
            )
            .into(),
        );
    }
    Ok((certificates.swap_remove(0), keys.swap_remove(0)))
}
pub ( crate ) fn pkcs12_to_der ( pkcs12 : & [ u8 ] , passphrase : & str ) -> anyhow :: Result < ( Vec < u8 > , Vec < u8 > ) > { let temp_dir = tempdir :: TempDir :: new ( " " ) . unwrap ( ) ; let cert_pem_file = temp_dir . path ( ) . join ( " " ) ; let pkcs12_file = temp_dir . path ( ) . join ( " " ) ; fs :: write ( & pkcs12_file , pkcs12 ) ? ; let output = Command :: new ( " " ) . arg ( " " ) . arg ( " " ) . arg ( " " ) . arg ( & pkcs12_file ) . arg ( " " ) . arg ( & cert_pem_file ) . arg ( " " ) . arg ( format ! ( " " passphrase ) ) . stdin ( Stdio :: null ( ) ) . stdout ( Stdio :: null ( ) ) . stderr ( Stdio :: inherit ( ) ) . output ( ) ? ; if ! output . status . success ( ) { return Err ( crate :: CommonError :: OpensslCommandFailedToConvert . into ( ) ) ; } let cert_pem = fs :: read_to_string ( cert_pem_file ) ? ; let pems = pem :: parse_many ( cert_pem ) ; let mut certificates : Vec < Vec < u8 > > = pems . iter ( ) . flat_map ( | p | match p . tag . as_str ( ) { " " => Some ( p . contents . clone ( ) ) , _ => None , } ) . collect ( ) ; let mut keys : Vec < Vec < u8 > > = pems . iter ( ) . flat_map ( | p | match p . tag . as_str ( ) { " " | " " => Some ( p . contents . clone ( ) ) , _ => None , } ) . collect ( ) ; if keys . len ( ) != 1 || certificates . len ( ) != 1 { return Err ( crate :: CommonError :: PemFromPkcs12ContainsNotSingleCertKeyPair ( pems . iter ( ) . map ( | p | p . tag . clone ( ) ) . collect ( ) , ) . into ( ) , ) ; } Ok ( ( certificates . swap_remove ( 0 ) , keys . swap_remove ( 0 ) ) ) }
PKCS #12 certificate to DER using openssl command.
PKCS #12 certificate to DER using openssl command .
pkcs12
&[u8]
passphrase
&str