Skip to content

Commit a6baece

Browse files
authored
x509: add Store API (#9411)
* Add x509.verification.Store One of many sub-breakouts. Signed-off-by: William Woodruff <[email protected]> * lib: actually load the new verify module Signed-off-by: William Woodruff <[email protected]> * fix hints, add initial store tests Signed-off-by: William Woodruff <[email protected]> * verify: use `any` instead of for-if loop Signed-off-by: William Woodruff <[email protected]> * verify: mark Store as frozen Signed-off-by: William Woodruff <[email protected]> * verify: don't use an interior PyList Signed-off-by: William Woodruff <[email protected]> * verify: don't overthink the types Signed-off-by: William Woodruff <[email protected]> * verification: __all__ Signed-off-by: William Woodruff <[email protected]> * verification: relocate __all__ Signed-off-by: William Woodruff <[email protected]> --------- Signed-off-by: William Woodruff <[email protected]>
1 parent c6d7bdf commit a6baece

File tree

7 files changed

+66
-1
lines changed

7 files changed

+66
-1
lines changed

src/cryptography/hazmat/bindings/_rust/x509.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ class Certificate: ...
4040
class RevokedCertificate: ...
4141
class CertificateRevocationList: ...
4242
class CertificateSigningRequest: ...
43+
44+
class Store:
45+
def __init__(self, certs: list[x509.Certificate]) -> None: ...

src/cryptography/x509/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from cryptography.x509 import certificate_transparency
7+
from cryptography.x509 import certificate_transparency, verification
88
from cryptography.x509.base import (
99
Attribute,
1010
AttributeNotFound,
@@ -179,6 +179,7 @@
179179
"load_pem_x509_crl",
180180
"load_der_x509_crl",
181181
"random_serial_number",
182+
"verification",
182183
"Attribute",
183184
"AttributeNotFound",
184185
"Attributes",

src/cryptography/x509/verification.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
from cryptography.hazmat.bindings._rust import x509 as rust_x509
6+
7+
__all__ = ["Store"]
8+
9+
Store = rust_x509.Store

src/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ fn _rust(py: pyo3::Python<'_>, m: &pyo3::types::PyModule) -> pyo3::PyResult<()>
153153
crate::x509::crl::add_to_module(x509_mod)?;
154154
crate::x509::csr::add_to_module(x509_mod)?;
155155
crate::x509::sct::add_to_module(x509_mod)?;
156+
crate::x509::verify::add_to_module(x509_mod)?;
156157
m.add_submodule(x509_mod)?;
157158

158159
let ocsp_mod = pyo3::prelude::PyModule::new(py, "ocsp")?;

src/rust/src/x509/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) mod ocsp_req;
1212
pub(crate) mod ocsp_resp;
1313
pub(crate) mod sct;
1414
pub(crate) mod sign;
15+
pub(crate) mod verify;
1516

1617
pub(crate) use common::{
1718
datetime_to_py, find_in_pem, parse_and_cache_extensions, parse_general_name,

src/rust/src/x509/verify.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 crate::x509::certificate::Certificate as PyCertificate;
6+
7+
#[pyo3::pyclass(
8+
frozen,
9+
name = "Store",
10+
module = "cryptography.hazmat.bindings._rust.x509"
11+
)]
12+
struct PyStore(Vec<pyo3::Py<PyCertificate>>);
13+
14+
#[pyo3::pymethods]
15+
impl PyStore {
16+
#[new]
17+
fn new(certs: Vec<pyo3::Py<PyCertificate>>) -> pyo3::PyResult<Self> {
18+
Ok(Self(certs))
19+
}
20+
}
21+
22+
pub(crate) fn add_to_module(module: &pyo3::prelude::PyModule) -> pyo3::PyResult<()> {
23+
module.add_class::<PyStore>()?;
24+
25+
Ok(())
26+
}

tests/x509/test_verification.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import os
6+
7+
import pytest
8+
9+
from cryptography import x509
10+
from cryptography.x509.verification import Store
11+
from tests.x509.test_x509 import _load_cert
12+
13+
14+
class TestStore:
15+
def test_store_rejects_non_certificates(self):
16+
with pytest.raises(TypeError):
17+
Store(["not a cert"]) # type: ignore[list-item]
18+
19+
def test_store_initializes(self):
20+
cert = _load_cert(
21+
os.path.join("x509", "cryptography.io.pem"),
22+
x509.load_pem_x509_certificate,
23+
)
24+
assert Store([cert]) is not None

0 commit comments

Comments
 (0)