Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rust/src/pkcs7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ where
x509::certificate::Certificate {
raw: raw_cert,
cached_extensions: pyo3::sync::PyOnceLock::new(),
cached_issuer: pyo3::sync::PyOnceLock::new(),
cached_subject: pyo3::sync::PyOnceLock::new(),
},
)?)?;

Expand Down
26 changes: 22 additions & 4 deletions src/rust/src/x509/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ self_cell::self_cell!(
pub(crate) struct Certificate {
pub(crate) raw: OwnedCertificate,
pub(crate) cached_extensions: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
pub(crate) cached_issuer: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
pub(crate) cached_subject: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
}

#[pyo3::pymethods]
Expand Down Expand Up @@ -138,14 +140,28 @@ impl Certificate {

#[getter]
fn issuer<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::PyAny>> {
Ok(x509::parse_name(py, self.raw.borrow_dependent().issuer())
.map_err(|e| e.add_location(asn1::ParseLocation::Field("issuer")))?)
Ok(self
.cached_issuer
.get_or_try_init(py, || {
x509::parse_name(py, self.raw.borrow_dependent().issuer())
.map_err(|e| e.add_location(asn1::ParseLocation::Field("issuer")))
.map(|v| v.unbind())
})?
.bind(py)
.clone())
}

#[getter]
fn subject<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::PyAny>> {
Ok(x509::parse_name(py, self.raw.borrow_dependent().subject())
.map_err(|e| e.add_location(asn1::ParseLocation::Field("subject")))?)
Ok(self
.cached_subject
.get_or_try_init(py, || {
x509::parse_name(py, self.raw.borrow_dependent().subject())
.map_err(|e| e.add_location(asn1::ParseLocation::Field("subject")))
.map(|v| v.unbind())
})?
.bind(py)
.clone())
}

#[getter]
Expand Down Expand Up @@ -441,6 +457,8 @@ pub(crate) fn load_der_x509_certificate(
Ok(Certificate {
raw,
cached_extensions: pyo3::sync::PyOnceLock::new(),
cached_issuer: pyo3::sync::PyOnceLock::new(),
cached_subject: pyo3::sync::PyOnceLock::new(),
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/rust/src/x509/ocsp_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ impl OCSPResponse {
x509::certificate::Certificate {
raw: raw_cert,
cached_extensions: pyo3::sync::PyOnceLock::new(),
cached_issuer: pyo3::sync::PyOnceLock::new(),
cached_subject: pyo3::sync::PyOnceLock::new(),
},
)?)?;
}
Expand Down
6 changes: 4 additions & 2 deletions tests/x509/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,15 +1028,17 @@ def test_country_jurisdiction_country_too_long(self, backend):
os.path.join("x509", "custom", "bad_country.pem"),
x509.load_pem_x509_certificate,
)
# Both warnings are emitted during the first parse_name call (when
# cert.subject is first accessed); subsequent accesses return the
# cached Name object without re-parsing, so both checks must live
# inside a single pytest.warns block.
with pytest.warns(UserWarning):
assert (
cert.subject.get_attributes_for_oid(x509.NameOID.COUNTRY_NAME)[
0
].value
== "too long"
)

with pytest.warns(UserWarning):
assert (
cert.subject.get_attributes_for_oid(
x509.NameOID.JURISDICTION_COUNTRY_NAME
Expand Down