diff options
author | Steven Fackler <sfackler@gmail.com> | 2017-07-15 21:46:11 -0700 |
---|---|---|
committer | Steven Fackler <sfackler@gmail.com> | 2017-07-15 21:46:11 -0700 |
commit | bcd0dcafcba31b7239faf1d582871f8fa83d69e9 (patch) | |
tree | f090be453d289f0f17ca4f6a3f458881e6f7091a /openssl/src/pkey.rs | |
parent | 5c2410c38af8ed2ee041081da84cd61dadc22e12 (diff) | |
download | rust-openssl-bcd0dcafcba31b7239faf1d582871f8fa83d69e9.zip |
Rustfmt
Diffstat (limited to 'openssl/src/pkey.rs')
-rw-r--r-- | openssl/src/pkey.rs | 120 |
1 files changed, 82 insertions, 38 deletions
diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 7bda47b5..aba52ae6 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -84,7 +84,11 @@ impl PKey { unsafe { let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let pkey = PKey(evp); - try!(cvt(ffi::EVP_PKEY_assign(pkey.0, ffi::EVP_PKEY_RSA, rsa.as_ptr() as *mut _))); + try!(cvt(ffi::EVP_PKEY_assign( + pkey.0, + ffi::EVP_PKEY_RSA, + rsa.as_ptr() as *mut _, + ))); mem::forget(rsa); Ok(pkey) } @@ -95,7 +99,11 @@ impl PKey { unsafe { let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let pkey = PKey(evp); - try!(cvt(ffi::EVP_PKEY_assign(pkey.0, ffi::EVP_PKEY_DSA, dsa.as_ptr() as *mut _))); + try!(cvt(ffi::EVP_PKEY_assign( + pkey.0, + ffi::EVP_PKEY_DSA, + dsa.as_ptr() as *mut _, + ))); mem::forget(dsa); Ok(pkey) } @@ -106,7 +114,11 @@ impl PKey { unsafe { let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let pkey = PKey(evp); - try!(cvt(ffi::EVP_PKEY_assign(pkey.0, ffi::EVP_PKEY_DH, dh.as_ptr() as *mut _))); + try!(cvt(ffi::EVP_PKEY_assign( + pkey.0, + ffi::EVP_PKEY_DH, + dh.as_ptr() as *mut _, + ))); mem::forget(dh); Ok(pkey) } @@ -117,7 +129,11 @@ impl PKey { unsafe { let evp = try!(cvt_p(ffi::EVP_PKEY_new())); let pkey = PKey(evp); - try!(cvt(ffi::EVP_PKEY_assign(pkey.0, ffi::EVP_PKEY_EC, ec_key.as_ptr() as *mut _))); + try!(cvt(ffi::EVP_PKEY_assign( + pkey.0, + ffi::EVP_PKEY_EC, + ec_key.as_ptr() as *mut _, + ))); mem::forget(ec_key); Ok(pkey) } @@ -130,10 +146,12 @@ impl PKey { pub fn hmac(key: &[u8]) -> Result<PKey, ErrorStack> { unsafe { assert!(key.len() <= c_int::max_value() as usize); - let key = try!(cvt_p(ffi::EVP_PKEY_new_mac_key(ffi::EVP_PKEY_HMAC, - ptr::null_mut(), - key.as_ptr() as *const _, - key.len() as c_int))); + let key = try!(cvt_p(ffi::EVP_PKEY_new_mac_key( + ffi::EVP_PKEY_HMAC, + ptr::null_mut(), + key.as_ptr() as *const _, + key.len() as c_int, + ))); Ok(PKey(key)) } } @@ -149,17 +167,19 @@ impl PKey { /// The callback should copy the password into the provided buffer and return the number of /// bytes written. pub fn private_key_from_pkcs8_callback<F>(der: &[u8], callback: F) -> Result<PKey, ErrorStack> - where F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack> + where + F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack>, { unsafe { ffi::init(); let mut cb = CallbackState::new(callback); let bio = try!(MemBioSlice::new(der)); - cvt_p(ffi::d2i_PKCS8PrivateKey_bio(bio.as_ptr(), - ptr::null_mut(), - Some(invoke_passwd_cb::<F>), - &mut cb as *mut _ as *mut _)) - .map(PKey) + cvt_p(ffi::d2i_PKCS8PrivateKey_bio( + bio.as_ptr(), + ptr::null_mut(), + Some(invoke_passwd_cb::<F>), + &mut cb as *mut _ as *mut _, + )).map(PKey) } } @@ -169,33 +189,38 @@ impl PKey { /// # Panics /// /// Panics if `passphrase` contains an embedded null. - pub fn private_key_from_pkcs8_passphrase(der: &[u8], - passphrase: &[u8]) - -> Result<PKey, ErrorStack> { + pub fn private_key_from_pkcs8_passphrase( + der: &[u8], + passphrase: &[u8], + ) -> Result<PKey, ErrorStack> { unsafe { ffi::init(); let bio = try!(MemBioSlice::new(der)); let passphrase = CString::new(passphrase).unwrap(); - cvt_p(ffi::d2i_PKCS8PrivateKey_bio(bio.as_ptr(), - ptr::null_mut(), - None, - passphrase.as_ptr() as *const _ as *mut _)) - .map(PKey) + cvt_p(ffi::d2i_PKCS8PrivateKey_bio( + bio.as_ptr(), + ptr::null_mut(), + None, + passphrase.as_ptr() as *const _ as *mut _, + )).map(PKey) } } #[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")] pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<PKey, ErrorStack> - where F: FnOnce(&mut [c_char]) -> usize + where + F: FnOnce(&mut [c_char]) -> usize, { ffi::init(); let mut cb = CallbackState::new(pass_cb); let mem_bio = try!(MemBioSlice::new(buf)); unsafe { - let evp = try!(cvt_p(ffi::PEM_read_bio_PrivateKey(mem_bio.as_ptr(), - ptr::null_mut(), - Some(invoke_passwd_cb_old::<F>), - &mut cb as *mut _ as *mut c_void))); + let evp = try!(cvt_p(ffi::PEM_read_bio_PrivateKey( + mem_bio.as_ptr(), + ptr::null_mut(), + Some(invoke_passwd_cb_old::<F>), + &mut cb as *mut _ as *mut c_void, + ))); Ok(PKey::from_ptr(evp)) } } @@ -224,7 +249,10 @@ impl PKeyCtx { impl PKeyCtxRef { pub fn set_rsa_padding(&mut self, pad: Padding) -> Result<(), ErrorStack> { unsafe { - try!(cvt(ffi::EVP_PKEY_CTX_set_rsa_padding(self.as_ptr(), pad.as_raw()))); + try!(cvt(ffi::EVP_PKEY_CTX_set_rsa_padding( + self.as_ptr(), + pad.as_raw(), + ))); } Ok(()) } @@ -232,7 +260,9 @@ impl PKeyCtxRef { pub fn rsa_padding(&self) -> Result<Padding, ErrorStack> { let mut pad: c_int = 0; unsafe { - try!(cvt(ffi::EVP_PKEY_CTX_get_rsa_padding(self.as_ptr(), &mut pad))); + try!(cvt( + ffi::EVP_PKEY_CTX_get_rsa_padding(self.as_ptr(), &mut pad), + )); }; Ok(Padding::from_raw(pad)) } @@ -246,17 +276,31 @@ impl PKeyCtxRef { pub fn derive_set_peer(&mut self, peer: &PKeyRef) -> Result<(), ErrorStack> { unsafe { - try!(cvt(ffi::EVP_PKEY_derive_set_peer(self.as_ptr(), peer.as_ptr()))); + try!(cvt( + ffi::EVP_PKEY_derive_set_peer(self.as_ptr(), peer.as_ptr()), + )); } Ok(()) } pub fn derive(&mut self) -> Result<Vec<u8>, ErrorStack> { let mut len: size_t = 0; - unsafe { try!(cvt(ffi::EVP_PKEY_derive(self.as_ptr(), ptr::null_mut(), &mut len))); } + unsafe { + try!(cvt(ffi::EVP_PKEY_derive( + self.as_ptr(), + ptr::null_mut(), + &mut len, + ))); + } let mut key = vec![0u8; len]; - unsafe { try!(cvt(ffi::EVP_PKEY_derive(self.as_ptr(), key.as_mut_ptr(), &mut len))); } + unsafe { + try!(cvt(ffi::EVP_PKEY_derive( + self.as_ptr(), + key.as_mut_ptr(), + &mut len, + ))); + } Ok(key) } } @@ -276,7 +320,8 @@ mod tests { fn test_to_password() { let rsa = Rsa::generate(2048).unwrap(); let pkey = PKey::from_rsa(rsa).unwrap(); - let pem = pkey.private_key_to_pem_passphrase(Cipher::aes_128_cbc(), b"foobar").unwrap(); + let pem = pkey.private_key_to_pem_passphrase(Cipher::aes_128_cbc(), b"foobar") + .unwrap(); PKey::private_key_from_pem_passphrase(&pem, b"foobar").unwrap(); assert!(PKey::private_key_from_pem_passphrase(&pem, b"fizzbuzz").is_err()); } @@ -292,11 +337,10 @@ mod tests { let mut password_queried = false; let key = include_bytes!("../test/pkcs8.der"); PKey::private_key_from_pkcs8_callback(key, |password| { - password_queried = true; - password[..6].copy_from_slice(b"mypass"); - Ok(6) - }) - .unwrap(); + password_queried = true; + password[..6].copy_from_slice(b"mypass"); + Ok(6) + }).unwrap(); assert!(password_queried); } |