Skip to content

Commit 3f813f7

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 7971c6b commit 3f813f7

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ impl<T: asn1::SimpleAsn1Writable, U: asn1::SimpleAsn1Writable> asn1::SimpleAsn1W
264264
}
265265

266266
pub trait Asn1Operation {
267+
type SequenceOf<'a, T>
268+
where
269+
T: 'a;
267270
type SequenceOfVec<'a, T>
268271
where
269272
T: 'a;
@@ -277,6 +280,10 @@ pub struct Asn1Read;
277280
pub struct Asn1Write;
278281

279282
impl Asn1Operation for Asn1Read {
283+
type SequenceOf<'a, T>
284+
= asn1::SequenceOf<'a, T>
285+
where
286+
T: 'a;
280287
type SequenceOfVec<'a, T>
281288
= asn1::SequenceOf<'a, T>
282289
where
@@ -288,6 +295,10 @@ impl Asn1Operation for Asn1Read {
288295
type OwnedBitString<'a> = asn1::BitString<'a>;
289296
}
290297
impl Asn1Operation for Asn1Write {
298+
type SequenceOf<'a, T>
299+
= asn1::SequenceOfWriter<'a, T>
300+
where
301+
T: 'a;
291302
type SequenceOfVec<'a, T>
292303
= asn1::SequenceOfWriter<'a, T, Vec<T>>
293304
where

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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
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::{
6-
common,
77
ocsp_req::{self, OCSPRequest as RawOCSPRequest},
88
oid,
99
};
@@ -14,11 +14,12 @@ use crate::error::{CryptographyError, CryptographyResult};
1414
use crate::x509::{extensions, ocsp};
1515
use crate::{exceptions, types, x509};
1616

17+
type ReadRawOCSPRequest<'a> = RawOCSPRequest<'a, Asn1Read>;
1718
self_cell::self_cell!(
1819
struct OwnedOCSPRequest {
1920
owner: pyo3::Py<pyo3::types::PyBytes>,
2021
#[covariant]
21-
dependent: RawOCSPRequest,
22+
dependent: ReadRawOCSPRequest,
2223
}
2324
);
2425

@@ -29,14 +30,7 @@ pub(crate) fn load_der_ocsp_request(
2930
) -> CryptographyResult<OCSPRequest> {
3031
let raw = OwnedOCSPRequest::try_new(data, |data| asn1::parse_single(data.as_bytes(py)))?;
3132

32-
if raw
33-
.borrow_dependent()
34-
.tbs_request
35-
.request_list
36-
.unwrap_read()
37-
.len()
38-
!= 1
39-
{
33+
if raw.borrow_dependent().tbs_request.request_list.len() != 1 {
4034
return Err(CryptographyError::from(
4135
pyo3::exceptions::PyNotImplementedError::new_err(
4236
"OCSP request contains more than one request",
@@ -63,7 +57,6 @@ impl OCSPRequest {
6357
.borrow_dependent()
6458
.tbs_request
6559
.request_list
66-
.unwrap_read()
6760
.clone()
6861
.next()
6962
.unwrap()
@@ -214,13 +207,11 @@ pub(crate) fn create_ocsp_request(
214207
req_cert,
215208
single_request_extensions: None,
216209
}];
217-
let ocsp_req = ocsp_req::OCSPRequest {
210+
let ocsp_req = ocsp_req::OCSPRequest::<Asn1Write> {
218211
tbs_request: ocsp_req::TBSRequest {
219212
version: 0,
220213
requestor_name: None,
221-
request_list: common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(
222-
&reqs,
223-
)),
214+
request_list: asn1::SequenceOfWriter::new(&reqs),
224215
raw_request_extensions: extensions,
225216
},
226217
optional_signature: None,

0 commit comments

Comments
 (0)