Skip to main content

csv_rs/certs/
mod.rs

1// Copyright (C) Hygon Info Technologies Ltd.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Everything needed for working with HYGON CSV certificate chains.
6
7pub mod builtin;
8pub mod ca;
9mod chain;
10pub mod csv;
11
12use serde::{Deserialize, Serialize};
13use std::{
14    convert::*,
15    io::{Error, ErrorKind, Read, Result, Write},
16};
17
18pub use chain::Chain;
19
20use openssl::hash;
21
22/// An interface for types that may containe entities
23/// such as signatures that must be verified.
24pub trait Verifiable {
25    /// An output type for successful verification.
26    type Output;
27
28    /// Self-verifies signatures.
29    fn verify(self) -> Result<Self::Output>;
30}
31
32/// An interface for types that can sign another type (i.e., a certificate).
33pub trait Signer<T> {
34    /// The now-signed type.
35    type Output;
36
37    /// Signs the target.
38    fn sign(&self, target: &mut T, uid: String) -> Result<Self::Output>;
39}
40
41/// Denotes a certificate's usage.
42#[repr(C)]
43#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
44pub struct Usage(u32);
45
46impl Usage {
47    /// HYGON Root Key.
48    pub const HRK: Usage = Usage(0x0000u32.to_le());
49
50    /// HYGON Signing key.
51    pub const HSK: Usage = Usage(0x0013u32.to_le());
52
53    /// Owner Certificate Authority.
54    pub const OCA: Usage = Usage(0x1001u32.to_le());
55
56    /// Platform Endorsement Key.
57    pub const PEK: Usage = Usage(0x1002u32.to_le());
58
59    /// Platform Diffie-Hellman.
60    pub const PDH: Usage = Usage(0x1003u32.to_le());
61
62    /// Chip Endorsement Key.
63    pub const CEK: Usage = Usage(0x1004u32.to_le());
64
65    const INV: Usage = Usage(0x1000u32.to_le());
66}
67
68impl From<u32> for Usage {
69    fn from(value: u32) -> Self {
70        Self(value.to_le())
71    }
72}
73
74impl TryFrom<Usage> for String {
75    type Error = Error;
76
77    fn try_from(value: Usage) -> Result<Self> {
78        match value {
79            Usage::HRK => Ok(String::from("HYGON-SSD-HRK")),
80            Usage::HSK => Ok(String::from("HYGON-SSD-HSK")),
81            Usage::OCA => Ok(String::from("HYGON-SSD-OCA")),
82            Usage::PEK => Ok(String::from("HYGON-SSD-PEK")),
83            Usage::PDH => Ok(String::from("HYGON-SSD-PDH")),
84            Usage::CEK => Ok(String::from("HYGON-SSD-CEK")),
85
86            _ => Err(ErrorKind::InvalidInput.into()),
87        }
88    }
89}
90
91impl TryFrom<Usage> for Algorithm {
92    type Error = Error;
93
94    fn try_from(value: Usage) -> Result<Self> {
95        match value {
96            Usage::PDH => Ok(Algorithm::SM2_DH),
97            Usage::HRK | Usage::HSK | Usage::OCA | Usage::PEK | Usage::CEK => Ok(Algorithm::SM2_SA),
98
99            _ => Err(ErrorKind::InvalidInput.into()),
100        }
101    }
102}
103
104#[repr(C)]
105#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
106pub struct Algorithm(u32);
107
108impl Algorithm {
109    pub const SM2_SA: Algorithm = Algorithm(0x0004u32.to_le());
110    pub const SM2_DH: Algorithm = Algorithm(0x0005u32.to_le());
111    pub const NONE: Algorithm = Algorithm(0x0000u32.to_le());
112}
113
114impl From<u32> for Algorithm {
115    fn from(value: u32) -> Self {
116        Self(value.to_le())
117    }
118}
119
120impl TryFrom<Algorithm> for hash::MessageDigest {
121    type Error = Error;
122
123    fn try_from(value: Algorithm) -> Result<Self> {
124        match value {
125            Algorithm::SM2_SA | Algorithm::SM2_DH => Ok(hash::MessageDigest::sm3()),
126
127            _ => Err(ErrorKind::InvalidInput.into()),
128        }
129    }
130}