pub(crate) fn der_to_pkcs12(cert: &[u8], key: &[u8]) -> anyhow::Result<(Vec<u8>, String)> {
    let temp_dir = tempdir::TempDir::new("tls-api-der-to-pkcs12").unwrap();

    let cert_file = temp_dir.path().join("cert.pem");
    let pkcs12_file = temp_dir.path().join("cert.pkcs12");

    let passphrase = "tls-api-123";

    let pem_data = pem::encode_many(&[
        pem::Pem {
            tag: "CERTIFICATE".to_owned(),
            contents: cert.to_owned(),
        },
        pem::Pem {
            // Technically it can be non-RSA PRIVATE KEY
            tag: "RSA PRIVATE KEY".to_owned(),
            contents: key.to_owned(),
        },
    ]);

    fs::write(&cert_file, pem_data)?;

    let output = Command::new("openssl")
        .arg("pkcs12")
        .arg("-export")
        .arg("-nodes")
        .arg("-in")
        .arg(&cert_file)
        .arg("-out")
        .arg(&pkcs12_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 pkcs12 = fs::read(pkcs12_file)?;
    Ok((pkcs12, passphrase.to_owned()))
}
/// Convert DER certificate to PKCS #12 using openssl command.
Convert DER certificate to PKCS #12 using openssl command.
Convert DER certificate to PKCS #12 using openssl command .
pub(crate) fn der_to_pkcs12(cert: &[u8], key: &[u8]) -> anyhow::Result<(Vec<u8>, String)> {
    let temp_dir = tempdir::TempDir::new("tls-api-der-to-pkcs12").unwrap();
    let cert_file = temp_dir.path().join("cert.pem");
    let pkcs12_file = temp_dir.path().join("cert.pkcs12");
    let passphrase = "tls-api-123";
    let pem_data = pem::encode_many(&[
        pem::Pem {
            tag: "CERTIFICATE".to_owned(),
            contents: cert.to_owned(),
        },
        pem::Pem {
            // Technically it can be non-RSA PRIVATE KEY
            tag: "RSA PRIVATE KEY".to_owned(),
            contents: key.to_owned(),
        },
    ]);
    fs::write(&cert_file, pem_data)?;
    let output = Command::new("openssl")
        .arg("pkcs12")
        .arg("-export")
        .arg("-nodes")
        .arg("-in")
        .arg(&cert_file)
        .arg("-out")
        .arg(&pkcs12_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 pkcs12 = fs::read(pkcs12_file)?;
    Ok((pkcs12, passphrase.to_owned()))
}
pub ( crate ) fn der_to_pkcs12 ( cert : & [ u8 ] , key : & [ u8 ] ) -> anyhow :: Result < ( Vec < u8 > , String ) > { let temp_dir = tempdir :: TempDir :: new ( " " ) . unwrap ( ) ; let cert_file = temp_dir . path ( ) . join ( " " ) ; let pkcs12_file = temp_dir . path ( ) . join ( " " ) ; let passphrase = " " ; let pem_data = pem :: encode_many ( & [ pem :: Pem { tag : " " . to_owned ( ) , contents : cert . to_owned ( ) , } , pem :: Pem { // Technically it can be non-RSA PRIVATE KEY tag : " " . to_owned ( ) , contents : key . to_owned ( ) , } , ] ) ; fs :: write ( & cert_file , pem_data ) ? ; let output = Command :: new ( " " ) . arg ( " " ) . arg ( " " ) . arg ( " " ) . arg ( " " ) . arg ( & cert_file ) . arg ( " " ) . arg ( & pkcs12_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 pkcs12 = fs :: read ( pkcs12_file ) ? ; Ok ( ( pkcs12 , passphrase . to_owned ( ) ) ) }
Convert DER certificate to PKCS #12 using openssl command.
Convert DER certificate to PKCS #12 using openssl command .
cert
&[u8]
key
&[u8]
cert
&[u8]
key
&[u8]