-
-
Notifications
You must be signed in to change notification settings - Fork 768
Add OCSP nonce functionality #1046
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6676a2
Add implementation of OCSP nonce functions.
scolby33 7d5ec83
Add test for creation of OCSP request.
scolby33 5e06a9a
Add custom return/error type for ocsp::check_nonce.
scolby33 8792ac6
Fix signature of ocsp::BasicResponseRef::copy_nonce.
scolby33 d688e83
Add Error::description implementation as required by older versions o…
scolby33 fc795f0
Merge branch 'master' into add-ocsp-nonce
scolby33 c238645
cargo fmt fixes
scolby33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use ffi; | ||
use foreign_types::ForeignTypeRef; | ||
use libc::{c_int, c_long, c_ulong}; | ||
use std::error::Error; | ||
use std::fmt; | ||
use std::mem; | ||
use std::ptr; | ||
|
||
|
@@ -28,6 +30,51 @@ bitflags! { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum OcspNonceCheckSuccessResult { | ||
PresentAndEqual, // 1 | ||
Absent, // 2 | ||
PresentInResponseOnly, // 3 | ||
} | ||
|
||
impl fmt::Display for OcspNonceCheckSuccessResult { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let message = match *self { | ||
OcspNonceCheckSuccessResult::PresentAndEqual => "Nonces Present and Equal", | ||
OcspNonceCheckSuccessResult::Absent => "Nonces Absent from Request and Response", | ||
OcspNonceCheckSuccessResult::PresentInResponseOnly => "Nonce Present in Response Only", | ||
}; | ||
write!(f, "{}", message) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: should |
||
pub enum OcspNonceCheckErrorResult { | ||
PresentAndUnequal, // 0 | ||
PresentInRequestOnly, // -1 | ||
Unknown(ErrorStack), // any other return value | ||
} | ||
|
||
impl fmt::Display for OcspNonceCheckErrorResult { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
OcspNonceCheckErrorResult::PresentAndUnequal => { | ||
write!(f, "Nonces Present and Unequal!") | ||
} | ||
OcspNonceCheckErrorResult::PresentInRequestOnly => { | ||
write!(f, "Nonce Present in Request Only!") | ||
} | ||
OcspNonceCheckErrorResult::Unknown(ref err) => err.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl Error for OcspNonceCheckErrorResult { | ||
fn description(&self) -> &str { | ||
"an error in OCSP nonce checking" | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub struct OcspResponseStatus(c_int); | ||
|
||
|
@@ -206,6 +253,24 @@ impl OcspBasicResponseRef { | |
} | ||
} | ||
} | ||
|
||
pub fn add_nonce(&mut self, val: Option<&[u8]>) -> Result<(), ErrorStack> { | ||
unsafe { | ||
let (ptr, len) = match val { | ||
Some(slice) => (slice.as_ptr() as *mut _, slice.len() as c_int), | ||
None => (ptr::null_mut(), 0), | ||
}; | ||
cvt(ffi::OCSP_basic_add1_nonce(self.as_ptr(), ptr, len))?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn copy_nonce(&mut self, req: &OcspRequestRef) -> Result<(), ErrorStack> { | ||
unsafe { | ||
cvt(ffi::OCSP_copy_nonce(self.as_ptr(), req.as_ptr()))?; | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
foreign_type_and_impl_send_sync! { | ||
|
@@ -344,6 +409,17 @@ impl OcspRequestRef { | |
Ok(OcspOneReqRef::from_ptr_mut(ptr)) | ||
} | ||
} | ||
|
||
pub fn add_nonce(&mut self, val: Option<&[u8]>) -> Result<(), ErrorStack> { | ||
unsafe { | ||
let (ptr, len) = match val { | ||
Some(slice) => (slice.as_ptr() as *mut _, slice.len() as c_int), | ||
None => (ptr::null_mut(), 0), | ||
}; | ||
cvt(ffi::OCSP_request_add1_nonce(self.as_ptr(), ptr, len))?; | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
foreign_type_and_impl_send_sync! { | ||
|
@@ -353,3 +429,53 @@ foreign_type_and_impl_send_sync! { | |
pub struct OcspOneReq; | ||
pub struct OcspOneReqRef; | ||
} | ||
|
||
pub fn check_nonce( | ||
req: &OcspRequestRef, | ||
bs: &OcspBasicResponseRef, | ||
) -> Result<OcspNonceCheckSuccessResult, OcspNonceCheckErrorResult> { | ||
unsafe { | ||
match ffi::OCSP_check_nonce(req.as_ptr(), bs.as_ptr()) { | ||
// good cases | ||
1 => Ok(OcspNonceCheckSuccessResult::PresentAndEqual), | ||
2 => Ok(OcspNonceCheckSuccessResult::Absent), | ||
3 => Ok(OcspNonceCheckSuccessResult::PresentInResponseOnly), | ||
// bad cases | ||
0 => Err(OcspNonceCheckErrorResult::PresentAndUnequal), | ||
-1 => Err(OcspNonceCheckErrorResult::PresentInRequestOnly), | ||
_ => Err(OcspNonceCheckErrorResult::Unknown(ErrorStack::get())), //something else! | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use hex::FromHex; | ||
|
||
use super::*; | ||
use hash::MessageDigest; | ||
use x509::X509; | ||
|
||
#[test] | ||
fn test_create_ocsp_request() { | ||
let subject = include_bytes!("../test/cert.pem"); | ||
let subject = X509::from_pem(subject).unwrap(); | ||
let issuer = include_bytes!("../test/root-ca.pem"); | ||
let issuer = X509::from_pem(issuer).unwrap(); | ||
|
||
let req_der = include_bytes!("../test/ocsp-req.der"); | ||
let req_nonce_der = include_bytes!("../test/ocsp-req-nonce.der"); | ||
|
||
let cert_id = OcspCertId::from_cert(MessageDigest::sha1(), &subject, &issuer).unwrap(); | ||
|
||
let mut req = OcspRequest::new().unwrap(); | ||
req.add_id(cert_id).unwrap(); | ||
|
||
assert_eq!(&*req.to_der().unwrap(), req_der.as_ref()); | ||
|
||
let nonce = Vec::from_hex("4413A2C5019A7C3A384CDD8AB30E3816").unwrap(); | ||
req.add_nonce(Some(&nonce)).unwrap(); | ||
|
||
assert_eq!(&*req.to_der().unwrap(), req_nonce_der.as_ref()); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ought I derive
Copy, Clone, PartialEq, Eq
as well?