pgp.c 13.1 KB
Newer Older
R
Roberto Sassu 已提交
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
/*
 * Copyright (C) 2011 Red Hat, Inc. All Rights Reserved.
 * Copyright (C) 2018 Huawei Technologies Duesseldorf GmbH
 *
 * Authors:
 *   David Howells <dhowells@redhat.com>
 *   Roberto Sassu <roberto.sassu@huawei.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 * File: pgp.c
 *      Parse PGP packets.
 */
#include <unistd.h>
#include <linux/kernel.h>
#include <sys/wait.h>
#include <sys/stat.h>

#include "pgp.h"

#define BIT_WORD(nr)               ((nr) / __BITS_PER_LONG)
#define BIT_MASK(nr)            (1UL << ((nr) % __BITS_PER_LONG))

/**
 * test_bit - Determine whether a bit is set
 * @nr: bit number to test
 * @addr: Address to start counting from
 */
static inline int test_bit(int nr, const volatile unsigned long *addr)
{
        return 1UL & (addr[BIT_WORD(nr)] >> (nr & (__BITS_PER_LONG-1)));
}

static inline void __set_bit(int nr, volatile unsigned long *addr)
{
        unsigned long mask = BIT_MASK(nr);
        unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

        *p  |= mask;
}

/**
 * pgp_parse_packet_header - Parse a PGP packet header
 * @_data: Start of the PGP packet (updated to PGP packet data)
 * @_datalen: Amount of data remaining in buffer (decreased)
 * @_type: Where the packet type will be returned
 * @_headerlen: Where the header length will be returned
 *
 * Parse a set of PGP packet header [RFC 4880: 4.2].
 *
 * Returns packet data size on success; non-zero on error.  If successful,
 * *_data and *_datalen will have been updated and *_headerlen will be set to
 * hold the length of the packet header.
 */
ssize_t pgp_parse_packet_header(const u8 **_data, size_t *_datalen,
R
Roberto Sassu 已提交
59 60
				enum pgp_packet_tag *_type,
				u8 *_headerlen)
R
Roberto Sassu 已提交
61
{
R
Roberto Sassu 已提交
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
	enum pgp_packet_tag type;
	const u8 *data = *_data;
	size_t size, datalen = *_datalen;

	pr_devel("-->pgp_parse_packet_header(,%zu,,)\n", datalen);

	if (datalen < 2)
		goto short_packet;

	pr_devel("pkthdr %02x, %02x\n", data[0], data[1]);

	type = *data++;
	datalen--;
	if (!(type & 0x80)) {
		pr_debug("Packet type does not have MSB set\n");
		return -EBADMSG;
	}
	type &= ~0x80;

	if (type & 0x40) {
		/* New packet length format */
		type &= ~0x40;
		pr_devel("new format: t=%u\n", type);
		switch (data[0]) {
		case 0x00 ... 0xbf:
			/* One-byte length */
			size = data[0];
			data++;
			datalen--;
			*_headerlen = 2;
			break;
		case 0xc0 ... 0xdf:
			/* Two-byte length */
			if (datalen < 2)
				goto short_packet;
			size = (data[0] - 192) * 256;
			size += data[1] + 192;
			data += 2;
			datalen -= 2;
			*_headerlen = 3;
			break;
		case 0xff:
			/* Five-byte length */
			if (datalen < 5)
				goto short_packet;
			size =  data[1] << 24;
			size |= data[2] << 16;
			size |= data[3] << 8;
			size |= data[4];
			data += 5;
			datalen -= 5;
			*_headerlen = 6;
			break;
		default:
			pr_debug("Partial body length packet not supported\n");
			return -EBADMSG;
		}
	} else {
		/* Old packet length format */
		u8 length_type = type & 0x03;
		type >>= 2;
		pr_devel("old format: t=%u lt=%u\n", type, length_type);

		switch (length_type) {
		case 0:
			/* One-byte length */
			size = data[0];
			data++;
			datalen--;
			*_headerlen = 2;
			break;
		case 1:
			/* Two-byte length */
			if (datalen < 2)
				goto short_packet;
			size  = data[0] << 8;
			size |= data[1];
			data += 2;
			datalen -= 2;
			*_headerlen = 3;
			break;
		case 2:
			/* Four-byte length */
			if (datalen < 4)
				goto short_packet;
			size  = data[0] << 24;
			size |= data[1] << 16;
			size |= data[2] << 8;
			size |= data[3];
			data += 4;
			datalen -= 4;
			*_headerlen = 5;
			break;
		default:
			pr_debug("Indefinite length packet not supported\n");
			return -EBADMSG;
		}
	}

	pr_devel("datalen=%zu size=%zu", datalen, size);
	if (datalen < size)
		goto short_packet;
	if ((int)size < 0)
		goto too_big;

	*_data = data;
	*_datalen = datalen;
	*_type = type;
	pr_devel("Found packet type=%u size=%zd\n", type, size);
	return size;
R
Roberto Sassu 已提交
172 173

short_packet:
R
Roberto Sassu 已提交
174 175
	pr_debug("Attempt to parse short packet\n");
	return -EBADMSG;
R
Roberto Sassu 已提交
176
too_big:
R
Roberto Sassu 已提交
177 178
	pr_debug("Signature subpacket size >2G\n");
	return -EMSGSIZE;
R
Roberto Sassu 已提交
179 180 181
}

struct pgp_parse_sig_params_ctx {
R
Roberto Sassu 已提交
182 183 184
	struct pgp_parse_sig_context base;
	struct pgp_sig_parameters *params;
	bool got_the_issuer;
R
Roberto Sassu 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
};

/**
 * pgp_parse_sig_subpkt_header - Parse a PGP V4 signature subpacket header
 * @_data: Start of the subpacket (updated to subpacket data)
 * @_datalen: Amount of data remaining in buffer (decreased)
 * @_type: Where the subpacket type will be returned
 *
 * Parse a PGP V4 signature subpacket header [RFC 4880: 5.2.3.1].
 *
 * Returns packet data size on success; non-zero on error.  If successful,
 * *_data and *_datalen will have been updated and *_headerlen will be set to
 * hold the length of the packet header.
 */
static ssize_t pgp_parse_sig_subpkt_header(const u8 **_data, size_t *_datalen,
R
Roberto Sassu 已提交
200
					   enum pgp_sig_subpkt_type *_type)
R
Roberto Sassu 已提交
201
{
R
Roberto Sassu 已提交
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
	enum pgp_sig_subpkt_type type;
	const u8 *data = *_data;
	size_t size, datalen = *_datalen;

	pr_devel("-->%s(,%zu,,)\n", __func__, datalen);

	if (datalen < 2)
		goto short_subpacket;

	pr_devel("subpkt hdr %02x, %02x\n", data[0], data[1]);

	switch (data[0]) {
	case 0x00 ... 0xbf:
		/* One-byte length */
		size = data[0];
		data++;
		datalen--;
		break;
	case 0xc0 ... 0xfe:
		/* Two-byte length */
		if (datalen < 3)
			goto short_subpacket;
		size = (data[0] - 192) * 256;
		size += data[1] + 192;
		data += 2;
		datalen -= 2;
		break;
	case 0xff:
		if (datalen < 6)
			goto short_subpacket;
		size  = data[1] << 24;
		size |= data[2] << 16;
		size |= data[3] << 8;
		size |= data[4];
		data += 5;
		datalen -= 5;
		break;
	}

	/* The type octet is included in the size */
	pr_devel("datalen=%zu size=%zu\n", datalen, size);
	if (datalen < size)
		goto short_subpacket;
	if (size == 0)
		goto very_short_subpacket;
	if ((int)size < 0)
		goto too_big;

	type = *data++ & ~PGP_SIG_SUBPKT_TYPE_CRITICAL_MASK;
	datalen--;
	size--;

	*_data = data;
	*_datalen = datalen;
	*_type = type;
	pr_devel("Found subpkt type=%u size=%zd\n", type, size);
	return size;
R
Roberto Sassu 已提交
259 260

very_short_subpacket:
R
Roberto Sassu 已提交
261 262
	pr_debug("Signature subpacket size can't be zero\n");
	return -EBADMSG;
R
Roberto Sassu 已提交
263
short_subpacket:
R
Roberto Sassu 已提交
264 265
	pr_debug("Attempt to parse short signature subpacket\n");
	return -EBADMSG;
R
Roberto Sassu 已提交
266
too_big:
R
Roberto Sassu 已提交
267 268
	pr_debug("Signature subpacket size >2G\n");
	return -EMSGSIZE;
R
Roberto Sassu 已提交
269 270 271 272 273 274 275 276 277 278 279
}

/**
 * pgp_parse_sig_subpkts - Parse a set of PGP V4 signatute subpackets
 * @_data: Data to be parsed (updated)
 * @_datalen: Amount of data (updated)
 * @ctx: Parsing context
 *
 * Parse a set of PGP signature subpackets [RFC 4880: 5.2.3].
 */
static int pgp_parse_sig_subpkts(const u8 *data, size_t datalen,
R
Roberto Sassu 已提交
280
				 struct pgp_parse_sig_context *ctx)
R
Roberto Sassu 已提交
281
{
R
Roberto Sassu 已提交
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
	enum pgp_sig_subpkt_type type;
	ssize_t pktlen;
	int ret;

	pr_devel("-->%s(,%zu,,)\n", __func__, datalen);

	while (datalen > 2) {
		pktlen = pgp_parse_sig_subpkt_header(&data, &datalen, &type);
		if (pktlen < 0)
			return pktlen;
		if (test_bit(type, ctx->types_of_interest)) {
			ret = ctx->process_packet(ctx, type, data, pktlen);
			if (ret < 0)
				return ret;
		}
		data += pktlen;
		datalen -= pktlen;
	}

	if (datalen != 0) {
		pr_debug("Excess octets in signature subpacket stream\n");
		return -EBADMSG;
	}

	return 0;
R
Roberto Sassu 已提交
307 308 309 310 311 312
}

/*
 * Process a V4 signature subpacket.
 */
static int pgp_process_sig_params_subpkt(struct pgp_parse_sig_context *context,
R
Roberto Sassu 已提交
313 314 315
					 enum pgp_sig_subpkt_type type,
					 const u8 *data,
					 size_t datalen)
R
Roberto Sassu 已提交
316
{
R
Roberto Sassu 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	struct pgp_parse_sig_params_ctx *ctx =
		container_of(context, struct pgp_parse_sig_params_ctx, base);

	if (ctx->got_the_issuer) {
		pr_debug("V4 signature packet has multiple issuers\n");
		return -EBADMSG;
	}

	if (datalen != 8) {
		pr_debug("V4 signature issuer subpkt not 8 long (%zu)\n",
			   datalen);
		return -EBADMSG;
	}

	memcpy(&ctx->params->issuer, data, 8);
	ctx->got_the_issuer = true;
	return 0;
R
Roberto Sassu 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

/**
 * pgp_parse_sig_params - Parse basic parameters from a PGP signature packet
 * @_data: Content of packet (updated)
 * @_datalen: Length of packet remaining (updated)
 * @p: The basic parameters
 *
 * Parse the basic parameters from a PGP signature packet [RFC 4880: 5.2] that
 * are needed to start off a signature verification operation.  The only ones
 * actually necessary are the signature type (which affects how the data is
 * transformed) and the hash algorithm.
 *
 * We also extract the public key algorithm and the issuer's key ID as we'll
 * need those to determine if we actually have the public key available.  If
 * not, then we can't verify the signature anyway.
 *
 * Returns 0 if successful or a negative error code.  *_data and *_datalen are
 * updated to point to the 16-bit subset of the hash value and the set of MPIs.
 */
int pgp_parse_sig_params(const u8 **_data, size_t *_datalen,
R
Roberto Sassu 已提交
355 356
			 struct pgp_sig_parameters *p,
			 const u8 **hashed, size_t *hashedlen)
R
Roberto Sassu 已提交
357
{
R
Roberto Sassu 已提交
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
	const u8 *data = *_data;
	size_t datalen = *_datalen;
	int ret;

	pr_devel("-->%s(,%zu,,)\n", __func__, datalen);

	if (datalen < 1)
		return -EBADMSG;
	p->version = *data;

	if (p->version == PGP_SIG_VERSION_3) {
		const struct pgp_signature_v3_packet *v3 = (const void *)data;

		if (datalen < sizeof(*v3)) {
			pr_debug("Short V3 signature packet\n");
			return -EBADMSG;
		}

		*hashedlen = v3->length_of_hashed;
		*hashed = (u8 *)&v3->hashed;

		datalen -= sizeof(*v3);
		data += sizeof(*v3);

		/* V3 has everything we need in the header */
		p->signature_type = v3->hashed.signature_type;
		memcpy(&p->issuer, &v3->issuer, 8);
		p->pubkey_algo = v3->pubkey_algo;
		p->hash_algo = v3->hash_algo;

	} else if (p->version == PGP_SIG_VERSION_4) {
		const struct pgp_signature_v4_packet *v4 = (const void *)data;
		struct pgp_parse_sig_params_ctx ctx = {
			.base.process_packet = pgp_process_sig_params_subpkt,
			.params = p,
			.got_the_issuer = false,
		};
		size_t subdatalen;

		if (datalen < sizeof(*v4) + 2 + 2 + 2) {
			pr_debug("Short V4 signature packet\n");
			return -EBADMSG;
		}
		datalen -= sizeof(*v4);
		data += sizeof(*v4);

		/* V4 has most things in the header... */
		p->signature_type = v4->signature_type;
		p->pubkey_algo = v4->pubkey_algo;
		p->hash_algo = v4->hash_algo;

		/* ... but we have to get the key ID from the subpackets, of
		 * which there are two sets.
		 */
		__set_bit(PGP_SIG_ISSUER, ctx.base.types_of_interest);

		subdatalen  = *data++ << 8;
		subdatalen |= *data++;
		datalen -= 2;

		*hashedlen = 4 + 2 + subdatalen;
		*hashed = *_data;

		if (subdatalen) {
			/* Hashed subpackets */
			pr_devel("hashed data: %zu (after %zu)\n",
				 subdatalen, sizeof(*v4));
			if (subdatalen > datalen + 2 + 2) {
				pr_debug("Short V4 signature packet [hdata]\n");
				return -EBADMSG;
			}
			ret = pgp_parse_sig_subpkts(data, subdatalen,
						    &ctx.base);
			if (ret < 0)
				return ret;
			data += subdatalen;
			datalen -= subdatalen;
		}

		subdatalen  = *data++ << 8;
		subdatalen |= *data++;
		datalen -= 2;
		if (subdatalen) {
			/* Unhashed subpackets */
			pr_devel("unhashed data: %zu\n", subdatalen);
			if (subdatalen > datalen + 2) {
				pr_debug("Short V4 signature packet [udata]\n");
				return -EBADMSG;
			}
			ret = pgp_parse_sig_subpkts(data, subdatalen,
						    &ctx.base);
			if (ret < 0)
				return ret;
			data += subdatalen;
			datalen -= subdatalen;
		}

		if (!ctx.got_the_issuer) {
			pr_debug("V4 signature packet lacks issuer\n");
			return -EBADMSG;
		}
	} else {
		pr_debug("Signature packet with unhandled version %d\n",
			 p->version);
		return -EBADMSG;
	}

	*_data = data;
	*_datalen = datalen;
	return 0;
R
Roberto Sassu 已提交
468 469 470
}

int pgp_get_signature_data(const u8 *signature, size_t signature_len,
R
Roberto Sassu 已提交
471 472
			   u8 **data, size_t *data_len, u8 **sig,
			   size_t *sig_len, u8 **issuer, u16 *algo)
R
Roberto Sassu 已提交
473
{
R
Roberto Sassu 已提交
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
	unsigned int nbytes, nbytes_alloc;
	enum pgp_packet_tag type;
	ssize_t pktlen;
	u8 headerlen;
	struct pgp_sig_parameters p;
	const u8 *hashed;
	size_t hashedlen;
	u8 trailer[6];
	int ret;

	*data = NULL;

	pktlen = pgp_parse_packet_header((const u8 **)&signature,
					 &signature_len, &type, &headerlen);
	if (pktlen < 0)
		return pktlen;

	ret = pgp_parse_sig_params(&signature, &signature_len, &p,
				   &hashed, &hashedlen);
	if (ret < 0)
		return ret;

	if (p.version == 3) {
		*data_len = hashedlen;
		*data = malloc(hashedlen);
		if (*data == NULL)
			return -ENOMEM;

		memcpy(*data, hashed, hashedlen);
	} else if (p.version == 4) {
		trailer[0] = p.version;
		trailer[1] = 0xffU;
		trailer[2] = hashedlen >> 24;
		trailer[3] = hashedlen >> 16;
		trailer[4] = hashedlen >> 8;
		trailer[5] = hashedlen;

		*data_len = hashedlen + sizeof(trailer);
		*data = malloc(hashedlen + 6);
		if (*data == NULL)
			return -ENOMEM;

		memcpy(*data, hashed, hashedlen);
		memcpy(*data + hashedlen, trailer, 6);
	}

	*algo = p.hash_algo;

	*issuer = malloc(4);
	if (!*issuer) {
		ret = -ENOMEM;
		goto out;
	}

	memcpy(*issuer, p.issuer.id + sizeof(uint32_t), sizeof(uint32_t));

	signature += 2;
	signature_len -= 2;

	nbytes = signature_len - 2;
	nbytes_alloc = __KERNEL_DIV_ROUND_UP(nbytes, 8) * 8;

	*sig = calloc(nbytes_alloc, sizeof(u8));
	if (!*sig) {
		ret = -ENOMEM;
		goto out;
	}

	memcpy(*sig + nbytes_alloc - nbytes, signature + 2, nbytes);
	*sig_len = nbytes_alloc;

	ret = 0;
R
Roberto Sassu 已提交
546
out:
R
Roberto Sassu 已提交
547 548 549 550
	if (ret < 0) {
		free(*data);
		free(*issuer);
	}
R
Roberto Sassu 已提交
551

R
Roberto Sassu 已提交
552
	return ret;
R
Roberto Sassu 已提交
553
}