Skip to content

Commit

Permalink
Merge pull request #1848 from alex/expose-lib-reason
Browse files Browse the repository at this point in the history
Expose the raw library and reason codes on Error
  • Loading branch information
sfackler committed Mar 19, 2023
2 parents 9ea51ec + 4bc21b0 commit e62129f
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions openssl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ impl Error {
self.line,
self.func.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
);
ffi::ERR_set_error(
ffi::ERR_GET_LIB(self.code),
ffi::ERR_GET_REASON(self.code),
ptr::null(),
);
ffi::ERR_set_error(self.library_code(), self.reason_code(), ptr::null());
}
}

Expand All @@ -214,9 +210,9 @@ impl Error {
let line = self.line.try_into().unwrap();
unsafe {
ffi::ERR_put_error(
ffi::ERR_GET_LIB(self.code),
self.library_code(),
ffi::ERR_GET_FUNC(self.code),
ffi::ERR_GET_REASON(self.code),
self.reason_code(),
self.file.as_ptr(),
line,
);
Expand All @@ -240,6 +236,15 @@ impl Error {
}
}

/// Returns the raw OpenSSL error constant for the library reporting the
/// error.
// On BoringSSL ERR_GET_{LIB,FUNC,REASON} are `unsafe`, but on
// OpenSSL/LibreSSL they're safe.
#[allow(unused_unsafe)]
pub fn library_code(&self) -> libc::c_int {
unsafe { ffi::ERR_GET_LIB(self.code) }
}

/// Returns the name of the function reporting the error.
pub fn function(&self) -> Option<RetStr<'_>> {
self.func.as_ref().map(|s| s.as_str())
Expand All @@ -257,6 +262,14 @@ impl Error {
}
}

/// Returns the raw OpenSSL error constant for the reason for the error.
// On BoringSSL ERR_GET_{LIB,FUNC,REASON} are `unsafe`, but on
// OpenSSL/LibreSSL they're safe.
#[allow(unused_unsafe)]
pub fn reason_code(&self) -> libc::c_int {
unsafe { ffi::ERR_GET_REASON(self.code) }
}

/// Returns the name of the source file which encountered the error.
pub fn file(&self) -> RetStr<'_> {
self.file.as_str()
Expand Down Expand Up @@ -304,17 +317,15 @@ impl fmt::Display for Error {
write!(fmt, "error:{:08X}", self.code())?;
match self.library() {
Some(l) => write!(fmt, ":{}", l)?,
None => write!(fmt, ":lib({})", unsafe { ffi::ERR_GET_LIB(self.code()) })?,
None => write!(fmt, ":lib({})", self.library_code())?,
}
match self.function() {
Some(f) => write!(fmt, ":{}", f)?,
None => write!(fmt, ":func({})", unsafe { ffi::ERR_GET_FUNC(self.code()) })?,
}
match self.reason() {
Some(r) => write!(fmt, ":{}", r)?,
None => write!(fmt, ":reason({})", unsafe {
ffi::ERR_GET_REASON(self.code())
})?,
None => write!(fmt, ":reason({})", self.reason_code())?,
}
write!(
fmt,
Expand Down Expand Up @@ -387,3 +398,18 @@ cfg_if! {
}
}
}

#[cfg(test)]
mod tests {
use crate::nid::Nid;

#[test]
fn test_error_library_code() {
let stack = Nid::create("not-an-oid", "invalid", "invalid").unwrap_err();
let errors = stack.errors();
#[cfg(not(boringssl))]
assert_eq!(errors[0].library_code(), ffi::ERR_LIB_ASN1);
#[cfg(boringssl)]
assert_eq!(errors[0].library_code(), ffi::ERR_LIB_OBJ as libc::c_int);
}
}

0 comments on commit e62129f

Please sign in to comment.