Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to convert OCSP Request types to GATs #12036

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/rust/cryptography-x509/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ impl<T: asn1::SimpleAsn1Writable, U: asn1::SimpleAsn1Writable> asn1::SimpleAsn1W
}

pub trait Asn1Operation {
type SequenceOf<'a, T>
where
T: 'a;
type SequenceOfVec<'a, T>
where
T: 'a;
Expand All @@ -277,6 +280,10 @@ pub struct Asn1Read;
pub struct Asn1Write;

impl Asn1Operation for Asn1Read {
type SequenceOf<'a, T>
= asn1::SequenceOf<'a, T>
where
T: 'a;
type SequenceOfVec<'a, T>
= asn1::SequenceOf<'a, T>
where
Expand All @@ -288,6 +295,10 @@ impl Asn1Operation for Asn1Read {
type OwnedBitString<'a> = asn1::BitString<'a>;
}
impl Asn1Operation for Asn1Write {
type SequenceOf<'a, T>
= asn1::SequenceOfWriter<'a, T>
where
T: 'a;
type SequenceOfVec<'a, T>
= asn1::SequenceOfWriter<'a, T, Vec<T>>
where
Expand Down
12 changes: 5 additions & 7 deletions src/rust/cryptography-x509/src/ocsp_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.

use crate::common::Asn1Operation;
use crate::{common, extensions, name};

#[derive(asn1::Asn1Read, asn1::Asn1Write)]
pub struct TBSRequest<'a> {
pub struct TBSRequest<'a, Op: Asn1Operation> {
#[explicit(0)]
#[default(0)]
pub version: u8,
#[explicit(1)]
pub requestor_name: Option<name::GeneralName<'a>>,
pub request_list: common::Asn1ReadableOrWritable<
asn1::SequenceOf<'a, Request<'a>>,
asn1::SequenceOfWriter<'a, Request<'a>>,
>,
pub request_list: Op::SequenceOf<'a, Request<'a>>,
#[explicit(2)]
pub raw_request_extensions: Option<extensions::RawExtensions<'a>>,
}
Expand All @@ -35,8 +33,8 @@ pub struct CertID<'a> {
}

#[derive(asn1::Asn1Read, asn1::Asn1Write)]
pub struct OCSPRequest<'a> {
pub tbs_request: TBSRequest<'a>,
pub struct OCSPRequest<'a, Op: Asn1Operation> {
pub tbs_request: TBSRequest<'a, Op>,
// Parsing out the full structure, which includes the entirety of a
// certificate is more trouble than it's worth, since it's not in the
// Python API.
Expand Down
21 changes: 6 additions & 15 deletions src/rust/src/x509/ocsp_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.

use cryptography_x509::common::{Asn1Read, Asn1Write};
use cryptography_x509::{
common,
ocsp_req::{self, OCSPRequest as RawOCSPRequest},
oid,
};
Expand All @@ -14,11 +14,12 @@ use crate::error::{CryptographyError, CryptographyResult};
use crate::x509::{extensions, ocsp};
use crate::{exceptions, types, x509};

type ReadRawOCSPRequest<'a> = RawOCSPRequest<'a, Asn1Read>;
self_cell::self_cell!(
struct OwnedOCSPRequest {
owner: pyo3::Py<pyo3::types::PyBytes>,
#[covariant]
dependent: RawOCSPRequest,
dependent: ReadRawOCSPRequest,
}
);

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

if raw
.borrow_dependent()
.tbs_request
.request_list
.unwrap_read()
.len()
!= 1
{
if raw.borrow_dependent().tbs_request.request_list.len() != 1 {
return Err(CryptographyError::from(
pyo3::exceptions::PyNotImplementedError::new_err(
"OCSP request contains more than one request",
Expand All @@ -63,7 +57,6 @@ impl OCSPRequest {
.borrow_dependent()
.tbs_request
.request_list
.unwrap_read()
.clone()
.next()
.unwrap()
Expand Down Expand Up @@ -214,13 +207,11 @@ pub(crate) fn create_ocsp_request(
req_cert,
single_request_extensions: None,
}];
let ocsp_req = ocsp_req::OCSPRequest {
let ocsp_req = ocsp_req::OCSPRequest::<Asn1Write> {
tbs_request: ocsp_req::TBSRequest {
version: 0,
requestor_name: None,
request_list: common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(
&reqs,
)),
request_list: asn1::SequenceOfWriter::new(&reqs),
raw_request_extensions: extensions,
},
optional_signature: None,
Expand Down
Loading