blob: f0aeb8163688e6f4167874d9a6ac0f3bd270891d [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* Kerberos-based RxRPC security
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Herbert Xu1afe5932016-01-24 21:19:01 +080012#include <crypto/skcipher.h>
David Howells17926a72007-04-26 15:48:28 -070013#include <linux/module.h>
14#include <linux/net.h>
15#include <linux/skbuff.h>
16#include <linux/udp.h>
David Howells17926a72007-04-26 15:48:28 -070017#include <linux/scatterlist.h>
18#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070020#include <net/sock.h>
21#include <net/af_rxrpc.h>
David Howells33941282009-09-14 01:17:35 +000022#include <keys/rxrpc-type.h>
David Howellsb1bdb692007-04-27 15:28:45 -070023#define rxrpc_debug rxkad_debug
David Howells17926a72007-04-26 15:48:28 -070024#include "ar-internal.h"
25
26#define RXKAD_VERSION 2
27#define MAXKRB5TICKETLEN 1024
28#define RXKAD_TKT_TYPE_KERBEROS_V5 256
29#define ANAME_SZ 40 /* size of authentication name */
30#define INST_SZ 40 /* size of principal's instance */
31#define REALM_SZ 40 /* size of principal's auth domain */
32#define SNAME_SZ 40 /* size of service name */
33
Eric Dumazet95c96172012-04-15 05:58:06 +000034unsigned int rxrpc_debug;
David Howells17926a72007-04-26 15:48:28 -070035module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
Paul Bolle424b00e2008-04-16 11:08:22 +010036MODULE_PARM_DESC(debug, "rxkad debugging mask");
David Howells17926a72007-04-26 15:48:28 -070037
38struct rxkad_level1_hdr {
39 __be32 data_size; /* true data size (excluding padding) */
40};
41
42struct rxkad_level2_hdr {
43 __be32 data_size; /* true data size (excluding padding) */
44 __be32 checksum; /* decrypted data checksum */
45};
46
David Howells8b815472009-09-14 01:17:30 +000047MODULE_DESCRIPTION("RxRPC network protocol type-2 security (Kerberos 4)");
David Howells17926a72007-04-26 15:48:28 -070048MODULE_AUTHOR("Red Hat, Inc.");
49MODULE_LICENSE("GPL");
50
51/*
52 * this holds a pinned cipher so that keventd doesn't get called by the cipher
53 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
54 * packets
55 */
Herbert Xu1afe5932016-01-24 21:19:01 +080056static struct crypto_skcipher *rxkad_ci;
David Howells17926a72007-04-26 15:48:28 -070057static DEFINE_MUTEX(rxkad_ci_mutex);
58
59/*
60 * initialise connection security
61 */
62static int rxkad_init_connection_security(struct rxrpc_connection *conn)
63{
Herbert Xu1afe5932016-01-24 21:19:01 +080064 struct crypto_skcipher *ci;
David Howells33941282009-09-14 01:17:35 +000065 struct rxrpc_key_token *token;
David Howells17926a72007-04-26 15:48:28 -070066 int ret;
67
68 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key));
69
David Howells146aa8b2015-10-21 14:04:48 +010070 token = conn->key->payload.data[0];
David Howells33941282009-09-14 01:17:35 +000071 conn->security_ix = token->security_index;
David Howells17926a72007-04-26 15:48:28 -070072
Herbert Xu1afe5932016-01-24 21:19:01 +080073 ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
David Howells17926a72007-04-26 15:48:28 -070074 if (IS_ERR(ci)) {
75 _debug("no cipher");
76 ret = PTR_ERR(ci);
77 goto error;
78 }
79
Herbert Xu1afe5932016-01-24 21:19:01 +080080 if (crypto_skcipher_setkey(ci, token->kad->session_key,
81 sizeof(token->kad->session_key)) < 0)
David Howells17926a72007-04-26 15:48:28 -070082 BUG();
83
84 switch (conn->security_level) {
85 case RXRPC_SECURITY_PLAIN:
86 break;
87 case RXRPC_SECURITY_AUTH:
88 conn->size_align = 8;
89 conn->security_size = sizeof(struct rxkad_level1_hdr);
90 conn->header_size += sizeof(struct rxkad_level1_hdr);
91 break;
92 case RXRPC_SECURITY_ENCRYPT:
93 conn->size_align = 8;
94 conn->security_size = sizeof(struct rxkad_level2_hdr);
95 conn->header_size += sizeof(struct rxkad_level2_hdr);
96 break;
97 default:
98 ret = -EKEYREJECTED;
99 goto error;
100 }
101
102 conn->cipher = ci;
103 ret = 0;
104error:
105 _leave(" = %d", ret);
106 return ret;
107}
108
109/*
110 * prime the encryption state with the invariant parts of a connection's
111 * description
112 */
113static void rxkad_prime_packet_security(struct rxrpc_connection *conn)
114{
David Howells33941282009-09-14 01:17:35 +0000115 struct rxrpc_key_token *token;
Herbert Xu1afe5932016-01-24 21:19:01 +0800116 SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700117 struct scatterlist sg[2];
118 struct rxrpc_crypt iv;
119 struct {
120 __be32 x[4];
121 } tmpbuf __attribute__((aligned(16))); /* must all be in same page */
122
123 _enter("");
124
125 if (!conn->key)
126 return;
127
David Howells146aa8b2015-10-21 14:04:48 +0100128 token = conn->key->payload.data[0];
David Howells33941282009-09-14 01:17:35 +0000129 memcpy(&iv, token->kad->session_key, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700130
David Howells0d12f8a2016-03-04 15:53:46 +0000131 tmpbuf.x[0] = htonl(conn->epoch);
132 tmpbuf.x[1] = htonl(conn->cid);
David Howells17926a72007-04-26 15:48:28 -0700133 tmpbuf.x[2] = 0;
134 tmpbuf.x[3] = htonl(conn->security_ix);
135
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700136 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
137 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
Herbert Xu1afe5932016-01-24 21:19:01 +0800138
139 skcipher_request_set_tfm(req, conn->cipher);
140 skcipher_request_set_callback(req, 0, NULL, NULL);
141 skcipher_request_set_crypt(req, &sg[1], &sg[0], sizeof(tmpbuf), iv.x);
142
143 crypto_skcipher_encrypt(req);
144 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700145
146 memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv));
David Howells2b15ef12016-03-04 15:59:13 +0000147 ASSERTCMP((u32 __force)conn->csum_iv.n[0], ==, (u32 __force)tmpbuf.x[2]);
David Howells17926a72007-04-26 15:48:28 -0700148
149 _leave("");
150}
151
152/*
153 * partially encrypt a packet (level 1 security)
154 */
155static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
156 struct sk_buff *skb,
157 u32 data_size,
158 void *sechdr)
159{
160 struct rxrpc_skb_priv *sp;
Herbert Xu1afe5932016-01-24 21:19:01 +0800161 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700162 struct rxrpc_crypt iv;
163 struct scatterlist sg[2];
164 struct {
165 struct rxkad_level1_hdr hdr;
166 __be32 first; /* first four bytes of data and padding */
167 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
168 u16 check;
169
170 sp = rxrpc_skb(skb);
171
172 _enter("");
173
David Howells0d12f8a2016-03-04 15:53:46 +0000174 check = sp->hdr.seq ^ sp->hdr.callNumber;
175 data_size |= (u32)check << 16;
David Howells17926a72007-04-26 15:48:28 -0700176
177 tmpbuf.hdr.data_size = htonl(data_size);
178 memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first));
179
180 /* start the encryption afresh */
181 memset(&iv, 0, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700182
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700183 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
184 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
Herbert Xu1afe5932016-01-24 21:19:01 +0800185
186 skcipher_request_set_tfm(req, call->conn->cipher);
187 skcipher_request_set_callback(req, 0, NULL, NULL);
188 skcipher_request_set_crypt(req, &sg[1], &sg[0], sizeof(tmpbuf), iv.x);
189
190 crypto_skcipher_encrypt(req);
191 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700192
193 memcpy(sechdr, &tmpbuf, sizeof(tmpbuf));
194
195 _leave(" = 0");
196 return 0;
197}
198
199/*
200 * wholly encrypt a packet (level 2 security)
201 */
202static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
David Howellsb4f13422016-03-04 15:56:19 +0000203 struct sk_buff *skb,
204 u32 data_size,
205 void *sechdr)
David Howells17926a72007-04-26 15:48:28 -0700206{
David Howells33941282009-09-14 01:17:35 +0000207 const struct rxrpc_key_token *token;
David Howells17926a72007-04-26 15:48:28 -0700208 struct rxkad_level2_hdr rxkhdr
209 __attribute__((aligned(8))); /* must be all on one page */
210 struct rxrpc_skb_priv *sp;
Herbert Xu1afe5932016-01-24 21:19:01 +0800211 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700212 struct rxrpc_crypt iv;
213 struct scatterlist sg[16];
214 struct sk_buff *trailer;
Eric Dumazet95c96172012-04-15 05:58:06 +0000215 unsigned int len;
David Howells17926a72007-04-26 15:48:28 -0700216 u16 check;
217 int nsg;
Herbert Xu1afe5932016-01-24 21:19:01 +0800218 int err;
David Howells17926a72007-04-26 15:48:28 -0700219
220 sp = rxrpc_skb(skb);
221
222 _enter("");
223
David Howells0d12f8a2016-03-04 15:53:46 +0000224 check = sp->hdr.seq ^ sp->hdr.callNumber;
David Howells17926a72007-04-26 15:48:28 -0700225
David Howells0d12f8a2016-03-04 15:53:46 +0000226 rxkhdr.data_size = htonl(data_size | (u32)check << 16);
David Howells17926a72007-04-26 15:48:28 -0700227 rxkhdr.checksum = 0;
228
229 /* encrypt from the session key */
David Howells146aa8b2015-10-21 14:04:48 +0100230 token = call->conn->key->payload.data[0];
David Howells33941282009-09-14 01:17:35 +0000231 memcpy(&iv, token->kad->session_key, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700232
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700233 sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
234 sg_init_one(&sg[1], &rxkhdr, sizeof(rxkhdr));
Herbert Xu1afe5932016-01-24 21:19:01 +0800235
236 skcipher_request_set_tfm(req, call->conn->cipher);
237 skcipher_request_set_callback(req, 0, NULL, NULL);
238 skcipher_request_set_crypt(req, &sg[1], &sg[0], sizeof(rxkhdr), iv.x);
239
240 crypto_skcipher_encrypt(req);
David Howells17926a72007-04-26 15:48:28 -0700241
242 /* we want to encrypt the skbuff in-place */
243 nsg = skb_cow_data(skb, 0, &trailer);
Herbert Xu1afe5932016-01-24 21:19:01 +0800244 err = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700245 if (nsg < 0 || nsg > 16)
Herbert Xu1afe5932016-01-24 21:19:01 +0800246 goto out;
David Howells17926a72007-04-26 15:48:28 -0700247
248 len = data_size + call->conn->size_align - 1;
249 len &= ~(call->conn->size_align - 1);
250
David S. Miller51c739d2007-10-30 21:29:29 -0700251 sg_init_table(sg, nsg);
252 skb_to_sgvec(skb, sg, 0, len);
Herbert Xu1afe5932016-01-24 21:19:01 +0800253
254 skcipher_request_set_crypt(req, sg, sg, len, iv.x);
255
256 crypto_skcipher_encrypt(req);
David Howells17926a72007-04-26 15:48:28 -0700257
258 _leave(" = 0");
Herbert Xu1afe5932016-01-24 21:19:01 +0800259 err = 0;
260
261out:
262 skcipher_request_zero(req);
263 return err;
David Howells17926a72007-04-26 15:48:28 -0700264}
265
266/*
267 * checksum an RxRPC packet header
268 */
269static int rxkad_secure_packet(const struct rxrpc_call *call,
David Howellsb4f13422016-03-04 15:56:19 +0000270 struct sk_buff *skb,
271 size_t data_size,
272 void *sechdr)
David Howells17926a72007-04-26 15:48:28 -0700273{
274 struct rxrpc_skb_priv *sp;
Herbert Xu1afe5932016-01-24 21:19:01 +0800275 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700276 struct rxrpc_crypt iv;
277 struct scatterlist sg[2];
278 struct {
279 __be32 x[2];
280 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
David Howells0d12f8a2016-03-04 15:53:46 +0000281 u32 x, y;
David Howells17926a72007-04-26 15:48:28 -0700282 int ret;
283
284 sp = rxrpc_skb(skb);
285
286 _enter("{%d{%x}},{#%u},%zu,",
David Howells0d12f8a2016-03-04 15:53:46 +0000287 call->debug_id, key_serial(call->conn->key), sp->hdr.seq,
David Howells17926a72007-04-26 15:48:28 -0700288 data_size);
289
290 if (!call->conn->cipher)
291 return 0;
292
293 ret = key_validate(call->conn->key);
294 if (ret < 0)
295 return ret;
296
297 /* continue encrypting from where we left off */
298 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700299
300 /* calculate the security checksum */
David Howells0d12f8a2016-03-04 15:53:46 +0000301 x = call->channel << (32 - RXRPC_CIDSHIFT);
302 x |= sp->hdr.seq & 0x3fffffff;
303 tmpbuf.x[0] = htonl(sp->hdr.callNumber);
304 tmpbuf.x[1] = htonl(x);
David Howells17926a72007-04-26 15:48:28 -0700305
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700306 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
307 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
Herbert Xu1afe5932016-01-24 21:19:01 +0800308
309 skcipher_request_set_tfm(req, call->conn->cipher);
310 skcipher_request_set_callback(req, 0, NULL, NULL);
311 skcipher_request_set_crypt(req, &sg[1], &sg[0], sizeof(tmpbuf), iv.x);
312
313 crypto_skcipher_encrypt(req);
314 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700315
Al Viro91e916c2008-03-29 03:08:38 +0000316 y = ntohl(tmpbuf.x[1]);
317 y = (y >> 16) & 0xffff;
318 if (y == 0)
319 y = 1; /* zero checksums are not permitted */
David Howells0d12f8a2016-03-04 15:53:46 +0000320 sp->hdr.cksum = y;
David Howells17926a72007-04-26 15:48:28 -0700321
322 switch (call->conn->security_level) {
323 case RXRPC_SECURITY_PLAIN:
324 ret = 0;
325 break;
326 case RXRPC_SECURITY_AUTH:
327 ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
328 break;
329 case RXRPC_SECURITY_ENCRYPT:
330 ret = rxkad_secure_packet_encrypt(call, skb, data_size,
331 sechdr);
332 break;
333 default:
334 ret = -EPERM;
335 break;
336 }
337
Al Viro91e916c2008-03-29 03:08:38 +0000338 _leave(" = %d [set %hx]", ret, y);
David Howells17926a72007-04-26 15:48:28 -0700339 return ret;
340}
341
342/*
343 * decrypt partial encryption on a packet (level 1 security)
344 */
345static int rxkad_verify_packet_auth(const struct rxrpc_call *call,
346 struct sk_buff *skb,
347 u32 *_abort_code)
348{
349 struct rxkad_level1_hdr sechdr;
350 struct rxrpc_skb_priv *sp;
Herbert Xu1afe5932016-01-24 21:19:01 +0800351 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700352 struct rxrpc_crypt iv;
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700353 struct scatterlist sg[16];
David Howells17926a72007-04-26 15:48:28 -0700354 struct sk_buff *trailer;
355 u32 data_size, buf;
356 u16 check;
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700357 int nsg;
David Howells17926a72007-04-26 15:48:28 -0700358
359 _enter("");
360
361 sp = rxrpc_skb(skb);
362
363 /* we want to decrypt the skbuff in-place */
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700364 nsg = skb_cow_data(skb, 0, &trailer);
365 if (nsg < 0 || nsg > 16)
David Howells17926a72007-04-26 15:48:28 -0700366 goto nomem;
367
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700368 sg_init_table(sg, nsg);
David S. Miller51c739d2007-10-30 21:29:29 -0700369 skb_to_sgvec(skb, sg, 0, 8);
David Howells17926a72007-04-26 15:48:28 -0700370
371 /* start the decryption afresh */
372 memset(&iv, 0, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700373
Herbert Xu1afe5932016-01-24 21:19:01 +0800374 skcipher_request_set_tfm(req, call->conn->cipher);
375 skcipher_request_set_callback(req, 0, NULL, NULL);
376 skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
377
378 crypto_skcipher_decrypt(req);
379 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700380
381 /* remove the decrypted packet length */
382 if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
383 goto datalen_error;
384 if (!skb_pull(skb, sizeof(sechdr)))
385 BUG();
386
387 buf = ntohl(sechdr.data_size);
388 data_size = buf & 0xffff;
389
390 check = buf >> 16;
David Howells0d12f8a2016-03-04 15:53:46 +0000391 check ^= sp->hdr.seq ^ sp->hdr.callNumber;
David Howells17926a72007-04-26 15:48:28 -0700392 check &= 0xffff;
393 if (check != 0) {
394 *_abort_code = RXKADSEALEDINCON;
395 goto protocol_error;
396 }
397
398 /* shorten the packet to remove the padding */
399 if (data_size > skb->len)
400 goto datalen_error;
401 else if (data_size < skb->len)
402 skb->len = data_size;
403
404 _leave(" = 0 [dlen=%x]", data_size);
405 return 0;
406
407datalen_error:
408 *_abort_code = RXKADDATALEN;
409protocol_error:
410 _leave(" = -EPROTO");
411 return -EPROTO;
412
413nomem:
414 _leave(" = -ENOMEM");
415 return -ENOMEM;
416}
417
418/*
419 * wholly decrypt a packet (level 2 security)
420 */
421static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call,
422 struct sk_buff *skb,
423 u32 *_abort_code)
424{
David Howells33941282009-09-14 01:17:35 +0000425 const struct rxrpc_key_token *token;
David Howells17926a72007-04-26 15:48:28 -0700426 struct rxkad_level2_hdr sechdr;
427 struct rxrpc_skb_priv *sp;
Herbert Xu1afe5932016-01-24 21:19:01 +0800428 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700429 struct rxrpc_crypt iv;
430 struct scatterlist _sg[4], *sg;
431 struct sk_buff *trailer;
432 u32 data_size, buf;
433 u16 check;
434 int nsg;
435
436 _enter(",{%d}", skb->len);
437
438 sp = rxrpc_skb(skb);
439
440 /* we want to decrypt the skbuff in-place */
441 nsg = skb_cow_data(skb, 0, &trailer);
442 if (nsg < 0)
443 goto nomem;
444
445 sg = _sg;
446 if (unlikely(nsg > 4)) {
447 sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
448 if (!sg)
449 goto nomem;
450 }
451
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700452 sg_init_table(sg, nsg);
David S. Miller51c739d2007-10-30 21:29:29 -0700453 skb_to_sgvec(skb, sg, 0, skb->len);
David Howells17926a72007-04-26 15:48:28 -0700454
455 /* decrypt from the session key */
David Howells146aa8b2015-10-21 14:04:48 +0100456 token = call->conn->key->payload.data[0];
David Howells33941282009-09-14 01:17:35 +0000457 memcpy(&iv, token->kad->session_key, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700458
Herbert Xu1afe5932016-01-24 21:19:01 +0800459 skcipher_request_set_tfm(req, call->conn->cipher);
460 skcipher_request_set_callback(req, 0, NULL, NULL);
461 skcipher_request_set_crypt(req, sg, sg, skb->len, iv.x);
462
463 crypto_skcipher_decrypt(req);
464 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700465 if (sg != _sg)
466 kfree(sg);
467
468 /* remove the decrypted packet length */
469 if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
470 goto datalen_error;
471 if (!skb_pull(skb, sizeof(sechdr)))
472 BUG();
473
474 buf = ntohl(sechdr.data_size);
475 data_size = buf & 0xffff;
476
477 check = buf >> 16;
David Howells0d12f8a2016-03-04 15:53:46 +0000478 check ^= sp->hdr.seq ^ sp->hdr.callNumber;
David Howells17926a72007-04-26 15:48:28 -0700479 check &= 0xffff;
480 if (check != 0) {
481 *_abort_code = RXKADSEALEDINCON;
482 goto protocol_error;
483 }
484
485 /* shorten the packet to remove the padding */
486 if (data_size > skb->len)
487 goto datalen_error;
488 else if (data_size < skb->len)
489 skb->len = data_size;
490
491 _leave(" = 0 [dlen=%x]", data_size);
492 return 0;
493
494datalen_error:
495 *_abort_code = RXKADDATALEN;
496protocol_error:
497 _leave(" = -EPROTO");
498 return -EPROTO;
499
500nomem:
501 _leave(" = -ENOMEM");
502 return -ENOMEM;
503}
504
505/*
506 * verify the security on a received packet
507 */
508static int rxkad_verify_packet(const struct rxrpc_call *call,
509 struct sk_buff *skb,
510 u32 *_abort_code)
511{
Herbert Xu1afe5932016-01-24 21:19:01 +0800512 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700513 struct rxrpc_skb_priv *sp;
514 struct rxrpc_crypt iv;
515 struct scatterlist sg[2];
516 struct {
517 __be32 x[2];
518 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
David Howells0d12f8a2016-03-04 15:53:46 +0000519 u16 cksum;
520 u32 x, y;
David Howells17926a72007-04-26 15:48:28 -0700521 int ret;
522
523 sp = rxrpc_skb(skb);
524
525 _enter("{%d{%x}},{#%u}",
David Howells0d12f8a2016-03-04 15:53:46 +0000526 call->debug_id, key_serial(call->conn->key), sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700527
528 if (!call->conn->cipher)
529 return 0;
530
David Howells8b815472009-09-14 01:17:30 +0000531 if (sp->hdr.securityIndex != RXRPC_SECURITY_RXKAD) {
David Howells17926a72007-04-26 15:48:28 -0700532 *_abort_code = RXKADINCONSISTENCY;
533 _leave(" = -EPROTO [not rxkad]");
534 return -EPROTO;
535 }
536
537 /* continue encrypting from where we left off */
538 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700539
540 /* validate the security checksum */
David Howells0d12f8a2016-03-04 15:53:46 +0000541 x = call->channel << (32 - RXRPC_CIDSHIFT);
542 x |= sp->hdr.seq & 0x3fffffff;
543 tmpbuf.x[0] = htonl(call->call_id);
544 tmpbuf.x[1] = htonl(x);
David Howells17926a72007-04-26 15:48:28 -0700545
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700546 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
547 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
Herbert Xu1afe5932016-01-24 21:19:01 +0800548
549 skcipher_request_set_tfm(req, call->conn->cipher);
550 skcipher_request_set_callback(req, 0, NULL, NULL);
551 skcipher_request_set_crypt(req, &sg[1], &sg[0], sizeof(tmpbuf), iv.x);
552
553 crypto_skcipher_encrypt(req);
554 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700555
Al Viro91e916c2008-03-29 03:08:38 +0000556 y = ntohl(tmpbuf.x[1]);
David Howells0d12f8a2016-03-04 15:53:46 +0000557 cksum = (y >> 16) & 0xffff;
558 if (cksum == 0)
559 cksum = 1; /* zero checksums are not permitted */
David Howells17926a72007-04-26 15:48:28 -0700560
David Howells17926a72007-04-26 15:48:28 -0700561 if (sp->hdr.cksum != cksum) {
562 *_abort_code = RXKADSEALEDINCON;
563 _leave(" = -EPROTO [csum failed]");
564 return -EPROTO;
565 }
566
567 switch (call->conn->security_level) {
568 case RXRPC_SECURITY_PLAIN:
569 ret = 0;
570 break;
571 case RXRPC_SECURITY_AUTH:
572 ret = rxkad_verify_packet_auth(call, skb, _abort_code);
573 break;
574 case RXRPC_SECURITY_ENCRYPT:
575 ret = rxkad_verify_packet_encrypt(call, skb, _abort_code);
576 break;
577 default:
578 ret = -ENOANO;
579 break;
580 }
581
582 _leave(" = %d", ret);
583 return ret;
584}
585
586/*
587 * issue a challenge
588 */
589static int rxkad_issue_challenge(struct rxrpc_connection *conn)
590{
591 struct rxkad_challenge challenge;
David Howells0d12f8a2016-03-04 15:53:46 +0000592 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700593 struct msghdr msg;
594 struct kvec iov[2];
595 size_t len;
David Howells0d12f8a2016-03-04 15:53:46 +0000596 u32 serial;
David Howells17926a72007-04-26 15:48:28 -0700597 int ret;
598
599 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
600
601 ret = key_validate(conn->key);
602 if (ret < 0)
603 return ret;
604
605 get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
606
607 challenge.version = htonl(2);
608 challenge.nonce = htonl(conn->security_nonce);
609 challenge.min_level = htonl(0);
610 challenge.__padding = 0;
611
612 msg.msg_name = &conn->trans->peer->srx.transport.sin;
613 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
614 msg.msg_control = NULL;
615 msg.msg_controllen = 0;
616 msg.msg_flags = 0;
617
David Howells0d12f8a2016-03-04 15:53:46 +0000618 whdr.epoch = htonl(conn->epoch);
619 whdr.cid = htonl(conn->cid);
620 whdr.callNumber = 0;
621 whdr.seq = 0;
622 whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
623 whdr.flags = conn->out_clientflag;
624 whdr.userStatus = 0;
625 whdr.securityIndex = conn->security_ix;
626 whdr._rsvd = 0;
627 whdr.serviceId = htons(conn->service_id);
David Howells17926a72007-04-26 15:48:28 -0700628
David Howells0d12f8a2016-03-04 15:53:46 +0000629 iov[0].iov_base = &whdr;
630 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700631 iov[1].iov_base = &challenge;
632 iov[1].iov_len = sizeof(challenge);
633
634 len = iov[0].iov_len + iov[1].iov_len;
635
David Howells0d12f8a2016-03-04 15:53:46 +0000636 serial = atomic_inc_return(&conn->serial);
637 whdr.serial = htonl(serial);
638 _proto("Tx CHALLENGE %%%u", serial);
David Howells17926a72007-04-26 15:48:28 -0700639
640 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
641 if (ret < 0) {
642 _debug("sendmsg failed: %d", ret);
643 return -EAGAIN;
644 }
645
646 _leave(" = 0");
647 return 0;
648}
649
650/*
651 * send a Kerberos security response
652 */
653static int rxkad_send_response(struct rxrpc_connection *conn,
David Howells0d12f8a2016-03-04 15:53:46 +0000654 struct rxrpc_host_header *hdr,
David Howells17926a72007-04-26 15:48:28 -0700655 struct rxkad_response *resp,
656 const struct rxkad_key *s2)
657{
David Howells0d12f8a2016-03-04 15:53:46 +0000658 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700659 struct msghdr msg;
660 struct kvec iov[3];
661 size_t len;
David Howells0d12f8a2016-03-04 15:53:46 +0000662 u32 serial;
David Howells17926a72007-04-26 15:48:28 -0700663 int ret;
664
665 _enter("");
666
667 msg.msg_name = &conn->trans->peer->srx.transport.sin;
668 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
669 msg.msg_control = NULL;
670 msg.msg_controllen = 0;
671 msg.msg_flags = 0;
672
David Howells0d12f8a2016-03-04 15:53:46 +0000673 memset(&whdr, 0, sizeof(whdr));
674 whdr.epoch = htonl(hdr->epoch);
675 whdr.cid = htonl(hdr->cid);
676 whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
677 whdr.flags = conn->out_clientflag;
678 whdr.securityIndex = hdr->securityIndex;
679 whdr.serviceId = htons(hdr->serviceId);
David Howells17926a72007-04-26 15:48:28 -0700680
David Howells0d12f8a2016-03-04 15:53:46 +0000681 iov[0].iov_base = &whdr;
682 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700683 iov[1].iov_base = resp;
684 iov[1].iov_len = sizeof(*resp);
David Howells0d12f8a2016-03-04 15:53:46 +0000685 iov[2].iov_base = (void *)s2->ticket;
David Howells17926a72007-04-26 15:48:28 -0700686 iov[2].iov_len = s2->ticket_len;
687
688 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
689
David Howells0d12f8a2016-03-04 15:53:46 +0000690 serial = atomic_inc_return(&conn->serial);
691 whdr.serial = htonl(serial);
692 _proto("Tx RESPONSE %%%u", serial);
David Howells17926a72007-04-26 15:48:28 -0700693
694 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 3, len);
695 if (ret < 0) {
696 _debug("sendmsg failed: %d", ret);
697 return -EAGAIN;
698 }
699
700 _leave(" = 0");
701 return 0;
702}
703
704/*
705 * calculate the response checksum
706 */
707static void rxkad_calc_response_checksum(struct rxkad_response *response)
708{
709 u32 csum = 1000003;
710 int loop;
711 u8 *p = (u8 *) response;
712
713 for (loop = sizeof(*response); loop > 0; loop--)
714 csum = csum * 0x10204081 + *p++;
715
716 response->encrypted.checksum = htonl(csum);
717}
718
719/*
720 * load a scatterlist with a potentially split-page buffer
721 */
722static void rxkad_sg_set_buf2(struct scatterlist sg[2],
723 void *buf, size_t buflen)
724{
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700725 int nsg = 1;
David Howells17926a72007-04-26 15:48:28 -0700726
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700727 sg_init_table(sg, 2);
David Howells17926a72007-04-26 15:48:28 -0700728
729 sg_set_buf(&sg[0], buf, buflen);
730 if (sg[0].offset + buflen > PAGE_SIZE) {
731 /* the buffer was split over two pages */
732 sg[0].length = PAGE_SIZE - sg[0].offset;
733 sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length);
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700734 nsg++;
David Howells17926a72007-04-26 15:48:28 -0700735 }
736
Jens Axboec46f2332007-10-31 12:06:37 +0100737 sg_mark_end(&sg[nsg - 1]);
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700738
David Howells17926a72007-04-26 15:48:28 -0700739 ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
740}
741
742/*
743 * encrypt the response packet
744 */
745static void rxkad_encrypt_response(struct rxrpc_connection *conn,
746 struct rxkad_response *resp,
747 const struct rxkad_key *s2)
748{
Herbert Xu1afe5932016-01-24 21:19:01 +0800749 SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
David Howells17926a72007-04-26 15:48:28 -0700750 struct rxrpc_crypt iv;
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700751 struct scatterlist sg[2];
David Howells17926a72007-04-26 15:48:28 -0700752
753 /* continue encrypting from where we left off */
754 memcpy(&iv, s2->session_key, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700755
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700756 rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
Herbert Xu1afe5932016-01-24 21:19:01 +0800757
758 skcipher_request_set_tfm(req, conn->cipher);
759 skcipher_request_set_callback(req, 0, NULL, NULL);
760 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
761
762 crypto_skcipher_encrypt(req);
763 skcipher_request_zero(req);
David Howells17926a72007-04-26 15:48:28 -0700764}
765
766/*
767 * respond to a challenge packet
768 */
769static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
770 struct sk_buff *skb,
771 u32 *_abort_code)
772{
David Howells33941282009-09-14 01:17:35 +0000773 const struct rxrpc_key_token *token;
David Howells17926a72007-04-26 15:48:28 -0700774 struct rxkad_challenge challenge;
775 struct rxkad_response resp
776 __attribute__((aligned(8))); /* must be aligned for crypto */
777 struct rxrpc_skb_priv *sp;
778 u32 version, nonce, min_level, abort_code;
779 int ret;
780
781 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
782
783 if (!conn->key) {
784 _leave(" = -EPROTO [no key]");
785 return -EPROTO;
786 }
787
788 ret = key_validate(conn->key);
789 if (ret < 0) {
790 *_abort_code = RXKADEXPIRED;
791 return ret;
792 }
793
794 abort_code = RXKADPACKETSHORT;
795 sp = rxrpc_skb(skb);
796 if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0)
797 goto protocol_error;
798
799 version = ntohl(challenge.version);
800 nonce = ntohl(challenge.nonce);
801 min_level = ntohl(challenge.min_level);
802
803 _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
David Howells0d12f8a2016-03-04 15:53:46 +0000804 sp->hdr.serial, version, nonce, min_level);
David Howells17926a72007-04-26 15:48:28 -0700805
806 abort_code = RXKADINCONSISTENCY;
807 if (version != RXKAD_VERSION)
808 goto protocol_error;
809
810 abort_code = RXKADLEVELFAIL;
811 if (conn->security_level < min_level)
812 goto protocol_error;
813
David Howells146aa8b2015-10-21 14:04:48 +0100814 token = conn->key->payload.data[0];
David Howells17926a72007-04-26 15:48:28 -0700815
816 /* build the response packet */
817 memset(&resp, 0, sizeof(resp));
818
David Howells098a2092016-03-04 15:59:00 +0000819 resp.version = htonl(RXKAD_VERSION);
820 resp.encrypted.epoch = htonl(conn->epoch);
821 resp.encrypted.cid = htonl(conn->cid);
822 resp.encrypted.securityIndex = htonl(conn->security_ix);
823 resp.encrypted.inc_nonce = htonl(nonce + 1);
824 resp.encrypted.level = htonl(conn->security_level);
825 resp.kvno = htonl(token->kad->kvno);
826 resp.ticket_len = htonl(token->kad->ticket_len);
827
David Howells17926a72007-04-26 15:48:28 -0700828 resp.encrypted.call_id[0] =
David Howells0d12f8a2016-03-04 15:53:46 +0000829 htonl(conn->channels[0] ? conn->channels[0]->call_id : 0);
David Howells17926a72007-04-26 15:48:28 -0700830 resp.encrypted.call_id[1] =
David Howells0d12f8a2016-03-04 15:53:46 +0000831 htonl(conn->channels[1] ? conn->channels[1]->call_id : 0);
David Howells17926a72007-04-26 15:48:28 -0700832 resp.encrypted.call_id[2] =
David Howells0d12f8a2016-03-04 15:53:46 +0000833 htonl(conn->channels[2] ? conn->channels[2]->call_id : 0);
David Howells17926a72007-04-26 15:48:28 -0700834 resp.encrypted.call_id[3] =
David Howells0d12f8a2016-03-04 15:53:46 +0000835 htonl(conn->channels[3] ? conn->channels[3]->call_id : 0);
David Howells17926a72007-04-26 15:48:28 -0700836
837 /* calculate the response checksum and then do the encryption */
838 rxkad_calc_response_checksum(&resp);
David Howells33941282009-09-14 01:17:35 +0000839 rxkad_encrypt_response(conn, &resp, token->kad);
840 return rxkad_send_response(conn, &sp->hdr, &resp, token->kad);
David Howells17926a72007-04-26 15:48:28 -0700841
842protocol_error:
843 *_abort_code = abort_code;
844 _leave(" = -EPROTO [%d]", abort_code);
845 return -EPROTO;
846}
847
848/*
849 * decrypt the kerberos IV ticket in the response
850 */
851static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
852 void *ticket, size_t ticket_len,
853 struct rxrpc_crypt *_session_key,
854 time_t *_expiry,
855 u32 *_abort_code)
856{
Herbert Xu1afe5932016-01-24 21:19:01 +0800857 struct skcipher_request *req;
David Howells17926a72007-04-26 15:48:28 -0700858 struct rxrpc_crypt iv, key;
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700859 struct scatterlist sg[1];
David Howells17926a72007-04-26 15:48:28 -0700860 struct in_addr addr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000861 unsigned int life;
David Howells17926a72007-04-26 15:48:28 -0700862 time_t issue, now;
863 bool little_endian;
864 int ret;
865 u8 *p, *q, *name, *end;
866
867 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
868
869 *_expiry = 0;
870
871 ret = key_validate(conn->server_key);
872 if (ret < 0) {
873 switch (ret) {
874 case -EKEYEXPIRED:
875 *_abort_code = RXKADEXPIRED;
876 goto error;
877 default:
878 *_abort_code = RXKADNOAUTH;
879 goto error;
880 }
881 }
882
David Howells146aa8b2015-10-21 14:04:48 +0100883 ASSERT(conn->server_key->payload.data[0] != NULL);
David Howells17926a72007-04-26 15:48:28 -0700884 ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
885
David Howells146aa8b2015-10-21 14:04:48 +0100886 memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -0700887
Herbert Xu1afe5932016-01-24 21:19:01 +0800888 req = skcipher_request_alloc(conn->server_key->payload.data[0],
889 GFP_NOFS);
890 if (!req) {
891 *_abort_code = RXKADNOAUTH;
892 ret = -ENOMEM;
893 goto error;
894 }
David Howells17926a72007-04-26 15:48:28 -0700895
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700896 sg_init_one(&sg[0], ticket, ticket_len);
Herbert Xu1afe5932016-01-24 21:19:01 +0800897
898 skcipher_request_set_callback(req, 0, NULL, NULL);
899 skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
900
901 crypto_skcipher_decrypt(req);
902 skcipher_request_free(req);
David Howells17926a72007-04-26 15:48:28 -0700903
904 p = ticket;
905 end = p + ticket_len;
906
907#define Z(size) \
908 ({ \
909 u8 *__str = p; \
910 q = memchr(p, 0, end - p); \
911 if (!q || q - p > (size)) \
912 goto bad_ticket; \
913 for (; p < q; p++) \
914 if (!isprint(*p)) \
915 goto bad_ticket; \
916 p++; \
917 __str; \
918 })
919
920 /* extract the ticket flags */
921 _debug("KIV FLAGS: %x", *p);
922 little_endian = *p & 1;
923 p++;
924
925 /* extract the authentication name */
926 name = Z(ANAME_SZ);
927 _debug("KIV ANAME: %s", name);
928
929 /* extract the principal's instance */
930 name = Z(INST_SZ);
931 _debug("KIV INST : %s", name);
932
933 /* extract the principal's authentication domain */
934 name = Z(REALM_SZ);
935 _debug("KIV REALM: %s", name);
936
937 if (end - p < 4 + 8 + 4 + 2)
938 goto bad_ticket;
939
940 /* get the IPv4 address of the entity that requested the ticket */
941 memcpy(&addr, p, sizeof(addr));
942 p += 4;
Harvey Harrison21454aa2008-10-31 00:54:56 -0700943 _debug("KIV ADDR : %pI4", &addr);
David Howells17926a72007-04-26 15:48:28 -0700944
945 /* get the session key from the ticket */
946 memcpy(&key, p, sizeof(key));
947 p += 8;
948 _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
949 memcpy(_session_key, &key, sizeof(key));
950
951 /* get the ticket's lifetime */
952 life = *p++ * 5 * 60;
953 _debug("KIV LIFE : %u", life);
954
955 /* get the issue time of the ticket */
956 if (little_endian) {
957 __le32 stamp;
958 memcpy(&stamp, p, 4);
959 issue = le32_to_cpu(stamp);
960 } else {
961 __be32 stamp;
962 memcpy(&stamp, p, 4);
963 issue = be32_to_cpu(stamp);
964 }
965 p += 4;
john stultz2c6b47d2007-07-24 17:47:43 -0700966 now = get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700967 _debug("KIV ISSUE: %lx [%lx]", issue, now);
968
969 /* check the ticket is in date */
970 if (issue > now) {
971 *_abort_code = RXKADNOAUTH;
972 ret = -EKEYREJECTED;
973 goto error;
974 }
975
976 if (issue < now - life) {
977 *_abort_code = RXKADEXPIRED;
978 ret = -EKEYEXPIRED;
979 goto error;
980 }
981
982 *_expiry = issue + life;
983
984 /* get the service name */
985 name = Z(SNAME_SZ);
986 _debug("KIV SNAME: %s", name);
987
988 /* get the service instance name */
989 name = Z(INST_SZ);
990 _debug("KIV SINST: %s", name);
991
992 ret = 0;
993error:
994 _leave(" = %d", ret);
995 return ret;
996
997bad_ticket:
998 *_abort_code = RXKADBADTICKET;
999 ret = -EBADMSG;
1000 goto error;
1001}
1002
1003/*
1004 * decrypt the response packet
1005 */
1006static void rxkad_decrypt_response(struct rxrpc_connection *conn,
1007 struct rxkad_response *resp,
1008 const struct rxrpc_crypt *session_key)
1009{
Herbert Xu1afe5932016-01-24 21:19:01 +08001010 SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci);
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001011 struct scatterlist sg[2];
David Howells17926a72007-04-26 15:48:28 -07001012 struct rxrpc_crypt iv;
1013
1014 _enter(",,%08x%08x",
1015 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
1016
1017 ASSERT(rxkad_ci != NULL);
1018
1019 mutex_lock(&rxkad_ci_mutex);
Herbert Xu1afe5932016-01-24 21:19:01 +08001020 if (crypto_skcipher_setkey(rxkad_ci, session_key->x,
1021 sizeof(*session_key)) < 0)
David Howells17926a72007-04-26 15:48:28 -07001022 BUG();
1023
1024 memcpy(&iv, session_key, sizeof(iv));
David Howells17926a72007-04-26 15:48:28 -07001025
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001026 rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
Herbert Xu1afe5932016-01-24 21:19:01 +08001027
1028 skcipher_request_set_tfm(req, rxkad_ci);
1029 skcipher_request_set_callback(req, 0, NULL, NULL);
1030 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
1031
1032 crypto_skcipher_decrypt(req);
1033 skcipher_request_zero(req);
1034
David Howells17926a72007-04-26 15:48:28 -07001035 mutex_unlock(&rxkad_ci_mutex);
1036
1037 _leave("");
1038}
1039
1040/*
1041 * verify a response
1042 */
1043static int rxkad_verify_response(struct rxrpc_connection *conn,
1044 struct sk_buff *skb,
1045 u32 *_abort_code)
1046{
1047 struct rxkad_response response
1048 __attribute__((aligned(8))); /* must be aligned for crypto */
1049 struct rxrpc_skb_priv *sp;
1050 struct rxrpc_crypt session_key;
1051 time_t expiry;
1052 void *ticket;
Al Viro91e916c2008-03-29 03:08:38 +00001053 u32 abort_code, version, kvno, ticket_len, level;
1054 __be32 csum;
David Howells17926a72007-04-26 15:48:28 -07001055 int ret;
1056
1057 _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
1058
1059 abort_code = RXKADPACKETSHORT;
1060 if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0)
1061 goto protocol_error;
1062 if (!pskb_pull(skb, sizeof(response)))
1063 BUG();
1064
1065 version = ntohl(response.version);
1066 ticket_len = ntohl(response.ticket_len);
1067 kvno = ntohl(response.kvno);
1068 sp = rxrpc_skb(skb);
1069 _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
David Howells0d12f8a2016-03-04 15:53:46 +00001070 sp->hdr.serial, version, kvno, ticket_len);
David Howells17926a72007-04-26 15:48:28 -07001071
1072 abort_code = RXKADINCONSISTENCY;
1073 if (version != RXKAD_VERSION)
David Howells4aa9cb32007-12-07 04:31:47 -08001074 goto protocol_error;
David Howells17926a72007-04-26 15:48:28 -07001075
1076 abort_code = RXKADTICKETLEN;
1077 if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1078 goto protocol_error;
1079
1080 abort_code = RXKADUNKNOWNKEY;
1081 if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1082 goto protocol_error;
1083
1084 /* extract the kerberos ticket and decrypt and decode it */
1085 ticket = kmalloc(ticket_len, GFP_NOFS);
1086 if (!ticket)
1087 return -ENOMEM;
1088
1089 abort_code = RXKADPACKETSHORT;
1090 if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0)
1091 goto protocol_error_free;
1092
1093 ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
1094 &expiry, &abort_code);
1095 if (ret < 0) {
1096 *_abort_code = abort_code;
1097 kfree(ticket);
1098 return ret;
1099 }
1100
1101 /* use the session key from inside the ticket to decrypt the
1102 * response */
1103 rxkad_decrypt_response(conn, &response, &session_key);
1104
1105 abort_code = RXKADSEALEDINCON;
David Howells0d12f8a2016-03-04 15:53:46 +00001106 if (ntohl(response.encrypted.epoch) != conn->epoch)
David Howells17926a72007-04-26 15:48:28 -07001107 goto protocol_error_free;
David Howells0d12f8a2016-03-04 15:53:46 +00001108 if (ntohl(response.encrypted.cid) != conn->cid)
David Howells17926a72007-04-26 15:48:28 -07001109 goto protocol_error_free;
1110 if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
1111 goto protocol_error_free;
1112 csum = response.encrypted.checksum;
1113 response.encrypted.checksum = 0;
1114 rxkad_calc_response_checksum(&response);
1115 if (response.encrypted.checksum != csum)
1116 goto protocol_error_free;
1117
1118 if (ntohl(response.encrypted.call_id[0]) > INT_MAX ||
1119 ntohl(response.encrypted.call_id[1]) > INT_MAX ||
1120 ntohl(response.encrypted.call_id[2]) > INT_MAX ||
1121 ntohl(response.encrypted.call_id[3]) > INT_MAX)
1122 goto protocol_error_free;
1123
1124 abort_code = RXKADOUTOFSEQUENCE;
David Howells0d12f8a2016-03-04 15:53:46 +00001125 if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1)
David Howells17926a72007-04-26 15:48:28 -07001126 goto protocol_error_free;
1127
1128 abort_code = RXKADLEVELFAIL;
1129 level = ntohl(response.encrypted.level);
1130 if (level > RXRPC_SECURITY_ENCRYPT)
1131 goto protocol_error_free;
1132 conn->security_level = level;
1133
1134 /* create a key to hold the security data and expiration time - after
1135 * this the connection security can be handled in exactly the same way
1136 * as for a client connection */
1137 ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
1138 if (ret < 0) {
1139 kfree(ticket);
1140 return ret;
1141 }
1142
1143 kfree(ticket);
1144 _leave(" = 0");
1145 return 0;
1146
1147protocol_error_free:
1148 kfree(ticket);
1149protocol_error:
1150 *_abort_code = abort_code;
1151 _leave(" = -EPROTO [%d]", abort_code);
1152 return -EPROTO;
1153}
1154
1155/*
1156 * clear the connection security
1157 */
1158static void rxkad_clear(struct rxrpc_connection *conn)
1159{
1160 _enter("");
1161
1162 if (conn->cipher)
Herbert Xu1afe5932016-01-24 21:19:01 +08001163 crypto_free_skcipher(conn->cipher);
David Howells17926a72007-04-26 15:48:28 -07001164}
1165
1166/*
1167 * RxRPC Kerberos-based security
1168 */
1169static struct rxrpc_security rxkad = {
1170 .owner = THIS_MODULE,
1171 .name = "rxkad",
David Howells8b815472009-09-14 01:17:30 +00001172 .security_index = RXRPC_SECURITY_RXKAD,
David Howells17926a72007-04-26 15:48:28 -07001173 .init_connection_security = rxkad_init_connection_security,
1174 .prime_packet_security = rxkad_prime_packet_security,
1175 .secure_packet = rxkad_secure_packet,
1176 .verify_packet = rxkad_verify_packet,
1177 .issue_challenge = rxkad_issue_challenge,
1178 .respond_to_challenge = rxkad_respond_to_challenge,
1179 .verify_response = rxkad_verify_response,
1180 .clear = rxkad_clear,
1181};
1182
1183static __init int rxkad_init(void)
1184{
1185 _enter("");
1186
1187 /* pin the cipher we need so that the crypto layer doesn't invoke
1188 * keventd to go get it */
Herbert Xu1afe5932016-01-24 21:19:01 +08001189 rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
David Howells17926a72007-04-26 15:48:28 -07001190 if (IS_ERR(rxkad_ci))
1191 return PTR_ERR(rxkad_ci);
1192
1193 return rxrpc_register_security(&rxkad);
1194}
1195
1196module_init(rxkad_init);
1197
1198static __exit void rxkad_exit(void)
1199{
1200 _enter("");
1201
1202 rxrpc_unregister_security(&rxkad);
Herbert Xu1afe5932016-01-24 21:19:01 +08001203 crypto_free_skcipher(rxkad_ci);
David Howells17926a72007-04-26 15:48:28 -07001204}
1205
1206module_exit(rxkad_exit);