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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if HAVE_LIBGPGME
#include <locale.h>
#include <gpgme.h>
#include "base64.h"
#endif
#include "signing.h"
#include "package.h"
#include "util.h"
#include "log.h"
#include "alpm.h"
#include "handle.h"
#if HAVE_LIBGPGME
#define CHECK_ERR(void) do { \
if(gpg_err_code(err) != GPG_ERR_NO_ERROR) { goto error; } \
} while(0)
static const char *string_validity(gpgme_validity_t validity)
{
switch(validity) {
case GPGME_VALIDITY_UNKNOWN:
return "unknown";
case GPGME_VALIDITY_UNDEFINED:
return "undefined";
case GPGME_VALIDITY_NEVER:
return "never";
case GPGME_VALIDITY_MARGINAL:
return "marginal";
case GPGME_VALIDITY_FULL:
return "full";
case GPGME_VALIDITY_ULTIMATE:
return "ultimate";
}
return "???";
}
static void sigsum_test_bit(gpgme_sigsum_t sigsum, alpm_list_t **summary,
gpgme_sigsum_t bit, const char *value)
{
if(sigsum & bit) {
*summary = alpm_list_add(*summary, (void *)value);
}
}
static alpm_list_t *list_sigsum(gpgme_sigsum_t sigsum)
{
alpm_list_t *summary = NULL;
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_VALID, "valid");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_GREEN, "green");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_RED, "red");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_KEY_REVOKED, "key revoked");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_KEY_EXPIRED, "key expired");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_SIG_EXPIRED, "sig expired");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_KEY_MISSING, "key missing");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_CRL_MISSING, "crl missing");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_CRL_TOO_OLD, "crl too old");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_BAD_POLICY, "bad policy");
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_SYS_ERROR, "sys error");
if(!sigsum) {
summary = alpm_list_add(summary, (void *)"(empty)");
}
return summary;
}
static int init_gpgme(alpm_handle_t *handle)
{
static int init = 0;
const char *version, *sigdir;
gpgme_error_t err;
gpgme_engine_info_t enginfo;
if(init) {
return 0;
}
sigdir = alpm_option_get_gpgdir(handle);
if (_alpm_access(handle, sigdir, "pubring.gpg", R_OK)
|| _alpm_access(handle, sigdir, "trustdb.gpg", R_OK)) {
handle->pm_errno = ALPM_ERR_NOT_A_FILE;
_alpm_log(handle, ALPM_LOG_DEBUG, "Signature verification will fail!\n");
}
version = gpgme_check_version(NULL);
_alpm_log(handle, ALPM_LOG_DEBUG, "GPGME version: %s\n", version);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
#ifdef LC_MESSAGES
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
#endif
err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
CHECK_ERR();
err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL, sigdir);
CHECK_ERR();
err = gpgme_get_engine_info(&enginfo);
CHECK_ERR();
_alpm_log(handle, ALPM_LOG_DEBUG, "GPGME engine info: file=%s, home=%s\n",
enginfo->file_name, enginfo->home_dir);
init = 1;
return 0;
error:
_alpm_log(handle, ALPM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err));
RET_ERR(handle, ALPM_ERR_GPGME, 1);
}
static int decode_signature(const char *base64_data,
unsigned char **data, size_t *data_len) {
size_t len = strlen(base64_data);
unsigned char *usline = (unsigned char *)base64_data;
size_t destlen = len * 3 / 4;
MALLOC(*data, destlen, goto error);
if(base64_decode(*data, &destlen, usline, len)) {
goto error;
}
*data_len = destlen;
return 0;
error:
*data = NULL;
*data_len = 0;
return 1;
}
int _alpm_gpgme_checksig(alpm_handle_t *handle, const char *path,
const char *base64_sig, alpm_sigresult_t *result)
{
int ret = -1, sigcount;
gpgme_error_t err;
gpgme_ctx_t ctx;
gpgme_data_t filedata, sigdata;
gpgme_verify_result_t verify_result;
gpgme_signature_t gpgsig;
char *sigpath = NULL;
unsigned char *decoded_sigdata = NULL;
FILE *file = NULL, *sigfile = NULL;
if(!path || _alpm_access(handle, NULL, path, R_OK) != 0) {
RET_ERR(handle, ALPM_ERR_NOT_A_FILE, -1);
}
if(!result) {
RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1);
}
result->count = 0;
if(!base64_sig) {
sigpath = _alpm_sigpath(handle, path);
_alpm_access(handle, NULL, sigpath, R_OK);
}
if(init_gpgme(handle)) {
return -1;
}
_alpm_log(handle, ALPM_LOG_DEBUG, "checking signature for %s\n", path);
memset(&ctx, 0, sizeof(ctx));
memset(&sigdata, 0, sizeof(sigdata));
memset(&filedata, 0, sizeof(filedata));
err = gpgme_new(&ctx);
CHECK_ERR();
file = fopen(path, "rb");
if(file == NULL) {
handle->pm_errno = ALPM_ERR_NOT_A_FILE;
goto error;
}
err = gpgme_data_new_from_stream(&filedata, file);
CHECK_ERR();
if(base64_sig) {
size_t data_len;
int decode_ret = decode_signature(base64_sig,
&decoded_sigdata, &data_len);
if(decode_ret) {
handle->pm_errno = ALPM_ERR_SIG_INVALID;
goto error;
}
err = gpgme_data_new_from_mem(&sigdata,
(char *)decoded_sigdata, data_len, 0);
} else {
sigfile = fopen(sigpath, "rb");
if(sigfile == NULL) {
_alpm_log(handle, ALPM_LOG_DEBUG, "sig path %s could not be opened\n",
sigpath);
handle->pm_errno = ALPM_ERR_SIG_MISSING;
goto error;
}
err = gpgme_data_new_from_stream(&sigdata, sigfile);
}
CHECK_ERR();
err = gpgme_op_verify(ctx, sigdata, filedata, NULL);
CHECK_ERR();
verify_result = gpgme_op_verify_result(ctx);
CHECK_ERR();
if(!verify_result || !verify_result->signatures) {
_alpm_log(handle, ALPM_LOG_DEBUG, "no signatures returned\n");
handle->pm_errno = ALPM_ERR_SIG_MISSING;
goto error;
}
for(gpgsig = verify_result->signatures, sigcount = 0;
gpgsig; gpgsig = gpgsig->next, sigcount++);
_alpm_log(handle, ALPM_LOG_DEBUG, "%d signatures returned\n", sigcount);
result->status = calloc(sigcount, sizeof(alpm_sigstatus_t));
result->validity = calloc(sigcount, sizeof(alpm_sigvalidity_t));
result->uid = calloc(sigcount, sizeof(char*));
if(!result->status || !result->validity || !result->uid) {
handle->pm_errno = ALPM_ERR_MEMORY;
goto error;
}
result->count = sigcount;
for(gpgsig = verify_result->signatures, sigcount = 0; gpgsig;
gpgsig = gpgsig->next, sigcount++) {
alpm_list_t *summary_list, *summary;
alpm_sigstatus_t status;
alpm_sigvalidity_t validity;
gpgme_key_t key;
_alpm_log(handle, ALPM_LOG_DEBUG, "fingerprint: %s\n", gpgsig->fpr);
summary_list = list_sigsum(gpgsig->summary);
for(summary = summary_list; summary; summary = summary->next) {
_alpm_log(handle, ALPM_LOG_DEBUG, "summary: %s\n", (const char *)summary->data);
}
alpm_list_free(summary_list);
_alpm_log(handle, ALPM_LOG_DEBUG, "status: %s\n", gpgme_strerror(gpgsig->status));
_alpm_log(handle, ALPM_LOG_DEBUG, "timestamp: %lu\n", gpgsig->timestamp);
_alpm_log(handle, ALPM_LOG_DEBUG, "exp_timestamp: %lu\n", gpgsig->exp_timestamp);
_alpm_log(handle, ALPM_LOG_DEBUG, "validity: %s; reason: %s\n",
string_validity(gpgsig->validity),
gpgme_strerror(gpgsig->validity_reason));
err = gpgme_get_key(ctx, gpgsig->fpr, &key, 0);
if(gpg_err_code(err) == GPG_ERR_EOF) {
_alpm_log(handle, ALPM_LOG_DEBUG, "key lookup failed, unknown key\n");
err = GPG_ERR_NO_ERROR;
STRDUP(result->uid[sigcount], gpgsig->fpr,
handle->pm_errno = ALPM_ERR_MEMORY; goto error);
} else {
CHECK_ERR();
if(key->uids) {
const char *uid = key->uids->uid;
STRDUP(result->uid[sigcount], uid,
handle->pm_errno = ALPM_ERR_MEMORY; goto error);
_alpm_log(handle, ALPM_LOG_DEBUG, "key user: %s\n", uid);
}
gpgme_key_unref(key);
}
switch(gpg_err_code(gpgsig->status)) {
case GPG_ERR_NO_ERROR:
status = ALPM_SIGSTATUS_VALID;
break;
case GPG_ERR_KEY_EXPIRED:
status = ALPM_SIGSTATUS_KEY_EXPIRED;
break;
case GPG_ERR_SIG_EXPIRED:
status = ALPM_SIGSTATUS_SIG_EXPIRED;
break;
case GPG_ERR_NO_PUBKEY:
status = ALPM_SIGSTATUS_KEY_UNKNOWN;
break;
case GPG_ERR_BAD_SIGNATURE:
default:
status = ALPM_SIGSTATUS_INVALID;
break;
}
if(status == ALPM_SIGSTATUS_VALID
|| status == ALPM_SIGSTATUS_KEY_EXPIRED) {
switch(gpgsig->validity) {
case GPGME_VALIDITY_ULTIMATE:
case GPGME_VALIDITY_FULL:
validity = ALPM_SIGVALIDITY_FULL;
break;
case GPGME_VALIDITY_MARGINAL:
validity = ALPM_SIGVALIDITY_MARGINAL;
break;
case GPGME_VALIDITY_NEVER:
validity = ALPM_SIGVALIDITY_NEVER;
break;
case GPGME_VALIDITY_UNKNOWN:
case GPGME_VALIDITY_UNDEFINED:
default:
validity = ALPM_SIGVALIDITY_UNKNOWN;
break;
}
} else {
validity = ALPM_SIGVALIDITY_NEVER;
}
result->status[sigcount] = status;
result->validity[sigcount] = validity;
}
ret = 0;
error:
gpgme_data_release(sigdata);
gpgme_data_release(filedata);
gpgme_release(ctx);
if(sigfile) {
fclose(sigfile);
}
if(file) {
fclose(file);
}
FREE(sigpath);
FREE(decoded_sigdata);
if(gpg_err_code(err) != GPG_ERR_NO_ERROR) {
_alpm_log(handle, ALPM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err));
RET_ERR(handle, ALPM_ERR_GPGME, -1);
}
return ret;
}
#else
int _alpm_gpgme_checksig(alpm_handle_t UNUSED *handle, const char UNUSED *path,
const char UNUSED *base64_sig, alpm_sigresult_t UNUSED *result)
{
return -1;
}
#endif
char *_alpm_sigpath(alpm_handle_t *handle, const char *path)
{
char *sigpath;
size_t len;
if(!path) {
return NULL;
}
len = strlen(path) + 5;
CALLOC(sigpath, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
sprintf(sigpath, "%s.sig", path);
return sigpath;
}
int _alpm_check_pgp_helper(alpm_handle_t *handle, const char *path,
const char *base64_sig, int optional, int marginal, int unknown,
enum _alpm_errno_t invalid_err)
{
alpm_sigresult_t result;
int ret;
memset(&result, 0, sizeof(result));
_alpm_log(handle, ALPM_LOG_DEBUG, "checking signatures for %s\n", path);
ret = _alpm_gpgme_checksig(handle, path, base64_sig, &result);
if(ret && handle->pm_errno == ALPM_ERR_SIG_MISSING) {
if(optional) {
_alpm_log(handle, ALPM_LOG_DEBUG, "missing optional signature\n");
handle->pm_errno = 0;
ret = 0;
} else {
_alpm_log(handle, ALPM_LOG_DEBUG, "missing required signature\n");
}
} else if(ret) {
_alpm_log(handle, ALPM_LOG_DEBUG, "signature check failed\n");
} else {
int num;
for(num = 0; !ret && num < result.count; num++) {
switch(result.status[num]) {
case ALPM_SIGSTATUS_VALID:
case ALPM_SIGSTATUS_KEY_EXPIRED:
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is valid\n");
switch(result.validity[num]) {
case ALPM_SIGVALIDITY_FULL:
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is fully trusted\n");
break;
case ALPM_SIGVALIDITY_MARGINAL:
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is marginal trust\n");
if(!marginal) {
ret = -1;
}
break;
case ALPM_SIGVALIDITY_UNKNOWN:
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is unknown trust\n");
if(!unknown) {
ret = -1;
}
break;
case ALPM_SIGVALIDITY_NEVER:
_alpm_log(handle, ALPM_LOG_DEBUG, "signature should never be trusted\n");
ret = -1;
break;
}
break;
case ALPM_SIGSTATUS_SIG_EXPIRED:
case ALPM_SIGSTATUS_KEY_UNKNOWN:
case ALPM_SIGSTATUS_INVALID:
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is not valid\n");
ret = -1;
break;
}
}
if(ret) {
handle->pm_errno = invalid_err;
}
}
alpm_sigresult_cleanup(&result);
return ret;
}
int SYMEXPORT alpm_pkg_check_pgp_signature(alpm_pkg_t *pkg,
alpm_sigresult_t *result)
{
ASSERT(pkg != NULL, return -1);
ASSERT(result != NULL, RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
pkg->handle->pm_errno = 0;
return _alpm_gpgme_checksig(pkg->handle, alpm_pkg_get_filename(pkg),
pkg->base64_sig, result);
}
int SYMEXPORT alpm_db_check_pgp_signature(alpm_db_t *db,
alpm_sigresult_t *result)
{
ASSERT(db != NULL, return -1);
ASSERT(result != NULL, RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, -1));
db->handle->pm_errno = 0;
return _alpm_gpgme_checksig(db->handle, _alpm_db_path(db), NULL, result);
}
int SYMEXPORT alpm_sigresult_cleanup(alpm_sigresult_t *result)
{
ASSERT(result != NULL, return -1);
if(result->count > 0) {
free(result->status);
if(result->uid) {
int i;
for(i = 0; i < result->count; i++) {
free(result->uid[i]);
}
free(result->uid);
}
}
return 0;
}
|