1 //**********************************************************************************
2 //EncryptPad Copyright 2016 Evgeny Pokhilko
3 //<http://www.evpo.net/encryptpad>
4 //
5 //This file is part of EncryptPad
6 //
7 //EncryptPad is free software: you can redistribute it and/or modify
8 //it under the terms of the GNU General Public License as published by
9 //the Free Software Foundation, either version 2 of the License, or
10 //(at your option) any later version.
11 //
12 //EncryptPad is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with EncryptPad.  If not, see <http://www.gnu.org/licenses/>.
19 //**********************************************************************************
20 #pragma once
21 #include <iostream>
22 #include <iterator>
23 #include <algorithm>
24 #include <memory>
25 #include <vector>
26 #include <functional>
27 #include "botan.h"
28 #include "packet_typedef.h"
29 #include "encryptmsg/algo_spec.h"
30 #include "algo_defaults.h"
31 #include "packet_stream.h"
32 #include "key_service.h"
33 
34 namespace EncryptPad
35 {
36     struct ProgressEvent
37     {
38         stream_length_type total_bytes;
39         stream_length_type complete_bytes;
40         bool cancel;
ProgressEventProgressEvent41         ProgressEvent():
42             total_bytes(0),
43             complete_bytes(0),
44             cancel(false)
45         {
46         }
47 
ProgressEventProgressEvent48         ProgressEvent(stream_length_type total_bytes, stream_length_type complete_bytes):
49             total_bytes(total_bytes),
50             complete_bytes(complete_bytes),
51             cancel(false)
52         {
53         }
54     };
55 
56     using ProgressCallback = std::function<void(ProgressEvent&)>;
57 
58     void DefaultProgressCallback(ProgressEvent &event);
59 
60     struct EncryptParams;
61 
62     // Secret parameters for encryption and decryption
63     struct EncryptParams
64     {
65         // Passphrase for decryption
66         // It needs to be a passphrase because we don't know the salt yet. We'll read it from the file.
67         // If passphrase is nullptr, then we'll try to find the key in key_service by salt.
68         const std::string *passphrase;
69 
70         KeyService *key_service;
71 
72         // Encryption parameters to decrypt the key file if it is encrypted
73         // If this EncryptParams is for the key file, this field should be null because the key file is never encrypted
74         // with another key file.
75         EncryptParams *key_file_encrypt_params;
76 
77         // Path to libcurl executable, which is used to download the key file from a remote location such as SSH
78         const std::string *libcurl_path;
79         const std::string *libcurl_parameters;
80         size_t memory_buffer;
81         ProgressCallback progress_callback;
82 
EncryptParamsEncryptParams83         EncryptParams():
84             passphrase(nullptr),
85             key_service(nullptr),
86             key_file_encrypt_params(nullptr),
87             libcurl_path(nullptr),
88             libcurl_parameters(nullptr),
89             memory_buffer(kDefaultMemoryBuffer),
90             progress_callback(ProgressCallback(DefaultProgressCallback))
91         {}
92     };
93 
94     // Packets RFC 4880
95     // Encrypted Message = {packets below}
96     // Symmetric Key Encrypted Session Key Packet = {}, Symmetrically Encrypted Integrity Protected Data Packet = {packets below}
97     // Compressed Data Packet = {packets below}, Modification Detection Code Packet (SHA-1 hash function against the data and the prefix replacing iv)
98     // Literal Data Packet (see page 46 RFC 4880)
99 
100     // 4 bytes' date representation. Not decided yet what it's going to be.
101     typedef unsigned int FileDate;
102 
103     // This information is not secret. It will be saved into a file unencrypted
104     struct PacketMetadata
105     {
106         unsigned int iterations;
107         EncryptMsg::Compression compression;
108         EncryptMsg::CipherAlgo cipher_algo;
109         EncryptMsg::HashAlgo hash_algo;
110         std::string file_name;
111         FileDate file_date;
112         bool is_binary;
113         bool is_armor;
114         bool cannot_use_wad;
115         EncryptMsg::Salt salt;
116 
117         // Key file
118         std::string key_file;
119         bool key_only;
120         bool persist_key_path;
121 
PacketMetadataPacketMetadata122         PacketMetadata()
123             :iterations(0), compression(EncryptMsg::Compression::Unknown),
124             cipher_algo(EncryptMsg::CipherAlgo::Unknown), hash_algo(EncryptMsg::HashAlgo::Unknown),
125             file_name(), file_date(0), is_binary(false), is_armor(false), cannot_use_wad(false),
126             salt(), key_file(), key_only(false), persist_key_path(false)
127         {
128         }
129     };
130 }
131