@@ -74,88 +74,15 @@ pub(crate) struct PolicyBuilder {
74
74
time : Option < asn1:: DateTime > ,
75
75
store : Option < pyo3:: Py < PyStore > > ,
76
76
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 > ,
150
77
ca_ext_policy : Option < ExtensionPolicy < PyCryptoOps > > ,
151
78
ee_ext_policy : Option < ExtensionPolicy < PyCryptoOps > > ,
152
79
}
153
80
154
- impl CustomPolicyBuilder {
81
+ impl PolicyBuilder {
155
82
/// Clones the builder, requires the GIL token to increase
156
83
/// 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 {
159
86
time : self . time . clone ( ) ,
160
87
store : self . store . as_ref ( ) . map ( |s| s. clone_ref ( py) ) ,
161
88
max_chain_depth : self . max_chain_depth ,
@@ -166,10 +93,10 @@ impl CustomPolicyBuilder {
166
93
}
167
94
168
95
#[ pyo3:: pymethods]
169
- impl CustomPolicyBuilder {
96
+ impl PolicyBuilder {
170
97
#[ new]
171
- fn new ( ) -> CustomPolicyBuilder {
172
- CustomPolicyBuilder {
98
+ fn new ( ) -> PolicyBuilder {
99
+ PolicyBuilder {
173
100
time : None ,
174
101
store : None ,
175
102
max_chain_depth : None ,
@@ -182,10 +109,10 @@ impl CustomPolicyBuilder {
182
109
& self ,
183
110
py : pyo3:: Python < ' _ > ,
184
111
new_time : pyo3:: Bound < ' _ , pyo3:: PyAny > ,
185
- ) -> CryptographyResult < CustomPolicyBuilder > {
112
+ ) -> CryptographyResult < PolicyBuilder > {
186
113
policy_builder_set_once_check ! ( self , time, "validation time" ) ;
187
114
188
- Ok ( CustomPolicyBuilder {
115
+ Ok ( PolicyBuilder {
189
116
time : Some ( py_to_datetime ( py, new_time) ?) ,
190
117
..self . py_clone ( py)
191
118
} )
@@ -195,10 +122,10 @@ impl CustomPolicyBuilder {
195
122
& self ,
196
123
py : pyo3:: Python < ' _ > ,
197
124
new_store : pyo3:: Py < PyStore > ,
198
- ) -> CryptographyResult < CustomPolicyBuilder > {
125
+ ) -> CryptographyResult < PolicyBuilder > {
199
126
policy_builder_set_once_check ! ( self , store, "trust store" ) ;
200
127
201
- Ok ( CustomPolicyBuilder {
128
+ Ok ( PolicyBuilder {
202
129
store : Some ( new_store) ,
203
130
..self . py_clone ( py)
204
131
} )
@@ -208,100 +135,80 @@ impl CustomPolicyBuilder {
208
135
& self ,
209
136
py : pyo3:: Python < ' _ > ,
210
137
new_max_chain_depth : u8 ,
211
- ) -> CryptographyResult < CustomPolicyBuilder > {
138
+ ) -> CryptographyResult < PolicyBuilder > {
212
139
policy_builder_set_once_check ! ( self , max_chain_depth, "maximum chain depth" ) ;
213
140
214
- Ok ( CustomPolicyBuilder {
141
+ Ok ( PolicyBuilder {
215
142
max_chain_depth : Some ( new_max_chain_depth) ,
216
143
..self . py_clone ( py)
217
144
} )
218
145
}
219
146
220
147
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 } )
225
168
}
226
169
227
170
fn build_server_verifier (
228
171
& self ,
229
172
py : pyo3:: Python < ' _ > ,
230
173
subject : pyo3:: PyObject ,
231
174
) -> 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,
235
208
} )
236
209
}
237
210
}
238
211
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
-
305
212
type PyCryptoPolicy < ' a > = Policy < ' a , PyCryptoOps > ;
306
213
307
214
/// This enum exists solely to provide heterogeneously typed ownership for `OwnedPolicy`.
0 commit comments