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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::{
    base64::{self, Decode},
    Error, Result,
};
use core::{fmt, str};
const ECDSA_SHA2_P256: &str = "ecdsa-sha2-nistp256";
const ECDSA_SHA2_P384: &str = "ecdsa-sha2-nistp384";
const ECDSA_SHA2_P521: &str = "ecdsa-sha2-nistp521";
const SSH_DSA: &str = "ssh-dss";
const SSH_ED25519: &str = "ssh-ed25519";
const SSH_RSA: &str = "ssh-rsa";
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Algorithm {
    
    Dsa,
    
    Ecdsa(EcdsaCurve),
    
    Ed25519,
    
    Rsa,
}
impl Algorithm {
    
    const MAX_SIZE: usize = 20;
    
    
    
    
    
    
    
    
    
    pub fn new(id: &str) -> Result<Self> {
        match id {
            ECDSA_SHA2_P256 => Ok(Algorithm::Ecdsa(EcdsaCurve::NistP256)),
            ECDSA_SHA2_P384 => Ok(Algorithm::Ecdsa(EcdsaCurve::NistP384)),
            ECDSA_SHA2_P521 => Ok(Algorithm::Ecdsa(EcdsaCurve::NistP521)),
            SSH_DSA => Ok(Algorithm::Dsa),
            SSH_ED25519 => Ok(Algorithm::Ed25519),
            SSH_RSA => Ok(Algorithm::Rsa),
            _ => Err(Error::Algorithm),
        }
    }
    
    pub fn as_str(self) -> &'static str {
        match self {
            Algorithm::Dsa => SSH_DSA,
            Algorithm::Ecdsa(EcdsaCurve::NistP256) => ECDSA_SHA2_P256,
            Algorithm::Ecdsa(EcdsaCurve::NistP384) => ECDSA_SHA2_P384,
            Algorithm::Ecdsa(EcdsaCurve::NistP521) => ECDSA_SHA2_P521,
            Algorithm::Ed25519 => SSH_ED25519,
            Algorithm::Rsa => SSH_RSA,
        }
    }
    
    pub fn is_dsa(self) -> bool {
        self == Algorithm::Dsa
    }
    
    pub fn is_ecdsa(self) -> bool {
        matches!(self, Algorithm::Ecdsa(_))
    }
    
    pub fn is_ed25519(self) -> bool {
        self == Algorithm::Ed25519
    }
    
    pub fn is_rsa(self) -> bool {
        self == Algorithm::Rsa
    }
}
impl Decode for Algorithm {
    fn decode(decoder: &mut base64::Decoder<'_>) -> Result<Self> {
        let mut buf = [0u8; Self::MAX_SIZE];
        Self::new(decoder.decode_str(&mut buf)?)
    }
}
impl fmt::Display for Algorithm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}
impl str::FromStr for Algorithm {
    type Err = Error;
    fn from_str(id: &str) -> Result<Self> {
        Self::new(id)
    }
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum CipherAlg {
    
    None,
}
impl CipherAlg {
    
    const MAX_SIZE: usize = 4;
    
    
    
    
    pub fn new(ciphername: &str) -> Result<Self> {
        match ciphername {
            "none" => Ok(CipherAlg::None),
            _ => Err(Error::Algorithm),
        }
    }
    
    pub fn as_str(self) -> &'static str {
        match self {
            CipherAlg::None => "none",
        }
    }
}
impl Decode for CipherAlg {
    fn decode(decoder: &mut base64::Decoder<'_>) -> Result<Self> {
        let mut buf = [0u8; Self::MAX_SIZE];
        Self::new(decoder.decode_str(&mut buf)?)
    }
}
impl fmt::Display for CipherAlg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}
impl str::FromStr for CipherAlg {
    type Err = Error;
    fn from_str(id: &str) -> Result<Self> {
        Self::new(id)
    }
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum EcdsaCurve {
    
    NistP256,
    
    NistP384,
    
    NistP521,
}
impl EcdsaCurve {
    
    const MAX_SIZE: usize = 8;
    
    
    
    
    
    
    
    pub fn new(id: &str) -> Result<Self> {
        match id {
            "nistp256" => Ok(EcdsaCurve::NistP256),
            "nistp384" => Ok(EcdsaCurve::NistP384),
            "nistp521" => Ok(EcdsaCurve::NistP521),
            _ => Err(Error::Algorithm),
        }
    }
    
    pub fn as_str(self) -> &'static str {
        match self {
            EcdsaCurve::NistP256 => "nistp256",
            EcdsaCurve::NistP384 => "nistp384",
            EcdsaCurve::NistP521 => "nistp521",
        }
    }
}
impl Decode for EcdsaCurve {
    fn decode(decoder: &mut base64::Decoder<'_>) -> Result<Self> {
        let mut buf = [0u8; Self::MAX_SIZE];
        Self::new(decoder.decode_str(&mut buf)?)
    }
}
impl fmt::Display for EcdsaCurve {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}
impl str::FromStr for EcdsaCurve {
    type Err = Error;
    fn from_str(id: &str) -> Result<Self> {
        EcdsaCurve::new(id)
    }
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum KdfAlg {
    
    None,
}
impl KdfAlg {
    
    const MAX_SIZE: usize = 4;
    
    
    
    
    pub fn new(kdfname: &str) -> Result<Self> {
        match kdfname {
            "none" => Ok(KdfAlg::None),
            _ => Err(Error::Algorithm),
        }
    }
    
    pub fn as_str(self) -> &'static str {
        match self {
            KdfAlg::None => "none",
        }
    }
}
impl Decode for KdfAlg {
    fn decode(decoder: &mut base64::Decoder<'_>) -> Result<Self> {
        let mut buf = [0u8; Self::MAX_SIZE];
        Self::new(decoder.decode_str(&mut buf)?)
    }
}
impl fmt::Display for KdfAlg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}
impl str::FromStr for KdfAlg {
    type Err = Error;
    fn from_str(id: &str) -> Result<Self> {
        Self::new(id)
    }
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct KdfOptions {}
impl KdfOptions {
    
    pub(crate) fn new(kdfoptions: &str) -> Result<Self> {
        
        if kdfoptions.is_empty() {
            Ok(Self {})
        } else {
            Err(Error::Algorithm)
        }
    }
}
impl Decode for KdfOptions {
    fn decode(decoder: &mut base64::Decoder<'_>) -> Result<Self> {
        let mut buf = [0u8; 0];
        Self::new(decoder.decode_str(&mut buf)?)
    }
}