@@ -7,6 +7,8 @@ use std::process::Command;
7
7
use std:: str:: FromStr ;
8
8
use std:: sync:: Arc ;
9
9
10
+ use pgp:: { Deserializable , SignedPublicKey } ;
11
+
10
12
use crate :: dist:: { dist, temp} ;
11
13
use crate :: errors:: * ;
12
14
use crate :: notifications:: * ;
@@ -33,21 +35,24 @@ impl Display for OverrideReason {
33
35
}
34
36
}
35
37
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
+
36
44
#[ derive( Debug ) ]
37
45
pub 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 ) ,
41
49
}
42
50
43
51
impl 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 {
49
54
match self {
50
- Self :: Builtin ( k ) => k ,
55
+ Self :: Builtin => & * BUILTIN_PGP_KEY ,
51
56
Self :: FromEnvironment ( _, k) => & k,
52
57
Self :: FromConfiguration ( _, k) => & k,
53
58
}
@@ -57,7 +62,7 @@ impl PgpPublicKey {
57
62
impl Display for PgpPublicKey {
58
63
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
59
64
match self {
60
- Self :: Builtin ( _ ) => write ! ( f, "builtin Rust release key" ) ,
65
+ Self :: Builtin => write ! ( f, "builtin Rust release key" ) ,
61
66
Self :: FromEnvironment ( p, _) => {
62
67
write ! ( f, "key specified in RUST_PGP_KEY ({})" , p. display( ) )
63
68
}
@@ -98,18 +103,24 @@ impl Cfg {
98
103
let download_dir = rustup_dir. join ( "downloads" ) ;
99
104
100
105
// 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" ) {
104
109
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) ) ;
107
115
}
108
116
settings_file. with ( |s| {
109
117
if let Some ( s) = & s. pgp_keys {
110
118
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) ) ;
113
124
}
114
125
Ok ( ( ) )
115
126
} ) ?;
0 commit comments