Return Result<(),SslError> instead of Option<SslError>
This commit is contained in:
parent
b6c5c113f5
commit
e1d65fc2be
|
|
@ -300,11 +300,11 @@ pub type VerifyCallbackData<T> = fn(preverify_ok: bool,
|
||||||
|
|
||||||
// FIXME: macro may be instead of inlining?
|
// FIXME: macro may be instead of inlining?
|
||||||
#[inline]
|
#[inline]
|
||||||
fn wrap_ssl_result(res: c_int) -> Option<SslError> {
|
fn wrap_ssl_result(res: c_int) -> Result<(),SslError> {
|
||||||
if res == 0 {
|
if res == 0 {
|
||||||
Some(SslError::get())
|
Err(SslError::get())
|
||||||
} else {
|
} else {
|
||||||
None
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -383,7 +383,7 @@ impl SslContext {
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
/// Specifies the file that contains trusted CA certificates.
|
/// Specifies the file that contains trusted CA certificates.
|
||||||
pub fn set_CA_file(&mut self, file: &Path) -> Option<SslError> {
|
pub fn set_CA_file(&mut self, file: &Path) -> Result<(),SslError> {
|
||||||
let file = CString::new(file.as_os_str().to_str().expect("invalid utf8")).unwrap();
|
let file = CString::new(file.as_os_str().to_str().expect("invalid utf8")).unwrap();
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -393,7 +393,7 @@ impl SslContext {
|
||||||
|
|
||||||
/// Specifies the file that contains certificate
|
/// Specifies the file that contains certificate
|
||||||
pub fn set_certificate_file(&mut self, file: &Path,
|
pub fn set_certificate_file(&mut self, file: &Path,
|
||||||
file_type: X509FileType) -> Option<SslError> {
|
file_type: X509FileType) -> Result<(),SslError> {
|
||||||
let file = CString::new(file.as_os_str().to_str().expect("invalid utf8")).unwrap();
|
let file = CString::new(file.as_os_str().to_str().expect("invalid utf8")).unwrap();
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -402,7 +402,7 @@ impl SslContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies the certificate
|
/// Specifies the certificate
|
||||||
pub fn set_certificate(&mut self, cert: &X509) -> Option<SslError> {
|
pub fn set_certificate(&mut self, cert: &X509) -> Result<(),SslError> {
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::SSL_CTX_use_certificate(*self.ctx, cert.get_handle())
|
ffi::SSL_CTX_use_certificate(*self.ctx, cert.get_handle())
|
||||||
|
|
@ -411,7 +411,7 @@ impl SslContext {
|
||||||
|
|
||||||
/// Adds a certificate to the certificate chain presented together with the
|
/// Adds a certificate to the certificate chain presented together with the
|
||||||
/// certificate specified using set_certificate()
|
/// certificate specified using set_certificate()
|
||||||
pub fn add_extra_chain_cert(&mut self, cert: &X509) -> Option<SslError> {
|
pub fn add_extra_chain_cert(&mut self, cert: &X509) -> Result<(),SslError> {
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::SSL_CTX_add_extra_chain_cert(*self.ctx, cert.get_handle()) as c_int
|
ffi::SSL_CTX_add_extra_chain_cert(*self.ctx, cert.get_handle()) as c_int
|
||||||
|
|
@ -420,7 +420,7 @@ impl SslContext {
|
||||||
|
|
||||||
/// Specifies the file that contains private key
|
/// Specifies the file that contains private key
|
||||||
pub fn set_private_key_file(&mut self, file: &Path,
|
pub fn set_private_key_file(&mut self, file: &Path,
|
||||||
file_type: X509FileType) -> Option<SslError> {
|
file_type: X509FileType) -> Result<(),SslError> {
|
||||||
let file = CString::new(file.as_os_str().to_str().expect("invalid utf8")).unwrap();
|
let file = CString::new(file.as_os_str().to_str().expect("invalid utf8")).unwrap();
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -429,7 +429,7 @@ impl SslContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies the private key
|
/// Specifies the private key
|
||||||
pub fn set_private_key(&mut self, key: &PKey) -> Option<SslError> {
|
pub fn set_private_key(&mut self, key: &PKey) -> Result<(),SslError> {
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::SSL_CTX_use_PrivateKey(*self.ctx, key.get_handle())
|
ffi::SSL_CTX_use_PrivateKey(*self.ctx, key.get_handle())
|
||||||
|
|
@ -437,14 +437,14 @@ impl SslContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check consistency of private key and certificate
|
/// Check consistency of private key and certificate
|
||||||
pub fn check_private_key(&mut self) -> Option<SslError> {
|
pub fn check_private_key(&mut self) -> Result<(),SslError> {
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
ffi::SSL_CTX_check_private_key(*self.ctx)
|
ffi::SSL_CTX_check_private_key(*self.ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cipher_list(&mut self, cipher_list: &str) -> Option<SslError> {
|
pub fn set_cipher_list(&mut self, cipher_list: &str) -> Result<(),SslError> {
|
||||||
wrap_ssl_result(
|
wrap_ssl_result(
|
||||||
unsafe {
|
unsafe {
|
||||||
let cipher_list = CString::new(cipher_list.as_bytes()).unwrap();
|
let cipher_list = CString::new(cipher_list.as_bytes()).unwrap();
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ fn test_verify_trusted() {
|
||||||
let mut ctx = SslContext::new(Sslv23).unwrap();
|
let mut ctx = SslContext::new(Sslv23).unwrap();
|
||||||
ctx.set_verify(SSL_VERIFY_PEER, None);
|
ctx.set_verify(SSL_VERIFY_PEER, None);
|
||||||
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
||||||
None => {}
|
Ok(_) => {}
|
||||||
Some(err) => panic!("Unexpected error {:?}", err)
|
Err(err) => panic!("Unexpected error {:?}", err)
|
||||||
}
|
}
|
||||||
match SslStream::new(&ctx, stream) {
|
match SslStream::new(&ctx, stream) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
|
|
@ -91,8 +91,8 @@ fn test_verify_trusted_callback_override_ok() {
|
||||||
let mut ctx = SslContext::new(Sslv23).unwrap();
|
let mut ctx = SslContext::new(Sslv23).unwrap();
|
||||||
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
|
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
|
||||||
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
||||||
None => {}
|
Ok(_) => {}
|
||||||
Some(err) => panic!("Unexpected error {:?}", err)
|
Err(err) => panic!("Unexpected error {:?}", err)
|
||||||
}
|
}
|
||||||
match SslStream::new(&ctx, stream) {
|
match SslStream::new(&ctx, stream) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
|
|
@ -109,8 +109,8 @@ fn test_verify_trusted_callback_override_bad() {
|
||||||
let mut ctx = SslContext::new(Sslv23).unwrap();
|
let mut ctx = SslContext::new(Sslv23).unwrap();
|
||||||
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
|
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
|
||||||
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
||||||
None => {}
|
Ok(_) => {}
|
||||||
Some(err) => panic!("Unexpected error {:?}", err)
|
Err(err) => panic!("Unexpected error {:?}", err)
|
||||||
}
|
}
|
||||||
assert!(SslStream::new(&ctx, stream).is_err());
|
assert!(SslStream::new(&ctx, stream).is_err());
|
||||||
}
|
}
|
||||||
|
|
@ -137,8 +137,8 @@ fn test_verify_trusted_get_error_ok() {
|
||||||
let mut ctx = SslContext::new(Sslv23).unwrap();
|
let mut ctx = SslContext::new(Sslv23).unwrap();
|
||||||
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
|
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
|
||||||
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
|
||||||
None => {}
|
Ok(_) => {}
|
||||||
Some(err) => panic!("Unexpected error {:?}", err)
|
Err(err) => panic!("Unexpected error {:?}", err)
|
||||||
}
|
}
|
||||||
assert!(SslStream::new(&ctx, stream).is_ok());
|
assert!(SslStream::new(&ctx, stream).is_ok());
|
||||||
}
|
}
|
||||||
|
|
@ -200,10 +200,10 @@ fn test_set_certificate_and_private_key() {
|
||||||
let cert = X509::from_pem(&mut cert_file).unwrap();
|
let cert = X509::from_pem(&mut cert_file).unwrap();
|
||||||
|
|
||||||
let mut ctx = SslContext::new(Sslv23).unwrap();
|
let mut ctx = SslContext::new(Sslv23).unwrap();
|
||||||
ctx.set_private_key(&key);
|
ctx.set_private_key(&key).unwrap();
|
||||||
ctx.set_certificate(&cert);
|
ctx.set_certificate(&cert).unwrap();
|
||||||
|
|
||||||
assert!(ctx.check_private_key().is_none());
|
assert!(ctx.check_private_key().is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue