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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
extern crate mime;

use std::fs::File;
use std::io::{self, Error as IoError, Read};
#[cfg(feature = "send3")]
use std::mem;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

#[cfg(feature = "send2")]
use self::mime::APPLICATION_OCTET_STREAM;
use mime_guess::{guess_mime_type, Mime};
use openssl::symm::encrypt_aead;
#[cfg(feature = "send2")]
use reqwest::header::AUTHORIZATION;
#[cfg(feature = "send2")]
use reqwest::multipart::{Form, Part};
use reqwest::Error as ReqwestError;
#[cfg(feature = "send2")]
use reqwest::Request;
#[cfg(feature = "send3")]
use serde_json;
use url::{ParseError as UrlParseError, Url};
#[cfg(feature = "send3")]
use websocket::{result::WebSocketError, OwnedMessage};

use super::params::{Error as ParamsError, Params, ParamsData};
use super::password::{Error as PasswordError, Password};
#[cfg(feature = "send2")]
use crate::api::nonce::header_nonce;
#[cfg(feature = "send2")]
use crate::api::request::ensure_success;
use crate::api::request::ResponseError;
use crate::api::Version;
use crate::client::Client;
use crate::crypto::b64;
use crate::crypto::key_set::KeySet;
#[cfg(feature = "send3")]
use crate::file::info::FileInfo;
use crate::file::metadata::Metadata;
use crate::file::remote_file::RemoteFile;
#[cfg(feature = "send3")]
use crate::io::ChunkRead;
#[cfg(feature = "send2")]
use crate::pipe::crypto::GcmCrypt;
#[cfg(feature = "send3")]
use crate::pipe::crypto::{ece, EceCrypt};
use crate::pipe::{
    prelude::*,
    progress::{ProgressPipe, ProgressReporter},
};

/// A file upload action to a Send server.
///
/// The server API version to use must be given.
pub struct Upload {
    /// The server API version to use.
    version: Version,

    /// The Send host to upload the file to.
    host: Url,

    /// The file to upload.
    path: PathBuf,

    /// The name of the file being uploaded.
    /// This has no relation to the file path, and will become the name of the
    /// shared file if set.
    name: Option<String>,

    /// An optional password to protect the file with.
    password: Option<String>,

    /// Optional file parameters to set.
    params: Option<ParamsData>,
}

impl Upload {
    /// Construct a new upload action.
    pub fn new(
        version: Version,
        host: Url,
        path: PathBuf,
        name: Option<String>,
        password: Option<String>,
        params: Option<ParamsData>,
    ) -> Self {
        Self {
            version,
            host,
            path,
            name,
            password,
            params,
        }
    }

    /// Invoke the upload action.
    pub fn invoke(
        self,
        client: &Client,
        reporter: Option<&Arc<Mutex<ProgressReporter>>>,
    ) -> Result<RemoteFile, Error> {
        // Create file data, generate a key
        let file = FileData::from(&self.path)?;
        let key = KeySet::generate(true);

        // Create the file reader
        let reader = self.create_reader(&key, reporter.cloned())?;
        let reader_len = reader.len_in() as u64;

        // Start the reporter
        if let Some(reporter) = reporter {
            reporter
                .lock()
                .map_err(|_| UploadError::Progress)?
                .start(reader_len);
        }

        // Execute the request
        let (result, nonce) = match self.version {
            #[cfg(feature = "send2")]
            Version::V2 => self.upload_send2(client, &key, &file, reader)?,
            #[cfg(feature = "send3")]
            Version::V3 => self.upload_send3(client, &key, &file, reader)?,
        };

        // Mark the reporter as finished
        if let Some(reporter) = reporter {
            reporter.lock().map_err(|_| UploadError::Progress)?.finish();
        }

        // Change the password if set
        if let Some(password) = self.password {
            Password::new(&result, &password, nonce.clone()).invoke(client)?;
        }

        // Change parameters if set
        if let Some(params) = self.params {
            Params::new(&result, params, nonce.clone()).invoke(client)?;
        }

        Ok(result)
    }

    /// Create a blob of encrypted metadata, used in Firefox Send v2.
    #[cfg(feature = "send2")]
    fn create_metadata(&self, key: &KeySet, file: &FileData) -> Result<Vec<u8>, MetaError> {
        // Determine what filename to use
        let name = self.name.clone().unwrap_or_else(|| file.name().to_owned());

        // Construct the metadata
        let metadata = Metadata::from_send2(key.iv(), name, &file.mime())
            .to_json()
            .into_bytes();

        // Encrypt the metadata
        let mut metadata_tag = vec![0u8; 16];
        let mut metadata = match encrypt_aead(
            KeySet::cipher(),
            key.meta_key().unwrap(),
            Some(&[0u8; 12]),
            &[],
            &metadata,
            &mut metadata_tag,
        ) {
            Ok(metadata) => metadata,
            Err(_) => return Err(MetaError::Encrypt),
        };

        // Append the encryption tag
        metadata.append(&mut metadata_tag);

        Ok(metadata)
    }

    /// Create file info to send to the server, used for Firefox Send v3.
    #[cfg(feature = "send3")]
    fn create_file_info(&self, key: &KeySet, file: &FileData) -> Result<String, MetaError> {
        // Determine what filename to use, build the metadata
        let name = self.name.clone().unwrap_or_else(|| file.name().to_owned());

        // Construct the metadata
        let mime = format!("{}", file.mime());
        let metadata = Metadata::from_send3(name, mime, file.size())
            .to_json()
            .into_bytes();

        // Encrypt the metadata
        let mut metadata_tag = vec![0u8; 16];
        let mut metadata = match encrypt_aead(
            KeySet::cipher(),
            key.meta_key().unwrap(),
            Some(&[0u8; 12]),
            &[],
            &metadata,
            &mut metadata_tag,
        ) {
            Ok(metadata) => metadata,
            Err(_) => return Err(MetaError::Encrypt),
        };

        // Append the encryption tag
        metadata.append(&mut metadata_tag);

        // Build file info for this metadata and return it as JSON
        Ok(FileInfo::from(None, None, b64::encode(&metadata), key).to_json())
    }

    /// Create a reader that reads the file as encrypted stream.
    fn create_reader(
        &self,
        key: &KeySet,
        reporter: Option<Arc<Mutex<ProgressReporter>>>,
    ) -> Result<Reader, Error> {
        // Open the file
        let file = match File::open(self.path.as_path()) {
            Ok(file) => file,
            Err(err) => return Err(FileError::Open(err).into()),
        };

        // Get the file length
        let len = file
            .metadata()
            .expect("failed to fetch file metadata")
            .len();

        // Build the progress pipe file reader
        let progress = ProgressPipe::zero(len, reporter);
        let reader = progress.reader(Box::new(file));

        // Build the encrypting file reader
        match self.version {
            #[cfg(feature = "send2")]
            Version::V2 => {
                let encrypt = GcmCrypt::encrypt(len as usize, key.file_key().unwrap(), key.iv());
                let reader = encrypt.reader(Box::new(reader));
                Ok(Reader::new(Box::new(reader)))
            }
            #[cfg(feature = "send3")]
            Version::V3 => {
                let ikm = key.secret().to_vec();
                let encrypt = EceCrypt::encrypt(len as usize, ikm, None);
                let reader = encrypt.reader(Box::new(reader));
                Ok(Reader::new(Box::new(reader)))
            }
        }
    }

    /// Upload the file to the server, used in Firefox Send v2.
    #[cfg(feature = "send2")]
    fn upload_send2(
        &self,
        client: &Client,
        key: &KeySet,
        file: &FileData,
        reader: Reader,
    ) -> Result<(RemoteFile, Option<Vec<u8>>), Error> {
        // Create metadata
        let metadata = self.create_metadata(&key, file)?;

        // Create the request to send
        let req = self.create_request_send2(client, &key, &metadata, reader)?;

        // Execute the request
        self.execute_request_send2(req, client, &key)
            .map_err(|e| e.into())
    }

    /// Build the request that will be send to the server, used in Firefox Send v2.
    #[cfg(feature = "send2")]
    fn create_request_send2(
        &self,
        client: &Client,
        key: &KeySet,
        metadata: &[u8],
        reader: Reader,
    ) -> Result<Request, UploadError> {
        // Get the reader output length
        let len = reader.len_out() as u64;

        // Configure a form to send
        let part = Part::reader_with_length(reader, len)
            .mime_str(APPLICATION_OCTET_STREAM.as_ref())
            .expect("failed to set request mime");
        let form = Form::new().part("data", part);

        // Define the URL to call
        let url = self.host.join("api/upload")?;

        // Build the request
        client
            .post(url.as_str())
            .header(
                AUTHORIZATION.as_str(),
                format!("send-v1 {}", key.auth_key_encoded().unwrap()),
            )
            .header("X-File-Metadata", b64::encode(&metadata))
            .multipart(form)
            .build()
            .map_err(|_| UploadError::Request)
    }

    /// Execute the given request, and create a file object that represents the
    /// uploaded file, used in Firefox Send v2.
    #[cfg(feature = "send2")]
    fn execute_request_send2(
        &self,
        req: Request,
        client: &Client,
        key: &KeySet,
    ) -> Result<(RemoteFile, Option<Vec<u8>>), UploadError> {
        // Execute the request
        let mut response = match client.execute(req) {
            Ok(response) => response,
            // TODO: attach the error context
            Err(_) => return Err(UploadError::Request),
        };

        // Ensure the response is successful
        ensure_success(&response).map_err(UploadError::Response)?;

        // Try to get the nonce, don't error on failure
        let nonce = header_nonce(&response).ok();

        // Decode the response
        let response: UploadResponse = match response.json() {
            Ok(response) => response,
            Err(err) => return Err(UploadError::Decode(err)),
        };

        // Transform the responce into a file object
        Ok((response.into_file(self.host.clone(), &key)?, nonce))
    }

    /// Upload the file to the server, used in Firefox Send v3.
    #[cfg(feature = "send3")]
    fn upload_send3(
        &self,
        client: &Client,
        key: &KeySet,
        file_data: &FileData,
        mut reader: Reader,
    ) -> Result<(RemoteFile, Option<Vec<u8>>), Error> {
        // Build the uploading websocket URL
        let ws_url = self
            .host
            .join("api/ws")
            .map_err(|e| Error::Upload(e.into()))?;

        // Build the websocket client used for uploading
        let mut client = client
            .websocket(ws_url.as_str())
            .map_err(|_| Error::Upload(UploadError::Request))?;

        // Create file info to sent when uploading
        let file_info = self
            .create_file_info(&key, file_data)
            .map_err(|e| -> Error { e.into() })?;
        let ws_metadata = OwnedMessage::Text(file_info);
        client
            .send_message(&ws_metadata)
            .map_err(|e| Error::Upload(e.into()))?;

        // Read the upload initialization response from the server
        let result = client
            .recv_message()
            .map_err(|_| Error::Upload(UploadError::InvalidResponse))?;
        let upload_response: UploadResponse = match result {
            OwnedMessage::Text(ref data) => serde_json::from_str(data)
                .map_err(|_| Error::Upload(UploadError::InvalidResponse))?,
            _ => return Err(UploadError::InvalidResponse.into()),
        };

        // Read the header part, and send it
        let mut header = vec![0u8; ece::HEADER_LEN as usize];
        reader
            .read_exact(&mut header)
            .expect("failed to read header from reader");
        client
            .send_message(&OwnedMessage::Binary(header))
            .map_err(|e| Error::Upload(e.into()))?;

        // Send the whole encrypted file in chunks as binary websocket messages
        let result =
            reader
                .chunks(ece::RS as usize)
                .fold(None, |result: Option<UploadError>, chunk| {
                    // Skip if an error occurred
                    if result.is_some() {
                        return result;
                    }

                    // Send the message, capture errors
                    let message = OwnedMessage::Binary(chunk.expect("invalid chunk"));
                    client.send_message(&message).err().map(|e| e.into())
                });
        if let Some(err) = result {
            return Err(err.into());
        }

        // Send the file footer
        client
            .send_message(&OwnedMessage::Binary(vec![0]))
            .map_err(|e| Error::Upload(e.into()))?;

        // Make sure we receive a success message from the server
        let status = match client
            .recv_message()
            .map_err(|_| Error::Upload(UploadError::InvalidResponse))?
        {
            OwnedMessage::Text(status) => Some(status),
            _ => None,
        };
        let ok = status
            .and_then(|s| serde_json::from_str::<UploadStatusResponse>(&s).ok())
            .map(|s| s.is_ok())
            .unwrap_or(false);
        if !ok {
            return Err(UploadError::Response(ResponseError::Undefined).into());
        }

        // Done uploading, explicitly close upload client
        let _ = client.shutdown();
        mem::drop(client);

        // Construct the remote file from the data we've obtained
        let remote_file = upload_response.into_file(self.host.clone(), &key)?;

        Ok((remote_file, None))
    }
}

/// The response from the server after a file has been uploaded.
/// This response contains the file ID and owner key, to manage the file.
///
/// It also contains the download URL, although an additional secret is
/// required.
///
/// The download URL can be generated using `download_url()` which will
/// include the required secret in the URL.
#[derive(Debug, Deserialize)]
struct UploadResponse {
    /// The file ID.
    id: String,

    /// The URL the file is reachable at.
    /// This includes the file ID, but does not include the secret.
    url: String,

    /// The owner key, used to do further file modifications.
    ///
    /// Called `owner` in Firefox Send v2, and `ownerToken` in Firefox Send v3.
    #[serde(alias = "ownerToken", alias = "owner")]
    owner_token: String,
}

impl UploadResponse {
    /// Convert this response into a file object.
    ///
    /// The `host` and `key` must be given.
    pub fn into_file(self, host: Url, key: &KeySet) -> Result<RemoteFile, UploadError> {
        Ok(RemoteFile::new_now(
            self.id,
            host,
            Url::parse(&self.url)?,
            key.secret().to_vec(),
            Some(self.owner_token),
        ))
    }
}

/// The status response from the server over the websocket during or after uploading.
/// This defines whether uploading was successful.
#[derive(Debug, Deserialize)]
#[cfg(feature = "send3")]
struct UploadStatusResponse {
    /// True if ok, false if not.
    ok: bool,
}

#[cfg(feature = "send3")]
impl UploadStatusResponse {
    /// Check if OK.
    pub fn is_ok(&self) -> bool {
        self.ok
    }
}

/// A struct that holds various file properties, such as it's name and it's
/// mime type.
struct FileData<'a> {
    /// The file name.
    name: &'a str,

    /// The file mime type.
    mime: Mime,

    /// The file size.
    #[allow(unused)]
    size: u64,
}

impl<'a> FileData<'a> {
    /// Create a file data object, from the file at the given path.
    pub fn from(path: &'a PathBuf) -> Result<Self, FileError> {
        // Make sure the given path is a file
        if !path.is_file() {
            return Err(FileError::NotAFile);
        }

        // Get the file name
        let name = match path.file_name() {
            Some(name) => name.to_str().unwrap_or("file"),
            None => "file",
        };

        // Get the file size
        let size = path.metadata()?.len();

        Ok(Self {
            name,
            mime: guess_mime_type(path),
            size,
        })
    }

    /// Get the file name.
    pub fn name(&self) -> &str {
        self.name
    }

    /// Get the file mime type.
    pub fn mime(&self) -> &Mime {
        &self.mime
    }

    /// Get the file size in bytes.
    #[cfg(feature = "send3")]
    pub fn size(&self) -> u64 {
        self.size
    }
}

/// A wrapped reader to make it easier to pass around.
struct Reader {
    // TODO: use better type
    inner: Box<dyn ReadLen>,
}

impl Reader {
    /// Construct a new wrapped reader.
    pub fn new(inner: Box<dyn ReadLen>) -> Self {
        Self { inner }
    }
}

impl Read for Reader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl PipeLen for Reader {
    fn len_in(&self) -> usize {
        self.inner.len_in()
    }

    fn len_out(&self) -> usize {
        self.inner.len_out()
    }
}

#[derive(Fail, Debug)]
pub enum Error {
    /// An error occurred while preparing a file for uploading.
    #[fail(display = "failed to prepare uploading the file")]
    Prepare(#[cause] PrepareError),

    /// An error occurred while opening, reading or using the file that
    /// the should be uploaded.
    // TODO: maybe append the file path here for further information
    #[fail(display = "")]
    File(#[cause] FileError),

    /// An error occurred while uploading the file.
    #[fail(display = "failed to upload the file")]
    Upload(#[cause] UploadError),

    /// An error occurred while chaining file parameters.
    #[fail(display = "failed to change file parameters")]
    Params(#[cause] ParamsError),

    /// An error occurred while setting the password.
    #[fail(display = "failed to set the password")]
    Password(#[cause] PasswordError),
}

impl From<MetaError> for Error {
    fn from(err: MetaError) -> Error {
        Error::Prepare(PrepareError::Meta(err))
    }
}

impl From<FileError> for Error {
    fn from(err: FileError) -> Error {
        Error::File(err)
    }
}

impl From<ReaderError> for Error {
    fn from(err: ReaderError) -> Error {
        Error::Prepare(PrepareError::Reader(err))
    }
}

impl From<UploadError> for Error {
    fn from(err: UploadError) -> Error {
        Error::Upload(err)
    }
}

impl From<ParamsError> for Error {
    fn from(err: ParamsError) -> Error {
        Error::Params(err)
    }
}

impl From<PasswordError> for Error {
    fn from(err: PasswordError) -> Error {
        Error::Password(err)
    }
}

#[derive(Fail, Debug)]
pub enum PrepareError {
    /// Failed to prepare the file metadata for uploading.
    #[fail(display = "failed to prepare file metadata")]
    Meta(#[cause] MetaError),

    /// Failed to create an encrypted file reader, that encrypts
    /// the file on the fly when it is read.
    #[fail(display = "failed to access the file to upload")]
    Reader(#[cause] ReaderError),

    /// Failed to create a client for uploading a file.
    #[fail(display = "failed to create uploader client")]
    Client,
}

#[derive(Fail, Debug)]
pub enum MetaError {
    /// An error occurred while encrypting the file metadata.
    #[fail(display = "failed to encrypt file metadata")]
    Encrypt,
}

#[derive(Fail, Debug)]
pub enum ReaderError {
    /// An error occurred while creating the file encryptor.
    #[fail(display = "failed to create file encryptor")]
    Encrypt,

    /// Failed to create the progress reader, attached to the file reader,
    /// to measure the uploading progress.
    #[fail(display = "failed to create progress reader")]
    Progress,
}

#[derive(Fail, Debug)]
pub enum FileError {
    /// The given path, is not not a file or doesn't exist.
    #[fail(display = "the given path is not an existing file")]
    NotAFile,

    /// Failed to open the file that must be uploaded for reading.
    #[fail(display = "failed to open the file to upload")]
    Open(#[cause] IoError),
}

impl From<IoError> for FileError {
    fn from(err: IoError) -> FileError {
        FileError::Open(err)
    }
}

#[derive(Fail, Debug)]
pub enum UploadError {
    /// Failed to start or update the uploading progress, because of this the
    /// upload can't continue.
    #[fail(display = "failed to update upload progress")]
    Progress,

    /// Sending the request to upload the file failed.
    #[fail(display = "failed to request file upload")]
    Request,

    /// An error occurred while streaming the encrypted file (including file info, header and
    /// footer) for uploading over a websocket.
    #[fail(display = "failed to stream file for upload over websocket")]
    #[cfg(feature = "send3")]
    UploadStream(#[cause] WebSocketError),

    /// The server responded with data that was not understood, or did not respond at all while a
    /// response was espected.
    #[fail(display = "got invalid response from server")]
    InvalidResponse,

    /// The server responded with an error for uploading.
    #[fail(display = "bad response from server for uploading")]
    Response(#[cause] ResponseError),

    /// Failed to decode the upload response from the server.
    /// Maybe the server responded with data from a newer API version.
    #[fail(display = "failed to decode upload response")]
    Decode(#[cause] ReqwestError),

    /// Failed to parse the retrieved URL from the upload response.
    #[fail(display = "failed to parse received URL")]
    ParseUrl(#[cause] UrlParseError),
}

#[cfg(feature = "send3")]
impl From<WebSocketError> for UploadError {
    fn from(err: WebSocketError) -> UploadError {
        UploadError::UploadStream(err)
    }
}

impl From<ResponseError> for UploadError {
    fn from(err: ResponseError) -> UploadError {
        UploadError::Response(err)
    }
}

impl From<UrlParseError> for UploadError {
    fn from(err: UrlParseError) -> UploadError {
        UploadError::ParseUrl(err)
    }
}