Skip to main content

ssh_key/private/
sk.rs

1//! Security Key (FIDO/U2F) private keys as described in [PROTOCOL.u2f].
2//!
3//! [PROTOCOL.u2f]: https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.u2f?annotate=HEAD
4
5use crate::{Error, Result, public};
6use alloc::vec::Vec;
7use encoding::{Decode, Encode};
8
9/// Security Key (FIDO/U2F) ECDSA/NIST P-256 private key as specified in
10/// [PROTOCOL.u2f](https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.u2f?annotate=HEAD).
11#[cfg(all(feature = "alloc", feature = "ecdsa"))]
12#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, PartialOrd, Ord)]
13#[ssh(error = Error)]
14pub struct SkEcdsaSha2NistP256 {
15    /// Public key.
16    public: public::SkEcdsaSha2NistP256,
17
18    /// Flags.
19    flags: u8,
20
21    /// FIDO/U2F key handle.
22    key_handle: Vec<u8>,
23
24    /// Reserved data.
25    reserved: Vec<u8>,
26}
27
28#[cfg(feature = "ecdsa")]
29impl SkEcdsaSha2NistP256 {
30    /// Construct new instance of SkEcdsaSha2NistP256.
31    ///
32    /// # Errors
33    /// Returns [`Error::Encoding`] in the event of an encoding error.
34    #[cfg(feature = "alloc")]
35    pub fn new(
36        public: public::SkEcdsaSha2NistP256,
37        flags: u8,
38        key_handle: impl Into<Vec<u8>>,
39    ) -> Result<Self> {
40        let key_handle = key_handle.into();
41
42        if key_handle.len() <= 255 {
43            Ok(SkEcdsaSha2NistP256 {
44                public,
45                flags,
46                key_handle,
47                reserved: Vec::<u8>::new(),
48            })
49        } else {
50            Err(encoding::Error::Length.into())
51        }
52    }
53
54    /// Get the ECDSA/NIST P-256 public key.
55    #[must_use]
56    pub fn public(&self) -> &public::SkEcdsaSha2NistP256 {
57        &self.public
58    }
59
60    /// Get flags.
61    #[must_use]
62    pub fn flags(&self) -> u8 {
63        self.flags
64    }
65
66    /// Get FIDO/U2F key handle.
67    #[must_use]
68    pub fn key_handle(&self) -> &[u8] {
69        &self.key_handle
70    }
71}
72
73/// Security Key (FIDO/U2F) Ed25519 private key as specified in
74/// [PROTOCOL.u2f](https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.u2f?annotate=HEAD).
75#[cfg(feature = "alloc")]
76#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, PartialOrd, Ord)]
77#[ssh(error = Error)]
78pub struct SkEd25519 {
79    /// Public key.
80    public: public::SkEd25519,
81
82    /// Flags.
83    flags: u8,
84
85    /// FIDO/U2F key handle.
86    key_handle: Vec<u8>,
87
88    /// Reserved data.
89    reserved: Vec<u8>,
90}
91
92impl SkEd25519 {
93    /// Construct new instance of [`SkEd25519`].
94    ///
95    /// # Errors
96    /// Returns [`Error::Encoding`] if `key_handle` is longer than 255.
97    #[cfg(feature = "alloc")]
98    pub fn new(
99        public: public::SkEd25519,
100        flags: u8,
101        key_handle: impl Into<Vec<u8>>,
102    ) -> Result<Self> {
103        let key_handle = key_handle.into();
104
105        if key_handle.len() <= 255 {
106            Ok(SkEd25519 {
107                public,
108                flags,
109                key_handle,
110                reserved: Vec::<u8>::new(),
111            })
112        } else {
113            Err(encoding::Error::Length.into())
114        }
115    }
116
117    /// Get the Ed25519 public key.
118    #[must_use]
119    pub fn public(&self) -> &public::SkEd25519 {
120        &self.public
121    }
122
123    /// Get flags.
124    #[must_use]
125    pub fn flags(&self) -> u8 {
126        self.flags
127    }
128
129    /// Get FIDO/U2F key handle.
130    #[must_use]
131    pub fn key_handle(&self) -> &[u8] {
132        &self.key_handle
133    }
134}