Skip to main content
This is unreleased documentation for the main (development) branch of crypto-glue.

x509_cert/ext/pkix/name/
hardware.rs

1use crate::ext::pkix::name::OtherName;
2use const_oid::db::rfc5911::ID_ON_HARDWARE_MODULE_NAME;
3use der::{
4    Any, Sequence, ValueOrd,
5    asn1::{ObjectIdentifier, OctetString},
6};
7
8/// HardwareModuleName as defined in [RFC 4108 Section 5].
9///
10/// ```text
11/// HardwareModuleName ::= SEQUENCE {
12///     hwType OBJECT IDENTIFIER,
13///     hwSerialNum OCTET STRING
14/// }
15/// ```
16///
17/// [RFC 4108 Section 5]: https://www.rfc-editor.org/rfc/rfc4108#section-5
18#[derive(Clone, Debug, Eq, PartialEq, Sequence, ValueOrd)]
19#[allow(missing_docs)]
20pub struct HardwareModuleName {
21    pub hw_type: ObjectIdentifier,
22
23    pub hw_serial_num: OctetString,
24}
25
26impl HardwareModuleName {
27    /// Convert from an [`OtherName`] if the other name is of the correct type.
28    ///
29    /// It will return `Ok(None)` if the [`OtherName`] contains another type.
30    pub fn from_other_name(other_name: &OtherName) -> der::Result<Option<Self>> {
31        if other_name.type_id == ID_ON_HARDWARE_MODULE_NAME {
32            other_name.value.decode_as().map(Some)
33        } else {
34            Ok(None)
35        }
36    }
37}
38
39impl TryFrom<&HardwareModuleName> for OtherName {
40    type Error = der::Error;
41
42    fn try_from(hmn: &HardwareModuleName) -> der::Result<Self> {
43        Ok(Self {
44            type_id: ID_ON_HARDWARE_MODULE_NAME,
45            value: Any::encode_from(hmn)?,
46        })
47    }
48}