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
#![forbid(unsafe_code,
unstable_features)]
#![deny(trivial_casts,
trivial_numeric_casts,
missing_docs,
unused_import_braces,
unused_extern_crates,
unused_qualifications)]
#![no_std]
extern crate alloc;
use alloc::vec::Vec;
#[derive(Debug)]
pub struct Log<'a> {
pub description: &'a str,
pub url: &'a str,
pub operated_by: &'a str,
pub key: &'a [u8],
pub id: [u8; 32],
pub max_merge_delay: usize
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Error {
MalformedSCT,
InvalidSignature,
TimestampInFuture,
UnsupportedSCTVersion,
UnknownLog,
}
impl Error {
pub fn should_be_fatal(&self) -> bool {
match *self {
Error::UnknownLog
| Error::UnsupportedSCTVersion => false,
_ => true
}
}
}
fn lookup(logs: &[&Log], id: &[u8]) -> Result<usize, Error> {
for (i, l) in logs.iter().enumerate() {
if id == &l.id {
return Ok(i);
}
}
Err(Error::UnknownLog)
}
fn decode_u64(inp: untrusted::Input) -> u64 {
let b = inp.as_slice_less_safe();
assert_eq!(b.len(), 8);
(b[0] as u64) << 56 |
(b[1] as u64) << 48 |
(b[2] as u64) << 40 |
(b[3] as u64) << 32 |
(b[4] as u64) << 24 |
(b[5] as u64) << 16 |
(b[6] as u64) << 8 |
(b[7] as u64)
}
fn decode_u16(inp: untrusted::Input) -> u16 {
let b = inp.as_slice_less_safe();
assert_eq!(b.len(), 2);
(b[0] as u16) << 8 | (b[1] as u16)
}
fn write_u64(v: u64, out: &mut Vec<u8>) {
out.push((v >> 56) as u8);
out.push((v >> 48) as u8);
out.push((v >> 40) as u8);
out.push((v >> 32) as u8);
out.push((v >> 24) as u8);
out.push((v >> 16) as u8);
out.push((v >> 8) as u8);
out.push(v as u8);
}
fn write_u24(v: u32, out: &mut Vec<u8>) {
out.push((v >> 16) as u8);
out.push((v >> 8) as u8);
out.push(v as u8);
}
fn write_u16(v: u16, out: &mut Vec<u8>) {
out.push((v >> 8) as u8);
out.push(v as u8);
}
struct SCT<'a> {
log_id: &'a [u8],
timestamp: u64,
sig_alg: u16,
sig: &'a [u8],
exts: &'a [u8],
}
const ECDSA_SHA256: u16 = 0x0403;
const ECDSA_SHA384: u16 = 0x0503;
const RSA_PKCS1_SHA256: u16 = 0x0401;
const RSA_PKCS1_SHA384: u16 = 0x0501;
const SCT_V1: u8 = 0u8;
const SCT_TIMESTAMP: u8 = 0u8;
const SCT_X509_ENTRY: [u8; 2] = [0, 0];
impl<'a> SCT<'a> {
fn verify(&self, key: &[u8], cert: &[u8]) -> Result<(), Error> {
let alg: &dyn ring::signature::VerificationAlgorithm = match self.sig_alg {
ECDSA_SHA256 => &ring::signature::ECDSA_P256_SHA256_ASN1,
ECDSA_SHA384 => &ring::signature::ECDSA_P384_SHA384_ASN1,
RSA_PKCS1_SHA256 => &ring::signature::RSA_PKCS1_2048_8192_SHA256,
RSA_PKCS1_SHA384 => &ring::signature::RSA_PKCS1_2048_8192_SHA384,
_ => return Err(Error::InvalidSignature)
};
let mut data = Vec::new();
data.push(SCT_V1);
data.push(SCT_TIMESTAMP);
write_u64(self.timestamp, &mut data);
data.extend_from_slice(&SCT_X509_ENTRY);
write_u24(cert.len() as u32, &mut data);
data.extend_from_slice(cert);
write_u16(self.exts.len() as u16, &mut data);
data.extend_from_slice(self.exts);
let key = ring::signature::UnparsedPublicKey::new(alg, key);
key.verify(&data, self.sig)
.map_err(|_| Error::InvalidSignature)
}
fn parse(enc: &'a [u8]) -> Result<SCT<'a>, Error> {
let inp = untrusted::Input::from(enc);
inp.read_all(
Error::MalformedSCT,
|rd| {
let version = rd.read_byte()
.map_err(|_| Error::MalformedSCT)?;
if version != 0 {
return Err(Error::UnsupportedSCTVersion);
}
let id = rd.read_bytes(32)
.map_err(|_| Error::MalformedSCT)?;
let timestamp = rd.read_bytes(8)
.map_err(|_| Error::MalformedSCT)
.map(decode_u64)?;
let ext_len = rd.read_bytes(2)
.map_err(|_| Error::MalformedSCT)
.map(decode_u16)?;
let exts = rd.read_bytes(ext_len as usize)
.map_err(|_| Error::MalformedSCT)?;
let sig_alg = rd.read_bytes(2)
.map_err(|_| Error::MalformedSCT)
.map(decode_u16)?;
let sig_len = rd.read_bytes(2)
.map_err(|_| Error::MalformedSCT)
.map(decode_u16)?;
let sig = rd.read_bytes(sig_len as usize)
.map_err(|_| Error::MalformedSCT)?;
let ret = SCT {
log_id: id.as_slice_less_safe(),
timestamp: timestamp,
sig_alg: sig_alg,
sig: sig.as_slice_less_safe(),
exts: exts.as_slice_less_safe(),
};
Ok(ret)
})
}
}
pub fn verify_sct(cert: &[u8],
sct: &[u8],
at_time: u64,
logs: &[&Log]) -> Result<usize, Error> {
let sct = SCT::parse(sct)?;
let i = lookup(logs, &sct.log_id)?;
let log = logs[i];
sct.verify(log.key, cert)?;
if sct.timestamp > at_time {
return Err(Error::TimestampInFuture);
}
Ok(i)
}
#[cfg(test)]
mod tests_google;
#[cfg(test)]
mod tests_generated;
#[cfg(test)]
mod tests;