2424use crate :: { error:: ParseError , peer_id:: * } ;
2525
2626pub mod ed25519;
27+ #[ cfg( feature = "rsa" ) ]
28+ pub mod rsa;
29+
2730pub ( crate ) mod noise;
2831#[ cfg( feature = "quic" ) ]
2932pub ( crate ) mod tls;
@@ -39,18 +42,6 @@ pub enum PublicKey {
3942}
4043
4144impl PublicKey {
42- /// Verify a signature for a message using this public key, i.e. check
43- /// that the signature has been produced by the corresponding
44- /// private key (authenticity), and that the message has not been
45- /// tampered with (integrity).
46- #[ must_use]
47- pub fn verify ( & self , msg : & [ u8 ] , sig : & [ u8 ] ) -> bool {
48- use PublicKey :: * ;
49- match self {
50- Ed25519 ( pk) => pk. verify ( msg, sig) ,
51- }
52- }
53-
5445 /// Encode the public key into a protobuf structure for storage or
5546 /// exchange with other nodes.
5647 pub fn to_protobuf_encoding ( & self ) -> Vec < u8 > {
@@ -63,16 +54,6 @@ impl PublicKey {
6354 buf
6455 }
6556
66- /// Decode a public key from a protobuf structure, e.g. read from storage
67- /// or received from another node.
68- pub fn from_protobuf_encoding ( bytes : & [ u8 ] ) -> Result < PublicKey , ParseError > {
69- use prost:: Message ;
70-
71- let pubkey = keys_proto:: PublicKey :: decode ( bytes) ?;
72-
73- pubkey. try_into ( )
74- }
75-
7657 /// Convert the `PublicKey` into the corresponding `PeerId`.
7758 pub fn to_peer_id ( & self ) -> PeerId {
7859 self . into ( )
@@ -110,3 +91,57 @@ impl From<ed25519::PublicKey> for PublicKey {
11091 PublicKey :: Ed25519 ( public_key)
11192 }
11293}
94+
95+ /// The public key of a remote node's identity keypair. Supports RSA keys additionally to ed25519.
96+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
97+ pub ( crate ) enum RemotePublicKey {
98+ /// A public Ed25519 key.
99+ Ed25519 ( ed25519:: PublicKey ) ,
100+ /// A public RSA key.
101+ #[ cfg( feature = "rsa" ) ]
102+ Rsa ( rsa:: PublicKey ) ,
103+ }
104+
105+ impl RemotePublicKey {
106+ /// Verify a signature for a message using this public key, i.e. check
107+ /// that the signature has been produced by the corresponding
108+ /// private key (authenticity), and that the message has not been
109+ /// tampered with (integrity).
110+ #[ 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+ /// Decode a public key from a protobuf structure, e.g. read from storage
121+ /// or received from another node.
122+ 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+
131+ impl 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+ }
0 commit comments