diff --git a/boring/Cargo.toml b/boring/Cargo.toml index 8f550c54..a7eb1a7f 100644 --- a/boring/Cargo.toml +++ b/boring/Cargo.toml @@ -13,12 +13,12 @@ edition = "2018" [dependencies] bitflags = "1.0" -foreign-types = "0.3.1" +foreign-types = "0.5" lazy_static = "1" libc = "0.2" boring-sys = { version = "1.1.0", path = "../boring-sys" } [dev-dependencies] tempdir = "0.3" -hex = "0.3" -rusty-hook = "^0.10.1" +hex = "0.4" +rusty-hook = "^0.11" diff --git a/boring/src/bn.rs b/boring/src/bn.rs index 5b63e63f..7c4e4782 100644 --- a/boring/src/bn.rs +++ b/boring/src/bn.rs @@ -80,7 +80,7 @@ impl BigNumContext { pub fn new() -> Result { unsafe { ffi::init(); - cvt_p(ffi::BN_CTX_new()).map(BigNumContext) + cvt_p(ffi::BN_CTX_new()).map(|p| BigNumContext::from_ptr(p)) } } } diff --git a/boring/src/conf.rs b/boring/src/conf.rs index 1b041b56..660dc26e 100644 --- a/boring/src/conf.rs +++ b/boring/src/conf.rs @@ -1,5 +1,6 @@ //! Interface for processing OpenSSL configuration files. use crate::ffi; +use foreign_types::ForeignType; use libc::c_void; use crate::cvt_p; @@ -34,6 +35,6 @@ foreign_type_and_impl_send_sync! { impl Conf { /// Create a configuration parser. pub fn new(method: ConfMethod) -> Result { - unsafe { cvt_p(ffi::NCONF_new(method.as_ptr())).map(Conf) } + unsafe { cvt_p(ffi::NCONF_new(method.as_ptr())).map(|p| Conf::from_ptr(p)) } } } diff --git a/boring/src/ec.rs b/boring/src/ec.rs index b3824d69..c6d1f139 100644 --- a/boring/src/ec.rs +++ b/boring/src/ec.rs @@ -122,7 +122,7 @@ impl EcGroup { pub fn from_curve_name(nid: Nid) -> Result { unsafe { init(); - cvt_p(ffi::EC_GROUP_new_by_curve_name(nid.as_raw())).map(EcGroup) + cvt_p(ffi::EC_GROUP_new_by_curve_name(nid.as_raw())).map(|p| EcGroup::from_ptr(p)) } } } @@ -421,7 +421,9 @@ impl EcPointRef { /// /// [`EC_POINT_dup`]: https://www.openssl.org/docs/man1.1.0/crypto/EC_POINT_dup.html pub fn to_owned(&self, group: &EcGroupRef) -> Result { - unsafe { cvt_p(ffi::EC_POINT_dup(self.as_ptr(), group.as_ptr())).map(EcPoint) } + unsafe { + cvt_p(ffi::EC_POINT_dup(self.as_ptr(), group.as_ptr())).map(|p| EcPoint::from_ptr(p)) + } } /// Determines if this point is equal to another. @@ -479,7 +481,7 @@ impl EcPoint { /// /// [`EC_POINT_new`]: https://www.openssl.org/docs/man1.1.0/crypto/EC_POINT_new.html pub fn new(group: &EcGroupRef) -> Result { - unsafe { cvt_p(ffi::EC_POINT_new(group.as_ptr())).map(EcPoint) } + unsafe { cvt_p(ffi::EC_POINT_new(group.as_ptr())).map(|p| EcPoint::from_ptr(p)) } } /// Creates point from a binary representation diff --git a/boring/src/macros.rs b/boring/src/macros.rs index 858341ec..68bb8db1 100644 --- a/boring/src/macros.rs +++ b/boring/src/macros.rs @@ -145,19 +145,13 @@ macro_rules! foreign_type_and_impl_send_sync { => { foreign_type! { $(#[$impl_attr])* - type CType = $ctype; - fn drop = $drop; - $(fn clone = $clone;)* $(#[$owned_attr])* - pub struct $owned; - $(#[$borrowed_attr])* - pub struct $borrowed; + pub unsafe type $owned: Send + Sync { + type CType = $ctype; + fn drop = $drop; + $(fn clone = $clone;)* + } } - - unsafe impl Send for $owned{} - unsafe impl Send for $borrowed{} - unsafe impl Sync for $owned{} - unsafe impl Sync for $borrowed{} }; } @@ -177,7 +171,7 @@ macro_rules! generic_foreign_type_and_impl_send_sync { pub struct $owned(*mut $ctype, ::std::marker::PhantomData); $(#[$impl_attr])* - impl ::foreign_types::ForeignType for $owned { + unsafe impl ::foreign_types::ForeignType for $owned { type CType = $ctype; type Ref = $borrowed; @@ -257,7 +251,7 @@ macro_rules! generic_foreign_type_and_impl_send_sync { pub struct $borrowed(::foreign_types::Opaque, ::std::marker::PhantomData); $(#[$impl_attr])* - impl ::foreign_types::ForeignTypeRef for $borrowed { + unsafe impl ::foreign_types::ForeignTypeRef for $borrowed { type CType = $ctype; } diff --git a/boring/src/pkcs12.rs b/boring/src/pkcs12.rs index fa01874d..c4c65d1b 100644 --- a/boring/src/pkcs12.rs +++ b/boring/src/pkcs12.rs @@ -196,7 +196,7 @@ impl Pkcs12Builder { self.mac_iter, keytype, )) - .map(Pkcs12) + .map(|p| Pkcs12::from_ptr(p)) } } } diff --git a/boring/src/ssl/mod.rs b/boring/src/ssl/mod.rs index f47806ef..a52f46ef 100644 --- a/boring/src/ssl/mod.rs +++ b/boring/src/ssl/mod.rs @@ -73,7 +73,7 @@ use std::mem::{self, ManuallyDrop}; use std::ops::{Deref, DerefMut}; use std::panic::resume_unwind; use std::path::Path; -use std::ptr; +use std::ptr::{self, NonNull}; use std::slice; use std::str; use std::sync::{Arc, Mutex}; @@ -1829,7 +1829,7 @@ impl ClientHello { /// Information about a cipher. pub struct SslCipher(*mut ffi::SSL_CIPHER); -impl ForeignType for SslCipher { +unsafe impl ForeignType for SslCipher { type CType = ffi::SSL_CIPHER; type Ref = SslCipherRef; @@ -1863,7 +1863,7 @@ impl DerefMut for SslCipher { /// [`SslCipher`]: struct.SslCipher.html pub struct SslCipherRef(Opaque); -impl ForeignTypeRef for SslCipherRef { +unsafe impl ForeignTypeRef for SslCipherRef { type CType = ffi::SSL_CIPHER; } @@ -1997,7 +1997,7 @@ impl ToOwned for SslSessionRef { fn to_owned(&self) -> SslSession { unsafe { SSL_SESSION_up_ref(self.as_ptr()); - SslSession(self.as_ptr()) + SslSession(NonNull::new_unchecked(self.as_ptr())) } } } diff --git a/boring/src/stack.rs b/boring/src/stack.rs index 1570eb87..5237608e 100644 --- a/boring/src/stack.rs +++ b/boring/src/stack.rs @@ -89,7 +89,7 @@ impl Borrow> for Stack { } } -impl ForeignType for Stack { +unsafe impl ForeignType for Stack { type CType = T::StackType; type Ref = StackRef; @@ -170,7 +170,7 @@ pub struct StackRef(Opaque, PhantomData); unsafe impl Send for StackRef {} unsafe impl Sync for StackRef {} -impl ForeignTypeRef for StackRef { +unsafe impl ForeignTypeRef for StackRef { type CType = T::StackType; } diff --git a/boring/src/x509/mod.rs b/boring/src/x509/mod.rs index 0257b6ea..28b12fd3 100644 --- a/boring/src/x509/mod.rs +++ b/boring/src/x509/mod.rs @@ -67,7 +67,7 @@ impl X509StoreContext { pub fn new() -> Result { unsafe { ffi::init(); - cvt_p(ffi::X509_STORE_CTX_new()).map(X509StoreContext) + cvt_p(ffi::X509_STORE_CTX_new()).map(|p| X509StoreContext::from_ptr(p)) } } } @@ -226,7 +226,7 @@ impl X509Builder { pub fn new() -> Result { unsafe { ffi::init(); - cvt_p(ffi::X509_new()).map(|p| X509Builder(X509(p))) + cvt_p(ffi::X509_new()).map(|p| X509Builder(X509::from_ptr(p))) } } @@ -664,7 +664,7 @@ impl X509 { return Err(ErrorStack::get()); } else { - certs.push(X509(r)); + certs.push(X509::from_ptr(r)); } } @@ -764,7 +764,8 @@ impl X509Extension { let name = name.as_ptr() as *mut _; let value = value.as_ptr() as *mut _; - cvt_p(ffi::X509V3_EXT_nconf(conf, context, name, value)).map(X509Extension) + cvt_p(ffi::X509V3_EXT_nconf(conf, context, name, value)) + .map(|p| X509Extension::from_ptr(p)) } } @@ -789,7 +790,8 @@ impl X509Extension { let name = name.as_raw(); let value = value.as_ptr() as *mut _; - cvt_p(ffi::X509V3_EXT_nconf_nid(conf, context, name, value)).map(X509Extension) + cvt_p(ffi::X509V3_EXT_nconf_nid(conf, context, name, value)) + .map(|p| X509Extension::from_ptr(p)) } } } @@ -802,7 +804,7 @@ impl X509NameBuilder { pub fn new() -> Result { unsafe { ffi::init(); - cvt_p(ffi::X509_NAME_new()).map(|p| X509NameBuilder(X509Name(p))) + cvt_p(ffi::X509_NAME_new()).map(|p| X509NameBuilder(X509Name::from_ptr(p))) } } @@ -1003,7 +1005,7 @@ impl X509ReqBuilder { pub fn new() -> Result { unsafe { ffi::init(); - cvt_p(ffi::X509_REQ_new()).map(|p| X509ReqBuilder(X509Req(p))) + cvt_p(ffi::X509_REQ_new()).map(|p| X509ReqBuilder(X509Req::from_ptr(p))) } } diff --git a/boring/src/x509/store.rs b/boring/src/x509/store.rs index f14470f5..4fc56c85 100644 --- a/boring/src/x509/store.rs +++ b/boring/src/x509/store.rs @@ -35,7 +35,7 @@ //! ``` use crate::ffi; -use foreign_types::ForeignTypeRef; +use foreign_types::{ForeignType, ForeignTypeRef}; use std::mem; use crate::error::ErrorStack; @@ -61,7 +61,7 @@ impl X509StoreBuilder { unsafe { ffi::init(); - cvt_p(ffi::X509_STORE_new()).map(X509StoreBuilder) + cvt_p(ffi::X509_STORE_new()).map(|p| X509StoreBuilder::from_ptr(p)) } }