From 784978bad0ff81173020599ed7b46acee18afd58 Mon Sep 17 00:00:00 2001 From: Atul Bhosale Date: Thu, 14 Nov 2019 00:09:37 +0530 Subject: [PATCH] Format code using 'cargo fmt' --- openssl-sys/build/find_vendored.rs | 5 +++- openssl-sys/build/main.rs | 2 +- openssl-sys/src/asn1.rs | 7 ++++- openssl-sys/src/cms.rs | 8 ++++-- openssl-sys/src/ec.rs | 5 +--- openssl/src/aes.rs | 13 +++++---- openssl/src/asn1.rs | 30 +++++++++++++------- openssl/src/cms.rs | 44 ++++++++++++++++++++---------- openssl/src/ec.rs | 17 ++++++------ openssl/src/sign.rs | 26 ++++++++++-------- openssl/src/ssl/callbacks.rs | 5 +++- openssl/src/ssl/mod.rs | 9 +----- openssl/src/symm.rs | 13 +++++++-- 13 files changed, 112 insertions(+), 72 deletions(-) diff --git a/openssl-sys/build/find_vendored.rs b/openssl-sys/build/find_vendored.rs index d3b97c35..45ccfe44 100644 --- a/openssl-sys/build/find_vendored.rs +++ b/openssl-sys/build/find_vendored.rs @@ -4,7 +4,10 @@ use std::path::PathBuf; pub fn get_openssl(_target: &str) -> (PathBuf, PathBuf) { let artifacts = openssl_src::Build::new().build(); println!("cargo:vendored=1"); - println!("cargo:root={}", artifacts.lib_dir().parent().unwrap().display()); + println!( + "cargo:root={}", + artifacts.lib_dir().parent().unwrap().display() + ); ( artifacts.lib_dir().to_path_buf(), diff --git a/openssl-sys/build/main.rs b/openssl-sys/build/main.rs index 9d6f0e85..162e11a6 100644 --- a/openssl-sys/build/main.rs +++ b/openssl-sys/build/main.rs @@ -1,8 +1,8 @@ +extern crate autocfg; extern crate cc; #[cfg(feature = "vendored")] extern crate openssl_src; extern crate pkg_config; -extern crate autocfg; #[cfg(target_env = "msvc")] extern crate vcpkg; diff --git a/openssl-sys/src/asn1.rs b/openssl-sys/src/asn1.rs index 2f2bf1b1..a42c1c54 100644 --- a/openssl-sys/src/asn1.rs +++ b/openssl-sys/src/asn1.rs @@ -40,7 +40,12 @@ extern "C" { pub fn ASN1_GENERALIZEDTIME_print(b: *mut BIO, tm: *const ASN1_GENERALIZEDTIME) -> c_int; pub fn ASN1_TIME_new() -> *mut ASN1_TIME; #[cfg(ossl102)] - pub fn ASN1_TIME_diff(pday: *mut c_int, psec: *mut c_int, from: *const ASN1_TIME, to: *const ASN1_TIME) -> c_int; + pub fn ASN1_TIME_diff( + pday: *mut c_int, + psec: *mut c_int, + from: *const ASN1_TIME, + to: *const ASN1_TIME, + ) -> c_int; pub fn ASN1_TIME_free(tm: *mut ASN1_TIME); pub fn ASN1_TIME_print(b: *mut BIO, tm: *const ASN1_TIME) -> c_int; pub fn ASN1_TIME_set(from: *mut ASN1_TIME, to: time_t) -> *mut ASN1_TIME; diff --git a/openssl-sys/src/cms.rs b/openssl-sys/src/cms.rs index b310ceab..ad5d7160 100644 --- a/openssl-sys/src/cms.rs +++ b/openssl-sys/src/cms.rs @@ -10,7 +10,11 @@ extern "C" { pub fn i2d_CMS_ContentInfo(a: *mut ::CMS_ContentInfo, pp: *mut *mut c_uchar) -> c_int; #[cfg(ossl101)] - pub fn d2i_CMS_ContentInfo(a: *mut *mut ::CMS_ContentInfo, pp: *mut *const c_uchar, length: c_long) -> *mut ::CMS_ContentInfo; + pub fn d2i_CMS_ContentInfo( + a: *mut *mut ::CMS_ContentInfo, + pp: *mut *const c_uchar, + length: c_long, + ) -> *mut ::CMS_ContentInfo; } #[cfg(ossl101)] @@ -76,7 +80,7 @@ extern "C" { certs: *mut stack_st_X509, data: *mut ::BIO, cipher: *const EVP_CIPHER, - flags: c_uint + flags: c_uint, ) -> *mut ::CMS_ContentInfo; #[cfg(ossl101)] diff --git a/openssl-sys/src/ec.rs b/openssl-sys/src/ec.rs index 0e9c4680..98e233fb 100644 --- a/openssl-sys/src/ec.rs +++ b/openssl-sys/src/ec.rs @@ -85,10 +85,7 @@ extern "C" { pub fn EC_POINT_free(point: *mut EC_POINT); - pub fn EC_POINT_dup( - p: *const EC_POINT, - group: *const EC_GROUP, - ) -> *mut EC_POINT; + pub fn EC_POINT_dup(p: *const EC_POINT, group: *const EC_GROUP) -> *mut EC_POINT; pub fn EC_POINT_get_affine_coordinates_GFp( group: *const EC_GROUP, diff --git a/openssl/src/aes.rs b/openssl/src/aes.rs index 879002ab..10e6840a 100644 --- a/openssl/src/aes.rs +++ b/openssl/src/aes.rs @@ -164,7 +164,7 @@ pub fn aes_ige(in_: &[u8], out: &mut [u8], key: &AesKey, iv: &mut [u8], mode: Mo /// * `in_`: The input buffer, storing the key to be wrapped /// /// Returns the number of bytes written into `out` -/// +/// /// # Panics /// /// Panics if either `out` or `in_` do not have sizes that are a multiple of 8, or if @@ -177,10 +177,11 @@ pub fn wrap_key( ) -> Result { unsafe { assert!(out.len() >= in_.len() + 8); // Ciphertext is 64 bits longer (see 2.2.1) - + let written = ffi::AES_wrap_key( &key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer. - iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr() as *const _), + iv.as_ref() + .map_or(ptr::null(), |iv| iv.as_ptr() as *const _), out.as_ptr() as *mut _, in_.as_ptr() as *const _, in_.len() as c_uint, @@ -201,7 +202,7 @@ pub fn wrap_key( /// * `in_`: The input ciphertext /// /// Returns the number of bytes written into `out` -/// +/// /// # Panics /// /// Panics if either `out` or `in_` do not have sizes that are a multiple of 8, or @@ -217,7 +218,8 @@ pub fn unwrap_key( let written = ffi::AES_unwrap_key( &key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer. - iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr() as *const _), + iv.as_ref() + .map_or(ptr::null(), |iv| iv.as_ptr() as *const _), out.as_ptr() as *mut _, in_.as_ptr() as *const _, in_.len() as c_uint, @@ -286,5 +288,4 @@ mod test { ); assert_eq!(&unwrapped[..], &key_data[..]); } - } diff --git a/openssl/src/asn1.rs b/openssl/src/asn1.rs index 6cd47f06..ca10e65b 100644 --- a/openssl/src/asn1.rs +++ b/openssl/src/asn1.rs @@ -127,9 +127,7 @@ impl Asn1TimeRef { let mut seconds = 0; let other = compare.as_ptr(); - let err = unsafe { - ffi::ASN1_TIME_diff(&mut days, &mut seconds, self.as_ptr(), other) - }; + let err = unsafe { ffi::ASN1_TIME_diff(&mut days, &mut seconds, self.as_ptr(), other) }; match err { 0 => Err(ErrorStack::get()), @@ -164,21 +162,27 @@ impl Asn1TimeRef { #[cfg(ossl102)] impl PartialEq for Asn1TimeRef { fn eq(&self, other: &Asn1TimeRef) -> bool { - self.diff(other).map(|t| t.days == 0 && t.secs == 0).unwrap_or(false) + self.diff(other) + .map(|t| t.days == 0 && t.secs == 0) + .unwrap_or(false) } } #[cfg(ossl102)] impl PartialEq for Asn1TimeRef { fn eq(&self, other: &Asn1Time) -> bool { - self.diff(other).map(|t| t.days == 0 && t.secs == 0).unwrap_or(false) + self.diff(other) + .map(|t| t.days == 0 && t.secs == 0) + .unwrap_or(false) } } #[cfg(ossl102)] impl<'a> PartialEq for &'a Asn1TimeRef { fn eq(&self, other: &Asn1Time) -> bool { - self.diff(other).map(|t| t.days == 0 && t.secs == 0).unwrap_or(false) + self.diff(other) + .map(|t| t.days == 0 && t.secs == 0) + .unwrap_or(false) } } @@ -286,21 +290,27 @@ impl Asn1Time { #[cfg(ossl102)] impl PartialEq for Asn1Time { fn eq(&self, other: &Asn1Time) -> bool { - self.diff(other).map(|t| t.days == 0 && t.secs == 0).unwrap_or(false) + self.diff(other) + .map(|t| t.days == 0 && t.secs == 0) + .unwrap_or(false) } } #[cfg(ossl102)] impl PartialEq for Asn1Time { fn eq(&self, other: &Asn1TimeRef) -> bool { - self.diff(other).map(|t| t.days == 0 && t.secs == 0).unwrap_or(false) + self.diff(other) + .map(|t| t.days == 0 && t.secs == 0) + .unwrap_or(false) } } #[cfg(ossl102)] impl<'a> PartialEq<&'a Asn1TimeRef> for Asn1Time { - fn eq(&self, other: & &'a Asn1TimeRef) -> bool { - self.diff(other).map(|t| t.days == 0 && t.secs == 0).unwrap_or(false) + fn eq(&self, other: &&'a Asn1TimeRef) -> bool { + self.diff(other) + .map(|t| t.days == 0 && t.secs == 0) + .unwrap_or(false) } } diff --git a/openssl/src/cms.rs b/openssl/src/cms.rs index 21af2a80..bed17f02 100644 --- a/openssl/src/cms.rs +++ b/openssl/src/cms.rs @@ -14,8 +14,8 @@ use error::ErrorStack; use libc::c_uint; use pkey::{HasPrivate, PKeyRef}; use stack::StackRef; -use x509::{X509Ref, X509}; use symm::Cipher; +use x509::{X509Ref, X509}; use {cvt, cvt_p}; bitflags! { @@ -206,8 +206,7 @@ impl CmsContentInfo { data: &[u8], cipher: Cipher, flags: CMSOptions, - ) -> Result - { + ) -> Result { unsafe { let data_bio = MemBioSlice::new(data)?; @@ -226,9 +225,9 @@ impl CmsContentInfo { #[cfg(test)] mod test { use super::*; + use pkcs12::Pkcs12; use stack::Stack; use x509::X509; - use pkcs12::Pkcs12; #[test] fn cms_encrypt_decrypt() { @@ -239,31 +238,48 @@ mod test { // load cert with private key let priv_cert_bytes = include_bytes!("../test/cms.p12"); let priv_cert = Pkcs12::from_der(priv_cert_bytes).expect("failed to load priv cert"); - let priv_cert = priv_cert.parse("mypass").expect("failed to parse priv cert"); + let priv_cert = priv_cert + .parse("mypass") + .expect("failed to parse priv cert"); // encrypt cms message using public key cert let input = String::from("My Message"); let mut cert_stack = Stack::new().expect("failed to create stack"); - cert_stack.push(pub_cert).expect("failed to add pub cert to stack"); + cert_stack + .push(pub_cert) + .expect("failed to add pub cert to stack"); - let encrypt = CmsContentInfo::encrypt(&cert_stack, &input.as_bytes(), Cipher::des_ede3_cbc(), CMSOptions::empty()) - .expect("failed create encrypted cms"); + let encrypt = CmsContentInfo::encrypt( + &cert_stack, + &input.as_bytes(), + Cipher::des_ede3_cbc(), + CMSOptions::empty(), + ) + .expect("failed create encrypted cms"); // decrypt cms message using private key cert (DER) { let encrypted_der = encrypt.to_der().expect("failed to create der from cms"); - let decrypt = CmsContentInfo::from_der(&encrypted_der).expect("failed read cms from der"); - let decrypt = decrypt.decrypt(&priv_cert.pkey, &priv_cert.cert).expect("failed to decrypt cms"); - let decrypt = String::from_utf8(decrypt).expect("failed to create string from cms content"); + let decrypt = + CmsContentInfo::from_der(&encrypted_der).expect("failed read cms from der"); + let decrypt = decrypt + .decrypt(&priv_cert.pkey, &priv_cert.cert) + .expect("failed to decrypt cms"); + let decrypt = + String::from_utf8(decrypt).expect("failed to create string from cms content"); assert_eq!(input, decrypt); } // decrypt cms message using private key cert (PEM) { let encrypted_pem = encrypt.to_pem().expect("failed to create pem from cms"); - let decrypt = CmsContentInfo::from_pem(&encrypted_pem).expect("failed read cms from pem"); - let decrypt = decrypt.decrypt(&priv_cert.pkey, &priv_cert.cert).expect("failed to decrypt cms"); - let decrypt = String::from_utf8(decrypt).expect("failed to create string from cms content"); + let decrypt = + CmsContentInfo::from_pem(&encrypted_pem).expect("failed read cms from pem"); + let decrypt = decrypt + .decrypt(&priv_cert.pkey, &priv_cert.cert) + .expect("failed to decrypt cms"); + let decrypt = + String::from_utf8(decrypt).expect("failed to create string from cms content"); assert_eq!(input, decrypt); } } diff --git a/openssl/src/ec.rs b/openssl/src/ec.rs index 957e50bb..75786516 100644 --- a/openssl/src/ec.rs +++ b/openssl/src/ec.rs @@ -288,7 +288,11 @@ impl EcGroupRef { /// [`EC_GROUP_get_curve_name`]: https://www.openssl.org/docs/man1.1.0/crypto/EC_GROUP_get_curve_name.html pub fn curve_name(&self) -> Option { let nid = unsafe { ffi::EC_GROUP_get_curve_name(self.as_ptr()) }; - if nid > 0 { Some(Nid::from_raw(nid)) } else { None } + if nid > 0 { + Some(Nid::from_raw(nid)) + } else { + None + } } } @@ -461,13 +465,8 @@ impl EcPointRef { /// OpenSSL documentation at [`EC_POINT_dup`] /// /// [`EC_POINT_dup`]: https://www.openssl.org/docs/man1.1.0/crypto/EC_POINT_dup.html - pub fn to_owned( - &self, - group: &EcGroupRef, - ) -> Result { - unsafe { - cvt_p(ffi::EC_POINT_dup(self.as_ptr(), group.as_ptr())).map(EcPoint) - } + pub fn to_owned(&self, group: &EcGroupRef) -> Result { + unsafe { cvt_p(ffi::EC_POINT_dup(self.as_ptr(), group.as_ptr())).map(EcPoint) } } /// Determines if this point is equal to another. @@ -911,7 +910,7 @@ mod test { fn cofactor() { let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); let mut ctx = BigNumContext::new().unwrap(); - let mut cofactor = BigNum::new().unwrap(); + let mut cofactor = BigNum::new().unwrap(); group.cofactor(&mut cofactor, &mut ctx).unwrap(); let one = BigNum::from_u32(1).unwrap(); assert_eq!(cofactor, one); diff --git a/openssl/src/sign.rs b/openssl/src/sign.rs index 5bc863af..a9d78eec 100644 --- a/openssl/src/sign.rs +++ b/openssl/src/sign.rs @@ -305,7 +305,7 @@ impl<'a> Signer<'a> { ptr::null_mut(), &mut len, ptr::null(), - 0 + 0, ))?; Ok(len) } @@ -358,12 +358,12 @@ impl<'a> Signer<'a> { unsafe { let mut sig_len = sig_buf.len(); cvt(ffi::EVP_DigestSign( - self.md_ctx, - sig_buf.as_mut_ptr() as *mut _, - &mut sig_len, - data_buf.as_ptr() as *const _, - data_buf.len() - ))?; + self.md_ctx, + sig_buf.as_mut_ptr() as *mut _, + &mut sig_len, + data_buf.as_ptr() as *const _, + data_buf.len(), + ))?; Ok(sig_len) } } @@ -436,13 +436,15 @@ impl<'a> Verifier<'a> { /// [`EVP_DigestVerifyInit`]: https://www.openssl.org/docs/manmaster/man3/EVP_DigestVerifyInit.html pub fn new_without_digest(pkey: &'a PKeyRef) -> Result, ErrorStack> where - T: HasPublic + T: HasPublic, { Verifier::new_intern(None, pkey) } - - fn new_intern(type_: Option, pkey: &'a PKeyRef) -> Result, ErrorStack> + fn new_intern( + type_: Option, + pkey: &'a PKeyRef, + ) -> Result, ErrorStack> where T: HasPublic, { @@ -590,13 +592,13 @@ impl<'a> Verifier<'a> { signature.len(), buf.as_ptr() as *const _, buf.len(), - ); + ); match r { 1 => Ok(true), 0 => { ErrorStack::get(); Ok(false) - }, + } _ => Err(ErrorStack::get()), } } diff --git a/openssl/src/ssl/callbacks.rs b/openssl/src/ssl/callbacks.rs index b0251834..05f55932 100644 --- a/openssl/src/ssl/callbacks.rs +++ b/openssl/src/ssl/callbacks.rs @@ -24,7 +24,10 @@ use pkey::Params; use ssl::AlpnError; #[cfg(ossl111)] use ssl::{ClientHelloResponse, ExtensionContext}; -use ssl::{SniError, Ssl, SslAlert, SslContext, SslContextRef, SslRef, SslSession, SslSessionRef, SESSION_CTX_INDEX}; +use ssl::{ + SniError, Ssl, SslAlert, SslContext, SslContextRef, SslRef, SslSession, SslSessionRef, + SESSION_CTX_INDEX, +}; #[cfg(ossl111)] use x509::X509Ref; use x509::{X509StoreContext, X509StoreContextRef}; diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index d2409163..2578c99a 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -486,7 +486,6 @@ impl NameType { lazy_static! { static ref INDEXES: Mutex> = Mutex::new(HashMap::new()); static ref SSL_INDEXES: Mutex> = Mutex::new(HashMap::new()); - static ref SESSION_CTX_INDEX: Index = Ssl::new_ex_index().unwrap(); } @@ -880,13 +879,7 @@ impl SslContextBuilder { /// [`SSL_CTX_add_client_CA`]: https://www.openssl.org/docs/man1.0.2/man3/SSL_CTX_set_client_CA_list.html #[cfg(not(libressl))] pub fn add_client_ca(&mut self, cacert: &X509Ref) -> Result<(), ErrorStack> { - unsafe { - cvt(ffi::SSL_CTX_add_client_CA( - self.as_ptr(), - cacert.as_ptr() - )) - .map(|_| ()) - } + unsafe { cvt(ffi::SSL_CTX_add_client_CA(self.as_ptr(), cacert.as_ptr())).map(|_| ()) } } /// Set the context identifier for sessions. diff --git a/openssl/src/symm.rs b/openssl/src/symm.rs index 86db3b22..c0d70ab9 100644 --- a/openssl/src/symm.rs +++ b/openssl/src/symm.rs @@ -544,7 +544,11 @@ impl Crypter { /// Panics if `output.len() > c_int::max_value()`. pub fn update(&mut self, input: &[u8], output: &mut [u8]) -> Result { unsafe { - let block_size = if self.block_size > 1 { self.block_size } else { 0 }; + let block_size = if self.block_size > 1 { + self.block_size + } else { + 0 + }; assert!(output.len() >= input.len() + block_size); assert!(output.len() <= c_int::max_value() as usize); let mut outl = output.len() as c_int; @@ -575,7 +579,9 @@ impl Crypter { /// where `block_size` is the block size of the cipher (see `Cipher::block_size`). pub fn finalize(&mut self, output: &mut [u8]) -> Result { unsafe { - if self.block_size > 1 { assert!(output.len() >= self.block_size); } + if self.block_size > 1 { + assert!(output.len() >= self.block_size); + } let mut outl = cmp::min(output.len(), c_int::max_value() as usize) as c_int; cvt(ffi::EVP_CipherFinal( @@ -811,7 +817,8 @@ mod tests { super::Mode::Encrypt, &key, Some(&iv), - ).unwrap(); + ) + .unwrap(); assert_eq!(c.update(&[0u8; 15], &mut [0u8; 15]).unwrap(), 15); assert_eq!(c.update(&[0u8; 1], &mut [0u8; 1]).unwrap(), 1);