signature_check.c 25.6 KB
Newer Older
1
/*  This file is part of "reprepro"
2
 *  Copyright (C) 2003,2004,2005,2006,2007,2009,2012 Bernhard R. Link
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
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include "signature_p.h"
#include "ignore.h"
29 30 31
#include "chunks.h"
#include "readtextfile.h"

32 33 34 35 36 37 38 39 40 41 42

#ifdef HAVE_LIBGPGME

static retvalue parse_condition_part(bool *allow_subkeys_p, bool *allow_bad_p, const char *full_condition, const char **condition_p, /*@out@*/ char **next_key_p) {
	const char *key = *condition_p, *p;
	char *next_key, *q;
	size_t kl;

	*allow_bad_p = false;
	*allow_subkeys_p = false;

43
	while (*key != '\0' && xisspace(*key))
44
		key++;
45 46 47
	if (*key == '\0') {
		fprintf(stderr,
"Error: unexpected end of VerifyRelease condition '%s'!\n",
48 49 50 51 52
				full_condition);
		return RET_ERROR;
	}

	p = key;
53 54
	while ((*p >= 'A' && *p <= 'F') || (*p >= 'a' && *p <= 'f')
			|| (*p >= '0' && *p <= '9'))
55
		p++;
56
	if (*p != '\0' && !xisspace(*p) && *p != '|' && *p != '!' && *p != '+') {
57 58 59 60 61 62
		fprintf(stderr,
"Error: Unexpected character 0x%02hhx='%c' in VerifyRelease condition '%s'!\n",
				*p, *p, full_condition);
		return RET_ERROR;
	}
	kl = p - key;
63
	if (kl < 8) {
64 65
		fprintf(stderr,
"Error: Too short key id '%.*s' in VerifyRelease condition '%s'!\n",
66
				(int)kl, key, full_condition);
67 68 69
		return RET_ERROR;
	}
	next_key = strndup(key, kl);
70
	if (FAILEDTOALLOC(next_key))
71 72
		return RET_ERROR_OOM;
	key = p;
73 74
	for (q = next_key ; *q != '\0' ; q++) {
		if (*q >= 'a' && *q <= 'f')
75 76
			*q -= 'a' - 'A';
	}
77
	while (*key != '\0' && xisspace(*key))
78
		key++;
79
	if (*key == '!') {
80 81 82
		*allow_bad_p = true;
		key++;
	}
83
	while (*key != '\0' && xisspace(*key))
84
		key++;
85
	if (*key == '+') {
86 87 88
		*allow_subkeys_p = true;
		key++;
	}
89
	while (*key != '\0' && xisspace(*key))
90
		key++;
91
	if ((*key >= 'A' && *key <= 'F')
92
			|| (*key >= 'a' && *key <= 'f')
93
			|| (*key >= '0' && *key <= '9')) {
94 95 96 97 98 99 100
		free(next_key);
		fprintf(stderr,
"Error: Space separated key-ids in VerifyRelease condition '%s'!\n"
"(Alternate keys can be separated with '|'. Do not put spaces in key-ids.)\n",
				full_condition);
		return RET_ERROR;
	}
101
	if (*key != '\0' && *key != '|') {
102 103 104 105 106 107
		free(next_key);
		fprintf(stderr,
"Error: Unexpected character 0x%02hhx='%c' in VerifyRelease condition '%s'!\n",
				*key, *key, full_condition);
		return RET_ERROR;
	}
108
	if (*key == '|')
109 110 111 112 113 114 115 116 117
		key++;
	*next_key_p = next_key;
	*condition_p = key;
	return RET_OK;
}

static struct known_key {
	struct known_key *next;
	/* subkeys, first is primary key */
118
	int count;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	struct known_subkey {
		/* full fingerprint or keyid */
		char *name;
		unsigned int name_len;
		/* true if revoked */
		bool revoked;
		/* true if expired */
		bool expired;
		/* false if invalid or cannot sign */
		bool cansign;
	} subkeys[];
} *known_keys = NULL;

struct requested_key {
	/* pointer to the key in question */
	const struct known_key *key;
	/* which of those keys are requested, -1 for any (i.e. allow subkeys) */
	int subkey;
	/* allow some problems, if requested by the user */
	bool allow_bad;
};

141 142
static retvalue found_key(struct known_key *k, int i, bool allow_subkeys, bool allow_bad, const char *full_condition, const struct known_key **key_found, int *index_found) {
	if (!allow_bad && k->subkeys[i].revoked) {
143 144 145 146 147 148
		fprintf(stderr,
"VerifyRelease condition '%s' lists revoked key '%s'.\n"
"(To use it anyway, append it with a '!' to force usage).\n",
			full_condition, k->subkeys[i].name);
		return RET_ERROR;
	}
149
	if (!allow_bad && k->subkeys[i].expired) {
150 151 152 153 154 155
		fprintf(stderr,
"VerifyRelease condition '%s' lists expired key '%s'.\n"
"(To use it anyway, append it with a '!' to force usage).\n",
			full_condition, k->subkeys[i].name);
		return RET_ERROR;
	}
156
	if (!allow_bad && !k->subkeys[i].cansign) {
157 158 159 160 161 162
		fprintf(stderr,
"VerifyRelease condition '%s' lists non-signing key '%s'.\n"
"(To use it anyway, append it with a '!' to force usage).\n",
			full_condition, k->subkeys[i].name);
		return RET_ERROR;
	}
163 164
	if (allow_subkeys) {
		if (i != 0) {
165 166 167 168 169 170 171 172 173 174 175 176 177
			fprintf(stderr,
"VerifyRelease condition '%s' lists non-primary key '%s' with '+'.\n",
			full_condition, k->subkeys[i].name);
			return RET_ERROR;
		}
		*index_found = -1;
	} else
		*index_found = i;
	*key_found = k;
	return RET_OK;
}

/* name must already be upper-case */
178
static retvalue load_key(const char *name, bool allow_subkeys, bool allow_bad, const char *full_condition, const struct known_key **key_found, int *index_found) {
179 180 181
	gpg_error_t err;
	gpgme_key_t gpgme_key = NULL;
	gpgme_subkey_t subkey;
182
	int found = -1;
183
	struct known_key *k;
184
	int i;
185 186 187
	size_t l = strlen(name);

	/* first look if this key was already retrieved: */
188 189
	for (k = known_keys ; k != NULL ; k = k->next) {
		for(i = 0 ; i < k->count ; i++) {
190 191
			struct known_subkey *s = &k->subkeys[i];

192
			if (s->name_len < l)
193
				continue;
194
			if (memcmp(name, s->name + (s->name_len - l), l) != 0)
195 196 197 198 199 200 201 202
				continue;
			return found_key(k, i, allow_subkeys, allow_bad,
					full_condition,
					key_found, index_found);
		}
	}
	/* If not yet found, request it: */
	err = gpgme_get_key(context, name, &gpgme_key, 0);
203
	if ((gpg_err_code(err) == GPG_ERR_EOF) && gpgme_key == NULL) {
204 205 206
		fprintf(stderr, "Error: unknown key '%s'!\n", name);
		return RET_ERROR_MISSING;
	}
207
	if (err != 0) {
208 209
		fprintf(stderr, "gpgme error %s:%d retrieving key '%s': %s\n",
				gpg_strsource(err), (int)gpg_err_code(err),
210 211
				name, gpg_strerror(err));
		if (gpg_err_code(err) == GPG_ERR_ENOMEM)
212 213 214 215 216 217
			return RET_ERROR_OOM;
		else
			return RET_ERROR_GPGME;
	}
	i = 0;
	subkey = gpgme_key->subkeys;
218
	while (subkey != NULL) {
219 220 221
		subkey = subkey->next;
		i++;
	}
222 223 224
	k = calloc(1, sizeof(struct known_key)
			+ i * sizeof(struct known_subkey));
	if (FAILEDTOALLOC(k)) {
225 226 227 228 229 230 231 232
		gpgme_key_unref(gpgme_key);
		return RET_ERROR_OOM;
	}
	k->count = i;
	k->next = known_keys;
	known_keys = k;

	subkey = gpgme_key->subkeys;
233
	for (i = 0 ; i < k->count ; i++ , subkey = subkey->next) {
234 235
		struct known_subkey *s = &k->subkeys[i];

236
		assert (subkey != NULL);
237 238 239 240 241

		s->revoked = subkey->revoked;
		s->expired = subkey->expired;
		s->cansign = subkey->can_sign && !subkey->invalid;
		s->name = strdup(subkey->keyid);
242
		if (FAILEDTOALLOC(s->name)) {
243 244 245
			gpgme_key_unref(gpgme_key);
			return RET_ERROR_OOM;
		}
246 247
		for (char *p = s->name ; *p != '\0' ; p++) {
			if (*p >= 'a' && *p <= 'z')
248 249 250
				*p -= 'a'-'A';
		}
		s->name_len = strlen(s->name);
251
		if (memcmp(name, s->name + (s->name_len - l), l) == 0)
252 253
			found = i;
	}
254
	assert (subkey == NULL);
255
	gpgme_key_unref(gpgme_key);
256
	if (found < 0) {
257 258 259 260 261 262 263 264 265
		fprintf(stderr, "Error: not a valid key id '%s'!\n"
"Use hex-igits from the end of the key as identifier\n", name);
		return RET_ERROR;
	}
	return found_key(k, found, allow_subkeys, allow_bad,
			full_condition, key_found, index_found);
}

static void free_known_key(/*@only@*/struct known_key *k) {
266
	int i;
267

268
	for (i = 0 ; i < k->count ; i++) {
269 270 271 272 273 274
		free(k->subkeys[i].name);
	}
	free(k);
}

void free_known_keys(void) {
275
	while (known_keys != NULL) {
276 277 278 279 280 281 282 283 284 285 286 287 288 289
		struct known_key *k = known_keys;
		known_keys = k->next;
		free_known_key(k);
	}
	known_keys = NULL;
}

/* This checks a Release.gpg/Release file pair. requirements is a list of
 * requirements. (as this Release file can be requested by multiple update
 * rules, there can be multiple requirements for one file) */

struct signature_requirement {
	/* next condition */
	struct signature_requirement *next;
290
	/* the original description for error messages */
291 292 293 294 295 296 297 298
	char *condition;
	/* an array of or-connected conditions */
	size_t num_keys;
	struct requested_key keys[];
};
#define sizeof_requirement(n) (sizeof(struct signature_requirement) + (n) * sizeof(struct requested_key))

void signature_requirements_free(struct signature_requirement *list) {
299
	while (list != NULL) {
300 301 302 303 304 305 306 307 308 309 310 311
		struct signature_requirement *p = list;
		list = p->next;

		free(p->condition);
		free(p);
	}
}

static bool key_good(const struct requested_key *req, const gpgme_signature_t signatures) {
	const struct known_key *k = req->key;
	gpgme_signature_t sig;

312
	for (sig = signatures ; sig != NULL ; sig = sig->next) {
313 314 315 316 317 318 319 320
		const char *fpr = sig->fpr;
		size_t l = strlen(sig->fpr);
		int i;
		/* while gpg reports the subkey of an key that is expired
		   to be expired to, it does not tell this in the signature,
		   so we use this here... */
		bool key_expired = false;

321
		if (req->subkey < 0) {
322
			/* any subkey is allowed */
323
			for(i = 0 ; i < k->count ; i++) {
324 325
				const struct known_subkey *s = &k->subkeys[i];

326
				if (s->name_len > l)
327
					continue;
328 329
				if (memcmp(s->name, fpr + (l - s->name_len),
							s->name_len) != 0)
330 331 332 333
					continue;
				key_expired = k->subkeys[i].expired;
				break;
			}
334
			if (i >= k->count)
335 336 337 338
				continue;
		} else {
			const struct known_subkey *s;

339 340 341 342
			assert (req->subkey < k->count);
			s = &k->subkeys[req->subkey];
			if (memcmp(s->name, fpr + (l - s->name_len),
						s->name_len) != 0)
343 344 345 346 347 348
				continue;
			key_expired = k->subkeys[req->subkey].expired;
		}
		/* only accept perfectly good signatures and silently
		   ignore everything else. Those are warned about or
		   even accepted in the run with key_good_enough */
349 350
		if (gpg_err_code(sig->status) == GPG_ERR_NO_ERROR
				&& !key_expired)
351 352 353 354 355 356 357 358 359 360 361 362 363
			return true;
		/* we have to continue otherwise,
		   as another subkey might still follow */
		continue;
	}
	/* no valid signature with this key found */
	return false;
}

static bool key_good_enough(const struct requested_key *req, const gpgme_signature_t signatures, const char *releasegpg, const char *release) {
	const struct known_key *k = req->key;
	gpgme_signature_t sig;

364
	for (sig = signatures ; sig != NULL ; sig = sig->next) {
365 366 367 368 369
		const char *fpr = sig->fpr;
		size_t l = strlen(sig->fpr);
		int i;
		bool key_expired = false; /* dito */

370
		if (req->subkey < 0) {
371
			/* any subkey is allowed */
372
			for(i = 0 ; i < k->count ; i++) {
373 374
				const struct known_subkey *s = &k->subkeys[i];

375
				if (s->name_len > l)
376
					continue;
377 378
				if (memcmp(s->name, fpr + (l - s->name_len),
							s->name_len) != 0)
379 380 381 382
					continue;
				key_expired = k->subkeys[i].expired;
				break;
			}
383
			if (i >= k->count)
384 385 386 387
				continue;
		} else {
			const struct known_subkey *s;

388 389 390 391
			assert (req->subkey < k->count);
			s = &k->subkeys[req->subkey];
			if (memcmp(s->name, fpr + (l - s->name_len),
						s->name_len) != 0)
392 393 394 395 396
				continue;
			key_expired = k->subkeys[req->subkey].expired;
		}
		/* this key we look for. if it is acceptable, we are finished.
		   if it is not acceptable, we still have to look at the other
397 398 399
		   signatures, as a signature with another subkey is following
		 */
		switch (gpg_err_code(sig->status)) {
400
			case GPG_ERR_NO_ERROR:
401
				if (! key_expired)
402
					return true;
403 404
				if (req->allow_bad && IGNORABLE(expiredkey)) {
					if (verbose >= 0)
405 406 407 408 409 410 411
						fprintf(stderr,
"WARNING: valid signature in '%s' with parent-expired '%s' is accepted as requested!\n",
							releasegpg, fpr);
					return true;
				}
				fprintf(stderr,
"Not accepting valid signature in '%s' with parent-EXPIRED '%s'\n", releasegpg, fpr);
412
				if (verbose >= 0)
413 414 415 416 417
					fprintf(stderr,
"(To ignore it append a ! to the key and run reprepro with --ignore=expiredkey)\n");
				/* not accepted */
				continue;
			case GPG_ERR_KEY_EXPIRED:
418 419
				if (req->allow_bad && IGNORABLE(expiredkey)) {
					if (verbose >= 0)
420 421 422 423 424 425 426
						fprintf(stderr,
"WARNING: valid signature in '%s' with expired '%s' is accepted as requested!\n",
							releasegpg, fpr);
					return true;
				}
				fprintf(stderr,
"Not accepting valid signature in '%s' with EXPIRED '%s'\n", releasegpg, fpr);
427
				if (verbose >= 0)
428 429 430 431 432
					fprintf(stderr,
"(To ignore it append a ! to the key and run reprepro with --ignore=expiredkey)\n");
				/* not accepted */
				continue;
			case GPG_ERR_CERT_REVOKED:
433 434
				if (req->allow_bad && IGNORABLE(revokedkey)) {
					if (verbose >= 0)
435 436 437 438 439 440 441
						fprintf(stderr,
"WARNING: valid signature in '%s' with revoked '%s' is accepted as requested!\n",
							releasegpg, fpr);
					return RET_OK;
				}
				fprintf(stderr,
"Not accepting valid signature in '%s' with REVOKED '%s'\n", releasegpg, fpr);
442
				if (verbose >= 0)
443 444 445 446 447
					fprintf(stderr,
"(To ignore it append a ! to the key and run reprepro with --ignore=revokedkey)\n");
				/* not accepted */
				continue;
			case GPG_ERR_SIG_EXPIRED:
448 449
				if (req->allow_bad && IGNORABLE(expiredsignature)) {
					if (verbose >= 0)
450 451 452 453 454 455 456
						fprintf(stderr,
"WARNING: valid but expired signature in '%s' with '%s' is accepted as requested!\n",
							releasegpg, fpr);
					return RET_OK;
				}
				fprintf(stderr,
"Not accepting valid but EXPIRED signature in '%s' with '%s'\n", releasegpg, fpr);
457
				if (verbose >= 0)
458 459 460 461 462 463 464 465 466
					fprintf(stderr,
"(To ignore it append a ! to the key and run reprepro with --ignore=expiredsignature)\n");
				/* not accepted */
				continue;
			case GPG_ERR_BAD_SIGNATURE:
			case GPG_ERR_NO_PUBKEY:
				/* not accepted */
				continue;
			case GPG_ERR_GENERAL:
467 468 469 470 471 472 473 474 475
				if (release == NULL)
					fprintf(stderr,
"gpgme returned an general error verifing signature with '%s' in '%s'!\n"
"Try running gpg --verify '%s' manually for hints what is happening.\n"
"If this does not print any errors, retry the command causing this message.\n",
						fpr, releasegpg,
						releasegpg);
				else
					fprintf(stderr,
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
"gpgme returned an general error verifing signature with '%s' in '%s'!\n"
"Try running gpg --verify '%s' '%s' manually for hints what is happening.\n"
"If this does not print any errors, retry the command causing this message.\n",
						fpr, releasegpg,
						releasegpg, release);
				continue;
			/* there sadly no more is a way to make sure we have
			 * all possible ones handled */
			default:
				break;
		}
		fprintf(stderr,
"Error checking signature (gpgme returned unexpected value %d)!\n"
"Please file a bug report, so reprepro can handle this in the future.\n",
			gpg_err_code(sig->status));
		return false;
	}
	return false;
}

retvalue signature_requirement_add(struct signature_requirement **list_p, const char *condition) {
	struct signature_requirement *req;
	const char *full_condition = condition;
	retvalue r;

	r = signature_init(false);
502
	if (RET_WAS_ERROR(r))
503 504
		return r;

505
	if (condition == NULL || strcmp(condition, "blindtrust") == 0)
506 507 508
		return RET_NOTHING;

	/* no need to add the same condition multiple times */
509 510
	for (req = *list_p ; req != NULL ; req = req->next) {
		if (strcmp(req->condition, condition) == 0)
511 512 513 514
			return RET_NOTHING;
	}

	req = malloc(sizeof_requirement(1));
515
	if (FAILEDTOALLOC(req))
516 517 518
		return RET_ERROR_OOM;
	req->next = NULL;
	req->condition = strdup(condition);
519
	if (FAILEDTOALLOC(req->condition)) {
520 521 522 523 524 525
		free(req);
		return RET_ERROR_OOM;
	}
	req->num_keys = 0;
	do {
		bool allow_subkeys, allow_bad;
526
		char *next_key;
527 528 529 530

		r = parse_condition_part(&allow_subkeys, &allow_bad,
				full_condition, &condition, &next_key);
		ASSERT_NOT_NOTHING(r);
531
		if (RET_WAS_ERROR(r)) {
532 533 534 535 536 537 538 539 540
			signature_requirements_free(req);
			return r;
		}
		req->keys[req->num_keys].allow_bad = allow_bad;
		r = load_key(next_key, allow_subkeys, allow_bad,
				full_condition,
				&req->keys[req->num_keys].key,
				&req->keys[req->num_keys].subkey);
		free(next_key);
541
		if (RET_WAS_ERROR(r)) {
542 543 544 545 546
			signature_requirements_free(req);
			return r;
		}
		req->num_keys++;

547
		if (*condition != '\0') {
548 549 550
			struct signature_requirement *h;

			h = realloc(req, sizeof_requirement(req->num_keys+1));
551
			if (FAILEDTOALLOC(h)) {
552 553 554 555 556 557
				signature_requirements_free(req);
				return r;
			}
			req = h;
		} else
			break;
558
	} while (true);
559 560 561 562 563 564 565 566 567 568
	req->next = *list_p;
	*list_p = req;
	return RET_OK;
}

static void print_signatures(FILE *f, gpgme_signature_t s, const char *releasegpg) {
	char timebuffer[20];
	struct tm *tm;
	time_t t;

569
	if (s == NULL) {
570 571 572 573 574 575 576 577
		fprintf(f, "gpgme reported no signatures in '%s':\n"
"Either there are really none or something else is strange.\n"
"One known reason for this effect is forgeting -b when signing.\n",
				releasegpg);
		return;
	}

	fprintf(f, "Signatures in '%s':\n", releasegpg);
578
	for (; s != NULL ; s = s->next) {
579 580 581
		t = s->timestamp; tm = localtime(&t);
		strftime(timebuffer, 19, "%Y-%m-%d", tm);
		fprintf(f, "'%s' (signed %s): ", s->fpr, timebuffer);
582
		switch (gpg_err_code(s->status)) {
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
			case GPG_ERR_NO_ERROR:
				fprintf(f, "valid\n");
				continue;
			case GPG_ERR_KEY_EXPIRED:
				fprintf(f, "expired key\n");
				continue;
			case GPG_ERR_CERT_REVOKED:
				fprintf(f, "key revoced\n");
				continue;
			case GPG_ERR_SIG_EXPIRED:
				t = s->exp_timestamp; tm = localtime(&t);
				strftime(timebuffer, 19, "%Y-%m-%d", tm);
				fprintf(f, "expired signature (since %s)\n",
						timebuffer);
				continue;
			case GPG_ERR_BAD_SIGNATURE:
				fprintf(f, "bad signature\n");
				continue;
			case GPG_ERR_NO_PUBKEY:
				fprintf(f, "missing pubkey\n");
				continue;
			default:
				fprintf(f, "unknown\n");
				continue;
		}
	}
}

Bernhard Link's avatar
Bernhard Link committed
611
static inline retvalue verify_signature(const struct signature_requirement *requirements, const char *releasegpg, const char *releasename) {
612 613 614 615 616
	gpgme_verify_result_t result;
	int i;
	const struct signature_requirement *req;

	result = gpgme_op_verify_result(context);
617 618 619
	if (result == NULL) {
		fprintf(stderr,
"Internal error communicating with libgpgme: no result record!\n\n");
620 621 622
		return RET_ERROR_GPGME;
	}

623
	for (req = requirements ; req != NULL ; req = req->next) {
624
		bool fulfilled = false;
625 626 627 628 629

		/* check first for good signatures, and then for good enough
		   signatures, to not pester the user with warnings of one
		   of the alternate keys, if the last one is good enough */

630
		for (i = 0 ; (size_t)i < req->num_keys ; i++) {
631

632
			if (key_good(&req->keys[i], result->signatures)) {
633
				fulfilled = true;
634 635 636
				break;
			}
		}
637
		for (i = 0 ; !fulfilled && (size_t)i < req->num_keys ; i++) {
638

639
			if (key_good_enough(&req->keys[i], result->signatures,
640
						releasegpg, releasename)) {
641
				fulfilled = true;
642 643 644
				break;
			}
		}
645
		if (!fulfilled) {
646
			fprintf(stderr,
647
"ERROR: Condition '%s' not fulfilled for '%s'.\n",
648
					req->condition, releasegpg);
649 650
			print_signatures(stderr, result->signatures,
					releasegpg);
651 652
			return RET_ERROR_BADSIG;
		}
653
		if (verbose > 10) {
654
			fprintf(stdout, "Condition '%s' fulfilled for '%s'.\n",
655 656 657
					req->condition, releasegpg);
		}
	}
658
	if (verbose > 20)
659 660 661
		print_signatures(stdout, result->signatures, releasegpg);
	return RET_OK;
}
Bernhard Link's avatar
Bernhard Link committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

retvalue signature_check(const struct signature_requirement *requirements, const char *releasegpg, const char *releasename, const char *releasedata, size_t releaselen) {
	gpg_error_t err;
	int gpgfd;
	gpgme_data_t dh, dh_gpg;

	assert (requirements != NULL);

	if (FAILEDTOALLOC(releasedata) || FAILEDTOALLOC(releasegpg))
		return RET_ERROR_OOM;

	assert (context != NULL);

	/* Read the file and its signature into memory: */
	gpgfd = open(releasegpg, O_RDONLY|O_NOCTTY);
	if (gpgfd < 0) {
		int e = errno;
		fprintf(stderr, "Error opening '%s': %s\n",
				releasegpg, strerror(e));
		return RET_ERRNO(e);
	}
	err = gpgme_data_new_from_fd(&dh_gpg, gpgfd);
	if (err != 0) {
		(void)close(gpgfd);
		fprintf(stderr, "Error reading '%s':\n", releasegpg);
		return gpgerror(err);
	}
	err = gpgme_data_new_from_mem(&dh, releasedata, releaselen, 0);
	if (err != 0) {
		gpgme_data_release(dh_gpg);
		return gpgerror(err);
	}

	/* Verify the signature */

	err = gpgme_op_verify(context, dh_gpg, dh, NULL);
	gpgme_data_release(dh_gpg);
	gpgme_data_release(dh);
	close(gpgfd);
	if (err != 0) {
		fprintf(stderr, "Error verifying '%s':\n", releasegpg);
		return gpgerror(err);
	}

	return verify_signature(requirements, releasegpg, releasename);
}
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

retvalue signature_check_inline(const struct signature_requirement *requirements, const char *filename, char **chunk_p) {
	gpg_error_t err;
	gpgme_data_t dh, dh_gpg;
	int fd;

	fd = open(filename, O_RDONLY|O_NOCTTY);
	if (fd < 0) {
		int e = errno;
		fprintf(stderr, "Error opening '%s': %s\n",
				filename, strerror(e));
		return RET_ERRNO(e);
	}
	err = gpgme_data_new_from_fd(&dh_gpg, fd);
	if (err != 0) {
		(void)close(fd);
		return gpgerror(err);
	}

	err = gpgme_data_new(&dh);
	if (err != 0) {
		(void)close(fd);
		gpgme_data_release(dh_gpg);
		return gpgerror(err);
	}
	err = gpgme_op_verify(context, dh_gpg, NULL, dh);
	(void)close(fd);
	if (gpg_err_code(err) == GPG_ERR_NO_DATA) {
		char *chunk; const char *n;
		size_t len;
		retvalue r;

		gpgme_data_release(dh);
		gpgme_data_release(dh_gpg);

		r = readtextfile(filename, filename, &chunk, &len);
		assert (r != RET_NOTHING);
		if (RET_WAS_ERROR(r))
			return r;

		assert (chunk[len] == '\0');
		len = chunk_extract(chunk, chunk, len, false, &n);
		if (chunk[0] == '-' || *n != '\0') {
			fprintf(stderr,
"Cannot parse '%s': found no signature but does not looks safe to be assumed unsigned, either.\n",
				filename);
Bernhard Link's avatar
Bernhard Link committed
754
			free(chunk);
755 756
			return RET_ERROR;
		}
Bernhard Link's avatar
Bernhard Link committed
757 758
		if (requirements != NULL) {
			free(chunk);
759
			return RET_ERROR_BADSIG;
Bernhard Link's avatar
Bernhard Link committed
760
		}
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
		fprintf(stderr,
"WARNING: No signature found in %s, assuming it is unsigned!\n",
				filename);
		assert (chunk[len] == '\0');
		*chunk_p = realloc(chunk, len+1);
		if (FAILEDTOALLOC(*chunk_p))
			*chunk_p = chunk;
		return RET_OK;
	} else {
		char *plain_data, *chunk;
		const char *n;
		size_t plain_len, len;
		retvalue r;

		if (err != 0) {
			gpgme_data_release(dh_gpg);
			gpgme_data_release(dh);
			return gpgerror(err);
		}
		gpgme_data_release(dh_gpg);
		plain_data = gpgme_data_release_and_get_mem(dh, &plain_len);
		if (plain_data == NULL) {
			fprintf(stderr,
"Error: libgpgme failed to extract the plain data out of\n"
"'%s'.\n"
"While it did so in a way indicating running out of memory, experience says\n"
"this also happens when gpg returns a error code it does not understand.\n"
"To check this please try running gpg --verify '%s' manually.\n"
"Continuing extracting it ignoring all signatures...",
					filename, filename);
			return RET_ERROR;
		}
		chunk = malloc(plain_len+1);
		if (FAILEDTOALLOC(chunk))
			return RET_ERROR_OOM;
		len = chunk_extract(chunk, plain_data, plain_len, false, &n);
#ifdef HAVE_GPGPME_FREE
		gpgme_free(plain_data);
#else
		free(plain_data);
#endif
		assert (len <= plain_len);
803
		if (plain_len != (size_t)(n - plain_data)) {
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
			fprintf(stderr,
"Cannot parse '%s': extraced signed data looks malformed.\n",
				filename);
			r = RET_ERROR;
		} else
			r = verify_signature(requirements, filename, NULL);
		if (RET_IS_OK(r)) {
			*chunk_p = realloc(chunk, len+1);
			if (FAILEDTOALLOC(*chunk_p))
				*chunk_p = chunk;
		} else
			free(chunk);
		return r;
	}
}
819 820
#else /* HAVE_LIBGPGME */

821
retvalue signature_check(const struct signature_requirement *requirements, const char *releasegpg, const char *releasename, const char *releasedata, size_t releaselen) {
822
	assert (requirements != NULL);
823

824
	if (FAILEDTOALLOC(releasedata) || FAILEDTOALLOC(releasegpg))
825 826 827 828 829 830 831
		return RET_ERROR_OOM;
	fprintf(stderr,
"ERROR: Cannot check signatures as this reprepro binary is compiled with support\n"
"for libgpgme.\n"); // TODO: "Only running external programs is supported.\n"
	return RET_ERROR_GPGME;
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
retvalue signature_check_inline(const struct signature_requirement *requirements, const char *filename, char **chunk_p) {
	retvalue r;
	char *chunk; size_t len;
	const char *n;

	if (requirements != NULL) {
		fprintf(stderr,
"ERROR: Cannot check signatures as this reprepro binary is compiled with support\n"
"for libgpgme.\n");
		return RET_ERROR_GPGME;
	}
	r = readtextfile(filename, filename, &chunk, &len);
	assert (r != RET_NOTHING);
	if (RET_WAS_ERROR(r))
		return r;
	assert (chunk[len] == '\0');

	len = chunk_extract(chunk, chunk, len, false, &n);
	if (len == 0) {
		fprintf(stderr, "Could not find any data within '%s'!\n",
				filename);
		free(chunk);
		return RET_ERROR;
	}
	if (chunk[0] == '-') {
		const char *endmarker;

		if (len < 10 || memcmp(chunk, "-----BEGIN", 10) != 0) {
			fprintf(stderr,
"Strange content of '%s': First non-space character is '-',\n"
"but it does not begin with '-----BEGIN'.\n", filename);
			free(chunk);
			return RET_ERROR;
		}
		len = chunk_extract(chunk, n, strlen(n), false, &n);

		endmarker = strstr(chunk, "\n-----");
		if (endmarker != NULL) {
			endmarker++;
			assert ((size_t)(endmarker-chunk) < len);
			len = endmarker-chunk;
			chunk[len] = '\0';
		} else if (*n == '\0') {
			fprintf(stderr,
"ERROR: Could not find end marker of signed data within '%s'.\n"
"Cannot determine what is data and what is not!\n",
						filename);
				free(chunk);
				return RET_ERROR;
		} else if (strncmp(n, "-----", 5) != 0) {
			fprintf(stderr,
"ERROR: Spurious empty line within '%s'.\n"
"Cannot determine what is data and what is not!\n",
					filename);
			free(chunk);
			return RET_ERROR;
		}
	} else {
		if (*n != '\0') {
			fprintf(stderr,
"Cannot parse '%s': found no signature but does not looks safe to be assumed unsigned, either.\n",
					filename);
			return RET_ERROR;
		}
		fprintf(stderr,
"WARNING: No signature found in %s, assuming it is unsigned!\n",
				filename);
	}
	assert (chunk[len] == '\0');
	*chunk_p = realloc(chunk, len+1);
	if (FAILEDTOALLOC(*chunk_p))
		*chunk_p = chunk;
	return RET_OK;
}

907 908 909 910
void signature_requirements_free(/*@only@*/struct signature_requirement *p) {
	free(p);
}

911 912 913 914
retvalue signature_requirement_add(UNUSED(struct signature_requirement **x), const char *condition) {
	if (condition == NULL || strcmp(condition, "blindtrust") == 0)
		return RET_NOTHING;

915 916 917 918 919 920 921 922 923 924
	fprintf(stderr,
"ERROR: Cannot check signatures as this reprepro binary is compiled with support\n"
"for libgpgme.\n"); // TODO: "Only running external programs is supported.\n"
	return RET_ERROR_GPGME;
}

void free_known_keys(void) {
}

#endif /* HAVE_LIBGPGME */