ssh_key/certificate/
options_map.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! OpenSSH certificate options used by critical options and extensions.

use crate::{Error, Result};
use alloc::{collections::BTreeMap, string::String, vec::Vec};
use core::{
    cmp::Ordering,
    ops::{Deref, DerefMut},
};
use encoding::{CheckedSum, Decode, Encode, Reader, Writer};

/// Key/value map type used for certificate's critical options and extensions.
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct OptionsMap(pub BTreeMap<String, String>);

impl OptionsMap {
    /// Create a new [`OptionsMap`].
    pub fn new() -> Self {
        Self::default()
    }
}

impl Deref for OptionsMap {
    type Target = BTreeMap<String, String>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for OptionsMap {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Decode for OptionsMap {
    type Error = Error;

    fn decode(reader: &mut impl Reader) -> Result<Self> {
        reader.read_prefixed(|reader| {
            let mut entries = Vec::<(String, String)>::new();

            while !reader.is_finished() {
                let name = String::decode(reader)?;
                let data = reader.read_prefixed(|reader| {
                    if reader.remaining_len() > 0 {
                        String::decode(reader)
                    } else {
                        Ok(String::default())
                    }
                })?;

                // Options must be lexically ordered by "name" if they appear in
                // the sequence. Each named option may only appear once in a
                // certificate.
                if let Some((prev_name, _)) = entries.last() {
                    if prev_name.cmp(&name) != Ordering::Less {
                        return Err(Error::FormatEncoding);
                    }
                }

                entries.push((name, data));
            }

            Ok(OptionsMap::from_iter(entries))
        })
    }
}

impl Encode for OptionsMap {
    fn encoded_len(&self) -> encoding::Result<usize> {
        self.iter()
            .try_fold(4, |acc, (name, data)| {
                [
                    acc,
                    name.encoded_len()?,
                    if data.is_empty() {
                        4
                    } else {
                        data.encoded_len_prefixed()?
                    },
                ]
                .checked_sum()
            })
            .map_err(Into::into)
    }

    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
        self.encoded_len()?
            .checked_sub(4)
            .ok_or(encoding::Error::Length)?
            .encode(writer)?;

        for (name, data) in self.iter() {
            name.encode(writer)?;
            if data.is_empty() {
                0usize.encode(writer)?;
            } else {
                data.encode_prefixed(writer)?
            }
        }

        Ok(())
    }
}

impl From<BTreeMap<String, String>> for OptionsMap {
    fn from(map: BTreeMap<String, String>) -> OptionsMap {
        OptionsMap(map)
    }
}

impl From<OptionsMap> for BTreeMap<String, String> {
    fn from(map: OptionsMap) -> BTreeMap<String, String> {
        map.0
    }
}

impl FromIterator<(String, String)> for OptionsMap {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (String, String)>,
    {
        BTreeMap::from_iter(iter).into()
    }
}