Skip to content

Commit aa23cf2

Browse files
committed
Attempt to convert OCSP Request types to GATs
This does currently work because GATs cause a type to be invariant
1 parent fd23bda commit aa23cf2

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/rust/cryptography-x509/src/common.rs

+11
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ impl<T: asn1::SimpleAsn1Writable, U: asn1::SimpleAsn1Writable> asn1::SimpleAsn1W
267267
}
268268

269269
pub trait Asn1Operation {
270+
type SequenceOf<'a, T>
271+
where
272+
T: 'a;
270273
type SequenceOfVec<'a, T>
271274
where
272275
T: 'a;
@@ -280,6 +283,10 @@ pub struct Asn1Read;
280283
pub struct Asn1Write;
281284

282285
impl Asn1Operation for Asn1Read {
286+
type SequenceOf<'a, T>
287+
= asn1::SequenceOf<'a, T>
288+
where
289+
T: 'a;
283290
type SequenceOfVec<'a, T>
284291
= asn1::SequenceOf<'a, T>
285292
where
@@ -291,6 +298,10 @@ impl Asn1Operation for Asn1Read {
291298
type OwnedBitString<'a> = asn1::BitString<'a>;
292299
}
293300
impl Asn1Operation for Asn1Write {
301+
type SequenceOf<'a, T>
302+
= asn1::SequenceOfWriter<'a, T>
303+
where
304+
T: 'a;
294305
type SequenceOfVec<'a, T>
295306
= asn1::SequenceOfWriter<'a, T, Vec<T>>
296307
where

src/rust/cryptography-x509/src/ocsp_req.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5+
use crate::common::Asn1Operation;
56
use crate::{common, extensions, name};
67

78
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
8-
pub struct TBSRequest<'a> {
9+
pub struct TBSRequest<'a, Op: Asn1Operation> {
910
#[explicit(0)]
1011
#[default(0)]
1112
pub version: u8,
1213
#[explicit(1)]
1314
pub requestor_name: Option<name::GeneralName<'a>>,
14-
pub request_list: common::Asn1ReadableOrWritable<
15-
asn1::SequenceOf<'a, Request<'a>>,
16-
asn1::SequenceOfWriter<'a, Request<'a>>,
17-
>,
15+
pub request_list: Op::SequenceOf<'a, Request<'a>>,
1816
#[explicit(2)]
1917
pub raw_request_extensions: Option<extensions::RawExtensions<'a>>,
2018
}
@@ -35,8 +33,8 @@ pub struct CertID<'a> {
3533
}
3634

3735
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
38-
pub struct OCSPRequest<'a> {
39-
pub tbs_request: TBSRequest<'a>,
36+
pub struct OCSPRequest<'a, Op: Asn1Operation> {
37+
pub tbs_request: TBSRequest<'a, Op>,
4038
// Parsing out the full structure, which includes the entirety of a
4139
// certificate is more trouble than it's worth, since it's not in the
4240
// Python API.

src/rust/src/x509/ocsp_req.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5+
use cryptography_x509::common::{Asn1Read, Asn1Write};
56
use cryptography_x509::ocsp_req::{self, OCSPRequest as RawOCSPRequest};
6-
use cryptography_x509::{common, oid};
7+
use cryptography_x509::oid;
78
use pyo3::types::{PyAnyMethods, PyListMethods};
89

910
use crate::asn1::{big_byte_slice_to_py_int, oid_to_py_oid, py_uint_to_big_endian_bytes};
1011
use crate::error::{CryptographyError, CryptographyResult};
1112
use crate::x509::{extensions, ocsp};
1213
use crate::{exceptions, types, x509};
1314

15+
type ReadRawOCSPRequest<'a> = RawOCSPRequest<'a, Asn1Read>;
1416
self_cell::self_cell!(
1517
struct OwnedOCSPRequest {
1618
owner: pyo3::Py<pyo3::types::PyBytes>,
1719
#[covariant]
18-
dependent: RawOCSPRequest,
20+
dependent: ReadRawOCSPRequest,
1921
}
2022
);
2123

@@ -26,14 +28,7 @@ pub(crate) fn load_der_ocsp_request(
2628
) -> CryptographyResult<OCSPRequest> {
2729
let raw = OwnedOCSPRequest::try_new(data, |data| asn1::parse_single(data.as_bytes(py)))?;
2830

29-
if raw
30-
.borrow_dependent()
31-
.tbs_request
32-
.request_list
33-
.unwrap_read()
34-
.len()
35-
!= 1
36-
{
31+
if raw.borrow_dependent().tbs_request.request_list.len() != 1 {
3732
return Err(CryptographyError::from(
3833
pyo3::exceptions::PyNotImplementedError::new_err(
3934
"OCSP request contains more than one request",
@@ -60,7 +55,6 @@ impl OCSPRequest {
6055
.borrow_dependent()
6156
.tbs_request
6257
.request_list
63-
.unwrap_read()
6458
.clone()
6559
.next()
6660
.unwrap()
@@ -211,13 +205,11 @@ pub(crate) fn create_ocsp_request(
211205
req_cert,
212206
single_request_extensions: None,
213207
}];
214-
let ocsp_req = ocsp_req::OCSPRequest {
208+
let ocsp_req = ocsp_req::OCSPRequest::<Asn1Write> {
215209
tbs_request: ocsp_req::TBSRequest {
216210
version: 0,
217211
requestor_name: None,
218-
request_list: common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(
219-
&reqs,
220-
)),
212+
request_list: asn1::SequenceOfWriter::new(&reqs),
221213
raw_request_extensions: extensions,
222214
},
223215
optional_signature: None,

0 commit comments

Comments
 (0)