aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/ssl
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2017-07-15 16:34:07 -0700
committerSteven Fackler <[email protected]>2017-07-15 16:50:36 -0700
commitfd52bbe85c1b67a5416ded43a0845be3d1c57b59 (patch)
treec0406ebe644ace5862242938f763753ecf0590c0 /openssl/src/ssl
parentMove callbacks to a submodule (diff)
downloadrust-openssl-fd52bbe85c1b67a5416ded43a0845be3d1c57b59.tar.xz
rust-openssl-fd52bbe85c1b67a5416ded43a0845be3d1c57b59.zip
Add an API to install extra data
Diffstat (limited to 'openssl/src/ssl')
-rw-r--r--openssl/src/ssl/mod.rs86
1 files changed, 78 insertions, 8 deletions
diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs
index 2c0a4b63..4f888f9d 100644
--- a/openssl/src/ssl/mod.rs
+++ b/openssl/src/ssl/mod.rs
@@ -93,7 +93,7 @@ use std::slice;
use std::str;
use std::sync::Mutex;
-use {init, cvt, cvt_p};
+use {init, cvt, cvt_p, cvt_n};
use dh::{Dh, DhRef};
use ec::EcKeyRef;
#[cfg(any(all(feature = "v101", ossl101), all(feature = "v102", ossl102)))]
@@ -106,8 +106,15 @@ use x509::store::X509Store;
use verify::X509VerifyParamRef;
use pkey::PKeyRef;
use error::ErrorStack;
+use ex_data::Index;
use util::Opaque;
use stack::{Stack, StackRef};
+use ssl::bio::BioMethod;
+use ssl::callbacks::*;
+
+pub use ssl::connector::{SslConnectorBuilder, SslConnector, SslAcceptorBuilder, SslAcceptor,
+ ConnectConfiguration};
+pub use ssl::error::{Error, HandshakeError};
mod error;
mod callbacks;
@@ -116,13 +123,6 @@ mod bio;
#[cfg(test)]
mod tests;
-use ssl::bio::BioMethod;
-use ssl::callbacks::*;
-
-pub use ssl::connector::{SslConnectorBuilder, SslConnector, SslAcceptorBuilder, SslAcceptor,
- ConnectConfiguration};
-pub use ssl::error::{Error, HandshakeError};
-
// FIXME drop SSL_ prefix
// FIXME remvove flags not used in OpenSSL 1.1
bitflags! {
@@ -741,6 +741,14 @@ impl SslContextBuilder {
}
}
+ /// Sets the extra data at the specified index.
+ pub fn set_ex_data<T>(&mut self, index: Index<SslContext, T>, data: T) {
+ unsafe {
+ let data = Box::new(data);
+ ffi::SSL_CTX_set_ex_data(self.as_ptr(), index.as_raw(), Box::into_raw(data) as *mut c_void);
+ }
+ }
+
pub fn build(self) -> SslContext {
let ctx = SslContext(self.0);
mem::forget(self);
@@ -779,6 +787,20 @@ impl SslContext {
pub fn builder(method: SslMethod) -> Result<SslContextBuilder, ErrorStack> {
SslContextBuilder::new(method)
}
+
+ /// Returns a new extra data index.
+ ///
+ /// Each invocation of this function is guaranteed to return a distinct
+ /// index.
+ pub fn new_ex_index<T>() -> Result<Index<SslContext, T>, ErrorStack>
+ where
+ T: 'static + Sync + Send
+ {
+ unsafe {
+ let idx = try!(cvt_n(compat::get_new_idx(free_data_box::<T>)));
+ Ok(Index::from_raw(idx))
+ }
+ }
}
impl SslContextRef {
@@ -825,6 +847,18 @@ impl SslContextRef {
StackRef::from_ptr(chain)
}
}
+
+ /// Returns a reference to the extra data at the specified index.
+ pub fn ex_data<T>(&self, index: Index<SslContext, T>) -> Option<&T> {
+ unsafe {
+ let data = ffi::SSL_CTX_get_ex_data(self.as_ptr(), index.as_raw());
+ if data.is_null() {
+ None
+ } else {
+ Some(&*(data as *const T))
+ }
+ }
+ }
}
pub struct CipherBits {
@@ -981,6 +1015,22 @@ foreign_type! {
pub struct SslRef;
}
+impl Ssl {
+ /// Returns a new extra data index.
+ ///
+ /// Each invocation of this function is guaranteed to return a distinct
+ /// index.
+ pub fn new_ex_index<T>() -> Result<Index<Ssl, T>, ErrorStack>
+ where
+ T: 'static + Sync + Send
+ {
+ unsafe {
+ let idx = try!(cvt_n(compat::get_new_ssl_idx(free_data_box::<T>)));
+ Ok(Index::from_raw(idx))
+ }
+ }
+}
+
impl fmt::Debug for SslRef {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut builder = fmt.debug_struct("Ssl");
@@ -1353,6 +1403,26 @@ impl SslRef {
pub fn is_server(&self) -> bool {
unsafe { compat::SSL_is_server(self.as_ptr()) != 0 }
}
+
+ /// Sets the extra data at the specified index.
+ pub fn set_ex_data<T>(&mut self, index: Index<Ssl, T>, data: T) {
+ unsafe {
+ let data = Box::new(data);
+ ffi::SSL_set_ex_data(self.as_ptr(), index.as_raw(), Box::into_raw(data) as *mut c_void);
+ }
+ }
+
+ /// Returns a reference to the extra data at the specified index.
+ pub fn ex_data<T>(&self, index: Index<Ssl, T>) -> Option<&T> {
+ unsafe {
+ let data = ffi::SSL_get_ex_data(self.as_ptr(), index.as_raw());
+ if data.is_null() {
+ None
+ } else {
+ Some(&*(data as *const T))
+ }
+ }
+ }
}
unsafe impl Sync for Ssl {}