discord_cassandra_cpp/cassandra/
ssl.rs

1use crate::cassandra::error::*;
2use crate::cassandra::util::{Protected, ProtectedInner};
3
4use crate::cassandra_sys::cass_ssl_add_trusted_cert_n;
5use crate::cassandra_sys::cass_ssl_free;
6use crate::cassandra_sys::cass_ssl_new;
7use crate::cassandra_sys::cass_ssl_set_cert_n;
8use crate::cassandra_sys::cass_ssl_set_private_key_n;
9use crate::cassandra_sys::cass_ssl_set_verify_flags;
10use crate::cassandra_sys::CassSsl as _Ssl;
11use crate::cassandra_sys::CassSslVerifyFlags;
12
13use std::os::raw::c_char;
14
15/// The individual SSL verification levels.
16#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
17#[allow(missing_docs)] // Meanings are defined in CQL documentation.
18#[allow(non_camel_case_types)] // Names are traditional.
19pub enum SslVerifyFlag {
20    NONE,
21    PEER_CERT,
22    PEER_IDENTITY,
23    PEER_IDENTITY_DNS,
24}
25
26enhance_nullary_enum!(SslVerifyFlag, CassSslVerifyFlags, {
27    (NONE, CASS_SSL_VERIFY_NONE, "NONE"),
28    (PEER_CERT, CASS_SSL_VERIFY_PEER_CERT, "PEER_CERT"),
29    (PEER_IDENTITY, CASS_SSL_VERIFY_PEER_IDENTITY, "PEER_IDENTITY"),
30    (PEER_IDENTITY_DNS, CASS_SSL_VERIFY_PEER_IDENTITY_DNS, "PEER_IDENTITY_DNS"),
31});
32
33fn to_bitset(flags: &[SslVerifyFlag]) -> i32 {
34    let mut res = 0;
35    for f in flags.iter() {
36        res = res | f.inner() as u32;
37    }
38    res as i32
39}
40
41/// Describes the SSL configuration of a cluster.
42#[derive(Debug)]
43pub struct Ssl(*mut _Ssl);
44
45// The underlying C type has no thread-local state, but does not support access
46// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
47unsafe impl Send for Ssl {}
48
49impl ProtectedInner<*mut _Ssl> for Ssl {
50    fn inner(&self) -> *mut _Ssl {
51        self.0
52    }
53}
54
55impl Protected<*mut _Ssl> for Ssl {
56    fn build(inner: *mut _Ssl) -> Self {
57        if inner.is_null() {
58            panic!("Unexpected null pointer")
59        };
60        Ssl(inner)
61    }
62}
63
64impl Drop for Ssl {
65    /// Frees a SSL context instance.
66    fn drop(&mut self) {
67        unsafe { cass_ssl_free(self.0) }
68    }
69}
70
71impl Default for Ssl {
72    /// Creates a new SSL context.
73    fn default() -> Ssl {
74        unsafe { Ssl(cass_ssl_new()) }
75    }
76}
77
78impl Ssl {
79    /// Adds a trusted certificate. This is used to verify
80    /// the peer's certificate.
81    pub fn add_trusted_cert(&mut self, cert: impl AsRef<str>) -> Result<&mut Self> {
82        let cert = cert.as_ref();
83        unsafe {
84            let cert_ptr = cert.as_ptr() as *const c_char;
85            cass_ssl_add_trusted_cert_n(self.0, cert_ptr, cert.len()).to_result(self)
86        }
87    }
88
89    /// Sets verification performed on the peer's certificate.
90    ///
91    /// CASS_SSL_VERIFY_NONE - No verification is performed
92    ///
93    /// CASS_SSL_VERIFY_PEER_CERT - Certificate is present and valid
94    ///
95    /// CASS_SSL_VERIFY_PEER_IDENTITY - IP address matches the certificate's
96    /// common name or one of its subject alternative names. This implies the
97    /// certificate is also present.
98    ///
99    /// <b>Default:</b> CASS_SSL_VERIFY_PEER_CERT
100    pub fn set_verify_flags(&mut self, flags: &[SslVerifyFlag]) {
101        unsafe { cass_ssl_set_verify_flags(self.0, to_bitset(flags)) }
102    }
103
104    /// Set client-side certificate chain. This is used to authenticate
105    /// the client on the server-side. This should contain the entire
106    /// Certificate chain starting with the certificate itself.
107    pub fn set_cert(&mut self, cert: &str) -> Result<&mut Self> {
108        unsafe {
109            let cert_ptr = cert.as_ptr() as *const c_char;
110            cass_ssl_set_cert_n(self.0, cert_ptr, cert.len()).to_result(self)
111        }
112    }
113
114    /// Set client-side private key. This is used to authenticate
115    /// the client on the server-side.
116    pub fn set_private_key(&mut self, key: &str, password: &str) -> Result<&mut Self> {
117        unsafe {
118            let key_ptr = key.as_ptr() as *const c_char;
119            let password_ptr = key.as_ptr() as *const c_char;
120            cass_ssl_set_private_key_n(self.0, key_ptr, key.len(), password_ptr, password.len())
121                .to_result(self)
122        }
123    }
124}