@@ -270,44 +270,84 @@ fn read_authorizer_from_snapshot(
270270 Ok ( builder)
271271}
272272
273- pub fn read_private_key_from ( from : & KeyBytes , alg : Algorithm ) -> Result < PrivateKey > {
274- let bytes = match from {
275- KeyBytes :: FromStdin ( KeyFormat :: RawBytes ) => read_stdin_bytes ( ) ?,
273+ pub fn read_private_key_from ( from : & KeyBytes , alg : & Option < Algorithm > ) -> Result < PrivateKey > {
274+ let key = match from {
275+ KeyBytes :: FromStdin ( KeyFormat :: RawBytes ) => {
276+ let bytes = read_stdin_bytes ( ) ?;
277+ PrivateKey :: from_bytes ( & bytes, alg. unwrap_or_default ( ) )
278+ . map_err ( |e| ParseError ( "private key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
279+ }
276280 KeyBytes :: FromStdin ( KeyFormat :: HexKey ) => {
277- hex:: decode ( read_stdin_string ( "hex-encoded private key" ) ?) ?
281+ let str = read_stdin_string ( "hex-encoded private key" ) ?;
282+ str. parse ( )
283+ . map_err ( |e| ParseError ( "private key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
278284 }
279285 KeyBytes :: FromFile ( KeyFormat :: RawBytes , path) => {
280- fs:: read ( path) . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?
286+ let bytes = fs:: read ( path) . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?;
287+ PrivateKey :: from_bytes ( & bytes, alg. unwrap_or_default ( ) )
288+ . map_err ( |e| ParseError ( "private key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
281289 }
282- KeyBytes :: FromFile ( KeyFormat :: HexKey , path) => hex:: decode (
283- fs:: read_to_string ( path)
284- . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?
285- . trim ( ) ,
286- ) ?,
287- KeyBytes :: HexString ( str) => hex:: decode ( str) ?,
290+ KeyBytes :: FromFile ( KeyFormat :: HexKey , path) => {
291+ let str = fs:: read_to_string ( path) . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?;
292+ str. parse ( )
293+ . map_err ( |e| ParseError ( "private key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
294+ }
295+ KeyBytes :: HexString ( str) => str
296+ . parse ( )
297+ . map_err ( |e| ParseError ( "private key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?,
288298 } ;
289- PrivateKey :: from_bytes ( & bytes, alg)
290- . map_err ( |e| ParseError ( "private key" . to_string ( ) , format ! ( "{}" , & e) ) . into ( ) )
299+ let key_alg = key. algorithm ( ) . into ( ) ;
300+
301+ if let Some ( a) = alg {
302+ if * a != key_alg {
303+ Err ( std:: io:: Error :: other ( format ! (
304+ "Inconsistent algorithm: key algorithm is {}, expected algorithm is {}" ,
305+ key_alg, a
306+ ) ) ) ?
307+ }
308+ }
309+
310+ Ok ( key)
291311}
292312
293- pub fn read_public_key_from ( from : & KeyBytes , alg : Algorithm ) -> Result < PublicKey > {
294- let bytes = match from {
295- KeyBytes :: FromStdin ( KeyFormat :: RawBytes ) => read_stdin_bytes ( ) ?,
313+ pub fn read_public_key_from ( from : & KeyBytes , alg : & Option < Algorithm > ) -> Result < PublicKey > {
314+ let key = match from {
315+ KeyBytes :: FromStdin ( KeyFormat :: RawBytes ) => {
316+ let bytes = read_stdin_bytes ( ) ?;
317+ PublicKey :: from_bytes ( & bytes, alg. unwrap_or_default ( ) )
318+ . map_err ( |e| ParseError ( "public key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
319+ }
296320 KeyBytes :: FromStdin ( KeyFormat :: HexKey ) => {
297- hex:: decode ( read_stdin_string ( "hex-encoded public key" ) ?) ?
321+ let str = read_stdin_string ( "hex-encoded public key" ) ?;
322+ str. parse ( )
323+ . map_err ( |e| ParseError ( "public key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
298324 }
299325 KeyBytes :: FromFile ( KeyFormat :: RawBytes , path) => {
300- fs:: read ( path) . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?
326+ let bytes = fs:: read ( path) . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?;
327+ PublicKey :: from_bytes ( & bytes, alg. unwrap_or_default ( ) )
328+ . map_err ( |e| ParseError ( "public key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
301329 }
302- KeyBytes :: FromFile ( KeyFormat :: HexKey , path) => hex:: decode (
303- fs:: read_to_string ( path)
304- . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?
305- . trim ( ) ,
306- ) ?,
307- KeyBytes :: HexString ( str) => hex:: decode ( str) ?,
330+ KeyBytes :: FromFile ( KeyFormat :: HexKey , path) => {
331+ let str = fs:: read_to_string ( path) . map_err ( |_| FileNotFound ( path. clone ( ) ) ) ?;
332+ str. parse ( )
333+ . map_err ( |e| ParseError ( "public key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?
334+ }
335+ KeyBytes :: HexString ( str) => str
336+ . parse ( )
337+ . map_err ( |e| ParseError ( "public key" . to_string ( ) , format ! ( "{}" , & e) ) ) ?,
308338 } ;
309- PublicKey :: from_bytes ( & bytes, alg)
310- . map_err ( |e| ParseError ( "public key" . to_string ( ) , format ! ( "{}" , & e) ) . into ( ) )
339+ let key_alg = key. algorithm ( ) . into ( ) ;
340+
341+ if let Some ( a) = alg {
342+ if * a != key_alg {
343+ Err ( std:: io:: Error :: other ( format ! (
344+ "Inconsistent algorithm: key algorithm is {}, expected algorithm is {}" ,
345+ key_alg, a
346+ ) ) ) ?
347+ }
348+ }
349+
350+ Ok ( key)
311351}
312352
313353pub fn read_biscuit_from ( from : & BiscuitBytes ) -> Result < UnverifiedBiscuit > {
0 commit comments