diff --git a/Cargo.toml b/Cargo.toml index 36afc3e..09b40eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sshcerts" -version = "0.9.0" +version = "0.9.1" authors = ["Mitchell Grenier "] edition = "2018" license-file = "LICENSE" diff --git a/src/ssh/cert.rs b/src/ssh/cert.rs index f497af4..995a111 100644 --- a/src/ssh/cert.rs +++ b/src/ssh/cert.rs @@ -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)?; @@ -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()? { @@ -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 { @@ -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); } }; @@ -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) => { @@ -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()) }, } diff --git a/src/ssh/privkey.rs b/src/ssh/privkey.rs index c5b7de8..9ceea7e 100644 --- a/src/ssh/privkey.rs +++ b/src/ssh/privkey.rs @@ -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(), ] @@ -168,8 +168,8 @@ fn read_private_key(reader: &mut Reader<'_>) -> Result { coefficient, p, q, - exp: exp, - exq: exq, + exp, + exq, } ), PublicKey { @@ -230,7 +230,7 @@ fn read_private_key(reader: &mut Reader<'_>) -> Result { key_type: kt, kind, pubkey, - comment: if comment.len() == 0 {None} else {Some(comment)}, + comment: if comment.is_empty() {None} else {Some(comment)}, }) } @@ -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) { diff --git a/src/ssh/pubkey.rs b/src/ssh/pubkey.rs index a6eecd5..a0c0f8c 100644 --- a/src/ssh/pubkey.rs +++ b/src/ssh/pubkey.rs @@ -143,9 +143,9 @@ impl Fingerprint { /// ``` pub fn compute>(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); @@ -194,18 +194,10 @@ impl PublicKey { pub fn from_string(contents: &str) -> Result { 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); @@ -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, @@ -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 { - let kt = KeyType::from_name(&kt_name)?; + let kt = KeyType::from_name(kt_name)?; let kind = match kt.kind { KeyTypeKind::Rsa | KeyTypeKind::RsaCert => { @@ -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) => { diff --git a/src/ssh/signer.rs b/src/ssh/signer.rs index bb1798c..7f020c7 100644 --- a/src/ssh/signer.rs +++ b/src/ssh/signer.rs @@ -30,11 +30,11 @@ pub fn create_signer(privkey: PrivateKey) -> Box Option /// This is in this file to prevent a circular dependency between PrivateKey /// and the signer module. -impl Into Option> + Send + Sync>> for PrivateKey { - fn into(self) -> Box Option> + Send + Sync> { +impl From for Box Option> + Send + Sync> { + fn from(priv_key: PrivateKey) -> Box Option> + Send + Sync> { Box::new(move |buf: &[u8]| { - ssh_cert_signer(buf, &self) - }) + ssh_cert_signer(buf, &priv_key) + }) } } @@ -59,9 +59,7 @@ pub fn ssh_cert_signer(buf: &[u8], privkey: &PrivateKey) -> Option> { 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) }, @@ -80,12 +78,12 @@ pub fn ssh_cert_signer(buf: &[u8], privkey: &PrivateKey) -> Option> { }; 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, } @@ -101,7 +99,7 @@ pub fn ssh_cert_signer(buf: &[u8], privkey: &PrivateKey) -> Option> { Err(_) => return None, }; - Some(key_pair.sign(&buf).as_ref().to_vec()) + Some(key_pair.sign(buf).as_ref().to_vec()) }, } } \ No newline at end of file diff --git a/src/ssh/writer.rs b/src/ssh/writer.rs index 8a26426..c214af4 100644 --- a/src/ssh/writer.rs +++ b/src/ssh/writer.rs @@ -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. diff --git a/src/utils.rs b/src/utils.rs index edb2e47..c681fc5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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> { - 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, }; diff --git a/src/x509/mod.rs b/src/x509/mod.rs index 58d1558..dcc22fc 100644 --- a/src/x509/mod.rs +++ b/src/x509/mod.rs @@ -83,7 +83,7 @@ pub fn der_encoding_to_ssh_public_key(key: &[u8]) -> Result { /// 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 { - 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) }; @@ -94,7 +94,7 @@ pub fn extract_ssh_pubkey_from_x509_certificate(cert: &[u8]) -> Result Result { - 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) }; diff --git a/src/yubikey/mod.rs b/src/yubikey/mod.rs index 149a3c8..c1e8343 100644 --- a/src/yubikey/mod.rs +++ b/src/yubikey/mod.rs @@ -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), } } } diff --git a/src/yubikey/piv/management.rs b/src/yubikey/piv/management.rs index fc1b34d..9eee196 100644 --- a/src/yubikey/piv/management.rs +++ b/src/yubikey/piv/management.rs @@ -133,14 +133,14 @@ impl super::Yubikey { /// Generate CSR for slot pub fn generate_csr(&mut self, slot: &SlotId, common_name: &str,) -> Result> { 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(); diff --git a/src/yubikey/piv/mod.rs b/src/yubikey/piv/mod.rs index caf0d51..9d8e384 100644 --- a/src/yubikey/piv/mod.rs +++ b/src/yubikey/piv/mod.rs @@ -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()) } } diff --git a/src/yubikey/piv/ssh.rs b/src/yubikey/piv/ssh.rs index 5b183a3..73c52ae 100644 --- a/src/yubikey/piv/ssh.rs +++ b/src/yubikey/piv/ssh.rs @@ -48,7 +48,7 @@ impl super::Yubikey { _ => return Err(Error::Unsupported), }; - self.sign_data(&buf, alg, slot) + self.sign_data(buf, alg, slot) } } \ No newline at end of file