diff options
| author | Steven Fackler <[email protected]> | 2016-10-21 20:50:56 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2016-10-21 20:50:56 -0700 |
| commit | 7267cbeae88687f8c33125178b6a226f632838f1 (patch) | |
| tree | f17b6853dffcd72bf884d11aba939605b61dff81 /openssl/src/ssl/mod.rs | |
| parent | Merge pull request #485 from sfackler/verify-error (diff) | |
| parent | Update BigNumRef (diff) | |
| download | rust-openssl-7267cbeae88687f8c33125178b6a226f632838f1.tar.xz rust-openssl-7267cbeae88687f8c33125178b6a226f632838f1.zip | |
Merge pull request #486 from sfackler/ref-overhaul
Use actual references for Ref types
Diffstat (limited to 'openssl/src/ssl/mod.rs')
| -rw-r--r-- | openssl/src/ssl/mod.rs | 129 |
1 files changed, 75 insertions, 54 deletions
diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index 83c7b7a1..26cafa9a 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -26,6 +26,7 @@ use x509::{X509StoreContext, X509FileType, X509, X509Ref, X509VerifyError}; use x509::verify::X509VerifyParamRef; use crypto::pkey::PKey; use error::ErrorStack; +use opaque::Opaque; pub mod error; mod bio; @@ -210,9 +211,9 @@ extern fn raw_sni<F>(ssl: *mut ffi::SSL, al: *mut c_int, _arg: *mut c_void) -> c let ssl_ctx = ffi::SSL_get_SSL_CTX(ssl); let callback = ffi::SSL_CTX_get_ex_data(ssl_ctx, get_verify_data_idx::<F>()); let callback: &F = &*(callback as *mut F); - let mut ssl = SslRef::from_ptr(ssl); + let ssl = SslRef::from_ptr_mut(ssl); - match callback(&mut ssl) { + match callback(ssl) { Ok(()) => ffi::SSL_TLSEXT_ERR_OK, Err(SniError::Fatal(e)) => { *al = e; @@ -335,15 +336,19 @@ pub enum SniError { } /// A borrowed SSL context object. -pub struct SslContextRef<'a>(*mut ffi::SSL_CTX, PhantomData<&'a ()>); +pub struct SslContextRef(Opaque); -impl<'a> SslContextRef<'a> { - pub unsafe fn from_ptr(ctx: *mut ffi::SSL_CTX) -> SslContextRef<'a> { - SslContextRef(ctx, PhantomData) +impl SslContextRef { + pub unsafe fn from_ptr<'a>(ctx: *mut ffi::SSL_CTX) -> &'a SslContextRef { + &*(ctx as *mut _) + } + + pub unsafe fn from_ptr_mut<'a>(ctx: *mut ffi::SSL_CTX) -> &'a mut SslContextRef { + &mut *(ctx as *mut _) } pub fn as_ptr(&self) -> *mut ffi::SSL_CTX { - self.0 + self as *const _ as *mut _ } /// Configures the certificate verification method for new connections. @@ -626,7 +631,7 @@ impl<'a> SslContextRef<'a> { } /// An owned SSL context object. -pub struct SslContext(SslContextRef<'static>); +pub struct SslContext(*mut ffi::SSL_CTX); unsafe impl Send for SslContext {} unsafe impl Sync for SslContext {} @@ -654,16 +659,20 @@ impl Drop for SslContext { } impl Deref for SslContext { - type Target = SslContextRef<'static>; + type Target = SslContextRef; - fn deref(&self) -> &SslContextRef<'static> { - &self.0 + fn deref(&self) -> &SslContextRef { + unsafe { + SslContextRef::from_ptr(self.0) + } } } impl DerefMut for SslContext { - fn deref_mut(&mut self) -> &mut SslContextRef<'static> { - &mut self.0 + fn deref_mut(&mut self) -> &mut SslContextRef { + unsafe { + SslContextRef::from_ptr_mut(self.0) + } } } @@ -683,11 +692,11 @@ impl SslContext { } pub unsafe fn from_ptr(ctx: *mut ffi::SSL_CTX) -> SslContext { - SslContext(SslContextRef::from_ptr(ctx)) + SslContext(ctx) } pub fn as_ptr(&self) -> *mut ffi::SSL_CTX { - (**self).as_ptr() + self.0 } } @@ -700,16 +709,21 @@ pub struct CipherBits { pub algorithm: i32, } -pub struct SslCipher<'a> { - cipher: *const ffi::SSL_CIPHER, - ph: PhantomData<&'a ()>, -} +pub struct SslCipherRef(Opaque); + +impl SslCipherRef { + pub unsafe fn from_ptr<'a>(ptr: *const ffi::SSL_CIPHER) -> &'a SslCipherRef { + &*(ptr as *const _) + } + + pub fn as_ptr(&self) -> *const ffi::SSL_CIPHER { + self as *const _ as *const _ + } -impl<'a> SslCipher<'a> { /// Returns the name of cipher. pub fn name(&self) -> &'static str { let name = unsafe { - let ptr = ffi::SSL_CIPHER_get_name(self.cipher); + let ptr = ffi::SSL_CIPHER_get_name(self.as_ptr()); CStr::from_ptr(ptr as *const _) }; @@ -719,7 +733,7 @@ impl<'a> SslCipher<'a> { /// Returns the SSL/TLS protocol version that first defined the cipher. pub fn version(&self) -> &'static str { let version = unsafe { - let ptr = ffi::SSL_CIPHER_get_version(self.cipher); + let ptr = ffi::SSL_CIPHER_get_version(self.as_ptr()); CStr::from_ptr(ptr as *const _) }; @@ -730,7 +744,7 @@ impl<'a> SslCipher<'a> { pub fn bits(&self) -> CipherBits { unsafe { let mut algo_bits = 0; - let secret_bits = ffi::SSL_CIPHER_get_bits(self.cipher, &mut algo_bits); + let secret_bits = ffi::SSL_CIPHER_get_bits(self.as_ptr(), &mut algo_bits); CipherBits { secret: secret_bits.into(), algorithm: algo_bits.into(), @@ -743,7 +757,7 @@ impl<'a> SslCipher<'a> { unsafe { // SSL_CIPHER_description requires a buffer of at least 128 bytes. let mut buf = [0; 128]; - let desc_ptr = ffi::SSL_CIPHER_description(self.cipher, buf.as_mut_ptr(), 128); + let desc_ptr = ffi::SSL_CIPHER_description(self.as_ptr(), buf.as_mut_ptr(), 128); if !desc_ptr.is_null() { String::from_utf8(CStr::from_ptr(desc_ptr as *const _).to_bytes().to_vec()).ok() @@ -754,12 +768,12 @@ impl<'a> SslCipher<'a> { } } -pub struct SslRef<'a>(*mut ffi::SSL, PhantomData<&'a ()>); +pub struct SslRef(Opaque); -unsafe impl<'a> Send for SslRef<'a> {} -unsafe impl<'a> Sync for SslRef<'a> {} +unsafe impl Send for SslRef {} +unsafe impl Sync for SslRef {} -impl<'a> fmt::Debug for SslRef<'a> { +impl fmt::Debug for SslRef { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let mut builder = fmt.debug_struct("SslRef"); builder.field("state", &self.state_string_long()); @@ -770,13 +784,17 @@ impl<'a> fmt::Debug for SslRef<'a> { } } -impl<'a> SslRef<'a> { - pub unsafe fn from_ptr(ssl: *mut ffi::SSL) -> SslRef<'a> { - SslRef(ssl, PhantomData) +impl SslRef { + pub unsafe fn from_ptr<'a>(ssl: *mut ffi::SSL) -> &'a SslRef { + &*(ssl as *mut _) + } + + pub unsafe fn from_ptr_mut<'a>(ssl: *mut ffi::SSL) -> &'a mut SslRef { + &mut *(ssl as *mut _) } pub fn as_ptr(&self) -> *mut ffi::SSL { - self.0 + self as *const _ as *mut _ } fn get_raw_rbio(&self) -> *mut ffi::BIO { @@ -823,17 +841,14 @@ impl<'a> SslRef<'a> { } } - pub fn current_cipher(&self) -> Option<SslCipher<'a>> { + pub fn current_cipher(&self) -> Option<&SslCipherRef> { unsafe { let ptr = ffi::SSL_get_current_cipher(self.as_ptr()); if ptr.is_null() { None } else { - Some(SslCipher { - cipher: ptr, - ph: PhantomData, - }) + Some(SslCipherRef::from_ptr(ptr)) } } } @@ -965,13 +980,15 @@ impl<'a> SslRef<'a> { } /// Returns the server's name for the current connection - pub fn servername(&self) -> Option<String> { - let name = unsafe { ffi::SSL_get_servername(self.as_ptr(), ffi::TLSEXT_NAMETYPE_host_name) }; - if name == ptr::null() { - return None; - } + pub fn servername(&self) -> Option<&str> { + unsafe { + let name = ffi::SSL_get_servername(self.as_ptr(), ffi::TLSEXT_NAMETYPE_host_name); + if name == ptr::null() { + return None; + } - unsafe { String::from_utf8(CStr::from_ptr(name as *const _).to_bytes().to_vec()).ok() } + Some(str::from_utf8(CStr::from_ptr(name as *const _).to_bytes()).unwrap()) + } } /// Changes the context corresponding to the current connection. @@ -982,7 +999,7 @@ impl<'a> SslRef<'a> { } /// Returns the context corresponding to the current connection - pub fn ssl_context(&self) -> SslContextRef<'a> { + pub fn ssl_context(&self) -> &SslContextRef { unsafe { let ssl_ctx = ffi::SSL_get_SSL_CTX(self.as_ptr()); SslContextRef::from_ptr(ssl_ctx) @@ -993,21 +1010,21 @@ impl<'a> SslRef<'a> { /// /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] - pub fn param(&mut self) -> X509VerifyParamRef<'a> { + pub fn param_mut(&mut self) -> &mut X509VerifyParamRef { unsafe { - X509VerifyParamRef::from_ptr(ffi::SSL_get0_param(self.as_ptr())) + X509VerifyParamRef::from_ptr_mut(ffi::SSL_get0_param(self.as_ptr())) } } /// Returns the result of X509 certificate verification. pub fn verify_result(&self) -> Option<X509VerifyError> { unsafe { - X509VerifyError::from_raw(ffi::SSL_get_verify_result(self.0)) + X509VerifyError::from_raw(ffi::SSL_get_verify_result(self.as_ptr())) } } } -pub struct Ssl(SslRef<'static>); +pub struct Ssl(*mut ffi::SSL); impl fmt::Debug for Ssl { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { @@ -1027,16 +1044,20 @@ impl Drop for Ssl { } impl Deref for Ssl { - type Target = SslRef<'static>; + type Target = SslRef; - fn deref(&self) -> &SslRef<'static> { - &self.0 + fn deref(&self) -> &SslRef { + unsafe { + SslRef::from_ptr(self.0) + } } } impl DerefMut for Ssl { - fn deref_mut(&mut self) -> &mut SslRef<'static> { - &mut self.0 + fn deref_mut(&mut self) -> &mut SslRef { + unsafe { + SslRef::from_ptr_mut(self.0) + } } } @@ -1049,7 +1070,7 @@ impl Ssl { } pub unsafe fn from_ptr(ssl: *mut ffi::SSL) -> Ssl { - Ssl(SslRef::from_ptr(ssl)) + Ssl(ssl) } /// Creates an SSL/TLS client operating over the provided stream. |