Skip to content

Commit 750f34e

Browse files
authored
Introduce new GAT based Asn1 Read/Write (#12011)
This replaces the runtime based Asn1ReadableOrWritable. Adopts it for IssuingDistributionPoint, DistributionPoint
1 parent a93d194 commit 750f34e

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

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

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

266+
pub trait Asn1Operation {
267+
type OwnedBitString<'a>;
268+
}
269+
270+
pub struct Asn1Read;
271+
pub struct Asn1Write;
272+
273+
impl Asn1Operation for Asn1Read {
274+
type OwnedBitString<'a> = asn1::BitString<'a>;
275+
}
276+
impl Asn1Operation for Asn1Write {
277+
type OwnedBitString<'a> = asn1::OwnedBitString;
278+
}
279+
266280
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
267281
pub struct DssSignature<'a> {
268282
pub r: asn1::BigUint<'a>,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
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

7-
pub type ReasonFlags<'a> =
8-
Option<common::Asn1ReadableOrWritable<asn1::BitString<'a>, asn1::OwnedBitString>>;
8+
pub type ReasonFlags<'a, Op> = Option<<Op as Asn1Operation>::OwnedBitString<'a>>;
99

1010
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Eq, Hash)]
1111
pub struct CertificateRevocationList<'a> {
@@ -41,7 +41,7 @@ pub struct RevokedCertificate<'a> {
4141
}
4242

4343
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
44-
pub struct IssuingDistributionPoint<'a> {
44+
pub struct IssuingDistributionPoint<'a, Op: Asn1Operation> {
4545
#[explicit(0)]
4646
pub distribution_point: Option<extensions::DistributionPointName<'a>>,
4747

@@ -54,7 +54,7 @@ pub struct IssuingDistributionPoint<'a> {
5454
pub only_contains_ca_certs: bool,
5555

5656
#[implicit(3)]
57-
pub only_some_reasons: ReasonFlags<'a>,
57+
pub only_some_reasons: ReasonFlags<'a, Op>,
5858

5959
#[implicit(4)]
6060
#[default(false)]

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::collections::HashSet;
66

77
use crate::common;
8+
use crate::common::Asn1Operation;
89
use crate::crl;
910
use crate::name;
1011

@@ -183,12 +184,12 @@ pub struct MSCertificateTemplate {
183184
}
184185

185186
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
186-
pub struct DistributionPoint<'a> {
187+
pub struct DistributionPoint<'a, Op: Asn1Operation> {
187188
#[explicit(0)]
188189
pub distribution_point: Option<DistributionPointName<'a>>,
189190

190191
#[implicit(1)]
191-
pub reasons: crl::ReasonFlags<'a>,
192+
pub reasons: crl::ReasonFlags<'a, Op>,
192193

193194
#[implicit(2)]
194195
pub crl_issuer: Option<name::SequenceOfGeneralName<'a>>,

src/rust/src/x509/certificate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::collections::hash_map::DefaultHasher;
66
use std::hash::{Hash, Hasher};
77

88
use cryptography_x509::certificate::Certificate as RawCertificate;
9+
use cryptography_x509::common::Asn1Read;
910
use cryptography_x509::common::{AlgorithmParameters, Asn1ReadableOrWritable};
1011
use cryptography_x509::extensions::{
1112
Admission, Admissions, AuthorityKeyIdentifier, BasicConstraints, DisplayText,
@@ -602,14 +603,13 @@ pub(crate) fn parse_distribution_point_name<'p>(
602603

603604
fn parse_distribution_point<'p>(
604605
py: pyo3::Python<'p>,
605-
dp: DistributionPoint<'p>,
606+
dp: DistributionPoint<'p, Asn1Read>,
606607
) -> CryptographyResult<pyo3::Bound<'p, pyo3::PyAny>> {
607608
let (full_name, relative_name) = match dp.distribution_point {
608609
Some(data) => parse_distribution_point_name(py, data)?,
609610
None => (py.None().into_bound(py), py.None().into_bound(py)),
610611
};
611-
let reasons =
612-
parse_distribution_point_reasons(py, dp.reasons.as_ref().map(|v| v.unwrap_read()))?;
612+
let reasons = parse_distribution_point_reasons(py, dp.reasons.as_ref())?;
613613
let crl_issuer = match dp.crl_issuer {
614614
Some(aci) => x509::parse_general_names(py, aci.unwrap_read())?,
615615
None => py.None().into_bound(py),
@@ -623,7 +623,7 @@ pub(crate) fn parse_distribution_points<'p>(
623623
py: pyo3::Python<'p>,
624624
ext: &Extension<'_>,
625625
) -> CryptographyResult<pyo3::Bound<'p, pyo3::PyAny>> {
626-
let dps = ext.value::<asn1::SequenceOf<'_, DistributionPoint<'_>>>()?;
626+
let dps = ext.value::<asn1::SequenceOf<'_, DistributionPoint<'_, Asn1Read>>>()?;
627627
let py_dps = pyo3::types::PyList::empty(py);
628628
for dp in dps {
629629
let py_dp = parse_distribution_point(py, dp)?;

src/rust/src/x509/crl.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66

77
use cryptography_x509::extensions::{Extension, IssuerAlternativeName};
88
use cryptography_x509::{
9-
common,
9+
common::{self, Asn1Read},
1010
crl::{
1111
self, CertificateRevocationList as RawCertificateRevocationList,
1212
RevokedCertificate as RawRevokedCertificate,
@@ -350,16 +350,13 @@ impl CertificateRevocationList {
350350
Ok(Some(certificate::parse_authority_key_identifier(py, ext)?))
351351
}
352352
oid::ISSUING_DISTRIBUTION_POINT_OID => {
353-
let idp = ext.value::<crl::IssuingDistributionPoint<'_>>()?;
353+
let idp = ext.value::<crl::IssuingDistributionPoint<'_, Asn1Read>>()?;
354354
let (full_name, relative_name) = match idp.distribution_point {
355355
Some(data) => certificate::parse_distribution_point_name(py, data)?,
356356
None => (py.None().into_bound(py), py.None().into_bound(py)),
357357
};
358358
let py_reasons = if let Some(reasons) = idp.only_some_reasons {
359-
certificate::parse_distribution_point_reasons(
360-
py,
361-
Some(reasons.unwrap_read()),
362-
)?
359+
certificate::parse_distribution_point_reasons(py, Some(&reasons))?
363360
} else {
364361
py.None().into_bound(py)
365362
};

src/rust/src/x509/extensions.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
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, crl, extensions, oid};
5+
use cryptography_x509::{
6+
common::{self, Asn1Write},
7+
crl, extensions, oid,
8+
};
69

710
use crate::asn1::{py_oid_to_oid, py_uint_to_big_endian_bytes};
811
use crate::error::{CryptographyError, CryptographyResult};
@@ -118,11 +121,11 @@ pub(crate) fn encode_distribution_points<'p>(
118121
};
119122
let reasons = if let Some(py_reasons) = py_dp.reasons {
120123
let reasons = certificate::encode_distribution_point_reasons(py, &py_reasons)?;
121-
Some(common::Asn1ReadableOrWritable::new_write(reasons))
124+
Some(reasons)
122125
} else {
123126
None
124127
};
125-
dps.push(extensions::DistributionPoint {
128+
dps.push(extensions::DistributionPoint::<Asn1Write> {
126129
crl_issuer,
127130
distribution_point,
128131
reasons,
@@ -331,7 +334,7 @@ fn encode_issuing_distribution_point(
331334
{
332335
let py_reasons = ext.getattr(pyo3::intern!(py, "only_some_reasons"))?;
333336
let reasons = certificate::encode_distribution_point_reasons(ext.py(), &py_reasons)?;
334-
Some(common::Asn1ReadableOrWritable::new_write(reasons))
337+
Some(reasons)
335338
} else {
336339
None
337340
};
@@ -360,7 +363,7 @@ fn encode_issuing_distribution_point(
360363
None
361364
};
362365

363-
let idp = crl::IssuingDistributionPoint {
366+
let idp = crl::IssuingDistributionPoint::<Asn1Write> {
364367
distribution_point,
365368
indirect_crl: ext.getattr(pyo3::intern!(py, "indirect_crl"))?.extract()?,
366369
only_contains_attribute_certs: ext

0 commit comments

Comments
 (0)