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
//! Trust Anchor Locators

use std::{fmt, str};
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::fs::{read_dir, DirEntry, File, ReadDir};
use std::io::{self, Read};
use std::path::Path;
use std::sync::Arc;
use bytes::Bytes;
use bcder::decode;
use log::{debug, error};
use serde::{Deserialize, Serialize};
use crate::crypto::PublicKey;
use super::uri;


//------------ Tal -----------------------------------------------------------

#[derive(Clone, Debug)]
pub struct Tal {
    uris: Vec<TalUri>,
    key_info: PublicKey,
    info: Arc<TalInfo>,
}

impl Tal {
    pub fn read_dir<P: AsRef<Path>>(path: P) -> Result<TalIter, io::Error> {
        read_dir(path).map(TalIter)
    }

    pub fn read<P: AsRef<Path>, R: Read>(
        path: P,
        reader: &mut R
    ) -> Result<Self, ReadError> {
        Self::read_named(
            path.as_ref().file_stem()
                .expect("TAL path needs to have a file name")
                .to_string_lossy().into_owned(),
            reader
        )
    }

    pub fn read_named<R: Read>(
        name: String,
        reader: &mut R
    ) -> Result<Self, ReadError> {
        let mut data = Vec::new();
        reader.read_to_end(&mut data)?;

        let mut data = data.as_slice();
        let mut uris = Vec::new();
        while let Some(&b'#') = data.first() {
            Self::skip_line(&mut data)?;
        }
        while let Some(uri) = Self::take_uri(&mut data)? {
            uris.push(uri)
        }
        let data: Vec<_> = data.iter().filter_map(|b|
            if b.is_ascii_whitespace() { None }
            else { Some(*b) }
        ).collect();
        let key_info = base64::decode(&data)?;
        let key_info = PublicKey::decode(key_info.as_ref())?;
        Ok(Tal {
            uris,
            key_info,
            info: Arc::new(TalInfo::from_name(name))
        })
    }

    /// Reorders the TAL URIs placing HTTPS URIs first.
    ///
    /// The method keeps the order within each scheme.
    pub fn prefer_https(&mut self) {
        self.uris.sort_by(|left, right| {
            match (left.is_https(), right.is_https()) {
                (true, false) => Ordering::Less,
                (false, true) => Ordering::Greater,
                _ => Ordering::Equal
            }
        })
    }

    fn skip_line(data: &mut &[u8]) -> Result<(), ReadError> {
        let mut split = data.splitn(2, |&ch| ch == b'\n');
        let _ = split.next().ok_or(ReadError::UnexpectedEof)?;
        *data = split.next().ok_or(ReadError::UnexpectedEof)?;
        Ok(())
    }

    fn take_uri(data: &mut &[u8]) -> Result<Option<TalUri>, ReadError> {
        let mut split = data.splitn(2, |&ch| ch == b'\n');
        let mut line = split.next().ok_or(ReadError::UnexpectedEof)?;
        *data = split.next().ok_or(ReadError::UnexpectedEof)?;
        if line.ends_with(b"\r") {
            line = line.split_last().unwrap().1;
        }
        if line.is_empty() {
            Ok(None)
        }
        else {
            Ok(Some(TalUri::from_slice(line)?))
        }
    }
}

impl Tal {
    pub fn uris(&self) -> ::std::slice::Iter<TalUri> {
        self.uris.iter()
    }

    pub fn key_info(&self) -> &PublicKey {
        &self.key_info
    }

    pub fn info(&self) -> &Arc<TalInfo> {
        &self.info
    }
}


//------------ TalIter -------------------------------------------------------

pub struct TalIter(ReadDir);

impl Iterator for TalIter {
    type Item = Result<Tal, ReadError>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.0.next() {
                Some(Ok(entry)) => {
                    match next_entry(&entry) {
                        Ok(Some(res)) => return Some(Ok(res)),
                        Ok(None) => { },
                        Err(err) => {
                            error!("Bad trust anchor {}", err);
                            return Some(Err(err))
                        }
                    }
                }
                Some(Err(err)) => return Some(Err(err.into())),
                None => return None
            };
        }
    }
}

fn next_entry(entry: &DirEntry) -> Result<Option<Tal>, ReadError> {
    if !entry.file_type()?.is_file() {
        return Ok(None)
    }
    let path = entry.path();
    debug!("Processing TAL {}", path.display());
    Tal::read(&path, &mut File::open(&path)?).map(Some)
}


//------------ TalUri --------------------------------------------------------

#[derive(
    Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize
)]
pub enum TalUri {
    Rsync(uri::Rsync),
    Https(uri::Https),
}

impl TalUri {
    pub fn from_string(s: String) -> Result<Self, uri::Error> {
        Self::from_bytes(Bytes::from(s))
    }

    pub fn from_slice(slice: &[u8]) -> Result<Self, uri::Error> {
        Self::from_bytes(Bytes::copy_from_slice(slice))
    }

    pub fn from_bytes(bytes: Bytes) -> Result<Self, uri::Error> {
        if let Ok(uri) = uri::Rsync::from_bytes(bytes.clone()) {
            return Ok(TalUri::Rsync(uri))
        }
        uri::Https::from_bytes(bytes).map(Into::into)
    }

    pub fn is_rsync(&self) -> bool {
        matches!(*self, TalUri::Rsync(_))
    }

    pub fn is_https(&self) -> bool {
        matches!(*self, TalUri::Https(_))
    }
}


//--- From

impl From<uri::Rsync> for TalUri {
    fn from(uri: uri::Rsync) -> Self {
        TalUri::Rsync(uri)
    }
}

impl From<uri::Https> for TalUri {
    fn from(uri: uri::Https) -> Self {
        TalUri::Https(uri)
    }
}


//--- TryFrom and FromStr

impl TryFrom<String> for TalUri {
    type Error = uri::Error;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::from_string(s)
    }
}

impl str::FromStr for TalUri {
    type Err = uri::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_bytes(Bytes::copy_from_slice(s.as_ref()))
    }
}


//--- Display

impl fmt::Display for TalUri {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            TalUri::Rsync(ref uri) => uri.fmt(f),
            TalUri::Https(ref uri) => uri.fmt(f)
        }
    }
}


//------------ TalInfo -------------------------------------------------------

#[derive(Clone, Debug)]
pub struct TalInfo {
    name: String,
}

impl TalInfo {
    pub fn from_name(name: String) -> Self {
        TalInfo { name }
    }

    pub fn into_arc(self) -> Arc<Self> {
        Arc::new(self)
    }

    pub fn name(&self) -> &str {
        self.name.as_ref()
    }
}


//------------ ReadError -----------------------------------------------------

#[derive(Debug)]
pub enum ReadError {
    Io(io::Error),
    UnexpectedEof,
    BadUri(uri::Error),
    BadKeyInfoEncoding(base64::DecodeError),
    BadKeyInfo(decode::Error),
}

impl From<io::Error> for ReadError {
    fn from(err: io::Error) -> ReadError {
        ReadError::Io(err)
    }
}

impl From<uri::Error> for ReadError {
    fn from(err: uri::Error) -> ReadError {
        ReadError::BadUri(err)
    }
}

impl From<base64::DecodeError> for ReadError {
    fn from(err: base64::DecodeError) -> ReadError {
        ReadError::BadKeyInfoEncoding(err)
    }
}

impl From<decode::Error> for ReadError {
    fn from(err: decode::Error) -> ReadError {
        ReadError::BadKeyInfo(err)
    }
}

impl fmt::Display for ReadError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ReadError::Io(ref err) => err.fmt(f),
            ReadError::UnexpectedEof
                => f.write_str("unexpected end of file"),
            ReadError::BadUri(ref err)
                => write!(f, "bad trust anchor URI: {}", err),
            ReadError::BadKeyInfoEncoding(ref err)
                => write!(f, "bad key info: {}", err),
            ReadError::BadKeyInfo(ref err)
                => write!(f, "bad key info: {}", err),
        }
    }
}


//============ Testing =======================================================

#[cfg(test)]
mod test {
    use bytes::Bytes;
    use crate::cert::Cert;
    use super::*;

    #[test]
    fn tal_read() {
        let tal = include_bytes!("../test-data/ripe.tal");
        let tal = Tal::read("ripe.tal", &mut tal.as_ref()).unwrap();
        let cert = Cert::decode(Bytes::from_static(
            include_bytes!("../test-data/ta.cer")
        )).unwrap();
        assert_eq!(
            tal.key_info(),
            cert.subject_public_key_info(),
        );
    }

    #[test]
    fn prefer_https() {
        let tal = include_bytes!("../test-data/ripe.tal");
        let mut tal = Tal::read("ripe.tal", &mut tal.as_ref()).unwrap();
        tal.uris = vec![
            TalUri::from_slice(b"rsync://a.example.com/1/1").unwrap(),
            TalUri::from_slice(b"https://d.example.com/1/1").unwrap(),
            TalUri::from_slice(b"rsync://k.example.com/2/1").unwrap(),
            TalUri::from_slice(b"https://2.example.com/2/1").unwrap(),
            TalUri::from_slice(b"https://i.example.com/3/1").unwrap(),
            TalUri::from_slice(b"rsync://g.example.com/3/1").unwrap(),
            TalUri::from_slice(b"https://r.example.com/4/1").unwrap(),
        ];
        tal.prefer_https();

        assert_eq!(
            tal.uris,
            vec![
                TalUri::from_slice(b"https://d.example.com/1/1").unwrap(),
                TalUri::from_slice(b"https://2.example.com/2/1").unwrap(),
                TalUri::from_slice(b"https://i.example.com/3/1").unwrap(),
                TalUri::from_slice(b"https://r.example.com/4/1").unwrap(),
                TalUri::from_slice(b"rsync://a.example.com/1/1").unwrap(),
                TalUri::from_slice(b"rsync://k.example.com/2/1").unwrap(),
                TalUri::from_slice(b"rsync://g.example.com/3/1").unwrap(),
            ]
        );
    }
}