Skip to main content

csv_rs/certs/ca/
chain.rs

1// Copyright (C) Hygon Info Technologies Ltd.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! For operating on Certificate Authority chains.
6
7use super::*;
8use crate::certs::{ca::cert::Certificate, Usage};
9
10use serde::{Deserialize, Serialize};
11
12/// A complete Certificate Authority chain.
13#[repr(C)]
14#[derive(Deserialize, Serialize)]
15pub struct Chain {
16    /// The HYGON Sighing Key certificate.
17    pub hsk: Certificate,
18
19    /// The HYGON Root Key certificate.
20    pub hrk: Certificate,
21}
22
23impl codicon::Decoder<()> for Chain {
24    type Error = Error;
25
26    fn decode(mut reader: impl Read, _: ()) -> Result<Self> {
27        let hsk = Certificate::decode(&mut reader, ())?;
28        if Usage::try_from(&hsk)? != Usage::HSK {
29            return Err(ErrorKind::InvalidInput.into());
30        }
31
32        let hrk: Certificate = Certificate::decode(&mut reader, ())?;
33        if Usage::try_from(&hrk)? != Usage::HRK {
34            return Err(ErrorKind::InvalidInput.into());
35        }
36
37        Ok(Self { hsk, hrk })
38    }
39}
40
41impl codicon::Encoder<()> for Chain {
42    type Error = Error;
43
44    fn encode(&self, mut writer: impl Write, _: ()) -> Result<()> {
45        self.hsk.encode(&mut writer, crate::Body)?;
46        self.hrk.encode(&mut writer, crate::Body)
47    }
48}
49
50impl<'a> Verifiable for &'a Chain {
51    type Output = &'a Certificate;
52
53    fn verify(self) -> Result<Self::Output> {
54        (&self.hrk, &self.hrk).verify()?;
55        (&self.hrk, &self.hsk).verify()?;
56        Ok(&self.hsk)
57    }
58}