Skip to content

Add X509_NAME_hash_ex to X509NameRef #2373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions openssl-sys/src/handwritten/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ extern "C" {

pub fn X509_NAME_new() -> *mut X509_NAME;
pub fn X509_NAME_cmp(x: *const X509_NAME, y: *const X509_NAME) -> c_int;
#[cfg(ossl300)]
pub fn X509_NAME_hash_ex(
x: *const X509_NAME,
ctx: *mut OSSL_LIB_CTX,
propq: *const c_char,
ok: *mut c_int,
) -> c_ulong;

#[cfg(not(ossl300))]
pub fn X509_NAME_hash(x: *mut X509_NAME) -> c_ulong;
pub fn X509_NAME_free(x: *mut X509_NAME);

pub fn X509_new() -> *mut X509;
Expand Down
47 changes: 47 additions & 0 deletions openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use crate::conf::ConfRef;
use crate::error::ErrorStack;
use crate::ex_data::Index;
use crate::hash::{DigestBytes, MessageDigest};
#[cfg(ossl300)]
use crate::lib_ctx::LibCtxRef;
use crate::nid::Nid;
use crate::pkey::{HasPrivate, HasPublic, PKey, PKeyRef, Public};
use crate::ssl::SslRef;
Expand Down Expand Up @@ -1218,6 +1220,51 @@ impl Stackable for X509Name {
}

impl X509NameRef {
/// Returns the hash of the X509 name
#[cfg(ossl300)]
#[corresponds(X509_NAME_hash_ex)]
pub fn hash_ex(&self, ctx: Option<LibCtxRef>, propq: Option<&str>) -> Result<u32, ErrorStack> {
let ctx_ref = ctx.map_or(ptr::null_mut(), |ctx| ctx.as_ptr());

let cstr;
let propq = if let Some(propq) = propq {
cstr = CString::new(propq).unwrap();
cstr.as_ptr()
} else {
ptr::null()
};

let mut ok: c_int = 0;
let hash;

#[allow(clippy::unnecessary_cast)]
unsafe {
hash = ffi::X509_NAME_hash_ex(self.as_ptr(), ctx_ref, propq, &mut ok) as u32;
}

if ok != 1 {
return Err(ErrorStack::get());
}

Ok(hash)
}

/// Returns the hash of the X509 name
#[cfg(ossl300)]
#[corresponds(X509_NAME_hash)]
pub fn hash(&self) -> u32 {
self.hash_ex(None, None).unwrap_or(0)
}

#[cfg(not(ossl300))]
#[corresponds(X509_NAME_hash)]
pub fn hash(&self) -> u32 {
#[allow(clippy::unnecessary_cast)]
unsafe {
ffi::X509_NAME_hash(self.as_ptr()) as u32
}
}

/// Returns the name entries by the nid.
pub fn entries_by_nid(&self, nid: Nid) -> X509NameEntries<'_> {
X509NameEntries {
Expand Down
2 changes: 2 additions & 0 deletions openssl/src/x509/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ fn test_load_crl() {
let crl = include_bytes!("../../test/test.crl");
let crl = X509Crl::from_der(crl).unwrap();
assert!(crl.verify(&ca.public_key().unwrap()).unwrap());
let issuer = crl.issuer_name();
assert_eq!(format!("{:08x}", issuer.hash()), "f88ed8fa");

let cert = include_bytes!("../../test/subca.crt");
let cert = X509::from_pem(cert).unwrap();
Expand Down