@@ -7,6 +7,8 @@ use std::process::Command;
77use std:: str:: FromStr ;
88use std:: sync:: Arc ;
99
10+ use pgp:: { Deserializable , SignedPublicKey } ;
11+
1012use crate :: dist:: { dist, temp} ;
1113use crate :: errors:: * ;
1214use crate :: notifications:: * ;
@@ -33,21 +35,24 @@ impl Display for OverrideReason {
3335 }
3436}
3537
38+ lazy_static:: lazy_static! {
39+ static ref BUILTIN_PGP_KEY : SignedPublicKey = pgp:: SignedPublicKey :: from_armor_single(
40+ io:: Cursor :: new( & include_bytes!( "rust-key.pgp.ascii" ) [ ..] )
41+ ) . unwrap( ) . 0 ;
42+ }
43+
3644#[ derive( Debug ) ]
3745pub enum PgpPublicKey {
38- Builtin ( & ' static [ u8 ] ) ,
39- FromEnvironment ( PathBuf , Vec < u8 > ) ,
40- FromConfiguration ( PathBuf , Vec < u8 > ) ,
46+ Builtin ,
47+ FromEnvironment ( PathBuf , SignedPublicKey ) ,
48+ FromConfiguration ( PathBuf , SignedPublicKey ) ,
4149}
4250
4351impl PgpPublicKey {
44- /// Retrieve the key data for this key
45- ///
46- /// This key might be ASCII Armored or may not, we make no
47- /// guarantees.
48- pub fn key_data ( & self ) -> & [ u8 ] {
52+ /// Retrieve the key.
53+ pub fn key ( & self ) -> & SignedPublicKey {
4954 match self {
50- Self :: Builtin ( k ) => k ,
55+ Self :: Builtin => & * BUILTIN_PGP_KEY ,
5156 Self :: FromEnvironment ( _, k) => & k,
5257 Self :: FromConfiguration ( _, k) => & k,
5358 }
@@ -57,7 +62,7 @@ impl PgpPublicKey {
5762impl Display for PgpPublicKey {
5863 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
5964 match self {
60- Self :: Builtin ( _ ) => write ! ( f, "builtin Rust release key" ) ,
65+ Self :: Builtin => write ! ( f, "builtin Rust release key" ) ,
6166 Self :: FromEnvironment ( p, _) => {
6267 write ! ( f, "key specified in RUST_PGP_KEY ({})" , p. display( ) )
6368 }
@@ -98,18 +103,24 @@ impl Cfg {
98103 let download_dir = rustup_dir. join ( "downloads" ) ;
99104
100105 // PGP keys
101- let mut pgp_keys: Vec < PgpPublicKey > =
102- vec ! [ PgpPublicKey :: Builtin ( include_bytes! ( "rust-key.pgp.ascii" ) ) ] ;
103- if let Some ( s_path) = env:: var_os ( "RUSTUP_PGP_KEY" ) {
106+ let mut pgp_keys: Vec < PgpPublicKey > = vec ! [ PgpPublicKey :: Builtin ] ;
107+
108+ if let Some ( ref s_path) = env:: var_os ( "RUSTUP_PGP_KEY" ) {
104109 let path = PathBuf :: from ( s_path) ;
105- let content = utils:: read_file_bytes ( "RUSTUP_PGP_KEY" , & path) ?;
106- pgp_keys. push ( PgpPublicKey :: FromEnvironment ( path, content) ) ;
110+ let file = utils:: open_file ( "RUSTUP_PGP_KEY" , & path) ?;
111+ let ( key, _) = SignedPublicKey :: from_armor_single ( file)
112+ . map_err ( |error| ErrorKind :: InvalidPgpKey ( PathBuf :: from ( s_path) , error) ) ?;
113+
114+ pgp_keys. push ( PgpPublicKey :: FromEnvironment ( path, key) ) ;
107115 }
108116 settings_file. with ( |s| {
109117 if let Some ( s) = & s. pgp_keys {
110118 let path = PathBuf :: from ( s) ;
111- let content = utils:: read_file_bytes ( "PGP Key from config" , & path) ?;
112- pgp_keys. push ( PgpPublicKey :: FromConfiguration ( path, content) ) ;
119+ let file = utils:: open_file ( "PGP Key from config" , & path) ?;
120+ let ( key, _) = SignedPublicKey :: from_armor_single ( file)
121+ . map_err ( |error| ErrorKind :: InvalidPgpKey ( PathBuf :: from ( s) , error) ) ?;
122+
123+ pgp_keys. push ( PgpPublicKey :: FromConfiguration ( path, key) ) ;
113124 }
114125 Ok ( ( ) )
115126 } ) ?;
0 commit comments