11//! Contains the Signature type along with associated types and Roles
22
3- pub use ed25519_dalek:: { Keypair , PublicKey , Signature as EdSignature , Signer } ;
3+ use base64:: Engine ;
4+ pub use ed25519_dalek:: {
5+ Signature as EdSignature ,
6+ Signer ,
7+ SigningKey as Keypair , // re-export under old name for backwards compatibility
8+ SigningKey ,
9+ VerifyingKey as PublicKey , // re-export under old name for backwards compatibility
10+ VerifyingKey ,
11+ } ;
412use serde:: { Deserialize , Serialize } ;
513use thiserror:: Error ;
614#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -197,7 +205,7 @@ impl KeyRing {
197205 self . key . push ( entry)
198206 }
199207
200- pub fn contains ( & self , key : & PublicKey ) -> bool {
208+ pub fn contains ( & self , key : & VerifyingKey ) -> bool {
201209 // This could definitely be optimized.
202210 for k in self . key . iter ( ) {
203211 // Note that we are skipping malformed keys because they don't matter
@@ -246,40 +254,43 @@ impl KeyEntry {
246254 /// In most cases, it is fine to construct a KeyEntry struct manually. This
247255 /// constructor merely encapsulates the logic to store the public key in its
248256 /// canonical encoded format (as a String).
249- pub fn new ( label : & str , roles : Vec < SignatureRole > , public_key : PublicKey ) -> Self {
250- let key = base64:: encode ( public_key. to_bytes ( ) ) ;
257+ pub fn new ( label : & str , roles : Vec < SignatureRole > , public_key : VerifyingKey ) -> Self {
258+ let key = base64:: engine :: general_purpose :: STANDARD . encode ( public_key. to_bytes ( ) ) ;
251259 KeyEntry {
252260 label : label. to_owned ( ) ,
253261 roles,
254262 key,
255263 label_signature : None ,
256264 }
257265 }
258- pub fn sign_label ( & mut self , key : Keypair ) {
266+ pub fn sign_label ( & mut self , key : SigningKey ) {
259267 let sig = key. sign ( self . label . as_bytes ( ) ) ;
260- self . label_signature = Some ( base64:: encode ( sig. to_bytes ( ) ) ) ;
268+ self . label_signature =
269+ Some ( base64:: engine:: general_purpose:: STANDARD . encode ( sig. to_bytes ( ) ) ) ;
261270 }
262- pub fn verify_label ( self , key : PublicKey ) -> anyhow:: Result < ( ) > {
271+ pub fn verify_label ( self , key : VerifyingKey ) -> anyhow:: Result < ( ) > {
263272 match self . label_signature {
264273 None => {
265274 tracing:: log:: info!( "Label was not signed. Skipping." ) ;
266275 Ok ( ( ) )
267276 }
268277 Some ( txt) => {
269- let decoded_txt = base64:: decode ( txt) ?;
278+ let decoded_txt = base64:: engine :: general_purpose :: STANDARD . decode ( txt) ?;
270279 let sig = EdSignature :: try_from ( decoded_txt. as_slice ( ) ) ?;
271280 key. verify_strict ( self . label . as_bytes ( ) , & sig) ?;
272281 Ok ( ( ) )
273282 }
274283 }
275284 }
276- pub ( crate ) fn public_key ( & self ) -> Result < PublicKey , SignatureError > {
277- let rawbytes = base64:: decode ( & self . key ) . map_err ( |_e| {
278- // We swallow the source error because it could disclose information about
279- // the secret key.
280- SignatureError :: CorruptKey ( "Base64 decoding of the public key failed" . to_owned ( ) )
281- } ) ?;
282- let pk = PublicKey :: from_bytes ( rawbytes. as_slice ( ) ) . map_err ( |e| {
285+ pub ( crate ) fn public_key ( & self ) -> Result < VerifyingKey , SignatureError > {
286+ let rawbytes = base64:: engine:: general_purpose:: STANDARD
287+ . decode ( & self . key )
288+ . map_err ( |_e| {
289+ // We swallow the source error because it could disclose information about
290+ // the secret key.
291+ SignatureError :: CorruptKey ( "Base64 decoding of the public key failed" . to_owned ( ) )
292+ } ) ?;
293+ let pk = VerifyingKey :: try_from ( rawbytes. as_slice ( ) ) . map_err ( |e| {
283294 error ! ( %e, "Error loading public key" ) ;
284295 // Don't leak information about the key, because this could be sent to
285296 // a remote. A generic error is all the user should see.
@@ -297,7 +308,7 @@ impl TryFrom<SecretKeyEntry> for KeyEntry {
297308 let mut s = Self {
298309 label : secret. label ,
299310 roles : secret. roles ,
300- key : base64:: encode ( skey. public . to_bytes ( ) ) ,
311+ key : base64:: engine :: general_purpose :: STANDARD . encode ( skey. verifying_key ( ) . as_bytes ( ) ) ,
301312 label_signature : None ,
302313 } ;
303314 s. sign_label ( skey) ;
@@ -312,7 +323,7 @@ impl TryFrom<&SecretKeyEntry> for KeyEntry {
312323 let mut s = Self {
313324 label : secret. label . clone ( ) ,
314325 roles : secret. roles . clone ( ) ,
315- key : base64:: encode ( skey. public . to_bytes ( ) ) ,
326+ key : base64:: engine :: general_purpose :: STANDARD . encode ( skey. verifying_key ( ) . as_bytes ( ) ) ,
316327 label_signature : None ,
317328 } ;
318329 s. sign_label ( skey) ;
@@ -337,28 +348,34 @@ pub struct SecretKeyEntry {
337348impl SecretKeyEntry {
338349 pub fn new ( label : & str , roles : Vec < SignatureRole > ) -> Self {
339350 let mut rng = rand:: rngs:: OsRng { } ;
340- let rawkey = Keypair :: generate ( & mut rng) ;
341- let keypair = base64:: encode ( rawkey. to_bytes ( ) ) ;
351+ let rawkey = SigningKey :: generate ( & mut rng) ;
352+ let keypair = base64:: engine :: general_purpose :: STANDARD . encode ( rawkey. to_keypair_bytes ( ) ) ;
342353 Self {
343354 label : label. to_owned ( ) ,
344355 keypair,
345356 roles,
346357 }
347358 }
348359
349- pub ( crate ) fn key ( & self ) -> Result < Keypair , SignatureError > {
350- let rawbytes = base64:: decode ( & self . keypair ) . map_err ( |_e| {
360+ pub ( crate ) fn key ( & self ) -> Result < SigningKey , SignatureError > {
361+ let rawbytes = base64:: engine:: general_purpose:: STANDARD
362+ . decode ( & self . keypair )
363+ . map_err ( |_e| {
364+ // We swallow the source error because it could disclose information about
365+ // the secret key.
366+ SignatureError :: CorruptKey ( "Base64 decoding of the keypair failed" . to_owned ( ) )
367+ } ) ?;
368+ let rawbytes = rawbytes. try_into ( ) . map_err ( |_e| {
351369 // We swallow the source error because it could disclose information about
352370 // the secret key.
353- SignatureError :: CorruptKey ( "Base64 decoding of the keypair failed " . to_owned ( ) )
371+ SignatureError :: CorruptKey ( "Invalid keypair length " . to_owned ( ) )
354372 } ) ?;
355- let keypair = Keypair :: from_bytes ( & rawbytes) . map_err ( |e| {
373+ SigningKey :: from_keypair_bytes ( & rawbytes) . map_err ( |e| {
356374 tracing:: log:: error!( "Error loading key: {}" , e) ;
357375 // Don't leak information about the key, because this could be sent to
358376 // a remote. A generic error is all the user should see.
359377 SignatureError :: CorruptKey ( "Could not load keypair" . to_owned ( ) )
360- } ) ?;
361- Ok ( keypair)
378+ } )
362379 }
363380}
364381
@@ -481,7 +498,7 @@ impl SecretKeyStorage for SecretKeyFile {
481498#[ cfg( test) ]
482499mod test {
483500 use super :: * ;
484- use ed25519_dalek:: Keypair ;
501+ use ed25519_dalek:: SigningKey ;
485502
486503 #[ test]
487504 fn test_parse_role ( ) {
@@ -508,7 +525,7 @@ mod test {
508525 #[ test]
509526 fn test_sign_label ( ) {
510527 let mut rng = rand:: rngs:: OsRng { } ;
511- let keypair = Keypair :: generate ( & mut rng) ;
528+ let keypair = SigningKey :: generate ( & mut rng) ;
512529
513530 let mut ke = KeyEntry {
514531 label : "Matt Butcher <matt@example.com>" . to_owned ( ) ,
@@ -517,7 +534,7 @@ mod test {
517534 label_signature : None ,
518535 } ;
519536
520- let pubkey = keypair. public ;
537+ let pubkey = keypair. verifying_key ( ) ;
521538 ke. sign_label ( keypair) ;
522539
523540 assert ! ( ke. label_signature. is_some( ) ) ;
0 commit comments