Merge tag 'ieee802154-for-davem-2019-09-28' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / net / rxrpc / rxkad.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
17926a79
DH
2/* Kerberos-based RxRPC security
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
17926a79
DH
6 */
7
9b6d5398
JP
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
1afe593b 10#include <crypto/skcipher.h>
17926a79
DH
11#include <linux/module.h>
12#include <linux/net.h>
13#include <linux/skbuff.h>
14#include <linux/udp.h>
17926a79
DH
15#include <linux/scatterlist.h>
16#include <linux/ctype.h>
5a0e3ad6 17#include <linux/slab.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
33941284 20#include <keys/rxrpc-type.h>
17926a79
DH
21#include "ar-internal.h"
22
23#define RXKAD_VERSION 2
24#define MAXKRB5TICKETLEN 1024
25#define RXKAD_TKT_TYPE_KERBEROS_V5 256
26#define ANAME_SZ 40 /* size of authentication name */
27#define INST_SZ 40 /* size of principal's instance */
28#define REALM_SZ 40 /* size of principal's auth domain */
29#define SNAME_SZ 40 /* size of service name */
30
17926a79
DH
31struct rxkad_level1_hdr {
32 __be32 data_size; /* true data size (excluding padding) */
33};
34
35struct rxkad_level2_hdr {
36 __be32 data_size; /* true data size (excluding padding) */
37 __be32 checksum; /* decrypted data checksum */
38};
39
17926a79
DH
40/*
41 * this holds a pinned cipher so that keventd doesn't get called by the cipher
42 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
43 * packets
44 */
69d826fa 45static struct crypto_sync_skcipher *rxkad_ci;
1db88c53 46static struct skcipher_request *rxkad_ci_req;
17926a79
DH
47static DEFINE_MUTEX(rxkad_ci_mutex);
48
49/*
50 * initialise connection security
51 */
52static int rxkad_init_connection_security(struct rxrpc_connection *conn)
53{
69d826fa 54 struct crypto_sync_skcipher *ci;
33941284 55 struct rxrpc_key_token *token;
17926a79
DH
56 int ret;
57
19ffa01c 58 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key));
17926a79 59
19ffa01c 60 token = conn->params.key->payload.data[0];
33941284 61 conn->security_ix = token->security_index;
17926a79 62
69d826fa 63 ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
17926a79
DH
64 if (IS_ERR(ci)) {
65 _debug("no cipher");
66 ret = PTR_ERR(ci);
67 goto error;
68 }
69
69d826fa 70 if (crypto_sync_skcipher_setkey(ci, token->kad->session_key,
1afe593b 71 sizeof(token->kad->session_key)) < 0)
17926a79
DH
72 BUG();
73
19ffa01c 74 switch (conn->params.security_level) {
17926a79
DH
75 case RXRPC_SECURITY_PLAIN:
76 break;
77 case RXRPC_SECURITY_AUTH:
78 conn->size_align = 8;
79 conn->security_size = sizeof(struct rxkad_level1_hdr);
17926a79
DH
80 break;
81 case RXRPC_SECURITY_ENCRYPT:
82 conn->size_align = 8;
83 conn->security_size = sizeof(struct rxkad_level2_hdr);
17926a79
DH
84 break;
85 default:
86 ret = -EKEYREJECTED;
87 goto error;
88 }
89
90 conn->cipher = ci;
91 ret = 0;
92error:
93 _leave(" = %d", ret);
94 return ret;
95}
96
97/*
98 * prime the encryption state with the invariant parts of a connection's
99 * description
100 */
a263629d 101static int rxkad_prime_packet_security(struct rxrpc_connection *conn)
17926a79 102{
1db88c53 103 struct skcipher_request *req;
33941284 104 struct rxrpc_key_token *token;
a263629d 105 struct scatterlist sg;
17926a79 106 struct rxrpc_crypt iv;
a263629d
HX
107 __be32 *tmpbuf;
108 size_t tmpsize = 4 * sizeof(__be32);
17926a79
DH
109
110 _enter("");
111
19ffa01c 112 if (!conn->params.key)
a263629d
HX
113 return 0;
114
115 tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
116 if (!tmpbuf)
117 return -ENOMEM;
17926a79 118
1db88c53
DH
119 req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS);
120 if (!req) {
121 kfree(tmpbuf);
122 return -ENOMEM;
123 }
124
19ffa01c 125 token = conn->params.key->payload.data[0];
33941284 126 memcpy(&iv, token->kad->session_key, sizeof(iv));
17926a79 127
a263629d
HX
128 tmpbuf[0] = htonl(conn->proto.epoch);
129 tmpbuf[1] = htonl(conn->proto.cid);
130 tmpbuf[2] = 0;
131 tmpbuf[3] = htonl(conn->security_ix);
1afe593b 132
a263629d 133 sg_init_one(&sg, tmpbuf, tmpsize);
69d826fa 134 skcipher_request_set_sync_tfm(req, conn->cipher);
1afe593b 135 skcipher_request_set_callback(req, 0, NULL, NULL);
a263629d 136 skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
1afe593b 137 crypto_skcipher_encrypt(req);
1db88c53 138 skcipher_request_free(req);
17926a79 139
a263629d
HX
140 memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv));
141 kfree(tmpbuf);
142 _leave(" = 0");
143 return 0;
17926a79
DH
144}
145
1db88c53
DH
146/*
147 * Allocate and prepare the crypto request on a call. For any particular call,
148 * this is called serially for the packets, so no lock should be necessary.
149 */
150static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call)
151{
152 struct crypto_skcipher *tfm = &call->conn->cipher->base;
153 struct skcipher_request *cipher_req = call->cipher_req;
154
155 if (!cipher_req) {
156 cipher_req = skcipher_request_alloc(tfm, GFP_NOFS);
157 if (!cipher_req)
158 return NULL;
159 call->cipher_req = cipher_req;
160 }
161
162 return cipher_req;
163}
164
165/*
166 * Clean up the crypto on a call.
167 */
168static void rxkad_free_call_crypto(struct rxrpc_call *call)
169{
170 if (call->cipher_req)
171 skcipher_request_free(call->cipher_req);
172 call->cipher_req = NULL;
173}
174
17926a79
DH
175/*
176 * partially encrypt a packet (level 1 security)
177 */
178static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
179 struct sk_buff *skb,
180 u32 data_size,
54424d38
KC
181 void *sechdr,
182 struct skcipher_request *req)
17926a79 183{
fb46f6ee 184 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
a263629d 185 struct rxkad_level1_hdr hdr;
17926a79 186 struct rxrpc_crypt iv;
a263629d 187 struct scatterlist sg;
17926a79
DH
188 u16 check;
189
17926a79
DH
190 _enter("");
191
5a924b89 192 check = sp->hdr.seq ^ call->call_id;
0d12f8a4 193 data_size |= (u32)check << 16;
17926a79 194
a263629d
HX
195 hdr.data_size = htonl(data_size);
196 memcpy(sechdr, &hdr, sizeof(hdr));
17926a79
DH
197
198 /* start the encryption afresh */
199 memset(&iv, 0, sizeof(iv));
17926a79 200
a263629d 201 sg_init_one(&sg, sechdr, 8);
69d826fa 202 skcipher_request_set_sync_tfm(req, call->conn->cipher);
1afe593b 203 skcipher_request_set_callback(req, 0, NULL, NULL);
a263629d 204 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
1afe593b
HX
205 crypto_skcipher_encrypt(req);
206 skcipher_request_zero(req);
17926a79 207
17926a79
DH
208 _leave(" = 0");
209 return 0;
210}
211
212/*
213 * wholly encrypt a packet (level 2 security)
214 */
215static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
b4f1342f
DH
216 struct sk_buff *skb,
217 u32 data_size,
54424d38
KC
218 void *sechdr,
219 struct skcipher_request *req)
17926a79 220{
33941284 221 const struct rxrpc_key_token *token;
a263629d 222 struct rxkad_level2_hdr rxkhdr;
17926a79 223 struct rxrpc_skb_priv *sp;
17926a79
DH
224 struct rxrpc_crypt iv;
225 struct scatterlist sg[16];
95c96174 226 unsigned int len;
17926a79 227 u16 check;
1afe593b 228 int err;
17926a79
DH
229
230 sp = rxrpc_skb(skb);
231
232 _enter("");
233
5a924b89 234 check = sp->hdr.seq ^ call->call_id;
17926a79 235
0d12f8a4 236 rxkhdr.data_size = htonl(data_size | (u32)check << 16);
17926a79 237 rxkhdr.checksum = 0;
a263629d 238 memcpy(sechdr, &rxkhdr, sizeof(rxkhdr));
17926a79
DH
239
240 /* encrypt from the session key */
19ffa01c 241 token = call->conn->params.key->payload.data[0];
33941284 242 memcpy(&iv, token->kad->session_key, sizeof(iv));
17926a79 243
68e3f5dd 244 sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
69d826fa 245 skcipher_request_set_sync_tfm(req, call->conn->cipher);
1afe593b 246 skcipher_request_set_callback(req, 0, NULL, NULL);
a263629d 247 skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
1afe593b 248 crypto_skcipher_encrypt(req);
17926a79
DH
249
250 /* we want to encrypt the skbuff in-place */
d0d5c0cd
DH
251 err = -EMSGSIZE;
252 if (skb_shinfo(skb)->nr_frags > 16)
1afe593b 253 goto out;
17926a79
DH
254
255 len = data_size + call->conn->size_align - 1;
256 len &= ~(call->conn->size_align - 1);
257
d0d5c0cd 258 sg_init_table(sg, ARRAY_SIZE(sg));
89a5ea99
JD
259 err = skb_to_sgvec(skb, sg, 0, len);
260 if (unlikely(err < 0))
261 goto out;
1afe593b 262 skcipher_request_set_crypt(req, sg, sg, len, iv.x);
1afe593b 263 crypto_skcipher_encrypt(req);
17926a79
DH
264
265 _leave(" = 0");
1afe593b
HX
266 err = 0;
267
268out:
269 skcipher_request_zero(req);
270 return err;
17926a79
DH
271}
272
273/*
274 * checksum an RxRPC packet header
275 */
a263629d 276static int rxkad_secure_packet(struct rxrpc_call *call,
b4f1342f
DH
277 struct sk_buff *skb,
278 size_t data_size,
279 void *sechdr)
17926a79
DH
280{
281 struct rxrpc_skb_priv *sp;
1db88c53 282 struct skcipher_request *req;
17926a79 283 struct rxrpc_crypt iv;
a263629d 284 struct scatterlist sg;
0d12f8a4 285 u32 x, y;
17926a79
DH
286 int ret;
287
288 sp = rxrpc_skb(skb);
289
290 _enter("{%d{%x}},{#%u},%zu,",
19ffa01c
DH
291 call->debug_id, key_serial(call->conn->params.key),
292 sp->hdr.seq, data_size);
17926a79
DH
293
294 if (!call->conn->cipher)
295 return 0;
296
19ffa01c 297 ret = key_validate(call->conn->params.key);
17926a79
DH
298 if (ret < 0)
299 return ret;
300
1db88c53
DH
301 req = rxkad_get_call_crypto(call);
302 if (!req)
303 return -ENOMEM;
304
17926a79
DH
305 /* continue encrypting from where we left off */
306 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
17926a79
DH
307
308 /* calculate the security checksum */
01a90a45 309 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
0d12f8a4 310 x |= sp->hdr.seq & 0x3fffffff;
5a924b89 311 call->crypto_buf[0] = htonl(call->call_id);
a263629d 312 call->crypto_buf[1] = htonl(x);
1afe593b 313
a263629d 314 sg_init_one(&sg, call->crypto_buf, 8);
69d826fa 315 skcipher_request_set_sync_tfm(req, call->conn->cipher);
1afe593b 316 skcipher_request_set_callback(req, 0, NULL, NULL);
a263629d 317 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
1afe593b
HX
318 crypto_skcipher_encrypt(req);
319 skcipher_request_zero(req);
17926a79 320
a263629d 321 y = ntohl(call->crypto_buf[1]);
91e916cf
AV
322 y = (y >> 16) & 0xffff;
323 if (y == 0)
324 y = 1; /* zero checksums are not permitted */
0d12f8a4 325 sp->hdr.cksum = y;
17926a79 326
19ffa01c 327 switch (call->conn->params.security_level) {
17926a79
DH
328 case RXRPC_SECURITY_PLAIN:
329 ret = 0;
330 break;
331 case RXRPC_SECURITY_AUTH:
54424d38
KC
332 ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr,
333 req);
17926a79
DH
334 break;
335 case RXRPC_SECURITY_ENCRYPT:
336 ret = rxkad_secure_packet_encrypt(call, skb, data_size,
54424d38 337 sechdr, req);
17926a79
DH
338 break;
339 default:
340 ret = -EPERM;
341 break;
342 }
343
91e916cf 344 _leave(" = %d [set %hx]", ret, y);
17926a79
DH
345 return ret;
346}
347
348/*
349 * decrypt partial encryption on a packet (level 1 security)
350 */
5a42976d 351static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
248f219c 352 unsigned int offset, unsigned int len,
54424d38
KC
353 rxrpc_seq_t seq,
354 struct skcipher_request *req)
17926a79
DH
355{
356 struct rxkad_level1_hdr sechdr;
17926a79 357 struct rxrpc_crypt iv;
68e3f5dd 358 struct scatterlist sg[16];
fb46f6ee 359 bool aborted;
17926a79
DH
360 u32 data_size, buf;
361 u16 check;
d0d5c0cd 362 int ret;
17926a79
DH
363
364 _enter("");
365
248f219c 366 if (len < 8) {
fb46f6ee
DH
367 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H",
368 RXKADSEALEDINCON);
5a42976d
DH
369 goto protocol_error;
370 }
17926a79 371
248f219c
DH
372 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
373 * directly into the target buffer.
374 */
d0d5c0cd 375 sg_init_table(sg, ARRAY_SIZE(sg));
89a5ea99
JD
376 ret = skb_to_sgvec(skb, sg, offset, 8);
377 if (unlikely(ret < 0))
378 return ret;
17926a79
DH
379
380 /* start the decryption afresh */
381 memset(&iv, 0, sizeof(iv));
17926a79 382
69d826fa 383 skcipher_request_set_sync_tfm(req, call->conn->cipher);
1afe593b
HX
384 skcipher_request_set_callback(req, 0, NULL, NULL);
385 skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
1afe593b
HX
386 crypto_skcipher_decrypt(req);
387 skcipher_request_zero(req);
17926a79 388
5a42976d 389 /* Extract the decrypted packet length */
248f219c 390 if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
fb46f6ee
DH
391 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1",
392 RXKADDATALEN);
5a42976d
DH
393 goto protocol_error;
394 }
248f219c
DH
395 offset += sizeof(sechdr);
396 len -= sizeof(sechdr);
17926a79
DH
397
398 buf = ntohl(sechdr.data_size);
399 data_size = buf & 0xffff;
400
401 check = buf >> 16;
5a42976d 402 check ^= seq ^ call->call_id;
17926a79
DH
403 check &= 0xffff;
404 if (check != 0) {
fb46f6ee
DH
405 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C",
406 RXKADSEALEDINCON);
17926a79
DH
407 goto protocol_error;
408 }
409
248f219c 410 if (data_size > len) {
fb46f6ee
DH
411 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L",
412 RXKADDATALEN);
5a42976d
DH
413 goto protocol_error;
414 }
17926a79
DH
415
416 _leave(" = 0 [dlen=%x]", data_size);
417 return 0;
418
17926a79 419protocol_error:
fb46f6ee
DH
420 if (aborted)
421 rxrpc_send_abort_packet(call);
17926a79 422 return -EPROTO;
17926a79
DH
423}
424
425/*
426 * wholly decrypt a packet (level 2 security)
427 */
5a42976d 428static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
248f219c 429 unsigned int offset, unsigned int len,
54424d38
KC
430 rxrpc_seq_t seq,
431 struct skcipher_request *req)
17926a79 432{
33941284 433 const struct rxrpc_key_token *token;
17926a79 434 struct rxkad_level2_hdr sechdr;
17926a79
DH
435 struct rxrpc_crypt iv;
436 struct scatterlist _sg[4], *sg;
fb46f6ee 437 bool aborted;
17926a79
DH
438 u32 data_size, buf;
439 u16 check;
89a5ea99 440 int nsg, ret;
17926a79
DH
441
442 _enter(",{%d}", skb->len);
443
248f219c 444 if (len < 8) {
fb46f6ee
DH
445 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H",
446 RXKADSEALEDINCON);
5a42976d
DH
447 goto protocol_error;
448 }
17926a79 449
248f219c
DH
450 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
451 * directly into the target buffer.
452 */
17926a79 453 sg = _sg;
d0d5c0cd
DH
454 nsg = skb_shinfo(skb)->nr_frags;
455 if (nsg <= 4) {
456 nsg = 4;
457 } else {
6da2ec56 458 sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO);
17926a79
DH
459 if (!sg)
460 goto nomem;
461 }
462
68e3f5dd 463 sg_init_table(sg, nsg);
89a5ea99
JD
464 ret = skb_to_sgvec(skb, sg, offset, len);
465 if (unlikely(ret < 0)) {
466 if (sg != _sg)
467 kfree(sg);
468 return ret;
469 }
17926a79
DH
470
471 /* decrypt from the session key */
19ffa01c 472 token = call->conn->params.key->payload.data[0];
33941284 473 memcpy(&iv, token->kad->session_key, sizeof(iv));
17926a79 474
69d826fa 475 skcipher_request_set_sync_tfm(req, call->conn->cipher);
1afe593b 476 skcipher_request_set_callback(req, 0, NULL, NULL);
248f219c 477 skcipher_request_set_crypt(req, sg, sg, len, iv.x);
1afe593b
HX
478 crypto_skcipher_decrypt(req);
479 skcipher_request_zero(req);
17926a79
DH
480 if (sg != _sg)
481 kfree(sg);
482
5a42976d 483 /* Extract the decrypted packet length */
248f219c 484 if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
fb46f6ee
DH
485 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2",
486 RXKADDATALEN);
5a42976d
DH
487 goto protocol_error;
488 }
248f219c
DH
489 offset += sizeof(sechdr);
490 len -= sizeof(sechdr);
17926a79
DH
491
492 buf = ntohl(sechdr.data_size);
493 data_size = buf & 0xffff;
494
495 check = buf >> 16;
5a42976d 496 check ^= seq ^ call->call_id;
17926a79
DH
497 check &= 0xffff;
498 if (check != 0) {
fb46f6ee
DH
499 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C",
500 RXKADSEALEDINCON);
17926a79
DH
501 goto protocol_error;
502 }
503
248f219c 504 if (data_size > len) {
fb46f6ee
DH
505 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L",
506 RXKADDATALEN);
5a42976d
DH
507 goto protocol_error;
508 }
17926a79
DH
509
510 _leave(" = 0 [dlen=%x]", data_size);
511 return 0;
512
17926a79 513protocol_error:
fb46f6ee
DH
514 if (aborted)
515 rxrpc_send_abort_packet(call);
17926a79
DH
516 return -EPROTO;
517
518nomem:
519 _leave(" = -ENOMEM");
520 return -ENOMEM;
521}
522
523/*
5a42976d
DH
524 * Verify the security on a received packet or subpacket (if part of a
525 * jumbo packet).
17926a79 526 */
5a42976d 527static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
248f219c 528 unsigned int offset, unsigned int len,
5a42976d 529 rxrpc_seq_t seq, u16 expected_cksum)
17926a79 530{
1db88c53 531 struct skcipher_request *req;
17926a79 532 struct rxrpc_crypt iv;
a263629d 533 struct scatterlist sg;
fb46f6ee 534 bool aborted;
0d12f8a4
DH
535 u16 cksum;
536 u32 x, y;
17926a79
DH
537
538 _enter("{%d{%x}},{#%u}",
5a42976d 539 call->debug_id, key_serial(call->conn->params.key), seq);
17926a79
DH
540
541 if (!call->conn->cipher)
542 return 0;
543
1db88c53
DH
544 req = rxkad_get_call_crypto(call);
545 if (!req)
546 return -ENOMEM;
547
17926a79
DH
548 /* continue encrypting from where we left off */
549 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
17926a79
DH
550
551 /* validate the security checksum */
01a90a45 552 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
5a42976d 553 x |= seq & 0x3fffffff;
a263629d
HX
554 call->crypto_buf[0] = htonl(call->call_id);
555 call->crypto_buf[1] = htonl(x);
1afe593b 556
a263629d 557 sg_init_one(&sg, call->crypto_buf, 8);
69d826fa 558 skcipher_request_set_sync_tfm(req, call->conn->cipher);
1afe593b 559 skcipher_request_set_callback(req, 0, NULL, NULL);
a263629d 560 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
1afe593b
HX
561 crypto_skcipher_encrypt(req);
562 skcipher_request_zero(req);
17926a79 563
a263629d 564 y = ntohl(call->crypto_buf[1]);
0d12f8a4
DH
565 cksum = (y >> 16) & 0xffff;
566 if (cksum == 0)
567 cksum = 1; /* zero checksums are not permitted */
17926a79 568
5a42976d 569 if (cksum != expected_cksum) {
fb46f6ee
DH
570 aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK",
571 RXKADSEALEDINCON);
572 goto protocol_error;
17926a79
DH
573 }
574
19ffa01c 575 switch (call->conn->params.security_level) {
17926a79 576 case RXRPC_SECURITY_PLAIN:
5a42976d 577 return 0;
17926a79 578 case RXRPC_SECURITY_AUTH:
54424d38 579 return rxkad_verify_packet_1(call, skb, offset, len, seq, req);
17926a79 580 case RXRPC_SECURITY_ENCRYPT:
54424d38 581 return rxkad_verify_packet_2(call, skb, offset, len, seq, req);
17926a79 582 default:
5a42976d 583 return -ENOANO;
17926a79 584 }
fb46f6ee
DH
585
586protocol_error:
587 if (aborted)
588 rxrpc_send_abort_packet(call);
589 return -EPROTO;
17926a79
DH
590}
591
248f219c
DH
592/*
593 * Locate the data contained in a packet that was partially encrypted.
594 */
595static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb,
596 unsigned int *_offset, unsigned int *_len)
597{
598 struct rxkad_level1_hdr sechdr;
599
600 if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
601 BUG();
602 *_offset += sizeof(sechdr);
603 *_len = ntohl(sechdr.data_size) & 0xffff;
604}
605
606/*
607 * Locate the data contained in a packet that was completely encrypted.
608 */
609static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb,
610 unsigned int *_offset, unsigned int *_len)
611{
612 struct rxkad_level2_hdr sechdr;
613
614 if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
615 BUG();
616 *_offset += sizeof(sechdr);
617 *_len = ntohl(sechdr.data_size) & 0xffff;
618}
619
620/*
621 * Locate the data contained in an already decrypted packet.
622 */
623static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
624 unsigned int *_offset, unsigned int *_len)
625{
626 switch (call->conn->params.security_level) {
627 case RXRPC_SECURITY_AUTH:
628 rxkad_locate_data_1(call, skb, _offset, _len);
629 return;
630 case RXRPC_SECURITY_ENCRYPT:
631 rxkad_locate_data_2(call, skb, _offset, _len);
632 return;
633 default:
634 return;
635 }
636}
637
17926a79
DH
638/*
639 * issue a challenge
640 */
641static int rxkad_issue_challenge(struct rxrpc_connection *conn)
642{
643 struct rxkad_challenge challenge;
0d12f8a4 644 struct rxrpc_wire_header whdr;
17926a79
DH
645 struct msghdr msg;
646 struct kvec iov[2];
647 size_t len;
0d12f8a4 648 u32 serial;
17926a79
DH
649 int ret;
650
19ffa01c 651 _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
17926a79 652
19ffa01c 653 ret = key_validate(conn->params.key);
17926a79
DH
654 if (ret < 0)
655 return ret;
656
657 get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
658
659 challenge.version = htonl(2);
660 challenge.nonce = htonl(conn->security_nonce);
661 challenge.min_level = htonl(0);
662 challenge.__padding = 0;
663
7b674e39
DH
664 msg.msg_name = &conn->params.peer->srx.transport;
665 msg.msg_namelen = conn->params.peer->srx.transport_len;
17926a79
DH
666 msg.msg_control = NULL;
667 msg.msg_controllen = 0;
668 msg.msg_flags = 0;
669
19ffa01c
DH
670 whdr.epoch = htonl(conn->proto.epoch);
671 whdr.cid = htonl(conn->proto.cid);
0d12f8a4
DH
672 whdr.callNumber = 0;
673 whdr.seq = 0;
674 whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
675 whdr.flags = conn->out_clientflag;
676 whdr.userStatus = 0;
677 whdr.securityIndex = conn->security_ix;
678 whdr._rsvd = 0;
68d6d1ae 679 whdr.serviceId = htons(conn->service_id);
0d12f8a4
DH
680
681 iov[0].iov_base = &whdr;
682 iov[0].iov_len = sizeof(whdr);
17926a79
DH
683 iov[1].iov_base = &challenge;
684 iov[1].iov_len = sizeof(challenge);
685
686 len = iov[0].iov_len + iov[1].iov_len;
687
0d12f8a4
DH
688 serial = atomic_inc_return(&conn->serial);
689 whdr.serial = htonl(serial);
690 _proto("Tx CHALLENGE %%%u", serial);
17926a79 691
85f32278 692 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
17926a79 693 if (ret < 0) {
6b47fe1d 694 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
4764c0da 695 rxrpc_tx_point_rxkad_challenge);
17926a79
DH
696 return -EAGAIN;
697 }
698
330bdcfa 699 conn->params.peer->last_tx_at = ktime_get_seconds();
4764c0da
DH
700 trace_rxrpc_tx_packet(conn->debug_id, &whdr,
701 rxrpc_tx_point_rxkad_challenge);
17926a79
DH
702 _leave(" = 0");
703 return 0;
704}
705
706/*
707 * send a Kerberos security response
708 */
709static int rxkad_send_response(struct rxrpc_connection *conn,
0d12f8a4 710 struct rxrpc_host_header *hdr,
17926a79
DH
711 struct rxkad_response *resp,
712 const struct rxkad_key *s2)
713{
0d12f8a4 714 struct rxrpc_wire_header whdr;
17926a79
DH
715 struct msghdr msg;
716 struct kvec iov[3];
717 size_t len;
0d12f8a4 718 u32 serial;
17926a79
DH
719 int ret;
720
721 _enter("");
722
7b674e39
DH
723 msg.msg_name = &conn->params.peer->srx.transport;
724 msg.msg_namelen = conn->params.peer->srx.transport_len;
17926a79
DH
725 msg.msg_control = NULL;
726 msg.msg_controllen = 0;
727 msg.msg_flags = 0;
728
0d12f8a4
DH
729 memset(&whdr, 0, sizeof(whdr));
730 whdr.epoch = htonl(hdr->epoch);
731 whdr.cid = htonl(hdr->cid);
732 whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
733 whdr.flags = conn->out_clientflag;
734 whdr.securityIndex = hdr->securityIndex;
735 whdr.serviceId = htons(hdr->serviceId);
17926a79 736
0d12f8a4
DH
737 iov[0].iov_base = &whdr;
738 iov[0].iov_len = sizeof(whdr);
17926a79
DH
739 iov[1].iov_base = resp;
740 iov[1].iov_len = sizeof(*resp);
0d12f8a4 741 iov[2].iov_base = (void *)s2->ticket;
17926a79
DH
742 iov[2].iov_len = s2->ticket_len;
743
744 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
745
0d12f8a4
DH
746 serial = atomic_inc_return(&conn->serial);
747 whdr.serial = htonl(serial);
748 _proto("Tx RESPONSE %%%u", serial);
17926a79 749
85f32278 750 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
17926a79 751 if (ret < 0) {
6b47fe1d 752 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
4764c0da 753 rxrpc_tx_point_rxkad_response);
17926a79
DH
754 return -EAGAIN;
755 }
756
330bdcfa 757 conn->params.peer->last_tx_at = ktime_get_seconds();
17926a79
DH
758 _leave(" = 0");
759 return 0;
760}
761
762/*
763 * calculate the response checksum
764 */
765static void rxkad_calc_response_checksum(struct rxkad_response *response)
766{
767 u32 csum = 1000003;
768 int loop;
769 u8 *p = (u8 *) response;
770
771 for (loop = sizeof(*response); loop > 0; loop--)
772 csum = csum * 0x10204081 + *p++;
773
774 response->encrypted.checksum = htonl(csum);
775}
776
17926a79
DH
777/*
778 * encrypt the response packet
779 */
1db88c53
DH
780static int rxkad_encrypt_response(struct rxrpc_connection *conn,
781 struct rxkad_response *resp,
782 const struct rxkad_key *s2)
17926a79 783{
1db88c53 784 struct skcipher_request *req;
17926a79 785 struct rxrpc_crypt iv;
a263629d 786 struct scatterlist sg[1];
17926a79 787
1db88c53
DH
788 req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS);
789 if (!req)
790 return -ENOMEM;
791
17926a79
DH
792 /* continue encrypting from where we left off */
793 memcpy(&iv, s2->session_key, sizeof(iv));
17926a79 794
a263629d
HX
795 sg_init_table(sg, 1);
796 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
69d826fa 797 skcipher_request_set_sync_tfm(req, conn->cipher);
1afe593b
HX
798 skcipher_request_set_callback(req, 0, NULL, NULL);
799 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
1afe593b 800 crypto_skcipher_encrypt(req);
1db88c53
DH
801 skcipher_request_free(req);
802 return 0;
17926a79
DH
803}
804
805/*
806 * respond to a challenge packet
807 */
808static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
809 struct sk_buff *skb,
810 u32 *_abort_code)
811{
33941284 812 const struct rxrpc_key_token *token;
17926a79 813 struct rxkad_challenge challenge;
8c2f826d 814 struct rxkad_response *resp;
248f219c 815 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
fb46f6ee 816 const char *eproto;
17926a79
DH
817 u32 version, nonce, min_level, abort_code;
818 int ret;
819
19ffa01c 820 _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
17926a79 821
fb46f6ee 822 eproto = tracepoint_string("chall_no_key");
ef68622d
DH
823 abort_code = RX_PROTOCOL_ERROR;
824 if (!conn->params.key)
825 goto protocol_error;
17926a79 826
ef68622d 827 abort_code = RXKADEXPIRED;
19ffa01c 828 ret = key_validate(conn->params.key);
ef68622d
DH
829 if (ret < 0)
830 goto other_error;
17926a79 831
fb46f6ee 832 eproto = tracepoint_string("chall_short");
17926a79 833 abort_code = RXKADPACKETSHORT;
775e5b71
DH
834 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
835 &challenge, sizeof(challenge)) < 0)
17926a79
DH
836 goto protocol_error;
837
838 version = ntohl(challenge.version);
839 nonce = ntohl(challenge.nonce);
840 min_level = ntohl(challenge.min_level);
841
842 _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
0d12f8a4 843 sp->hdr.serial, version, nonce, min_level);
17926a79 844
fb46f6ee 845 eproto = tracepoint_string("chall_ver");
17926a79
DH
846 abort_code = RXKADINCONSISTENCY;
847 if (version != RXKAD_VERSION)
848 goto protocol_error;
849
850 abort_code = RXKADLEVELFAIL;
ef68622d 851 ret = -EACCES;
19ffa01c 852 if (conn->params.security_level < min_level)
ef68622d 853 goto other_error;
17926a79 854
19ffa01c 855 token = conn->params.key->payload.data[0];
17926a79
DH
856
857 /* build the response packet */
8c2f826d
DH
858 resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
859 if (!resp)
860 return -ENOMEM;
861
862 resp->version = htonl(RXKAD_VERSION);
863 resp->encrypted.epoch = htonl(conn->proto.epoch);
864 resp->encrypted.cid = htonl(conn->proto.cid);
865 resp->encrypted.securityIndex = htonl(conn->security_ix);
866 resp->encrypted.inc_nonce = htonl(nonce + 1);
867 resp->encrypted.level = htonl(conn->params.security_level);
868 resp->kvno = htonl(token->kad->kvno);
869 resp->ticket_len = htonl(token->kad->ticket_len);
870 resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
871 resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
872 resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
873 resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
17926a79
DH
874
875 /* calculate the response checksum and then do the encryption */
8c2f826d 876 rxkad_calc_response_checksum(resp);
1db88c53
DH
877 ret = rxkad_encrypt_response(conn, resp, token->kad);
878 if (ret == 0)
879 ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
8c2f826d
DH
880 kfree(resp);
881 return ret;
17926a79
DH
882
883protocol_error:
fb46f6ee 884 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
ef68622d
DH
885 ret = -EPROTO;
886other_error:
17926a79 887 *_abort_code = abort_code;
ef68622d 888 return ret;
17926a79
DH
889}
890
891/*
892 * decrypt the kerberos IV ticket in the response
893 */
894static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
fb46f6ee 895 struct sk_buff *skb,
17926a79
DH
896 void *ticket, size_t ticket_len,
897 struct rxrpc_crypt *_session_key,
10674a03 898 time64_t *_expiry,
17926a79
DH
899 u32 *_abort_code)
900{
1afe593b 901 struct skcipher_request *req;
fb46f6ee 902 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 903 struct rxrpc_crypt iv, key;
68e3f5dd 904 struct scatterlist sg[1];
17926a79 905 struct in_addr addr;
95c96174 906 unsigned int life;
fb46f6ee 907 const char *eproto;
10674a03 908 time64_t issue, now;
17926a79
DH
909 bool little_endian;
910 int ret;
fb46f6ee 911 u32 abort_code;
17926a79
DH
912 u8 *p, *q, *name, *end;
913
914 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
915
916 *_expiry = 0;
917
918 ret = key_validate(conn->server_key);
919 if (ret < 0) {
920 switch (ret) {
921 case -EKEYEXPIRED:
fb46f6ee 922 abort_code = RXKADEXPIRED;
ef68622d 923 goto other_error;
17926a79 924 default:
fb46f6ee 925 abort_code = RXKADNOAUTH;
ef68622d 926 goto other_error;
17926a79
DH
927 }
928 }
929
146aa8b1 930 ASSERT(conn->server_key->payload.data[0] != NULL);
17926a79
DH
931 ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
932
146aa8b1 933 memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
17926a79 934
ef68622d 935 ret = -ENOMEM;
1afe593b
HX
936 req = skcipher_request_alloc(conn->server_key->payload.data[0],
937 GFP_NOFS);
ef68622d
DH
938 if (!req)
939 goto temporary_error;
17926a79 940
68e3f5dd 941 sg_init_one(&sg[0], ticket, ticket_len);
1afe593b
HX
942 skcipher_request_set_callback(req, 0, NULL, NULL);
943 skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
1afe593b
HX
944 crypto_skcipher_decrypt(req);
945 skcipher_request_free(req);
17926a79
DH
946
947 p = ticket;
948 end = p + ticket_len;
949
fb46f6ee 950#define Z(field) \
17926a79
DH
951 ({ \
952 u8 *__str = p; \
fb46f6ee 953 eproto = tracepoint_string("rxkad_bad_"#field); \
17926a79 954 q = memchr(p, 0, end - p); \
fb46f6ee 955 if (!q || q - p > (field##_SZ)) \
17926a79
DH
956 goto bad_ticket; \
957 for (; p < q; p++) \
958 if (!isprint(*p)) \
959 goto bad_ticket; \
960 p++; \
961 __str; \
962 })
963
964 /* extract the ticket flags */
965 _debug("KIV FLAGS: %x", *p);
966 little_endian = *p & 1;
967 p++;
968
969 /* extract the authentication name */
fb46f6ee 970 name = Z(ANAME);
17926a79
DH
971 _debug("KIV ANAME: %s", name);
972
973 /* extract the principal's instance */
fb46f6ee 974 name = Z(INST);
17926a79
DH
975 _debug("KIV INST : %s", name);
976
977 /* extract the principal's authentication domain */
fb46f6ee 978 name = Z(REALM);
17926a79
DH
979 _debug("KIV REALM: %s", name);
980
fb46f6ee 981 eproto = tracepoint_string("rxkad_bad_len");
17926a79
DH
982 if (end - p < 4 + 8 + 4 + 2)
983 goto bad_ticket;
984
985 /* get the IPv4 address of the entity that requested the ticket */
986 memcpy(&addr, p, sizeof(addr));
987 p += 4;
21454aaa 988 _debug("KIV ADDR : %pI4", &addr);
17926a79
DH
989
990 /* get the session key from the ticket */
991 memcpy(&key, p, sizeof(key));
992 p += 8;
993 _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
994 memcpy(_session_key, &key, sizeof(key));
995
996 /* get the ticket's lifetime */
997 life = *p++ * 5 * 60;
998 _debug("KIV LIFE : %u", life);
999
1000 /* get the issue time of the ticket */
1001 if (little_endian) {
1002 __le32 stamp;
1003 memcpy(&stamp, p, 4);
10674a03 1004 issue = rxrpc_u32_to_time64(le32_to_cpu(stamp));
17926a79
DH
1005 } else {
1006 __be32 stamp;
1007 memcpy(&stamp, p, 4);
10674a03 1008 issue = rxrpc_u32_to_time64(be32_to_cpu(stamp));
17926a79
DH
1009 }
1010 p += 4;
10674a03
BW
1011 now = ktime_get_real_seconds();
1012 _debug("KIV ISSUE: %llx [%llx]", issue, now);
17926a79
DH
1013
1014 /* check the ticket is in date */
1015 if (issue > now) {
fb46f6ee 1016 abort_code = RXKADNOAUTH;
17926a79 1017 ret = -EKEYREJECTED;
ef68622d 1018 goto other_error;
17926a79
DH
1019 }
1020
1021 if (issue < now - life) {
fb46f6ee 1022 abort_code = RXKADEXPIRED;
17926a79 1023 ret = -EKEYEXPIRED;
ef68622d 1024 goto other_error;
17926a79
DH
1025 }
1026
1027 *_expiry = issue + life;
1028
1029 /* get the service name */
fb46f6ee 1030 name = Z(SNAME);
17926a79
DH
1031 _debug("KIV SNAME: %s", name);
1032
1033 /* get the service instance name */
fb46f6ee 1034 name = Z(INST);
17926a79 1035 _debug("KIV SINST: %s", name);
ef68622d 1036 return 0;
17926a79
DH
1037
1038bad_ticket:
fb46f6ee
DH
1039 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
1040 abort_code = RXKADBADTICKET;
ef68622d
DH
1041 ret = -EPROTO;
1042other_error:
fb46f6ee 1043 *_abort_code = abort_code;
ef68622d
DH
1044 return ret;
1045temporary_error:
1046 return ret;
17926a79
DH
1047}
1048
1049/*
1050 * decrypt the response packet
1051 */
1052static void rxkad_decrypt_response(struct rxrpc_connection *conn,
1053 struct rxkad_response *resp,
1054 const struct rxrpc_crypt *session_key)
1055{
1db88c53 1056 struct skcipher_request *req = rxkad_ci_req;
a263629d 1057 struct scatterlist sg[1];
17926a79
DH
1058 struct rxrpc_crypt iv;
1059
1060 _enter(",,%08x%08x",
1061 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
1062
17926a79 1063 mutex_lock(&rxkad_ci_mutex);
69d826fa 1064 if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x,
1db88c53 1065 sizeof(*session_key)) < 0)
17926a79
DH
1066 BUG();
1067
1068 memcpy(&iv, session_key, sizeof(iv));
17926a79 1069
a263629d
HX
1070 sg_init_table(sg, 1);
1071 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
69d826fa 1072 skcipher_request_set_sync_tfm(req, rxkad_ci);
1afe593b
HX
1073 skcipher_request_set_callback(req, 0, NULL, NULL);
1074 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
1afe593b
HX
1075 crypto_skcipher_decrypt(req);
1076 skcipher_request_zero(req);
1077
17926a79
DH
1078 mutex_unlock(&rxkad_ci_mutex);
1079
1080 _leave("");
1081}
1082
1083/*
1084 * verify a response
1085 */
1086static int rxkad_verify_response(struct rxrpc_connection *conn,
1087 struct sk_buff *skb,
1088 u32 *_abort_code)
1089{
8c2f826d 1090 struct rxkad_response *response;
248f219c 1091 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 1092 struct rxrpc_crypt session_key;
fb46f6ee 1093 const char *eproto;
10674a03 1094 time64_t expiry;
17926a79 1095 void *ticket;
91e916cf
AV
1096 u32 abort_code, version, kvno, ticket_len, level;
1097 __be32 csum;
a1399f8b 1098 int ret, i;
17926a79
DH
1099
1100 _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
1101
8c2f826d
DH
1102 ret = -ENOMEM;
1103 response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
1104 if (!response)
1105 goto temporary_error;
1106
fb46f6ee 1107 eproto = tracepoint_string("rxkad_rsp_short");
17926a79 1108 abort_code = RXKADPACKETSHORT;
775e5b71 1109 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
8c2f826d 1110 response, sizeof(*response)) < 0)
17926a79 1111 goto protocol_error;
8c2f826d 1112 if (!pskb_pull(skb, sizeof(*response)))
17926a79
DH
1113 BUG();
1114
8c2f826d
DH
1115 version = ntohl(response->version);
1116 ticket_len = ntohl(response->ticket_len);
1117 kvno = ntohl(response->kvno);
17926a79 1118 _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
0d12f8a4 1119 sp->hdr.serial, version, kvno, ticket_len);
17926a79 1120
fb46f6ee 1121 eproto = tracepoint_string("rxkad_rsp_ver");
17926a79
DH
1122 abort_code = RXKADINCONSISTENCY;
1123 if (version != RXKAD_VERSION)
4aa9cb32 1124 goto protocol_error;
17926a79 1125
fb46f6ee 1126 eproto = tracepoint_string("rxkad_rsp_tktlen");
17926a79
DH
1127 abort_code = RXKADTICKETLEN;
1128 if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1129 goto protocol_error;
1130
fb46f6ee 1131 eproto = tracepoint_string("rxkad_rsp_unkkey");
17926a79
DH
1132 abort_code = RXKADUNKNOWNKEY;
1133 if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1134 goto protocol_error;
1135
1136 /* extract the kerberos ticket and decrypt and decode it */
ef68622d 1137 ret = -ENOMEM;
17926a79
DH
1138 ticket = kmalloc(ticket_len, GFP_NOFS);
1139 if (!ticket)
ef68622d 1140 goto temporary_error;
17926a79 1141
fb46f6ee 1142 eproto = tracepoint_string("rxkad_tkt_short");
17926a79 1143 abort_code = RXKADPACKETSHORT;
775e5b71
DH
1144 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1145 ticket, ticket_len) < 0)
17926a79
DH
1146 goto protocol_error_free;
1147
fb46f6ee 1148 ret = rxkad_decrypt_ticket(conn, skb, ticket, ticket_len, &session_key,
ef68622d
DH
1149 &expiry, _abort_code);
1150 if (ret < 0)
8c2f826d 1151 goto temporary_error_free_resp;
17926a79
DH
1152
1153 /* use the session key from inside the ticket to decrypt the
1154 * response */
8c2f826d 1155 rxkad_decrypt_response(conn, response, &session_key);
17926a79 1156
fb46f6ee 1157 eproto = tracepoint_string("rxkad_rsp_param");
17926a79 1158 abort_code = RXKADSEALEDINCON;
8c2f826d 1159 if (ntohl(response->encrypted.epoch) != conn->proto.epoch)
17926a79 1160 goto protocol_error_free;
8c2f826d 1161 if (ntohl(response->encrypted.cid) != conn->proto.cid)
17926a79 1162 goto protocol_error_free;
8c2f826d 1163 if (ntohl(response->encrypted.securityIndex) != conn->security_ix)
17926a79 1164 goto protocol_error_free;
8c2f826d
DH
1165 csum = response->encrypted.checksum;
1166 response->encrypted.checksum = 0;
1167 rxkad_calc_response_checksum(response);
fb46f6ee 1168 eproto = tracepoint_string("rxkad_rsp_csum");
8c2f826d 1169 if (response->encrypted.checksum != csum)
17926a79
DH
1170 goto protocol_error_free;
1171
a1399f8b
DH
1172 spin_lock(&conn->channel_lock);
1173 for (i = 0; i < RXRPC_MAXCALLS; i++) {
1174 struct rxrpc_call *call;
8c2f826d 1175 u32 call_id = ntohl(response->encrypted.call_id[i]);
a1399f8b 1176
fb46f6ee 1177 eproto = tracepoint_string("rxkad_rsp_callid");
a1399f8b
DH
1178 if (call_id > INT_MAX)
1179 goto protocol_error_unlock;
1180
fb46f6ee 1181 eproto = tracepoint_string("rxkad_rsp_callctr");
a1399f8b
DH
1182 if (call_id < conn->channels[i].call_counter)
1183 goto protocol_error_unlock;
fb46f6ee
DH
1184
1185 eproto = tracepoint_string("rxkad_rsp_callst");
a1399f8b
DH
1186 if (call_id > conn->channels[i].call_counter) {
1187 call = rcu_dereference_protected(
1188 conn->channels[i].call,
1189 lockdep_is_held(&conn->channel_lock));
1190 if (call && call->state < RXRPC_CALL_COMPLETE)
1191 goto protocol_error_unlock;
1192 conn->channels[i].call_counter = call_id;
1193 }
1194 }
1195 spin_unlock(&conn->channel_lock);
17926a79 1196
fb46f6ee 1197 eproto = tracepoint_string("rxkad_rsp_seq");
17926a79 1198 abort_code = RXKADOUTOFSEQUENCE;
8c2f826d 1199 if (ntohl(response->encrypted.inc_nonce) != conn->security_nonce + 1)
17926a79
DH
1200 goto protocol_error_free;
1201
fb46f6ee 1202 eproto = tracepoint_string("rxkad_rsp_level");
17926a79 1203 abort_code = RXKADLEVELFAIL;
8c2f826d 1204 level = ntohl(response->encrypted.level);
17926a79
DH
1205 if (level > RXRPC_SECURITY_ENCRYPT)
1206 goto protocol_error_free;
19ffa01c 1207 conn->params.security_level = level;
17926a79
DH
1208
1209 /* create a key to hold the security data and expiration time - after
1210 * this the connection security can be handled in exactly the same way
1211 * as for a client connection */
1212 ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
ef68622d 1213 if (ret < 0)
8c2f826d 1214 goto temporary_error_free_ticket;
17926a79
DH
1215
1216 kfree(ticket);
8c2f826d 1217 kfree(response);
17926a79
DH
1218 _leave(" = 0");
1219 return 0;
1220
a1399f8b
DH
1221protocol_error_unlock:
1222 spin_unlock(&conn->channel_lock);
17926a79
DH
1223protocol_error_free:
1224 kfree(ticket);
1225protocol_error:
8c2f826d 1226 kfree(response);
fb46f6ee 1227 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
17926a79 1228 *_abort_code = abort_code;
17926a79 1229 return -EPROTO;
ef68622d 1230
8c2f826d 1231temporary_error_free_ticket:
ef68622d 1232 kfree(ticket);
8c2f826d
DH
1233temporary_error_free_resp:
1234 kfree(response);
ef68622d
DH
1235temporary_error:
1236 /* Ignore the response packet if we got a temporary error such as
1237 * ENOMEM. We just want to send the challenge again. Note that we
1238 * also come out this way if the ticket decryption fails.
1239 */
1240 return ret;
17926a79
DH
1241}
1242
1243/*
1244 * clear the connection security
1245 */
1246static void rxkad_clear(struct rxrpc_connection *conn)
1247{
1248 _enter("");
1249
1250 if (conn->cipher)
69d826fa 1251 crypto_free_sync_skcipher(conn->cipher);
17926a79
DH
1252}
1253
648af7fc
DH
1254/*
1255 * Initialise the rxkad security service.
1256 */
1257static int rxkad_init(void)
1258{
1db88c53
DH
1259 struct crypto_sync_skcipher *tfm;
1260 struct skcipher_request *req;
1261
648af7fc
DH
1262 /* pin the cipher we need so that the crypto layer doesn't invoke
1263 * keventd to go get it */
1db88c53
DH
1264 tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
1265 if (IS_ERR(tfm))
1266 return PTR_ERR(tfm);
1267
1268 req = skcipher_request_alloc(&tfm->base, GFP_KERNEL);
1269 if (!req)
1270 goto nomem_tfm;
1271
1272 rxkad_ci_req = req;
1273 rxkad_ci = tfm;
1274 return 0;
1275
1276nomem_tfm:
1277 crypto_free_sync_skcipher(tfm);
1278 return -ENOMEM;
648af7fc
DH
1279}
1280
1281/*
1282 * Clean up the rxkad security service.
1283 */
1284static void rxkad_exit(void)
1285{
1db88c53
DH
1286 crypto_free_sync_skcipher(rxkad_ci);
1287 skcipher_request_free(rxkad_ci_req);
648af7fc
DH
1288}
1289
17926a79
DH
1290/*
1291 * RxRPC Kerberos-based security
1292 */
648af7fc 1293const struct rxrpc_security rxkad = {
17926a79 1294 .name = "rxkad",
8b815477 1295 .security_index = RXRPC_SECURITY_RXKAD,
648af7fc
DH
1296 .init = rxkad_init,
1297 .exit = rxkad_exit,
17926a79
DH
1298 .init_connection_security = rxkad_init_connection_security,
1299 .prime_packet_security = rxkad_prime_packet_security,
1300 .secure_packet = rxkad_secure_packet,
1301 .verify_packet = rxkad_verify_packet,
1db88c53 1302 .free_call_crypto = rxkad_free_call_crypto,
248f219c 1303 .locate_data = rxkad_locate_data,
17926a79
DH
1304 .issue_challenge = rxkad_issue_challenge,
1305 .respond_to_challenge = rxkad_respond_to_challenge,
1306 .verify_response = rxkad_verify_response,
1307 .clear = rxkad_clear,
1308};