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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
use failure;
use failure::Fail;
use clap::ArgMatches;
use itertools::Itertools;
use crate::openpgp::Packet;
use crate::openpgp::tpk::{TPKBuilder, CipherSuite};
use crate::openpgp::types::KeyFlags;
use crate::openpgp::armor::{Writer, Kind};
use crate::openpgp::serialize::Serialize;
use crate::create_or_stdout;
pub fn generate(m: &ArgMatches, force: bool) -> failure::Fallible<()> {
let mut builder = TPKBuilder::new();
// User ID
match m.values_of("userid") {
Some(uids) => for uid in uids {
builder = builder.add_userid(uid);
},
None => {
eprintln!("No user ID given, using direct key signature");
}
}
// Expiration.
const SECONDS_IN_DAY : u64 = 24 * 60 * 60;
const SECONDS_IN_YEAR : u64 =
// Average number of days in a year.
(365.2422222 * SECONDS_IN_DAY as f64) as u64;
let even_off = |s| {
if s < 7 * SECONDS_IN_DAY {
// Don't round down, too small.
s
} else {
s - (s % SECONDS_IN_DAY)
}
};
match m.value_of("expiry") {
Some(expiry) if expiry == "never" =>
builder = builder.set_expiration(None),
Some(expiry) => {
let mut expiry = expiry.chars().peekable();
let _ = expiry.by_ref()
.peeking_take_while(|c| c.is_whitespace())
.for_each(|_| ());
let digits = expiry.by_ref()
.peeking_take_while(|c| {
*c == '+' || *c == '-' || c.is_digit(10)
}).collect::<String>();
let _ = expiry.by_ref()
.peeking_take_while(|c| c.is_whitespace())
.for_each(|_| ());
let suffix = expiry.next();
let _ = expiry.by_ref()
.peeking_take_while(|c| c.is_whitespace())
.for_each(|_| ());
let junk = expiry.collect::<String>();
if digits == "" {
return Err(format_err!(
"--expiry: missing count \
(try: '2y' for 2 years)"));
}
let count = match digits.parse::<i32>() {
Ok(count) if count < 0 =>
return Err(format_err!(
"--expiry: Expiration can't be in the past")),
Ok(count) => count as u64,
Err(err) =>
return Err(err.context(
"--expiry: count is out of range").into()),
};
let factor = match suffix {
Some('y') | Some('Y') => SECONDS_IN_YEAR,
Some('m') | Some('M') => SECONDS_IN_YEAR / 12,
Some('w') | Some('W') => 7 * SECONDS_IN_DAY,
Some('d') | Some('D') => SECONDS_IN_DAY,
None =>
return Err(format_err!(
"--expiry: missing suffix \
(try: '{}y', '{}m', '{}w' or '{}d' instead)",
digits, digits, digits, digits)),
Some(suffix) =>
return Err(format_err!(
"--expiry: invalid suffix '{}' \
(try: '{}y', '{}m', '{}w' or '{}d' instead)",
suffix, digits, digits, digits, digits)),
};
if junk != "" {
return Err(format_err!(
"--expiry: contains trailing junk ('{:?}') \
(try: '{}{}')",
junk, count, factor));
}
builder = builder.set_expiration(
Some(std::time::Duration::new(even_off(count * factor), 0)));
}
// Not specified. Use the default.
None => {
builder = builder.set_expiration(
Some(std::time::Duration::new(even_off(3 * SECONDS_IN_YEAR), 0))
);
}
};
// Cipher Suite
match m.value_of("cipher-suite") {
Some("rsa3k") => {
builder = builder.set_cipher_suite(CipherSuite::RSA3k);
}
Some("rsa4k") => {
builder = builder.set_cipher_suite(CipherSuite::RSA4k);
}
Some("cv25519") => {
builder = builder.set_cipher_suite(CipherSuite::Cv25519);
}
Some(ref cs) => {
return Err(format_err!("Unknown cipher suite '{}'", cs));
}
None => panic!("argument has a default value"),
}
// Signing Capability
match (m.is_present("can-sign"), m.is_present("cannot-sign")) {
(false, false) | (true, false) => {
builder = builder.add_signing_subkey();
}
(false, true) => { /* no signing subkey */ }
(true, true) => {
return Err(
format_err!("Conflicting arguments --can-sign and --cannot-sign"));
}
}
// Encryption Capability
match (m.value_of("can-encrypt"), m.is_present("cannot-encrypt")) {
(Some("all"), false) | (None, false) => {
builder = builder.add_encryption_subkey();
}
(Some("rest"), false) => {
builder = builder.add_subkey(KeyFlags::default()
.set_encrypt_at_rest(true),
None);
}
(Some("transport"), false) => {
builder = builder.add_subkey(KeyFlags::default()
.set_encrypt_for_transport(true),
None);
}
(None, true) => { /* no encryption subkey */ }
(Some(_), true) => {
return Err(
format_err!("Conflicting arguments --can-encrypt and \
--cannot-encrypt"));
}
(Some(ref cap), false) => {
return Err(
format_err!("Unknown encryption capability '{}'", cap));
}
}
if m.is_present("with-password") {
let p0 = rpassword::read_password_from_tty(Some(
"Enter password to protect the key: "))
|