Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit de53b3e

Browse files
committedSep 29, 2024·
Remove CustomPolicyBuilder in favor of extending PolicyBuilder.
1 parent 181cb31 commit de53b3e

File tree

7 files changed

+97
-297
lines changed

7 files changed

+97
-297
lines changed
 

‎docs/x509/verification.rst

-61
Original file line numberDiff line numberDiff line change
@@ -297,64 +297,3 @@ the root of trust:
297297
for server verification.
298298

299299
:returns: An instance of :class:`ClientVerifier`
300-
301-
.. class:: CustomPolicyBuilder
302-
303-
.. versionadded:: 44.0.0
304-
305-
A CustomPolicyBuilder provides a builder-style interface for constructing a
306-
Verifier, but provides additional control over the verification policy compared to :class:`PolicyBuilder`.
307-
308-
.. method:: time(new_time)
309-
310-
Sets the verifier's verification time.
311-
312-
If not called explicitly, this is set to :meth:`datetime.datetime.now`
313-
when :meth:`build_server_verifier` or :meth:`build_client_verifier`
314-
is called.
315-
316-
:param new_time: The :class:`datetime.datetime` to use in the verifier
317-
318-
:returns: A new instance of :class:`PolicyBuilder`
319-
320-
.. method:: store(new_store)
321-
322-
Sets the verifier's trust store.
323-
324-
:param new_store: The :class:`Store` to use in the verifier
325-
326-
:returns: A new instance of :class:`PolicyBuilder`
327-
328-
.. method:: max_chain_depth(new_max_chain_depth)
329-
330-
Sets the verifier's maximum chain building depth.
331-
332-
This depth behaves tracks the length of the intermediate CA
333-
chain: a maximum depth of zero means that the leaf must be directly
334-
issued by a member of the store, a depth of one means no more than
335-
one intermediate CA, and so forth. Note that self-issued intermediates
336-
don't count against the chain depth, per RFC 5280.
337-
338-
:param new_max_chain_depth: The maximum depth to allow in the verifier
339-
340-
:returns: A new instance of :class:`PolicyBuilder`
341-
342-
.. method:: build_server_verifier(subject)
343-
344-
Builds a verifier for verifying server certificates.
345-
346-
:param subject: A :class:`Subject` to use in the verifier
347-
348-
:returns: An instance of :class:`ServerVerifier`
349-
350-
.. method:: build_client_verifier()
351-
352-
Builds a verifier for verifying client certificates.
353-
354-
.. warning::
355-
356-
This API is not suitable for website (i.e. server) certificate
357-
verification. You **must** use :meth:`build_server_verifier`
358-
for server verification.
359-
360-
:returns: An instance of :class:`ClientVerifier`

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

-11
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,6 @@ class PolicyBuilder:
6767
self, subject: x509.verification.Subject
6868
) -> ServerVerifier: ...
6969

70-
class CustomPolicyBuilder:
71-
def time(self, new_time: datetime.datetime) -> CustomPolicyBuilder: ...
72-
def store(self, new_store: Store) -> CustomPolicyBuilder: ...
73-
def max_chain_depth(
74-
self, new_max_chain_depth: int
75-
) -> CustomPolicyBuilder: ...
76-
def build_client_verifier(self) -> ClientVerifier: ...
77-
def build_server_verifier(
78-
self, subject: x509.verification.Subject
79-
) -> ServerVerifier: ...
80-
8170
class VerifiedClient:
8271
@property
8372
def subjects(self) -> list[x509.GeneralName] | None: ...

‎src/cryptography/x509/verification.py

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
__all__ = [
1313
"ClientVerifier",
1414
"PolicyBuilder",
15-
"CustomPolicyBuilder",
1615
"ServerVerifier",
1716
"Store",
1817
"Subject",
@@ -26,5 +25,4 @@
2625
ClientVerifier = rust_x509.ClientVerifier
2726
ServerVerifier = rust_x509.ServerVerifier
2827
PolicyBuilder = rust_x509.PolicyBuilder
29-
CustomPolicyBuilder = rust_x509.CustomPolicyBuilder
3028
VerificationError = rust_x509.VerificationError

‎src/rust/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ mod _rust {
132132
use crate::x509::sct::Sct;
133133
#[pymodule_export]
134134
use crate::x509::verify::{
135-
CustomPolicyBuilder, PolicyBuilder, PyClientVerifier, PyServerVerifier, PyStore,
136-
PyVerifiedClient, VerificationError,
135+
PolicyBuilder, PyClientVerifier, PyServerVerifier, PyStore, PyVerifiedClient,
136+
VerificationError,
137137
};
138138
}
139139

‎src/rust/src/x509/verify.rs

+65-158
Original file line numberDiff line numberDiff line change
@@ -74,88 +74,15 @@ pub(crate) struct PolicyBuilder {
7474
time: Option<asn1::DateTime>,
7575
store: Option<pyo3::Py<PyStore>>,
7676
max_chain_depth: Option<u8>,
77-
}
78-
79-
#[pyo3::pymethods]
80-
impl PolicyBuilder {
81-
#[new]
82-
fn new() -> PolicyBuilder {
83-
PolicyBuilder {
84-
time: None,
85-
store: None,
86-
max_chain_depth: None,
87-
}
88-
}
89-
90-
fn time(
91-
&self,
92-
py: pyo3::Python<'_>,
93-
new_time: pyo3::Bound<'_, pyo3::PyAny>,
94-
) -> CryptographyResult<PolicyBuilder> {
95-
policy_builder_set_once_check!(self, time, "validation time");
96-
97-
Ok(PolicyBuilder {
98-
time: Some(py_to_datetime(py, new_time)?),
99-
store: self.store.as_ref().map(|s| s.clone_ref(py)),
100-
max_chain_depth: self.max_chain_depth,
101-
})
102-
}
103-
104-
fn store(&self, new_store: pyo3::Py<PyStore>) -> CryptographyResult<PolicyBuilder> {
105-
policy_builder_set_once_check!(self, store, "trust store");
106-
107-
Ok(PolicyBuilder {
108-
time: self.time.clone(),
109-
store: Some(new_store),
110-
max_chain_depth: self.max_chain_depth,
111-
})
112-
}
113-
114-
fn max_chain_depth(
115-
&self,
116-
py: pyo3::Python<'_>,
117-
new_max_chain_depth: u8,
118-
) -> CryptographyResult<PolicyBuilder> {
119-
policy_builder_set_once_check!(self, max_chain_depth, "maximum chain depth");
120-
121-
Ok(PolicyBuilder {
122-
time: self.time.clone(),
123-
store: self.store.as_ref().map(|s| s.clone_ref(py)),
124-
max_chain_depth: Some(new_max_chain_depth),
125-
})
126-
}
127-
128-
fn build_client_verifier(&self, py: pyo3::Python<'_>) -> CryptographyResult<PyClientVerifier> {
129-
build_client_verifier_impl(py, &self.store, &self.time, |time| {
130-
Policy::client(PyCryptoOps {}, time, self.max_chain_depth)
131-
})
132-
}
133-
134-
fn build_server_verifier(
135-
&self,
136-
py: pyo3::Python<'_>,
137-
subject: pyo3::PyObject,
138-
) -> CryptographyResult<PyServerVerifier> {
139-
build_server_verifier_impl(py, &self.store, &self.time, subject, |subject, time| {
140-
Policy::server(PyCryptoOps {}, subject, time, self.max_chain_depth)
141-
})
142-
}
143-
}
144-
145-
#[pyo3::pyclass(frozen, module = "cryptography.x509.verification")]
146-
pub(crate) struct CustomPolicyBuilder {
147-
time: Option<asn1::DateTime>,
148-
store: Option<pyo3::Py<PyStore>>,
149-
max_chain_depth: Option<u8>,
15077
ca_ext_policy: Option<ExtensionPolicy<PyCryptoOps>>,
15178
ee_ext_policy: Option<ExtensionPolicy<PyCryptoOps>>,
15279
}
15380

154-
impl CustomPolicyBuilder {
81+
impl PolicyBuilder {
15582
/// Clones the builder, requires the GIL token to increase
15683
/// reference count for `self.store`.
157-
fn py_clone(&self, py: pyo3::Python<'_>) -> CustomPolicyBuilder {
158-
CustomPolicyBuilder {
84+
fn py_clone(&self, py: pyo3::Python<'_>) -> PolicyBuilder {
85+
PolicyBuilder {
15986
time: self.time.clone(),
16087
store: self.store.as_ref().map(|s| s.clone_ref(py)),
16188
max_chain_depth: self.max_chain_depth,
@@ -166,10 +93,10 @@ impl CustomPolicyBuilder {
16693
}
16794

16895
#[pyo3::pymethods]
169-
impl CustomPolicyBuilder {
96+
impl PolicyBuilder {
17097
#[new]
171-
fn new() -> CustomPolicyBuilder {
172-
CustomPolicyBuilder {
98+
fn new() -> PolicyBuilder {
99+
PolicyBuilder {
173100
time: None,
174101
store: None,
175102
max_chain_depth: None,
@@ -182,10 +109,10 @@ impl CustomPolicyBuilder {
182109
&self,
183110
py: pyo3::Python<'_>,
184111
new_time: pyo3::Bound<'_, pyo3::PyAny>,
185-
) -> CryptographyResult<CustomPolicyBuilder> {
112+
) -> CryptographyResult<PolicyBuilder> {
186113
policy_builder_set_once_check!(self, time, "validation time");
187114

188-
Ok(CustomPolicyBuilder {
115+
Ok(PolicyBuilder {
189116
time: Some(py_to_datetime(py, new_time)?),
190117
..self.py_clone(py)
191118
})
@@ -195,10 +122,10 @@ impl CustomPolicyBuilder {
195122
&self,
196123
py: pyo3::Python<'_>,
197124
new_store: pyo3::Py<PyStore>,
198-
) -> CryptographyResult<CustomPolicyBuilder> {
125+
) -> CryptographyResult<PolicyBuilder> {
199126
policy_builder_set_once_check!(self, store, "trust store");
200127

201-
Ok(CustomPolicyBuilder {
128+
Ok(PolicyBuilder {
202129
store: Some(new_store),
203130
..self.py_clone(py)
204131
})
@@ -208,100 +135,80 @@ impl CustomPolicyBuilder {
208135
&self,
209136
py: pyo3::Python<'_>,
210137
new_max_chain_depth: u8,
211-
) -> CryptographyResult<CustomPolicyBuilder> {
138+
) -> CryptographyResult<PolicyBuilder> {
212139
policy_builder_set_once_check!(self, max_chain_depth, "maximum chain depth");
213140

214-
Ok(CustomPolicyBuilder {
141+
Ok(PolicyBuilder {
215142
max_chain_depth: Some(new_max_chain_depth),
216143
..self.py_clone(py)
217144
})
218145
}
219146

220147
fn build_client_verifier(&self, py: pyo3::Python<'_>) -> CryptographyResult<PyClientVerifier> {
221-
build_client_verifier_impl(py, &self.store, &self.time, |time| {
222-
// TODO: Replace with a custom policy once it's implemented in cryptography-x509-verification
223-
Policy::client(PyCryptoOps {}, time, self.max_chain_depth)
224-
})
148+
let store = match self.store.as_ref() {
149+
Some(s) => s.clone_ref(py),
150+
None => {
151+
return Err(CryptographyError::from(
152+
pyo3::exceptions::PyValueError::new_err(
153+
"A client verifier must have a trust store.",
154+
),
155+
));
156+
}
157+
};
158+
159+
let time = match self.time.as_ref() {
160+
Some(t) => t.clone(),
161+
None => datetime_now(py)?,
162+
};
163+
164+
// TODO: Pass extension policies here once implemented in cryptography-x509-verification.
165+
let policy = Policy::client(PyCryptoOps {}, time, self.max_chain_depth);
166+
167+
Ok(PyClientVerifier { policy, store })
225168
}
226169

227170
fn build_server_verifier(
228171
&self,
229172
py: pyo3::Python<'_>,
230173
subject: pyo3::PyObject,
231174
) -> CryptographyResult<PyServerVerifier> {
232-
build_server_verifier_impl(py, &self.store, &self.time, subject, |subject, time| {
233-
// TODO: Replace with a custom policy once it's implemented in cryptography-x509-verification
234-
Policy::server(PyCryptoOps {}, subject, time, self.max_chain_depth)
175+
let store = match self.store.as_ref() {
176+
Some(s) => s.clone_ref(py),
177+
None => {
178+
return Err(CryptographyError::from(
179+
pyo3::exceptions::PyValueError::new_err(
180+
"A server verifier must have a trust store.",
181+
),
182+
));
183+
}
184+
};
185+
186+
let time = match self.time.as_ref() {
187+
Some(t) => t.clone(),
188+
None => datetime_now(py)?,
189+
};
190+
let subject_owner = build_subject_owner(py, &subject)?;
191+
192+
let policy = OwnedPolicy::try_new(subject_owner, |subject_owner| {
193+
let subject = build_subject(py, subject_owner)?;
194+
195+
// TODO: Pass extension policies here once implemented in cryptography-x509-verification.
196+
Ok::<PyCryptoPolicy<'_>, pyo3::PyErr>(Policy::server(
197+
PyCryptoOps {},
198+
subject,
199+
time,
200+
self.max_chain_depth,
201+
))
202+
})?;
203+
204+
Ok(PyServerVerifier {
205+
py_subject: subject,
206+
policy,
207+
store,
235208
})
236209
}
237210
}
238211

239-
/// This is a helper to avoid code duplication between PolicyBuilder and CustomPolicyBuilder.
240-
fn build_server_verifier_impl(
241-
py: pyo3::Python<'_>,
242-
store: &Option<pyo3::Py<PyStore>>,
243-
time: &Option<asn1::DateTime>,
244-
subject: pyo3::PyObject,
245-
make_policy: impl Fn(Subject<'_>, asn1::DateTime) -> PyCryptoPolicy<'_>,
246-
) -> CryptographyResult<PyServerVerifier> {
247-
let store = match store {
248-
Some(s) => s.clone_ref(py),
249-
None => {
250-
return Err(CryptographyError::from(
251-
pyo3::exceptions::PyValueError::new_err(
252-
"A server verifier must have a trust store.",
253-
),
254-
));
255-
}
256-
};
257-
258-
let time = match time.as_ref() {
259-
Some(t) => t.clone(),
260-
None => datetime_now(py)?,
261-
};
262-
let subject_owner = build_subject_owner(py, &subject)?;
263-
264-
let policy = OwnedPolicy::try_new(subject_owner, |subject_owner| {
265-
let subject = build_subject(py, subject_owner)?;
266-
Ok::<PyCryptoPolicy<'_>, pyo3::PyErr>(make_policy(subject, time))
267-
})?;
268-
269-
Ok(PyServerVerifier {
270-
py_subject: subject,
271-
policy,
272-
store,
273-
})
274-
}
275-
276-
/// This is a helper to avoid code duplication between PolicyBuilder and CustomPolicyBuilder.
277-
fn build_client_verifier_impl(
278-
py: pyo3::Python<'_>,
279-
store: &Option<pyo3::Py<PyStore>>,
280-
time: &Option<asn1::DateTime>,
281-
make_policy: impl Fn(asn1::DateTime) -> PyCryptoPolicy<'static>,
282-
) -> CryptographyResult<PyClientVerifier> {
283-
let store = match store.as_ref() {
284-
Some(s) => s.clone_ref(py),
285-
None => {
286-
return Err(CryptographyError::from(
287-
pyo3::exceptions::PyValueError::new_err(
288-
"A client verifier must have a trust store.",
289-
),
290-
));
291-
}
292-
};
293-
294-
let time = match time.as_ref() {
295-
Some(t) => t.clone(),
296-
None => datetime_now(py)?,
297-
};
298-
299-
Ok(PyClientVerifier {
300-
policy: make_policy(time),
301-
store,
302-
})
303-
}
304-
305212
type PyCryptoPolicy<'a> = Policy<'a, PyCryptoOps>;
306213

307214
/// This enum exists solely to provide heterogeneously typed ownership for `OwnedPolicy`.

0 commit comments

Comments
 (0)
Please sign in to comment.