Skip to main content

signstar_yubihsm2/object/
id.rs

1//! YubiHSM2 objects.
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use yubihsm::object::{Handle, Id, Type};
6
7use crate::automation::ObjectType;
8
9/// Identifier for an object stored on a YubiHSM2.
10///
11/// The YubiHSM2 provides several different types of objects.
12/// Each object type serves as a namespace, which means that an object of a specific type is
13/// isolated from objects of a different type.
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
16#[cfg_attr(
17    feature = "serde",
18    serde(tag = "object_type", content = "object_id", rename_all = "kebab-case")
19)]
20pub enum ObjectId {
21    /// Asymmetric key used for data signing.
22    AsymmetricKey(Id),
23
24    /// Authentication key used for authentication.
25    AuthenticationKey(Id),
26
27    /// Wrapping key used for exporting other objects under wrap.
28    WrappingKey(Id),
29
30    /// Opaque byte arrays which hold implementation-defined data, e.g. an OpenPGP certificate.
31    Opaque(Id),
32
33    /// HMAC-signing key.
34    Hmac(Id),
35
36    /// SSH certificate template.
37    Template(Id),
38
39    /// One-Time-Password AEAD key.
40    Otp(Id),
41}
42
43impl ObjectId {
44    /// Returns the raw identifier of the YubiHSM2 object.
45    pub fn id(&self) -> Id {
46        match self {
47            ObjectId::AsymmetricKey(id) => *id,
48            ObjectId::AuthenticationKey(id) => *id,
49            ObjectId::WrappingKey(id) => *id,
50            ObjectId::Opaque(id) => *id,
51            ObjectId::Hmac(id) => *id,
52            ObjectId::Template(id) => *id,
53            ObjectId::Otp(id) => *id,
54        }
55    }
56
57    /// Returns the type of the YubiHSM2 object.
58    pub fn object_type(&self) -> Type {
59        match self {
60            ObjectId::AsymmetricKey(_) => Type::AsymmetricKey,
61            ObjectId::AuthenticationKey(_) => Type::AuthenticationKey,
62            ObjectId::WrappingKey(_) => Type::WrapKey,
63            ObjectId::Opaque(_) => Type::Opaque,
64            ObjectId::Hmac(_) => Type::HmacKey,
65            ObjectId::Template(_) => Type::Template,
66            ObjectId::Otp(_) => Type::OtpAeadKey,
67        }
68    }
69}
70
71impl From<(ObjectType, Id)> for ObjectId {
72    fn from(value: (ObjectType, Id)) -> Self {
73        match value.0 {
74            ObjectType::AsymmetricKey => Self::AsymmetricKey(value.1),
75            ObjectType::AuthenticationKey => Self::AuthenticationKey(value.1),
76            ObjectType::HmacKey => Self::Hmac(value.1),
77            ObjectType::Opaque => Self::Opaque(value.1),
78            ObjectType::OtpAeakey => Self::Otp(value.1),
79            ObjectType::Template => Self::Template(value.1),
80            ObjectType::WrapKey => Self::WrappingKey(value.1),
81        }
82    }
83}
84
85impl From<Handle> for ObjectId {
86    fn from(value: Handle) -> Self {
87        match value.object_type {
88            Type::Opaque => ObjectId::Opaque(value.object_id),
89            Type::AuthenticationKey => ObjectId::AuthenticationKey(value.object_id),
90            Type::AsymmetricKey => ObjectId::AsymmetricKey(value.object_id),
91            Type::WrapKey => ObjectId::WrappingKey(value.object_id),
92            Type::HmacKey => ObjectId::Hmac(value.object_id),
93            Type::Template => ObjectId::Template(value.object_id),
94            Type::OtpAeadKey => ObjectId::Otp(value.object_id),
95        }
96    }
97}