-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgp.c
246 lines (209 loc) · 6.5 KB
/
pgp.c
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
#include <gcrypt.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "pgp.h"
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
#define B7 0x80
#define B6 0x40
#define B5 0x20
#define B4 0x10
#define B3 0x08
#define B2 0x04
#define B1 0x02
#define B0 0x01
#define NEW_TAG(head) head & ~(B7 | B6)
#define OLD_TAG(head) (head & ~(B7 | B6)) >> 2
#define LENGTH_TYPE(head) (head & (B0 | B1))
#define SYM_KEY_ENC_KEY 3
#define SYM_KEY_ENC_DATA 9
#define OBSOLETE_LITERAL 10
#define LITERAL 11
#define SYM_KEY_ENC_MDC_DATA 18
struct sym_algo algos[] = {
{GCRY_CIPHER_NONE, 0, 0, "Plaintext"},
{GCRY_CIPHER_IDEA, 16, 8, "IDEA"},
{GCRY_CIPHER_3DES, 24, 8, "TripleDES"},
{GCRY_CIPHER_CAST5, 16, 8, "CAST5"},
{GCRY_CIPHER_BLOWFISH, 16, 8, "Blowfish"},
{GCRY_CIPHER_SAFER_SK128, 16, 8, "Reserved"},
{GCRY_CIPHER_DES_SK, 0, 0, "Reserved"},
{GCRY_CIPHER_AES128, 16, 16, "AES 128"},
{GCRY_CIPHER_AES192, 24, 16, "AES 192"},
{GCRY_CIPHER_AES256, 32, 16, "AES 256"},
{GCRY_CIPHER_TWOFISH, 32, 16, "Twofish"},
};
struct hash_algo halgos[] = {
{GCRY_MD_NONE, "None"},
{GCRY_MD_MD5, "MD5"},
{GCRY_MD_SHA1, "SHA1"},
{GCRY_MD_RMD160, "RIPEMD160"},
{-1, "Reserved"},
{-1, "Reserved"},
{-1, "Reserved"},
{-1, "Reserved"},
{GCRY_MD_SHA256, "SHA256"},
{GCRY_MD_SHA384, "SHA384"},
{GCRY_MD_SHA512, "SHA512"},
{GCRY_MD_SHA224, "SHA224"},
};
ssize_t get_new_length(FILE *pgp) {
uint32_t len = 0;
uint32_t l2;
if (fread(&len, 1, 1, pgp) != 1) {
perror("Error reading old packet length from pgp file ");
return -1;
}
debug("First length byte is %u.\n", len);
if (len >= 192 && len < 224) {
fread(&l2, 1, 1, pgp);
len = ((len - 192) << 8 ) + l2 + 192;
} else if (len >= 224 && len < 255) {
// We don't care about the whole packet, the first part is enough for
// our needs. (I hope.)
len = 1 << (len & 0x1f);
} else if (len == 255) {
fread(&l2, 4, 1, pgp);
len = l2;
}
return len;
}
ssize_t get_old_length(uint8_t head, FILE *pgp) {
uint32_t len = 0;
if (LENGTH_TYPE(head) == 3) {
fprintf(stderr, "Error, unsupported indeterminate length in pgp packet\n");
return -1;
}
if (fread(&len, 1 << LENGTH_TYPE(head), 1, pgp) != 1) {
perror("Error reading old packet length from pgp file ");
return -1;
}
return len;
}
int get_tag_and_length(uint8_t head, FILE *pgp, uint8_t *tag, uint32_t *length) {
if (head & B6) {
*tag = NEW_TAG(head);
if (((*length) = get_new_length(pgp)) == -1) {
return -1;
}
} else {
*tag = OLD_TAG(head);
if (((*length) = get_old_length(head, pgp)) == -1) {
return -1;
}
}
return 0;
}
int parse_packets(FILE *pgp, uint8_t head, struct pgp_data *data) {
uint8_t tag = 0;
uint8_t sym_version = 0;
uint32_t length = 0;
if (get_tag_and_length(head, pgp, &tag, &length) == -1) {
return -1;
}
switch (tag) {
case OBSOLETE_LITERAL:
case LITERAL:
debug("Literal data packet of 0x%x bytes, skipping.\n", length);
fseek(pgp, length, SEEK_CUR);
break;
case SYM_KEY_ENC_KEY:
debug("Symmetrically encrypted key packet, reading 0x%x bytes, %lu, %lu\n",
length, sizeof(struct s2k), sizeof(struct sym_enc_key));
if (fread(&data->key_fmt, sizeof(struct sym_enc_key), 1, pgp) != 1) {
perror("Error reading key spec from pgp file ");
return -1;
} else if (data->key_fmt.version != 4) {
fprintf(stderr, "Error, unsupported symmetric key format version : %hhu",
data->key_fmt.version);
return -1;
} else if (data->key_fmt.s2k_type != 3) {
fprintf(stderr, "Error, only iterated and salted s2k are supported\n");
return -1;
}
length -= sizeof(struct sym_enc_key);
if (length < 0) {
fprintf(stderr,
"Error, symmetrically encrypted key packet is bigger than packet length.\n");
return -1;
}
if (data->key_fmt.algorithm == 5 || data->key_fmt.algorithm == 6) {
fprintf(stderr, "Error, unsupported reserved cipher algorithm number : %hhu.\n",
data->key_fmt.algorithm);
return -1;
}
if (fread(&data->s2k_fmt, sizeof(struct s2k), 1, pgp) != 1) {
perror("Error reading s2k spec from pgp file ");
return -1;
}
length -= sizeof(struct s2k);
if (length != 0) {
fprintf(stderr, "Error, invalid symmetrically encrypted key packet size.\n");
return -1;
}
if (data->s2k_fmt.algorithm == 0 || data->s2k_fmt.algorithm == 4 ||
data->s2k_fmt.algorithm == 5 || data->s2k_fmt.algorithm == 6 ||
data->s2k_fmt.algorithm == 7) {
fprintf(stderr, "Error, unsupported reserved hash algorithm number : %hhu.\n",
data->key_fmt.algorithm);
return -1;
}
data->s2k_count = 16 + (data->s2k_fmt.count & 15);
data->s2k_count = data->s2k_count << ((data->s2k_fmt.count >> 4) + 6);
break;
case SYM_KEY_ENC_DATA:
case SYM_KEY_ENC_MDC_DATA:
debug("Symmetrically encrypted data packet. Reading 0x%x bytes.\n", length);
if (length > ENC_BUFFER_SIZE) {
length = ENC_BUFFER_SIZE;
}
if (tag == SYM_KEY_ENC_MDC_DATA) {
if (fread(&sym_version, 1, 1, pgp) != 1) {
fprintf(stderr, "Error, unexpected end of pgp file before symmetric version tag.\n");
return -1;
}
length--;
if (sym_version != 1) {
fprintf(stderr, "Error, unsupported symmetric data packet version : %hhu.\n",
sym_version);
return -1;
}
}
if (fread(&data->enc_data, length, 1, pgp) != 1) {
fprintf(stderr, "Error, unexpected end of pgp file before end of data packet.\n");
return -1;
}
return 1; // We shouldn't need any more than that to bruteforce.
default:
fprintf(stderr, "Error, unsupported tag %u\n", tag);
return -1;
}
return 0;
}
int parse_pgp(FILE *pgp, struct pgp_data *data) {
memset(&data->key_fmt, 0, sizeof(struct sym_enc_key));
memset(&data->s2k_fmt, 0, sizeof(struct s2k));
while (1) {
uint8_t head = 0x0;
if (fread(&head, 1, 1, pgp) != 1) {
perror("Error reading from pgp file ");
return -1;
}
debug("header byte : %x\n", head);
if ((head & B7) == 0) {
fprintf(stderr, "Error, not an OpenPGP file\n");
return -1;
}
switch(parse_packets(pgp, head, data)) {
case -1:
return -1;
case 1:
return 0;
}
}
return 0;
}