Skip to main content

signstar_common/
request_api.rs

1//! Types related to Signstar requests and responses.
2
3use std::fmt::Display;
4
5use base64ct::{Base64, Encoding as _};
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9use crate::backend::BackendType;
10
11/// The type of the certificate that has been generated for the signing key.
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
14pub enum CertificateType {
15    /// OpenPGP certificate.
16    OpenPgp,
17}
18
19impl CertificateType {
20    /// Print raw certificate bytes with correct framing.
21    ///
22    /// For OpenPGP, for example, this is the same as using armor.
23    pub fn with_framing(&self, raw_bytes: &[u8]) -> CertificateData {
24        let bytes = Base64::encode_string(raw_bytes);
25        match self {
26            CertificateType::OpenPgp => CertificateData(format!(
27                "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n{bytes}\n-----END PGP PUBLIC KEY BLOCK-----"
28            )),
29        }
30    }
31}
32
33/// Certificate data, as presented to the user.
34///
35/// This representation already contains protocol framing and is base64-encoded.
36#[derive(Debug)]
37#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
38pub struct CertificateData(String);
39
40impl AsRef<str> for CertificateData {
41    fn as_ref(&self) -> &str {
42        &self.0
43    }
44}
45
46impl Display for CertificateData {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        self.0.fmt(f)
49    }
50}
51
52/// Data and metadata of a certificate residing in a Signstar backend.
53#[derive(Debug)]
54#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
55pub struct Certificate {
56    /// Base64-encoded certificate with protocol framing.
57    pub certificate: CertificateData,
58
59    /// The type of the certificate.
60    pub r#type: CertificateType,
61
62    /// The name of the backend, e.g. `NetHSM` or `YubiHSM2`.
63    pub backend_id: BackendType,
64
65    /// The signing key ID on that particular backend.
66    pub signing_key_id: String,
67}
68
69/// List of certificates stored on the Signstar host.
70#[derive(Debug)]
71#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
72pub struct Certificates {
73    /// Array of base64-encoded certificates.
74    pub certs: Vec<Certificate>,
75}