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

Convert the remaining extensions to use Asn1Operation #12030

Merged
merged 1 commit into from
Nov 24, 2024
Merged
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
31 changes: 10 additions & 21 deletions src/rust/cryptography-x509/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,45 +273,34 @@ pub struct NamingAuthority<'a> {
pub text: Option<DisplayText<'a>>,
}

type SequenceOfDisplayTexts<'a> = common::Asn1ReadableOrWritable<
asn1::SequenceOf<'a, DisplayText<'a>>,
asn1::SequenceOfWriter<'a, DisplayText<'a>, Vec<DisplayText<'a>>>,
>;
type SequenceOfDisplayTexts<'a, Op> = <Op as Asn1Operation>::SequenceOfVec<'a, DisplayText<'a>>;

type SequenceOfObjectIdentifiers<'a> = common::Asn1ReadableOrWritable<
asn1::SequenceOf<'a, asn1::ObjectIdentifier>,
asn1::SequenceOfWriter<'a, asn1::ObjectIdentifier, Vec<asn1::ObjectIdentifier>>,
>;
type SequenceOfObjectIdentifiers<'a, Op> =
<Op as Asn1Operation>::SequenceOfVec<'a, asn1::ObjectIdentifier>;

#[derive(asn1::Asn1Read, asn1::Asn1Write)]
pub struct ProfessionInfo<'a> {
pub struct ProfessionInfo<'a, Op: Asn1Operation> {
#[explicit(0)]
pub naming_authority: Option<NamingAuthority<'a>>,
pub profession_items: SequenceOfDisplayTexts<'a>,
pub profession_oids: Option<SequenceOfObjectIdentifiers<'a>>,
pub profession_items: SequenceOfDisplayTexts<'a, Op>,
pub profession_oids: Option<SequenceOfObjectIdentifiers<'a, Op>>,
pub registration_number: Option<asn1::PrintableString<'a>>,
pub add_profession_info: Option<&'a [u8]>,
}

#[derive(asn1::Asn1Read, asn1::Asn1Write)]
pub struct Admission<'a> {
pub struct Admission<'a, Op: Asn1Operation + 'a> {
#[explicit(0)]
pub admission_authority: Option<name::GeneralName<'a>>,
#[explicit(1)]
pub naming_authority: Option<NamingAuthority<'a>>,
pub profession_infos: common::Asn1ReadableOrWritable<
asn1::SequenceOf<'a, ProfessionInfo<'a>>,
asn1::SequenceOfWriter<'a, ProfessionInfo<'a>, Vec<ProfessionInfo<'a>>>,
>,
pub profession_infos: Op::SequenceOfVec<'a, ProfessionInfo<'a, Op>>,
}

#[derive(asn1::Asn1Read, asn1::Asn1Write)]
pub struct Admissions<'a> {
pub struct Admissions<'a, Op: Asn1Operation> {
pub admission_authority: Option<name::GeneralName<'a>>,
pub contents_of_admissions: common::Asn1ReadableOrWritable<
asn1::SequenceOf<'a, Admission<'a>>,
asn1::SequenceOfWriter<'a, Admission<'a>, Vec<Admission<'a>>>,
>,
pub contents_of_admissions: Op::SequenceOfVec<'a, Admission<'a, Op>>,
}

#[cfg(test)]
Expand Down
15 changes: 7 additions & 8 deletions src/rust/src/x509/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ fn parse_naming_authority<'p>(

fn parse_profession_infos<'p, 'a>(
py: pyo3::Python<'p>,
profession_infos: &asn1::SequenceOf<'a, ProfessionInfo<'a>>,
profession_infos: &asn1::SequenceOf<'a, ProfessionInfo<'a, Asn1Read>>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::PyAny>> {
let py_infos = pyo3::types::PyList::empty(py);
for info in profession_infos.clone() {
Expand All @@ -735,14 +735,14 @@ fn parse_profession_infos<'p, 'a>(
None => py.None().into_bound(py),
};
let py_profession_items = pyo3::types::PyList::empty(py);
for item in info.profession_items.unwrap_read().clone() {
for item in info.profession_items {
let py_item = parse_display_text(py, item)?;
py_profession_items.append(py_item)?;
}
let py_profession_oids = match info.profession_oids {
Some(oids) => {
let py_oids = pyo3::types::PyList::empty(py);
for oid in oids.unwrap_read().clone() {
for oid in oids {
let py_oid = oid_to_py_oid(py, &oid)?;
py_oids.append(py_oid)?;
}
Expand Down Expand Up @@ -772,7 +772,7 @@ fn parse_profession_infos<'p, 'a>(

fn parse_admissions<'p, 'a>(
py: pyo3::Python<'p>,
admissions: &asn1::SequenceOf<'a, Admission<'a>>,
admissions: &asn1::SequenceOf<'a, Admission<'a, Asn1Read>>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::PyAny>> {
let py_admissions = pyo3::types::PyList::empty(py);
for admission in admissions.clone() {
Expand All @@ -784,7 +784,7 @@ fn parse_admissions<'p, 'a>(
Some(data) => parse_naming_authority(py, data)?,
None => py.None().into_bound(py),
};
let py_infos = parse_profession_infos(py, admission.profession_infos.unwrap_read())?;
let py_infos = parse_profession_infos(py, &admission.profession_infos)?;

let py_entry = types::ADMISSION.get(py)?.call1((
py_admission_authority,
Expand Down Expand Up @@ -935,13 +935,12 @@ pub fn parse_cert_ext<'p>(
))?))
}
oid::ADMISSIONS_OID => {
let admissions = ext.value::<Admissions<'_>>()?;
let admissions = ext.value::<Admissions<'_, Asn1Read>>()?;
let admission_authority = match admissions.admission_authority {
Some(authority) => x509::parse_general_name(py, authority)?,
None => py.None().into_bound(py),
};
let py_admissions =
parse_admissions(py, admissions.contents_of_admissions.unwrap_read())?;
let py_admissions = parse_admissions(py, &admissions.contents_of_admissions)?;
Ok(Some(
types::ADMISSIONS
.get(py)?
Expand Down
24 changes: 8 additions & 16 deletions src/rust/src/x509/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.

use cryptography_x509::{
common::{self, Asn1Write},
crl, extensions, oid,
};
use cryptography_x509::{common::Asn1Write, crl, extensions, oid};

use crate::asn1::{py_oid_to_oid, py_uint_to_big_endian_bytes};
use crate::error::{CryptographyError, CryptographyResult};
Expand Down Expand Up @@ -456,7 +453,7 @@ fn encode_profession_info<'a>(
ka_bytes: &'a cryptography_keepalive::KeepAlive<pyo3::pybacked::PyBackedBytes>,
ka_str: &'a cryptography_keepalive::KeepAlive<pyo3::pybacked::PyBackedStr>,
py_info: &pyo3::Bound<'a, pyo3::PyAny>,
) -> CryptographyResult<extensions::ProfessionInfo<'a>> {
) -> CryptographyResult<extensions::ProfessionInfo<'a, Asn1Write>> {
let py_naming_authority = py_info.getattr(pyo3::intern!(py, "naming_authority"))?;
let naming_authority = if !py_naming_authority.is_none() {
Some(encode_naming_authority(py, ka_str, &py_naming_authority)?)
Expand All @@ -471,8 +468,7 @@ fn encode_profession_info<'a>(
let item = extensions::DisplayText::Utf8String(asn1::Utf8String::new(py_item_str));
profession_items.push(item);
}
let profession_items =
common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(profession_items));
let profession_items = asn1::SequenceOfWriter::new(profession_items);
let py_oids = py_info.getattr(pyo3::intern!(py, "profession_oids"))?;
let profession_oids = if !py_oids.is_none() {
let mut profession_oids = vec![];
Expand All @@ -481,9 +477,7 @@ fn encode_profession_info<'a>(
let oid = py_oid_to_oid(py_oid)?;
profession_oids.push(oid);
}
Some(common::Asn1ReadableOrWritable::new_write(
asn1::SequenceOfWriter::new(profession_oids),
))
Some(asn1::SequenceOfWriter::new(profession_oids))
} else {
None
};
Expand Down Expand Up @@ -524,7 +518,7 @@ fn encode_admission<'a>(
ka_bytes: &'a cryptography_keepalive::KeepAlive<pyo3::pybacked::PyBackedBytes>,
ka_str: &'a cryptography_keepalive::KeepAlive<pyo3::pybacked::PyBackedStr>,
py_admission: &pyo3::Bound<'a, pyo3::PyAny>,
) -> CryptographyResult<extensions::Admission<'a>> {
) -> CryptographyResult<extensions::Admission<'a, Asn1Write>> {
let py_admission_authority = py_admission.getattr(pyo3::intern!(py, "admission_authority"))?;
let admission_authority = if !py_admission_authority.is_none() {
Some(x509::common::encode_general_name(
Expand All @@ -548,8 +542,7 @@ fn encode_admission<'a>(
for py_info in py_profession_infos.try_iter()? {
profession_infos.push(encode_profession_info(py, ka_bytes, ka_str, &py_info?)?);
}
let profession_infos =
common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(profession_infos));
let profession_infos = asn1::SequenceOfWriter::new(profession_infos);
Ok(extensions::Admission {
admission_authority,
naming_authority,
Expand Down Expand Up @@ -726,10 +719,9 @@ pub(crate) fn encode_extension(
admissions.push(admission);
}

let contents_of_admissions =
common::Asn1ReadableOrWritable::new_write(asn1::SequenceOfWriter::new(admissions));
let contents_of_admissions = asn1::SequenceOfWriter::new(admissions);

let admission = extensions::Admissions {
let admission = extensions::Admissions::<Asn1Write> {
admission_authority,
contents_of_admissions,
};
Expand Down