1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use anyhow::{bail, Context, Result};
use std::fs::{metadata, set_permissions, OpenOptions};
use std::io::{self, Read, Write};
use std::os::unix::fs::PermissionsExt;
use std::process::{Child, Command, Stdio};
use std::result;
use tempfile::{self, TempDir};
pub struct GpgReader<R: Read> {
_gpgdir: TempDir,
source: R,
child: Child,
}
impl<R: Read> GpgReader<R> {
pub fn new(source: R, signature: &[u8]) -> Result<Self> {
let gpgdir = tempfile::Builder::new()
.prefix("coreos-installer-")
.tempdir()
.context("creating temporary directory")?;
let meta = metadata(gpgdir.path()).context("getting metadata for temporary directory")?;
let mut permissions = meta.permissions();
permissions.set_mode(0o700);
set_permissions(gpgdir.path(), permissions)
.context("setting mode for temporary directory")?;
let keys = include_bytes!("signing-keys.asc");
let mut import = Command::new("gpg")
.arg("--homedir")
.arg(gpgdir.path())
.arg("--batch")
.arg("--quiet")
.arg("--import")
.stdin(Stdio::piped())
.spawn()
.context("running gpg --import")?;
import
.stdin
.as_mut()
.unwrap()
.write_all(keys)
.context("importing GPG keys")?;
if !import.wait().context("waiting for gpg --import")?.success() {
bail!("gpg --import failed");
}
let mut list = Command::new("gpg")
.arg("--homedir")
.arg(gpgdir.path())
.arg("--batch")
.arg("--list-keys")
.arg("--with-colons")
.stdout(Stdio::piped())
.spawn()
.context("running gpg --list-keys")?;
let mut list_output = String::new();
list.stdout
.as_mut()
.unwrap()
.read_to_string(&mut list_output)
.context("listing GPG keys")?;
if !list
.wait()
.context("waiting for gpg --list-keys")?
.success()
{
bail!("gpg --list-keys failed");
}
let mut trust: Vec<&str> = Vec::new();
for line in list_output.lines() {
let fields: Vec<&str> = line.split(':').collect();
if fields[0] != "pub" {
continue;
}
if fields.len() >= 5 {
trust.append(&mut vec!["--trusted-key", fields[4]]);
}
}
let trustdb = Command::new("gpg")
.arg("--homedir")
.arg(gpgdir.path())
.arg("--batch")
.arg("--check-trustdb")
.args(trust)
.output()
.context("running gpg --check-trustdb")?;
if !trustdb.status.success() {
eprint!("{}", String::from_utf8_lossy(&*trustdb.stderr));
bail!("gpg --check-trustdb failed");
}
let mut signature_path = gpgdir.path().to_path_buf();
signature_path.push("signature");
let mut signature_file = OpenOptions::new()
.create_new(true)
.write(true)
.open(&signature_path)
.context("creating signature file")?;
signature_file
.write_all(signature)
.context("writing signature file")?;
let verify = Command::new("gpg")
.arg("--homedir")
.arg(gpgdir.path())
.arg("--batch")
.arg("--verify")
.arg(&signature_path)
.arg("-")
.stdin(Stdio::piped())
.spawn()
.context("running gpg --verify")?;
Ok(GpgReader {
_gpgdir: gpgdir,
source,
child: verify,
})
}
pub fn consume(&mut self) -> Result<()> {
let mut buf: [u8; 4096] = [0; 4096];
while self.read(&mut buf).context("reading signed content")? > 0 {}
Ok(())
}
}
impl<R: Read> Read for GpgReader<R> {
fn read(&mut self, buf: &mut [u8]) -> result::Result<usize, io::Error> {
if buf.is_empty() {
return Ok(0);
}
let count = self.source.read(buf)?;
if count > 0 {
self.child
.stdin
.as_mut()
.unwrap()
.write_all(&buf[0..count])?;
} else {
if !self.child.wait()?.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
"GPG verification failure",
));
}
}
Ok(count)
}
}
impl<R: Read> Drop for GpgReader<R> {
fn drop(&mut self) {
let _ = self.child.wait();
}
}