1
//! Support for encoding the network document meta-format
2
//!
3
//! Implements writing documents according to
4
//! [dir-spec.txt](https://spec.torproject.org/dir-spec).
5
//! section 1.2 and 1.3.
6
//!
7
//! This facility processes output that complies with the meta-document format,
8
//! (`dir-spec.txt` section 1.2) -
9
//! unless `raw` methods are called with improper input.
10
//!
11
//! However, no checks are done on keyword presence/absence, multiplicity, or ordering,
12
//! so the output may not necessarily conform to the format of the particular intended document.
13
//! It is the caller's responsibility to call `.item()` in the right order,
14
//! with the right keywords and arguments.
15

            
16
// TODO Plan for encoding signed documents:
17
//
18
//  * Derive an encoder function for Foo; the encoder gives you Encoded<Foo>.
19
//  * Write code ad-hoc to construct FooSignatures.
20
//  * Call encoder-core-provided method on Encoded to add the signatures
21
//
22
// Method(s) on Encoded<Foo> are provided centrally to let you get the &str to hash it.
23
//
24
// Nothing cooked is provided to help with the signature encoding layering violation:
25
// the central encoding derives do not provide any way to obtain a partly-encoded
26
// signature item so that it can be added to the hash.
27
//
28
// So the signing code must recapitulate some of the item encoding.  This will generally
29
// be simply a const str (or similar) with the encoded item name and any parameters,
30
// in precisely the form that needs to be appended to the hash.
31
//
32
// This does leave us open to bugs where the hashed data doesn't match what ends up
33
// being encoded, but since it's a fixed string, such a bug couldn't survive a smoke test.
34
//
35
// If there are items where the layering violation involves encoding
36
// of variable parameters, this would need further work, either ad-hoc,
37
// or additional traits/macrology/etc. if there's enough cases where it's needed.
38

            
39
mod multiplicity;
40
#[macro_use]
41
mod derive;
42
mod impls;
43

            
44
use std::cmp;
45
use std::collections::BTreeSet;
46
use std::fmt::Write;
47
use std::iter;
48
use std::marker::PhantomData;
49
use std::sync::Arc;
50

            
51
use base64ct::{Base64, Base64Unpadded, Encoding};
52
use educe::Educe;
53
use itertools::Itertools;
54
use paste::paste;
55
use rand::{CryptoRng, RngCore};
56
use tor_bytes::EncodeError;
57
use tor_error::internal;
58
use void::Void;
59

            
60
use crate::KeywordEncodable;
61
use crate::parse::tokenize::tag_keywords_ok;
62
use crate::types::misc::Iso8601TimeSp;
63

            
64
// Exports used by macros, which treat this module as a prelude
65
#[doc(hidden)]
66
pub use {
67
    crate::netdoc_ordering_check,
68
    derive::{DisplayHelper, RestMustComeLastMarker},
69
    multiplicity::{
70
        MultiplicityMethods, MultiplicitySelector, OptionalityMethods,
71
        SingletonMultiplicitySelector,
72
    },
73
    std::fmt::{self, Display},
74
    std::result::Result,
75
    tor_error::{Bug, into_internal},
76
};
77

            
78
/// Encoder, representing a partially-built document.
79
///
80
/// For example usage, see the tests in this module, or a descriptor building
81
/// function in tor-netdoc (such as `hsdesc::build::inner::HsDescInner::build_sign`).
82
#[derive(Debug, Clone)]
83
pub struct NetdocEncoder {
84
    /// The being-built document, with everything accumulated so far
85
    ///
86
    /// If an [`ItemEncoder`] exists, it will add a newline when it's dropped.
87
    ///
88
    /// `Err` means bad values passed to some builder function.
89
    /// Such errors are accumulated here for the benefit of handwritten document encoders.
90
    built: Result<String, Bug>,
91
}
92

            
93
/// Encoder for an individual item within a being-built document
94
///
95
/// Returned by [`NetdocEncoder::item()`].
96
#[derive(Debug)]
97
pub struct ItemEncoder<'n> {
98
    /// The document including the partial item that we're building
99
    ///
100
    /// We will always add a newline when we're dropped
101
    doc: &'n mut NetdocEncoder,
102
}
103

            
104
/// Position within a (perhaps partially-) built document
105
///
106
/// This is provided mainly to allow the caller to perform signature operations
107
/// on the part of the document that is to be signed.
108
/// (Sometimes this is only part of it.)
109
///
110
/// There is no enforced linkage between this and the document it refers to.
111
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
112
pub struct Cursor {
113
    /// The offset (in bytes, as for `&str`)
114
    ///
115
    /// Can be out of range if the corresponding `NetdocEncoder` is contains an `Err`.
116
    offset: usize,
117
}
118

            
119
/// Types that can be added as argument(s) to item keyword lines
120
///
121
/// Implemented for strings, and various other types.
122
///
123
/// This is a separate trait so we can control the formatting of (eg) [`Iso8601TimeSp`],
124
/// without having a method on `ItemEncoder` for each argument type.
125
//
126
// TODO consider renaming this to ItemArgumentEncodable to mirror all the other related traits.
127
pub trait ItemArgument {
128
    /// Format as a string suitable for including as a netdoc keyword line argument
129
    ///
130
    /// The implementation is responsible for checking that the syntax is legal.
131
    /// For example, if `self` is a string, it must check that the string is
132
    /// in legal as a single argument.
133
    ///
134
    /// Some netdoc values (eg times) turn into several arguments; in that case,
135
    /// one `ItemArgument` may format into multiple arguments, and this method
136
    /// is responsible for writing them all, with the necessary spaces.
137
    fn write_arg_onto(&self, out: &mut ItemEncoder<'_>) -> Result<(), Bug>;
138
}
139

            
140
impl NetdocEncoder {
141
    /// Start encoding a document
142
11530
    pub fn new() -> Self {
143
11530
        NetdocEncoder {
144
11530
            built: Ok(String::new()),
145
11530
        }
146
11530
    }
147

            
148
    /// Adds an item to the being-built document
149
    ///
150
    /// The item can be further extended with arguments or an object,
151
    /// using the returned `ItemEncoder`.
152
62960
    pub fn item(&mut self, keyword: impl KeywordEncodable) -> ItemEncoder {
153
62960
        self.raw(&keyword.to_str());
154
62960
        ItemEncoder { doc: self }
155
62960
    }
156

            
157
    /// Internal name for `push_raw_string()`
158
295170
    fn raw(&mut self, s: &dyn Display) {
159
301383
        self.write_with(|b| {
160
295170
            write!(b, "{}", s).expect("write! failed on String");
161
295170
            Ok(())
162
295170
        });
163
295170
    }
164

            
165
    /// Extend the being-built document with a fallible function `f`
166
    ///
167
    /// Doesn't call `f` if the building has already failed,
168
    /// and handles the error if `f` fails.
169
303904
    fn write_with(&mut self, f: impl FnOnce(&mut String) -> Result<(), Bug>) {
170
303904
        let Ok(build) = &mut self.built else {
171
            return;
172
        };
173
303904
        match f(build) {
174
303904
            Ok(()) => (),
175
            Err(e) => {
176
                self.built = Err(e);
177
            }
178
        }
179
303904
    }
180

            
181
    /// Adds raw text to the being-built document
182
    ///
183
    /// `s` is added as raw text, after the newline ending the previous item.
184
    /// If `item` is subsequently called, the start of that item
185
    /// will immediately follow `s`.
186
    ///
187
    /// It is the responsibility of the caller to obey the metadocument syntax.
188
    /// In particular, `s` should end with a newline.
189
    /// No checks are performed.
190
    /// Incorrect use might lead to malformed documents, or later errors.
191
    pub fn push_raw_string(&mut self, s: &dyn Display) {
192
        self.raw(s);
193
    }
194

            
195
    /// Return a cursor, pointing to just after the last item (if any)
196
7652
    pub fn cursor(&self) -> Cursor {
197
7652
        let offset = match &self.built {
198
7652
            Ok(b) => b.len(),
199
            Err(_) => usize::MAX,
200
        };
201
7652
        Cursor { offset }
202
7652
    }
203

            
204
    /// Obtain the text of a section of the document
205
    ///
206
    /// Useful for making a signature.
207
3826
    pub fn slice(&self, begin: Cursor, end: Cursor) -> Result<&str, Bug> {
208
3826
        self.built
209
3826
            .as_ref()
210
3826
            .map_err(Clone::clone)?
211
3826
            .get(begin.offset..end.offset)
212
3826
            .ok_or_else(|| internal!("NetdocEncoder::slice out of bounds, Cursor mismanaged"))
213
3826
    }
214

            
215
    /// Obtain the document so far in textual form
216
4
    pub fn text_sofar(&self) -> Result<&str, Bug> {
217
4
        self.built.as_deref().map_err(Clone::clone)
218
4
    }
219

            
220
    /// Build the document into textual form
221
11518
    pub fn finish(self) -> Result<String, Bug> {
222
11518
        self.built
223
11518
    }
224
}
225

            
226
impl Default for NetdocEncoder {
227
16
    fn default() -> Self {
228
        // We must open-code this because the actual encoder contains Result, which isn't Default
229
16
        NetdocEncoder::new()
230
16
    }
231
}
232

            
233
impl<T: crate::NormalItemArgument> ItemArgument for T {
234
61490
    fn write_arg_onto(&self, out: &mut ItemEncoder<'_>) -> Result<(), Bug> {
235
61490
        (*self.to_string()).write_arg_onto(out)
236
61490
    }
237
}
238

            
239
impl<'n> ItemEncoder<'n> {
240
    /// Add a single argument.
241
    ///
242
    /// Convenience method that defers error handling, for use in infallible contexts.
243
    /// Consider whether to use `ItemArgument::write_arg_onto` directly, instead.
244
    ///
245
    /// If the argument is not in the correct syntax, a `Bug`
246
    /// error will be reported (later).
247
    //
248
    // This is not a hot path.  `dyn` for smaller code size.
249
95676
    pub fn arg(mut self, arg: &dyn ItemArgument) -> Self {
250
95676
        self.add_arg(arg);
251
95676
        self
252
95676
    }
253

            
254
    /// Add a single argument, to a borrowed `ItemEncoder`
255
    ///
256
    /// If the argument is not in the correct syntax, a `Bug`
257
    /// error will be reported (later).
258
    //
259
    // Needed for implementing `ItemArgument`
260
95682
    pub(crate) fn add_arg(&mut self, arg: &dyn ItemArgument) {
261
95682
        let () = arg
262
95682
            .write_arg_onto(self)
263
95682
            .unwrap_or_else(|err| self.doc.built = Err(err));
264
95682
    }
265

            
266
    /// Add zero or more arguments, supplied as a single string.
267
    ///
268
    /// `args` should zero or more valid argument strings,
269
    /// separated by (single) spaces.
270
    /// This is not (properly) checked.
271
    /// Incorrect use might lead to malformed documents, or later errors.
272
16
    pub fn args_raw_string(&mut self, args: &dyn Display) {
273
16
        let args = args.to_string();
274
16
        if !args.is_empty() {
275
16
            self.args_raw_nonempty(&args);
276
16
        }
277
16
    }
278

            
279
    /// Add one or more arguments, supplied as a single string, without any checking
280
95810
    fn args_raw_nonempty(&mut self, args: &dyn Display) {
281
95810
        self.doc.raw(&format_args!(" {}", args));
282
95810
    }
283

            
284
    /// Add an `ItemObjectEncodable` to the item
285
    //
286
    // Note that the `ItemValueEncodable` derive macro (in `derive.rs`)
287
    // also implements this functionality.
288
6
    pub fn object(self, object: &dyn ItemObjectEncodable) {
289
6
        let label = object.label();
290
6
        let mut buf = vec![];
291
6
        object
292
6
            .write_object_onto(&mut buf)
293
6
            .unwrap_or_else(|err| self.doc.built = Err(err));
294
6
        self.object_bytes(label, buf);
295
6
    }
296

            
297
    /// Add an object to the item, given the keyword and a `tor_bytes::WriteableOnce`
298
    ///
299
    /// Checks that `keywords` is in the correct syntax.
300
    /// Doesn't check that it makes semantic sense for the position of the document.
301
    /// `data` will be PEM (base64) encoded.
302
    //
303
    // If keyword is not in the correct syntax, a `Bug` is stored in self.doc.
304
8734
    pub fn object_bytes(
305
8734
        self,
306
8734
        keywords: &str,
307
8734
        // Writeable isn't dyn-compatible
308
8734
        data: impl tor_bytes::WriteableOnce,
309
8734
    ) {
310
        use crate::parse::tokenize::object::*;
311

            
312
8734
        self.doc.write_with(|out| {
313
8734
            if keywords.is_empty() || !tag_keywords_ok(keywords) {
314
                return Err(internal!("bad object keywords string {:?}", keywords));
315
8734
            }
316
8734
            let data = {
317
8734
                let mut bytes = vec![];
318
8734
                data.write_into(&mut bytes)?;
319
8734
                Base64::encode_string(&bytes)
320
            };
321
8734
            let mut data = &data[..];
322
8734
            writeln!(out, "\n{BEGIN_STR}{keywords}{TAG_END}").expect("write!");
323
1017408
            while !data.is_empty() {
324
1008674
                let (l, r) = if data.len() > BASE64_PEM_MAX_LINE {
325
999944
                    data.split_at(BASE64_PEM_MAX_LINE)
326
                } else {
327
8730
                    (data, "")
328
                };
329
1008674
                writeln!(out, "{l}").expect("write!");
330
1008674
                data = r;
331
            }
332
            // final newline will be written by Drop impl
333
8734
            write!(out, "{END_STR}{keywords}{TAG_END}").expect("write!");
334
8734
            Ok(())
335
8734
        });
336
8734
    }
337

            
338
    /// Finish encoding this item
339
    ///
340
    /// The item will also automatically be finished if the `ItemEncoder` is dropped.
341
46
    pub fn finish(self) {}
342
}
343

            
344
impl Drop for ItemEncoder<'_> {
345
99680
    fn drop(&mut self) {
346
99680
        self.doc.raw(&'\n');
347
99680
    }
348
}
349

            
350
/// Ordering, to be used when encoding network documents
351
///
352
/// Implemented for anything `Ord`.
353
///
354
/// Can also be implemented manually, for if a type cannot be `Ord`
355
/// (perhaps for trait coherence reasons).
356
pub trait EncodeOrd {
357
    /// Compare `self` and `other`
358
    ///
359
    /// As `Ord::cmp`.
360
    fn encode_cmp(&self, other: &Self) -> cmp::Ordering;
361
}
362
impl<T: Ord> EncodeOrd for T {
363
20
    fn encode_cmp(&self, other: &Self) -> cmp::Ordering {
364
20
        self.cmp(other)
365
20
    }
366
}
367

            
368
/// Documents (or sub-documents) that can be encoded in the netdoc metaformat
369
pub trait NetdocEncodable {
370
    /// Append the document onto `out`
371
    fn encode_unsigned(&self, out: &mut NetdocEncoder) -> Result<(), Bug>;
372
}
373

            
374
/// Collections of fields that can be encoded in the netdoc metaformat
375
///
376
/// Whole documents have structure; a `NetdocEncodableFields` does not.
377
pub trait NetdocEncodableFields {
378
    /// Append the document onto `out`
379
    fn encode_fields(&self, out: &mut NetdocEncoder) -> Result<(), Bug>;
380
}
381

            
382
/// Items that can be encoded in network documents
383
pub trait ItemValueEncodable {
384
    /// Write the item's arguments, and any object, onto `out`
385
    ///
386
    /// `out` will have been freshly returned from [`NetdocEncoder::item`].
387
    fn write_item_value_onto(&self, out: ItemEncoder) -> Result<(), Bug>;
388
}
389

            
390
/// An Object value that be encoded into a netdoc
391
pub trait ItemObjectEncodable {
392
    /// The label (keyword(s) in `BEGIN` and `END`)
393
    fn label(&self) -> &str;
394

            
395
    /// Represent the actual value as bytes.
396
    ///
397
    /// The caller, not the object, is responsible for base64 encoding.
398
    //
399
    // This is not a tor_bytes::Writeable supertrait because tor_bytes's writer argument
400
    // is generic, which prevents many deisrable manipulations of an `impl Writeable`.
401
    fn write_object_onto(&self, b: &mut Vec<u8>) -> Result<(), Bug>;
402
}
403

            
404
/// Builders for network documents.
405
///
406
/// This trait is a bit weird, because its `Self` type must contain the *private* keys
407
/// necessary to sign the document!
408
///
409
/// So it is implemented for "builders", not for documents themselves.
410
/// Some existing documents can be constructed only via these builders.
411
/// The newer approach is for documents to be transparent data, at the Rust level,
412
/// and to derive an encoder.
413
/// TODO this derive approach is not yet implemented!
414
///
415
/// Actual document types, which only contain the information in the document,
416
/// don't implement this trait.
417
pub trait NetdocBuilder {
418
    /// Build the document into textual form.
419
    fn build_sign<R: RngCore + CryptoRng>(self, rng: &mut R) -> Result<String, EncodeError>;
420
}
421

            
422
/// implement [`ItemValueEncodable`] for a particular tuple size
423
macro_rules! item_value_encodable_for_tuple {
424
    { $($i:literal)* } => { paste! {
425
        impl< $( [<T$i>]: ItemArgument, )* > ItemValueEncodable for ( $( [<T$i>], )* ) {
426
82
            fn write_item_value_onto(
427
82
                &self,
428
82
                #[allow(unused)]
429
82
                mut out: ItemEncoder,
430
82
            ) -> Result<(), Bug> {
431
                $(
432
48
                    <[<T$i>] as ItemArgument>::write_arg_onto(&self.$i, &mut out)?;
433
                )*
434
82
                Ok(())
435
82
            }
436
        }
437
    } }
438
}
439

            
440
item_value_encodable_for_tuple! {}
441
item_value_encodable_for_tuple! { 0 }
442
item_value_encodable_for_tuple! { 0 1 }
443
item_value_encodable_for_tuple! { 0 1 2 }
444
item_value_encodable_for_tuple! { 0 1 2 3 }
445
item_value_encodable_for_tuple! { 0 1 2 3 4 }
446
item_value_encodable_for_tuple! { 0 1 2 3 4 5 }
447
item_value_encodable_for_tuple! { 0 1 2 3 4 5 6 }
448
item_value_encodable_for_tuple! { 0 1 2 3 4 5 6 7 }
449
item_value_encodable_for_tuple! { 0 1 2 3 4 5 6 7 8 }
450
item_value_encodable_for_tuple! { 0 1 2 3 4 5 6 7 8 9 }
451

            
452
#[cfg(test)]
453
mod test {
454
    // @@ begin test lint list maintained by maint/add_warning @@
455
    #![allow(clippy::bool_assert_comparison)]
456
    #![allow(clippy::clone_on_copy)]
457
    #![allow(clippy::dbg_macro)]
458
    #![allow(clippy::mixed_attributes_style)]
459
    #![allow(clippy::print_stderr)]
460
    #![allow(clippy::print_stdout)]
461
    #![allow(clippy::single_char_pattern)]
462
    #![allow(clippy::unwrap_used)]
463
    #![allow(clippy::unchecked_time_subtraction)]
464
    #![allow(clippy::useless_vec)]
465
    #![allow(clippy::needless_pass_by_value)]
466
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
467
    use super::*;
468
    use std::str::FromStr;
469

            
470
    use crate::types::misc::Iso8601TimeNoSp;
471
    use base64ct::{Base64Unpadded, Encoding};
472

            
473
    #[test]
474
    fn time_formats_as_args() {
475
        use crate::doc::authcert::AuthCertKwd as ACK;
476
        use crate::doc::netstatus::NetstatusKwd as NK;
477

            
478
        let t_sp = Iso8601TimeSp::from_str("2020-04-18 08:36:57").unwrap();
479
        let t_no_sp = Iso8601TimeNoSp::from_str("2021-04-18T08:36:57").unwrap();
480

            
481
        let mut encode = NetdocEncoder::new();
482
        encode.item(ACK::DIR_KEY_EXPIRES).arg(&t_sp);
483
        encode
484
            .item(NK::SHARED_RAND_PREVIOUS_VALUE)
485
            .arg(&"3")
486
            .arg(&"bMZR5Q6kBadzApPjd5dZ1tyLt1ckv1LfNCP/oyGhCXs=")
487
            .arg(&t_no_sp);
488

            
489
        let doc = encode.finish().unwrap();
490
        println!("{}", doc);
491
        assert_eq!(
492
            doc,
493
            r"dir-key-expires 2020-04-18 08:36:57
494
shared-rand-previous-value 3 bMZR5Q6kBadzApPjd5dZ1tyLt1ckv1LfNCP/oyGhCXs= 2021-04-18T08:36:57
495
"
496
        );
497
    }
498

            
499
    #[test]
500
    fn authcert() {
501
        use crate::doc::authcert::AuthCertKwd as ACK;
502
        use crate::doc::authcert::{AuthCert, UncheckedAuthCert};
503

            
504
        // c&p from crates/tor-llcrypto/tests/testvec.rs
505
        let pk_rsa = {
506
            let pem = "
507
MIGJAoGBANUntsY9boHTnDKKlM4VfczcBE6xrYwhDJyeIkh7TPrebUBBvRBGmmV+
508
PYK8AM9irDtqmSR+VztUwQxH9dyEmwrM2gMeym9uXchWd/dt7En/JNL8srWIf7El
509
qiBHRBGbtkF/Re5pb438HC/CGyuujp43oZ3CUYosJOfY/X+sD0aVAgMBAAE";
510
            Base64Unpadded::decode_vec(&pem.replace('\n', "")).unwrap()
511
        };
512

            
513
        let mut encode = NetdocEncoder::new();
514
        encode.item(ACK::DIR_KEY_CERTIFICATE_VERSION).arg(&3);
515
        encode
516
            .item(ACK::FINGERPRINT)
517
            .arg(&"9367f9781da8eabbf96b691175f0e701b43c602e");
518
        encode
519
            .item(ACK::DIR_KEY_PUBLISHED)
520
            .arg(&Iso8601TimeSp::from_str("2020-04-18 08:36:57").unwrap());
521
        encode
522
            .item(ACK::DIR_KEY_EXPIRES)
523
            .arg(&Iso8601TimeSp::from_str("2021-04-18 08:36:57").unwrap());
524
        encode
525
            .item(ACK::DIR_IDENTITY_KEY)
526
            .object_bytes("RSA PUBLIC KEY", &*pk_rsa);
527
        encode
528
            .item(ACK::DIR_SIGNING_KEY)
529
            .object_bytes("RSA PUBLIC KEY", &*pk_rsa);
530
        encode
531
            .item(ACK::DIR_KEY_CROSSCERT)
532
            .object_bytes("ID SIGNATURE", []);
533
        encode
534
            .item(ACK::DIR_KEY_CERTIFICATION)
535
            .object_bytes("SIGNATURE", []);
536

            
537
        let doc = encode.finish().unwrap();
538
        eprintln!("{}", doc);
539
        assert_eq!(
540
            doc,
541
            r"dir-key-certificate-version 3
542
fingerprint 9367f9781da8eabbf96b691175f0e701b43c602e
543
dir-key-published 2020-04-18 08:36:57
544
dir-key-expires 2021-04-18 08:36:57
545
dir-identity-key
546
-----BEGIN RSA PUBLIC KEY-----
547
MIGJAoGBANUntsY9boHTnDKKlM4VfczcBE6xrYwhDJyeIkh7TPrebUBBvRBGmmV+
548
PYK8AM9irDtqmSR+VztUwQxH9dyEmwrM2gMeym9uXchWd/dt7En/JNL8srWIf7El
549
qiBHRBGbtkF/Re5pb438HC/CGyuujp43oZ3CUYosJOfY/X+sD0aVAgMBAAE=
550
-----END RSA PUBLIC KEY-----
551
dir-signing-key
552
-----BEGIN RSA PUBLIC KEY-----
553
MIGJAoGBANUntsY9boHTnDKKlM4VfczcBE6xrYwhDJyeIkh7TPrebUBBvRBGmmV+
554
PYK8AM9irDtqmSR+VztUwQxH9dyEmwrM2gMeym9uXchWd/dt7En/JNL8srWIf7El
555
qiBHRBGbtkF/Re5pb438HC/CGyuujp43oZ3CUYosJOfY/X+sD0aVAgMBAAE=
556
-----END RSA PUBLIC KEY-----
557
dir-key-crosscert
558
-----BEGIN ID SIGNATURE-----
559
-----END ID SIGNATURE-----
560
dir-key-certification
561
-----BEGIN SIGNATURE-----
562
-----END SIGNATURE-----
563
"
564
        );
565

            
566
        let _: UncheckedAuthCert = AuthCert::parse(&doc).unwrap();
567
    }
568
}