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
use std::time::SystemTime;
use super::{IntroAuthType, IntroPointDesc};
use crate::batching_split_before::IteratorExt as _;
use crate::parse::tokenize::{ItemResult, NetDocReader};
use crate::parse::{keyword::Keyword, parser::SectionRules};
use crate::types::misc::{UnvalidatedEdCert, B64};
use crate::{ParseErrorKind as EK, Result};
use itertools::Itertools as _;
use once_cell::sync::Lazy;
use smallvec::SmallVec;
use tor_checkable::signed::SignatureGated;
use tor_checkable::timed::TimerangeBound;
use tor_checkable::Timebound;
use tor_hscrypto::pk::{HsIntroPtSessionIdKey, HsSvcNtorKey};
use tor_llcrypto::pk::ed25519::Ed25519Identity;
use tor_llcrypto::pk::{curve25519, ed25519, ValidatableSignature};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "hsdesc-inner-docs", visibility::make(pub))]
pub(super) struct HsDescInner {
pub(super) intro_auth_types: Option<SmallVec<[IntroAuthType; 2]>>,
pub(super) single_onion_service: bool,
pub(super) intro_points: Vec<IntroPointDesc>,
}
decl_keyword! {
pub(crate) HsInnerKwd {
"create2-formats" => CREATE2_FORMATS,
"intro-auth-required" => INTRO_AUTH_REQUIRED,
"single-onion-service" => SINGLE_ONION_SERVICE,
"introduction-point" => INTRODUCTION_POINT,
"onion-key" => ONION_KEY,
"auth-key" => AUTH_KEY,
"enc-key" => ENC_KEY,
"enc-key-cert" => ENC_KEY_CERT,
"legacy-key" => LEGACY_KEY,
"legacy-key-cert" => LEGACY_KEY_CERT,
}
}
static HS_INNER_HEADER_RULES: Lazy<SectionRules<HsInnerKwd>> = Lazy::new(|| {
use HsInnerKwd::*;
let mut rules = SectionRules::builder();
rules.add(CREATE2_FORMATS.rule().required().args(1..));
rules.add(INTRO_AUTH_REQUIRED.rule().args(1..));
rules.add(SINGLE_ONION_SERVICE.rule());
rules.add(UNRECOGNIZED.rule().may_repeat().obj_optional());
rules.build()
});
static HS_INNER_INTRO_RULES: Lazy<SectionRules<HsInnerKwd>> = Lazy::new(|| {
use HsInnerKwd::*;
let mut rules = SectionRules::builder();
rules.add(INTRODUCTION_POINT.rule().required().args(1..));
rules.add(ONION_KEY.rule().required().may_repeat().args(2..));
rules.add(AUTH_KEY.rule().required().obj_required());
rules.add(ENC_KEY.rule().required().may_repeat().args(2..));
rules.add(ENC_KEY_CERT.rule().required().obj_required());
rules.add(UNRECOGNIZED.rule().may_repeat().obj_optional());
rules.build()
});
pub(crate) type UncheckedHsDescInner = TimerangeBound<SignatureGated<HsDescInner>>;
struct InnerCertData {
signing_key: Ed25519Identity,
subject_key: ed25519::PublicKey,
signature: Box<dyn ValidatableSignature>,
expiry: SystemTime,
}
fn handle_inner_certificate(
tok: &crate::parse::tokenize::Item<HsInnerKwd>,
want_tag: &str,
want_type: tor_cert::CertType,
) -> Result<InnerCertData> {
let make_err = |e, msg| {
EK::BadObjectVal
.with_msg(msg)
.with_source(e)
.at_pos(tok.pos())
};
let cert = tok
.parse_obj::<UnvalidatedEdCert>(want_tag)?
.check_cert_type(want_type)?
.into_unchecked();
let cert = cert
.check_key(None) .map_err(|e| make_err(e, "Certificate was not self-signed"))?;
let (cert, signature) = cert
.dangerously_split()
.map_err(|e| make_err(e, "Certificate was not Ed25519-signed"))?;
let signature = Box::new(signature);
let cert = cert.dangerously_assume_timely();
let expiry = cert.expiry();
let subject_key = cert
.subject_key()
.as_ed25519()
.ok_or_else(|| {
EK::BadObjectVal
.with_msg("Certified key was not Ed25519")
.at_pos(tok.pos())
})?
.try_into()
.map_err(|_| {
EK::BadObjectVal
.with_msg("Certified key was not valid Ed25519")
.at_pos(tok.pos())
})?;
let signing_key = *cert.signing_key().ok_or_else(|| {
EK::BadObjectVal
.with_msg("Signing key was not Ed25519")
.at_pos(tok.pos())
})?;
Ok(InnerCertData {
signing_key,
subject_key,
signature,
expiry,
})
}
impl HsDescInner {
#[cfg_attr(feature = "hsdesc-inner-docs", visibility::make(pub))]
pub(super) fn parse(s: &str) -> Result<(Option<Ed25519Identity>, UncheckedHsDescInner)> {
let mut reader = NetDocReader::new(s);
let result = Self::take_from_reader(&mut reader).map_err(|e| e.within(s))?;
Ok(result)
}
fn take_from_reader(
input: &mut NetDocReader<'_, HsInnerKwd>,
) -> Result<(Option<Ed25519Identity>, UncheckedHsDescInner)> {
use HsInnerKwd::*;
let mut sections =
input.batching_split_before_with_header(|item| item.is_ok_with_kwd(INTRODUCTION_POINT));
let header = HS_INNER_HEADER_RULES.parse(&mut sections)?;
{
let tok = header.required(CREATE2_FORMATS)?;
if !tok.args().any(|s| s == "2") {
return Err(EK::BadArgument
.at_pos(tok.pos())
.with_msg("Onion service descriptor does not support ntor handshake."));
}
}
let auth_types = if let Some(tok) = header.get(INTRO_AUTH_REQUIRED) {
let mut auth_types: SmallVec<[IntroAuthType; 2]> = SmallVec::new();
let mut push = |at| {
if !auth_types.contains(&at) {
auth_types.push(at);
}
};
for arg in tok.args() {
#[allow(clippy::single_match)]
match arg {
"ed25519" => push(IntroAuthType::Ed25519),
_ => (), }
}
if auth_types.is_empty() {
return Err(EK::BadArgument
.at_pos(tok.pos())
.with_msg("No recognized introduction authentication methods."));
}
Some(auth_types)
} else {
None
};
let is_single_onion_service = header.get(SINGLE_ONION_SERVICE).is_some();
let mut signatures = Vec::new();
let mut expirations = Vec::new();
let mut cert_signing_key: Option<Ed25519Identity> = None;
let mut intro_points = Vec::new();
let mut sections = sections.subsequent();
while let Some(mut ipt_section) = sections.next_batch() {
let ipt_section = HS_INNER_INTRO_RULES.parse(&mut ipt_section)?;
let link_specifiers = {
let tok = ipt_section.required(INTRODUCTION_POINT)?;
let ls = tok.parse_arg::<B64>(0)?;
let mut r = tor_bytes::Reader::from_slice(ls.as_bytes());
let n = r.take_u8()?;
let res = r.extract_n(n.into())?;
r.should_be_exhausted()?;
res
};
let ntor_onion_key = {
let tok = ipt_section
.slice(ONION_KEY)
.iter()
.filter(|item| item.arg(0) == Some("ntor"))
.exactly_one()
.map_err(|_| EK::MissingToken.with_msg("No unique ntor onion key found."))?;
tok.parse_arg::<B64>(1)?.into_array()?.into()
};
let auth_key: HsIntroPtSessionIdKey = {
let tok = ipt_section.required(AUTH_KEY)?;
let InnerCertData {
signing_key,
subject_key,
signature,
expiry,
} = handle_inner_certificate(
tok,
"ED25519 CERT",
tor_cert::CertType::HS_IP_V_SIGNING,
)?;
expirations.push(expiry);
signatures.push(signature);
if cert_signing_key.get_or_insert(signing_key) != &signing_key {
return Err(EK::BadObjectVal
.at_pos(tok.pos())
.with_msg("Mismatched signing key"));
}
subject_key.into()
};
let svc_ntor_key: HsSvcNtorKey = {
let tok = ipt_section
.slice(ENC_KEY)
.iter()
.filter(|item| item.arg(0) == Some("ntor"))
.exactly_one()
.map_err(|_| EK::MissingToken.with_msg("No unique ntor onion key found."))?;
let key = curve25519::PublicKey::from(tok.parse_arg::<B64>(1)?.into_array()?);
key.into()
};
{
let tok = ipt_section.required(ENC_KEY_CERT)?;
let InnerCertData {
signing_key,
subject_key,
signature,
expiry,
} = handle_inner_certificate(
tok,
"ED25519 CERT",
tor_cert::CertType::HS_IP_CC_SIGNING,
)?;
expirations.push(expiry);
signatures.push(signature);
let expected_ed_key =
tor_llcrypto::pk::keymanip::convert_curve25519_to_ed25519_public(
&svc_ntor_key,
0,
);
if expected_ed_key != Some(subject_key) {
return Err(EK::BadObjectVal
.at_pos(tok.pos())
.with_msg("Mismatched subject key"));
}
if cert_signing_key.get_or_insert(signing_key) != &signing_key {
return Err(EK::BadObjectVal
.at_pos(tok.pos())
.with_msg("Mismatched signing key"));
}
};
intro_points.push(IntroPointDesc {
link_specifiers,
ipt_ntor_key: ntor_onion_key,
ipt_sid_key: auth_key,
svc_ntor_key,
});
}
let inner = HsDescInner {
intro_auth_types: auth_types,
single_onion_service: is_single_onion_service,
intro_points,
};
let sig_gated = SignatureGated::new(inner, signatures);
let time_bound = match expirations.iter().min() {
Some(t) => TimerangeBound::new(sig_gated, ..t),
None => TimerangeBound::new(sig_gated, ..),
};
Ok((cert_signing_key, time_bound))
}
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
use tor_checkable::{SelfSigned, Timebound};
use super::*;
use crate::doc::hsdesc::{
middle::HsDescMiddle,
outer::HsDescOuter,
test::{TEST_DATA, TEST_SUBCREDENTIAL},
};
#[test]
fn parse_good() -> Result<()> {
let desc = HsDescOuter::parse(TEST_DATA)?
.dangerously_assume_wellsigned()
.dangerously_assume_timely();
let subcred = TEST_SUBCREDENTIAL.into();
let body = desc.decrypt_body(&subcred).unwrap();
let body = std::str::from_utf8(&body[..]).unwrap();
let middle = HsDescMiddle::parse(body)?;
let inner_body = middle
.decrypt_inner(&desc.blinded_id(), desc.revision_counter(), &subcred, None)
.unwrap();
let inner_body = std::str::from_utf8(&inner_body).unwrap();
let inner = HsDescInner::parse(inner_body)?;
Ok(())
}
}