|
1 | 1 | use asn1::Encode; |
2 | 2 | use rasn::types::ObjectIdentifier; |
3 | 3 |
|
4 | | -use crate::asn1::error::Asn1Error; |
| 4 | +use crate::asn1::error::AnchorAsn1Error; |
5 | 5 | use crate::asn1::{ALGORITHM_ATTRIBUTE_OIDS, SENSITIVE_ATTRIBUTE_OIDS}; |
6 | 6 |
|
7 | 7 | /// 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> { |
9 | 9 | ALGORITHM_ATTRIBUTE_OIDS |
10 | 10 | .iter() |
11 | 11 | .find(|(_, stored_oid)| *stored_oid == oid) |
12 | 12 | .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}") }) |
14 | 14 | } |
15 | 15 |
|
16 | 16 | /// 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> { |
18 | 18 | let name_str = name.as_ref(); |
19 | 19 | SENSITIVE_ATTRIBUTE_OIDS |
20 | 20 | .get(name_str) |
21 | 21 | .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}") }) |
23 | 23 | } |
24 | 24 |
|
25 | 25 | /// Convert an asn1 ObjectIdentifier to a rasn ObjectIdentifier via DER bytes. |
26 | 26 | #[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> { |
28 | 28 | // 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 | + })?; |
32 | 32 |
|
33 | 33 | // Decode the DER bytes as a rasn ObjectIdentifier using BER decoder |
34 | 34 | 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:?}") })?; |
36 | 36 |
|
37 | 37 | Ok(rasn_oid) |
38 | 38 | } |
39 | 39 |
|
40 | 40 | /// Parse an OID string into a rasn ObjectIdentifier. |
41 | 41 | /// |
42 | 42 | /// 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> { |
44 | 44 | let oid_str = oid_str.as_ref(); |
45 | 45 |
|
46 | 46 | // Parse OID string into u32 arcs |
47 | 47 | let arcs: Result<Vec<u32>, _> = oid_str.split('.').map(|s| s.parse::<u32>()).collect(); |
48 | 48 | let arcs = match arcs { |
49 | 49 | 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}") }), |
51 | 51 | }; |
52 | 52 |
|
53 | 53 | // Create ObjectIdentifier from arcs |
54 | 54 | match ObjectIdentifier::new(arcs) { |
55 | 55 | 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 | + } |
57 | 59 | } |
58 | 60 | } |
59 | 61 |
|
|
0 commit comments