Skip to main content

csv_rs/certs/
chain.rs

1// Copyright (C) Hygon Info Technologies Ltd.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Utilities for operating on entire certificate chains.
6
7use super::*;
8use crate::certs::{ca, csv};
9
10use serde::{Deserialize, Serialize};
11
12/// A complete certificate chain.
13#[repr(C)]
14#[derive(Deserialize, Serialize)]
15pub struct Chain {
16    /// The Certificate Authority chain
17    pub ca: ca::Chain,
18
19    /// The CSV platform chain.
20    pub csv: csv::Chain,
21}
22
23impl codicon::Decoder<()> for Chain {
24    type Error = Error;
25
26    fn decode(mut reader: impl Read, _: ()) -> Result<Self> {
27        let csv = csv::Chain::decode(&mut reader, ())?;
28        let ca = ca::Chain::decode(&mut reader, ())?;
29        Ok(Self { ca, csv })
30    }
31}
32
33impl codicon::Encoder<()> for Chain {
34    type Error = Error;
35
36    fn encode(&self, mut writer: impl Write, _: ()) -> Result<()> {
37        self.csv.encode(&mut writer, ())?;
38        self.ca.encode(&mut writer, ())
39    }
40}
41
42impl<'a> Verifiable for &'a Chain {
43    type Output = &'a csv::Certificate;
44
45    fn verify(self) -> Result<Self::Output> {
46        let ask = self.ca.verify()?;
47        (ask, &self.csv.cek).verify()?;
48        self.csv.verify()
49    }
50}