|
| 1 | +// This file is dual licensed under the terms of the Apache License, Version |
| 2 | +// 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | +// for complete details. |
| 4 | + |
| 5 | +use pyo3::types::PyAnyMethods; |
| 6 | + |
| 7 | +use crate::pyopenssl::error::{PyOpenSslError, PyOpenSslResult}; |
| 8 | +use crate::types; |
| 9 | + |
| 10 | +pub(crate) const SSLV23_METHOD: u32 = 3; |
| 11 | +pub(crate) const TLSV1_METHOD: u32 = 4; |
| 12 | +pub(crate) const TLSV1_1_METHOD: u32 = 5; |
| 13 | +pub(crate) const TLSV1_2_METHOD: u32 = 6; |
| 14 | +pub(crate) const TLS_METHOD: u32 = 7; |
| 15 | +pub(crate) const TLS_SERVER_METHOD: u32 = 8; |
| 16 | +pub(crate) const TLS_CLIENT_METHOD: u32 = 9; |
| 17 | +pub(crate) const DTLS_METHOD: u32 = 10; |
| 18 | +pub(crate) const DTLS_SERVER_METHOD: u32 = 11; |
| 19 | +pub(crate) const DTLS_CLIENT_METHOD: u32 = 12; |
| 20 | + |
| 21 | +#[pyo3::pyclass(subclass, module = "OpenSSL.SSL")] |
| 22 | +pub(crate) struct Context { |
| 23 | + ssl_ctx: openssl::ssl::SslContextBuilder, |
| 24 | +} |
| 25 | + |
| 26 | +#[pyo3::pymethods] |
| 27 | +impl Context { |
| 28 | + #[new] |
| 29 | + fn new(method: u32) -> PyOpenSslResult<Self> { |
| 30 | + let (ssl_method, version) = match method { |
| 31 | + SSLV23_METHOD => (openssl::ssl::SslMethod::tls(), None), |
| 32 | + TLSV1_METHOD => ( |
| 33 | + openssl::ssl::SslMethod::tls(), |
| 34 | + Some(openssl::ssl::SslVersion::TLS1), |
| 35 | + ), |
| 36 | + TLSV1_1_METHOD => ( |
| 37 | + openssl::ssl::SslMethod::tls(), |
| 38 | + Some(openssl::ssl::SslVersion::TLS1_1), |
| 39 | + ), |
| 40 | + TLSV1_2_METHOD => ( |
| 41 | + openssl::ssl::SslMethod::tls(), |
| 42 | + Some(openssl::ssl::SslVersion::TLS1_2), |
| 43 | + ), |
| 44 | + TLS_METHOD => (openssl::ssl::SslMethod::tls(), None), |
| 45 | + TLS_SERVER_METHOD => (openssl::ssl::SslMethod::tls_server(), None), |
| 46 | + TLS_CLIENT_METHOD => (openssl::ssl::SslMethod::tls_client(), None), |
| 47 | + DTLS_METHOD => (openssl::ssl::SslMethod::dtls(), None), |
| 48 | + DTLS_SERVER_METHOD => (openssl::ssl::SslMethod::dtls_server(), None), |
| 49 | + DTLS_CLIENT_METHOD => (openssl::ssl::SslMethod::dtls_client(), None), |
| 50 | + _ => { |
| 51 | + return Err(PyOpenSslError::from( |
| 52 | + pyo3::exceptions::PyValueError::new_err("No such protocol"), |
| 53 | + )) |
| 54 | + } |
| 55 | + }; |
| 56 | + let mut ssl_ctx = openssl::ssl::SslContext::builder(ssl_method)?; |
| 57 | + if let Some(version) = version { |
| 58 | + ssl_ctx.set_min_proto_version(Some(version))?; |
| 59 | + ssl_ctx.set_max_proto_version(Some(version))?; |
| 60 | + } |
| 61 | + |
| 62 | + Ok(Context { ssl_ctx }) |
| 63 | + } |
| 64 | + |
| 65 | + #[getter] |
| 66 | + fn _context<'p>(&self, py: pyo3::Python<'p>) -> PyOpenSslResult<pyo3::Bound<'p, pyo3::PyAny>> { |
| 67 | + Ok(types::FFI.get(py)?.call_method1( |
| 68 | + pyo3::intern!(py, "cast"), |
| 69 | + ( |
| 70 | + pyo3::intern!(py, "SSL_CTX *"), |
| 71 | + self.ssl_ctx.as_ptr() as usize, |
| 72 | + ), |
| 73 | + )?) |
| 74 | + } |
| 75 | +} |
0 commit comments