ssh_key/public/
opaque.rs

1//! Opaque public keys.
2//!
3//! [`OpaquePublicKey`] represents a public key meant to be used with an algorithm unknown to this
4//! crate, i.e. public keys that use a custom algorithm as specified in [RFC4251 § 6].
5//!
6//! They are said to be opaque, because the meaning of their underlying byte representation is not
7//! specified.
8//!
9//! [RFC4251 § 6]: https://www.rfc-editor.org/rfc/rfc4251.html#section-6
10
11use crate::{Algorithm, Error, Result};
12use alloc::vec::Vec;
13use encoding::{Decode, Encode, Reader, Writer};
14
15/// An opaque public key with a custom algorithm name.
16///
17/// The encoded representation of an `OpaquePublicKey` is the encoded representation of its
18/// [`OpaquePublicKeyBytes`].
19#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
20pub struct OpaquePublicKey {
21    /// The [`Algorithm`] of this public key.
22    pub algorithm: Algorithm,
23    /// The key data
24    pub key: OpaquePublicKeyBytes,
25}
26
27/// The underlying representation of an [`OpaquePublicKey`].
28///
29/// The encoded representation of an `OpaquePublicKeyBytes` consists of a 4-byte length prefix,
30/// followed by its byte representation.
31#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
32pub struct OpaquePublicKeyBytes(Vec<u8>);
33
34impl OpaquePublicKey {
35    /// Create a new `OpaquePublicKey`.
36    pub fn new(key: Vec<u8>, algorithm: Algorithm) -> Self {
37        Self {
38            key: OpaquePublicKeyBytes(key),
39            algorithm,
40        }
41    }
42
43    /// Get the [`Algorithm`] for this public key type.
44    pub fn algorithm(&self) -> Algorithm {
45        self.algorithm.clone()
46    }
47
48    /// Decode [`OpaquePublicKey`] for the specified algorithm.
49    pub(super) fn decode_as(reader: &mut impl Reader, algorithm: Algorithm) -> Result<Self> {
50        Ok(Self {
51            algorithm,
52            key: OpaquePublicKeyBytes::decode(reader)?,
53        })
54    }
55}
56
57impl Decode for OpaquePublicKeyBytes {
58    type Error = Error;
59
60    fn decode(reader: &mut impl Reader) -> Result<Self> {
61        let len = usize::decode(reader)?;
62        let mut bytes = vec![0; len];
63        reader.read(&mut bytes)?;
64        Ok(Self(bytes))
65    }
66}
67
68impl Encode for OpaquePublicKeyBytes {
69    fn encoded_len(&self) -> encoding::Result<usize> {
70        self.0.encoded_len()
71    }
72
73    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
74        self.0.encode(writer)
75    }
76}
77
78impl Encode for OpaquePublicKey {
79    fn encoded_len(&self) -> encoding::Result<usize> {
80        self.key.encoded_len()
81    }
82
83    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
84        self.key.encode(writer)
85    }
86}
87
88impl AsRef<[u8]> for OpaquePublicKey {
89    fn as_ref(&self) -> &[u8] {
90        self.key.as_ref()
91    }
92}
93
94impl AsRef<[u8]> for OpaquePublicKeyBytes {
95    fn as_ref(&self) -> &[u8] {
96        &self.0
97    }
98}