ssh_key/certificate/
options_map.rs

1//! OpenSSH certificate options used by critical options and extensions.
2
3use crate::{Error, Result};
4use alloc::{collections::BTreeMap, string::String, vec::Vec};
5use core::{
6    cmp::Ordering,
7    ops::{Deref, DerefMut},
8};
9use encoding::{CheckedSum, Decode, Encode, Reader, Writer};
10
11/// Key/value map type used for certificate's critical options and extensions.
12#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
13pub struct OptionsMap(pub BTreeMap<String, String>);
14
15impl OptionsMap {
16    /// Create a new [`OptionsMap`].
17    pub fn new() -> Self {
18        Self::default()
19    }
20}
21
22impl Deref for OptionsMap {
23    type Target = BTreeMap<String, String>;
24
25    fn deref(&self) -> &Self::Target {
26        &self.0
27    }
28}
29
30impl DerefMut for OptionsMap {
31    fn deref_mut(&mut self) -> &mut Self::Target {
32        &mut self.0
33    }
34}
35
36impl Decode for OptionsMap {
37    type Error = Error;
38
39    fn decode(reader: &mut impl Reader) -> Result<Self> {
40        reader.read_prefixed(|reader| {
41            let mut entries = Vec::<(String, String)>::new();
42
43            while !reader.is_finished() {
44                let name = String::decode(reader)?;
45                let data = reader.read_prefixed(|reader| {
46                    if reader.remaining_len() > 0 {
47                        String::decode(reader)
48                    } else {
49                        Ok(String::default())
50                    }
51                })?;
52
53                // Options must be lexically ordered by "name" if they appear in
54                // the sequence. Each named option may only appear once in a
55                // certificate.
56                if let Some((prev_name, _)) = entries.last() {
57                    if prev_name.cmp(&name) != Ordering::Less {
58                        return Err(Error::FormatEncoding);
59                    }
60                }
61
62                entries.push((name, data));
63            }
64
65            Ok(OptionsMap::from_iter(entries))
66        })
67    }
68}
69
70impl Encode for OptionsMap {
71    fn encoded_len(&self) -> encoding::Result<usize> {
72        self.iter().try_fold(4, |acc, (name, data)| {
73            [
74                acc,
75                name.encoded_len()?,
76                if data.is_empty() {
77                    4
78                } else {
79                    data.encoded_len_prefixed()?
80                },
81            ]
82            .checked_sum()
83        })
84    }
85
86    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
87        self.encoded_len()?
88            .checked_sub(4)
89            .ok_or(encoding::Error::Length)?
90            .encode(writer)?;
91
92        for (name, data) in self.iter() {
93            name.encode(writer)?;
94            if data.is_empty() {
95                0usize.encode(writer)?;
96            } else {
97                data.encode_prefixed(writer)?
98            }
99        }
100
101        Ok(())
102    }
103}
104
105impl From<BTreeMap<String, String>> for OptionsMap {
106    fn from(map: BTreeMap<String, String>) -> OptionsMap {
107        OptionsMap(map)
108    }
109}
110
111impl From<OptionsMap> for BTreeMap<String, String> {
112    fn from(map: OptionsMap) -> BTreeMap<String, String> {
113        map.0
114    }
115}
116
117impl FromIterator<(String, String)> for OptionsMap {
118    fn from_iter<T>(iter: T) -> Self
119    where
120        T: IntoIterator<Item = (String, String)>,
121    {
122        BTreeMap::from_iter(iter).into()
123    }
124}