forked from calccrypto/OpenPGP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.cpp
353 lines (308 loc) · 10.1 KB
/
Message.cpp
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
#include "Message.h"
namespace OpenPGP {
// OpenPGP Message :- Encrypted Message | Signed Message | Compressed Message | Literal Message.
bool Message::OpenMessage(std::list <Token>::iterator it, std::list <Token> &){
if ((*it == ENCRYPTEDMESSAGE) || (*it == SIGNEDMESSAGE) || (*it == COMPRESSEDMESSAGE) || (*it == LITERALMESSAGE)){
*it = OPENMessage;
return true;
}
return false;
}
// Compressed Message :- Compressed Data Packet.
bool Message::CompressedMessage(std::list <Token>::iterator it, std::list <Token> &){
if (*it == CDP){
*it = COMPRESSEDMESSAGE;
return true;
}
return false;
}
// Literal Message :- Literal Data Packet.
bool Message::LiteralMessage(std::list <Token>::iterator it, std::list <Token> &){
if (*it == LDP){
*it = LITERALMESSAGE;
return true;
}
return false;
}
// ESK :- Public-Key Encrypted Session Key Packet | Symmetric-Key Encrypted Session Key Packet.
bool Message::EncryptedSessionKey(std::list <Token>::iterator it, std::list <Token> &){
if ((*it == PKESKP) || (*it == SKESKP)){
*it = ESK;
return true;
}
return false;
}
// ESK Sequence :- ESK | ESK Sequence, ESK.
bool Message::ESKSequence(std::list <Token>::iterator it, std::list <Token> & s){
if (*it == ESK){
*it = ESKSEQUENCE;
return true;
}
else if (*it == ESKSEQUENCE){
std::list <Token>::iterator it2 = it; it2++;
if (*it2 == ESK){
s.erase(it2);
*it = ESKSEQUENCE;
return true;
}
}
return false;
}
// Encrypted Data :- Symmetrically Encrypted Data Packet | Symmetrically Encrypted Integrity Protected Data Packet
bool Message::EncryptedData(std::list <Token>::iterator it, std::list <Token> &){
if ((*it == SEDP) || (*it == SEIPDP)){
*it = ENCRYPTEDDATA;
return true;
}
return false;
}
// Encrypted Message :- Encrypted Data | ESK Sequence, Encrypted Data.
bool Message::EncryptedMessage(std::list <Token>::iterator it, std::list <Token> & s){
if (*it == ENCRYPTEDDATA){
*it = ENCRYPTEDMESSAGE;
return true;
}
else if (*it == ESKSEQUENCE){
std::list <Token>::iterator it2 = it; it2++;
if (*it2 == ENCRYPTEDDATA){
*it = ENCRYPTEDMESSAGE;
s.erase(it2);
return true;
}
}
return false;
}
// One-Pass Signed Message :- One-Pass Signature Packet, OpenPGP Message, Corresponding Signature Packet.
bool Message::OnePassSignedMessage(std::list <Token>::iterator it, std::list <Token> & s){
std::list <Token>::iterator it2 = it; it2++;
std::list <Token>::iterator it3 = it2; it3++;
if ((*it == OPSP) && (*it2 == OPENMessage) && (*it3 == SP)){
*it = ONEPASSSIGNEDMESSAGE;
s.erase(it2);
s.erase(it3);
return true;
}
return false;
}
// Signed Message :- Signature Packet, OpenPGP Message | One-Pass Signed Message.
bool Message::SignedMessage(std::list <Token>::iterator it, std::list <Token> & s){
if (*it == ONEPASSSIGNEDMESSAGE){
*it = SIGNEDMESSAGE;
return true;
}
else if (*it == SP){
std::list <Token>::iterator it2 = it; it2++;
if (*it2 == OPENMessage){
*it = SIGNEDMESSAGE;
s.erase(it2);
return true;
}
}
return false;
}
bool Message::decompress() {
comp.reset();
// check if compressed
if ((packets.size() == 1) && (packets[0] -> get_tag() == Packet::COMPRESSED_DATA)){
comp = std::static_pointer_cast <Packet::Tag8> (packets[0]);
const std::string compressed = comp -> get_data();
comp -> set_data("");
comp -> set_partial(packets[0] -> get_partial());
packets.clear();
read(compressed);
type = MESSAGE;
// warn if decompressed packet sequence is not meaningful
if (!meaningful()){
// "Error: Decompression failure.\n";
return false;
}
}
return true;
}
Message::Message()
: PGP(),
comp(nullptr)
{
type = MESSAGE;
}
Message::Message(const PGP & copy)
: PGP(copy),
comp(nullptr)
{
type = MESSAGE;
if (!decompress()){
throw std::runtime_error("Error: Failed to decompress data");
}
}
Message::Message(const Message & copy)
: PGP(copy),
comp(copy.comp)
{
if (comp){
comp = std::static_pointer_cast <Packet::Tag8> (comp);
}
}
Message::Message(const std::string & data)
: PGP(data),
comp(nullptr)
{
type = MESSAGE;
// throw if decompressed packet sequence is not meaningful
if (!meaningful()){
throw std::runtime_error("Error: Data does not form a meaningful PGP Message");
}
if (!decompress()){
throw std::runtime_error("Error: Failed to decompress data");
}
}
Message::Message(std::istream & stream)
: PGP(stream),
comp(nullptr)
{
type = MESSAGE;
// throw if packet sequence is not meaningful
if (!meaningful()){
throw std::runtime_error("Error: Data does not form a meaningful PGP Message");
}
if (!decompress()){
throw std::runtime_error("Error: Failed to decompress data");
}
}
Message::~Message(){}
std::string Message::show(const std::size_t indents, const std::size_t indent_size) const{
std::stringstream out;
if (comp){ // if compression was used, add a header
out << comp -> show(indents, indent_size);
}
out << PGP::show(indents + static_cast <bool> (comp), indent_size);
return out.str();
}
std::string Message::raw(const Packet::Tag::Format header) const{
std::string out = PGP::raw(header);
if (comp){ // if compression was used; compress data
comp -> set_data(out);
out = comp -> write(header);
comp -> set_data(""); // hold compressed data for as little time as possible
}
return out;
}
std::string Message::write(const PGP::Armored armor, const Packet::Tag::Format header) const{
std::string packet_string = raw(header);
// put data into a Compressed Data Packet if compression is used
if (comp){
comp -> set_data(packet_string);
packet_string = comp -> write(header);
}
if ((armor == Armored::NO) || // no armor
((armor == Armored::DEFAULT) && !armored)){ // or use stored value, and stored value is no
return packet_string;
}
std::string out = "-----BEGIN PGP MESSAGE-----\n";
for(Armor_Key const & key : keys){
out += key.first + ": " + key.second + "\n";
}
out += "\n";
return out + format_string(ascii2radix64(packet_string), MAX_LINE_LENGTH) + "=" + ascii2radix64(unhexlify(makehex(crc24(packet_string), 6))) + "\n-----END PGP MESSAGE-----\n";
}
uint8_t Message::get_comp() const{
if (comp){
return comp -> get_comp();
}
return Compression::ID::UNCOMPRESSED;
}
void Message::set_comp(const uint8_t c){
comp.reset(); // free comp / set it to nullptr
if (c){ // if not uncompressed
comp = std::make_shared <Packet::Tag8> ();
comp -> set_comp(c);
}
}
bool Message::match(const PGP & pgp, const Message::Token & token){
if (pgp.get_type() != MESSAGE){
// "Error: ASCII Armor type is not MESSAGE.\n";
return false;
}
if (!pgp.get_packets().size()){
// "Error: No packets found.\n";
return false;
}
if ((token != OPENMessage) &&
(token != ENCRYPTEDMESSAGE) &&
(token != SIGNEDMESSAGE) &&
// (token != ONEPASSSIGNEDMESSAGE) && // special case of Signed Message
(token != COMPRESSEDMESSAGE) &&
(token != LITERALMESSAGE)){
// "Error: Invalid Token to match.\n";
return false;
}
// get list of packets and convert them to Token
std::list <Token> s;
for(Packet::Tag::Ptr const & p : pgp.get_packets()){
Token push;
if (p -> get_tag() == Packet::COMPRESSED_DATA){
push = CDP;
}
else if (p -> get_tag() == Packet::LITERAL_DATA){
push = LDP;
}
else if (p -> get_tag() == Packet::PUBLIC_KEY_ENCRYPTED_SESSION_KEY){
push = PKESKP;
}
else if (p -> get_tag() == Packet::SYMMETRIC_KEY_ENCRYPTED_SESSION_KEY){
push = SKESKP;
}
else if (p -> get_tag() == Packet::SYMMETRICALLY_ENCRYPTED_DATA){
push = SEDP;
}
else if (p -> get_tag() == Packet::SYM_ENCRYPTED_INTEGRITY_PROTECTED_DATA){
push = SEIPDP;
}
else if (p -> get_tag() == Packet::ONE_PASS_SIGNATURE){
push = OPSP;
}
else if (p -> get_tag() == Packet::SIGNATURE){
push = SP;
}
else{
// "Error: Non-Message packet found.\n";
return false;
}
s.push_back(push);
}
while ((*(s.begin()) != token) || (s.size() != 1)){ // while the sentence has not been fully parsed, or has been fully parse but not correctly
bool reduced = false;
for(std::list <Token>::iterator it = s.begin(); it != s.end(); it++){ // for each token
// make sure the sentence continues to fit at least one of the rules at least once per loop over the sentence
if (OpenMessage (it, s) ||
CompressedMessage (it, s) ||
LiteralMessage (it, s) ||
EncryptedSessionKey (it, s) ||
ESKSequence (it, s) ||
EncryptedData (it, s) ||
EncryptedMessage (it, s) ||
OnePassSignedMessage (it, s) ||
SignedMessage (it, s)){
reduced = true;
break;
}
}
if (!reduced){
// "Error: Failed to reduce tokens.\n";
return false;
}
}
return true;
}
bool Message::match(const Message::Token & token) const{
return match(*this, token);
}
bool Message::meaningful(const PGP & pgp){
return match(pgp, OPENMessage);
}
bool Message::meaningful() const{
return match(OPENMessage);
}
PGP::Ptr Message::clone() const{
return std::make_shared <Message> (*this);
}
}