forked from calccrypto/OpenPGP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.cpp
284 lines (239 loc) · 10.7 KB
/
decrypt.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
#include "decrypt.h"
Tag5::Ptr find_decrypting_key(const PGPSecretKey & k, const std::string & keyid){
for(Packet::Ptr const & p : k.get_packets()){
if ((p -> get_tag() == 5) || (p -> get_tag() == 7)){
std::string raw = p -> raw();
Tag5::Ptr key = std::make_shared<Tag5>(raw);
if (key -> get_public_ptr() -> get_keyid() != keyid ){
key.reset();
continue;
}
// make sure key has encrypting keys
if ((key -> get_pka() == 1) || // RSA
(key -> get_pka() == 2) || // RSA
(key -> get_pka() == 16)){ // ElGamal
return key;
}
key.reset();
}
}
return nullptr;
}
std::string pka_decrypt(const uint8_t pka, std::vector <PGPMPI> & data, const std::vector <PGPMPI> & pri, const std::vector <PGPMPI> & pub){
if (pka < 3){ // RSA
return mpitoraw(RSA_decrypt(data[0], pri, pub));
}
if (pka == 16){ // ElGamal
return ElGamal_decrypt(data, pri, pub);
}
else{
std::stringstream s; s << static_cast <unsigned int> (pka);
throw std::runtime_error("Error: PKA number " + s.str() + " not allowed or unknown.");
}
return ""; // should never reach here; mainly just to remove compiler warnings
}
std::vector <PGPMPI> decrypt_secret_key(const Tag5::Ptr & pri, const std::string & passphrase){
std::vector <PGPMPI> out;
S2K::Ptr s2k = pri -> get_s2k();
// calculate key used in encryption algorithm
std::string key = s2k -> run(passphrase, Symmetric_Algorithm_Key_Length.at(Symmetric_Algorithms.at(pri -> get_sym())) >> 3);
// decrypt secret key
std::string secret_key = use_normal_CFB_decrypt(pri -> get_sym(), pri -> get_secret(), key, pri -> get_IV());
// get checksum and remove it from the string
const unsigned int hash_size = (pri -> get_s2k_con() == 254)?20:2;
std::string checksum = secret_key.substr(secret_key.size() - hash_size, hash_size);
secret_key = secret_key.substr(0, secret_key.size() - hash_size);
// calculate and check checksum
if(pri -> get_s2k_con() == 254){
if (use_hash(s2k -> get_hash(), secret_key) != checksum){
throw std::runtime_error("Error: Secret key checksum and calculated checksum do not match.");
}
}
else{ // all other values; **UNTESTED**
uint16_t sum = 0;
for(char & c : secret_key){
sum += static_cast <unsigned char> (c);
}
if (unhexlify(makehex(sum, 4)) != checksum){
if (use_hash(s2k -> get_hash(), secret_key) != checksum){
throw std::runtime_error("Error: Secret key checksum and calculated checksum do not match.");
}
}
}
// extract MPI values
while (secret_key.size()){
out.push_back(read_MPI(secret_key));
}
s2k.reset();
return out;
}
PGPMessage decrypt_data(const uint8_t sym, const PGPMessage & m, const std::string & session_key, const bool writefile, const PGPPublicKey::Ptr & verify){
// currently packet tag being operated on
uint8_t packet;
// get blocksize of symmetric key algorithm
unsigned int BS = Symmetric_Algorithm_Block_Length.at(Symmetric_Algorithms.at(sym)) >> 3;
// Find encrypted data
std::string data = "";
// find start of encrypted data
unsigned int i = 0;
std::vector <Packet::Ptr> packets = m.get_packets();
while ((i < packets.size()) && (packets[i] -> get_tag() != 9) && (packets[i] -> get_tag() != 18)){
i++;
}
// copy initial data to string
if (packets[i] -> get_tag() == 9){
data = packets[i] -> raw();
Tag9 tag9(data);
data = tag9.get_encrypted_data();
packet = 9;
}
else if (packets[i] -> get_tag() == 18){
data = packets[i] -> raw();
Tag18 tag18(data);
data = tag18.get_protected_data();
packet = 18;
}
else{
throw std::runtime_error("Error: No encrypted data found.");
}
// does not work
// // if the packet was a partial start
// if (packets[i] -> get_partial()){
// i++;
// // add the rest of the data
// for(; i < packets.size(); i++){
// std::string raw = packets[i] -> raw();
// Partial::Ptr part = std::make_shared <Partial> (raw);
// data += part -> get_stream();
// // if the current packet is parital end, break after adding data
// if (packets[i] -> get_partial() == 3){
// break;
// }
// }
// }
if (!data.size()){
throw std::runtime_error("Error: No encrypted data packet(s) found.");
}
// decrypt data
data = use_OpenPGP_CFB_decrypt(sym, packet, data, session_key);
// strip extra data
if (packet == 18){ // Symmetrically Encrypted Integrity Protected Data Packet (Tag 18)
std::string checksum = data.substr(data.size() - 20, 20); // get given SHA1 checksum
data = data.substr(0, data.size() - 20); // remove SHA1 checksum
if (use_hash(2, data) != checksum){ // check SHA1 checksum
throw std::runtime_error("Error: Given checksum and calculated checksum do not match.");
}
data = data.substr(0, data.size() - 2); // get rid of \xd3\x14
}
data = data.substr(BS + 2, data.size() - BS - 2); // get rid of prefix
// decompress and parse decrypted data
return PGPMessage(data);
}
std::string decrypt_pka(const PGPSecretKey & pri, const PGPMessage & m, const std::string & passphrase, const bool writefile, const PGPPublicKey::Ptr & verify){
if ((m.get_ASCII_Armor() != 0)/* && (m.get_ASCII_Armor() != 3) && (m.get_ASCII_Armor() != 4)*/){
throw std::runtime_error("Error: No encrypted message found.");
}
if (pri.get_ASCII_Armor() != 2){
throw std::runtime_error("Error: No Private Key found.");
}
// reused variables
uint8_t packet; // currently used packet tag
std::string data; // temp stuff
std::string session_key; // session key
uint8_t sym; // symmetric key algorithm used to encrypt original data
// find session key packet; should be first packet
for(Packet::Ptr const & p : m.get_packets()){
if ((p -> get_tag() == 1) || (p -> get_tag() == 3)){
data = p -> raw();
packet = p -> get_tag();
break;
}
}
if (packet == 1){}
// return symmetrically-encrypted-key decrypted data
else if (packet == 3){
return decrypt_sym(m, passphrase);
}
else{
std::stringstream s; s << Packet_Tags.at(packet) << " (Tag " << static_cast <unsigned int> (packet) << ").";
throw std::runtime_error("Error: Expected Public-Key Encrypted Session Key Packet (Tag 1). Instead got " + s.str());
}
// Public-Key Encrypted Session Key Packet (Tag 1)
Tag1 tag1(data);
uint8_t pka = tag1.get_pka();
std::vector <PGPMPI> session_key_mpi = tag1.get_mpi();
// find corresponding secret key
Tag5::Ptr sec = find_decrypting_key(pri, tag1.get_keyid());
if (!sec){
throw std::runtime_error("Error: Correct Private Key not found.");
}
std::vector <PGPMPI> pub_mpi = sec -> get_mpi();
std::vector <PGPMPI> pri_mpi = decrypt_secret_key(sec, passphrase);
// get session key
session_key = zero + pka_decrypt(pka, session_key_mpi, pri_mpi, pub_mpi); // symmetric algorithm, session key, 2 octet checksum wrapped in EME_PKCS1_ENCODE
session_key = EME_PKCS1v1_5_DECODE(session_key); // remove EME_PKCS1 encoding
sym = session_key[0]; // get symmetric algorithm
std::string checksum = session_key.substr(session_key.size() - 2, 2); // get 2 octet checksum
session_key = session_key.substr(1, session_key.size() - 3); // remove both from session key
uint16_t sum = 0;
for(char & c : session_key){ // calculate session key checksum
sum += static_cast <uint8_t> (c);
}
if (unhexlify(makehex(sum, 4)) != checksum){ // check session key checksums
throw std::runtime_error("Error: Calculated session key checksum does not match given checksum.");
}
sec.reset();
// decrypt the data with the extracted key
PGPMessage decrypted = decrypt_data(sym, m, session_key, writefile, verify);
std::string out = "";
// if signing key provided, check the signature
if (verify){
out = "Message was" + std::string(verify_message(*verify, decrypted)?"":" not") + " signed by key " + hexlify(verify -> keyid()) + ".\n";
}
// extract data
for(Packet::Ptr const & p : decrypted.get_packets()){
if (p -> get_tag() == 11){
std::string raw = p -> raw();
Tag11 tag11(raw);
out += tag11.out(writefile);
}
}
return out;
}
std::string decrypt_sym(const PGPMessage & m, const std::string & passphrase, const bool writefile, const PGPPublicKey::Ptr & verify){
std::cerr << "Warning: decrypt_sym is untested. Potentially incorrect" << std::endl;
if ((m.get_ASCII_Armor() != 0)/* && (m.get_ASCII_Armor() != 3) && (m.get_ASCII_Armor() != 4)*/){
throw std::runtime_error("Error: No encrypted message found.");
}
uint8_t packet; // currently used packet tag
std::string data; // temp stuff
// find session key packet; should be first packet
for(Packet::Ptr const & p : m.get_packets()){
if ((p -> get_tag() == 1) || (p -> get_tag() == 3)){
data = p -> raw();
packet = p -> get_tag();
break;
}
}
if (packet == 1){
throw std::runtime_error("Error: Use decrypt_pka to decrypt this data.");
}
else if (packet == 3){}
else{
std::stringstream s; s << Packet_Tags.at(packet) << " (Tag " << static_cast <unsigned int> (packet) << ").";
throw std::runtime_error("Error: Expected Symmetric-Key Encrypted Session Key Packet (Tag 3). Instead got " + s.str());
}
Tag3 tag3(data);
data = tag3.get_key(passphrase);
PGPMessage decrypted = decrypt_data(data[0], m, data.substr(1, data.size() - 1), writefile, nullptr);
std::string out = "";
// extract data
for(Packet::Ptr const & p : decrypted.get_packets()){
if (p -> get_tag() == 11){
std::string raw = p -> raw();
Tag11 tag11(raw);
out += tag11.out(writefile);
}
}
return out;
}