rxkad.c 29.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* Kerberos-based RxRPC security
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.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; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

H
Herbert Xu 已提交
14
#include <crypto/skcipher.h>
15 16 17 18 19 20
#include <linux/module.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/scatterlist.h>
#include <linux/ctype.h>
21
#include <linux/slab.h>
22 23
#include <net/sock.h>
#include <net/af_rxrpc.h>
24
#include <keys/rxrpc-type.h>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "ar-internal.h"

#define RXKAD_VERSION			2
#define MAXKRB5TICKETLEN		1024
#define RXKAD_TKT_TYPE_KERBEROS_V5	256
#define ANAME_SZ			40	/* size of authentication name */
#define INST_SZ				40	/* size of principal's instance */
#define REALM_SZ			40	/* size of principal's auth domain */
#define SNAME_SZ			40	/* size of service name */

struct rxkad_level1_hdr {
	__be32	data_size;	/* true data size (excluding padding) */
};

struct rxkad_level2_hdr {
	__be32	data_size;	/* true data size (excluding padding) */
	__be32	checksum;	/* decrypted data checksum */
};

/*
 * this holds a pinned cipher so that keventd doesn't get called by the cipher
 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
 * packets
 */
H
Herbert Xu 已提交
49
static struct crypto_skcipher *rxkad_ci;
50 51 52 53 54 55 56
static DEFINE_MUTEX(rxkad_ci_mutex);

/*
 * initialise connection security
 */
static int rxkad_init_connection_security(struct rxrpc_connection *conn)
{
H
Herbert Xu 已提交
57
	struct crypto_skcipher *ci;
58
	struct rxrpc_key_token *token;
59 60
	int ret;

61
	_enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key));
62

63
	token = conn->params.key->payload.data[0];
64
	conn->security_ix = token->security_index;
65

H
Herbert Xu 已提交
66
	ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
67 68 69 70 71 72
	if (IS_ERR(ci)) {
		_debug("no cipher");
		ret = PTR_ERR(ci);
		goto error;
	}

H
Herbert Xu 已提交
73 74
	if (crypto_skcipher_setkey(ci, token->kad->session_key,
				   sizeof(token->kad->session_key)) < 0)
75 76
		BUG();

77
	switch (conn->params.security_level) {
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
	case RXRPC_SECURITY_PLAIN:
		break;
	case RXRPC_SECURITY_AUTH:
		conn->size_align = 8;
		conn->security_size = sizeof(struct rxkad_level1_hdr);
		conn->header_size += sizeof(struct rxkad_level1_hdr);
		break;
	case RXRPC_SECURITY_ENCRYPT:
		conn->size_align = 8;
		conn->security_size = sizeof(struct rxkad_level2_hdr);
		conn->header_size += sizeof(struct rxkad_level2_hdr);
		break;
	default:
		ret = -EKEYREJECTED;
		goto error;
	}

	conn->cipher = ci;
	ret = 0;
error:
	_leave(" = %d", ret);
	return ret;
}

/*
 * prime the encryption state with the invariant parts of a connection's
 * description
 */
106
static int rxkad_prime_packet_security(struct rxrpc_connection *conn)
107
{
108
	struct rxrpc_key_token *token;
H
Herbert Xu 已提交
109
	SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
110
	struct scatterlist sg;
111
	struct rxrpc_crypt iv;
112 113
	__be32 *tmpbuf;
	size_t tmpsize = 4 * sizeof(__be32);
114 115 116

	_enter("");

117
	if (!conn->params.key)
118 119 120 121 122
		return 0;

	tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
	if (!tmpbuf)
		return -ENOMEM;
123

124
	token = conn->params.key->payload.data[0];
125
	memcpy(&iv, token->kad->session_key, sizeof(iv));
126

127 128 129 130
	tmpbuf[0] = htonl(conn->proto.epoch);
	tmpbuf[1] = htonl(conn->proto.cid);
	tmpbuf[2] = 0;
	tmpbuf[3] = htonl(conn->security_ix);
H
Herbert Xu 已提交
131

132
	sg_init_one(&sg, tmpbuf, tmpsize);
H
Herbert Xu 已提交
133 134
	skcipher_request_set_tfm(req, conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
135
	skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
H
Herbert Xu 已提交
136 137
	crypto_skcipher_encrypt(req);
	skcipher_request_zero(req);
138

139 140 141 142
	memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv));
	kfree(tmpbuf);
	_leave(" = 0");
	return 0;
143 144 145 146 147 148 149 150 151 152 153
}

/*
 * partially encrypt a packet (level 1 security)
 */
static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
				    struct sk_buff *skb,
				    u32 data_size,
				    void *sechdr)
{
	struct rxrpc_skb_priv *sp;
H
Herbert Xu 已提交
154
	SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
155
	struct rxkad_level1_hdr hdr;
156
	struct rxrpc_crypt iv;
157
	struct scatterlist sg;
158 159 160 161 162 163
	u16 check;

	sp = rxrpc_skb(skb);

	_enter("");

164 165
	check = sp->hdr.seq ^ sp->hdr.callNumber;
	data_size |= (u32)check << 16;
166

167 168
	hdr.data_size = htonl(data_size);
	memcpy(sechdr, &hdr, sizeof(hdr));
169 170 171 172

	/* start the encryption afresh */
	memset(&iv, 0, sizeof(iv));

173
	sg_init_one(&sg, sechdr, 8);
H
Herbert Xu 已提交
174 175
	skcipher_request_set_tfm(req, call->conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
176
	skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
H
Herbert Xu 已提交
177 178
	crypto_skcipher_encrypt(req);
	skcipher_request_zero(req);
179 180 181 182 183 184 185 186 187

	_leave(" = 0");
	return 0;
}

/*
 * wholly encrypt a packet (level 2 security)
 */
static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
188 189 190
				       struct sk_buff *skb,
				       u32 data_size,
				       void *sechdr)
191
{
192
	const struct rxrpc_key_token *token;
193
	struct rxkad_level2_hdr rxkhdr;
194
	struct rxrpc_skb_priv *sp;
H
Herbert Xu 已提交
195
	SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
196 197 198
	struct rxrpc_crypt iv;
	struct scatterlist sg[16];
	struct sk_buff *trailer;
199
	unsigned int len;
200 201
	u16 check;
	int nsg;
H
Herbert Xu 已提交
202
	int err;
203 204 205 206 207

	sp = rxrpc_skb(skb);

	_enter("");

208
	check = sp->hdr.seq ^ sp->hdr.callNumber;
209

210
	rxkhdr.data_size = htonl(data_size | (u32)check << 16);
211
	rxkhdr.checksum = 0;
212
	memcpy(sechdr, &rxkhdr, sizeof(rxkhdr));
213 214

	/* encrypt from the session key */
215
	token = call->conn->params.key->payload.data[0];
216
	memcpy(&iv, token->kad->session_key, sizeof(iv));
217

218
	sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
H
Herbert Xu 已提交
219 220
	skcipher_request_set_tfm(req, call->conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
221
	skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
H
Herbert Xu 已提交
222
	crypto_skcipher_encrypt(req);
223 224 225

	/* we want to encrypt the skbuff in-place */
	nsg = skb_cow_data(skb, 0, &trailer);
H
Herbert Xu 已提交
226
	err = -ENOMEM;
227
	if (nsg < 0 || nsg > 16)
H
Herbert Xu 已提交
228
		goto out;
229 230 231 232

	len = data_size + call->conn->size_align - 1;
	len &= ~(call->conn->size_align - 1);

233 234
	sg_init_table(sg, nsg);
	skb_to_sgvec(skb, sg, 0, len);
H
Herbert Xu 已提交
235 236
	skcipher_request_set_crypt(req, sg, sg, len, iv.x);
	crypto_skcipher_encrypt(req);
237 238

	_leave(" = 0");
H
Herbert Xu 已提交
239 240 241 242 243
	err = 0;

out:
	skcipher_request_zero(req);
	return err;
244 245 246 247 248
}

/*
 * checksum an RxRPC packet header
 */
249
static int rxkad_secure_packet(struct rxrpc_call *call,
250 251 252
			       struct sk_buff *skb,
			       size_t data_size,
			       void *sechdr)
253 254
{
	struct rxrpc_skb_priv *sp;
H
Herbert Xu 已提交
255
	SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
256
	struct rxrpc_crypt iv;
257
	struct scatterlist sg;
258
	u32 x, y;
259 260 261 262 263
	int ret;

	sp = rxrpc_skb(skb);

	_enter("{%d{%x}},{#%u},%zu,",
264 265
	       call->debug_id, key_serial(call->conn->params.key),
	       sp->hdr.seq, data_size);
266 267 268 269

	if (!call->conn->cipher)
		return 0;

270
	ret = key_validate(call->conn->params.key);
271 272 273 274 275 276 277
	if (ret < 0)
		return ret;

	/* continue encrypting from where we left off */
	memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));

	/* calculate the security checksum */
278
	x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
279
	x |= sp->hdr.seq & 0x3fffffff;
280 281
	call->crypto_buf[0] = htonl(sp->hdr.callNumber);
	call->crypto_buf[1] = htonl(x);
H
Herbert Xu 已提交
282

283
	sg_init_one(&sg, call->crypto_buf, 8);
H
Herbert Xu 已提交
284 285
	skcipher_request_set_tfm(req, call->conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
286
	skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
H
Herbert Xu 已提交
287 288
	crypto_skcipher_encrypt(req);
	skcipher_request_zero(req);
289

290
	y = ntohl(call->crypto_buf[1]);
A
Al Viro 已提交
291 292 293
	y = (y >> 16) & 0xffff;
	if (y == 0)
		y = 1; /* zero checksums are not permitted */
294
	sp->hdr.cksum = y;
295

296
	switch (call->conn->params.security_level) {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	case RXRPC_SECURITY_PLAIN:
		ret = 0;
		break;
	case RXRPC_SECURITY_AUTH:
		ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
		break;
	case RXRPC_SECURITY_ENCRYPT:
		ret = rxkad_secure_packet_encrypt(call, skb, data_size,
						  sechdr);
		break;
	default:
		ret = -EPERM;
		break;
	}

A
Al Viro 已提交
312
	_leave(" = %d [set %hx]", ret, y);
313 314 315 316 317 318
	return ret;
}

/*
 * decrypt partial encryption on a packet (level 1 security)
 */
319
static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
320
				 unsigned int offset, unsigned int len,
321
				 rxrpc_seq_t seq)
322 323
{
	struct rxkad_level1_hdr sechdr;
H
Herbert Xu 已提交
324
	SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
325
	struct rxrpc_crypt iv;
326
	struct scatterlist sg[16];
327 328 329
	struct sk_buff *trailer;
	u32 data_size, buf;
	u16 check;
330
	int nsg;
331 332 333

	_enter("");

334
	if (len < 8) {
335 336 337
		rxrpc_abort_call("V1H", call, seq, RXKADSEALEDINCON, EPROTO);
		goto protocol_error;
	}
338

339 340 341
	/* Decrypt the skbuff in-place.  TODO: We really want to decrypt
	 * directly into the target buffer.
	 */
342 343
	nsg = skb_cow_data(skb, 0, &trailer);
	if (nsg < 0 || nsg > 16)
344 345
		goto nomem;

346
	sg_init_table(sg, nsg);
347
	skb_to_sgvec(skb, sg, offset, 8);
348 349 350 351

	/* start the decryption afresh */
	memset(&iv, 0, sizeof(iv));

H
Herbert Xu 已提交
352 353 354 355 356
	skcipher_request_set_tfm(req, call->conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
	skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
	crypto_skcipher_decrypt(req);
	skcipher_request_zero(req);
357

358
	/* Extract the decrypted packet length */
359
	if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
360 361 362
		rxrpc_abort_call("XV1", call, seq, RXKADDATALEN, EPROTO);
		goto protocol_error;
	}
363 364
	offset += sizeof(sechdr);
	len -= sizeof(sechdr);
365 366 367 368 369

	buf = ntohl(sechdr.data_size);
	data_size = buf & 0xffff;

	check = buf >> 16;
370
	check ^= seq ^ call->call_id;
371 372
	check &= 0xffff;
	if (check != 0) {
373
		rxrpc_abort_call("V1C", call, seq, RXKADSEALEDINCON, EPROTO);
374 375 376
		goto protocol_error;
	}

377
	if (data_size > len) {
378 379 380
		rxrpc_abort_call("V1L", call, seq, RXKADDATALEN, EPROTO);
		goto protocol_error;
	}
381 382 383 384 385

	_leave(" = 0 [dlen=%x]", data_size);
	return 0;

protocol_error:
386
	rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
387 388 389 390 391 392 393 394 395 396 397
	_leave(" = -EPROTO");
	return -EPROTO;

nomem:
	_leave(" = -ENOMEM");
	return -ENOMEM;
}

/*
 * wholly decrypt a packet (level 2 security)
 */
398
static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
399
				 unsigned int offset, unsigned int len,
400
				 rxrpc_seq_t seq)
401
{
402
	const struct rxrpc_key_token *token;
403
	struct rxkad_level2_hdr sechdr;
H
Herbert Xu 已提交
404
	SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
405 406 407 408 409 410 411 412 413
	struct rxrpc_crypt iv;
	struct scatterlist _sg[4], *sg;
	struct sk_buff *trailer;
	u32 data_size, buf;
	u16 check;
	int nsg;

	_enter(",{%d}", skb->len);

414
	if (len < 8) {
415 416 417
		rxrpc_abort_call("V2H", call, seq, RXKADSEALEDINCON, EPROTO);
		goto protocol_error;
	}
418

419 420 421
	/* Decrypt the skbuff in-place.  TODO: We really want to decrypt
	 * directly into the target buffer.
	 */
422 423 424 425 426 427 428 429 430 431 432
	nsg = skb_cow_data(skb, 0, &trailer);
	if (nsg < 0)
		goto nomem;

	sg = _sg;
	if (unlikely(nsg > 4)) {
		sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
		if (!sg)
			goto nomem;
	}

433
	sg_init_table(sg, nsg);
434
	skb_to_sgvec(skb, sg, offset, len);
435 436

	/* decrypt from the session key */
437
	token = call->conn->params.key->payload.data[0];
438
	memcpy(&iv, token->kad->session_key, sizeof(iv));
439

H
Herbert Xu 已提交
440 441
	skcipher_request_set_tfm(req, call->conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
442
	skcipher_request_set_crypt(req, sg, sg, len, iv.x);
H
Herbert Xu 已提交
443 444
	crypto_skcipher_decrypt(req);
	skcipher_request_zero(req);
445 446 447
	if (sg != _sg)
		kfree(sg);

448
	/* Extract the decrypted packet length */
449
	if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
450 451 452
		rxrpc_abort_call("XV2", call, seq, RXKADDATALEN, EPROTO);
		goto protocol_error;
	}
453 454
	offset += sizeof(sechdr);
	len -= sizeof(sechdr);
455 456 457 458 459

	buf = ntohl(sechdr.data_size);
	data_size = buf & 0xffff;

	check = buf >> 16;
460
	check ^= seq ^ call->call_id;
461 462
	check &= 0xffff;
	if (check != 0) {
463
		rxrpc_abort_call("V2C", call, seq, RXKADSEALEDINCON, EPROTO);
464 465 466
		goto protocol_error;
	}

467
	if (data_size > len) {
468 469 470
		rxrpc_abort_call("V2L", call, seq, RXKADDATALEN, EPROTO);
		goto protocol_error;
	}
471 472 473 474 475

	_leave(" = 0 [dlen=%x]", data_size);
	return 0;

protocol_error:
476
	rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
477 478 479 480 481 482 483 484 485
	_leave(" = -EPROTO");
	return -EPROTO;

nomem:
	_leave(" = -ENOMEM");
	return -ENOMEM;
}

/*
486 487
 * Verify the security on a received packet or subpacket (if part of a
 * jumbo packet).
488
 */
489
static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
490
			       unsigned int offset, unsigned int len,
491
			       rxrpc_seq_t seq, u16 expected_cksum)
492
{
H
Herbert Xu 已提交
493
	SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
494
	struct rxrpc_crypt iv;
495
	struct scatterlist sg;
496 497
	u16 cksum;
	u32 x, y;
498 499

	_enter("{%d{%x}},{#%u}",
500
	       call->debug_id, key_serial(call->conn->params.key), seq);
501 502 503 504 505 506 507 508

	if (!call->conn->cipher)
		return 0;

	/* continue encrypting from where we left off */
	memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));

	/* validate the security checksum */
509
	x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
510
	x |= seq & 0x3fffffff;
511 512
	call->crypto_buf[0] = htonl(call->call_id);
	call->crypto_buf[1] = htonl(x);
H
Herbert Xu 已提交
513

514
	sg_init_one(&sg, call->crypto_buf, 8);
H
Herbert Xu 已提交
515 516
	skcipher_request_set_tfm(req, call->conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
517
	skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
H
Herbert Xu 已提交
518 519
	crypto_skcipher_encrypt(req);
	skcipher_request_zero(req);
520

521
	y = ntohl(call->crypto_buf[1]);
522 523 524
	cksum = (y >> 16) & 0xffff;
	if (cksum == 0)
		cksum = 1; /* zero checksums are not permitted */
525

526 527
	if (cksum != expected_cksum) {
		rxrpc_abort_call("VCK", call, seq, RXKADSEALEDINCON, EPROTO);
528
		rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
529 530 531 532
		_leave(" = -EPROTO [csum failed]");
		return -EPROTO;
	}

533
	switch (call->conn->params.security_level) {
534
	case RXRPC_SECURITY_PLAIN:
535
		return 0;
536
	case RXRPC_SECURITY_AUTH:
537
		return rxkad_verify_packet_1(call, skb, offset, len, seq);
538
	case RXRPC_SECURITY_ENCRYPT:
539
		return rxkad_verify_packet_2(call, skb, offset, len, seq);
540
	default:
541
		return -ENOANO;
542 543 544
	}
}

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
/*
 * Locate the data contained in a packet that was partially encrypted.
 */
static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb,
				unsigned int *_offset, unsigned int *_len)
{
	struct rxkad_level1_hdr sechdr;

	if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
		BUG();
	*_offset += sizeof(sechdr);
	*_len = ntohl(sechdr.data_size) & 0xffff;
}

/*
 * Locate the data contained in a packet that was completely encrypted.
 */
static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb,
				unsigned int *_offset, unsigned int *_len)
{
	struct rxkad_level2_hdr sechdr;

	if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
		BUG();
	*_offset += sizeof(sechdr);
	*_len = ntohl(sechdr.data_size) & 0xffff;
}

/*
 * Locate the data contained in an already decrypted packet.
 */
static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
			      unsigned int *_offset, unsigned int *_len)
{
	switch (call->conn->params.security_level) {
	case RXRPC_SECURITY_AUTH:
		rxkad_locate_data_1(call, skb, _offset, _len);
		return;
	case RXRPC_SECURITY_ENCRYPT:
		rxkad_locate_data_2(call, skb, _offset, _len);
		return;
	default:
		return;
	}
}

591 592 593 594 595 596
/*
 * issue a challenge
 */
static int rxkad_issue_challenge(struct rxrpc_connection *conn)
{
	struct rxkad_challenge challenge;
597
	struct rxrpc_wire_header whdr;
598 599 600
	struct msghdr msg;
	struct kvec iov[2];
	size_t len;
601
	u32 serial;
602 603
	int ret;

604
	_enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
605

606
	ret = key_validate(conn->params.key);
607 608 609 610 611 612 613 614 615 616
	if (ret < 0)
		return ret;

	get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));

	challenge.version	= htonl(2);
	challenge.nonce		= htonl(conn->security_nonce);
	challenge.min_level	= htonl(0);
	challenge.__padding	= 0;

617 618
	msg.msg_name	= &conn->params.peer->srx.transport.sin;
	msg.msg_namelen	= sizeof(conn->params.peer->srx.transport.sin);
619 620 621 622
	msg.msg_control	= NULL;
	msg.msg_controllen = 0;
	msg.msg_flags	= 0;

623 624
	whdr.epoch	= htonl(conn->proto.epoch);
	whdr.cid	= htonl(conn->proto.cid);
625 626 627 628 629 630 631
	whdr.callNumber	= 0;
	whdr.seq	= 0;
	whdr.type	= RXRPC_PACKET_TYPE_CHALLENGE;
	whdr.flags	= conn->out_clientflag;
	whdr.userStatus	= 0;
	whdr.securityIndex = conn->security_ix;
	whdr._rsvd	= 0;
632
	whdr.serviceId	= htons(conn->params.service_id);
633 634 635

	iov[0].iov_base	= &whdr;
	iov[0].iov_len	= sizeof(whdr);
636 637 638 639 640
	iov[1].iov_base	= &challenge;
	iov[1].iov_len	= sizeof(challenge);

	len = iov[0].iov_len + iov[1].iov_len;

641 642 643
	serial = atomic_inc_return(&conn->serial);
	whdr.serial = htonl(serial);
	_proto("Tx CHALLENGE %%%u", serial);
644

645
	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
646 647 648 649 650 651 652 653 654 655 656 657 658
	if (ret < 0) {
		_debug("sendmsg failed: %d", ret);
		return -EAGAIN;
	}

	_leave(" = 0");
	return 0;
}

/*
 * send a Kerberos security response
 */
static int rxkad_send_response(struct rxrpc_connection *conn,
659
			       struct rxrpc_host_header *hdr,
660 661 662
			       struct rxkad_response *resp,
			       const struct rxkad_key *s2)
{
663
	struct rxrpc_wire_header whdr;
664 665 666
	struct msghdr msg;
	struct kvec iov[3];
	size_t len;
667
	u32 serial;
668 669 670 671
	int ret;

	_enter("");

672 673
	msg.msg_name	= &conn->params.peer->srx.transport.sin;
	msg.msg_namelen	= sizeof(conn->params.peer->srx.transport.sin);
674 675 676 677
	msg.msg_control	= NULL;
	msg.msg_controllen = 0;
	msg.msg_flags	= 0;

678 679 680 681 682 683 684
	memset(&whdr, 0, sizeof(whdr));
	whdr.epoch	= htonl(hdr->epoch);
	whdr.cid	= htonl(hdr->cid);
	whdr.type	= RXRPC_PACKET_TYPE_RESPONSE;
	whdr.flags	= conn->out_clientflag;
	whdr.securityIndex = hdr->securityIndex;
	whdr.serviceId	= htons(hdr->serviceId);
685

686 687
	iov[0].iov_base	= &whdr;
	iov[0].iov_len	= sizeof(whdr);
688 689
	iov[1].iov_base	= resp;
	iov[1].iov_len	= sizeof(*resp);
690
	iov[2].iov_base	= (void *)s2->ticket;
691 692 693 694
	iov[2].iov_len	= s2->ticket_len;

	len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;

695 696 697
	serial = atomic_inc_return(&conn->serial);
	whdr.serial = htonl(serial);
	_proto("Tx RESPONSE %%%u", serial);
698

699
	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
	if (ret < 0) {
		_debug("sendmsg failed: %d", ret);
		return -EAGAIN;
	}

	_leave(" = 0");
	return 0;
}

/*
 * calculate the response checksum
 */
static void rxkad_calc_response_checksum(struct rxkad_response *response)
{
	u32 csum = 1000003;
	int loop;
	u8 *p = (u8 *) response;

	for (loop = sizeof(*response); loop > 0; loop--)
		csum = csum * 0x10204081 + *p++;

	response->encrypted.checksum = htonl(csum);
}

/*
 * encrypt the response packet
 */
static void rxkad_encrypt_response(struct rxrpc_connection *conn,
				   struct rxkad_response *resp,
				   const struct rxkad_key *s2)
{
H
Herbert Xu 已提交
731
	SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
732
	struct rxrpc_crypt iv;
733
	struct scatterlist sg[1];
734 735 736 737

	/* continue encrypting from where we left off */
	memcpy(&iv, s2->session_key, sizeof(iv));

738 739
	sg_init_table(sg, 1);
	sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
H
Herbert Xu 已提交
740 741 742 743 744
	skcipher_request_set_tfm(req, conn->cipher);
	skcipher_request_set_callback(req, 0, NULL, NULL);
	skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
	crypto_skcipher_encrypt(req);
	skcipher_request_zero(req);
745 746 747 748 749 750 751 752 753
}

/*
 * respond to a challenge packet
 */
static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
				      struct sk_buff *skb,
				      u32 *_abort_code)
{
754
	const struct rxrpc_key_token *token;
755 756 757
	struct rxkad_challenge challenge;
	struct rxkad_response resp
		__attribute__((aligned(8))); /* must be aligned for crypto */
758
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
759 760 761
	u32 version, nonce, min_level, abort_code;
	int ret;

762
	_enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
763

764
	if (!conn->params.key) {
765 766 767 768
		_leave(" = -EPROTO [no key]");
		return -EPROTO;
	}

769
	ret = key_validate(conn->params.key);
770 771 772 773 774 775
	if (ret < 0) {
		*_abort_code = RXKADEXPIRED;
		return ret;
	}

	abort_code = RXKADPACKETSHORT;
776
	if (skb_copy_bits(skb, sp->offset, &challenge, sizeof(challenge)) < 0)
777 778 779 780 781 782 783
		goto protocol_error;

	version = ntohl(challenge.version);
	nonce = ntohl(challenge.nonce);
	min_level = ntohl(challenge.min_level);

	_proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
784
	       sp->hdr.serial, version, nonce, min_level);
785 786 787 788 789 790

	abort_code = RXKADINCONSISTENCY;
	if (version != RXKAD_VERSION)
		goto protocol_error;

	abort_code = RXKADLEVELFAIL;
791
	if (conn->params.security_level < min_level)
792 793
		goto protocol_error;

794
	token = conn->params.key->payload.data[0];
795 796 797 798

	/* build the response packet */
	memset(&resp, 0, sizeof(resp));

799
	resp.version			= htonl(RXKAD_VERSION);
800 801
	resp.encrypted.epoch		= htonl(conn->proto.epoch);
	resp.encrypted.cid		= htonl(conn->proto.cid);
802 803
	resp.encrypted.securityIndex	= htonl(conn->security_ix);
	resp.encrypted.inc_nonce	= htonl(nonce + 1);
804
	resp.encrypted.level		= htonl(conn->params.security_level);
805 806 807
	resp.kvno			= htonl(token->kad->kvno);
	resp.ticket_len			= htonl(token->kad->ticket_len);

808 809 810 811
	resp.encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
	resp.encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
	resp.encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
	resp.encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
812 813 814

	/* calculate the response checksum and then do the encryption */
	rxkad_calc_response_checksum(&resp);
815 816
	rxkad_encrypt_response(conn, &resp, token->kad);
	return rxkad_send_response(conn, &sp->hdr, &resp, token->kad);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

protocol_error:
	*_abort_code = abort_code;
	_leave(" = -EPROTO [%d]", abort_code);
	return -EPROTO;
}

/*
 * decrypt the kerberos IV ticket in the response
 */
static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
				void *ticket, size_t ticket_len,
				struct rxrpc_crypt *_session_key,
				time_t *_expiry,
				u32 *_abort_code)
{
H
Herbert Xu 已提交
833
	struct skcipher_request *req;
834
	struct rxrpc_crypt iv, key;
835
	struct scatterlist sg[1];
836
	struct in_addr addr;
837
	unsigned int life;
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	time_t issue, now;
	bool little_endian;
	int ret;
	u8 *p, *q, *name, *end;

	_enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));

	*_expiry = 0;

	ret = key_validate(conn->server_key);
	if (ret < 0) {
		switch (ret) {
		case -EKEYEXPIRED:
			*_abort_code = RXKADEXPIRED;
			goto error;
		default:
			*_abort_code = RXKADNOAUTH;
			goto error;
		}
	}

859
	ASSERT(conn->server_key->payload.data[0] != NULL);
860 861
	ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);

862
	memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
863

H
Herbert Xu 已提交
864 865 866 867 868 869 870
	req = skcipher_request_alloc(conn->server_key->payload.data[0],
				     GFP_NOFS);
	if (!req) {
		*_abort_code = RXKADNOAUTH;
		ret = -ENOMEM;
		goto error;
	}
871

872
	sg_init_one(&sg[0], ticket, ticket_len);
H
Herbert Xu 已提交
873 874 875 876
	skcipher_request_set_callback(req, 0, NULL, NULL);
	skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
	crypto_skcipher_decrypt(req);
	skcipher_request_free(req);
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 907 908 909 910 911 912 913 914 915 916

	p = ticket;
	end = p + ticket_len;

#define Z(size)						\
	({						\
		u8 *__str = p;				\
		q = memchr(p, 0, end - p);		\
		if (!q || q - p > (size))		\
			goto bad_ticket;		\
		for (; p < q; p++)			\
			if (!isprint(*p))		\
				goto bad_ticket;	\
		p++;					\
		__str;					\
	})

	/* extract the ticket flags */
	_debug("KIV FLAGS: %x", *p);
	little_endian = *p & 1;
	p++;

	/* extract the authentication name */
	name = Z(ANAME_SZ);
	_debug("KIV ANAME: %s", name);

	/* extract the principal's instance */
	name = Z(INST_SZ);
	_debug("KIV INST : %s", name);

	/* extract the principal's authentication domain */
	name = Z(REALM_SZ);
	_debug("KIV REALM: %s", name);

	if (end - p < 4 + 8 + 4 + 2)
		goto bad_ticket;

	/* get the IPv4 address of the entity that requested the ticket */
	memcpy(&addr, p, sizeof(addr));
	p += 4;
H
Harvey Harrison 已提交
917
	_debug("KIV ADDR : %pI4", &addr);
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

	/* get the session key from the ticket */
	memcpy(&key, p, sizeof(key));
	p += 8;
	_debug("KIV KEY  : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
	memcpy(_session_key, &key, sizeof(key));

	/* get the ticket's lifetime */
	life = *p++ * 5 * 60;
	_debug("KIV LIFE : %u", life);

	/* get the issue time of the ticket */
	if (little_endian) {
		__le32 stamp;
		memcpy(&stamp, p, 4);
		issue = le32_to_cpu(stamp);
	} else {
		__be32 stamp;
		memcpy(&stamp, p, 4);
		issue = be32_to_cpu(stamp);
	}
	p += 4;
940
	now = get_seconds();
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
	_debug("KIV ISSUE: %lx [%lx]", issue, now);

	/* check the ticket is in date */
	if (issue > now) {
		*_abort_code = RXKADNOAUTH;
		ret = -EKEYREJECTED;
		goto error;
	}

	if (issue < now - life) {
		*_abort_code = RXKADEXPIRED;
		ret = -EKEYEXPIRED;
		goto error;
	}

	*_expiry = issue + life;

	/* get the service name */
	name = Z(SNAME_SZ);
	_debug("KIV SNAME: %s", name);

	/* get the service instance name */
	name = Z(INST_SZ);
	_debug("KIV SINST: %s", name);

	ret = 0;
error:
	_leave(" = %d", ret);
	return ret;

bad_ticket:
	*_abort_code = RXKADBADTICKET;
	ret = -EBADMSG;
	goto error;
}

/*
 * decrypt the response packet
 */
static void rxkad_decrypt_response(struct rxrpc_connection *conn,
				   struct rxkad_response *resp,
				   const struct rxrpc_crypt *session_key)
{
H
Herbert Xu 已提交
984
	SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci);
985
	struct scatterlist sg[1];
986 987 988 989 990 991 992 993
	struct rxrpc_crypt iv;

	_enter(",,%08x%08x",
	       ntohl(session_key->n[0]), ntohl(session_key->n[1]));

	ASSERT(rxkad_ci != NULL);

	mutex_lock(&rxkad_ci_mutex);
H
Herbert Xu 已提交
994 995
	if (crypto_skcipher_setkey(rxkad_ci, session_key->x,
				   sizeof(*session_key)) < 0)
996 997 998 999
		BUG();

	memcpy(&iv, session_key, sizeof(iv));

1000 1001
	sg_init_table(sg, 1);
	sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
H
Herbert Xu 已提交
1002 1003 1004 1005 1006 1007
	skcipher_request_set_tfm(req, rxkad_ci);
	skcipher_request_set_callback(req, 0, NULL, NULL);
	skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
	crypto_skcipher_decrypt(req);
	skcipher_request_zero(req);

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	mutex_unlock(&rxkad_ci_mutex);

	_leave("");
}

/*
 * verify a response
 */
static int rxkad_verify_response(struct rxrpc_connection *conn,
				 struct sk_buff *skb,
				 u32 *_abort_code)
{
	struct rxkad_response response
		__attribute__((aligned(8))); /* must be aligned for crypto */
1022
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1023 1024 1025
	struct rxrpc_crypt session_key;
	time_t expiry;
	void *ticket;
A
Al Viro 已提交
1026 1027
	u32 abort_code, version, kvno, ticket_len, level;
	__be32 csum;
1028
	int ret, i;
1029 1030 1031 1032

	_enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));

	abort_code = RXKADPACKETSHORT;
1033
	if (skb_copy_bits(skb, sp->offset, &response, sizeof(response)) < 0)
1034 1035 1036 1037 1038 1039 1040 1041
		goto protocol_error;
	if (!pskb_pull(skb, sizeof(response)))
		BUG();

	version = ntohl(response.version);
	ticket_len = ntohl(response.ticket_len);
	kvno = ntohl(response.kvno);
	_proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1042
	       sp->hdr.serial, version, kvno, ticket_len);
1043 1044 1045

	abort_code = RXKADINCONSISTENCY;
	if (version != RXKAD_VERSION)
D
David Howells 已提交
1046
		goto protocol_error;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	abort_code = RXKADTICKETLEN;
	if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
		goto protocol_error;

	abort_code = RXKADUNKNOWNKEY;
	if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
		goto protocol_error;

	/* extract the kerberos ticket and decrypt and decode it */
	ticket = kmalloc(ticket_len, GFP_NOFS);
	if (!ticket)
		return -ENOMEM;

	abort_code = RXKADPACKETSHORT;
1062
	if (skb_copy_bits(skb, sp->offset, ticket, ticket_len) < 0)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		goto protocol_error_free;

	ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
				   &expiry, &abort_code);
	if (ret < 0) {
		*_abort_code = abort_code;
		kfree(ticket);
		return ret;
	}

	/* use the session key from inside the ticket to decrypt the
	 * response */
	rxkad_decrypt_response(conn, &response, &session_key);

	abort_code = RXKADSEALEDINCON;
1078
	if (ntohl(response.encrypted.epoch) != conn->proto.epoch)
1079
		goto protocol_error_free;
1080
	if (ntohl(response.encrypted.cid) != conn->proto.cid)
1081 1082 1083 1084 1085 1086 1087 1088 1089
		goto protocol_error_free;
	if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
		goto protocol_error_free;
	csum = response.encrypted.checksum;
	response.encrypted.checksum = 0;
	rxkad_calc_response_checksum(&response);
	if (response.encrypted.checksum != csum)
		goto protocol_error_free;

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	spin_lock(&conn->channel_lock);
	for (i = 0; i < RXRPC_MAXCALLS; i++) {
		struct rxrpc_call *call;
		u32 call_id = ntohl(response.encrypted.call_id[i]);

		if (call_id > INT_MAX)
			goto protocol_error_unlock;

		if (call_id < conn->channels[i].call_counter)
			goto protocol_error_unlock;
		if (call_id > conn->channels[i].call_counter) {
			call = rcu_dereference_protected(
				conn->channels[i].call,
				lockdep_is_held(&conn->channel_lock));
			if (call && call->state < RXRPC_CALL_COMPLETE)
				goto protocol_error_unlock;
			conn->channels[i].call_counter = call_id;
		}
	}
	spin_unlock(&conn->channel_lock);
1110 1111

	abort_code = RXKADOUTOFSEQUENCE;
1112
	if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1)
1113 1114 1115 1116 1117 1118
		goto protocol_error_free;

	abort_code = RXKADLEVELFAIL;
	level = ntohl(response.encrypted.level);
	if (level > RXRPC_SECURITY_ENCRYPT)
		goto protocol_error_free;
1119
	conn->params.security_level = level;
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

	/* create a key to hold the security data and expiration time - after
	 * this the connection security can be handled in exactly the same way
	 * as for a client connection */
	ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
	if (ret < 0) {
		kfree(ticket);
		return ret;
	}

	kfree(ticket);
	_leave(" = 0");
	return 0;

1134 1135
protocol_error_unlock:
	spin_unlock(&conn->channel_lock);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
protocol_error_free:
	kfree(ticket);
protocol_error:
	*_abort_code = abort_code;
	_leave(" = -EPROTO [%d]", abort_code);
	return -EPROTO;
}

/*
 * clear the connection security
 */
static void rxkad_clear(struct rxrpc_connection *conn)
{
	_enter("");

	if (conn->cipher)
H
Herbert Xu 已提交
1152
		crypto_free_skcipher(conn->cipher);
1153 1154
}

1155 1156 1157 1158 1159 1160 1161 1162
/*
 * Initialise the rxkad security service.
 */
static int rxkad_init(void)
{
	/* pin the cipher we need so that the crypto layer doesn't invoke
	 * keventd to go get it */
	rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
W
Wu Fengguang 已提交
1163
	return PTR_ERR_OR_ZERO(rxkad_ci);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
}

/*
 * Clean up the rxkad security service.
 */
static void rxkad_exit(void)
{
	if (rxkad_ci)
		crypto_free_skcipher(rxkad_ci);
}

1175 1176 1177
/*
 * RxRPC Kerberos-based security
 */
1178
const struct rxrpc_security rxkad = {
1179
	.name				= "rxkad",
1180
	.security_index			= RXRPC_SECURITY_RXKAD,
1181 1182
	.init				= rxkad_init,
	.exit				= rxkad_exit,
1183 1184 1185 1186
	.init_connection_security	= rxkad_init_connection_security,
	.prime_packet_security		= rxkad_prime_packet_security,
	.secure_packet			= rxkad_secure_packet,
	.verify_packet			= rxkad_verify_packet,
1187
	.locate_data			= rxkad_locate_data,
1188 1189 1190 1191 1192
	.issue_challenge		= rxkad_issue_challenge,
	.respond_to_challenge		= rxkad_respond_to_challenge,
	.verify_response		= rxkad_verify_response,
	.clear				= rxkad_clear,
};