Skip to content

Commit f01ee1d

Browse files
authored
Convert several additional extensions to use Asn1Operation (#12020)
1 parent 7124ffb commit f01ee1d

File tree

8 files changed

+58
-65
lines changed

8 files changed

+58
-65
lines changed

src/rust/cryptography-x509-verification/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::vec;
1818
use asn1::ObjectIdentifier;
1919
use cryptography_x509::extensions::{DuplicateExtensionsError, Extensions};
2020
use cryptography_x509::{
21+
common::Asn1Read,
2122
extensions::{NameConstraints, SubjectAlternativeName},
2223
name::GeneralName,
2324
oid::{NAME_CONSTRAINTS_OID, SUBJECT_ALTERNATIVE_NAME_OID},
@@ -216,7 +217,7 @@ impl<'a, 'chain> NameChain<'a, 'chain> {
216217

217218
fn evaluate_constraints<B: CryptoOps>(
218219
&self,
219-
constraints: &NameConstraints<'chain>,
220+
constraints: &NameConstraints<'chain, Asn1Read>,
220221
budget: &mut Budget,
221222
) -> ValidationResult<'chain, (), B> {
222223
if let Some(child) = self.child {
@@ -227,7 +228,7 @@ impl<'a, 'chain> NameChain<'a, 'chain> {
227228
// If there are no applicable constraints, the SAN is considered valid so the default is true.
228229
let mut permit = true;
229230
if let Some(permitted_subtrees) = &constraints.permitted_subtrees {
230-
for p in permitted_subtrees.unwrap_read().clone() {
231+
for p in permitted_subtrees.clone() {
231232
let status = self.evaluate_single_constraint(&p.base, &san, budget)?;
232233
if status.is_applied() {
233234
permit = status.is_match();
@@ -245,7 +246,7 @@ impl<'a, 'chain> NameChain<'a, 'chain> {
245246
}
246247

247248
if let Some(excluded_subtrees) = &constraints.excluded_subtrees {
248-
for e in excluded_subtrees.unwrap_read().clone() {
249+
for e in excluded_subtrees.clone() {
249250
let status = self.evaluate_single_constraint(&e.base, &san, budget)?;
250251
if status.is_match() {
251252
return Err(ValidationError::new(ValidationErrorKind::Other(

src/rust/cryptography-x509-verification/src/policy/extension.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ pub(crate) mod ee {
381381
pub(crate) mod ca {
382382
use cryptography_x509::{
383383
certificate::Certificate,
384+
common::Asn1Read,
384385
extensions::{
385386
AuthorityKeyIdentifier, BasicConstraints, ExtendedKeyUsage, Extension, KeyUsage,
386387
NameConstraints,
@@ -413,7 +414,7 @@ pub(crate) mod ca {
413414
// some chains that are not strictly CABF compliant (e.g. ones where intermediate
414415
// CAs are missing AKIs), but this is a relatively minor discrepancy.
415416
if let Some(extn) = extn {
416-
let aki: AuthorityKeyIdentifier<'_> = extn.value()?;
417+
let aki: AuthorityKeyIdentifier<'_, Asn1Read> = extn.value()?;
417418
// 7.1.2.11.1 Authority Key Identifier:
418419

419420
// keyIdentifier MUST be present.
@@ -478,16 +479,16 @@ pub(crate) mod ca {
478479
extn: Option<&Extension<'_>>,
479480
) -> ValidationResult<'chain, (), B> {
480481
if let Some(extn) = extn {
481-
let name_constraints: NameConstraints<'_> = extn.value()?;
482+
let name_constraints: NameConstraints<'_, Asn1Read> = extn.value()?;
482483

483484
let permitted_subtrees_empty = name_constraints
484485
.permitted_subtrees
485486
.as_ref()
486-
.map_or(true, |pst| pst.unwrap_read().is_empty());
487+
.map_or(true, |pst| pst.is_empty());
487488
let excluded_subtrees_empty = name_constraints
488489
.excluded_subtrees
489490
.as_ref()
490-
.map_or(true, |est| est.unwrap_read().is_empty());
491+
.map_or(true, |est| est.is_empty());
491492

492493
if permitted_subtrees_empty && excluded_subtrees_empty {
493494
return Err(ValidationError::new(ValidationErrorKind::Other(

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

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

266266
pub trait Asn1Operation {
267267
type SequenceOfVec<'a, T>
268+
where
269+
T: 'a;
270+
type SetOfVec<'a, T>
268271
where
269272
T: 'a;
270273
type OwnedBitString<'a>;
@@ -278,13 +281,21 @@ impl Asn1Operation for Asn1Read {
278281
= asn1::SequenceOf<'a, T>
279282
where
280283
T: 'a;
284+
type SetOfVec<'a, T>
285+
= asn1::SetOf<'a, T>
286+
where
287+
T: 'a;
281288
type OwnedBitString<'a> = asn1::BitString<'a>;
282289
}
283290
impl Asn1Operation for Asn1Write {
284291
type SequenceOfVec<'a, T>
285292
= asn1::SequenceOfWriter<'a, T, Vec<T>>
286293
where
287294
T: 'a;
295+
type SetOfVec<'a, T>
296+
= asn1::SetOfWriter<'a, T, Vec<T>>
297+
where
298+
T: 'a;
288299
type OwnedBitString<'a> = asn1::OwnedBitString;
289300
}
290301

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct RevokedCertificate<'a> {
4343
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
4444
pub struct IssuingDistributionPoint<'a, Op: Asn1Operation> {
4545
#[explicit(0)]
46-
pub distribution_point: Option<extensions::DistributionPointName<'a>>,
46+
pub distribution_point: Option<extensions::DistributionPointName<'a, Op>>,
4747

4848
#[implicit(1)]
4949
#[default(false)]

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,15 @@ pub enum DisplayText<'a> {
142142
BmpString(asn1::BMPString<'a>),
143143
}
144144

145-
// Needed due to clippy type complexity warning.
146-
pub type SequenceOfSubtrees<'a> = common::Asn1ReadableOrWritable<
147-
asn1::SequenceOf<'a, GeneralSubtree<'a>>,
148-
asn1::SequenceOfWriter<'a, GeneralSubtree<'a>, Vec<GeneralSubtree<'a>>>,
149-
>;
145+
pub type SequenceOfSubtrees<'a, Op> = <Op as Asn1Operation>::SequenceOfVec<'a, GeneralSubtree<'a>>;
150146

151147
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
152-
pub struct NameConstraints<'a> {
148+
pub struct NameConstraints<'a, Op: Asn1Operation> {
153149
#[implicit(0)]
154-
pub permitted_subtrees: Option<SequenceOfSubtrees<'a>>,
150+
pub permitted_subtrees: Option<SequenceOfSubtrees<'a, Op>>,
155151

156152
#[implicit(1)]
157-
pub excluded_subtrees: Option<SequenceOfSubtrees<'a>>,
153+
pub excluded_subtrees: Option<SequenceOfSubtrees<'a, Op>>,
158154
}
159155

160156
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
@@ -179,39 +175,30 @@ pub struct MSCertificateTemplate {
179175
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
180176
pub struct DistributionPoint<'a, Op: Asn1Operation> {
181177
#[explicit(0)]
182-
pub distribution_point: Option<DistributionPointName<'a>>,
178+
pub distribution_point: Option<DistributionPointName<'a, Op>>,
183179

184180
#[implicit(1)]
185181
pub reasons: crl::ReasonFlags<'a, Op>,
186182

187183
#[implicit(2)]
188-
pub crl_issuer: Option<name::SequenceOfGeneralName<'a>>,
184+
pub crl_issuer: Option<name::SequenceOfGeneralName<'a, Op>>,
189185
}
190186

191187
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
192-
pub enum DistributionPointName<'a> {
188+
pub enum DistributionPointName<'a, Op: Asn1Operation> {
193189
#[implicit(0)]
194-
FullName(name::SequenceOfGeneralName<'a>),
190+
FullName(name::SequenceOfGeneralName<'a, Op>),
195191

196192
#[implicit(1)]
197-
NameRelativeToCRLIssuer(
198-
common::Asn1ReadableOrWritable<
199-
asn1::SetOf<'a, common::AttributeTypeValue<'a>>,
200-
asn1::SetOfWriter<
201-
'a,
202-
common::AttributeTypeValue<'a>,
203-
Vec<common::AttributeTypeValue<'a>>,
204-
>,
205-
>,
206-
),
193+
NameRelativeToCRLIssuer(Op::SetOfVec<'a, common::AttributeTypeValue<'a>>),
207194
}
208195

209196
#[derive(asn1::Asn1Read, asn1::Asn1Write)]
210-
pub struct AuthorityKeyIdentifier<'a> {
197+
pub struct AuthorityKeyIdentifier<'a, Op: Asn1Operation> {
211198
#[implicit(0)]
212199
pub key_identifier: Option<&'a [u8]>,
213200
#[implicit(1)]
214-
pub authority_cert_issuer: Option<name::SequenceOfGeneralName<'a>>,
201+
pub authority_cert_issuer: Option<name::SequenceOfGeneralName<'a, Op>>,
215202
#[implicit(2)]
216203
pub authority_cert_serial_number: Option<asn1::BigUint<'a>>,
217204
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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;
5+
use crate::common::{self, Asn1Operation};
66

77
pub type NameReadable<'a> = asn1::SequenceOf<'a, asn1::SetOf<'a, common::AttributeTypeValue<'a>>>;
88

@@ -82,7 +82,5 @@ pub enum GeneralName<'a> {
8282
RegisteredID(asn1::ObjectIdentifier),
8383
}
8484

85-
pub(crate) type SequenceOfGeneralName<'a> = common::Asn1ReadableOrWritable<
86-
asn1::SequenceOf<'a, GeneralName<'a>>,
87-
asn1::SequenceOfWriter<'a, GeneralName<'a>, Vec<GeneralName<'a>>>,
88-
>;
85+
pub(crate) type SequenceOfGeneralName<'a, Op> =
86+
<Op as Asn1Operation>::SequenceOfVec<'a, GeneralName<'a>>;

src/rust/src/x509/certificate.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,28 +574,27 @@ fn parse_cp<'p>(
574574

575575
fn parse_general_subtrees<'p>(
576576
py: pyo3::Python<'p>,
577-
subtrees: SequenceOfSubtrees<'_>,
577+
subtrees: SequenceOfSubtrees<'_, Asn1Read>,
578578
) -> CryptographyResult<pyo3::Bound<'p, pyo3::PyAny>> {
579579
let gns = pyo3::types::PyList::empty(py);
580-
for gs in subtrees.unwrap_read().clone() {
580+
for gs in subtrees {
581581
gns.append(x509::parse_general_name(py, gs.base)?)?;
582582
}
583583
Ok(gns.into_any())
584584
}
585585

586586
pub(crate) fn parse_distribution_point_name<'p>(
587587
py: pyo3::Python<'p>,
588-
dp: DistributionPointName<'p>,
588+
dp: DistributionPointName<'p, Asn1Read>,
589589
) -> CryptographyResult<(pyo3::Bound<'p, pyo3::PyAny>, pyo3::Bound<'p, pyo3::PyAny>)> {
590590
Ok(match dp {
591591
DistributionPointName::FullName(data) => (
592-
x509::parse_general_names(py, data.unwrap_read())?,
592+
x509::parse_general_names(py, &data)?,
593593
py.None().into_bound(py),
594594
),
595-
DistributionPointName::NameRelativeToCRLIssuer(data) => (
596-
py.None().into_bound(py),
597-
x509::parse_rdn(py, data.unwrap_read())?,
598-
),
595+
DistributionPointName::NameRelativeToCRLIssuer(data) => {
596+
(py.None().into_bound(py), x509::parse_rdn(py, &data)?)
597+
}
599598
})
600599
}
601600

@@ -609,7 +608,7 @@ fn parse_distribution_point<'p>(
609608
};
610609
let reasons = parse_distribution_point_reasons(py, dp.reasons.as_ref())?;
611610
let crl_issuer = match dp.crl_issuer {
612-
Some(aci) => x509::parse_general_names(py, aci.unwrap_read())?,
611+
Some(aci) => x509::parse_general_names(py, &aci)?,
613612
None => py.None().into_bound(py),
614613
};
615614
Ok(types::DISTRIBUTION_POINT
@@ -674,13 +673,13 @@ pub(crate) fn parse_authority_key_identifier<'p>(
674673
py: pyo3::Python<'p>,
675674
ext: &Extension<'p>,
676675
) -> Result<pyo3::Bound<'p, pyo3::PyAny>, CryptographyError> {
677-
let aki = ext.value::<AuthorityKeyIdentifier<'_>>()?;
676+
let aki = ext.value::<AuthorityKeyIdentifier<'_, Asn1Read>>()?;
678677
let serial = match aki.authority_cert_serial_number {
679678
Some(biguint) => big_byte_slice_to_py_int(py, biguint.as_bytes())?.unbind(),
680679
None => py.None(),
681680
};
682681
let issuer = match aki.authority_cert_issuer {
683-
Some(aci) => x509::parse_general_names(py, aci.unwrap_read())?,
682+
Some(aci) => x509::parse_general_names(py, &aci)?,
684683
None => py.None().into_bound(py),
685684
};
686685
Ok(types::AUTHORITY_KEY_IDENTIFIER
@@ -911,7 +910,7 @@ pub fn parse_cert_ext<'p>(
911910
Ok(Some(types::FRESHEST_CRL.get(py)?.call1((dp,))?))
912911
}
913912
oid::NAME_CONSTRAINTS_OID => {
914-
let nc = ext.value::<NameConstraints<'_>>()?;
913+
let nc = ext.value::<NameConstraints<'_, Asn1Read>>()?;
915914
let permitted_subtrees = match nc.permitted_subtrees {
916915
Some(data) => parse_general_subtrees(py, data)?,
917916
None => py.None().into_bound(py),

src/rust/src/x509/extensions.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn encode_general_subtrees<'a>(
1919
ka_bytes: &'a cryptography_keepalive::KeepAlive<pyo3::pybacked::PyBackedBytes>,
2020
ka_str: &'a cryptography_keepalive::KeepAlive<pyo3::pybacked::PyBackedStr>,
2121
subtrees: &pyo3::Bound<'a, pyo3::PyAny>,
22-
) -> Result<Option<extensions::SequenceOfSubtrees<'a>>, CryptographyError> {
22+
) -> Result<Option<extensions::SequenceOfSubtrees<'a, Asn1Write>>, CryptographyError> {
2323
if subtrees.is_none() {
2424
Ok(None)
2525
} else {
@@ -32,9 +32,7 @@ fn encode_general_subtrees<'a>(
3232
maximum: None,
3333
});
3434
}
35-
Ok(Some(common::Asn1ReadableOrWritable::new_write(
36-
asn1::SequenceOfWriter::new(subtree_seq),
37-
)))
35+
Ok(Some(asn1::SequenceOfWriter::new(subtree_seq)))
3836
}
3937
}
4038

@@ -55,9 +53,7 @@ pub(crate) fn encode_authority_key_identifier<'a>(
5553
let authority_cert_issuer = if let Some(authority_cert_issuer) = aki.authority_cert_issuer {
5654
let gns =
5755
x509::common::encode_general_names(py, &ka_bytes, &ka_str, &authority_cert_issuer)?;
58-
Some(common::Asn1ReadableOrWritable::new_write(
59-
asn1::SequenceOfWriter::new(gns),
60-
))
56+
Some(asn1::SequenceOfWriter::new(gns))
6157
} else {
6258
None
6359
};
@@ -69,7 +65,9 @@ pub(crate) fn encode_authority_key_identifier<'a>(
6965
} else {
7066
None
7167
};
72-
Ok(asn1::write_single(&extensions::AuthorityKeyIdentifier {
68+
Ok(asn1::write_single(&extensions::AuthorityKeyIdentifier::<
69+
Asn1Write,
70+
> {
7371
authority_cert_issuer,
7472
authority_cert_serial_number,
7573
key_identifier: aki.key_identifier.as_deref(),
@@ -96,16 +94,14 @@ pub(crate) fn encode_distribution_points<'p>(
9694

9795
let crl_issuer = if let Some(py_crl_issuer) = py_dp.crl_issuer {
9896
let gns = x509::common::encode_general_names(py, &ka_bytes, &ka_str, &py_crl_issuer)?;
99-
Some(common::Asn1ReadableOrWritable::new_write(
100-
asn1::SequenceOfWriter::new(gns),
101-
))
97+
Some(asn1::SequenceOfWriter::new(gns))
10298
} else {
10399
None
104100
};
105101
let distribution_point = if let Some(py_full_name) = py_dp.full_name {
106102
let gns = x509::common::encode_general_names(py, &ka_bytes, &ka_str, &py_full_name)?;
107103
Some(extensions::DistributionPointName::FullName(
108-
common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(gns)),
104+
asn1::SequenceOfWriter::new(gns),
109105
))
110106
} else if let Some(py_relative_name) = py_dp.relative_name {
111107
let mut name_entries = vec![];
@@ -114,7 +110,7 @@ pub(crate) fn encode_distribution_points<'p>(
114110
name_entries.push(ne);
115111
}
116112
Some(extensions::DistributionPointName::NameRelativeToCRLIssuer(
117-
common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(name_entries)),
113+
asn1::SetOfWriter::new(name_entries),
118114
))
119115
} else {
120116
None
@@ -338,7 +334,7 @@ fn encode_issuing_distribution_point(
338334
let py_full_name = ext.getattr(pyo3::intern!(py, "full_name"))?;
339335
let gns = x509::common::encode_general_names(ext.py(), &ka_bytes, &ka_str, &py_full_name)?;
340336
Some(extensions::DistributionPointName::FullName(
341-
common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(gns)),
337+
asn1::SequenceOfWriter::new(gns),
342338
))
343339
} else if ext
344340
.getattr(pyo3::intern!(py, "relative_name"))?
@@ -353,7 +349,7 @@ fn encode_issuing_distribution_point(
353349
name_entries.push(name_entry);
354350
}
355351
Some(extensions::DistributionPointName::NameRelativeToCRLIssuer(
356-
common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(name_entries)),
352+
asn1::SetOfWriter::new(name_entries),
357353
))
358354
} else {
359355
None
@@ -610,7 +606,7 @@ pub(crate) fn encode_extension(
610606

611607
let permitted = ext.getattr(pyo3::intern!(py, "permitted_subtrees"))?;
612608
let excluded = ext.getattr(pyo3::intern!(py, "excluded_subtrees"))?;
613-
let nc = extensions::NameConstraints {
609+
let nc = extensions::NameConstraints::<Asn1Write> {
614610
permitted_subtrees: encode_general_subtrees(
615611
ext.py(),
616612
&ka_bytes,

0 commit comments

Comments
 (0)