Skip to main content

signstar_yubihsm2/object/
id.rs

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