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
extern crate anyhow;
extern crate gpgme;
extern crate lazy_static;
extern crate log;
pub mod gpg;
use anyhow::{bail, Error};
use lazy_static::lazy_static;
use log::{debug, info};
use gpg::{DeleteKeyFlags, GPG};
pub use gpg::{KeyGenerationResult, Protocol};
use std::clone::Clone;
use std::sync::Arc;
lazy_static! {
static ref DELETE_FLAG: DeleteKeyFlags = DeleteKeyFlags::all();
}
pub trait Hook: Sync + Send {
fn process(&self, result: &KeyGenerationResult) -> bool;
}
impl<F> Hook for F
where
F: Fn(&KeyGenerationResult) -> bool + Sync + Send + Clone,
{
fn process(&self, result: &KeyGenerationResult) -> bool {
debug!("Executing hook function...");
self(result)
}
}
pub struct VanityGPG<'a> {
id: usize,
gpg: GPG,
params: &'a str,
match_hooks: Vec<Arc<dyn Hook>>,
}
impl<'a> VanityGPG<'a> {
pub fn new(
id: usize,
protocol: Protocol,
engine_path: Option<&'a str>,
home_dir: Option<&'a str>,
params: &'a str,
) -> Result<Self, Error> {
debug!("Initiating VanityGPG instance");
Ok(Self {
id,
gpg: GPG::new(protocol, engine_path, home_dir)?,
params,
match_hooks: vec![],
})
}
pub fn register_hook(&mut self, hook: impl Hook + 'static) {
debug!("({}): Registering hook...", self.id);
self.match_hooks.push(Arc::new(hook));
}
pub fn try_once(&mut self) -> Result<bool, Error> {
if self.match_hooks.is_empty() {
debug!("({}): No hooks available", self.id);
bail!("({}): No hooks available", self.id);
}
let result = self.gpg.generate_key(self.params)?;
let cloned_result = result.clone();
let fingerprint = cloned_result.fingerprint()?;
info!("({}): [{}] Generated", self.id, &fingerprint);
let key = self.gpg.get_key(fingerprint)?;
let matched = self
.match_hooks
.clone()
.iter()
.fold(false, move |acc, hook| hook.process(&result) || acc);
if !matched {
self.gpg.delete_key_with_flags(key, *DELETE_FLAG)?;
info!("({}): [{}] Deleted", self.id, &fingerprint);
} else {
info!("({}): [{}] Matched", self.id, &fingerprint);
}
Ok(matched)
}
pub fn enter_loop(&mut self) -> Result<(), Error> {
debug!("({}) Entering loop", self.id);
loop {
self.try_once()?;
}
}
}
#[cfg(test)]
mod test_vanity_gpg {
use super::{KeyGenerationResult, Protocol, VanityGPG};
const RSA_PARAMS: &'static str = r#"
<GnupgKeyParms format="internal">
Key-Type: RSA
Key-Length: 4096
Key-Usage: sign
Subkey-Type: RSA
Subkey-Length: 4096
Subkey-Usage: encrypt
Name-Real: Kay Lin
Name-Email: i@v2bv.net
Expire-Date: 0
Passphrase: 114514
</GnupgKeyParms>
"#;
const ECC_PARAMS: &'static str = r#"
<GnupgKeyParms format="internal">
Key-Type: EdDSA
Key-Curve: ed25519
Key-Usage: sign
Subkey-Type: ECDH
Subkey-Curve: Curve25519
Subkey-Usage: encrypt
Name-Real: Kay Lin
Name-Email: i@v2bv.net
Expire-Date: 0
Passphrase: 114514
</GnupgKeyParms>
"#;
#[test]
fn no_hook() {
let mut vanity_gpg =
VanityGPG::new(0, Protocol::OpenPgp, None, Some("./gpg"), RSA_PARAMS).unwrap();
assert!(true, vanity_gpg.try_once().is_err());
}
#[test]
fn ecc_generation() {
let mut vanity_gpg =
VanityGPG::new(0, Protocol::OpenPgp, None, Some("./gpg"), ECC_PARAMS).unwrap();
vanity_gpg.register_hook(|result: &KeyGenerationResult| {
assert!(result.has_primary_key());
false
});
vanity_gpg.try_once().unwrap();
}
#[test]
fn rsa_generation() {
let mut vanity_gpg =
VanityGPG::new(0, Protocol::OpenPgp, None, Some("./gpg"), RSA_PARAMS).unwrap();
vanity_gpg.register_hook(|result: &KeyGenerationResult| {
assert!(result.has_primary_key());
false
});
vanity_gpg.try_once().unwrap();
}
}