Skip to content

Commit b16b741

Browse files
committed
Ergonomic improvements.
1 parent 61459ec commit b16b741

File tree

17 files changed

+459
-241
lines changed

17 files changed

+459
-241
lines changed

src/lib/asn1/error.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use snafu::Snafu;
33
/// Error type for ASN.1.
44
#[derive(Debug, Snafu)]
55
#[snafu(visibility(pub))]
6-
pub enum Asn1Error {
6+
pub enum AnchorAsn1Error {
77
#[snafu(display("Invalid OID: {message}"))]
88
InvalidOid { message: String },
99

@@ -14,7 +14,7 @@ pub enum Asn1Error {
1414
Asn1DecodeError { source: rasn::error::DecodeError },
1515
}
1616

17-
crate::impl_source_error_from!(Asn1Error, {
17+
crate::impl_source_error_from!(AnchorAsn1Error, {
1818
rasn::error::EncodeError => Asn1EncodeError,
1919
rasn::error::DecodeError => Asn1DecodeError,
2020
});
@@ -26,7 +26,7 @@ mod tests {
2626

2727
test_error_from_conversions!(
2828
test_from_conversions,
29-
Asn1Error,
29+
AnchorAsn1Error,
3030
[
3131
rasn::error::EncodeError::length_exceeds_platform_size(rasn::Codec::Der),
3232
rasn::error::DecodeError::integer_overflow(100, rasn::Codec::Der),
@@ -36,11 +36,13 @@ mod tests {
3636
test_error_variants!(
3737
test_error_variants,
3838
[
39-
Asn1Error::InvalidOid { message: "test.oid".to_string() },
40-
Asn1Error::Asn1EncodeError {
39+
AnchorAsn1Error::InvalidOid { message: "test.oid".to_string() },
40+
AnchorAsn1Error::Asn1EncodeError {
4141
source: rasn::error::EncodeError::length_exceeds_platform_size(rasn::Codec::Der)
4242
},
43-
Asn1Error::Asn1DecodeError { source: rasn::error::DecodeError::integer_overflow(100, rasn::Codec::Der) },
43+
AnchorAsn1Error::Asn1DecodeError {
44+
source: rasn::error::DecodeError::integer_overflow(100, rasn::Codec::Der)
45+
},
4446
]
4547
);
4648
}

src/lib/asn1/utils.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
11
use asn1::Encode;
22
use rasn::types::ObjectIdentifier;
33

4-
use crate::asn1::error::Asn1Error;
4+
use crate::asn1::error::AnchorAsn1Error;
55
use crate::asn1::{ALGORITHM_ATTRIBUTE_OIDS, SENSITIVE_ATTRIBUTE_OIDS};
66

77
/// Lookup algorithm name by OID
8-
pub fn get_algorithm_by_oid(oid: &ObjectIdentifier) -> Result<&'static str, Asn1Error> {
8+
pub fn get_algorithm_by_oid(oid: &ObjectIdentifier) -> Result<&'static str, AnchorAsn1Error> {
99
ALGORITHM_ATTRIBUTE_OIDS
1010
.iter()
1111
.find(|(_, stored_oid)| *stored_oid == oid)
1212
.map(|(name, _)| *name)
13-
.ok_or_else(|| Asn1Error::InvalidOid { message: format!("Unknown algorithm OID: {oid}") })
13+
.ok_or_else(|| AnchorAsn1Error::InvalidOid { message: format!("Unknown algorithm OID: {oid}") })
1414
}
1515

1616
/// Get OID for certificate attribute
17-
pub fn get_sensitive_attribute_oid<T: AsRef<str>>(name: T) -> Result<ObjectIdentifier, Asn1Error> {
17+
pub fn get_sensitive_attribute_oid<T: AsRef<str>>(name: T) -> Result<ObjectIdentifier, AnchorAsn1Error> {
1818
let name_str = name.as_ref();
1919
SENSITIVE_ATTRIBUTE_OIDS
2020
.get(name_str)
2121
.cloned()
22-
.ok_or_else(|| Asn1Error::InvalidOid { message: format!("Unknown sensitive attribute: {name_str}") })
22+
.ok_or_else(|| AnchorAsn1Error::InvalidOid { message: format!("Unknown sensitive attribute: {name_str}") })
2323
}
2424

2525
/// Convert an asn1 ObjectIdentifier to a rasn ObjectIdentifier via DER bytes.
2626
#[allow(dead_code)]
27-
pub(crate) fn as_rasn_oid(oid: asn1::ObjectIdentifier) -> Result<rasn::types::ObjectIdentifier, Asn1Error> {
27+
pub(crate) fn as_rasn_oid(oid: asn1::ObjectIdentifier) -> Result<rasn::types::ObjectIdentifier, AnchorAsn1Error> {
2828
// Convert asn1 OID to DER bytes
29-
let der_bytes = oid
30-
.to_der()
31-
.map_err(|e| Asn1Error::InvalidOid { message: format!("Failed to encode ObjectIdentifier to DER: {e:?}") })?;
29+
let der_bytes = oid.to_der().map_err(|e| AnchorAsn1Error::InvalidOid {
30+
message: format!("Failed to encode ObjectIdentifier to DER: {e:?}"),
31+
})?;
3232

3333
// Decode the DER bytes as a rasn ObjectIdentifier using BER decoder
3434
let rasn_oid = rasn::ber::decode::<rasn::types::ObjectIdentifier>(&der_bytes)
35-
.map_err(|e| Asn1Error::InvalidOid { message: format!("Failed to decode ObjectIdentifier: {e:?}") })?;
35+
.map_err(|e| AnchorAsn1Error::InvalidOid { message: format!("Failed to decode ObjectIdentifier: {e:?}") })?;
3636

3737
Ok(rasn_oid)
3838
}
3939

4040
/// Parse an OID string into a rasn ObjectIdentifier.
4141
///
4242
/// Takes a string like "1.2.3.4.5" and converts it to an ObjectIdentifier
43-
pub fn parse_oid_string<S: AsRef<str>>(oid_str: S) -> Result<ObjectIdentifier, Asn1Error> {
43+
pub fn parse_oid_string<S: AsRef<str>>(oid_str: S) -> Result<ObjectIdentifier, AnchorAsn1Error> {
4444
let oid_str = oid_str.as_ref();
4545

4646
// Parse OID string into u32 arcs
4747
let arcs: Result<Vec<u32>, _> = oid_str.split('.').map(|s| s.parse::<u32>()).collect();
4848
let arcs = match arcs {
4949
Ok(arcs) => arcs,
50-
Err(e) => return Err(Asn1Error::InvalidOid { message: format!("Failed to parse OID '{oid_str}': {e}") }),
50+
Err(e) => return Err(AnchorAsn1Error::InvalidOid { message: format!("Failed to parse OID '{oid_str}': {e}") }),
5151
};
5252

5353
// Create ObjectIdentifier from arcs
5454
match ObjectIdentifier::new(arcs) {
5555
Some(oid) => Ok(oid),
56-
None => Err(Asn1Error::InvalidOid { message: format!("Failed to create ObjectIdentifier from '{oid_str}'") }),
56+
None => {
57+
Err(AnchorAsn1Error::InvalidOid { message: format!("Failed to create ObjectIdentifier from '{oid_str}'") })
58+
}
5759
}
5860
}
5961

0 commit comments

Comments
 (0)