1use crate::{error::ParseError, peer_id::*};
25
26pub mod ed25519;
27#[cfg(feature = "rsa")]
28pub mod rsa;
29
30pub(crate) mod noise;
31#[cfg(feature = "quic")]
32pub(crate) mod tls;
33pub(crate) mod keys_proto {
34 include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
35}
36
37#[derive(Clone, Debug, PartialEq, Eq)]
39pub enum PublicKey {
40 Ed25519(ed25519::PublicKey),
42}
43
44impl PublicKey {
45 pub fn to_protobuf_encoding(&self) -> Vec<u8> {
48 use prost::Message;
49
50 let public_key = keys_proto::PublicKey::from(self);
51
52 let mut buf = Vec::with_capacity(public_key.encoded_len());
53 public_key.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
54 buf
55 }
56
57 pub fn to_peer_id(&self) -> PeerId {
59 self.into()
60 }
61}
62
63impl From<&PublicKey> for keys_proto::PublicKey {
64 fn from(key: &PublicKey) -> Self {
65 match key {
66 PublicKey::Ed25519(key) => keys_proto::PublicKey {
67 r#type: keys_proto::KeyType::Ed25519 as i32,
68 data: key.to_bytes().to_vec(),
69 },
70 }
71 }
72}
73
74impl TryFrom<keys_proto::PublicKey> for PublicKey {
75 type Error = ParseError;
76
77 fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
78 let key_type = keys_proto::KeyType::try_from(pubkey.r#type)
79 .map_err(|_| ParseError::UnknownKeyType(pubkey.r#type))?;
80
81 if key_type == keys_proto::KeyType::Ed25519 {
82 Ok(ed25519::PublicKey::try_from_bytes(&pubkey.data).map(PublicKey::Ed25519)?)
83 } else {
84 Err(ParseError::UnknownKeyType(key_type as i32))
85 }
86 }
87}
88
89impl From<ed25519::PublicKey> for PublicKey {
90 fn from(public_key: ed25519::PublicKey) -> Self {
91 PublicKey::Ed25519(public_key)
92 }
93}
94
95#[derive(Clone, Debug, PartialEq, Eq)]
97pub enum RemotePublicKey {
98 Ed25519(ed25519::PublicKey),
100 #[cfg(feature = "rsa")]
102 Rsa(rsa::PublicKey),
103}
104
105impl RemotePublicKey {
106 #[must_use]
111 pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
112 use RemotePublicKey::*;
113 match self {
114 Ed25519(pk) => pk.verify(msg, sig),
115 #[cfg(feature = "rsa")]
116 Rsa(pk) => pk.verify(msg, sig),
117 }
118 }
119
120 pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<RemotePublicKey, ParseError> {
123 use prost::Message;
124
125 let pubkey = keys_proto::PublicKey::decode(bytes)?;
126
127 pubkey.try_into()
128 }
129}
130
131impl TryFrom<keys_proto::PublicKey> for RemotePublicKey {
132 type Error = ParseError;
133
134 fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
135 let key_type = keys_proto::KeyType::try_from(pubkey.r#type)
136 .map_err(|_| ParseError::UnknownKeyType(pubkey.r#type))?;
137
138 match key_type {
139 keys_proto::KeyType::Ed25519 =>
140 ed25519::PublicKey::try_from_bytes(&pubkey.data).map(RemotePublicKey::Ed25519),
141 #[cfg(feature = "rsa")]
142 keys_proto::KeyType::Rsa =>
143 rsa::PublicKey::try_decode_x509(&pubkey.data).map(RemotePublicKey::Rsa),
144 _ => Err(ParseError::UnknownKeyType(key_type as i32)),
145 }
146 }
147}