-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathlib.rs
More file actions
313 lines (285 loc) · 10.3 KB
/
lib.rs
File metadata and controls
313 lines (285 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// This file is dual licensed under the terms of the Apache License, Version
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.
#![deny(rust_2018_idioms, clippy::undocumented_unsafe_blocks)]
#![allow(unknown_lints, non_local_definitions, clippy::result_large_err)]
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
use std::env;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
use openssl::provider;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
use pyo3::PyTypeInfo;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
use crate::error::CryptographyResult;
mod asn1;
mod backend;
mod buf;
mod declarative_asn1;
mod error;
mod exceptions;
pub(crate) mod oid;
mod padding;
mod pkcs12;
mod pkcs7;
mod test_support;
pub(crate) mod types;
mod x509;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
#[pyo3::pyclass(module = "cryptography.hazmat.bindings._rust")]
struct LoadedProviders {
legacy: Option<provider::Provider>,
_default: provider::Provider,
fips: Option<provider::Provider>,
}
#[pyo3::pyfunction]
fn openssl_version() -> i64 {
openssl::version::number()
}
#[pyo3::pyfunction]
fn openssl_version_text() -> &'static str {
openssl::version::version()
}
#[pyo3::pyfunction]
fn is_fips_enabled() -> bool {
cryptography_openssl::fips::is_enabled()
}
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
fn _initialize_providers(py: pyo3::Python<'_>) -> CryptographyResult<LoadedProviders> {
// As of OpenSSL 3.0.0 we must register a legacy cipher provider
// to get RC2 (needed for junk asymmetric private key
// serialization), RC4, Blowfish, IDEA, SEED, etc. These things
// are ugly legacy, but we aren't going to get rid of them
// any time soon.
let load_legacy = !cfg!(CRYPTOGRAPHY_BUILD_OPENSSL_NO_LEGACY)
&& !env::var("CRYPTOGRAPHY_OPENSSL_NO_LEGACY").is_ok_and(|v| !v.is_empty() && v != "0");
let legacy = if load_legacy {
let legacy_result = provider::Provider::load(None, "legacy");
if legacy_result.is_err() {
let message = c"OpenSSL 3's legacy provider failed to load. Legacy algorithms will not be available. If you need those algorithms, check your OpenSSL configuration.";
let warning_cls = pyo3::exceptions::PyWarning::type_object(py);
pyo3::PyErr::warn(py, &warning_cls, message, 1)?;
None
} else {
Some(legacy_result?)
}
} else {
None
};
let _default = provider::Provider::load(None, "default")?;
Ok(LoadedProviders {
legacy,
_default,
fips: None,
})
}
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
// NO-COVERAGE-START
#[pyo3::pyfunction]
// NO-COVERAGE-END
fn enable_fips(providers: &mut LoadedProviders) -> CryptographyResult<()> {
providers.fips = Some(provider::Provider::load(None, "fips")?);
cryptography_openssl::fips::enable()?;
Ok(())
}
#[pyo3::pymodule(gil_used = false)]
mod _rust {
use pyo3::types::PyModuleMethods;
#[pymodule_export]
use crate::asn1::asn1_mod;
#[pymodule_export]
use crate::exceptions::exceptions;
#[pymodule_export]
use crate::oid::ObjectIdentifier;
#[pymodule_export]
use crate::padding::{
ANSIX923PaddingContext, ANSIX923UnpaddingContext, PKCS7PaddingContext,
PKCS7UnpaddingContext,
};
#[pymodule_export]
use crate::pkcs12::pkcs12;
#[pymodule_export]
use crate::pkcs7::pkcs7_mod;
#[pymodule_export]
use crate::test_support::test_support;
#[pyo3::pymodule(gil_used = false)]
mod declarative_asn1 {
#[pymodule_export]
use crate::declarative_asn1::asn1::{decode_der, encode_der};
#[pymodule_export]
use crate::declarative_asn1::types::{
non_root_python_to_rust, AnnotatedType, Annotation, BitString, Encoding,
GeneralizedTime, IA5String, Null, PrintableString, Size, Type, UtcTime, Variant,
};
}
#[pyo3::pymodule(gil_used = false)]
mod x509 {
#[pymodule_export]
use crate::x509::certificate::{
create_x509_certificate, load_der_x509_certificate, load_pem_x509_certificate,
load_pem_x509_certificates, Certificate,
};
#[pymodule_export]
use crate::x509::common::{encode_extension_value, encode_name_bytes};
#[pymodule_export]
use crate::x509::crl::{
create_revoked_certificate, create_x509_crl, load_der_x509_crl, load_pem_x509_crl,
CertificateRevocationList, RevokedCertificate,
};
#[pymodule_export]
use crate::x509::csr::{
create_x509_csr, load_der_x509_csr, load_pem_x509_csr, CertificateSigningRequest,
};
#[pymodule_export]
use crate::x509::sct::Sct;
#[pymodule_export]
use crate::x509::verify::{
PolicyBuilder, PyClientVerifier, PyCriticality, PyCrlRevocationChecker,
PyExtensionPolicy, PyPolicy, PyRevocationChecker, PyServerVerifier, PyStore,
PyVerifiedClient, VerificationError,
};
}
#[pyo3::pymodule(gil_used = false)]
mod ocsp {
#[pymodule_export]
use crate::x509::ocsp_req::{create_ocsp_request, load_der_ocsp_request, OCSPRequest};
#[pymodule_export]
use crate::x509::ocsp_resp::{
create_ocsp_response, load_der_ocsp_response, OCSPResponse, OCSPSingleResponse,
};
}
#[pyo3::pymodule(gil_used = false)]
mod openssl {
use pyo3::prelude::PyModuleMethods;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
#[pymodule_export]
use super::super::enable_fips;
#[pymodule_export]
use super::super::{is_fips_enabled, openssl_version, openssl_version_text};
#[pymodule_export]
use crate::backend::aead::aead;
#[pymodule_export]
use crate::backend::ciphers::ciphers;
#[pymodule_export]
use crate::backend::cmac::cmac;
#[pymodule_export]
use crate::backend::dh::dh;
#[pymodule_export]
use crate::backend::dsa::dsa;
#[pymodule_export]
use crate::backend::ec::ec;
#[pymodule_export]
use crate::backend::ed25519::ed25519;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
#[pymodule_export]
use crate::backend::ed448::ed448;
#[pymodule_export]
use crate::backend::hashes::hashes;
#[pymodule_export]
use crate::backend::hmac::hmac;
#[pymodule_export]
use crate::backend::kdf::kdf;
#[pymodule_export]
use crate::backend::keys::keys;
#[pymodule_export]
use crate::backend::poly1305::poly1305;
#[pymodule_export]
use crate::backend::rsa::rsa;
#[pymodule_export]
use crate::backend::x25519::x25519;
#[cfg(not(any(
CRYPTOGRAPHY_IS_LIBRESSL,
CRYPTOGRAPHY_IS_BORINGSSL,
CRYPTOGRAPHY_IS_AWSLC
)))]
#[pymodule_export]
use crate::backend::x448::x448;
#[pymodule_export]
use crate::error::{capture_error_stack, raise_openssl_error, OpenSSLError};
#[pymodule_export]
const CRYPTOGRAPHY_OPENSSL_309_OR_GREATER: bool = cfg!(CRYPTOGRAPHY_OPENSSL_309_OR_GREATER);
#[pymodule_export]
const CRYPTOGRAPHY_OPENSSL_320_OR_GREATER: bool = cfg!(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER);
#[pymodule_export]
const CRYPTOGRAPHY_OPENSSL_330_OR_GREATER: bool = cfg!(CRYPTOGRAPHY_OPENSSL_330_OR_GREATER);
#[pymodule_export]
const CRYPTOGRAPHY_OPENSSL_350_OR_GREATER: bool = cfg!(CRYPTOGRAPHY_OPENSSL_350_OR_GREATER);
#[pymodule_export]
const CRYPTOGRAPHY_IS_LIBRESSL: bool = cfg!(CRYPTOGRAPHY_IS_LIBRESSL);
#[pymodule_export]
const CRYPTOGRAPHY_IS_BORINGSSL: bool = cfg!(CRYPTOGRAPHY_IS_BORINGSSL);
#[pymodule_export]
const CRYPTOGRAPHY_IS_AWSLC: bool = cfg!(CRYPTOGRAPHY_IS_AWSLC);
#[pymodule_init]
fn init(openssl_mod: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> {
cfg_if::cfg_if! {
if #[cfg(not(any(CRYPTOGRAPHY_IS_LIBRESSL, CRYPTOGRAPHY_IS_BORINGSSL, CRYPTOGRAPHY_IS_AWSLC)))] {
let providers = super::super::_initialize_providers(openssl_mod.py())?;
if providers.legacy.is_some() {
openssl_mod.add("_legacy_provider_loaded", true)?;
} else {
openssl_mod.add("_legacy_provider_loaded", false)?;
}
openssl_mod.add("_providers", providers)?;
} else {
// default value for non-OpenSSL implementations
openssl_mod.add("_legacy_provider_loaded", false)?;
}
}
cfg_if::cfg_if! {
if #[cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)] {
use std::ptr;
use std::cmp::max;
let available = std::thread::available_parallelism().map_or(0, |v| v.get() as u64);
// SAFETY: This sets a libctx provider limit, but we always use the same libctx by passing NULL.
unsafe {
let current = openssl_sys::OSSL_get_max_threads(ptr::null_mut());
// Set the thread limit to the max of available parallelism or current limit.
openssl_sys::OSSL_set_max_threads(ptr::null_mut(), max(available, current));
}
}
}
Ok(())
}
}
#[pymodule_init]
fn init(m: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> {
m.add_submodule(&cryptography_cffi::create_module(m.py())?)?;
Ok(())
}
}