Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 // Copyright 2021 Heiko Schaefer <[email protected]>
2 //
3 // SPDX-FileCopyrightText: 2021 Heiko Schaefer <[email protected]>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5
6 use anyhow::{Result, anyhow};
7 use serde::{Deserialize, Serialize};
8
9 /// Data structure for an OpenPGP Keylist, as specified in
10 /// https://www.ietf.org/archive/id/draft-mccain-keylist-05.txt
11 #[derive(Debug, Serialize, Deserialize)]
12 pub struct Keylist {
13 pub metadata: Metadata,
14 pub keys: Vec<Key>,
15 }
16
17 impl Keylist {
18 /// Create a Keylist with metadata set, but no Keys
19 pub fn new(metadata: Metadata) -> Self {
20 Self { metadata, keys: Vec::new() }
21 }
22
23 /// Transform this Keylist into a SignedKeylist
24 pub fn sign<'a>(
25 self,
26 sign: Box<dyn Fn(&str) -> Result<String> + 'a>,
27 ) -> Result<SignedKeylist> {
28 let keylist = serde_json::to_string(&self)?;
29 let sig = sign(&keylist)?;
30
31 Ok(SignedKeylist { keylist, sig })
32 }
33 }
34
35 #[derive(Debug, Serialize, Deserialize)]
36 pub struct Metadata {
37 // e.g. "https://www.example.com/keylist.json.asc"
38 pub signature_uri: String,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
41 pub keyserver: Option<String>,
42
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub comment: Option<String>,
45 }
46
47 #[derive(Debug, Serialize, Deserialize)]
48 pub struct Key {
49 pub fingerprint: String,
50
51 #[serde(skip_serializing_if = "Option::is_none")]
52 pub name: Option<String>,
53
54 #[serde(skip_serializing_if = "Option::is_none")]
55 pub email: Option<String>,
56
57 #[serde(skip_serializing_if = "Option::is_none")]
58 pub keyserver: Option<String>,
59
60 #[serde(skip_serializing_if = "Option::is_none")]
61 pub comment: Option<String>,
62 }
63
64 pub struct SignedKeylist {
65 pub keylist: String,
66 pub sig: String,
67 }
68
69 impl SignedKeylist {
70 /// If the SignedKeylist validates (according to `verify_sig`), this
71 /// function returns a Keylist (which gives access to the individual
72 /// fields of the Keylist).
73 ///
74 /// `verify_sig` takes a message and a detached signature and checks
75 /// if the signature is valid (the certificate that is used to
76 /// validate has to be embedded in the Fn - no key store is
77 /// used for this test)
78 pub fn verify(
79 &self,
80 verify_sig: Box<dyn FnOnce(&[u8], &str) -> Result<bool>>,
81 ) -> Result<Keylist> {
82 let verify: Result<bool> =
83 verify_sig(self.keylist.as_bytes(), &self.sig);
84
85 if verify.is_ok() && *verify.as_ref().unwrap() {
86 Ok(serde_json::from_str(&self.keylist)
87 .map_err(|e| anyhow!(e))?)
88 } else {
89 Err(anyhow!(
90 "Signature verification failed {:?}",
91 verify
92 ))
93 }
94 }
95
96 /// Retrieve a Keylist from the provided url.
97 /// This implicitly also loads the signature_uri from within that Keylist.
98 ///
99 /// The resulting SignedKeylist needs to be verified as a next step,
100 /// before its data can be used.
101 pub fn from_url(url: String) -> Result<Self> {
102 let keylist = reqwest::blocking::get(&url)?.text()?;
103
104 let kl: Keylist = serde_json::from_str(&keylist)
105 .map_err(|e| anyhow!(e))?;
106
107 let sig = reqwest::blocking::get(&kl.metadata.signature_uri)?.text()?;
108
109 Ok(SignedKeylist { keylist, sig })
110 }
111 }