aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/x509/mod.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2018-03-11 13:37:21 -0700
committerGitHub <[email protected]>2018-03-11 13:37:21 -0700
commit00359a1a55898629eb4b068ae68e9bc4e38740f9 (patch)
tree2dea94c87e87dfd062debcb0fcc777319d6b87c9 /openssl/src/x509/mod.rs
parentMerge pull request #870 from sfackler/tweaks (diff)
parentChanges `init` to take a closure which is called with the initialized context (diff)
downloadrust-openssl-00359a1a55898629eb4b068ae68e9bc4e38740f9.tar.xz
rust-openssl-00359a1a55898629eb4b068ae68e9bc4e38740f9.zip
Merge pull request #861 from bkchr/verify_certificate
Implements `X509_verify_cert`
Diffstat (limited to 'openssl/src/x509/mod.rs')
-rw-r--r--openssl/src/x509/mod.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs
index 9638e6a3..146a77b0 100644
--- a/openssl/src/x509/mod.rs
+++ b/openssl/src/x509/mod.rs
@@ -67,6 +67,18 @@ impl X509StoreContext {
pub fn ssl_idx() -> Result<Index<X509StoreContext, SslRef>, ErrorStack> {
unsafe { cvt_n(ffi::SSL_get_ex_data_X509_STORE_CTX_idx()).map(|idx| Index::from_raw(idx)) }
}
+
+ /// Creates a new `X509StoreContext` instance.
+ ///
+ /// This corresponds to [`X509_STORE_CTX_new`].
+ ///
+ /// [`X509_STORE_CTX_new`]: https://www.openssl.org/docs/man1.1.0/crypto/X509_STORE_CTX_new.html
+ pub fn new() -> Result<X509StoreContext, ErrorStack> {
+ unsafe {
+ ffi::init();
+ cvt_p(ffi::X509_STORE_CTX_new()).map(|p| X509StoreContext(p))
+ }
+ }
}
impl X509StoreContextRef {
@@ -95,6 +107,70 @@ impl X509StoreContextRef {
unsafe { X509VerifyResult::from_raw(ffi::X509_STORE_CTX_get_error(self.as_ptr())) }
}
+ /// Initializes this context with the given certificate, certificates chain and certificate
+ /// store. After initializing the context, the `with_context` closure is called with the prepared
+ /// context. As long as the closure is running, the context stays initialized and can be used
+ /// to e.g. verify a certificate. The context will be cleaned up, after the closure finished.
+ ///
+ /// * `trust` - The certificate store with the trusted certificates.
+ /// * `cert` - The certificate that should be verified.
+ /// * `cert_chain` - The certificates chain.
+ /// * `with_context` - The closure that is called with the initialized context.
+ ///
+ /// This corresponds to [`X509_STORE_CTX_init`] before calling `with_context` and to
+ /// [`X509_STORE_CTX_cleanup`] after calling `with_context`.
+ ///
+ /// [`X509_STORE_CTX_init`]: https://www.openssl.org/docs/man1.0.2/crypto/X509_STORE_CTX_init.html
+ /// [`X509_STORE_CTX_cleanup`]: https://www.openssl.org/docs/man1.0.2/crypto/X509_STORE_CTX_cleanup.html
+ pub fn init<F, T>(&mut self, trust: &store::X509StoreRef, cert: &X509Ref,
+ cert_chain: &StackRef<X509>, with_context: F) -> Result<T, ErrorStack>
+ where
+ F: FnOnce(&mut X509StoreContextRef) -> Result<T, ErrorStack>
+ {
+ struct Cleanup<'a>(&'a mut X509StoreContextRef);
+
+ impl<'a> Drop for Cleanup<'a> {
+ fn drop(&mut self) {
+ self.0.cleanup();
+ }
+ }
+
+ unsafe {
+ cvt(ffi::X509_STORE_CTX_init(self.as_ptr(), trust.as_ptr(),
+ cert.as_ptr(), cert_chain.as_ptr()))?;
+
+ let cleanup = Cleanup(self);
+ with_context(cleanup.0)
+ }
+ }
+
+ /// Verifies the stored certificate.
+ /// It is required to call `init` in beforehand, to initialize the required values.
+ ///
+ /// This corresponds to [`X509_verify_cert`].
+ ///
+ /// [`X509_verify_cert`]: https://www.openssl.org/docs/man1.0.2/crypto/X509_verify_cert.html
+ ///
+ /// # Result
+ ///
+ /// The Result must be `Ok(())` to be a valid certificate, otherwise the cert is not valid.
+ pub fn verify_cert(&mut self) -> Result<(), ErrorStack> {
+ unsafe {
+ cvt(ffi::X509_verify_cert(self.as_ptr())).map(|_| ())
+ }
+ }
+
+ /// Cleans-up the context.
+ ///
+ /// This corresponds to [`X509_STORE_CTX_cleanup`].
+ ///
+ /// [`X509_STORE_CTX_cleanup`]: https://www.openssl.org/docs/man1.0.2/crypto/X509_STORE_CTX_cleanup.html
+ fn cleanup(&mut self) {
+ unsafe {
+ ffi::X509_STORE_CTX_cleanup(self.as_ptr());
+ }
+ }
+
/// Set the error code of the context.
///
/// This corresponds to [`X509_STORE_CTX_set_error`].