Switch X509Name over to new borrow setup
The use of actual references enables us to be correct with respect to mutability without needing two structs for the mutable and immutable cases and more deref impls.
This commit is contained in:
parent
cc65318cc4
commit
b3eb8d516c
|
|
@ -32,6 +32,7 @@ pub mod nid;
|
||||||
pub mod ssl;
|
pub mod ssl;
|
||||||
pub mod version;
|
pub mod version;
|
||||||
pub mod x509;
|
pub mod x509;
|
||||||
|
mod opaque;
|
||||||
|
|
||||||
pub fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
|
pub fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
|
||||||
if r.is_null() {
|
if r.is_null() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
use std::cell::UnsafeCell;
|
||||||
|
|
||||||
|
/// This is intended to be used as the inner type for types designed to be pointed to by references
|
||||||
|
/// converted from raw C pointers. It has an `UnsafeCell` internally to inform the compiler about
|
||||||
|
/// aliasability and doesn't implement `Copy`, so it can't be dereferenced.
|
||||||
|
pub struct Opaque(UnsafeCell<()>);
|
||||||
|
|
@ -18,9 +18,10 @@ use bio::{MemBio, MemBioSlice};
|
||||||
use crypto::hash::MessageDigest;
|
use crypto::hash::MessageDigest;
|
||||||
use crypto::pkey::PKey;
|
use crypto::pkey::PKey;
|
||||||
use crypto::rand::rand_bytes;
|
use crypto::rand::rand_bytes;
|
||||||
|
use error::ErrorStack;
|
||||||
use ffi;
|
use ffi;
|
||||||
use nid::Nid;
|
use nid::Nid;
|
||||||
use error::ErrorStack;
|
use opaque::Opaque;
|
||||||
|
|
||||||
#[cfg(ossl10x)]
|
#[cfg(ossl10x)]
|
||||||
use ffi::{
|
use ffi::{
|
||||||
|
|
@ -401,9 +402,11 @@ impl<'a> X509Ref<'a> {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subject_name<'b>(&'b self) -> X509Name<'b> {
|
pub fn subject_name(&self) -> &X509NameRef {
|
||||||
let name = unsafe { ffi::X509_get_subject_name(self.0) };
|
unsafe {
|
||||||
X509Name(name, PhantomData)
|
let name = ffi::X509_get_subject_name(self.0);
|
||||||
|
X509NameRef::from_ptr(name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns this certificate's SAN entries, if they exist.
|
/// Returns this certificate's SAN entries, if they exist.
|
||||||
|
|
@ -534,17 +537,25 @@ impl Drop for X509 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct X509Name<'x>(*mut ffi::X509_NAME, PhantomData<&'x ()>);
|
pub struct X509NameRef(Opaque);
|
||||||
|
|
||||||
|
impl X509NameRef {
|
||||||
|
pub unsafe fn from_ptr<'a>(ptr: *mut ffi::X509_NAME) -> &'a X509NameRef {
|
||||||
|
&*(ptr as *mut _)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_ptr(&self) -> *mut ffi::X509_NAME {
|
||||||
|
self as *const _ as *mut _
|
||||||
|
}
|
||||||
|
|
||||||
impl<'x> X509Name<'x> {
|
|
||||||
pub fn text_by_nid(&self, nid: Nid) -> Option<SslString> {
|
pub fn text_by_nid(&self, nid: Nid) -> Option<SslString> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let loc = ffi::X509_NAME_get_index_by_NID(self.0, nid as c_int, -1);
|
let loc = ffi::X509_NAME_get_index_by_NID(self.as_ptr(), nid as c_int, -1);
|
||||||
if loc == -1 {
|
if loc == -1 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ne = ffi::X509_NAME_get_entry(self.0, loc);
|
let ne = ffi::X509_NAME_get_entry(self.as_ptr(), loc);
|
||||||
if ne.is_null() {
|
if ne.is_null() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue