Merge pull request #572 from sfackler/foreign-types

Switch to foreign_types
This commit is contained in:
Steven Fackler 2017-02-04 08:54:25 -08:00 committed by GitHub
commit 084cf3c66b
21 changed files with 272 additions and 150 deletions

View File

@ -19,6 +19,7 @@ v110 = []
[dependencies] [dependencies]
bitflags = "0.7" bitflags = "0.7"
foreign-types = "0.1"
lazy_static = "0.2" lazy_static = "0.2"
libc = "0.2" libc = "0.2"
openssl-sys = { version = "0.9.6", path = "../openssl-sys" } openssl-sys = { version = "0.9.6", path = "../openssl-sys" }

View File

@ -1,4 +1,5 @@
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::{c_long, c_char}; use libc::{c_long, c_char};
use std::fmt; use std::fmt;
use std::ptr; use std::ptr;
@ -8,10 +9,15 @@ use std::str;
use {cvt, cvt_p}; use {cvt, cvt_p};
use bio::MemBio; use bio::MemBio;
use error::ErrorStack; use error::ErrorStack;
use types::{OpenSslType, OpenSslTypeRef};
use string::OpensslString; use string::OpensslString;
type_!(Asn1GeneralizedTime, Asn1GeneralizedTimeRef, ffi::ASN1_GENERALIZEDTIME, ffi::ASN1_GENERALIZEDTIME_free); foreign_type! {
type CType = ffi::ASN1_GENERALIZEDTIME;
fn drop = ffi::ASN1_GENERALIZEDTIME_free;
pub struct Asn1GeneralizedTime;
pub struct Asn1GeneralizedTimeRef;
}
impl fmt::Display for Asn1GeneralizedTimeRef { impl fmt::Display for Asn1GeneralizedTimeRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -23,7 +29,13 @@ impl fmt::Display for Asn1GeneralizedTimeRef {
} }
} }
type_!(Asn1Time, Asn1TimeRef, ffi::ASN1_TIME, ffi::ASN1_TIME_free); foreign_type! {
type CType = ffi::ASN1_TIME;
fn drop = ffi::ASN1_TIME_free;
pub struct Asn1Time;
pub struct Asn1TimeRef;
}
impl fmt::Display for Asn1TimeRef { impl fmt::Display for Asn1TimeRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -51,7 +63,13 @@ impl Asn1Time {
} }
} }
type_!(Asn1String, Asn1StringRef, ffi::ASN1_STRING, ffi::ASN1_STRING_free); foreign_type! {
type CType = ffi::ASN1_STRING;
fn drop = ffi::ASN1_STRING_free;
pub struct Asn1String;
pub struct Asn1StringRef;
}
impl Asn1StringRef { impl Asn1StringRef {
pub fn as_utf8(&self) -> Result<OpensslString, ErrorStack> { pub fn as_utf8(&self) -> Result<OpensslString, ErrorStack> {

View File

@ -1,4 +1,5 @@
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::c_int; use libc::c_int;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ffi::CString; use std::ffi::CString;
@ -8,7 +9,6 @@ use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub, Deref};
use {cvt, cvt_p, cvt_n}; use {cvt, cvt_p, cvt_n};
use error::ErrorStack; use error::ErrorStack;
use string::OpensslString; use string::OpensslString;
use types::{OpenSslType, OpenSslTypeRef};
#[cfg(ossl10x)] #[cfg(ossl10x)]
use ffi::{get_rfc2409_prime_768 as BN_get_rfc2409_prime_768, use ffi::{get_rfc2409_prime_768 as BN_get_rfc2409_prime_768,
@ -40,7 +40,13 @@ pub const MSB_ONE: MsbOption = MsbOption(0);
/// of bits in the original numbers. /// of bits in the original numbers.
pub const TWO_MSB_ONE: MsbOption = MsbOption(1); pub const TWO_MSB_ONE: MsbOption = MsbOption(1);
type_!(BigNumContext, BigNumContextRef, ffi::BN_CTX, ffi::BN_CTX_free); foreign_type! {
type CType = ffi::BN_CTX;
fn drop = ffi::BN_CTX_free;
pub struct BigNumContext;
pub struct BigNumContextRef;
}
impl BigNumContext { impl BigNumContext {
/// Returns a new `BigNumContext`. /// Returns a new `BigNumContext`.
@ -509,7 +515,13 @@ impl BigNumRef {
} }
} }
type_!(BigNum, BigNumRef, ffi::BIGNUM, ffi::BN_free); foreign_type! {
type CType = ffi::BIGNUM;
fn drop = ffi::BN_free;
pub struct BigNum;
pub struct BigNumRef;
}
impl BigNum { impl BigNum {
/// Creates a new `BigNum` with the value 0. /// Creates a new `BigNum` with the value 0.

View File

@ -1,13 +1,20 @@
use error::ErrorStack; use error::ErrorStack;
use ffi; use ffi;
use foreign_types::ForeignTypeRef;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use {cvt, cvt_p, init}; use {cvt, cvt_p, init};
use bn::BigNum; use bn::BigNum;
use types::OpenSslTypeRef;
type_!(Dh, DhRef, ffi::DH, ffi::DH_free); foreign_type! {
type CType = ffi::DH;
fn drop = ffi::DH_free;
pub struct Dh;
pub struct DhRef;
}
impl DhRef { impl DhRef {
to_pem!(ffi::PEM_write_bio_DHparams); to_pem!(ffi::PEM_write_bio_DHparams);

View File

@ -1,16 +1,22 @@
use error::ErrorStack;
use ffi; use ffi;
use foreign_types::ForeignTypeRef;
use libc::{c_int, c_char, c_void}; use libc::{c_int, c_char, c_void};
use std::fmt; use std::fmt;
use std::ptr; use std::ptr;
use {cvt, cvt_p};
use bio::MemBioSlice; use bio::MemBioSlice;
use bn::BigNumRef; use bn::BigNumRef;
use {cvt, cvt_p}; use error::ErrorStack;
use types::OpenSslTypeRef;
use util::{CallbackState, invoke_passwd_cb_old}; use util::{CallbackState, invoke_passwd_cb_old};
type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free); foreign_type! {
type CType = ffi::DSA;
fn drop = ffi::DSA_free;
pub struct Dsa;
pub struct DsaRef;
}
impl DsaRef { impl DsaRef {
private_key_to_pem!(ffi::PEM_write_bio_DSAPrivateKey); private_key_to_pem!(ffi::PEM_write_bio_DSAPrivateKey);

View File

@ -1,4 +1,5 @@
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use libc::c_int; use libc::c_int;
@ -7,7 +8,6 @@ use {cvt, cvt_n, cvt_p, init};
use bn::{BigNumRef, BigNumContextRef}; use bn::{BigNumRef, BigNumContextRef};
use error::ErrorStack; use error::ErrorStack;
use nid::Nid; use nid::Nid;
use types::{OpenSslType, OpenSslTypeRef};
pub const POINT_CONVERSION_COMPRESSED: PointConversionForm = pub const POINT_CONVERSION_COMPRESSED: PointConversionForm =
PointConversionForm(ffi::point_conversion_form_t::POINT_CONVERSION_COMPRESSED); PointConversionForm(ffi::point_conversion_form_t::POINT_CONVERSION_COMPRESSED);
@ -29,7 +29,13 @@ pub struct PointConversionForm(ffi::point_conversion_form_t);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Asn1Flag(c_int); pub struct Asn1Flag(c_int);
type_!(EcGroup, EcGroupRef, ffi::EC_GROUP, ffi::EC_GROUP_free); foreign_type! {
type CType = ffi::EC_GROUP;
fn drop = ffi::EC_GROUP_free;
pub struct EcGroup;
pub struct EcGroupRef;
}
impl EcGroup { impl EcGroup {
/// Returns the group of a standard named curve. /// Returns the group of a standard named curve.
@ -103,7 +109,13 @@ impl EcGroupRef {
} }
} }
type_!(EcPoint, EcPointRef, ffi::EC_POINT, ffi::EC_POINT_free); foreign_type! {
type CType = ffi::EC_POINT;
fn drop = ffi::EC_POINT_free;
pub struct EcPoint;
pub struct EcPointRef;
}
impl EcPointRef { impl EcPointRef {
/// Computes `a + b`, storing the result in `self`. /// Computes `a + b`, storing the result in `self`.
@ -253,7 +265,13 @@ impl EcPoint {
} }
} }
type_!(EcKey, EcKeyRef, ffi::EC_KEY, ffi::EC_KEY_free); foreign_type! {
type CType = ffi::EC_KEY;
fn drop = ffi::EC_KEY_free;
pub struct EcKey;
pub struct EcKeyRef;
}
impl EcKeyRef { impl EcKeyRef {
private_key_to_pem!(ffi::PEM_write_bio_ECPrivateKey); private_key_to_pem!(ffi::PEM_write_bio_ECPrivateKey);
@ -355,7 +373,14 @@ impl EcKey {
private_key_from_der!(EcKey, ffi::d2i_ECPrivateKey); private_key_from_der!(EcKey, ffi::d2i_ECPrivateKey);
} }
type_!(EcKeyBuilder, EcKeyBuilderRef, ffi::EC_KEY, ffi::EC_KEY_free);
foreign_type! {
type CType = ffi::EC_KEY;
fn drop = ffi::EC_KEY_free;
pub struct EcKeyBuilder;
pub struct EcKeyBuilderRef;
}
impl EcKeyBuilder { impl EcKeyBuilder {
pub fn new() -> Result<EcKeyBuilder, ErrorStack> { pub fn new() -> Result<EcKeyBuilder, ErrorStack> {

View File

@ -2,6 +2,8 @@
#[macro_use] #[macro_use]
extern crate bitflags; extern crate bitflags;
#[macro_use]
extern crate foreign_types;
extern crate libc; extern crate libc;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;

View File

@ -1,45 +1,4 @@
macro_rules! type_ {
($n:ident, $r:ident, $c:path, $d:path) => {
pub struct $n(*mut $c);
impl ::types::OpenSslType for $n {
type CType = $c;
type Ref = $r;
unsafe fn from_ptr(ptr: *mut $c) -> $n {
$n(ptr)
}
}
impl Drop for $n {
fn drop(&mut self) {
unsafe { $d(self.0) }
}
}
impl ::std::ops::Deref for $n {
type Target = $r;
fn deref(&self) -> &$r {
unsafe { ::types::OpenSslTypeRef::from_ptr(self.0) }
}
}
impl ::std::ops::DerefMut for $n {
fn deref_mut(&mut self) -> &mut $r {
unsafe { ::types::OpenSslTypeRef::from_ptr_mut(self.0) }
}
}
pub struct $r(::util::Opaque);
impl ::types::OpenSslTypeRef for $r {
type CType = $c;
}
}
}
macro_rules! private_key_from_pem { macro_rules! private_key_from_pem {
($t:ident, $f:path) => { ($t:ident, $f:path) => {
from_pem_inner!(/// Deserializes a PEM-formatted private key. from_pem_inner!(/// Deserializes a PEM-formatted private key.
@ -161,9 +120,11 @@ macro_rules! to_der_inner {
#[$m] #[$m]
pub fn $n(&self) -> Result<Vec<u8>, ::error::ErrorStack> { pub fn $n(&self) -> Result<Vec<u8>, ::error::ErrorStack> {
unsafe { unsafe {
let len = try!(::cvt($f(::types::OpenSslTypeRef::as_ptr(self), ptr::null_mut()))); let len = try!(::cvt($f(::foreign_types::ForeignTypeRef::as_ptr(self),
ptr::null_mut())));
let mut buf = vec![0; len as usize]; let mut buf = vec![0; len as usize];
try!(::cvt($f(::types::OpenSslTypeRef::as_ptr(self), &mut buf.as_mut_ptr()))); try!(::cvt($f(::foreign_types::ForeignTypeRef::as_ptr(self),
&mut buf.as_mut_ptr())));
Ok(buf) Ok(buf)
} }
} }

View File

@ -1,4 +1,5 @@
use ffi; use ffi;
use foreign_types::ForeignTypeRef;
use libc::{c_int, c_long, c_ulong}; use libc::{c_int, c_long, c_ulong};
use std::ptr; use std::ptr;
use std::mem; use std::mem;
@ -8,7 +9,6 @@ use asn1::Asn1GeneralizedTimeRef;
use error::ErrorStack; use error::ErrorStack;
use hash::MessageDigest; use hash::MessageDigest;
use stack::StackRef; use stack::StackRef;
use types::OpenSslTypeRef;
use x509::store::X509StoreRef; use x509::store::X509StoreRef;
use x509::{X509, X509Ref}; use x509::{X509, X509Ref};
@ -135,7 +135,13 @@ impl<'a> Status<'a> {
} }
} }
type_!(OcspBasicResponse, OcspBasicResponseRef, ffi::OCSP_BASICRESP, ffi::OCSP_BASICRESP_free); foreign_type! {
type CType = ffi::OCSP_BASICRESP;
fn drop = ffi::OCSP_BASICRESP_free;
pub struct OcspBasicResponse;
pub struct OcspBasicResponseRef;
}
impl OcspBasicResponseRef { impl OcspBasicResponseRef {
/// Verifies the validity of the response. /// Verifies the validity of the response.
@ -189,7 +195,13 @@ impl OcspBasicResponseRef {
} }
} }
type_!(OcspCertId, OcspCertIdRef, ffi::OCSP_CERTID, ffi::OCSP_CERTID_free); foreign_type! {
type CType = ffi::OCSP_CERTID;
fn drop = ffi::OCSP_CERTID_free;
pub struct OcspCertId;
pub struct OcspCertIdRef;
}
impl OcspCertId { impl OcspCertId {
/// Constructs a certificate ID for certificate `subject`. /// Constructs a certificate ID for certificate `subject`.
@ -204,7 +216,13 @@ impl OcspCertId {
} }
} }
type_!(OcspResponse, OcspResponseRef, ffi::OCSP_RESPONSE, ffi::OCSP_RESPONSE_free); foreign_type! {
type CType = ffi::OCSP_RESPONSE;
fn drop = ffi::OCSP_RESPONSE_free;
pub struct OcspResponse;
pub struct OcspResponseRef;
}
impl OcspResponse { impl OcspResponse {
/// Creates an OCSP response from the status and optional body. /// Creates an OCSP response from the status and optional body.
@ -245,7 +263,13 @@ impl OcspResponseRef {
} }
} }
type_!(OcspRequest, OcspRequestRef, ffi::OCSP_REQUEST, ffi::OCSP_REQUEST_free); foreign_type! {
type CType = ffi::OCSP_REQUEST;
fn drop = ffi::OCSP_REQUEST_free;
pub struct OcspRequest;
pub struct OcspRequestRef;
}
impl OcspRequest { impl OcspRequest {
pub fn new() -> Result<OcspRequest, ErrorStack> { pub fn new() -> Result<OcspRequest, ErrorStack> {
@ -271,4 +295,10 @@ impl OcspRequestRef {
} }
} }
type_!(OcspOneReq, OcspOneReqRef, ffi::OCSP_ONEREQ, ffi::OCSP_ONEREQ_free); foreign_type! {
type CType = ffi::OCSP_ONEREQ;
fn drop = ffi::OCSP_ONEREQ_free;
pub struct OcspOneReq;
pub struct OcspOneReqRef;
}

View File

@ -1,6 +1,7 @@
//! PKCS #12 archives. //! PKCS #12 archives.
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::c_int; use libc::c_int;
use std::ptr; use std::ptr;
use std::ffi::CString; use std::ffi::CString;
@ -9,11 +10,16 @@ use {cvt, cvt_p};
use pkey::{PKey, PKeyRef}; use pkey::{PKey, PKeyRef};
use error::ErrorStack; use error::ErrorStack;
use x509::X509; use x509::X509;
use types::{OpenSslType, OpenSslTypeRef};
use stack::Stack; use stack::Stack;
use nid; use nid;
type_!(Pkcs12, Pkcs12Ref, ffi::PKCS12, ffi::PKCS12_free); foreign_type! {
type CType = ffi::PKCS12;
fn drop = ffi::PKCS12_free;
pub struct Pkcs12;
pub struct Pkcs12Ref;
}
impl Pkcs12Ref { impl Pkcs12Ref {
to_der!(ffi::i2d_PKCS12); to_der!(ffi::i2d_PKCS12);

View File

@ -2,6 +2,7 @@ use libc::{c_void, c_char, c_int};
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use ffi; use ffi;
use foreign_types::{Opaque, ForeignType, ForeignTypeRef};
use {cvt, cvt_p}; use {cvt, cvt_p};
use bio::MemBioSlice; use bio::MemBioSlice;
@ -11,9 +12,14 @@ use ec::EcKey;
use rsa::{Rsa, Padding}; use rsa::{Rsa, Padding};
use error::ErrorStack; use error::ErrorStack;
use util::{CallbackState, invoke_passwd_cb_old}; use util::{CallbackState, invoke_passwd_cb_old};
use types::{OpenSslType, OpenSslTypeRef};
type_!(PKey, PKeyRef, ffi::EVP_PKEY, ffi::EVP_PKEY_free); foreign_type! {
type CType = ffi::EVP_PKEY;
fn drop = ffi::EVP_PKEY_free;
pub struct PKey;
pub struct PKeyRef;
}
impl PKeyRef { impl PKeyRef {
/// Returns a copy of the internal RSA key. /// Returns a copy of the internal RSA key.
@ -151,7 +157,7 @@ impl PKey {
} }
} }
pub struct PKeyCtxRef(::util::Opaque); pub struct PKeyCtxRef(Opaque);
impl PKeyCtxRef { impl PKeyCtxRef {
pub fn set_rsa_padding(&mut self, pad: Padding) -> Result<(), ErrorStack> { pub fn set_rsa_padding(&mut self, pad: Padding) -> Result<(), ErrorStack> {
@ -170,7 +176,7 @@ impl PKeyCtxRef {
} }
} }
impl ::types::OpenSslTypeRef for PKeyCtxRef { impl ForeignTypeRef for PKeyCtxRef {
type CType = ffi::EVP_PKEY_CTX; type CType = ffi::EVP_PKEY_CTX;
} }

View File

@ -3,13 +3,13 @@ use std::fmt;
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use libc::{c_int, c_void, c_char}; use libc::{c_int, c_void, c_char};
use foreign_types::ForeignTypeRef;
use {cvt, cvt_p, cvt_n}; use {cvt, cvt_p, cvt_n};
use bn::{BigNum, BigNumRef}; use bn::{BigNum, BigNumRef};
use bio::MemBioSlice; use bio::MemBioSlice;
use error::ErrorStack; use error::ErrorStack;
use util::{CallbackState, invoke_passwd_cb_old}; use util::{CallbackState, invoke_passwd_cb_old};
use types::OpenSslTypeRef;
/// Type of encryption padding to use. /// Type of encryption padding to use.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -29,7 +29,13 @@ pub const NO_PADDING: Padding = Padding(ffi::RSA_NO_PADDING);
pub const PKCS1_PADDING: Padding = Padding(ffi::RSA_PKCS1_PADDING); pub const PKCS1_PADDING: Padding = Padding(ffi::RSA_PKCS1_PADDING);
pub const PKCS1_OAEP_PADDING: Padding = Padding(ffi::RSA_PKCS1_OAEP_PADDING); pub const PKCS1_OAEP_PADDING: Padding = Padding(ffi::RSA_PKCS1_OAEP_PADDING);
type_!(Rsa, RsaRef, ffi::RSA, ffi::RSA_free); foreign_type! {
type CType = ffi::RSA;
fn drop = ffi::RSA_free;
pub struct Rsa;
pub struct RsaRef;
}
impl RsaRef { impl RsaRef {
private_key_to_pem!(ffi::PEM_write_bio_RSAPrivateKey); private_key_to_pem!(ffi::PEM_write_bio_RSAPrivateKey);

View File

@ -62,6 +62,7 @@
//! assert!(memcmp::eq(&hmac, &target)); //! assert!(memcmp::eq(&hmac, &target));
//! ``` //! ```
use ffi; use ffi;
use foreign_types::ForeignTypeRef;
use std::io::{self, Write}; use std::io::{self, Write};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ptr; use std::ptr;
@ -70,7 +71,6 @@ use {cvt, cvt_p};
use hash::MessageDigest; use hash::MessageDigest;
use pkey::{PKeyRef, PKeyCtxRef}; use pkey::{PKeyRef, PKeyCtxRef};
use error::ErrorStack; use error::ErrorStack;
use types::OpenSslTypeRef;
#[cfg(ossl110)] #[cfg(ossl110)]
use ffi::{EVP_MD_CTX_new, EVP_MD_CTX_free}; use ffi::{EVP_MD_CTX_new, EVP_MD_CTX_free};
@ -120,11 +120,11 @@ impl<'a> Signer<'a> {
} }
pub fn pkey_ctx(&self) -> &PKeyCtxRef { pub fn pkey_ctx(&self) -> &PKeyCtxRef {
unsafe { ::types::OpenSslTypeRef::from_ptr(self.pkey_ctx) } unsafe { PKeyCtxRef::from_ptr(self.pkey_ctx) }
} }
pub fn pkey_ctx_mut(&mut self) -> &mut PKeyCtxRef { pub fn pkey_ctx_mut(&mut self) -> &mut PKeyCtxRef {
unsafe { ::types::OpenSslTypeRef::from_ptr_mut(self.pkey_ctx) } unsafe { PKeyCtxRef::from_ptr_mut(self.pkey_ctx) }
} }
pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> { pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {
@ -200,11 +200,11 @@ impl<'a> Verifier<'a> {
} }
pub fn pkey_ctx(&self) -> &PKeyCtxRef { pub fn pkey_ctx(&self) -> &PKeyCtxRef {
unsafe { ::types::OpenSslTypeRef::from_ptr(self.pkey_ctx) } unsafe { PKeyCtxRef::from_ptr(self.pkey_ctx) }
} }
pub fn pkey_ctx_mut(&mut self) -> &mut PKeyCtxRef { pub fn pkey_ctx_mut(&mut self) -> &mut PKeyCtxRef {
unsafe { ::types::OpenSslTypeRef::from_ptr_mut(self.pkey_ctx) } unsafe { PKeyCtxRef::from_ptr_mut(self.pkey_ctx) }
} }
pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> { pub fn update(&mut self, buf: &[u8]) -> Result<(), ErrorStack> {

View File

@ -71,6 +71,7 @@
//! } //! }
//! ``` //! ```
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::{c_int, c_void, c_long, c_ulong}; use libc::{c_int, c_void, c_long, c_ulong};
use libc::{c_uchar, c_uint}; use libc::{c_uchar, c_uint};
use std::any::Any; use std::any::Any;
@ -102,7 +103,6 @@ use x509::store::{X509StoreBuilderRef, X509StoreRef};
use verify::X509VerifyParamRef; use verify::X509VerifyParamRef;
use pkey::PKeyRef; use pkey::PKeyRef;
use error::ErrorStack; use error::ErrorStack;
use types::{OpenSslType, OpenSslTypeRef};
use util::Opaque; use util::Opaque;
use stack::{Stack, StackRef}; use stack::{Stack, StackRef};
@ -966,7 +966,13 @@ impl SslContextBuilder {
} }
} }
type_!(SslContext, SslContextRef, ffi::SSL_CTX, ffi::SSL_CTX_free); foreign_type! {
type CType = ffi::SSL_CTX;
fn drop = ffi::SSL_CTX_free;
pub struct SslContext;
pub struct SslContextRef;
}
unsafe impl Send for SslContext {} unsafe impl Send for SslContext {}
unsafe impl Sync for SslContext {} unsafe impl Sync for SslContext {}
@ -1051,7 +1057,7 @@ pub struct CipherBits {
pub struct SslCipher(*mut ffi::SSL_CIPHER); pub struct SslCipher(*mut ffi::SSL_CIPHER);
impl OpenSslType for SslCipher { impl ForeignType for SslCipher {
type CType = ffi::SSL_CIPHER; type CType = ffi::SSL_CIPHER;
type Ref = SslCipherRef; type Ref = SslCipherRef;
@ -1076,7 +1082,7 @@ impl DerefMut for SslCipher {
pub struct SslCipherRef(Opaque); pub struct SslCipherRef(Opaque);
impl OpenSslTypeRef for SslCipherRef { impl ForeignTypeRef for SslCipherRef {
type CType = ffi::SSL_CIPHER; type CType = ffi::SSL_CIPHER;
} }
@ -1124,7 +1130,13 @@ impl SslCipherRef {
} }
} }
type_!(SslSession, SslSessionRef, ffi::SSL_SESSION, ffi::SSL_SESSION_free); foreign_type! {
type CType = ffi::SSL_SESSION;
fn drop = ffi::SSL_SESSION_free;
pub struct SslSession;
pub struct SslSessionRef;
}
impl SslSessionRef { impl SslSessionRef {
/// Returns the SSL session ID. /// Returns the SSL session ID.
@ -1149,7 +1161,13 @@ impl SslSessionRef {
} }
} }
type_!(Ssl, SslRef, ffi::SSL, ffi::SSL_free); foreign_type! {
type CType = ffi::SSL;
fn drop = ffi::SSL_free;
pub struct Ssl;
pub struct SslRef;
}
impl fmt::Debug for SslRef { impl fmt::Debug for SslRef {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {

View File

@ -174,7 +174,7 @@ macro_rules! run_test(
use hash::MessageDigest; use hash::MessageDigest;
use x509::X509StoreContext; use x509::X509StoreContext;
use hex::FromHex; use hex::FromHex;
use types::OpenSslTypeRef; use foreign_types::ForeignTypeRef;
use super::Server; use super::Server;
#[test] #[test]

View File

@ -1,12 +1,12 @@
use std::ops::{Deref, DerefMut, Index, IndexMut}; use foreign_types::{ForeignTypeRef, ForeignType};
use std::iter; use libc::c_int;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::convert::AsRef; use std::convert::AsRef;
use std::iter;
use std::marker::PhantomData; use std::marker::PhantomData;
use libc::c_int;
use std::mem; use std::mem;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use types::{OpenSslType, OpenSslTypeRef};
use util::Opaque; use util::Opaque;
#[cfg(ossl10x)] #[cfg(ossl10x)]
@ -17,9 +17,8 @@ use ffi::{OPENSSL_sk_pop, OPENSSL_sk_free, OPENSSL_sk_num, OPENSSL_sk_value, OPE
/// Trait implemented by types which can be placed in a stack. /// Trait implemented by types which can be placed in a stack.
/// ///
/// Like `OpenSslType`, it should not be implemented for any type outside /// It should not be implemented for any type outside of this crate.
/// of this crate. pub trait Stackable: ForeignType {
pub trait Stackable: OpenSslType {
/// The C stack type for this element. /// The C stack type for this element.
/// ///
/// Generally called `stack_st_{ELEMENT_TYPE}`, normally hidden by the /// Generally called `stack_st_{ELEMENT_TYPE}`, normally hidden by the
@ -72,7 +71,7 @@ impl<T: Stackable> Borrow<StackRef<T>> for Stack<T> {
} }
} }
impl<T: Stackable> OpenSslType for Stack<T> { impl<T: Stackable> ForeignType for Stack<T> {
type CType = T::StackType; type CType = T::StackType;
type Ref = StackRef<T>; type Ref = StackRef<T>;
@ -140,7 +139,7 @@ impl<T: Stackable> ExactSizeIterator for IntoIter<T> {}
pub struct StackRef<T: Stackable>(Opaque, PhantomData<T>); pub struct StackRef<T: Stackable>(Opaque, PhantomData<T>);
impl<T: Stackable> OpenSslTypeRef for StackRef<T> { impl<T: Stackable> ForeignTypeRef for StackRef<T> {
type CType = T::StackType; type CType = T::StackType;
} }

View File

@ -1,14 +1,20 @@
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::{c_char, c_void}; use libc::{c_char, c_void};
use std::fmt; use std::fmt;
use std::ffi::CStr; use std::ffi::CStr;
use std::ops::Deref; use std::ops::Deref;
use std::str; use std::str;
use types::{OpenSslType, OpenSslTypeRef};
use stack::Stackable; use stack::Stackable;
type_!(OpensslString, OpensslStringRef, c_char, free); foreign_type! {
type CType = c_char;
fn drop = free;
pub struct OpensslString;
pub struct OpensslStringRef;
}
impl OpensslString { impl OpensslString {
#[deprecated(note = "use from_ptr", since = "0.9.7")] #[deprecated(note = "use from_ptr", since = "0.9.7")]

View File

@ -1,40 +1,5 @@
//! Items used by other types. #[deprecated(note = "use foreign_types instead", since = "0.9.7")]
pub use foreign_types::ForeignType as OpenSslType;
/// A type implemented by wrappers over OpenSSL types. #[deprecated(note = "use foreign_types instead", since = "0.9.7")]
/// pub use foreign_types::ForeignTypeRef as OpenSslTypeRef;
/// This should not be implemented by anything outside of this crate; new methods may be added at
/// any time.
pub trait OpenSslType: Sized {
/// The raw C type.
type CType;
/// The type representing a reference to this type.
type Ref: OpenSslTypeRef<CType = Self::CType>;
/// Constructs an instance of this type from its raw type.
unsafe fn from_ptr(ptr: *mut Self::CType) -> Self;
}
/// A trait implemented by types which reference borrowed OpenSSL types.
///
/// This should not be implemented by anything outside of this crate; new methods may be added at
/// any time.
pub trait OpenSslTypeRef: Sized {
/// The raw C type.
type CType;
/// Constructs a shared instance of this type from its raw type.
unsafe fn from_ptr<'a>(ptr: *mut Self::CType) -> &'a Self {
&*(ptr as *mut _)
}
/// Constructs a mutable reference of this type from its raw type.
unsafe fn from_ptr_mut<'a>(ptr: *mut Self::CType) -> &'a mut Self {
&mut *(ptr as *mut _)
}
/// Returns a raw pointer to the wrapped value.
fn as_ptr(&self) -> *mut Self::CType {
self as *const _ as *mut _
}
}

View File

@ -1,9 +1,9 @@
use libc::c_uint; use libc::c_uint;
use ffi; use ffi;
use foreign_types::ForeignTypeRef;
use cvt; use cvt;
use error::ErrorStack; use error::ErrorStack;
use types::OpenSslTypeRef;
bitflags! { bitflags! {
pub flags X509CheckFlags: c_uint { pub flags X509CheckFlags: c_uint {
@ -19,7 +19,13 @@ bitflags! {
} }
} }
type_!(X509VerifyParam, X509VerifyParamRef, ffi::X509_VERIFY_PARAM, ffi::X509_VERIFY_PARAM_free); foreign_type! {
type CType = ffi::X509_VERIFY_PARAM;
fn drop = ffi::X509_VERIFY_PARAM_free;
pub struct X509VerifyParam;
pub struct X509VerifyParamRef;
}
impl X509VerifyParamRef { impl X509VerifyParamRef {
pub fn set_hostflags(&mut self, hostflags: X509CheckFlags) { pub fn set_hostflags(&mut self, hostflags: X509CheckFlags) {

View File

@ -1,3 +1,5 @@
use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::{c_char, c_int, c_long, c_ulong}; use libc::{c_char, c_int, c_long, c_ulong};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::HashMap; use std::collections::HashMap;
@ -17,9 +19,7 @@ use hash::MessageDigest;
use pkey::{PKey, PKeyRef}; use pkey::{PKey, PKeyRef};
use rand::rand_bytes; use rand::rand_bytes;
use error::ErrorStack; use error::ErrorStack;
use ffi;
use nid::Nid; use nid::Nid;
use types::{OpenSslType, OpenSslTypeRef};
use string::OpensslString; use string::OpensslString;
use stack::{Stack, StackRef, Stackable}; use stack::{Stack, StackRef, Stackable};
@ -53,7 +53,13 @@ pub const X509_FILETYPE_PEM: X509FileType = X509FileType(ffi::X509_FILETYPE_PEM)
pub const X509_FILETYPE_ASN1: X509FileType = X509FileType(ffi::X509_FILETYPE_ASN1); pub const X509_FILETYPE_ASN1: X509FileType = X509FileType(ffi::X509_FILETYPE_ASN1);
pub const X509_FILETYPE_DEFAULT: X509FileType = X509FileType(ffi::X509_FILETYPE_DEFAULT); pub const X509_FILETYPE_DEFAULT: X509FileType = X509FileType(ffi::X509_FILETYPE_DEFAULT);
type_!(X509StoreContext, X509StoreContextRef, ffi::X509_STORE_CTX, ffi::X509_STORE_CTX_free); foreign_type! {
type CType = ffi::X509_STORE_CTX;
fn drop = ffi::X509_STORE_CTX_free;
pub struct X509StoreContext;
pub struct X509StoreContextRef;
}
impl X509StoreContextRef { impl X509StoreContextRef {
pub fn error(&self) -> Option<X509VerifyError> { pub fn error(&self) -> Option<X509VerifyError> {
@ -354,7 +360,13 @@ impl X509Generator {
} }
} }
type_!(X509, X509Ref, ffi::X509, ffi::X509_free); foreign_type! {
type CType = ffi::X509;
fn drop = ffi::X509_free;
pub struct X509;
pub struct X509Ref;
}
impl X509Ref { impl X509Ref {
pub fn subject_name(&self) -> &X509NameRef { pub fn subject_name(&self) -> &X509NameRef {
@ -513,7 +525,13 @@ impl Stackable for X509 {
type StackType = ffi::stack_st_X509; type StackType = ffi::stack_st_X509;
} }
type_!(X509Name, X509NameRef, ffi::X509_NAME, ffi::X509_NAME_free); foreign_type! {
type CType = ffi::X509_NAME;
fn drop = ffi::X509_NAME_free;
pub struct X509Name;
pub struct X509NameRef;
}
impl X509Name { impl X509Name {
/// Loads subject names from a file containing PEM-formatted certificates. /// Loads subject names from a file containing PEM-formatted certificates.
@ -567,7 +585,13 @@ impl<'a> Iterator for X509NameEntries<'a> {
} }
} }
type_!(X509NameEntry, X509NameEntryRef, ffi::X509_NAME_ENTRY, ffi::X509_NAME_ENTRY_free); foreign_type! {
type CType = ffi::X509_NAME_ENTRY;
fn drop = ffi::X509_NAME_ENTRY_free;
pub struct X509NameEntry;
pub struct X509NameEntryRef;
}
impl X509NameEntryRef { impl X509NameEntryRef {
pub fn data(&self) -> &Asn1StringRef { pub fn data(&self) -> &Asn1StringRef {
@ -578,7 +602,13 @@ impl X509NameEntryRef {
} }
} }
type_!(X509Req, X509ReqRef, ffi::X509_REQ, ffi::X509_REQ_free); foreign_type! {
type CType = ffi::X509_REQ;
fn drop = ffi::X509_REQ_free;
pub struct X509Req;
pub struct X509ReqRef;
}
impl X509Req { impl X509Req {
/// Reads CSR from PEM /// Reads CSR from PEM
@ -724,7 +754,13 @@ impl X509VerifyError {
} }
} }
type_!(GeneralName, GeneralNameRef, ffi::GENERAL_NAME, ffi::GENERAL_NAME_free); foreign_type! {
type CType = ffi::GENERAL_NAME;
fn drop = ffi::GENERAL_NAME_free;
pub struct GeneralName;
pub struct GeneralNameRef;
}
impl GeneralNameRef { impl GeneralNameRef {
/// Returns the contents of this `GeneralName` if it is a `dNSName`. /// Returns the contents of this `GeneralName` if it is a `dNSName`.

View File

@ -1,12 +1,18 @@
use ffi; use ffi;
use foreign_types::ForeignTypeRef;
use std::mem; use std::mem;
use {cvt, cvt_p}; use {cvt, cvt_p};
use error::ErrorStack; use error::ErrorStack;
use types::OpenSslTypeRef;
use x509::X509; use x509::X509;
type_!(X509StoreBuilder, X509StoreBuilderRef, ffi::X509_STORE, ffi::X509_STORE_free); foreign_type! {
type CType = ffi::X509_STORE;
fn drop = ffi::X509_STORE_free;
pub struct X509StoreBuilder;
pub struct X509StoreBuilderRef;
}
impl X509StoreBuilder { impl X509StoreBuilder {
/// Returns a builder for a certificate store. /// Returns a builder for a certificate store.
@ -50,4 +56,10 @@ impl X509StoreBuilderRef {
} }
} }
type_!(X509Store, X509StoreRef, ffi::X509_STORE, ffi::X509_STORE_free); foreign_type! {
type CType = ffi::X509_STORE;
fn drop = ffi::X509_STORE_free;
pub struct X509Store;
pub struct X509StoreRef;
}