Merge pull request #166 from Ryman/master

Rustup
This commit is contained in:
Steven Fackler 2015-02-20 13:43:12 -08:00
commit cf80af7d1f
7 changed files with 52 additions and 51 deletions

View File

@ -1,5 +1,5 @@
use libc::{c_int, c_ulong, c_void}; use libc::{c_int, c_ulong, c_void};
use std::ffi::{CString, c_str_to_bytes}; use std::ffi::{CStr, CString};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::{fmt, ptr}; use std::{fmt, ptr};
@ -88,7 +88,7 @@ impl BigNum {
pub fn from_dec_str(s: &str) -> Result<BigNum, SslError> { pub fn from_dec_str(s: &str) -> Result<BigNum, SslError> {
BigNum::new().and_then(|v| unsafe { BigNum::new().and_then(|v| unsafe {
let c_str = CString::from_slice(s.as_bytes()); let c_str = CString::new(s.as_bytes()).unwrap();
try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr())); try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr()));
Ok(v) Ok(v)
}) })
@ -96,7 +96,7 @@ impl BigNum {
pub fn from_hex_str(s: &str) -> Result<BigNum, SslError> { pub fn from_hex_str(s: &str) -> Result<BigNum, SslError> {
BigNum::new().and_then(|v| unsafe { BigNum::new().and_then(|v| unsafe {
let c_str = CString::from_slice(s.as_bytes()); let c_str = CString::new(s.as_bytes()).unwrap();
try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr())); try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr()));
Ok(v) Ok(v)
}) })
@ -421,7 +421,7 @@ impl BigNum {
unsafe { unsafe {
let buf = ffi::BN_bn2dec(self.raw()); let buf = ffi::BN_bn2dec(self.raw());
assert!(!buf.is_null()); assert!(!buf.is_null());
let str = String::from_utf8(c_str_to_bytes(&buf).to_vec()).unwrap(); let str = String::from_utf8(CStr::from_ptr(buf).to_bytes().to_vec()).unwrap();
ffi::CRYPTO_free(buf as *mut c_void); ffi::CRYPTO_free(buf as *mut c_void);
str str
} }
@ -431,7 +431,7 @@ impl BigNum {
unsafe { unsafe {
let buf = ffi::BN_bn2hex(self.raw()); let buf = ffi::BN_bn2hex(self.raw());
assert!(!buf.is_null()); assert!(!buf.is_null());
let str = String::from_utf8(c_str_to_bytes(&buf).to_vec()).unwrap(); let str = String::from_utf8(CStr::from_ptr(buf).to_bytes().to_vec()).unwrap();
ffi::CRYPTO_free(buf as *mut c_void); ffi::CRYPTO_free(buf as *mut c_void);
str str
} }

View File

@ -253,7 +253,7 @@ mod tests {
]; ];
let mut h = HMAC::new(MD5, &*tests[0].0); let mut h = HMAC::new(MD5, &*tests[0].0);
for i in 0..100us { for i in 0..100usize {
let test = &tests[i % 2]; let test = &tests[i % 2];
test_hmac_recycle(&mut h, test); test_hmac_recycle(&mut h, test);
} }
@ -351,7 +351,7 @@ mod tests {
]; ];
let mut h = HMAC::new(SHA1, &*tests[0].0); let mut h = HMAC::new(SHA1, &*tests[0].0);
for i in 0..100us { for i in 0..100usize {
let test = &tests[i % 2]; let test = &tests[i % 2];
test_hmac_recycle(&mut h, test); test_hmac_recycle(&mut h, test);
} }
@ -382,7 +382,7 @@ mod tests {
// recycle test // recycle test
let mut h = HMAC::new(ty, &*tests[5].0); let mut h = HMAC::new(ty, &*tests[5].0);
for i in 0..100us { for i in 0..100usize {
let test = &tests[4 + i % 2]; let test = &tests[4 + i % 2];
let tup = (test.0.clone(), test.1.clone(), results[4 + i % 2].clone()); let tup = (test.0.clone(), test.1.clone(), results[4 + i % 2].clone());
test_hmac_recycle(&mut h, &tup); test_hmac_recycle(&mut h, &tup);

View File

@ -1,4 +1,4 @@
#![feature(unsafe_destructor, core, old_io, std_misc, libc, hash, old_path)] #![feature(unsafe_destructor, core, old_io, std_misc, libc, old_path)]
#![crate_name="openssl"] #![crate_name="openssl"]
#![crate_type="rlib"] #![crate_type="rlib"]
#![crate_type="dylib"] #![crate_type="dylib"]

View File

@ -4,7 +4,7 @@ pub use self::OpensslError::*;
use libc::c_ulong; use libc::c_ulong;
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::ffi::c_str_to_bytes; use std::ffi::CStr;
use std::old_io::IoError; use std::old_io::IoError;
use ffi; use ffi;
@ -75,21 +75,21 @@ pub enum OpensslError {
fn get_lib(err: c_ulong) -> String { fn get_lib(err: c_ulong) -> String {
unsafe { unsafe {
let bytes = c_str_to_bytes(&ffi::ERR_lib_error_string(err)).to_vec(); let bytes = CStr::from_ptr(ffi::ERR_lib_error_string(err)).to_bytes().to_vec();
String::from_utf8(bytes).unwrap() String::from_utf8(bytes).unwrap()
} }
} }
fn get_func(err: c_ulong) -> String { fn get_func(err: c_ulong) -> String {
unsafe { unsafe {
let bytes = c_str_to_bytes(&ffi::ERR_func_error_string(err)).to_vec(); let bytes = CStr::from_ptr(ffi::ERR_func_error_string(err)).to_bytes().to_vec();
String::from_utf8(bytes).unwrap() String::from_utf8(bytes).unwrap()
} }
} }
fn get_reason(err: c_ulong) -> String { fn get_reason(err: c_ulong) -> String {
unsafe { unsafe {
let bytes = c_str_to_bytes(&ffi::ERR_reason_error_string(err)).to_vec(); let bytes = CStr::from_ptr(ffi::ERR_reason_error_string(err)).to_bytes().to_vec();
String::from_utf8(bytes).unwrap() String::from_utf8(bytes).unwrap()
} }
} }

View File

@ -1,5 +1,5 @@
use libc::{c_int, c_void, c_long}; use libc::{c_int, c_void, c_long};
use std::ffi::{CString, c_str_to_bytes}; use std::ffi::{CStr, CString};
use std::old_io::{IoResult, IoError, EndOfFile, OtherIoError, Stream, Reader, Writer}; use std::old_io::{IoResult, IoError, EndOfFile, OtherIoError, Stream, Reader, Writer};
use std::mem; use std::mem;
use std::fmt; use std::fmt;
@ -186,7 +186,7 @@ impl fmt::Debug for SslContext {
impl Drop for SslContext { impl Drop for SslContext {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::SSL_CTX_free(self.ctx.ptr) } unsafe { ffi::SSL_CTX_free(*self.ctx) }
} }
} }
@ -200,18 +200,19 @@ impl SslContext {
return Err(SslError::get()); return Err(SslError::get());
} }
Ok(SslContext { ctx: ptr::Unique(ctx) }) let ctx = unsafe { SslContext { ctx: ptr::Unique::new(ctx) } };
Ok(ctx)
} }
/// Configures the certificate verification method for new connections. /// Configures the certificate verification method for new connections.
pub fn set_verify(&mut self, mode: SslVerifyMode, pub fn set_verify(&mut self, mode: SslVerifyMode,
verify: Option<VerifyCallback>) { verify: Option<VerifyCallback>) {
unsafe { unsafe {
ffi::SSL_CTX_set_ex_data(self.ctx.ptr, VERIFY_IDX, ffi::SSL_CTX_set_ex_data(*self.ctx, VERIFY_IDX,
mem::transmute(verify)); mem::transmute(verify));
let f: extern fn(c_int, *mut ffi::X509_STORE_CTX) -> c_int = let f: extern fn(c_int, *mut ffi::X509_STORE_CTX) -> c_int =
raw_verify; raw_verify;
ffi::SSL_CTX_set_verify(self.ctx.ptr, mode as c_int, Some(f)); ffi::SSL_CTX_set_verify(*self.ctx, mode as c_int, Some(f));
} }
} }
@ -224,20 +225,20 @@ impl SslContext {
data: T) { data: T) {
let data = Box::new(data); let data = Box::new(data);
unsafe { unsafe {
ffi::SSL_CTX_set_ex_data(self.ctx.ptr, VERIFY_IDX, ffi::SSL_CTX_set_ex_data(*self.ctx, VERIFY_IDX,
mem::transmute(Some(verify))); mem::transmute(Some(verify)));
ffi::SSL_CTX_set_ex_data(self.ctx.ptr, get_verify_data_idx::<T>(), ffi::SSL_CTX_set_ex_data(*self.ctx, get_verify_data_idx::<T>(),
mem::transmute(data)); mem::transmute(data));
let f: extern fn(c_int, *mut ffi::X509_STORE_CTX) -> c_int = let f: extern fn(c_int, *mut ffi::X509_STORE_CTX) -> c_int =
raw_verify_with_data::<T>; raw_verify_with_data::<T>;
ffi::SSL_CTX_set_verify(self.ctx.ptr, mode as c_int, Some(f)); ffi::SSL_CTX_set_verify(*self.ctx, mode as c_int, Some(f));
} }
} }
/// Sets verification depth /// Sets verification depth
pub fn set_verify_depth(&mut self, depth: u32) { pub fn set_verify_depth(&mut self, depth: u32) {
unsafe { unsafe {
ffi::SSL_CTX_set_verify_depth(self.ctx.ptr, depth as c_int); ffi::SSL_CTX_set_verify_depth(*self.ctx, depth as c_int);
} }
} }
@ -246,8 +247,8 @@ impl SslContext {
pub fn set_CA_file(&mut self, file: &Path) -> Option<SslError> { pub fn set_CA_file(&mut self, file: &Path) -> Option<SslError> {
wrap_ssl_result( wrap_ssl_result(
unsafe { unsafe {
let file = CString::from_slice(file.as_vec()); let file = CString::new(file.as_vec()).unwrap();
ffi::SSL_CTX_load_verify_locations(self.ctx.ptr, file.as_ptr(), ptr::null()) ffi::SSL_CTX_load_verify_locations(*self.ctx, file.as_ptr(), ptr::null())
}) })
} }
@ -256,8 +257,8 @@ impl SslContext {
file_type: X509FileType) -> Option<SslError> { file_type: X509FileType) -> Option<SslError> {
wrap_ssl_result( wrap_ssl_result(
unsafe { unsafe {
let file = CString::from_slice(file.as_vec()); let file = CString::new(file.as_vec()).unwrap();
ffi::SSL_CTX_use_certificate_file(self.ctx.ptr, file.as_ptr(), file_type as c_int) ffi::SSL_CTX_use_certificate_file(*self.ctx, file.as_ptr(), file_type as c_int)
}) })
} }
@ -266,16 +267,16 @@ impl SslContext {
file_type: X509FileType) -> Option<SslError> { file_type: X509FileType) -> Option<SslError> {
wrap_ssl_result( wrap_ssl_result(
unsafe { unsafe {
let file = CString::from_slice(file.as_vec()); let file = CString::new(file.as_vec()).unwrap();
ffi::SSL_CTX_use_PrivateKey_file(self.ctx.ptr, file.as_ptr(), file_type as c_int) ffi::SSL_CTX_use_PrivateKey_file(*self.ctx, file.as_ptr(), file_type as c_int)
}) })
} }
pub fn set_cipher_list(&mut self, cipher_list: &str) -> Option<SslError> { pub fn set_cipher_list(&mut self, cipher_list: &str) -> Option<SslError> {
wrap_ssl_result( wrap_ssl_result(
unsafe { unsafe {
let cipher_list = CString::from_slice(cipher_list.as_bytes()); let cipher_list = CString::new(cipher_list.as_bytes()).unwrap();
ffi::SSL_CTX_set_cipher_list(self.ctx.ptr, cipher_list.as_ptr()) ffi::SSL_CTX_set_cipher_list(*self.ctx, cipher_list.as_ptr())
}) })
} }
} }
@ -309,31 +310,31 @@ impl fmt::Debug for Ssl {
impl Drop for Ssl { impl Drop for Ssl {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::SSL_free(self.ssl.ptr) } unsafe { ffi::SSL_free(*self.ssl) }
} }
} }
impl Ssl { impl Ssl {
pub fn new(ctx: &SslContext) -> Result<Ssl, SslError> { pub fn new(ctx: &SslContext) -> Result<Ssl, SslError> {
let ssl = unsafe { ffi::SSL_new(ctx.ctx.ptr) }; let ssl = unsafe { ffi::SSL_new(*ctx.ctx) };
if ssl == ptr::null_mut() { if ssl == ptr::null_mut() {
return Err(SslError::get()); return Err(SslError::get());
} }
let ssl = Ssl { ssl: ptr::Unique(ssl) }; let ssl = unsafe { Ssl { ssl: ptr::Unique::new(ssl) } };
let rbio = try!(MemBio::new()); let rbio = try!(MemBio::new());
let wbio = try!(MemBio::new()); let wbio = try!(MemBio::new());
unsafe { ffi::SSL_set_bio(ssl.ssl.ptr, rbio.unwrap(), wbio.unwrap()) } unsafe { ffi::SSL_set_bio(*ssl.ssl, rbio.unwrap(), wbio.unwrap()) }
Ok(ssl) Ok(ssl)
} }
fn get_rbio<'a>(&'a self) -> MemBioRef<'a> { fn get_rbio<'a>(&'a self) -> MemBioRef<'a> {
unsafe { self.wrap_bio(ffi::SSL_get_rbio(self.ssl.ptr)) } unsafe { self.wrap_bio(ffi::SSL_get_rbio(*self.ssl)) }
} }
fn get_wbio<'a>(&'a self) -> MemBioRef<'a> { fn get_wbio<'a>(&'a self) -> MemBioRef<'a> {
unsafe { self.wrap_bio(ffi::SSL_get_wbio(self.ssl.ptr)) } unsafe { self.wrap_bio(ffi::SSL_get_wbio(*self.ssl)) }
} }
fn wrap_bio<'a>(&'a self, bio: *mut ffi::BIO) -> MemBioRef<'a> { fn wrap_bio<'a>(&'a self, bio: *mut ffi::BIO) -> MemBioRef<'a> {
@ -345,25 +346,25 @@ impl Ssl {
} }
fn connect(&self) -> c_int { fn connect(&self) -> c_int {
unsafe { ffi::SSL_connect(self.ssl.ptr) } unsafe { ffi::SSL_connect(*self.ssl) }
} }
fn accept(&self) -> c_int { fn accept(&self) -> c_int {
unsafe { ffi::SSL_accept(self.ssl.ptr) } unsafe { ffi::SSL_accept(*self.ssl) }
} }
fn read(&self, buf: &mut [u8]) -> c_int { fn read(&self, buf: &mut [u8]) -> c_int {
unsafe { ffi::SSL_read(self.ssl.ptr, buf.as_ptr() as *mut c_void, unsafe { ffi::SSL_read(*self.ssl, buf.as_ptr() as *mut c_void,
buf.len() as c_int) } buf.len() as c_int) }
} }
fn write(&self, buf: &[u8]) -> c_int { fn write(&self, buf: &[u8]) -> c_int {
unsafe { ffi::SSL_write(self.ssl.ptr, buf.as_ptr() as *const c_void, unsafe { ffi::SSL_write(*self.ssl, buf.as_ptr() as *const c_void,
buf.len() as c_int) } buf.len() as c_int) }
} }
fn get_error(&self, ret: c_int) -> LibSslError { fn get_error(&self, ret: c_int) -> LibSslError {
let err = unsafe { ffi::SSL_get_error(self.ssl.ptr, ret) }; let err = unsafe { ffi::SSL_get_error(*self.ssl, ret) };
match FromPrimitive::from_int(err as isize) { match FromPrimitive::from_int(err as isize) {
Some(err) => err, Some(err) => err,
None => unreachable!() None => unreachable!()
@ -377,8 +378,8 @@ impl Ssl {
// #define SSL_set_tlsext_host_name(s,name) \ // #define SSL_set_tlsext_host_name(s,name) \
// SSL_ctrl(s,SSL_CTRL_SET_TLSEXT_HOSTNAME,TLSEXT_NAMETYPE_host_name,(char *)name) // SSL_ctrl(s,SSL_CTRL_SET_TLSEXT_HOSTNAME,TLSEXT_NAMETYPE_host_name,(char *)name)
let hostname = CString::from_slice(hostname.as_bytes()); let hostname = CString::new(hostname.as_bytes()).unwrap();
ffi::SSL_ctrl(self.ssl.ptr, ffi::SSL_CTRL_SET_TLSEXT_HOSTNAME, ffi::SSL_ctrl(*self.ssl, ffi::SSL_CTRL_SET_TLSEXT_HOSTNAME,
ffi::TLSEXT_NAMETYPE_host_name, ffi::TLSEXT_NAMETYPE_host_name,
hostname.as_ptr() as *mut c_void) hostname.as_ptr() as *mut c_void)
}; };
@ -393,7 +394,7 @@ impl Ssl {
pub fn get_peer_certificate(&self) -> Option<X509> { pub fn get_peer_certificate(&self) -> Option<X509> {
unsafe { unsafe {
let ptr = ffi::SSL_get_peer_certificate(self.ssl.ptr); let ptr = ffi::SSL_get_peer_certificate(*self.ssl);
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
@ -534,14 +535,14 @@ impl<S: Stream> SslStream<S> {
/// either None, indicating no compression is in use, or a string /// either None, indicating no compression is in use, or a string
/// with the compression name. /// with the compression name.
pub fn get_compression(&self) -> Option<String> { pub fn get_compression(&self) -> Option<String> {
let ptr = unsafe { ffi::SSL_get_current_compression(self.ssl.ssl.ptr) }; let ptr = unsafe { ffi::SSL_get_current_compression(*self.ssl.ssl) };
if ptr == ptr::null() { if ptr == ptr::null() {
return None; return None;
} }
let meth = unsafe { ffi::SSL_COMP_get_name(ptr) }; let meth = unsafe { ffi::SSL_COMP_get_name(ptr) };
let s = unsafe { let s = unsafe {
String::from_utf8(c_str_to_bytes(&meth).to_vec()).unwrap() String::from_utf8(CStr::from_ptr(meth).to_bytes().to_vec()).unwrap()
}; };
Some(s) Some(s)

View File

@ -1,7 +1,7 @@
use serialize::hex::FromHex; use serialize::hex::FromHex;
use std::old_io::net::tcp::TcpStream; use std::old_io::net::tcp::TcpStream;
use std::old_io::{Writer}; use std::old_io::{Writer};
use std::thread::Thread; use std::thread;
use crypto::hash::Type::{SHA256}; use crypto::hash::Type::{SHA256};
use ssl::SslMethod::Sslv23; use ssl::SslMethod::Sslv23;
@ -199,7 +199,7 @@ fn test_clone() {
let stream = TcpStream::connect("127.0.0.1:15418").unwrap(); let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), stream).unwrap(); let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), stream).unwrap();
let mut stream2 = stream.clone(); let mut stream2 = stream.clone();
let _t = Thread::spawn(move || { let _t = thread::spawn(move || {
stream2.write_all("GET /\r\n\r\n".as_bytes()).unwrap(); stream2.write_all("GET /\r\n\r\n".as_bytes()).unwrap();
stream2.flush().unwrap(); stream2.flush().unwrap();
}); });

View File

@ -247,7 +247,7 @@ impl X509Generator {
let mut ctx: ffi::X509V3_CTX = mem::zeroed(); let mut ctx: ffi::X509V3_CTX = mem::zeroed();
ffi::X509V3_set_ctx(&mut ctx, x509, x509, ffi::X509V3_set_ctx(&mut ctx, x509, x509,
ptr::null_mut(), ptr::null_mut(), 0); ptr::null_mut(), ptr::null_mut(), 0);
let value = CString::from_slice(value.as_bytes()); let value = CString::new(value.as_bytes()).unwrap();
let ext = ffi::X509V3_EXT_conf_nid(ptr::null_mut(), let ext = ffi::X509V3_EXT_conf_nid(ptr::null_mut(),
mem::transmute(&ctx), mem::transmute(&ctx),
extension, extension,
@ -265,8 +265,8 @@ impl X509Generator {
fn add_name(name: *mut ffi::X509_NAME, key: &str, value: &str) -> Result<(), SslError> { fn add_name(name: *mut ffi::X509_NAME, key: &str, value: &str) -> Result<(), SslError> {
let value_len = value.len() as c_int; let value_len = value.len() as c_int;
lift_ssl!(unsafe { lift_ssl!(unsafe {
let key = CString::from_slice(key.as_bytes()); let key = CString::new(key.as_bytes()).unwrap();
let value = CString::from_slice(value.as_bytes()); let value = CString::new(value.as_bytes()).unwrap();
ffi::X509_NAME_add_entry_by_txt(name, key.as_ptr(), ffi::MBSTRING_UTF8, ffi::X509_NAME_add_entry_by_txt(name, key.as_ptr(), ffi::MBSTRING_UTF8,
value.as_ptr(), value_len, -1, 0) value.as_ptr(), value_len, -1, 0)
}) })