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
use arrayvec::ArrayVec;
use core::borrow::Borrow;
use core::convert::TryFrom;
use snafu::{ensure, NoneError, ResultExt};
use ed25519_dalek::{Signature as DalekSignature, Verifier};
use super::{decode::decode, is_lipmaa_required, Entry};
use crate::yamf_hash::new_blake2b;
use crate::yamf_hash::YamfHash;
#[cfg(feature = "std")]
pub mod batch;
#[cfg(feature = "std")]
pub use batch::{verify_batch, verify_batch_signatures};
pub mod error;
pub use error::*;
impl<'a, H, S> Entry<H, S>
where
H: Borrow<[u8]>,
S: Borrow<[u8]>,
{
pub fn verify_signature(&self) -> Result<()> {
let ssb_sig = DalekSignature::try_from(self.sig.as_ref().unwrap().0.borrow())
.map_err(|_| NoneError)
.context(DecodeSigError)?;
let mut buff = [0u8; 512];
let encoded_size = self.encode_for_signing(&mut buff).unwrap();
let pub_key = self.author.borrow();
pub_key
.verify(&buff[..encoded_size], &ssb_sig)
.map_err(|_| NoneError)
.context(InvalidSignature)
}
}
pub fn verify_links_and_payload(
entry: &Entry<&[u8], &[u8]>,
payload: Option<(&[u8], YamfHash<ArrayVec<[u8; 64]>>)>,
lipmaa_link: Option<(&[u8], YamfHash<ArrayVec<[u8; 64]>>)>,
backlink: Option<(&[u8], YamfHash<ArrayVec<[u8; 64]>>)>,
) -> Result<(), Error> {
if let Some((payload, payload_hash)) = payload {
ensure!(payload_hash == entry.payload_hash, PayloadHashDidNotMatch);
ensure!(
payload.len() == entry.payload_size as usize,
PayloadLengthDidNotMatch {
actual: payload.len(),
expected: entry.payload_size
}
);
}
let lipmaa_is_required = is_lipmaa_required(entry.seq_num);
match (
lipmaa_link,
entry.lipmaa_link.as_ref(),
entry.seq_num,
lipmaa_is_required,
) {
(None, None, seq_num, _) if seq_num == 1 => Ok(()),
(Some((lipmaa, lipmaa_hash)), Some(ref entry_lipmaa), seq_num, true) if seq_num > 1 => {
ensure!(lipmaa_hash == **entry_lipmaa, LipmaaHashDoesNotMatch);
let lipmaa_entry = decode(lipmaa).context(DecodeLipmaaEntry)?;
ensure!(
entry.log_id == lipmaa_entry.log_id,
LipmaaLogIdDoesNotMatch {
expected: entry.log_id,
actual: lipmaa_entry.log_id
}
);
ensure!(
entry.author == lipmaa_entry.author,
LipmaaAuthorDoesNotMatch
);
Ok(())
}
(_, _, seq_num, false) if seq_num > 1 => Ok(()),
(None, _, _, true) => Err(Error::LipmaaLinkRequired),
(_, _, _, _) => Err(Error::UnknownError),
}?;
match (backlink, entry.backlink.as_ref(), entry.seq_num) {
(_, None, seq_num) if seq_num == 1 => Ok(()),
(Some((backlink, backlink_hash)), Some(ref entry_backlink), seq_num) if seq_num > 1 => {
let backlink_entry = decode(backlink).context(DecodeBacklinkEntry)?;
ensure!(
entry.log_id == backlink_entry.log_id,
BacklinkLogIdDoesNotMatch {
expected: entry.log_id,
actual: backlink_entry.log_id
}
);
ensure!(
entry.author == backlink_entry.author,
BacklinkAuthorDoesNotMatch
);
ensure!(!backlink_entry.is_end_of_feed, PublishedAfterEndOfFeed);
ensure!(backlink_hash == **entry_backlink, BacklinkHashDoesNotMatch);
Ok(())
}
(None, Some(_), seq_num) if seq_num > 1 => Ok(()),
(_, _, _) => Err(Error::UnknownError),
}?;
Ok(())
}
pub fn verify(
entry_bytes: &[u8],
payload: Option<&[u8]>,
lipmaa_link: Option<&[u8]>,
backlink: Option<&[u8]>,
) -> Result<(), Error> {
let entry = decode(entry_bytes).context(DecodeEntry)?;
let payload_and_hash = payload.map(|payload| (payload, new_blake2b(payload)));
let lipmaa_link_and_hash = lipmaa_link.map(|link| (link, new_blake2b(link)));
let backlink_and_hash = backlink.map(|link| (link, new_blake2b(link)));
verify_links_and_payload(
&entry,
payload_and_hash,
lipmaa_link_and_hash,
backlink_and_hash,
)?;
entry.verify_signature()
}