Struct openssh_keys::PublicKey

source ·
pub struct PublicKey {
    pub options: Option<String>,
    pub data: Data,
    pub comment: Option<String>,
}
Expand description

PublicKey is the struct representation of an ssh public key.

Fields§

§options: Option<String>§data: Data§comment: Option<String>

Implementations§

source§

impl PublicKey

source

pub fn parse(key: &str) -> Result<Self>

parse takes a string and parses it as a public key from an authorized keys file. the format it expects is described here https://tools.ietf.org/html/rfc4253#section-6.6 and here https://man.openbsd.org/sshd#AUTHORIZED_KEYS_FILE_FORMAT

sshd describes an additional, optional “options” field for public keys in the authorized_keys file. This field allows for passing of options to sshd that only apply to that particular public key. This means that a public key in an authorized keys file is a strict superset of the public key format described in rfc4253. Another superset of a public key is what is present in the known_hosts file. This file has a hostname as the first thing on the line. This parser treats the hostname the same as an option field. When one of these things is found at the beginning of a line, it is treated as a semi-opaque string that is carried with the public key and reproduced when the key is printed. It is not entirely opaque, since the parser needs to be aware of quoting semantics within the option fields, since options surrounded by double quotes can contain spaces, which are otherwise the main delimiter of the parts of a public key.

You can parse and output ssh keys like this

let rsa_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCcMCOEryBa8IkxXacjIawaQPp08hR5h7+4vZePZ7DByTG3tqKgZYRJ86BaR+4fmdikFoQjvLJVUmwniq3wixhkP7VLCbqip3YHzxXrzxkbPC3w3O1Bdmifwn9cb8RcZXfXncCsSu+h5XCtQ5BOi41Iit3d13gIe/rfXVDURmRanV6R7Voljxdjmp/zyReuzc2/w5SI6Boi4tmcUlxAI7sFuP1kA3pABDhPtc3TDgAcPUIBoDCoY8q2egI197UuvbgsW2qraUcuQxbMvJOMSFg2FQrE2bpEqC4CtBn7+HiJrkVOHjV7bvSv7jd1SuX5XqkwMCRtdMuRpJr7CyZoFL5n demos@anduin";
let key = openssh_keys::PublicKey::parse(rsa_key).unwrap();
let out = key.to_string();
assert_eq!(rsa_key, out);

parse somewhat attempts to keep track of comments, but it doesn’t fully comply with the rfc in that regard.

source

pub fn read_keys<R>(r: R) -> Result<Vec<Self>>
where R: Read,

read_keys takes a reader and parses it as an authorized_keys file. it returns an error if it can’t read or parse any of the public keys in the list.

source

pub fn from_rsa(e: Vec<u8>, n: Vec<u8>) -> Self

get an ssh public key from rsa components

source

pub fn from_dsa(p: Vec<u8>, q: Vec<u8>, g: Vec<u8>, pkey: Vec<u8>) -> Self

get an ssh public key from dsa components

source

pub fn keytype(&self) -> &'static str

keytype returns the type of key in the format described by rfc4253 The output will be ssh-{type} where type is [rsa,ed25519,ecdsa,dsa]

source

pub fn data(&self) -> Vec<u8>

data returns the data section of the key in the format described by rfc4253 the contents of the data section depend on the keytype. For RSA keys it contains the keytype, exponent, and modulus in that order. Other types have other data sections. This function doesn’t base64 encode the data, that task is left to the consumer of the output.

source

pub fn set_comment(&mut self, comment: &str)

source

pub fn to_key_format(&self) -> String

to_key_format returns a string representation of the ssh key. this string output is appropriate to use as a public key file. it adheres to the format described in https://tools.ietf.org/html/rfc4253#section-6.6

an ssh key consists of four pieces:

[options] ssh-keytype data comment

the output of the data section is described in the documentation for the data function. the options section is optional, and is not part of the spec. rather, it is a field present in authorized_keys files or known_hosts files.

source

pub fn size(&self) -> usize

size returns the size of the stored ssh key. for rsa keys this is determined by the number of bits in the modulus. for dsa keys it’s the number of bits in the prime p.

see https://github.com/openssh/openssh-portable/blob/master/sshkey.c#L261 for more details

source

pub fn fingerprint(&self) -> String

fingerprint returns a string representing the fingerprint of the ssh key the format of the fingerprint is described tersely in https://tools.ietf.org/html/rfc4716#page-6. This uses the ssh-keygen defaults of a base64 encoded SHA256 hash.

source

pub fn to_fingerprint_string(&self) -> String

to_fingerprint_string prints out the fingerprint in the same format used by ssh-keygen -l -f key, specifically the implementation here - https://github.com/openssh/openssh-portable/blob/master/ssh-keygen.c#L842 right now it just sticks with the defaults of a base64 encoded SHA256 hash.

source

pub fn fingerprint_md5(&self) -> String

fingerprint_m5 returns a string representing the fingerprint of the ssh key the format of the fingerprint is MD5, and the output looks like, fb:a0:5b:a0:21:01:47:33:3b:8d:9e:14:1a:4c:db:6d .

source

pub fn to_fingerprint_md5_string(&self) -> String

to_fingerprint_m5_string prints out the fingerprint in the in hex format used by ssh-keygen -l -E md5 -f key, and the output looks like, 2048 MD5:fb:a0:5b:a0:21:01:47:33:3b:8d:9e:14:1a:4c:db:6d demos@anduin (RSA) .

Trait Implementations§

source§

impl Clone for PublicKey

source§

fn clone(&self) -> PublicKey

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PublicKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for PublicKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for PublicKey

§

type Err = OpenSSHKeyError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for PublicKey

Two public keys are equivalent if their data sections are equivalent, ignoring their comment section.

source§

fn eq(&self, other: &PublicKey) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for PublicKey

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.