aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/ssl/mod.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-10-25 23:12:56 -0700
committerSteven Fackler <[email protected]>2016-10-25 23:12:56 -0700
commitf4b70067719a6724658a2b67e80eaf9b38ab89bc (patch)
tree83ff953158aac04d09ab0258e2e68334c1ab894d /openssl/src/ssl/mod.rs
parentMerge pull request #492 from sfackler/gcm (diff)
downloadrust-openssl-f4b70067719a6724658a2b67e80eaf9b38ab89bc.tar.xz
rust-openssl-f4b70067719a6724658a2b67e80eaf9b38ab89bc.zip
Don't allow mutation of SslContexts
SslContext is reference counted and the various setter methods don't take out locks where necessary. Fix this by adding a builder for the context.
Diffstat (limited to 'openssl/src/ssl/mod.rs')
-rw-r--r--openssl/src/ssl/mod.rs85
1 files changed, 56 insertions, 29 deletions
diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs
index 2577600f..d6f5e160 100644
--- a/openssl/src/ssl/mod.rs
+++ b/openssl/src/ssl/mod.rs
@@ -335,20 +335,34 @@ pub enum SniError {
NoAck,
}
-/// A borrowed SSL context object.
-pub struct SslContextRef(Opaque);
+pub struct SslContextBuilder(*mut ffi::SSL_CTX);
-impl SslContextRef {
- pub unsafe fn from_ptr<'a>(ctx: *mut ffi::SSL_CTX) -> &'a SslContextRef {
- &*(ctx as *mut _)
+impl Drop for SslContextBuilder {
+ fn drop(&mut self) {
+ unsafe { ffi::SSL_CTX_free(self.as_ptr()) }
}
+}
- pub unsafe fn from_ptr_mut<'a>(ctx: *mut ffi::SSL_CTX) -> &'a mut SslContextRef {
- &mut *(ctx as *mut _)
+impl SslContextBuilder {
+ pub fn new(method: SslMethod) -> Result<SslContextBuilder, ErrorStack> {
+ init();
+
+ let mut ctx = unsafe {
+ let ctx = try!(cvt_p(ffi::SSL_CTX_new(method.as_ptr())));
+ SslContextBuilder::from_ptr(ctx)
+ };
+
+ try!(ctx.set_mode(ffi::SSL_MODE_AUTO_RETRY | ffi::SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER));
+
+ Ok(ctx)
+ }
+
+ pub unsafe fn from_ptr(ctx: *mut ffi::SSL_CTX) -> SslContextBuilder {
+ SslContextBuilder(ctx)
}
pub fn as_ptr(&self) -> *mut ffi::SSL_CTX {
- self as *const _ as *mut _
+ self.0
}
/// Configures the certificate verification method for new connections.
@@ -409,7 +423,7 @@ impl SslContextRef {
pub fn set_tmp_dh(&mut self, dh: &DH) -> Result<(), ErrorStack> {
unsafe {
- cvt(ffi::SSL_CTX_set_tmp_dh(self.as_ptr(), dh.as_ptr()) as i32).map(|_| ())
+ cvt(ffi::SSL_CTX_set_tmp_dh(self.as_ptr(), dh.as_ptr()) as c_int).map(|_| ())
}
}
@@ -470,7 +484,7 @@ impl SslContextRef {
/// Specifies the file that contains certificate chain
pub fn set_certificate_chain_file<P: AsRef<Path>>(&mut self, file: P)
- -> Result<(), ErrorStack> {
+ -> Result<(), ErrorStack> {
let file = CString::new(file.as_ref().as_os_str().to_str().unwrap()).unwrap();
unsafe {
cvt(ffi::SSL_CTX_use_certificate_chain_file(self.as_ptr(),
@@ -517,13 +531,6 @@ impl SslContextRef {
}
}
- /// Check consistency of private key and certificate
- pub fn check_private_key(&mut self) -> Result<(), ErrorStack> {
- unsafe {
- cvt(ffi::SSL_CTX_check_private_key(self.as_ptr())).map(|_| ())
- }
- }
-
pub fn set_cipher_list(&mut self, cipher_list: &str) -> Result<(), ErrorStack> {
let cipher_list = CString::new(cipher_list).unwrap();
unsafe {
@@ -628,6 +635,36 @@ impl SslContextRef {
Ok(())
}
}
+
+ /// Checks consistency between the private key and certificate.
+ pub fn check_private_key(&self) -> Result<(), ErrorStack> {
+ unsafe {
+ cvt(ffi::SSL_CTX_check_private_key(self.as_ptr())).map(|_| ())
+ }
+ }
+
+ pub fn build(self) -> SslContext {
+ let ctx = SslContext(self.0);
+ mem::forget(self);
+ ctx
+ }
+}
+
+/// A borrowed SSL context object.
+pub struct SslContextRef(Opaque);
+
+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 as *const _ as *mut _
+ }
}
/// An owned SSL context object.
@@ -677,18 +714,8 @@ impl DerefMut for SslContext {
}
impl SslContext {
- /// Creates a new SSL context.
- pub fn new(method: SslMethod) -> Result<SslContext, ErrorStack> {
- init();
-
- let mut ctx = unsafe {
- let ctx = try!(cvt_p(ffi::SSL_CTX_new(method.as_ptr())));
- SslContext::from_ptr(ctx)
- };
-
- try!(ctx.set_mode(ffi::SSL_MODE_AUTO_RETRY | ffi::SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER));
-
- Ok(ctx)
+ pub fn builder(method: SslMethod) -> Result<SslContextBuilder, ErrorStack> {
+ SslContextBuilder::new(method)
}
pub unsafe fn from_ptr(ctx: *mut ffi::SSL_CTX) -> SslContext {