//Runs in self-signed mode, with options extract from CliArgs
Runs in self-signed mode, with options extract from CliArgs
Runs in self - signed mode with options extract from CliArgs
fn run_self_signed(fname: Option<PathBuf>, pod: Option<PemOrDer>) -> io::Result<()> {
    let mut buffer: Vec<u8> = Vec::new();
    let mut parser: PemOrDer = PemOrDer::Pem;
    //First we run all possible parameters options and at the end of this 'match'
    //block, we'll have the contents from the file into 'buffer' above and
    //whether it is a pem or der file in 'parser'.
    match fname {
        //No filename? No problem! we use stdin.
        None => {
            //the caveat is that the user must specify whether we're looking at PEM or DER
            // bytes.
            match pod {
                Some(tgt) => parser = tgt,
                None => {
                    return Err(io::Error::new(
                        io::ErrorKind::Other,
                        "Must specify '--type' option when reading from stdin",
                    ))
                }
            }
            io::stdin().read_to_end(&mut buffer)?;
        }
        //We were given a file, alright. We'll make sure it exits
        Some(fname) => {
            ensure_file_exists(&fname)?;
            //And if the user specified an extension, we make sure the file has the right
            // extension, otherwise we try to figure it out whether its a PEM or
            // DER publickey from the file extension.
            match pod {
                Some(tgt) => {
                    ensure_file_extension(&fname, &tgt.to_string())?;
                    parser = tgt;
                }
                None => {
                    if let Some(ext) = fname.extension() {
                        parser = PemOrDer::from_str(ext.to_str().unwrap()).map_err(|_e| {
                            io::Error::new(
                                io::ErrorKind::InvalidInput,
                                format!("Can't parse file extension ({}).", fname.display()),
                            )
                        })?;
                    }
                }
            }
            File::open(fname)?.read_to_end(&mut buffer)?;
        }
    };
    //Finally, we're ready to do the actual work: read in the public key,
    //export it as der then calculate the principal_id. I chose to read the key
    //even when the input is supposed to be in der format as an extra validaton
    // step.
    let pkey = match parser {
        PemOrDer::Der => pkey::PKey::public_key_from_der(&buffer)?,
        PemOrDer::Pem => pkey::PKey::public_key_from_pem(&buffer)?,
    };
    let pkey_der = pkey.public_key_to_der()?;
    println!("{:?}", PrincipalId::new_self_authenticating(&pkey_der));
    Ok(())
}
fn run_self_signed ( fname : Option < PathBuf > , pod : Option < PemOrDer > ) -> io :: Result < ( ) > { let mut buffer : Vec < u8 > = Vec :: new ( ) ; let mut parser : PemOrDer = PemOrDer :: Pem ; //First we run all possible parameters options and at the end of this 'match' //block, we'll have the contents from the file into 'buffer' above and //whether it is a pem or der file in 'parser'. match fname { //No filename? No problem! we use stdin. None => { //the caveat is that the user must specify whether we're looking at PEM or DER // bytes. match pod { Some ( tgt ) => parser = tgt , None => { return Err ( io :: Error :: new ( io :: ErrorKind :: Other , " " , ) ) } } io :: stdin ( ) . read_to_end ( & mut buffer ) ? ; } //We were given a file, alright. We'll make sure it exits Some ( fname ) => { ensure_file_exists ( & fname ) ? ; //And if the user specified an extension, we make sure the file has the right // extension, otherwise we try to figure it out whether its a PEM or // DER publickey from the file extension. match pod { Some ( tgt ) => { ensure_file_extension ( & fname , & tgt . to_string ( ) ) ? ; parser = tgt ; } None => { if let Some ( ext ) = fname . extension ( ) { parser = PemOrDer :: from_str ( ext . to_str ( ) . unwrap ( ) ) . map_err ( | _e | { io :: Error :: new ( io :: ErrorKind :: InvalidInput , format ! ( " " fname display ( ) ) , ) } ) ? ; } } } File :: open ( fname ) ? . read_to_end ( & mut buffer ) ? ; } } ; //Finally, we're ready to do the actual work: read in the public key, //export it as der then calculate the principal_id. I chose to read the key //even when the input is supposed to be in der format as an extra validaton // step. let pkey = match parser { PemOrDer :: Der => pkey :: PKey :: public_key_from_der ( & buffer ) ? , PemOrDer :: Pem => pkey :: PKey :: public_key_from_pem ( & buffer ) ? , } ; let pkey_der = pkey . public_key_to_der ( ) ? ; println ! ( " " PrincipalId new_self_authenticating ( pkey_der ) ) ; Ok ( ( ) ) }
Runs in self-signed mode, with options extract from CliArgs
Runs in self - signed mode with options extract from CliArgs
fname
Option<PathBuf>
pod
Option<PemOrDer>