Skip to main content

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, Decoder, 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
27impl OpaquePublicKey {
28    /// Create a new `OpaquePublicKey`.
29    #[must_use]
30    pub fn new(key: Vec<u8>, algorithm: Algorithm) -> Self {
31        Self {
32            key: OpaquePublicKeyBytes(key),
33            algorithm,
34        }
35    }
36
37    /// Get the [`Algorithm`] for this public key type.
38    #[must_use]
39    pub fn algorithm(&self) -> Algorithm {
40        self.algorithm.clone()
41    }
42
43    /// Decode [`OpaquePublicKey`] for the specified algorithm.
44    pub(super) fn decode_as(
45        reader: &mut Decoder<impl Reader>,
46        algorithm: Algorithm,
47    ) -> Result<Self> {
48        Ok(Self {
49            algorithm,
50            key: OpaquePublicKeyBytes::decode(reader)?,
51        })
52    }
53}
54
55impl AsRef<[u8]> for OpaquePublicKey {
56    fn as_ref(&self) -> &[u8] {
57        self.key.as_ref()
58    }
59}
60
61impl Encode for OpaquePublicKey {
62    fn encoded_len(&self) -> encoding::Result<usize> {
63        self.key.encoded_len()
64    }
65
66    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
67        self.key.encode(writer)
68    }
69}
70
71/// The underlying representation of an [`OpaquePublicKey`].
72///
73/// The encoded representation of an `OpaquePublicKeyBytes` consists of a 4-byte length prefix,
74/// followed by its byte representation.
75#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
76pub struct OpaquePublicKeyBytes(Vec<u8>);
77
78impl Decode for OpaquePublicKeyBytes {
79    type Error = Error;
80
81    fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
82        Ok(Self(Vec::decode(reader)?))
83    }
84}
85
86impl Encode for OpaquePublicKeyBytes {
87    fn encoded_len(&self) -> encoding::Result<usize> {
88        self.0.encoded_len()
89    }
90
91    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
92        self.0.encode(writer)
93    }
94}
95
96impl AsRef<[u8]> for OpaquePublicKeyBytes {
97    fn as_ref(&self) -> &[u8] {
98        &self.0
99    }
100}