ssh_key/certificate/
cert_type.rs

1//! OpenSSH certificate types.
2
3use crate::{Error, Result};
4use encoding::{Decode, Encode, Reader, Writer};
5
6/// Types of OpenSSH certificates: user or host.
7#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
8#[repr(u32)]
9pub enum CertType {
10    /// User certificate
11    User = 1,
12
13    /// Host certificate
14    Host = 2,
15}
16
17impl CertType {
18    /// Is this a host certificate?
19    pub fn is_host(self) -> bool {
20        self == CertType::Host
21    }
22
23    /// Is this a user certificate?
24    pub fn is_user(self) -> bool {
25        self == CertType::User
26    }
27}
28
29impl Default for CertType {
30    fn default() -> Self {
31        Self::User
32    }
33}
34
35impl Decode for CertType {
36    type Error = Error;
37
38    fn decode(reader: &mut impl Reader) -> Result<Self> {
39        u32::decode(reader)?.try_into()
40    }
41}
42
43impl Encode for CertType {
44    fn encoded_len(&self) -> encoding::Result<usize> {
45        Ok(4)
46    }
47
48    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
49        u32::from(*self).encode(writer)?;
50        Ok(())
51    }
52}
53
54impl From<CertType> for u32 {
55    fn from(cert_type: CertType) -> u32 {
56        cert_type as u32
57    }
58}
59
60impl TryFrom<u32> for CertType {
61    type Error = Error;
62
63    fn try_from(n: u32) -> Result<CertType> {
64        match n {
65            1 => Ok(CertType::User),
66            2 => Ok(CertType::Host),
67            _ => Err(Error::FormatEncoding),
68        }
69    }
70}