pub fn pem_to_der(pem: &str, label: &str) -> io::Result<Vec<u8>> {
    fn invalid_data_err(msg: impl std::string::ToString) -> io::Error {
        io::Error::new(io::ErrorKind::InvalidData, msg.to_string())
    }

    let lines: Vec<_> = pem.trim_end().lines().collect();
    let n = lines.len();

    if n < 3 {
        return Err(invalid_data_err("input file is too short"));
    }

    let expect = format!("-----BEGIN {}-----", label);
    if !lines[0].starts_with(&expect) {
        return Err(invalid_data_err(format!(
            "PEM file doesn't start with 'BEGIN {}' block",
            label
        )));
    }
    let expect = format!("-----END {}-----", label);
    if !lines[n - 1].starts_with(&expect) {
        return Err(invalid_data_err(format!(
            "PEM file doesn't end with 'END {}' block",
            label
        )));
    }

    base64::decode(&lines[1..n - 1].join(""))
        .map_err(|err| invalid_data_err(format!("failed to decode base64: {}", err)))
}
/// Converts PEM encoded data to DER
Converts PEM encoded data to DER
Converts PEM encoded data to DER
pub fn pem_to_der(pem: &str, label: &str) -> io::Result<Vec<u8>> {
    fn invalid_data_err(msg: impl std::string::ToString) -> io::Error {
        io::Error::new(io::ErrorKind::InvalidData, msg.to_string())
    }
    let lines: Vec<_> = pem.trim_end().lines().collect();
    let n = lines.len();
    if n < 3 {
        return Err(invalid_data_err("input file is too short"));
    }
    let expect = format!("-----BEGIN {}-----", label);
    if !lines[0].starts_with(&expect) {
        return Err(invalid_data_err(format!(
            "PEM file doesn't start with 'BEGIN {}' block",
            label
        )));
    }
    let expect = format!("-----END {}-----", label);
    if !lines[n - 1].starts_with(&expect) {
        return Err(invalid_data_err(format!(
            "PEM file doesn't end with 'END {}' block",
            label
        )));
    }
    base64::decode(&lines[1..n - 1].join(""))
        .map_err(|err| invalid_data_err(format!("failed to decode base64: {}", err)))
}
pub fn pem_to_der ( pem : & str , label : & str ) -> io :: Result < Vec < u8 > > { fn invalid_data_err ( msg : impl std :: string :: ToString ) -> io :: Error { io :: Error :: new ( io :: ErrorKind :: InvalidData , msg . to_string ( ) ) } let lines : Vec < _ > = pem . trim_end ( ) . lines ( ) . collect ( ) ; let n = lines . len ( ) ; if n < 3 { return Err ( invalid_data_err ( " " ) ) ; } let expect = format ! ( " " label ) ; if ! lines [ 0 ] . starts_with ( & expect ) { return Err ( invalid_data_err ( format ! ( " " label ) ) ) ; } let expect = format ! ( " " label ) ; if ! lines [ n - 1 ] . starts_with ( & expect ) { return Err ( invalid_data_err ( format ! ( " " label ) ) ) ; } base64 :: decode ( & lines [ 1 .. n - 1 ] . join ( " " ) ) . map_err ( | err | invalid_data_err ( format ! ( " " err ) ) ) }
Converts PEM encoded data to DER
Converts PEM encoded data to DER
pem
&str
label
&str
pem
&str
label
&str