Skip to content

Commit 212b75f

Browse files
committed
rust: better error types, dedupe
Signed-off-by: William Woodruff <[email protected]> extensions: unwrap -> expect Signed-off-by: William Woodruff <[email protected]>
1 parent cdd65d4 commit 212b75f

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::common;
66
use crate::extensions;
77
use crate::extensions::Extensions;
8+
use crate::extensions::ExtensionsError;
89
use crate::name;
910

1011
#[derive(asn1::Asn1Read, asn1::Asn1Write, Hash, PartialEq, Clone)]
@@ -36,7 +37,7 @@ pub struct TbsCertificate<'a> {
3637
}
3738

3839
impl<'a> TbsCertificate<'a> {
39-
pub fn extensions(&'a self) -> Result<Option<Extensions<'a>>, asn1::ObjectIdentifier> {
40+
pub fn extensions(&'a self) -> Result<Option<Extensions<'a>>, ExtensionsError> {
4041
Extensions::from_raw_extensions(self.raw_extensions.as_ref())
4142
}
4243
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ pub type RawExtensions<'a> = common::Asn1ReadableOrWritable<
1414
asn1::SequenceOfWriter<'a, Extension<'a>, Vec<Extension<'a>>>,
1515
>;
1616

17+
pub enum ExtensionsError {
18+
DuplicateOid(asn1::ObjectIdentifier),
19+
}
20+
1721
/// An invariant-enforcing wrapper for `RawExtensions`.
1822
///
1923
/// In particular, an `Extensions` cannot be constructed from a `RawExtensions`
@@ -27,14 +31,14 @@ impl<'a> Extensions<'a> {
2731
/// OID, if there are any duplicates.
2832
pub fn from_raw_extensions(
2933
raw: Option<&RawExtensions<'a>>,
30-
) -> Result<Option<Self>, asn1::ObjectIdentifier> {
34+
) -> Result<Option<Self>, ExtensionsError> {
3135
match raw {
3236
Some(raw_exts) => {
3337
let mut seen_oids = HashSet::new();
3438

3539
for ext in raw_exts.unwrap_read().clone() {
3640
if !seen_oids.insert(ext.extn_id.clone()) {
37-
return Err(ext.extn_id);
41+
return Err(ExtensionsError::DuplicateOid(ext.extn_id));
3842
}
3943
}
4044

src/rust/src/x509/certificate.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ impl Certificate {
193193
let val = self.raw.borrow_value();
194194
let mut tbs_precert = val.tbs_cert.clone();
195195
// Remove the SCT list extension
196-
match val.tbs_cert.extensions() {
197-
Ok(Some(extensions)) => {
196+
match val.tbs_cert.extensions()? {
197+
Some(extensions) => {
198198
let readable_extensions = extensions.as_raw().unwrap_read().clone();
199199
let ext_count = readable_extensions.len();
200200
let filtered_extensions: Vec<Extension<'_>> = readable_extensions
@@ -214,19 +214,11 @@ impl Certificate {
214214
let result = asn1::write_single(&tbs_precert)?;
215215
Ok(pyo3::types::PyBytes::new(py, &result))
216216
}
217-
Ok(None) => Err(CryptographyError::from(
217+
None => Err(CryptographyError::from(
218218
pyo3::exceptions::PyValueError::new_err(
219219
"Could not find any extensions in TBS certificate",
220220
),
221221
)),
222-
Err(oid) => {
223-
let oid_obj = oid_to_py_oid(py, &oid)?;
224-
Err(exceptions::DuplicateExtension::new_err((
225-
format!("Duplicate {} extension found", oid),
226-
oid_obj.into_py(py),
227-
))
228-
.into())
229-
}
230222
}
231223
}
232224

src/rust/src/x509/common.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use cryptography_x509::common::{Asn1ReadableOrWritable, AttributeTypeValue, RawT
99
use cryptography_x509::extensions::{AccessDescription, Extension, Extensions, RawExtensions};
1010
use cryptography_x509::name::{GeneralName, Name, OtherName, UnvalidatedIA5String};
1111
use pyo3::types::IntoPyDict;
12-
use pyo3::{IntoPy, ToPyObject};
12+
use pyo3::ToPyObject;
1313

1414
/// Parse all sections in a PEM file and return the first matching section.
1515
/// If no matching sections are found, return an error.
@@ -397,16 +397,8 @@ pub(crate) fn parse_and_cache_extensions<
397397
return Ok(cached.clone_ref(py));
398398
}
399399

400-
let extensions = match Extensions::from_raw_extensions(raw_extensions.as_ref()) {
401-
Ok(extensions) => extensions,
402-
Err(oid) => {
403-
let oid_obj = oid_to_py_oid(py, &oid)?;
404-
return Err(exceptions::DuplicateExtension::new_err((
405-
format!("Duplicate {} extension found", oid),
406-
oid_obj.into_py(py),
407-
)));
408-
}
409-
};
400+
let extensions = Extensions::from_raw_extensions(raw_extensions.as_ref())
401+
.map_err(CryptographyError::from)?;
410402

411403
let x509_module = py.import(pyo3::intern!(py, "cryptography.x509"))?;
412404
let exts = pyo3::types::PyList::empty(py);

src/rust/src/x509/extensions.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
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::asn1::{py_oid_to_oid, py_uint_to_big_endian_bytes};
5+
use crate::asn1::{oid_to_py_oid, py_oid_to_oid, py_uint_to_big_endian_bytes};
66
use crate::error::{CryptographyError, CryptographyResult};
7-
use crate::x509;
87
use crate::x509::{certificate, sct};
8+
use crate::{exceptions, x509};
9+
use cryptography_x509::extensions::ExtensionsError;
910
use cryptography_x509::{common, crl, extensions, oid};
11+
use pyo3::IntoPy;
12+
13+
impl From<ExtensionsError> for CryptographyError {
14+
fn from(err: ExtensionsError) -> Self {
15+
match err {
16+
ExtensionsError::DuplicateOid(oid) => pyo3::Python::with_gil(|py| {
17+
let oid_obj =
18+
oid_to_py_oid(py, &oid).expect("Failed to convert OID to Python object");
19+
exceptions::DuplicateExtension::new_err((
20+
format!("Duplicate {} extension found", oid),
21+
oid_obj.into_py(py),
22+
))
23+
.into()
24+
}),
25+
}
26+
}
27+
}
1028

1129
fn encode_general_subtrees<'a>(
1230
py: pyo3::Python<'a>,

0 commit comments

Comments
 (0)