diff options
Diffstat (limited to 'src/ssl')
| -rw-r--r-- | src/ssl/error.rs | 19 | ||||
| -rw-r--r-- | src/ssl/mod.rs | 49 | ||||
| -rw-r--r-- | src/ssl/tests.rs | 18 |
3 files changed, 48 insertions, 38 deletions
diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 888a9cdc..c52879a0 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -3,8 +3,8 @@ pub use self::OpensslError::*; use libc::c_ulong; use std::error; +use std::ffi::c_str_to_bytes; use std::io::IoError; -use std::c_str::CString; use ffi; @@ -51,15 +51,24 @@ pub enum OpensslError { } fn get_lib(err: c_ulong) -> String { - unsafe { CString::new(ffi::ERR_lib_error_string(err), false) }.to_string() + unsafe { + let bytes = c_str_to_bytes(&ffi::ERR_lib_error_string(err)).to_vec(); + String::from_utf8(bytes).unwrap() + } } fn get_func(err: c_ulong) -> String { - unsafe { CString::new(ffi::ERR_func_error_string(err), false).to_string() } + unsafe { + let bytes = c_str_to_bytes(&ffi::ERR_func_error_string(err)).to_vec(); + String::from_utf8(bytes).unwrap() + } } fn get_reason(err: c_ulong) -> String { - unsafe { CString::new(ffi::ERR_reason_error_string(err), false).to_string() } + unsafe { + let bytes = c_str_to_bytes(&ffi::ERR_reason_error_string(err)).to_vec(); + String::from_utf8(bytes).unwrap() + } } impl SslError { @@ -100,7 +109,7 @@ fn test_uknown_error_should_have_correct_messages() { let UnknownError { ref library, ref function, ref reason } = errs[0]; - assert_eq!(library.as_slice(),"SSL routines"); + assert_eq!(library.as_slice(), "SSL routines"); assert_eq!(function.as_slice(), "SSL23_GET_SERVER_HELLO"); assert_eq!(reason.as_slice(), "sslv3 alert handshake failure"); } diff --git a/src/ssl/mod.rs b/src/ssl/mod.rs index 390e8230..4d11ae4a 100644 --- a/src/ssl/mod.rs +++ b/src/ssl/mod.rs @@ -1,5 +1,5 @@ use libc::{c_int, c_void, c_long}; -use std::c_str::ToCStr; +use std::ffi::{CString, c_str_to_bytes}; use std::io::{IoResult, IoError, EndOfFile, Stream, Reader, Writer}; use std::mem; use std::num::FromPrimitive; @@ -237,39 +237,39 @@ impl SslContext { #[allow(non_snake_case)] /// Specifies the file that contains trusted CA certificates. pub fn set_CA_file(&mut self, file: &Path) -> Option<SslError> { - wrap_ssl_result(file.with_c_str(|file| { + wrap_ssl_result( unsafe { - ffi::SSL_CTX_load_verify_locations(self.ctx.0, file, ptr::null()) - } - })) + let file = CString::from_slice(file.as_vec()); + ffi::SSL_CTX_load_verify_locations(self.ctx.0, file.as_ptr(), ptr::null()) + }) } /// Specifies the file that contains certificate pub fn set_certificate_file(&mut self, file: &Path, file_type: X509FileType) -> Option<SslError> { - wrap_ssl_result(file.with_c_str(|file| { + wrap_ssl_result( unsafe { - ffi::SSL_CTX_use_certificate_file(self.ctx.0, file, file_type as c_int) - } - })) + let file = CString::from_slice(file.as_vec()); + ffi::SSL_CTX_use_certificate_file(self.ctx.0, file.as_ptr(), file_type as c_int) + }) } /// Specifies the file that contains private key pub fn set_private_key_file(&mut self, file: &Path, file_type: X509FileType) -> Option<SslError> { - wrap_ssl_result(file.with_c_str(|file| { + wrap_ssl_result( unsafe { - ffi::SSL_CTX_use_PrivateKey_file(self.ctx.0, file, file_type as c_int) - } - })) + let file = CString::from_slice(file.as_vec()); + ffi::SSL_CTX_use_PrivateKey_file(self.ctx.0, file.as_ptr(), file_type as c_int) + }) } pub fn set_cipher_list(&mut self, cipher_list: &str) -> Option<SslError> { - wrap_ssl_result(cipher_list.with_c_str(|cipher_list| { + wrap_ssl_result( unsafe { - ffi::SSL_CTX_set_cipher_list(self.ctx.0, cipher_list) - } - })) + let cipher_list = CString::from_slice(cipher_list.as_bytes()); + ffi::SSL_CTX_set_cipher_list(self.ctx.0, cipher_list.as_ptr()) + }) } } @@ -358,17 +358,16 @@ impl Ssl { /// Set the host name to be used with SNI (Server Name Indication). pub fn set_hostname(&self, hostname: &str) -> Result<(), SslError> { - let ret = hostname.with_c_str(|hostname| { - unsafe { + let ret = unsafe { // This is defined as a macro: // #define SSL_set_tlsext_host_name(s,name) \ // SSL_ctrl(s,SSL_CTRL_SET_TLSEXT_HOSTNAME,TLSEXT_NAMETYPE_host_name,(char *)name) + let hostname = CString::from_slice(hostname.as_bytes()); ffi::SSL_ctrl(self.ssl.0, ffi::SSL_CTRL_SET_TLSEXT_HOSTNAME, ffi::TLSEXT_NAMETYPE_host_name, - hostname as *const c_void as *mut c_void) - } - }); + hostname.as_ptr() as *mut c_void) + }; // For this case, 0 indicates failure. if ret == 0 { @@ -496,7 +495,7 @@ impl<S: Stream> SslStream<S> { LibSslError::ErrorWantWrite => { try_ssl_stream!(self.flush()) } LibSslError::ErrorZeroReturn => return Err(SslSessionClosed), LibSslError::ErrorSsl => return Err(SslError::get()), - err => panic!("unexpected error {}", err), + err => panic!("unexpected error {:?}", err), } } } @@ -521,7 +520,9 @@ impl<S: Stream> SslStream<S> { } let meth = unsafe { ffi::SSL_COMP_get_name(ptr) }; - let s = unsafe { String::from_raw_buf(meth as *const u8) }; + let s = unsafe { + String::from_utf8(c_str_to_bytes(&meth).to_vec()).unwrap() + }; Some(s) } diff --git a/src/ssl/tests.rs b/src/ssl/tests.rs index 6723fa58..d78e21c5 100644 --- a/src/ssl/tests.rs +++ b/src/ssl/tests.rs @@ -27,7 +27,7 @@ fn test_verify_untrusted() { ctx.set_verify(SslVerifyPeer, None); match SslStream::new(&ctx, stream) { Ok(_) => panic!("expected failure"), - Err(err) => println!("error {}", err) + Err(err) => println!("error {:?}", err) } } @@ -38,11 +38,11 @@ fn test_verify_trusted() { ctx.set_verify(SslVerifyPeer, None); match ctx.set_CA_file(&Path::new("test/cert.pem")) { None => {} - Some(err) => panic!("Unexpected error {}", err) + Some(err) => panic!("Unexpected error {:?}", err) } match SslStream::new(&ctx, stream) { Ok(_) => (), - Err(err) => panic!("Expected success, got {}", err) + Err(err) => panic!("Expected success, got {:?}", err) } } @@ -56,7 +56,7 @@ fn test_verify_untrusted_callback_override_ok() { ctx.set_verify(SslVerifyPeer, Some(callback as VerifyCallback)); match SslStream::new(&ctx, stream) { Ok(_) => (), - Err(err) => panic!("Expected success, got {}", err) + Err(err) => panic!("Expected success, got {:?}", err) } } @@ -81,11 +81,11 @@ fn test_verify_trusted_callback_override_ok() { ctx.set_verify(SslVerifyPeer, Some(callback as VerifyCallback)); match ctx.set_CA_file(&Path::new("test/cert.pem")) { None => {} - Some(err) => panic!("Unexpected error {}", err) + Some(err) => panic!("Unexpected error {:?}", err) } match SslStream::new(&ctx, stream) { Ok(_) => (), - Err(err) => panic!("Expected success, got {}", err) + Err(err) => panic!("Expected success, got {:?}", err) } } @@ -99,7 +99,7 @@ fn test_verify_trusted_callback_override_bad() { ctx.set_verify(SslVerifyPeer, Some(callback as VerifyCallback)); match ctx.set_CA_file(&Path::new("test/cert.pem")) { None => {} - Some(err) => panic!("Unexpected error {}", err) + Some(err) => panic!("Unexpected error {:?}", err) } assert!(SslStream::new(&ctx, stream).is_err()); } @@ -127,7 +127,7 @@ fn test_verify_trusted_get_error_ok() { ctx.set_verify(SslVerifyPeer, Some(callback as VerifyCallback)); match ctx.set_CA_file(&Path::new("test/cert.pem")) { None => {} - Some(err) => panic!("Unexpected error {}", err) + Some(err) => panic!("Unexpected error {:?}", err) } assert!(SslStream::new(&ctx, stream).is_ok()); } @@ -170,7 +170,7 @@ fn test_verify_callback_data() { match SslStream::new(&ctx, stream) { Ok(_) => (), - Err(err) => panic!("Expected success, got {}", err) + Err(err) => panic!("Expected success, got {:?}", err) } } |