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
use std::collections::BTreeMap;
use std::io;
use chrono::Duration;
use smallvec::SmallVec;
use crate::composed::key::KeyDetails;
use crate::composed::signed_key::{SignedPublicKey, SignedSecretKey};
use crate::crypto::public_key::PublicKeyAlgorithm;
use crate::errors::Result;
use crate::packet;
use crate::ser::Serialize;
use crate::types::{KeyId, KeyTrait, PublicKeyTrait, SignedUser, SignedUserAttribute};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SignedKeyDetails {
pub revocation_signatures: Vec<packet::Signature>,
pub direct_signatures: Vec<packet::Signature>,
pub users: Vec<SignedUser>,
pub user_attributes: Vec<SignedUserAttribute>,
}
impl SignedKeyDetails {
pub fn new(
revocation_signatures: Vec<packet::Signature>,
direct_signatures: Vec<packet::Signature>,
mut users: Vec<SignedUser>,
mut user_attributes: Vec<SignedUserAttribute>,
) -> Self {
users.retain(|user| {
if user.signatures.is_empty() {
warn!("ignoring unsigned {}", user.id);
false
} else {
true
}
});
user_attributes.retain(|attr| {
if attr.signatures.is_empty() {
warn!("ignoring unsigned {}", attr.attr);
false
} else {
true
}
});
SignedKeyDetails {
revocation_signatures,
direct_signatures,
users,
user_attributes,
}
}
pub fn key_expiration_time(&self) -> Option<Duration> {
self.users
.iter()
.flat_map(|user| &user.signatures)
.filter_map(|sig| sig.key_expiration_time())
.max()
.map(|tm| Duration::seconds(tm.timestamp()))
}
fn verify_users(&self, key: &impl PublicKeyTrait) -> Result<()> {
for user in &self.users {
user.verify(key)?;
}
Ok(())
}
fn verify_attributes(&self, key: &impl PublicKeyTrait) -> Result<()> {
for attr in &self.user_attributes {
attr.verify(key)?;
}
Ok(())
}
fn verify_revocation_signatures(&self, key: &impl PublicKeyTrait) -> Result<()> {
for sig in &self.revocation_signatures {
sig.verify_key(key)?;
}
Ok(())
}
fn verify_direct_signatures(&self, key: &impl PublicKeyTrait) -> Result<()> {
for sig in &self.direct_signatures {
sig.verify_key(key)?;
}
Ok(())
}
pub fn verify(&self, key: &impl PublicKeyTrait) -> Result<()> {
self.verify_users(key)?;
self.verify_attributes(key)?;
self.verify_revocation_signatures(key)?;
self.verify_direct_signatures(key)?;
Ok(())
}
pub fn as_unsigned(&self) -> KeyDetails {
let primary_user = self.users.iter().find(|u| u.is_primary()).map_or_else(
|| self.users.first().expect("missing user ids"),
|user| user,
);
let primary_user_id = primary_user.id.clone();
let primary_sig = primary_user
.signatures
.first()
.expect("invalid primary user");
let keyflags = primary_sig.key_flags();
let preferred_symmetric_algorithms =
SmallVec::from_slice(primary_sig.preferred_symmetric_algs());
let preferred_hash_algorithms = SmallVec::from_slice(primary_sig.preferred_hash_algs());
let preferred_compression_algorithms =
SmallVec::from_slice(primary_sig.preferred_compression_algs());
let revocation_key = primary_sig.revocation_key().cloned();
KeyDetails::new(
primary_user_id,
self.users
.iter()
.filter(|u| !u.is_primary())
.map(|u| u.id.clone())
.collect(),
self.user_attributes
.iter()
.map(|a| a.attr.clone())
.collect(),
keyflags,
preferred_symmetric_algorithms,
preferred_hash_algorithms,
preferred_compression_algorithms,
revocation_key,
)
}
}
impl Serialize for SignedKeyDetails {
fn to_writer<W: io::Write>(&self, writer: &mut W) -> Result<()> {
for sig in &self.revocation_signatures {
packet::write_packet(writer, sig)?;
}
for sig in &self.direct_signatures {
packet::write_packet(writer, sig)?;
}
for user in &self.users {
user.to_writer(writer)?;
}
for attr in &self.user_attributes {
attr.to_writer(writer)?;
}
Ok(())
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[allow(clippy::large_enum_variant)] pub enum PublicOrSecret {
Public(SignedPublicKey),
Secret(SignedSecretKey),
}
impl PublicOrSecret {
pub fn verify(&self) -> Result<()> {
match self {
PublicOrSecret::Public(k) => k.verify(),
PublicOrSecret::Secret(k) => k.verify(),
}
}
pub fn to_armored_writer(
&self,
writer: &mut impl io::Write,
headers: Option<&BTreeMap<String, String>>,
) -> Result<()> {
match self {
PublicOrSecret::Public(k) => k.to_armored_writer(writer, headers),
PublicOrSecret::Secret(k) => k.to_armored_writer(writer, headers),
}
}
pub fn to_armored_bytes(&self, headers: Option<&BTreeMap<String, String>>) -> Result<Vec<u8>> {
match self {
PublicOrSecret::Public(k) => k.to_armored_bytes(headers),
PublicOrSecret::Secret(k) => k.to_armored_bytes(headers),
}
}
pub fn to_armored_string(&self, headers: Option<&BTreeMap<String, String>>) -> Result<String> {
match self {
PublicOrSecret::Public(k) => k.to_armored_string(headers),
PublicOrSecret::Secret(k) => k.to_armored_string(headers),
}
}
pub fn into_secret(self) -> SignedSecretKey {
match self {
PublicOrSecret::Public(_) => panic!("Can not convert a public into a secret key"),
PublicOrSecret::Secret(k) => k,
}
}
pub fn into_public(self) -> SignedPublicKey {
match self {
PublicOrSecret::Secret(_) => panic!("Can not convert a secret into a public key"),
PublicOrSecret::Public(k) => k,
}
}
pub fn is_public(&self) -> bool {
match self {
PublicOrSecret::Secret(_) => false,
PublicOrSecret::Public(_) => true,
}
}
pub fn is_secret(&self) -> bool {
match self {
PublicOrSecret::Secret(_) => true,
PublicOrSecret::Public(_) => false,
}
}
}
impl Serialize for PublicOrSecret {
fn to_writer<W: io::Write>(&self, writer: &mut W) -> Result<()> {
match self {
PublicOrSecret::Public(k) => k.to_writer(writer),
PublicOrSecret::Secret(k) => k.to_writer(writer),
}
}
}
impl KeyTrait for PublicOrSecret {
fn fingerprint(&self) -> Vec<u8> {
match self {
PublicOrSecret::Public(k) => k.fingerprint(),
PublicOrSecret::Secret(k) => k.fingerprint(),
}
}
fn key_id(&self) -> KeyId {
match self {
PublicOrSecret::Public(k) => k.key_id(),
PublicOrSecret::Secret(k) => k.key_id(),
}
}
fn algorithm(&self) -> PublicKeyAlgorithm {
match self {
PublicOrSecret::Public(k) => k.algorithm(),
PublicOrSecret::Secret(k) => k.algorithm(),
}
}
}