vb@125
|
1 |
#include "pEp_internal.h"
|
vb@37
|
2 |
#include "message_api.h"
|
vb@37
|
3 |
|
vb@130
|
4 |
#include "platform.h"
|
vb@220
|
5 |
#include "mime.h"
|
vb@85
|
6 |
|
vb@37
|
7 |
#include <assert.h>
|
vb@37
|
8 |
#include <string.h>
|
vb@39
|
9 |
#include <stdlib.h>
|
vb@39
|
10 |
|
vb@190
|
11 |
#ifndef MIN
|
vb@190
|
12 |
#define MIN(A, B) ((B) > (A) ? (A) : (B))
|
vb@190
|
13 |
#endif
|
vb@300
|
14 |
#ifndef MAX
|
vb@300
|
15 |
#define MAX(A, B) ((B) > (A) ? (B) : (A))
|
vb@300
|
16 |
#endif
|
vb@190
|
17 |
|
vb@235
|
18 |
|
vb@235
|
19 |
static bool string_equality(const char *s1, const char *s2)
|
vb@235
|
20 |
{
|
vb@235
|
21 |
if (s1 == NULL || s2 == NULL)
|
vb@235
|
22 |
return false;
|
vb@235
|
23 |
|
vb@235
|
24 |
assert(s1 && s2);
|
vb@235
|
25 |
|
vb@235
|
26 |
return strcmp(s1, s2) == 0;
|
vb@235
|
27 |
}
|
vb@235
|
28 |
|
vb@235
|
29 |
static bool is_mime_type(const bloblist_t *bl, const char *mt)
|
vb@235
|
30 |
{
|
vb@235
|
31 |
assert(mt);
|
vb@235
|
32 |
|
vb@235
|
33 |
return bl && string_equality(bl->mime_type, mt);
|
vb@235
|
34 |
}
|
vb@235
|
35 |
|
vb@235
|
36 |
static bool is_fileending(const bloblist_t *bl, const char *fe)
|
vb@235
|
37 |
{
|
vb@235
|
38 |
assert(fe);
|
vb@235
|
39 |
|
vb@235
|
40 |
if (bl == NULL || bl->filename == NULL)
|
vb@235
|
41 |
return false;
|
vb@235
|
42 |
|
vb@235
|
43 |
assert(bl && bl->filename);
|
vb@235
|
44 |
|
vb@235
|
45 |
size_t fe_len = strlen(fe);
|
vb@235
|
46 |
size_t fn_len = strlen(bl->filename);
|
vb@235
|
47 |
|
vb@235
|
48 |
if (fn_len <= fe_len)
|
vb@235
|
49 |
return false;
|
vb@235
|
50 |
|
vb@235
|
51 |
assert(fn_len > fe_len);
|
vb@235
|
52 |
|
vb@235
|
53 |
return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
|
vb@235
|
54 |
}
|
vb@235
|
55 |
|
vb@284
|
56 |
static void add_opt_field(message *msg, const char *name, const char *value)
|
vb@284
|
57 |
{
|
vb@284
|
58 |
assert(msg);
|
vb@284
|
59 |
|
vb@284
|
60 |
if (msg && name && value) {
|
vb@284
|
61 |
stringpair_t *pair = new_stringpair(name, value);
|
vb@284
|
62 |
if (pair == NULL)
|
vb@284
|
63 |
return;
|
vb@284
|
64 |
|
vb@284
|
65 |
stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
|
vb@284
|
66 |
if (field == NULL)
|
vb@284
|
67 |
return;
|
vb@284
|
68 |
|
vb@284
|
69 |
if (msg->opt_fields == NULL)
|
vb@284
|
70 |
msg->opt_fields = field;
|
vb@284
|
71 |
}
|
vb@284
|
72 |
}
|
vb@284
|
73 |
|
vb@83
|
74 |
static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
|
vb@62
|
75 |
{
|
vb@62
|
76 |
char * ptext;
|
vb@81
|
77 |
|
vb@83
|
78 |
assert(shortmsg);
|
vb@83
|
79 |
assert(strcmp(shortmsg, "pEp") != 0);
|
vb@62
|
80 |
|
vb@83
|
81 |
if (longmsg == NULL)
|
vb@63
|
82 |
longmsg = "";
|
vb@63
|
83 |
|
vb@83
|
84 |
ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
|
vb@109
|
85 |
assert(ptext);
|
vb@62
|
86 |
if (ptext == NULL)
|
vb@62
|
87 |
return NULL;
|
vb@62
|
88 |
|
vb@85
|
89 |
strcpy(ptext, "Subject: ");
|
vb@83
|
90 |
strcat(ptext, shortmsg);
|
vb@62
|
91 |
strcat(ptext, "\n\n");
|
vb@63
|
92 |
strcat(ptext, longmsg);
|
vb@62
|
93 |
|
vb@62
|
94 |
return ptext;
|
vb@62
|
95 |
}
|
vb@44
|
96 |
|
vb@82
|
97 |
static int seperate_short_and_long(const char *src, char **shortmsg, char **longmsg)
|
vb@82
|
98 |
{
|
vb@82
|
99 |
char *_shortmsg = NULL;
|
vb@82
|
100 |
char *_longmsg = NULL;
|
vb@82
|
101 |
|
vb@82
|
102 |
assert(src);
|
vb@82
|
103 |
assert(shortmsg);
|
vb@82
|
104 |
assert(longmsg);
|
vb@82
|
105 |
|
vb@82
|
106 |
*shortmsg = NULL;
|
vb@82
|
107 |
*longmsg = NULL;
|
vb@82
|
108 |
|
vb@85
|
109 |
if (strncasecmp(src, "subject: ", 9) == 0) {
|
vb@82
|
110 |
char *line_end = strchr(src, '\n');
|
vb@311
|
111 |
|
vb@82
|
112 |
if (line_end == NULL) {
|
vb@82
|
113 |
_shortmsg = strdup(src + 9);
|
vb@82
|
114 |
if (_shortmsg == NULL)
|
vb@82
|
115 |
goto enomem;
|
vb@82
|
116 |
// _longmsg = NULL;
|
vb@82
|
117 |
}
|
vb@82
|
118 |
else {
|
vb@82
|
119 |
size_t n = line_end - src;
|
vb@166
|
120 |
|
vb@82
|
121 |
if (*(line_end - 1) == '\r')
|
vb@166
|
122 |
_shortmsg = strndup(src + 9, n - 10);
|
vb@82
|
123 |
else
|
vb@166
|
124 |
_shortmsg = strndup(src + 9, n - 9);
|
vb@166
|
125 |
|
vb@82
|
126 |
if (_shortmsg == NULL)
|
vb@82
|
127 |
goto enomem;
|
vb@166
|
128 |
|
vb@166
|
129 |
while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
|
vb@166
|
130 |
++n;
|
vb@166
|
131 |
|
vb@166
|
132 |
if (*(src + n)) {
|
vb@166
|
133 |
_longmsg = strdup(src + n);
|
vb@166
|
134 |
if (_longmsg == NULL)
|
vb@166
|
135 |
goto enomem;
|
vb@166
|
136 |
}
|
vb@82
|
137 |
}
|
vb@82
|
138 |
}
|
vb@82
|
139 |
else {
|
vb@113
|
140 |
_shortmsg = strdup("");
|
vb@82
|
141 |
if (_shortmsg == NULL)
|
vb@82
|
142 |
goto enomem;
|
vb@82
|
143 |
_longmsg = strdup(src);
|
vb@82
|
144 |
if (_longmsg == NULL)
|
vb@82
|
145 |
goto enomem;
|
vb@82
|
146 |
}
|
vb@311
|
147 |
|
vb@82
|
148 |
*shortmsg = _shortmsg;
|
vb@82
|
149 |
*longmsg = _longmsg;
|
vb@82
|
150 |
|
vb@82
|
151 |
return 0;
|
vb@82
|
152 |
|
vb@82
|
153 |
enomem:
|
vb@82
|
154 |
free(_shortmsg);
|
vb@82
|
155 |
free(_longmsg);
|
vb@82
|
156 |
|
vb@82
|
157 |
return -1;
|
vb@82
|
158 |
}
|
vb@82
|
159 |
|
vb@113
|
160 |
static PEP_STATUS copy_fields(message *dst, const message *src)
|
vb@113
|
161 |
{
|
vb@164
|
162 |
assert(dst);
|
vb@164
|
163 |
assert(src);
|
vb@164
|
164 |
|
vb@113
|
165 |
free_timestamp(dst->sent);
|
vb@113
|
166 |
dst->sent = NULL;
|
vb@113
|
167 |
if (src->sent) {
|
vb@113
|
168 |
dst->sent = timestamp_dup(src->sent);
|
vb@113
|
169 |
if (dst->sent == NULL)
|
vb@113
|
170 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
171 |
}
|
vb@113
|
172 |
|
vb@113
|
173 |
free_timestamp(dst->recv);
|
vb@113
|
174 |
dst->recv = NULL;
|
vb@113
|
175 |
if (src->recv) {
|
vb@113
|
176 |
dst->recv = timestamp_dup(src->recv);
|
vb@113
|
177 |
if (dst->recv == NULL)
|
vb@113
|
178 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
179 |
}
|
vb@113
|
180 |
|
vb@113
|
181 |
free_identity(dst->from);
|
vb@113
|
182 |
dst->from = NULL;
|
vb@113
|
183 |
if (src->from) {
|
vb@113
|
184 |
dst->from = identity_dup(src->from);
|
vb@113
|
185 |
if (dst->from == NULL)
|
vb@113
|
186 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
187 |
}
|
vb@113
|
188 |
|
vb@113
|
189 |
free_identity_list(dst->to);
|
vb@113
|
190 |
dst->to = NULL;
|
vb@274
|
191 |
if (src->to && src->to->ident) {
|
vb@113
|
192 |
dst->to = identity_list_dup(src->to);
|
vb@113
|
193 |
if (dst->to == NULL)
|
vb@113
|
194 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
195 |
}
|
vb@113
|
196 |
|
vb@113
|
197 |
free_identity(dst->recv_by);
|
vb@113
|
198 |
dst->recv_by = NULL;
|
vb@113
|
199 |
if (src->recv_by) {
|
vb@113
|
200 |
dst->recv_by = identity_dup(src->recv_by);
|
vb@113
|
201 |
if (dst->recv_by == NULL)
|
vb@113
|
202 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
203 |
}
|
vb@113
|
204 |
|
vb@113
|
205 |
free_identity_list(dst->cc);
|
vb@113
|
206 |
dst->cc = NULL;
|
vb@274
|
207 |
if (src->cc && src->cc->ident) {
|
vb@113
|
208 |
dst->cc = identity_list_dup(src->cc);
|
vb@113
|
209 |
if (dst->cc == NULL)
|
vb@113
|
210 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
211 |
}
|
vb@113
|
212 |
|
vb@113
|
213 |
free_identity_list(dst->bcc);
|
vb@113
|
214 |
dst->bcc = NULL;
|
vb@274
|
215 |
if (src->bcc && src->bcc->ident) {
|
vb@113
|
216 |
dst->bcc = identity_list_dup(src->bcc);
|
vb@113
|
217 |
if (dst->bcc == NULL)
|
vb@113
|
218 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
219 |
}
|
vb@113
|
220 |
|
vb@113
|
221 |
free_identity_list(dst->reply_to);
|
vb@113
|
222 |
dst->reply_to = NULL;
|
vb@274
|
223 |
if (src->reply_to && src->reply_to->ident) {
|
vb@113
|
224 |
dst->reply_to = identity_list_dup(src->reply_to);
|
vb@113
|
225 |
if (dst->reply_to == NULL)
|
vb@113
|
226 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
227 |
}
|
vb@113
|
228 |
|
vb@113
|
229 |
free_stringlist(dst->in_reply_to);
|
vb@113
|
230 |
dst->in_reply_to = NULL;
|
vb@274
|
231 |
if (src->in_reply_to && src->in_reply_to->value) {
|
vb@113
|
232 |
dst->in_reply_to = stringlist_dup(src->in_reply_to);
|
vb@113
|
233 |
if (dst->in_reply_to == NULL)
|
vb@113
|
234 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
235 |
}
|
vb@113
|
236 |
|
vb@113
|
237 |
free_stringlist(dst->references);
|
vb@113
|
238 |
dst->references = NULL;
|
vb@113
|
239 |
if (src->references) {
|
vb@113
|
240 |
dst->references = stringlist_dup(src->references);
|
vb@113
|
241 |
if (dst->references == NULL)
|
vb@113
|
242 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
243 |
}
|
vb@113
|
244 |
|
vb@113
|
245 |
free_stringlist(dst->keywords);
|
vb@113
|
246 |
dst->keywords = NULL;
|
vb@274
|
247 |
if (src->keywords && src->keywords->value) {
|
vb@113
|
248 |
dst->keywords = stringlist_dup(src->keywords);
|
vb@113
|
249 |
if (dst->keywords == NULL)
|
vb@113
|
250 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
251 |
}
|
vb@113
|
252 |
|
vb@113
|
253 |
free(dst->comments);
|
vb@113
|
254 |
dst->comments = NULL;
|
vb@113
|
255 |
if (src->comments) {
|
vb@113
|
256 |
dst->comments = strdup(src->comments);
|
vb@113
|
257 |
assert(dst->comments);
|
vb@113
|
258 |
if (dst->comments == NULL)
|
vb@113
|
259 |
return PEP_OUT_OF_MEMORY;
|
vb@113
|
260 |
}
|
vb@113
|
261 |
|
vb@113
|
262 |
return PEP_STATUS_OK;
|
vb@113
|
263 |
}
|
vb@113
|
264 |
|
vb@81
|
265 |
static message * clone_to_empty_message(const message * src)
|
vb@80
|
266 |
{
|
vb@113
|
267 |
PEP_STATUS status;
|
vb@80
|
268 |
message * msg = NULL;
|
vb@80
|
269 |
|
vb@81
|
270 |
assert(src);
|
vb@81
|
271 |
|
vb@113
|
272 |
msg = calloc(1, sizeof(message));
|
vb@113
|
273 |
assert(msg);
|
vb@80
|
274 |
if (msg == NULL)
|
vb@80
|
275 |
goto enomem;
|
vb@80
|
276 |
|
vb@82
|
277 |
msg->dir = src->dir;
|
vb@82
|
278 |
|
vb@113
|
279 |
status = copy_fields(msg, src);
|
vb@113
|
280 |
if (status != PEP_STATUS_OK)
|
vb@113
|
281 |
goto enomem;
|
vb@81
|
282 |
|
vb@80
|
283 |
return msg;
|
vb@80
|
284 |
|
vb@80
|
285 |
enomem:
|
vb@113
|
286 |
free_message(msg);
|
vb@80
|
287 |
return NULL;
|
vb@80
|
288 |
}
|
vb@80
|
289 |
|
vb@260
|
290 |
static PEP_STATUS encrypt_PGP_MIME(
|
vb@311
|
291 |
PEP_SESSION session,
|
vb@311
|
292 |
const message *src,
|
vb@311
|
293 |
stringlist_t *keys,
|
vb@311
|
294 |
message *dst
|
vb@260
|
295 |
)
|
vb@260
|
296 |
{
|
vb@260
|
297 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@260
|
298 |
bool free_ptext = false;
|
vb@260
|
299 |
char *ptext;
|
vb@288
|
300 |
char *ctext;
|
vb@288
|
301 |
char *_ctext = NULL;
|
vb@260
|
302 |
char *mimetext = NULL;
|
vb@260
|
303 |
size_t csize;
|
vb@260
|
304 |
assert(dst->longmsg == NULL);
|
vb@260
|
305 |
dst->enc_format = PEP_enc_PGP_MIME;
|
vb@260
|
306 |
|
vb@260
|
307 |
if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
|
vb@260
|
308 |
ptext = combine_short_and_long(src->shortmsg, src->longmsg);
|
vb@260
|
309 |
if (ptext == NULL)
|
vb@260
|
310 |
goto enomem;
|
vb@260
|
311 |
free_ptext = true;
|
vb@260
|
312 |
}
|
vb@260
|
313 |
else if (src->longmsg) {
|
vb@260
|
314 |
ptext = src->longmsg;
|
vb@260
|
315 |
}
|
vb@260
|
316 |
else {
|
vb@260
|
317 |
ptext = "pEp";
|
vb@260
|
318 |
}
|
vb@260
|
319 |
|
vb@260
|
320 |
message *_src = calloc(1, sizeof(message));
|
vb@260
|
321 |
assert(_src);
|
vb@260
|
322 |
if (_src == NULL)
|
vb@260
|
323 |
goto enomem;
|
vb@260
|
324 |
_src->longmsg = ptext;
|
vb@260
|
325 |
_src->longmsg_formatted = src->longmsg_formatted;
|
vb@260
|
326 |
_src->attachments = src->attachments;
|
vb@260
|
327 |
_src->enc_format = PEP_enc_none;
|
vb@260
|
328 |
status = mime_encode_message(_src, true, &mimetext);
|
vb@260
|
329 |
assert(status == PEP_STATUS_OK);
|
vb@260
|
330 |
if (free_ptext)
|
vb@260
|
331 |
free(ptext);
|
vb@260
|
332 |
free(_src);
|
vb@260
|
333 |
assert(mimetext);
|
vb@260
|
334 |
if (mimetext == NULL)
|
vb@260
|
335 |
goto pep_error;
|
vb@260
|
336 |
|
vb@260
|
337 |
status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
|
vb@311
|
338 |
&ctext, &csize);
|
vb@260
|
339 |
free(mimetext);
|
vb@260
|
340 |
if (ctext == NULL)
|
vb@260
|
341 |
goto pep_error;
|
vb@260
|
342 |
|
vb@260
|
343 |
dst->longmsg = strdup("this message was encrypted with p≡p "
|
vb@311
|
344 |
"http://pEp-project.org");
|
vb@260
|
345 |
if (dst->longmsg == NULL)
|
vb@260
|
346 |
goto enomem;
|
vb@260
|
347 |
|
vb@260
|
348 |
char *v = strdup("Version: 1");
|
vb@260
|
349 |
if (v == NULL)
|
vb@260
|
350 |
goto enomem;
|
vb@260
|
351 |
|
vb@260
|
352 |
bloblist_t *_a = new_bloblist(v, 11, "application/pgp-encrypted", NULL);
|
vb@260
|
353 |
if (_a == NULL)
|
vb@260
|
354 |
goto enomem;
|
vb@260
|
355 |
dst->attachments = _a;
|
vb@288
|
356 |
|
vb@288
|
357 |
_ctext = malloc(csize);
|
vb@288
|
358 |
assert(_ctext);
|
vb@288
|
359 |
if (_ctext == NULL)
|
vb@288
|
360 |
goto enomem;
|
vb@288
|
361 |
memcpy(_ctext, ctext, csize);
|
vb@288
|
362 |
|
vb@288
|
363 |
_a = bloblist_add(_a, _ctext, csize, "application/octet-stream",
|
vb@311
|
364 |
"msg.asc");
|
vb@260
|
365 |
if (_a == NULL)
|
vb@260
|
366 |
goto enomem;
|
vb@260
|
367 |
|
vb@260
|
368 |
return PEP_STATUS_OK;
|
vb@260
|
369 |
|
vb@260
|
370 |
enomem:
|
vb@260
|
371 |
status = PEP_OUT_OF_MEMORY;
|
vb@260
|
372 |
|
vb@260
|
373 |
pep_error:
|
vb@260
|
374 |
if (free_ptext)
|
vb@260
|
375 |
free(ptext);
|
vb@288
|
376 |
free(_ctext);
|
vb@260
|
377 |
return status;
|
vb@260
|
378 |
}
|
vb@260
|
379 |
|
vb@260
|
380 |
static PEP_STATUS encrypt_PGP_in_pieces(
|
vb@311
|
381 |
PEP_SESSION session,
|
vb@311
|
382 |
const message *src,
|
vb@311
|
383 |
stringlist_t *keys,
|
vb@311
|
384 |
message *dst
|
vb@260
|
385 |
)
|
vb@260
|
386 |
{
|
vb@260
|
387 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@288
|
388 |
char *ctext;
|
vb@260
|
389 |
size_t csize;
|
vb@260
|
390 |
|
vb@260
|
391 |
assert(dst->longmsg == NULL);
|
vb@260
|
392 |
assert(dst->attachments == NULL);
|
vb@260
|
393 |
|
vb@260
|
394 |
dst->enc_format = PEP_enc_pieces;
|
vb@260
|
395 |
|
vb@320
|
396 |
if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
|
vb@260
|
397 |
char *ptext = combine_short_and_long(src->shortmsg, src->longmsg);
|
vb@260
|
398 |
if (ptext == NULL)
|
vb@260
|
399 |
goto enomem;
|
vb@260
|
400 |
|
vb@260
|
401 |
status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
|
vb@311
|
402 |
&csize);
|
vb@260
|
403 |
free(ptext);
|
vb@288
|
404 |
if (ctext) {
|
vb@288
|
405 |
dst->longmsg = strndup(ctext, csize);
|
vb@288
|
406 |
assert(dst->longmsg);
|
vb@288
|
407 |
if (dst->longmsg == NULL)
|
vb@288
|
408 |
goto enomem;
|
vb@288
|
409 |
}
|
vb@288
|
410 |
else {
|
vb@260
|
411 |
goto pep_error;
|
vb@288
|
412 |
}
|
vb@260
|
413 |
}
|
vb@320
|
414 |
else if (src->longmsg && src->longmsg[0]) {
|
vb@260
|
415 |
char *ptext = src->longmsg;
|
vb@260
|
416 |
status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
|
vb@311
|
417 |
&csize);
|
vb@288
|
418 |
if (ctext) {
|
vb@288
|
419 |
dst->longmsg = strndup(ctext, csize);
|
vb@288
|
420 |
assert(dst->longmsg);
|
vb@288
|
421 |
if (dst->longmsg == NULL)
|
vb@288
|
422 |
goto enomem;
|
vb@288
|
423 |
}
|
vb@288
|
424 |
else {
|
vb@260
|
425 |
goto pep_error;
|
vb@288
|
426 |
}
|
vb@284
|
427 |
}
|
vb@284
|
428 |
else {
|
vb@284
|
429 |
dst->longmsg = strdup("");
|
vb@284
|
430 |
if (dst->longmsg == NULL)
|
vb@284
|
431 |
goto enomem;
|
vb@260
|
432 |
}
|
vb@260
|
433 |
|
vb@320
|
434 |
if (src->longmsg_formatted && src->longmsg_formatted[0]) {
|
vb@260
|
435 |
char *ptext = src->longmsg_formatted;
|
vb@260
|
436 |
status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
|
vb@311
|
437 |
&csize);
|
vb@260
|
438 |
if (ctext) {
|
vb@288
|
439 |
char *_ctext = malloc(csize);
|
vb@288
|
440 |
assert(_ctext);
|
vb@288
|
441 |
if (_ctext == NULL)
|
vb@288
|
442 |
goto enomem;
|
vb@288
|
443 |
memcpy(_ctext, ctext, csize);
|
vb@288
|
444 |
|
vb@288
|
445 |
bloblist_t *_a = bloblist_add(dst->attachments, _ctext, csize,
|
vb@311
|
446 |
"application/octet-stream", "PGPexch.htm.pgp");
|
vb@284
|
447 |
if (_a == NULL)
|
vb@260
|
448 |
goto enomem;
|
vb@284
|
449 |
if (dst->attachments == NULL)
|
vb@284
|
450 |
dst->attachments = _a;
|
vb@260
|
451 |
}
|
vb@260
|
452 |
else {
|
vb@260
|
453 |
goto pep_error;
|
vb@260
|
454 |
}
|
vb@260
|
455 |
}
|
vb@260
|
456 |
|
vb@260
|
457 |
if (src->attachments) {
|
vb@284
|
458 |
if (dst->attachments == NULL) {
|
vb@284
|
459 |
dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
|
vb@284
|
460 |
if (dst->attachments == NULL)
|
vb@284
|
461 |
goto enomem;
|
vb@284
|
462 |
}
|
vb@260
|
463 |
|
vb@284
|
464 |
bloblist_t *_s = src->attachments;
|
vb@284
|
465 |
bloblist_t *_d = dst->attachments;
|
vb@284
|
466 |
|
vb@301
|
467 |
for (int n = 0; _s && _s->value; _s = _s->next) {
|
vb@260
|
468 |
int psize = _s->size;
|
vb@301
|
469 |
char *ptext = _s->value;
|
vb@260
|
470 |
status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
|
vb@311
|
471 |
&csize);
|
vb@260
|
472 |
if (ctext) {
|
vb@285
|
473 |
char *filename = NULL;
|
vb@285
|
474 |
|
vb@285
|
475 |
if (_s->filename) {
|
vb@285
|
476 |
size_t len = strlen(_s->filename);
|
vb@285
|
477 |
filename = calloc(1, len + 5);
|
vb@285
|
478 |
if (filename == NULL)
|
vb@285
|
479 |
goto enomem;
|
vb@285
|
480 |
|
vb@285
|
481 |
strcpy(filename, _s->filename);
|
vb@285
|
482 |
strcpy(filename + len, ".pgp");
|
vb@285
|
483 |
}
|
vb@285
|
484 |
else {
|
vb@285
|
485 |
filename = calloc(1, 20);
|
vb@285
|
486 |
if (filename == NULL)
|
vb@285
|
487 |
goto enomem;
|
vb@285
|
488 |
|
vb@285
|
489 |
++n;
|
vb@285
|
490 |
n &= 0xffff;
|
vb@285
|
491 |
snprintf(filename, 20, "Attachment%d.pgp", n);
|
vb@285
|
492 |
}
|
vb@285
|
493 |
|
vb@288
|
494 |
char *_ctext = malloc(csize);
|
vb@288
|
495 |
assert(_ctext);
|
vb@288
|
496 |
if (_ctext == NULL)
|
vb@288
|
497 |
goto enomem;
|
vb@288
|
498 |
memcpy(_ctext, ctext, csize);
|
vb@288
|
499 |
|
vb@288
|
500 |
_d = bloblist_add(_d, _ctext, csize, "application/octet-stream",
|
vb@311
|
501 |
filename);
|
vb@260
|
502 |
if (_d == NULL)
|
vb@260
|
503 |
goto enomem;
|
vb@260
|
504 |
}
|
vb@260
|
505 |
else {
|
vb@260
|
506 |
goto pep_error;
|
vb@260
|
507 |
}
|
vb@260
|
508 |
}
|
vb@260
|
509 |
}
|
vb@260
|
510 |
|
vb@260
|
511 |
return PEP_STATUS_OK;
|
vb@260
|
512 |
|
vb@260
|
513 |
enomem:
|
vb@260
|
514 |
status = PEP_OUT_OF_MEMORY;
|
vb@260
|
515 |
|
vb@260
|
516 |
pep_error:
|
vb@260
|
517 |
return status;
|
vb@260
|
518 |
}
|
vb@260
|
519 |
|
vb@311
|
520 |
static char * keylist_to_string(const stringlist_t *keylist)
|
vb@311
|
521 |
{
|
vb@311
|
522 |
if (keylist) {
|
vb@311
|
523 |
size_t size = stringlist_length(keylist);
|
vb@311
|
524 |
|
vb@311
|
525 |
const stringlist_t *_kl;
|
vb@311
|
526 |
for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
|
vb@311
|
527 |
size += strlen(_kl->value);
|
vb@311
|
528 |
}
|
vb@311
|
529 |
|
vb@311
|
530 |
char *result = calloc(1, size);
|
vb@311
|
531 |
if (result == NULL)
|
vb@311
|
532 |
return NULL;
|
vb@311
|
533 |
|
vb@311
|
534 |
char *_r = result;
|
vb@311
|
535 |
for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
|
vb@311
|
536 |
_r = stpcpy(_r, _kl->value);
|
vb@311
|
537 |
if (_kl->next && _kl->next->value)
|
vb@311
|
538 |
_r = stpcpy(_r, ",");
|
vb@311
|
539 |
}
|
vb@311
|
540 |
|
vb@311
|
541 |
return result;
|
vb@311
|
542 |
}
|
vb@311
|
543 |
else {
|
vb@311
|
544 |
return NULL;
|
vb@311
|
545 |
}
|
vb@311
|
546 |
}
|
vb@311
|
547 |
|
vb@311
|
548 |
static const char * color_to_string(PEP_color color)
|
vb@311
|
549 |
{
|
vb@311
|
550 |
switch (color) {
|
vb@311
|
551 |
case PEP_rating_cannot_decrypt:
|
vb@311
|
552 |
return "cannot_decrypt";
|
vb@311
|
553 |
case PEP_rating_have_no_key:
|
vb@311
|
554 |
return "have_no_key";
|
vb@311
|
555 |
case PEP_rating_unencrypted:
|
vb@311
|
556 |
return "unencrypted";
|
vb@311
|
557 |
case PEP_rating_unreliable:
|
vb@311
|
558 |
return "unreliable";
|
vb@311
|
559 |
case PEP_rating_reliable:
|
vb@311
|
560 |
return "reliable";
|
vb@311
|
561 |
case PEP_rating_trusted:
|
vb@311
|
562 |
return "trusted";
|
vb@311
|
563 |
case PEP_rating_trusted_and_anonymized:
|
vb@311
|
564 |
return "trusted_and_anonymized";
|
vb@311
|
565 |
case PEP_rating_fully_anonymous:
|
vb@311
|
566 |
return "fully_anonymous";
|
vb@311
|
567 |
case PEP_rating_under_attack:
|
vb@311
|
568 |
return "unter_attack";
|
vb@311
|
569 |
case PEP_rating_b0rken:
|
vb@311
|
570 |
return "b0rken";
|
vb@311
|
571 |
default:
|
vb@311
|
572 |
return "undefined";
|
vb@311
|
573 |
}
|
vb@311
|
574 |
}
|
vb@311
|
575 |
|
vb@311
|
576 |
static void decorate_message(
|
vb@311
|
577 |
message *msg,
|
vb@311
|
578 |
PEP_color color,
|
vb@311
|
579 |
stringlist_t *keylist
|
vb@311
|
580 |
)
|
vb@311
|
581 |
{
|
vb@311
|
582 |
assert(msg);
|
vb@311
|
583 |
|
vb@311
|
584 |
add_opt_field(msg, "X-pEp-Version", "1.0");
|
vb@311
|
585 |
|
vb@311
|
586 |
if (color != PEP_rating_undefined)
|
vb@311
|
587 |
add_opt_field(msg, "X-EncStatus", color_to_string(color));
|
vb@311
|
588 |
|
vb@311
|
589 |
if (keylist) {
|
vb@311
|
590 |
char *_keylist = keylist_to_string(keylist);
|
vb@311
|
591 |
add_opt_field(msg, "X-KeyList", _keylist);
|
vb@311
|
592 |
free(_keylist);
|
vb@311
|
593 |
}
|
vb@311
|
594 |
}
|
vb@311
|
595 |
|
vb@311
|
596 |
static PEP_color _rating(PEP_comm_type ct)
|
vb@311
|
597 |
{
|
vb@311
|
598 |
if (ct == PEP_ct_unknown)
|
vb@311
|
599 |
return PEP_rating_undefined;
|
vb@311
|
600 |
|
vb@311
|
601 |
else if (ct == PEP_ct_compromized)
|
vb@311
|
602 |
return PEP_rating_under_attack;
|
vb@311
|
603 |
|
vb@311
|
604 |
else if (ct >= PEP_ct_confirmed_enc_anon)
|
vb@311
|
605 |
return PEP_rating_trusted_and_anonymized;
|
vb@311
|
606 |
|
vb@311
|
607 |
else if (ct >= PEP_ct_strong_encryption)
|
vb@311
|
608 |
return PEP_rating_trusted;
|
vb@311
|
609 |
|
vb@311
|
610 |
else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
|
vb@311
|
611 |
return PEP_rating_reliable;
|
vb@311
|
612 |
|
vb@311
|
613 |
else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
|
vb@311
|
614 |
return PEP_rating_unencrypted;
|
vb@311
|
615 |
|
vb@311
|
616 |
else
|
vb@311
|
617 |
return PEP_rating_unreliable;
|
vb@311
|
618 |
}
|
vb@311
|
619 |
|
vb@311
|
620 |
static bool is_encrypted_attachment(const bloblist_t *blob)
|
vb@311
|
621 |
{
|
vb@311
|
622 |
char *ext;
|
vb@311
|
623 |
|
vb@311
|
624 |
assert(blob);
|
vb@311
|
625 |
|
vb@311
|
626 |
if (blob->filename == NULL)
|
vb@311
|
627 |
return false;
|
vb@311
|
628 |
|
vb@311
|
629 |
ext = strrchr(blob->filename, '.');
|
vb@311
|
630 |
if (ext == NULL)
|
vb@311
|
631 |
return false;
|
vb@311
|
632 |
|
vb@320
|
633 |
if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
|
vb@311
|
634 |
if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
|
vb@311
|
635 |
strcmp(ext, ".asc") == 0)
|
vb@311
|
636 |
return true;
|
vb@311
|
637 |
}
|
vb@320
|
638 |
else if (strcmp(blob->mime_type, "text/plain") == 0) {
|
vb@311
|
639 |
if (strcmp(ext, ".asc") == 0)
|
vb@311
|
640 |
return true;
|
vb@311
|
641 |
}
|
vb@311
|
642 |
|
vb@311
|
643 |
return false;
|
vb@311
|
644 |
}
|
vb@311
|
645 |
|
vb@311
|
646 |
static bool is_encrypted_html_attachment(const bloblist_t *blob)
|
vb@311
|
647 |
{
|
vb@311
|
648 |
assert(blob);
|
vb@311
|
649 |
assert(blob->filename);
|
vb@311
|
650 |
|
vb@311
|
651 |
if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
|
vb@311
|
652 |
if (strcmp(blob->filename + 11, ".pgp") == 0 ||
|
vb@311
|
653 |
strcmp(blob->filename + 11, ".asc") == 0)
|
vb@311
|
654 |
return true;
|
vb@311
|
655 |
}
|
vb@311
|
656 |
|
vb@311
|
657 |
return false;
|
vb@311
|
658 |
}
|
vb@311
|
659 |
|
vb@311
|
660 |
static char * without_double_ending(const char *filename)
|
vb@311
|
661 |
{
|
vb@311
|
662 |
char *ext;
|
vb@311
|
663 |
|
vb@311
|
664 |
assert(filename);
|
vb@311
|
665 |
|
vb@311
|
666 |
ext = strrchr(filename, '.');
|
vb@311
|
667 |
if (ext == NULL)
|
vb@311
|
668 |
return NULL;
|
vb@311
|
669 |
|
vb@311
|
670 |
return strndup(filename, ext - filename);
|
vb@311
|
671 |
}
|
vb@311
|
672 |
|
vb@311
|
673 |
static PEP_color decrypt_color(PEP_STATUS status)
|
vb@311
|
674 |
{
|
vb@311
|
675 |
switch (status) {
|
vb@311
|
676 |
case PEP_UNENCRYPTED:
|
vb@311
|
677 |
case PEP_VERIFIED:
|
vb@311
|
678 |
case PEP_VERIFY_NO_KEY:
|
vb@311
|
679 |
case PEP_VERIFIED_AND_TRUSTED:
|
vb@311
|
680 |
return PEP_rating_unencrypted;
|
vb@311
|
681 |
|
vb@311
|
682 |
case PEP_DECRYPTED:
|
vb@311
|
683 |
return PEP_rating_unreliable;
|
vb@311
|
684 |
|
vb@311
|
685 |
case PEP_DECRYPTED_AND_VERIFIED:
|
vb@311
|
686 |
return PEP_rating_reliable;
|
vb@311
|
687 |
|
vb@311
|
688 |
case PEP_DECRYPT_NO_KEY:
|
vb@311
|
689 |
return PEP_rating_have_no_key;
|
vb@311
|
690 |
|
vb@311
|
691 |
case PEP_DECRYPT_WRONG_FORMAT:
|
vb@311
|
692 |
case PEP_CANNOT_DECRYPT_UNKNOWN:
|
vb@311
|
693 |
return PEP_rating_cannot_decrypt;
|
vb@311
|
694 |
|
vb@311
|
695 |
default:
|
vb@311
|
696 |
return PEP_rating_undefined;
|
vb@311
|
697 |
}
|
vb@311
|
698 |
}
|
vb@311
|
699 |
|
vb@311
|
700 |
static PEP_color key_color(PEP_SESSION session, const char *fpr)
|
vb@311
|
701 |
{
|
vb@311
|
702 |
PEP_comm_type comm_type = PEP_ct_unknown;
|
vb@311
|
703 |
|
vb@311
|
704 |
assert(session);
|
vb@311
|
705 |
assert(fpr);
|
vb@311
|
706 |
|
vb@311
|
707 |
PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
|
vb@311
|
708 |
if (status != PEP_STATUS_OK)
|
vb@311
|
709 |
return PEP_rating_undefined;
|
vb@311
|
710 |
|
vb@311
|
711 |
return _rating(comm_type);
|
vb@311
|
712 |
}
|
vb@311
|
713 |
|
vb@311
|
714 |
static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
|
vb@311
|
715 |
{
|
vb@311
|
716 |
PEP_color color = PEP_rating_reliable;
|
vb@311
|
717 |
|
vb@311
|
718 |
assert(keylist && keylist->value);
|
vb@311
|
719 |
if (keylist == NULL || keylist->value == NULL)
|
vb@311
|
720 |
return PEP_rating_unencrypted;
|
vb@311
|
721 |
|
vb@311
|
722 |
stringlist_t *_kl;
|
vb@311
|
723 |
for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
|
vb@311
|
724 |
PEP_comm_type ct;
|
vb@311
|
725 |
PEP_STATUS status;
|
vb@311
|
726 |
|
vb@311
|
727 |
color = key_color(session, _kl->value);
|
vb@311
|
728 |
if (color == PEP_rating_under_attack)
|
vb@311
|
729 |
return PEP_rating_under_attack;
|
vb@311
|
730 |
|
vb@311
|
731 |
if (color >= PEP_rating_reliable) {
|
vb@311
|
732 |
status = least_trust(session, _kl->value, &ct);
|
vb@311
|
733 |
if (status != PEP_STATUS_OK)
|
vb@311
|
734 |
return PEP_rating_undefined;
|
vb@311
|
735 |
if (ct == PEP_ct_unknown)
|
vb@311
|
736 |
color = PEP_rating_unreliable;
|
vb@311
|
737 |
else
|
vb@311
|
738 |
color = _rating(ct);
|
vb@311
|
739 |
}
|
vb@311
|
740 |
}
|
vb@311
|
741 |
|
vb@311
|
742 |
return color;
|
vb@311
|
743 |
}
|
vb@311
|
744 |
|
vb@311
|
745 |
static PEP_comm_type _get_comm_type(
|
vb@311
|
746 |
PEP_SESSION session,
|
vb@311
|
747 |
PEP_comm_type max_comm_type,
|
vb@311
|
748 |
pEp_identity *ident
|
vb@311
|
749 |
)
|
vb@311
|
750 |
{
|
vb@311
|
751 |
PEP_STATUS status = update_identity(session, ident);
|
vb@311
|
752 |
|
vb@311
|
753 |
if (max_comm_type == PEP_ct_compromized)
|
vb@311
|
754 |
return PEP_ct_compromized;
|
vb@311
|
755 |
|
vb@311
|
756 |
if (status == PEP_STATUS_OK) {
|
vb@311
|
757 |
if (ident->comm_type == PEP_ct_compromized)
|
vb@311
|
758 |
return PEP_ct_compromized;
|
vb@311
|
759 |
else
|
vb@311
|
760 |
return MIN(max_comm_type, ident->comm_type);
|
vb@311
|
761 |
}
|
vb@311
|
762 |
else {
|
vb@311
|
763 |
return PEP_ct_unknown;
|
vb@311
|
764 |
}
|
vb@311
|
765 |
}
|
vb@311
|
766 |
|
vb@311
|
767 |
void import_attached_keys(PEP_SESSION session, const message *msg)
|
vb@311
|
768 |
{
|
vb@311
|
769 |
assert(session);
|
vb@311
|
770 |
assert(msg);
|
vb@311
|
771 |
|
vb@311
|
772 |
bloblist_t *bl;
|
vb@311
|
773 |
for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
|
vb@311
|
774 |
assert(bl && bl->value && bl->size);
|
vb@311
|
775 |
|
vb@311
|
776 |
if (bl->mime_type == NULL ||
|
vb@311
|
777 |
is_mime_type(bl, "application/octet-stream")) {
|
vb@311
|
778 |
if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
|
vb@311
|
779 |
is_fileending(bl, ".key") ||
|
vb@311
|
780 |
string_equality(bl->filename, "key.asc"))
|
vb@311
|
781 |
import_key(session, bl->value, bl->size);
|
vb@311
|
782 |
}
|
vb@311
|
783 |
else if (is_mime_type(bl, "application/pgp-keys")) {
|
vb@311
|
784 |
import_key(session, bl->value, bl->size);
|
vb@311
|
785 |
}
|
vb@311
|
786 |
else if (is_mime_type(bl, "text/plain")) {
|
vb@311
|
787 |
if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
|
vb@311
|
788 |
is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
|
vb@311
|
789 |
import_key(session, bl->value, bl->size);
|
vb@311
|
790 |
}
|
vb@311
|
791 |
}
|
vb@311
|
792 |
}
|
vb@311
|
793 |
|
vb@311
|
794 |
void attach_own_key(PEP_SESSION session, message *msg)
|
vb@311
|
795 |
{
|
vb@311
|
796 |
char *keydata;
|
vb@311
|
797 |
size_t size;
|
vb@311
|
798 |
bloblist_t *bl;
|
vb@311
|
799 |
|
vb@311
|
800 |
assert(session);
|
vb@311
|
801 |
assert(msg);
|
vb@311
|
802 |
|
vb@311
|
803 |
if (msg->dir == PEP_dir_incoming)
|
vb@311
|
804 |
return;
|
vb@311
|
805 |
|
vb@311
|
806 |
assert(msg->from && msg->from->fpr);
|
vb@311
|
807 |
if (msg->from == NULL || msg->from->fpr == NULL)
|
vb@311
|
808 |
return;
|
vb@311
|
809 |
|
vb@311
|
810 |
PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
|
vb@311
|
811 |
assert(status == PEP_STATUS_OK);
|
vb@311
|
812 |
if (status != PEP_STATUS_OK)
|
vb@311
|
813 |
return;
|
vb@311
|
814 |
assert(size);
|
vb@311
|
815 |
|
vb@311
|
816 |
bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
|
vb@311
|
817 |
"pEp_key.asc");
|
vb@311
|
818 |
if (msg->attachments == NULL && bl)
|
vb@311
|
819 |
msg->attachments = bl;
|
vb@311
|
820 |
}
|
vb@311
|
821 |
|
vb@311
|
822 |
PEP_cryptotech determine_encryption_format(message *msg)
|
vb@311
|
823 |
{
|
vb@311
|
824 |
assert(msg);
|
vb@311
|
825 |
|
vb@311
|
826 |
if (is_PGP_message_text(msg->longmsg)) {
|
vb@311
|
827 |
msg->enc_format = PEP_enc_pieces;
|
vb@311
|
828 |
return PEP_crypt_OpenPGP;
|
vb@311
|
829 |
}
|
vb@311
|
830 |
else if (msg->attachments && msg->attachments->next &&
|
vb@311
|
831 |
is_mime_type(msg->attachments, "application/pgp-encrypted") &&
|
vb@311
|
832 |
is_PGP_message_text(msg->attachments->next->value)
|
vb@311
|
833 |
) {
|
vb@311
|
834 |
msg->enc_format = PEP_enc_PGP_MIME;
|
vb@311
|
835 |
return PEP_crypt_OpenPGP;
|
vb@311
|
836 |
}
|
vb@311
|
837 |
else {
|
vb@311
|
838 |
msg->enc_format = PEP_enc_none;
|
vb@311
|
839 |
return PEP_crypt_none;
|
vb@311
|
840 |
}
|
vb@311
|
841 |
}
|
vb@311
|
842 |
|
vb@48
|
843 |
DYNAMIC_API PEP_STATUS encrypt_message(
|
vb@37
|
844 |
PEP_SESSION session,
|
vb@113
|
845 |
message *src,
|
vb@37
|
846 |
stringlist_t * extra,
|
vb@38
|
847 |
message **dst,
|
vb@81
|
848 |
PEP_enc_format enc_format
|
vb@37
|
849 |
)
|
vb@37
|
850 |
{
|
vb@37
|
851 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@63
|
852 |
message * msg = NULL;
|
vb@63
|
853 |
stringlist_t * keys = NULL;
|
vb@37
|
854 |
|
vb@37
|
855 |
assert(session);
|
vb@37
|
856 |
assert(src);
|
vb@37
|
857 |
assert(dst);
|
vb@260
|
858 |
assert(enc_format != PEP_enc_none);
|
vb@81
|
859 |
|
vb@260
|
860 |
if (!(session && src && dst && enc_format != PEP_enc_none))
|
vb@191
|
861 |
return PEP_ILLEGAL_VALUE;
|
vb@191
|
862 |
|
vb@259
|
863 |
determine_encryption_format(src);
|
vb@260
|
864 |
if (src->enc_format != PEP_enc_none)
|
vb@260
|
865 |
return PEP_ILLEGAL_VALUE;
|
vb@259
|
866 |
|
vb@37
|
867 |
*dst = NULL;
|
vb@67
|
868 |
|
vb@236
|
869 |
status = myself(session, src->from);
|
vb@236
|
870 |
if (status != PEP_STATUS_OK)
|
vb@236
|
871 |
goto pep_error;
|
vb@236
|
872 |
|
vb@80
|
873 |
keys = new_stringlist(src->from->fpr);
|
vb@63
|
874 |
if (keys == NULL)
|
vb@63
|
875 |
goto enomem;
|
vb@37
|
876 |
|
vb@39
|
877 |
stringlist_t *_k = keys;
|
vb@39
|
878 |
|
vb@39
|
879 |
if (extra) {
|
vb@39
|
880 |
_k = stringlist_append(_k, extra);
|
vb@63
|
881 |
if (_k == NULL)
|
vb@63
|
882 |
goto enomem;
|
vb@37
|
883 |
}
|
vb@39
|
884 |
|
vb@299
|
885 |
bool dest_keys_found = true;
|
vb@299
|
886 |
|
vb@37
|
887 |
identity_list * _il;
|
vb@299
|
888 |
for (_il = src->to; _il && _il->ident; _il = _il->next) {
|
vb@299
|
889 |
PEP_STATUS _status = update_identity(session, _il->ident);
|
vb@299
|
890 |
if (_status != PEP_STATUS_OK) {
|
vb@299
|
891 |
status = _status;
|
vb@63
|
892 |
goto pep_error;
|
vb@299
|
893 |
}
|
vb@63
|
894 |
|
vb@299
|
895 |
if (_il->ident->fpr && _il->ident->fpr[0]) {
|
vb@39
|
896 |
_k = stringlist_add(_k, _il->ident->fpr);
|
vb@63
|
897 |
if (_k == NULL)
|
vb@63
|
898 |
goto enomem;
|
vb@37
|
899 |
}
|
vb@299
|
900 |
else {
|
vb@299
|
901 |
dest_keys_found = false;
|
vb@37
|
902 |
status = PEP_KEY_NOT_FOUND;
|
vb@299
|
903 |
}
|
vb@299
|
904 |
}
|
vb@299
|
905 |
|
vb@299
|
906 |
for (_il = src->cc; _il && _il->ident; _il = _il->next) {
|
vb@299
|
907 |
PEP_STATUS _status = update_identity(session, _il->ident);
|
vb@299
|
908 |
if (_status != PEP_STATUS_OK)
|
vb@299
|
909 |
{
|
vb@299
|
910 |
status = _status;
|
vb@299
|
911 |
goto pep_error;
|
vb@299
|
912 |
}
|
vb@299
|
913 |
|
vb@299
|
914 |
if (_il->ident->fpr && _il->ident->fpr[0]) {
|
vb@299
|
915 |
_k = stringlist_add(_k, _il->ident->fpr);
|
vb@299
|
916 |
if (_k == NULL)
|
vb@299
|
917 |
goto enomem;
|
vb@299
|
918 |
}
|
vb@299
|
919 |
else {
|
vb@299
|
920 |
dest_keys_found = false;
|
vb@299
|
921 |
status = PEP_KEY_NOT_FOUND;
|
vb@299
|
922 |
}
|
vb@37
|
923 |
}
|
vb@37
|
924 |
|
vb@301
|
925 |
if (!dest_keys_found) {
|
vb@301
|
926 |
free_stringlist(keys);
|
vb@301
|
927 |
attach_own_key(session, src);
|
vb@301
|
928 |
return PEP_UNENCRYPTED;
|
vb@301
|
929 |
}
|
vb@301
|
930 |
else {
|
vb@299
|
931 |
msg = clone_to_empty_message(src);
|
vb@299
|
932 |
if (msg == NULL)
|
vb@299
|
933 |
goto enomem;
|
vb@299
|
934 |
|
vb@81
|
935 |
switch (enc_format) {
|
vb@260
|
936 |
case PEP_enc_PGP_MIME:
|
vb@260
|
937 |
status = encrypt_PGP_MIME(session, src, keys, msg);
|
vb@260
|
938 |
if (status != PEP_STATUS_OK)
|
vb@63
|
939 |
goto pep_error;
|
vb@260
|
940 |
break;
|
vb@62
|
941 |
|
vb@62
|
942 |
case PEP_enc_pieces:
|
vb@260
|
943 |
status = encrypt_PGP_in_pieces(session, src, keys, msg);
|
vb@319
|
944 |
if (status == PEP_OUT_OF_MEMORY)
|
vb@319
|
945 |
goto enomem;
|
vb@319
|
946 |
if (status != PEP_STATUS_OK) {
|
vb@319
|
947 |
attach_own_key(session, src);
|
vb@260
|
948 |
goto pep_error;
|
vb@319
|
949 |
}
|
vb@319
|
950 |
else {
|
vb@319
|
951 |
attach_own_key(session, msg);
|
vb@319
|
952 |
}
|
vb@38
|
953 |
break;
|
vb@38
|
954 |
|
vb@81
|
955 |
case PEP_enc_PEP:
|
vb@81
|
956 |
// TODO: implement
|
vb@81
|
957 |
NOT_IMPLEMENTED
|
vb@81
|
958 |
|
vb@38
|
959 |
default:
|
vb@38
|
960 |
assert(0);
|
vb@63
|
961 |
status = PEP_ILLEGAL_VALUE;
|
vb@63
|
962 |
goto pep_error;
|
vb@37
|
963 |
}
|
vb@37
|
964 |
}
|
vb@37
|
965 |
|
vb@37
|
966 |
free_stringlist(keys);
|
vb@63
|
967 |
|
vb@299
|
968 |
if (msg && msg->shortmsg == NULL)
|
vb@64
|
969 |
msg->shortmsg = strdup("pEp");
|
vb@64
|
970 |
|
vb@311
|
971 |
if (msg)
|
vb@311
|
972 |
decorate_message(msg, PEP_rating_undefined, NULL);
|
vb@311
|
973 |
|
vb@63
|
974 |
*dst = msg;
|
vb@299
|
975 |
return status;
|
vb@63
|
976 |
|
vb@63
|
977 |
enomem:
|
vb@63
|
978 |
status = PEP_OUT_OF_MEMORY;
|
vb@63
|
979 |
|
vb@63
|
980 |
pep_error:
|
vb@63
|
981 |
free_stringlist(keys);
|
vb@63
|
982 |
free_message(msg);
|
vb@63
|
983 |
|
vb@37
|
984 |
return status;
|
vb@37
|
985 |
}
|
vb@37
|
986 |
|
vb@48
|
987 |
DYNAMIC_API PEP_STATUS decrypt_message(
|
vb@37
|
988 |
PEP_SESSION session,
|
vb@113
|
989 |
message *src,
|
vb@241
|
990 |
message **dst,
|
vb@251
|
991 |
stringlist_t **keylist,
|
vb@251
|
992 |
PEP_color *color
|
vb@37
|
993 |
)
|
vb@37
|
994 |
{
|
vb@37
|
995 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@261
|
996 |
PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
|
vb@73
|
997 |
message *msg = NULL;
|
vb@112
|
998 |
char *ctext;
|
vb@112
|
999 |
size_t csize;
|
vb@269
|
1000 |
char *ptext = NULL;
|
vb@112
|
1001 |
size_t psize;
|
vb@241
|
1002 |
stringlist_t *_keylist = NULL;
|
vb@37
|
1003 |
|
vb@74
|
1004 |
assert(session);
|
vb@74
|
1005 |
assert(src);
|
vb@74
|
1006 |
assert(dst);
|
vb@241
|
1007 |
assert(keylist);
|
vb@251
|
1008 |
assert(color);
|
vb@73
|
1009 |
|
vb@251
|
1010 |
if (!(session && src && dst && keylist && color))
|
vb@191
|
1011 |
return PEP_ILLEGAL_VALUE;
|
vb@191
|
1012 |
|
vb@259
|
1013 |
import_attached_keys(session, src);
|
vb@259
|
1014 |
PEP_cryptotech crypto = determine_encryption_format(src);
|
vb@259
|
1015 |
|
vb@74
|
1016 |
*dst = NULL;
|
vb@251
|
1017 |
*keylist = NULL;
|
vb@251
|
1018 |
*color = PEP_rating_undefined;
|
vb@81
|
1019 |
|
vb@261
|
1020 |
switch (src->enc_format) {
|
vb@269
|
1021 |
case PEP_enc_none:
|
vb@301
|
1022 |
*color = PEP_rating_unencrypted;
|
vb@301
|
1023 |
return PEP_UNENCRYPTED;
|
vb@269
|
1024 |
|
vb@261
|
1025 |
case PEP_enc_PGP_MIME:
|
vb@301
|
1026 |
ctext = src->attachments->next->value;
|
vb@269
|
1027 |
csize = src->attachments->next->size;
|
vb@117
|
1028 |
|
vb@265
|
1029 |
status = cryptotech[crypto].decrypt_and_verify(session, ctext,
|
vb@265
|
1030 |
csize, &ptext, &psize, &_keylist);
|
vb@261
|
1031 |
if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
|
vb@261
|
1032 |
goto pep_error;
|
vb@261
|
1033 |
decrypt_status = status;
|
vb@261
|
1034 |
break;
|
vb@261
|
1035 |
|
vb@261
|
1036 |
case PEP_enc_pieces:
|
vb@261
|
1037 |
ctext = src->longmsg;
|
vb@261
|
1038 |
csize = strlen(ctext);
|
vb@261
|
1039 |
|
vb@265
|
1040 |
status = cryptotech[crypto].decrypt_and_verify(session, ctext,
|
vb@265
|
1041 |
csize, &ptext, &psize, &_keylist);
|
vb@261
|
1042 |
if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
|
vb@261
|
1043 |
goto pep_error;
|
vb@261
|
1044 |
decrypt_status = status;
|
vb@261
|
1045 |
break;
|
vb@261
|
1046 |
|
vb@261
|
1047 |
default:
|
vb@261
|
1048 |
NOT_IMPLEMENTED
|
vb@259
|
1049 |
}
|
vb@113
|
1050 |
|
vb@256
|
1051 |
*color = decrypt_color(status);
|
vb@259
|
1052 |
|
vb@259
|
1053 |
if (*color != PEP_rating_under_attack) {
|
vb@259
|
1054 |
PEP_color kl_color = PEP_rating_undefined;
|
vb@259
|
1055 |
|
vb@259
|
1056 |
if (_keylist)
|
vb@259
|
1057 |
kl_color = keylist_color(session, _keylist);
|
vb@259
|
1058 |
|
vb@259
|
1059 |
if (kl_color == PEP_rating_under_attack)
|
vb@251
|
1060 |
*color = PEP_rating_under_attack;
|
vb@259
|
1061 |
|
vb@260
|
1062 |
else if (*color >= PEP_rating_reliable &&
|
vb@259
|
1063 |
kl_color < PEP_rating_reliable)
|
vb@259
|
1064 |
*color = PEP_rating_unreliable;
|
vb@259
|
1065 |
|
vb@260
|
1066 |
else if (*color >= PEP_rating_reliable &&
|
vb@260
|
1067 |
kl_color >= PEP_rating_trusted)
|
vb@260
|
1068 |
*color = kl_color;
|
vb@251
|
1069 |
}
|
vb@251
|
1070 |
|
vb@256
|
1071 |
if (ptext) {
|
vb@256
|
1072 |
switch (src->enc_format) {
|
vb@256
|
1073 |
case PEP_enc_PGP_MIME:
|
vb@269
|
1074 |
status = mime_decode_message(ptext, psize, &msg);
|
vb@113
|
1075 |
if (status != PEP_STATUS_OK)
|
vb@113
|
1076 |
goto pep_error;
|
vb@256
|
1077 |
break;
|
vb@256
|
1078 |
|
vb@256
|
1079 |
case PEP_enc_pieces:
|
vb@256
|
1080 |
msg = clone_to_empty_message(src);
|
vb@256
|
1081 |
if (msg == NULL)
|
vb@113
|
1082 |
goto enomem;
|
vb@113
|
1083 |
|
vb@256
|
1084 |
msg->longmsg = strdup(ptext);
|
vb@256
|
1085 |
if (msg->longmsg == NULL)
|
vb@256
|
1086 |
goto enomem;
|
vb@256
|
1087 |
|
vb@256
|
1088 |
bloblist_t *_m = msg->attachments;
|
vb@320
|
1089 |
if (_m == NULL && src->attachments && src->attachments->value) {
|
vb@320
|
1090 |
msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
|
vb@320
|
1091 |
_m = msg->attachments;
|
vb@320
|
1092 |
}
|
vb@320
|
1093 |
|
vb@256
|
1094 |
bloblist_t *_s;
|
vb@320
|
1095 |
for (_s = src->attachments; _s && _s->value; _s = _s->next) {
|
vb@256
|
1096 |
if (is_encrypted_attachment(_s)) {
|
vb@256
|
1097 |
stringlist_t *_keylist = NULL;
|
vb@301
|
1098 |
ctext = _s->value;
|
vb@256
|
1099 |
csize = _s->size;
|
vb@256
|
1100 |
|
vb@256
|
1101 |
status = decrypt_and_verify(session, ctext, csize,
|
vb@256
|
1102 |
&ptext, &psize, &_keylist);
|
vb@256
|
1103 |
free_stringlist(_keylist);
|
vb@256
|
1104 |
|
vb@289
|
1105 |
if (ptext) {
|
vb@289
|
1106 |
if (is_encrypted_html_attachment(_s)) {
|
vb@290
|
1107 |
msg->longmsg_formatted = strdup(ptext);
|
vb@290
|
1108 |
if (msg->longmsg_formatted == NULL)
|
vb@290
|
1109 |
goto pep_error;
|
vb@289
|
1110 |
}
|
vb@289
|
1111 |
else {
|
vb@289
|
1112 |
char * mime_type = "application/octet-stream";
|
vb@289
|
1113 |
char * filename =
|
vb@289
|
1114 |
without_double_ending(_s->filename);
|
vb@289
|
1115 |
if (filename == NULL)
|
vb@289
|
1116 |
goto enomem;
|
vb@289
|
1117 |
|
vb@290
|
1118 |
char *_ptext = malloc(psize);
|
vb@290
|
1119 |
assert(_ptext);
|
vb@290
|
1120 |
if (_ptext == NULL)
|
vb@290
|
1121 |
goto enomem;
|
vb@290
|
1122 |
memcpy(_ptext, ptext, psize);
|
vb@290
|
1123 |
|
vb@290
|
1124 |
_m = bloblist_add(_m, _ptext, psize, mime_type,
|
vb@289
|
1125 |
filename);
|
vb@290
|
1126 |
if (_m == NULL)
|
vb@289
|
1127 |
goto enomem;
|
vb@290
|
1128 |
|
vb@290
|
1129 |
if (msg->attachments == NULL)
|
vb@290
|
1130 |
msg->attachments = _m;
|
vb@289
|
1131 |
}
|
vb@256
|
1132 |
}
|
vb@256
|
1133 |
else {
|
vb@320
|
1134 |
char *copy = malloc(_s->size);
|
vb@320
|
1135 |
memcpy(copy, _s->value, _s->size);
|
vb@320
|
1136 |
_m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
|
vb@256
|
1137 |
if (_m == NULL)
|
vb@256
|
1138 |
goto enomem;
|
vb@256
|
1139 |
}
|
vb@256
|
1140 |
}
|
vb@320
|
1141 |
else {
|
vb@320
|
1142 |
char *copy = malloc(_s->size);
|
vb@320
|
1143 |
memcpy(copy, _s->value, _s->size);
|
vb@320
|
1144 |
_m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
|
vb@320
|
1145 |
if (_m == NULL)
|
vb@320
|
1146 |
goto enomem;
|
vb@320
|
1147 |
}
|
vb@256
|
1148 |
}
|
vb@256
|
1149 |
|
vb@256
|
1150 |
break;
|
vb@256
|
1151 |
|
vb@256
|
1152 |
default:
|
vb@256
|
1153 |
// BUG: must implement more
|
vb@256
|
1154 |
NOT_IMPLEMENTED
|
vb@256
|
1155 |
}
|
vb@256
|
1156 |
|
vb@256
|
1157 |
switch (src->enc_format) {
|
vb@256
|
1158 |
case PEP_enc_PGP_MIME:
|
vb@256
|
1159 |
case PEP_enc_pieces:
|
vb@256
|
1160 |
status = copy_fields(msg, src);
|
vb@256
|
1161 |
if (status != PEP_STATUS_OK)
|
vb@256
|
1162 |
goto pep_error;
|
vb@256
|
1163 |
|
vb@280
|
1164 |
if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
|
vb@256
|
1165 |
{
|
vb@256
|
1166 |
char * shortmsg;
|
vb@256
|
1167 |
char * longmsg;
|
vb@256
|
1168 |
|
vb@256
|
1169 |
int r = seperate_short_and_long(msg->longmsg, &shortmsg,
|
vb@256
|
1170 |
&longmsg);
|
vb@256
|
1171 |
if (r == -1)
|
vb@256
|
1172 |
goto enomem;
|
vb@256
|
1173 |
|
vb@256
|
1174 |
free(msg->shortmsg);
|
vb@256
|
1175 |
free(msg->longmsg);
|
vb@256
|
1176 |
|
vb@256
|
1177 |
msg->shortmsg = shortmsg;
|
vb@256
|
1178 |
msg->longmsg = longmsg;
|
vb@256
|
1179 |
}
|
vb@256
|
1180 |
else {
|
vb@256
|
1181 |
msg->shortmsg = strdup(src->shortmsg);
|
vb@256
|
1182 |
if (msg->shortmsg == NULL)
|
vb@256
|
1183 |
goto enomem;
|
vb@256
|
1184 |
}
|
vb@256
|
1185 |
break;
|
vb@256
|
1186 |
|
vb@256
|
1187 |
default:
|
vb@256
|
1188 |
// BUG: must implement more
|
vb@256
|
1189 |
NOT_IMPLEMENTED
|
vb@256
|
1190 |
}
|
vb@256
|
1191 |
|
vb@256
|
1192 |
import_attached_keys(session, msg);
|
vb@113
|
1193 |
}
|
vb@113
|
1194 |
|
vb@283
|
1195 |
if (msg)
|
vb@311
|
1196 |
decorate_message(msg, *color, _keylist);
|
vb@235
|
1197 |
|
vb@74
|
1198 |
*dst = msg;
|
vb@241
|
1199 |
*keylist = _keylist;
|
vb@241
|
1200 |
|
vb@267
|
1201 |
return PEP_STATUS_OK;
|
vb@73
|
1202 |
|
vb@73
|
1203 |
enomem:
|
vb@73
|
1204 |
status = PEP_OUT_OF_MEMORY;
|
vb@73
|
1205 |
|
vb@73
|
1206 |
pep_error:
|
vb@73
|
1207 |
free_message(msg);
|
vb@241
|
1208 |
free_stringlist(_keylist);
|
vb@39
|
1209 |
|
vb@37
|
1210 |
return status;
|
vb@37
|
1211 |
}
|
vb@37
|
1212 |
|
vb@251
|
1213 |
DYNAMIC_API PEP_STATUS outgoing_message_color(
|
vb@190
|
1214 |
PEP_SESSION session,
|
vb@190
|
1215 |
message *msg,
|
vb@232
|
1216 |
PEP_color *color
|
vb@190
|
1217 |
)
|
vb@190
|
1218 |
{
|
vb@190
|
1219 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@190
|
1220 |
PEP_comm_type max_comm_type = PEP_ct_pEp;
|
vb@190
|
1221 |
bool comm_type_determined = false;
|
vb@190
|
1222 |
identity_list * il;
|
vb@190
|
1223 |
|
vb@190
|
1224 |
assert(session);
|
vb@190
|
1225 |
assert(msg);
|
vb@251
|
1226 |
assert(msg->from);
|
vb@251
|
1227 |
assert(msg->dir == PEP_dir_outgoing);
|
vb@190
|
1228 |
assert(color);
|
vb@190
|
1229 |
|
vb@191
|
1230 |
if (!(session && msg && color))
|
vb@191
|
1231 |
return PEP_ILLEGAL_VALUE;
|
vb@191
|
1232 |
|
vb@251
|
1233 |
if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
|
vb@251
|
1234 |
return PEP_ILLEGAL_VALUE;
|
vb@251
|
1235 |
|
vb@237
|
1236 |
*color = PEP_rating_undefined;
|
vb@190
|
1237 |
|
vb@251
|
1238 |
status = myself(session, msg->from);
|
vb@251
|
1239 |
if (status != PEP_STATUS_OK)
|
vb@251
|
1240 |
return status;
|
vb@251
|
1241 |
|
vb@251
|
1242 |
for (il = msg->to; il != NULL; il = il->next) {
|
vb@251
|
1243 |
if (il->ident) {
|
vb@294
|
1244 |
update_identity(session, il->ident);
|
vb@251
|
1245 |
max_comm_type = _get_comm_type(session, max_comm_type,
|
vb@251
|
1246 |
il->ident);
|
vb@190
|
1247 |
comm_type_determined = true;
|
vb@251
|
1248 |
}
|
vb@251
|
1249 |
}
|
vb@190
|
1250 |
|
vb@251
|
1251 |
for (il = msg->cc; il != NULL; il = il->next) {
|
vb@251
|
1252 |
if (il->ident) {
|
vb@294
|
1253 |
update_identity(session, il->ident);
|
vb@251
|
1254 |
max_comm_type = _get_comm_type(session, max_comm_type,
|
vb@251
|
1255 |
il->ident);
|
vb@251
|
1256 |
comm_type_determined = true;
|
vb@251
|
1257 |
}
|
vb@190
|
1258 |
}
|
vb@190
|
1259 |
|
vb@190
|
1260 |
if (comm_type_determined == false)
|
vb@237
|
1261 |
*color = PEP_rating_undefined;
|
vb@190
|
1262 |
else
|
vb@300
|
1263 |
*color = MAX(_rating(max_comm_type), PEP_rating_unencrypted);
|
vb@190
|
1264 |
|
vb@190
|
1265 |
return PEP_STATUS_OK;
|
vb@190
|
1266 |
}
|
vb@190
|
1267 |
|
vb@240
|
1268 |
DYNAMIC_API PEP_STATUS identity_color(
|
vb@239
|
1269 |
PEP_SESSION session,
|
vb@239
|
1270 |
pEp_identity *ident,
|
vb@239
|
1271 |
PEP_color *color
|
vb@239
|
1272 |
)
|
vb@239
|
1273 |
{
|
vb@239
|
1274 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@239
|
1275 |
|
vb@239
|
1276 |
assert(session);
|
vb@239
|
1277 |
assert(ident);
|
vb@239
|
1278 |
assert(color);
|
vb@239
|
1279 |
|
vb@239
|
1280 |
if (!(session && ident && color))
|
vb@239
|
1281 |
return PEP_ILLEGAL_VALUE;
|
vb@239
|
1282 |
|
vb@239
|
1283 |
if (ident->me)
|
vb@239
|
1284 |
status = myself(session, ident);
|
vb@239
|
1285 |
else
|
vb@239
|
1286 |
status = update_identity(session, ident);
|
vb@239
|
1287 |
|
vb@239
|
1288 |
if (status == PEP_STATUS_OK)
|
vb@239
|
1289 |
*color = _rating(ident->comm_type);
|
vb@239
|
1290 |
|
vb@239
|
1291 |
return status;
|
vb@239
|
1292 |
}
|
vb@239
|
1293 |
|