Skip to content

Commit

Permalink
Fix all clippy lints (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
obelisk authored Jan 26, 2022
1 parent 078ee55 commit e12ae50
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sshcerts"
version = "0.9.0"
version = "0.9.1"
authors = ["Mitchell Grenier <[email protected]>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
24 changes: 11 additions & 13 deletions src/ssh/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,15 @@ impl Certificate {
let mut iter = s.split_whitespace();

let kt_name = iter
.next()
.ok_or_else(|| Error::InvalidFormat)?;
.next().ok_or(Error::InvalidFormat)?;

let key_type = KeyType::from_name(&kt_name)?;
let key_type = KeyType::from_name(kt_name)?;
if !key_type.is_cert {
return Err(Error::NotCertificate);
}

let data = iter
.next()
.ok_or_else(|| Error::InvalidFormat)?;
.next().ok_or(Error::InvalidFormat)?;

let comment = iter.next().map(String::from);
let decoded = base64::decode(&data)?;
Expand All @@ -228,7 +226,7 @@ impl Certificate {
}

let nonce = reader.read_bytes()?;
let key = PublicKey::from_reader(&kt_name, &mut reader)?;
let key = PublicKey::from_reader(kt_name, &mut reader)?;
let serial = reader.read_u64()?;

let cert_type = match reader.read_u32()? {
Expand Down Expand Up @@ -464,7 +462,7 @@ impl Certificate {

match &self.signature_key.kind {
PublicKeyKind::Ecdsa(_) => {
writer.write_string(&self.signature_key.key_type.name);
writer.write_string(self.signature_key.key_type.name);
if let Some(signature) = crate::utils::signature_convert_asn1_ecdsa_to_ssh(signature) {
writer.write_bytes(&signature);
} else {
Expand All @@ -473,11 +471,11 @@ impl Certificate {
},
PublicKeyKind::Rsa(_) => {
writer.write_string("rsa-sha2-512");
writer.write_bytes(&signature);
writer.write_bytes(signature);
},
_ => {
writer.write_string(&self.signature_key.key_type.name);
writer.write_bytes(&signature);
writer.write_string(self.signature_key.key_type.name);
writer.write_bytes(signature);
}
};

Expand Down Expand Up @@ -633,7 +631,7 @@ fn verify_signature(signature_buf: &[u8], signed_bytes: &[u8], public_key: &Publ
let mut sig = r;
sig.extend(s);

UnparsedPublicKey::new(alg, &key.key).verify(&signed_bytes, &sig)?;
UnparsedPublicKey::new(alg, &key.key).verify(signed_bytes, &sig)?;
Ok(signature_buf.to_vec())
},
PublicKeyKind::Rsa(key) => {
Expand All @@ -645,14 +643,14 @@ fn verify_signature(signature_buf: &[u8], signed_bytes: &[u8], public_key: &Publ
};
let signature = reader.read_bytes()?;
let public_key = RsaPublicKeyComponents { n: &key.n, e: &key.e };
public_key.verify(alg, &signed_bytes, &signature)?;
public_key.verify(alg, signed_bytes, &signature)?;
Ok(signature_buf.to_vec())
},
PublicKeyKind::Ed25519(key) => {
let alg = &ED25519;
let signature = reader.read_bytes()?;
let peer_public_key = UnparsedPublicKey::new(alg, &key.key);
peer_public_key.verify(&signed_bytes, &signature)?;
peer_public_key.verify(signed_bytes, &signature)?;
Ok(signature_buf.to_vec())
},
}
Expand Down
16 changes: 8 additions & 8 deletions src/ssh/privkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ impl ToASN1 for RsaPrivateKey {
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, &self.d))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, &self.p))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, &self.q))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, &self.exp.as_ref().unwrap()))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, &self.exq.as_ref().unwrap()))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, self.exp.as_ref().unwrap()))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, self.exq.as_ref().unwrap()))],
vec![ASN1Block::Integer(0, BigInt::from_bytes_be(Sign::Plus, &self.coefficient))],
Vec::new(),
]
Expand Down Expand Up @@ -168,8 +168,8 @@ fn read_private_key(reader: &mut Reader<'_>) -> Result<PrivateKey> {
coefficient,
p,
q,
exp: exp,
exq: exq,
exp,
exq,
}
),
PublicKey {
Expand Down Expand Up @@ -230,7 +230,7 @@ fn read_private_key(reader: &mut Reader<'_>) -> Result<PrivateKey> {
key_type: kt,
kind,
pubkey,
comment: if comment.len() == 0 {None} else {Some(comment)},
comment: if comment.is_empty() {None} else {Some(comment)},
})
}

Expand Down Expand Up @@ -330,13 +330,13 @@ impl PrivateKey {
let salt = enc_reader.read_bytes()?;
let rounds = enc_reader.read_u32()?;
let mut output = [0; 48];
if let Err(_) = bcrypt_pbkdf(passphrase.as_str(), &salt, rounds, &mut output) {
if bcrypt_pbkdf(passphrase.as_str(), &salt, rounds, &mut output).is_err() {
return Err(Error::InvalidFormat);
}

let mut cipher = Aes256Ctr::new(
&GenericArray::from_slice(&output[..32]),
&GenericArray::from_slice(&output[32..]),
GenericArray::from_slice(&output[..32]),
GenericArray::from_slice(&output[32..]),
);

match cipher.try_apply_keystream(&mut remaining_bytes) {
Expand Down
26 changes: 9 additions & 17 deletions src/ssh/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ impl Fingerprint {
/// ```
pub fn compute<T: ?Sized + AsRef<[u8]>>(kind: FingerprintKind, data: &T) -> Fingerprint {
let digest = match kind {
FingerprintKind::Sha256 => digest::digest(&digest::SHA256, &data.as_ref()).as_ref().to_vec(),
FingerprintKind::Sha384 => digest::digest(&digest::SHA384, &data.as_ref()).as_ref().to_vec(),
FingerprintKind::Sha512 => digest::digest(&digest::SHA512, &data.as_ref()).as_ref().to_vec(),
FingerprintKind::Sha256 => digest::digest(&digest::SHA256, data.as_ref()).as_ref().to_vec(),
FingerprintKind::Sha384 => digest::digest(&digest::SHA384, data.as_ref()).as_ref().to_vec(),
FingerprintKind::Sha512 => digest::digest(&digest::SHA512, data.as_ref()).as_ref().to_vec(),
};

let mut encoded = base64::encode(&digest);
Expand Down Expand Up @@ -194,18 +194,10 @@ impl PublicKey {
pub fn from_string(contents: &str) -> Result<PublicKey> {
let mut iter = contents.split_whitespace();

let kt_name = iter
.next()
.ok_or_else(|| Error::InvalidFormat)?;

let data = iter
.next()
.ok_or_else(|| Error::InvalidFormat)?;

let kt_name = iter.next().ok_or(Error::InvalidFormat)?;
let data = iter.next().ok_or(Error::InvalidFormat)?;
let comment = iter.next().map(String::from);

let key_type = KeyType::from_name(&kt_name)?;

let key_type = KeyType::from_name(kt_name)?;
let decoded = base64::decode(&data)?;
let mut reader = Reader::new(&decoded);

Expand All @@ -216,7 +208,7 @@ impl PublicKey {
}

// Construct a new `PublicKey` value and preserve the `comment` value.
let k = PublicKey::from_reader(&kt_name, &mut reader)?;
let k = PublicKey::from_reader(kt_name, &mut reader)?;
let key = PublicKey {
key_type,
kind: k.kind,
Expand Down Expand Up @@ -256,7 +248,7 @@ impl PublicKey {
/// we already have a reader for reading an OpenSSH certificate key and
/// we want to extract the public key information from it.
pub(crate) fn from_reader(kt_name: &str, reader: &mut Reader<'_>) -> Result<PublicKey> {
let kt = KeyType::from_name(&kt_name)?;
let kt = KeyType::from_name(kt_name)?;

let kind = match kt.kind {
KeyTypeKind::Rsa | KeyTypeKind::RsaCert => {
Expand Down Expand Up @@ -339,7 +331,7 @@ impl PublicKey {
w.write_mpint(&k.n);
}
PublicKeyKind::Ecdsa(ref k) => {
w.write_string(&k.curve.identifier);
w.write_string(k.curve.identifier);
w.write_bytes(&k.key);
}
PublicKeyKind::Ed25519(ref k) => {
Expand Down
18 changes: 8 additions & 10 deletions src/ssh/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub fn create_signer(privkey: PrivateKey) -> Box<dyn Fn(&[u8]) -> Option<Vec<u8>

/// This is in this file to prevent a circular dependency between PrivateKey
/// and the signer module.
impl Into<Box<dyn Fn(&[u8]) -> Option<Vec<u8>> + Send + Sync>> for PrivateKey {
fn into(self) -> Box<dyn Fn(&[u8]) -> Option<Vec<u8>> + Send + Sync> {
impl From<PrivateKey> for Box<dyn Fn(&[u8]) -> Option<Vec<u8>> + Send + Sync> {
fn from(priv_key: PrivateKey) -> Box<dyn Fn(&[u8]) -> Option<Vec<u8>> + Send + Sync> {
Box::new(move |buf: &[u8]| {
ssh_cert_signer(buf, &self)
})
ssh_cert_signer(buf, &priv_key)
})
}
}

Expand All @@ -59,9 +59,7 @@ pub fn ssh_cert_signer(buf: &[u8], privkey: &PrivateKey) -> Option<Vec<u8>> {
let rng = rand::SystemRandom::new();
let mut signature = vec![0; keypair.public_modulus_len()];

if let Err(_) = keypair.sign(&signature::RSA_PKCS1_SHA512, &rng, buf, &mut signature) {
return None
}
keypair.sign(&signature::RSA_PKCS1_SHA512, &rng, buf, &mut signature).ok()?;

Some(signature)
},
Expand All @@ -80,12 +78,12 @@ pub fn ssh_cert_signer(buf: &[u8], privkey: &PrivateKey) -> Option<Vec<u8>> {
};

let key = if key.key[0] == 0x0_u8 {&key.key[1..]} else {&key.key};
let key_pair = match signature::EcdsaKeyPair::from_private_key_and_public_key(alg, &key, &pubkey) {
let key_pair = match signature::EcdsaKeyPair::from_private_key_and_public_key(alg, key, pubkey) {
Ok(kp) => kp,
Err(_) => return None,
};

match key_pair.sign(&rng, &buf) {
match key_pair.sign(&rng, buf) {
Ok(sig) => Some(sig.as_ref().to_vec()),
Err(_) => None,
}
Expand All @@ -101,7 +99,7 @@ pub fn ssh_cert_signer(buf: &[u8], privkey: &PrivateKey) -> Option<Vec<u8>> {
Err(_) => return None,
};

Some(key_pair.sign(&buf).as_ref().to_vec())
Some(key_pair.sign(buf).as_ref().to_vec())
},
}
}
2 changes: 1 addition & 1 deletion src/ssh/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Writer {
let size = val.len() as u32;
let mut buf = size.to_be_bytes().to_vec();
self.inner.append(&mut buf);
self.inner.extend_from_slice(&val);
self.inner.extend_from_slice(val);
}

/// Writes a `string` value to the underlying byte sequence.
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn asn_der_to_r_s(buf: &[u8]) -> Option<(&[u8], &[u8])> {
/// This function will take an ASN1 encoded ECDSA signature and return
/// an SSH Signature blob
pub fn signature_convert_asn1_ecdsa_to_ssh(signature: &[u8]) -> Option<Vec<u8>> {
let (r,s) = match asn_der_to_r_s(&signature) {
let (r,s) = match asn_der_to_r_s(signature) {
Some((r,s)) => (r, s),
None => return None,
};
Expand Down
4 changes: 2 additions & 2 deletions src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn der_encoding_to_ssh_public_key(key: &[u8]) -> Result<PublicKey, Error> {
/// This function is used to extract an SSH public key from an x509
/// certificate
pub fn extract_ssh_pubkey_from_x509_certificate(cert: &[u8]) -> Result<PublicKey, Error> {
let parsed_cert = match x509_parser::parse_x509_certificate(&cert) {
let parsed_cert = match x509_parser::parse_x509_certificate(cert) {
Ok((_, c)) => c,
Err(_) => return Err(Error::ParsingError)
};
Expand All @@ -94,7 +94,7 @@ pub fn extract_ssh_pubkey_from_x509_certificate(cert: &[u8]) -> Result<PublicKey
/// This function is used to extract an SSH public key from an x509
/// certificate signing request
pub fn extract_ssh_pubkey_from_x509_csr(csr: &[u8]) -> Result<PublicKey, Error> {
let parsed_csr = match x509_parser::certification_request::X509CertificationRequest::from_der(&csr) {
let parsed_csr = match x509_parser::certification_request::X509CertificationRequest::from_der(csr) {
Ok((_, csr)) => csr,
Err(_) => return Err(Error::ParsingError)
};
Expand Down
2 changes: 1 addition & 1 deletion src/yubikey/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Error::PivError(ref e) => write!(f, "{}", e.to_string()),
Error::PivError(ref e) => write!(f, "{}", e),
}
}
}
4 changes: 2 additions & 2 deletions src/yubikey/piv/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ impl super::Yubikey {
/// Generate CSR for slot
pub fn generate_csr(&mut self, slot: &SlotId, common_name: &str,) -> Result<Vec<u8>> {
let mut params = rcgen::CertificateParams::new(vec![]);
params.alg = match self.configured(&slot)? {
params.alg = match self.configured(slot)? {
PublicKeyInfo::EcP256(_) => &rcgen::PKCS_ECDSA_P256_SHA256,
PublicKeyInfo::EcP384(_) => &rcgen::PKCS_ECDSA_P384_SHA384,
_ => return Err(Error::Unsupported),
};
params.distinguished_name.push(rcgen::DnType::CommonName, common_name.to_string());

let csr_signer = CSRSigner::new(self.yk.serial().into(), slot.clone());
let csr_signer = CSRSigner::new(self.yk.serial().into(), *slot);
params.key_pair = Some(rcgen::KeyPair::from_remote(Box::new(csr_signer)).unwrap());

let csr = rcgen::Certificate::from_params(params).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/yubikey/piv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Yubikey {

impl std::fmt::Debug for Yubikey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "YubiKey: {}", self.yk.serial().to_string())
writeln!(f, "YubiKey: {}", self.yk.serial())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/yubikey/piv/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl super::Yubikey {
_ => return Err(Error::Unsupported),
};

self.sign_data(&buf, alg, slot)
self.sign_data(buf, alg, slot)
}

}

0 comments on commit e12ae50

Please sign in to comment.