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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::sync::Arc;
use std::sync::RwLock;
use smallvec::SmallVec;
use smallvec::smallvec;
use anyhow::Context;
use sequoia_openpgp as openpgp;
use openpgp::cert::Cert;
use openpgp::cert::raw::RawCert;
use openpgp::cert::raw::RawCertParser;
use openpgp::Fingerprint;
use openpgp::KeyID;
use openpgp::KeyHandle;
use openpgp::packet::UserID;
use openpgp::parse::Parse;
use openpgp::Result;
use crate::LazyCert;
use crate::store::MergeCerts;
use crate::store::Store;
use crate::store::StoreError;
use crate::store::StoreUpdate;
use crate::store::UserIDIndex;
use crate::store::UserIDQueryParams;
const TRACE: bool = false;
/// Manages a slice of bytes, `RawCert`s, `Cert`s, or `LazyCert`s.
///
/// `Cert`s implements `StoreUpdate`, but it does not write the
/// updates to disk; they are only updated in memory.
pub struct Certs<'a> {
inner: RwLock<CertsInner<'a>>,
}
assert_send_and_sync!(Certs<'_>);
struct CertsInner<'a> {
// Indexed by primary key fingerprint.
certs: BTreeMap<Fingerprint, Arc<LazyCert<'a>>>,
// Indexed by a key's KeyID (primary key or subkey) and maps to
// the primary key.
keys: BTreeMap<KeyID, SmallVec<[Fingerprint; 1]>>,
userid_index: UserIDIndex,
}
impl<'a> Certs<'a>
{
/// Returns an empty `Certs` store.
///
/// This is useful as a placeholder. But, certificates can also
/// be added to it using the [`StoreUpdate`] interface.
pub fn empty() -> Self {
Certs {
inner: RwLock::new(CertsInner {
certs: Default::default(),
keys: Default::default(),
userid_index: UserIDIndex::new(),
}),
}
}
/// Returns a new `Certs`.
pub fn from_bytes(bytes: &'a [u8]) -> Result<Self> {
tracer!(TRACE, "Certs::from_bytes");
let raw_certs = RawCertParser::from_bytes(bytes)?
.filter_map(|c| {
match c {
Ok(c) => Some(c),
Err(err) => {
t!("Parsing raw certificate: {}", err);
None
}
}
});
Self::from_certs(raw_certs)
}
/// Returns a new `Certs`.
pub fn from_certs<I>(certs: impl IntoIterator<Item=I>)
-> Result<Self>
where I: Into<LazyCert<'a>>
{
let r = Self::empty();
for cert in certs {
r.update(Arc::new(cert.into())).expect("implementation doesn't fail")
}
Ok(r)
}
}
impl<'a> Store<'a> for CertsInner<'a>
{
fn lookup_by_cert(&self, kh: &KeyHandle) -> Result<Vec<Arc<LazyCert<'a>>>> {
tracer!(TRACE, "Certs::lookup_by_cert");
match kh {
KeyHandle::Fingerprint(fpr) => {
self.lookup_by_cert_fpr(fpr).map(|c| vec![ c ])
}
KeyHandle::KeyID(keyid) => {
let certs: Vec<Arc<LazyCert>> = self.keys.get(keyid)
.ok_or_else(|| {
anyhow::Error::from(
StoreError::NotFound(kh.clone()))
})?
.iter()
.filter_map(|fpr| self.certs.get(fpr).cloned())
// Check the constraints before we convert the
// rawcert to a cert.
.filter(|cert| cert.key_handle().aliases(kh))
.collect();
if certs.is_empty() {
Err(StoreError::NotFound(kh.clone()).into())
} else {
Ok(certs)
}
}
}
}
fn lookup_by_cert_fpr(&self, fingerprint: &Fingerprint) -> Result<Arc<LazyCert<'a>>> {
tracer!(TRACE, "Certs::lookup_by_cert_fpr");
if let Some(cert) = self.certs.get(fingerprint).cloned() {
Ok(cert)
} else {
Err(StoreError::NotFound(
KeyHandle::from(fingerprint.clone())).into())
}
}
fn lookup_by_cert_or_subkey(&self, kh: &KeyHandle) -> Result<Vec<Arc<LazyCert<'a>>>> {
tracer!(TRACE, "Certs::lookup_by_cert_or_subkey");
let keyid = KeyID::from(kh);
let certs: Vec<Arc<LazyCert<'a>>> = self.keys.get(&keyid)
.ok_or_else(|| {
anyhow::Error::from(
StoreError::NotFound(kh.clone()))
})?
.iter()
.filter_map(|fpr| self.certs.get(fpr))
// Check the constraints before we convert the rawcert to a
// cert.
.filter(|cert| {
cert.keys().any(|k| k.key_handle().aliases(kh))
})
.cloned()
.collect();
if certs.is_empty() {
Err(StoreError::NotFound(kh.clone()).into())
} else {
Ok(certs)
}
}
fn select_userid(&self, params: &UserIDQueryParams, pattern: &str)
-> Result<Vec<Arc<LazyCert<'a>>>>
{
tracer!(TRACE, "Certs::select_userid");
t!("params: {:?}, pattern: {:?}", params, pattern);
let matches = self.userid_index.select_userid(params, pattern)?;
let matches = matches
.into_iter()
.map(|fpr| {
self.lookup_by_cert_fpr(&fpr).expect("indexed")
})
.collect();
Ok(matches)
}
fn fingerprints<'b>(&'b self) -> Box<dyn Iterator<Item=Fingerprint> + 'b> {
Box::new(self.certs.keys().cloned())
}
fn certs<'b>(&'b self) -> Box<dyn Iterator<Item=Arc<LazyCert<'a>>> + 'b>
where 'a: 'b
{
Box::new(self.certs.values().cloned())
}
fn prefetch_all(&self) {
// Because the interior mutability is implemented via Certs,
// not CertInner, and we need a mutable reference here, but we
// only have a normal, non-mutable reference, the prefetch
// implementation is dispatched via `Certs`.
}
fn prefetch_some(&self, _certs: &[KeyHandle]) {
// Because the interior mutability is implemented via Certs,
// not CertInner, and we need a mutable reference here, but we
// only have a normal, non-mutable reference, the prefetch
// implementation is dispatched via `Certs`.
}
}
impl<'a> Store<'a> for Certs<'a>
{
fn lookup_by_cert(&self, kh: &KeyHandle) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.lookup_by_cert(kh)
}
fn lookup_by_cert_fpr(&self, fingerprint: &Fingerprint)
-> Result<Arc<LazyCert<'a>>>
{
let inner = self.inner.read().unwrap();
inner.lookup_by_cert_fpr(fingerprint)
}
fn lookup_by_cert_or_subkey(&self, kh: &KeyHandle) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.lookup_by_cert_or_subkey(kh)
}
fn select_userid(&self, query: &UserIDQueryParams, pattern: &str)
-> Result<Vec<Arc<LazyCert<'a>>>>
{
let inner = self.inner.read().unwrap();
inner.select_userid(query, pattern)
}
fn lookup_by_userid(&self, userid: &UserID) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.lookup_by_userid(userid)
}
fn grep_userid(&self, pattern: &str) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.grep_userid(pattern)
}
fn lookup_by_email(&self, email: &str) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.lookup_by_email(email)
}
fn grep_email(&self, pattern: &str) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.grep_email(pattern)
}
fn lookup_by_email_domain(&self, domain: &str) -> Result<Vec<Arc<LazyCert<'a>>>> {
let inner = self.inner.read().unwrap();
inner.lookup_by_email_domain(domain)
}
fn fingerprints<'b>(&'b self) -> Box<dyn Iterator<Item=Fingerprint> + 'b> {
// We aren't lazy to avoid holding the lock.
let inner = self.inner.read().unwrap();
let fprs: Vec<_> = inner.fingerprints().collect();
Box::new(fprs.into_iter())
}
fn certs<'b>(&'b self)
-> Box<dyn Iterator<Item=Arc<LazyCert<'a>>> + 'b>
where 'a: 'b
{
// We aren't lazy to avoid holding the lock.
let inner = self.inner.read().unwrap();
let certs: Vec<_> = inner.certs().collect();
Box::new(certs.into_iter())
}
fn prefetch_all(&self) {
let mut inner = self.inner.write().unwrap();
inner.prefetch(true, &[])
}
fn prefetch_some(&self, certs: &[KeyHandle]) {
let mut inner = self.inner.write().unwrap();
inner.prefetch(false, certs)
}
}
impl CertsInner<'_> {
/// Prefetches the certs identified by `khs`, or `all`.
fn prefetch(&mut self, all: bool, khs: &[KeyHandle]) {
// LazyCert is Sync and Send, but keeping a reference to the
// RawCerts in self.certs prevents us from later updating
// self.certs. This requires a bit of acrobatics to get
// right.
tracer!(TRACE, "Certs::prefetch_some");
t!("Prefetch: {} certificates", khs.len());
let mut certs: Vec<RawCert>
= self.certs.iter().filter_map(|(fpr, cert)| {
if ! cert.is_parsed() {
if all
|| khs.iter()
.any(|kh| {
kh.aliases(&KeyHandle::from(fpr.clone()))
})
{
// Unfortunately we have to clone the bytes,
// because we cannot keep self.certs borrowed.
t!("Queuing {} to be prefetched", fpr);
use std::ops::Deref;
cert.deref().clone().into_raw_cert().ok()
} else {
None
}
} else {
None
}
}).collect();
let cert_count = certs.len();
// Use not more threads than we have cores. No point in
// spawning a single thread, just do it inline. Also, some
// platforms don't support spawning threads.
let mut threads = num_cpus::get();
if threads <= 1 {
threads = 0;
} else if cert_count < 16 {
// The keyring is small, limit the number of threads.
threads = 2;
} else {
// Sort the certificates so they are ordered from most
// packets to least. More packets implies more work, and
// this will hopefully result in a more equal distribution
// of load.
certs.sort_unstable_by_key(|c| {
usize::MAX - c.count()
});
}
t!("Using {} threads", threads);
let mut count = 0;
if threads == 0 {
for raw in certs {
t!("Inline processing {}!", raw.keyid());
// Silently ignore errors. This will be caught later
// when the caller looks this one up.
match Cert::try_from(&raw) {
Ok(cert) => {
let fpr = cert.fingerprint();
t!("Caching {}", fpr);
self.certs.insert(fpr, Arc::new(cert.into()));
count += 1;
}
Err(err) => {
t!("Parsing raw cert {}: {}",
raw.keyid(), err);
}
}
}
} else {
use crossbeam::thread;
use crossbeam::channel::unbounded as channel;
// Avoid an extra level of indentation.
let result = thread::scope(|thread_scope| {
// A communication channel for sending work to the workers.
let (work_tx, work_rx) = channel();
// A communication channel for returning results to the
// main thread.
let (results_tx, results_rx) = channel();
let mut threads_extant = Vec::new();
for cert in certs.into_iter() {
if threads_extant.len() < threads {
let tid = threads_extant.len();
t!("Starting thread {} of {}",
tid, threads);
let mut work = Some(Ok(cert));
// The thread's state.
let work_rx = work_rx.clone();
let results_tx = results_tx.clone();
threads_extant.push(thread_scope.spawn(move |_| {
loop {
match work.take().unwrap_or_else(|| work_rx.recv()) {
Err(_) => break,
Ok(raw) => {
t!("Thread {} dequeuing {}!",
tid, raw.keyid());
// Silently ignore errors. This will be
// caught later when the caller looks
// this one up.
match Cert::try_from(&raw) {
Ok(cert) => {
let _ = results_tx.send(cert);
}
Err(err) => {
t!("Parsing raw cert {}: {}",
raw.keyid(), err);
}
}
}
}
}
t!("Thread {} exiting", tid);
}));
} else {
work_tx.send(cert).unwrap();
}
}
// When the threads see this drop, they will exit.
drop(work_tx);
// Drop our reference to results_tx. When the last thread
// exits, the last reference will be dropped and the loop
// below will exit.
drop(results_tx);
while let Ok(cert) = results_rx.recv() {
let fpr = cert.fingerprint();
t!("Caching {}", fpr);
self.certs.insert(fpr, Arc::new(cert.into()));
count += 1;
}
}); // thread scope.
// We're just caching results so we can ignore errors.
if let Err(err) = result {
t!("{:?}", err);
}
}
t!("Prefetched {} certificates, ({} RawCerts had errors)",
count, cert_count - count);
}
}
impl<'a> StoreUpdate<'a> for Certs<'a> {
fn update_by(&self, cert: Arc<LazyCert<'a>>,
merge_strategy: &dyn MergeCerts<'a>)
-> Result<Arc<LazyCert<'a>>>
{
tracer!(TRACE, "Certs::update_by");
let mut inner = self.inner.write().unwrap();
let fpr = cert.fingerprint();
// Add the cert fingerprint -> cert entry.
let merged: Arc<LazyCert>;
match inner.certs.entry(fpr.clone()) {
Entry::Occupied(mut oe) => {
t!("Updating {}", fpr);
let old = oe.get().clone();
merged = merge_strategy.merge_public(cert, Some(old))
.with_context(|| {
format!("Merging two version of {}", fpr)
})?;
*oe.get_mut() = merged.clone();
}
Entry::Vacant(ve) => {
t!("Inserting {}", fpr);
merged = merge_strategy.merge_public(cert, None)?;
ve.insert(merged.clone());
}
}
// Populate the key map. This is a merge so we are not
// removing anything.
for k in merged.keys() {
match inner.keys.entry(k.keyid()) {
Entry::Occupied(mut oe) => {
let fprs = oe.get_mut();
if ! fprs.contains(&fpr) {
fprs.push(fpr.clone());
}
}
Entry::Vacant(ve) => {
ve.insert(smallvec![ fpr.clone() ]);
}
}
}
inner.userid_index.insert(&fpr, merged.userids());
Ok(merged)
}
}