David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* 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 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 14 | #include <crypto/skcipher.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/net.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/udp.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 19 | #include <linux/scatterlist.h> |
| 20 | #include <linux/ctype.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 22 | #include <net/sock.h> |
| 23 | #include <net/af_rxrpc.h> |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 24 | #include <keys/rxrpc-type.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 25 | #include "ar-internal.h" |
| 26 | |
| 27 | #define RXKAD_VERSION 2 |
| 28 | #define MAXKRB5TICKETLEN 1024 |
| 29 | #define RXKAD_TKT_TYPE_KERBEROS_V5 256 |
| 30 | #define ANAME_SZ 40 /* size of authentication name */ |
| 31 | #define INST_SZ 40 /* size of principal's instance */ |
| 32 | #define REALM_SZ 40 /* size of principal's auth domain */ |
| 33 | #define SNAME_SZ 40 /* size of service name */ |
| 34 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 35 | struct rxkad_level1_hdr { |
| 36 | __be32 data_size; /* true data size (excluding padding) */ |
| 37 | }; |
| 38 | |
| 39 | struct rxkad_level2_hdr { |
| 40 | __be32 data_size; /* true data size (excluding padding) */ |
| 41 | __be32 checksum; /* decrypted data checksum */ |
| 42 | }; |
| 43 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 44 | /* |
| 45 | * this holds a pinned cipher so that keventd doesn't get called by the cipher |
| 46 | * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE |
| 47 | * packets |
| 48 | */ |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 49 | static struct crypto_skcipher *rxkad_ci; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 50 | static DEFINE_MUTEX(rxkad_ci_mutex); |
| 51 | |
| 52 | /* |
| 53 | * initialise connection security |
| 54 | */ |
| 55 | static int rxkad_init_connection_security(struct rxrpc_connection *conn) |
| 56 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 57 | struct crypto_skcipher *ci; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 58 | struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 59 | int ret; |
| 60 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 61 | _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 62 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 63 | token = conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 64 | conn->security_ix = token->security_index; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 65 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 66 | ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 67 | if (IS_ERR(ci)) { |
| 68 | _debug("no cipher"); |
| 69 | ret = PTR_ERR(ci); |
| 70 | goto error; |
| 71 | } |
| 72 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 73 | if (crypto_skcipher_setkey(ci, token->kad->session_key, |
| 74 | sizeof(token->kad->session_key)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 75 | BUG(); |
| 76 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 77 | switch (conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 78 | case RXRPC_SECURITY_PLAIN: |
| 79 | break; |
| 80 | case RXRPC_SECURITY_AUTH: |
| 81 | conn->size_align = 8; |
| 82 | conn->security_size = sizeof(struct rxkad_level1_hdr); |
| 83 | conn->header_size += sizeof(struct rxkad_level1_hdr); |
| 84 | break; |
| 85 | case RXRPC_SECURITY_ENCRYPT: |
| 86 | conn->size_align = 8; |
| 87 | conn->security_size = sizeof(struct rxkad_level2_hdr); |
| 88 | conn->header_size += sizeof(struct rxkad_level2_hdr); |
| 89 | break; |
| 90 | default: |
| 91 | ret = -EKEYREJECTED; |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | conn->cipher = ci; |
| 96 | ret = 0; |
| 97 | error: |
| 98 | _leave(" = %d", ret); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * prime the encryption state with the invariant parts of a connection's |
| 104 | * description |
| 105 | */ |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 106 | static int rxkad_prime_packet_security(struct rxrpc_connection *conn) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 107 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 108 | struct rxrpc_key_token *token; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 109 | SKCIPHER_REQUEST_ON_STACK(req, conn->cipher); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 110 | struct scatterlist sg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 111 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 112 | __be32 *tmpbuf; |
| 113 | size_t tmpsize = 4 * sizeof(__be32); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 114 | |
| 115 | _enter(""); |
| 116 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 117 | if (!conn->params.key) |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | |
| 120 | tmpbuf = kmalloc(tmpsize, GFP_KERNEL); |
| 121 | if (!tmpbuf) |
| 122 | return -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 123 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 124 | token = conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 125 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 126 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 127 | tmpbuf[0] = htonl(conn->proto.epoch); |
| 128 | tmpbuf[1] = htonl(conn->proto.cid); |
| 129 | tmpbuf[2] = 0; |
| 130 | tmpbuf[3] = htonl(conn->security_ix); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 131 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 132 | sg_init_one(&sg, tmpbuf, tmpsize); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 133 | skcipher_request_set_tfm(req, conn->cipher); |
| 134 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 135 | skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 136 | crypto_skcipher_encrypt(req); |
| 137 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 138 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 139 | memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv)); |
| 140 | kfree(tmpbuf); |
| 141 | _leave(" = 0"); |
| 142 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * partially encrypt a packet (level 1 security) |
| 147 | */ |
| 148 | static int rxkad_secure_packet_auth(const struct rxrpc_call *call, |
| 149 | struct sk_buff *skb, |
| 150 | u32 data_size, |
| 151 | void *sechdr) |
| 152 | { |
| 153 | struct rxrpc_skb_priv *sp; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 154 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 155 | struct rxkad_level1_hdr hdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 156 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 157 | struct scatterlist sg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 158 | u16 check; |
| 159 | |
| 160 | sp = rxrpc_skb(skb); |
| 161 | |
| 162 | _enter(""); |
| 163 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 164 | check = sp->hdr.seq ^ sp->hdr.callNumber; |
| 165 | data_size |= (u32)check << 16; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 166 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 167 | hdr.data_size = htonl(data_size); |
| 168 | memcpy(sechdr, &hdr, sizeof(hdr)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 169 | |
| 170 | /* start the encryption afresh */ |
| 171 | memset(&iv, 0, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 172 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 173 | sg_init_one(&sg, sechdr, 8); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 174 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 175 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 176 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 177 | crypto_skcipher_encrypt(req); |
| 178 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 179 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 180 | _leave(" = 0"); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * wholly encrypt a packet (level 2 security) |
| 186 | */ |
| 187 | static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call, |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 188 | struct sk_buff *skb, |
| 189 | u32 data_size, |
| 190 | void *sechdr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 191 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 192 | const struct rxrpc_key_token *token; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 193 | struct rxkad_level2_hdr rxkhdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 194 | struct rxrpc_skb_priv *sp; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 195 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 196 | struct rxrpc_crypt iv; |
| 197 | struct scatterlist sg[16]; |
| 198 | struct sk_buff *trailer; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 199 | unsigned int len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 200 | u16 check; |
| 201 | int nsg; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 202 | int err; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | |
| 204 | sp = rxrpc_skb(skb); |
| 205 | |
| 206 | _enter(""); |
| 207 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 208 | check = sp->hdr.seq ^ sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 209 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 210 | rxkhdr.data_size = htonl(data_size | (u32)check << 16); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 211 | rxkhdr.checksum = 0; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 212 | memcpy(sechdr, &rxkhdr, sizeof(rxkhdr)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 213 | |
| 214 | /* encrypt from the session key */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 215 | token = call->conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 216 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 217 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 218 | sg_init_one(&sg[0], sechdr, sizeof(rxkhdr)); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 219 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 220 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 221 | skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 222 | crypto_skcipher_encrypt(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 223 | |
| 224 | /* we want to encrypt the skbuff in-place */ |
| 225 | nsg = skb_cow_data(skb, 0, &trailer); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 226 | err = -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 227 | if (nsg < 0 || nsg > 16) |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 228 | goto out; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 229 | |
| 230 | len = data_size + call->conn->size_align - 1; |
| 231 | len &= ~(call->conn->size_align - 1); |
| 232 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 233 | sg_init_table(sg, nsg); |
| 234 | skb_to_sgvec(skb, sg, 0, len); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 235 | skcipher_request_set_crypt(req, sg, sg, len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 236 | crypto_skcipher_encrypt(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 237 | |
| 238 | _leave(" = 0"); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 239 | err = 0; |
| 240 | |
| 241 | out: |
| 242 | skcipher_request_zero(req); |
| 243 | return err; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
| 247 | * checksum an RxRPC packet header |
| 248 | */ |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 249 | static int rxkad_secure_packet(struct rxrpc_call *call, |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 250 | struct sk_buff *skb, |
| 251 | size_t data_size, |
| 252 | void *sechdr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 253 | { |
| 254 | struct rxrpc_skb_priv *sp; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 255 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 256 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 257 | struct scatterlist sg; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 258 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 259 | int ret; |
| 260 | |
| 261 | sp = rxrpc_skb(skb); |
| 262 | |
| 263 | _enter("{%d{%x}},{#%u},%zu,", |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 264 | call->debug_id, key_serial(call->conn->params.key), |
| 265 | sp->hdr.seq, data_size); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 266 | |
| 267 | if (!call->conn->cipher) |
| 268 | return 0; |
| 269 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 270 | ret = key_validate(call->conn->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 271 | if (ret < 0) |
| 272 | return ret; |
| 273 | |
| 274 | /* continue encrypting from where we left off */ |
| 275 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 276 | |
| 277 | /* calculate the security checksum */ |
David Howells | 01a90a4 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 278 | x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 279 | x |= sp->hdr.seq & 0x3fffffff; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 280 | call->crypto_buf[0] = htonl(sp->hdr.callNumber); |
| 281 | call->crypto_buf[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 282 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 283 | sg_init_one(&sg, call->crypto_buf, 8); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 284 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 285 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 286 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 287 | crypto_skcipher_encrypt(req); |
| 288 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 289 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 290 | y = ntohl(call->crypto_buf[1]); |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 291 | y = (y >> 16) & 0xffff; |
| 292 | if (y == 0) |
| 293 | y = 1; /* zero checksums are not permitted */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 294 | sp->hdr.cksum = y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 295 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 296 | switch (call->conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 297 | case RXRPC_SECURITY_PLAIN: |
| 298 | ret = 0; |
| 299 | break; |
| 300 | case RXRPC_SECURITY_AUTH: |
| 301 | ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr); |
| 302 | break; |
| 303 | case RXRPC_SECURITY_ENCRYPT: |
| 304 | ret = rxkad_secure_packet_encrypt(call, skb, data_size, |
| 305 | sechdr); |
| 306 | break; |
| 307 | default: |
| 308 | ret = -EPERM; |
| 309 | break; |
| 310 | } |
| 311 | |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 312 | _leave(" = %d [set %hx]", ret, y); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * decrypt partial encryption on a packet (level 1 security) |
| 318 | */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 319 | static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb, |
| 320 | rxrpc_seq_t seq) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 321 | { |
| 322 | struct rxkad_level1_hdr sechdr; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 323 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 324 | struct rxrpc_crypt iv; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 325 | struct scatterlist sg[16]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 326 | struct sk_buff *trailer; |
| 327 | u32 data_size, buf; |
| 328 | u16 check; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 329 | int nsg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 330 | |
| 331 | _enter(""); |
| 332 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 333 | if (skb->len < 8) { |
| 334 | rxrpc_abort_call("V1H", call, seq, RXKADSEALEDINCON, EPROTO); |
| 335 | goto protocol_error; |
| 336 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 337 | |
| 338 | /* we want to decrypt the skbuff in-place */ |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 339 | nsg = skb_cow_data(skb, 0, &trailer); |
| 340 | if (nsg < 0 || nsg > 16) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 341 | goto nomem; |
| 342 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 343 | sg_init_table(sg, nsg); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 344 | skb_to_sgvec(skb, sg, 0, 8); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 345 | |
| 346 | /* start the decryption afresh */ |
| 347 | memset(&iv, 0, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 348 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 349 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 350 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 351 | skcipher_request_set_crypt(req, sg, sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 352 | crypto_skcipher_decrypt(req); |
| 353 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 354 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 355 | /* Extract the decrypted packet length */ |
| 356 | if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) { |
| 357 | rxrpc_abort_call("XV1", call, seq, RXKADDATALEN, EPROTO); |
| 358 | goto protocol_error; |
| 359 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 360 | if (!skb_pull(skb, sizeof(sechdr))) |
| 361 | BUG(); |
| 362 | |
| 363 | buf = ntohl(sechdr.data_size); |
| 364 | data_size = buf & 0xffff; |
| 365 | |
| 366 | check = buf >> 16; |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 367 | check ^= seq ^ call->call_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 368 | check &= 0xffff; |
| 369 | if (check != 0) { |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 370 | rxrpc_abort_call("V1C", call, seq, RXKADSEALEDINCON, EPROTO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 371 | goto protocol_error; |
| 372 | } |
| 373 | |
| 374 | /* shorten the packet to remove the padding */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 375 | if (data_size > skb->len) { |
| 376 | rxrpc_abort_call("V1L", call, seq, RXKADDATALEN, EPROTO); |
| 377 | goto protocol_error; |
| 378 | } |
| 379 | if (data_size < skb->len) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 380 | skb->len = data_size; |
| 381 | |
| 382 | _leave(" = 0 [dlen=%x]", data_size); |
| 383 | return 0; |
| 384 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 385 | protocol_error: |
| 386 | _leave(" = -EPROTO"); |
| 387 | return -EPROTO; |
| 388 | |
| 389 | nomem: |
| 390 | _leave(" = -ENOMEM"); |
| 391 | return -ENOMEM; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * wholly decrypt a packet (level 2 security) |
| 396 | */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 397 | static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb, |
| 398 | rxrpc_seq_t seq) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 399 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 400 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 401 | struct rxkad_level2_hdr sechdr; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 402 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 403 | struct rxrpc_crypt iv; |
| 404 | struct scatterlist _sg[4], *sg; |
| 405 | struct sk_buff *trailer; |
| 406 | u32 data_size, buf; |
| 407 | u16 check; |
| 408 | int nsg; |
| 409 | |
| 410 | _enter(",{%d}", skb->len); |
| 411 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 412 | if (skb->len < 8) { |
| 413 | rxrpc_abort_call("V2H", call, seq, RXKADSEALEDINCON, EPROTO); |
| 414 | goto protocol_error; |
| 415 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 416 | |
| 417 | /* we want to decrypt the skbuff in-place */ |
| 418 | nsg = skb_cow_data(skb, 0, &trailer); |
| 419 | if (nsg < 0) |
| 420 | goto nomem; |
| 421 | |
| 422 | sg = _sg; |
| 423 | if (unlikely(nsg > 4)) { |
| 424 | sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO); |
| 425 | if (!sg) |
| 426 | goto nomem; |
| 427 | } |
| 428 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 429 | sg_init_table(sg, nsg); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 430 | skb_to_sgvec(skb, sg, 0, skb->len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 431 | |
| 432 | /* decrypt from the session key */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 433 | token = call->conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 434 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 435 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 436 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 437 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 438 | skcipher_request_set_crypt(req, sg, sg, skb->len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 439 | crypto_skcipher_decrypt(req); |
| 440 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 441 | if (sg != _sg) |
| 442 | kfree(sg); |
| 443 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 444 | /* Extract the decrypted packet length */ |
| 445 | if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) { |
| 446 | rxrpc_abort_call("XV2", call, seq, RXKADDATALEN, EPROTO); |
| 447 | goto protocol_error; |
| 448 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 449 | if (!skb_pull(skb, sizeof(sechdr))) |
| 450 | BUG(); |
| 451 | |
| 452 | buf = ntohl(sechdr.data_size); |
| 453 | data_size = buf & 0xffff; |
| 454 | |
| 455 | check = buf >> 16; |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 456 | check ^= seq ^ call->call_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 457 | check &= 0xffff; |
| 458 | if (check != 0) { |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 459 | rxrpc_abort_call("V2C", call, seq, RXKADSEALEDINCON, EPROTO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 460 | goto protocol_error; |
| 461 | } |
| 462 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 463 | if (data_size > skb->len) { |
| 464 | rxrpc_abort_call("V2L", call, seq, RXKADDATALEN, EPROTO); |
| 465 | goto protocol_error; |
| 466 | } |
| 467 | if (data_size < skb->len) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 468 | skb->len = data_size; |
| 469 | |
| 470 | _leave(" = 0 [dlen=%x]", data_size); |
| 471 | return 0; |
| 472 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 473 | protocol_error: |
| 474 | _leave(" = -EPROTO"); |
| 475 | return -EPROTO; |
| 476 | |
| 477 | nomem: |
| 478 | _leave(" = -ENOMEM"); |
| 479 | return -ENOMEM; |
| 480 | } |
| 481 | |
| 482 | /* |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 483 | * Verify the security on a received packet or subpacket (if part of a |
| 484 | * jumbo packet). |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 485 | */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 486 | static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb, |
| 487 | rxrpc_seq_t seq, u16 expected_cksum) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 488 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 489 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 490 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 491 | struct scatterlist sg; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 492 | u16 cksum; |
| 493 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 494 | |
| 495 | _enter("{%d{%x}},{#%u}", |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 496 | call->debug_id, key_serial(call->conn->params.key), seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 497 | |
| 498 | if (!call->conn->cipher) |
| 499 | return 0; |
| 500 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 501 | /* continue encrypting from where we left off */ |
| 502 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 503 | |
| 504 | /* validate the security checksum */ |
David Howells | 01a90a4 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 505 | x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 506 | x |= seq & 0x3fffffff; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 507 | call->crypto_buf[0] = htonl(call->call_id); |
| 508 | call->crypto_buf[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 509 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 510 | sg_init_one(&sg, call->crypto_buf, 8); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 511 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 512 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 513 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 514 | crypto_skcipher_encrypt(req); |
| 515 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 516 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 517 | y = ntohl(call->crypto_buf[1]); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 518 | cksum = (y >> 16) & 0xffff; |
| 519 | if (cksum == 0) |
| 520 | cksum = 1; /* zero checksums are not permitted */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 521 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 522 | if (cksum != expected_cksum) { |
| 523 | rxrpc_abort_call("VCK", call, seq, RXKADSEALEDINCON, EPROTO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 524 | _leave(" = -EPROTO [csum failed]"); |
| 525 | return -EPROTO; |
| 526 | } |
| 527 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 528 | switch (call->conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 529 | case RXRPC_SECURITY_PLAIN: |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 530 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 531 | case RXRPC_SECURITY_AUTH: |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 532 | return rxkad_verify_packet_1(call, skb, seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 533 | case RXRPC_SECURITY_ENCRYPT: |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 534 | return rxkad_verify_packet_2(call, skb, seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 535 | default: |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame^] | 536 | return -ENOANO; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 537 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* |
| 541 | * issue a challenge |
| 542 | */ |
| 543 | static int rxkad_issue_challenge(struct rxrpc_connection *conn) |
| 544 | { |
| 545 | struct rxkad_challenge challenge; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 546 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 547 | struct msghdr msg; |
| 548 | struct kvec iov[2]; |
| 549 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 550 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 551 | int ret; |
| 552 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 553 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 554 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 555 | ret = key_validate(conn->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 556 | if (ret < 0) |
| 557 | return ret; |
| 558 | |
| 559 | get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce)); |
| 560 | |
| 561 | challenge.version = htonl(2); |
| 562 | challenge.nonce = htonl(conn->security_nonce); |
| 563 | challenge.min_level = htonl(0); |
| 564 | challenge.__padding = 0; |
| 565 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 566 | msg.msg_name = &conn->params.peer->srx.transport.sin; |
| 567 | msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 568 | msg.msg_control = NULL; |
| 569 | msg.msg_controllen = 0; |
| 570 | msg.msg_flags = 0; |
| 571 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 572 | whdr.epoch = htonl(conn->proto.epoch); |
| 573 | whdr.cid = htonl(conn->proto.cid); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 574 | whdr.callNumber = 0; |
| 575 | whdr.seq = 0; |
| 576 | whdr.type = RXRPC_PACKET_TYPE_CHALLENGE; |
| 577 | whdr.flags = conn->out_clientflag; |
| 578 | whdr.userStatus = 0; |
| 579 | whdr.securityIndex = conn->security_ix; |
| 580 | whdr._rsvd = 0; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 581 | whdr.serviceId = htons(conn->params.service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 582 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 583 | iov[0].iov_base = &whdr; |
| 584 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 585 | iov[1].iov_base = &challenge; |
| 586 | iov[1].iov_len = sizeof(challenge); |
| 587 | |
| 588 | len = iov[0].iov_len + iov[1].iov_len; |
| 589 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 590 | serial = atomic_inc_return(&conn->serial); |
| 591 | whdr.serial = htonl(serial); |
| 592 | _proto("Tx CHALLENGE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 593 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 594 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 595 | if (ret < 0) { |
| 596 | _debug("sendmsg failed: %d", ret); |
| 597 | return -EAGAIN; |
| 598 | } |
| 599 | |
| 600 | _leave(" = 0"); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * send a Kerberos security response |
| 606 | */ |
| 607 | static int rxkad_send_response(struct rxrpc_connection *conn, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 608 | struct rxrpc_host_header *hdr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 609 | struct rxkad_response *resp, |
| 610 | const struct rxkad_key *s2) |
| 611 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 612 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 613 | struct msghdr msg; |
| 614 | struct kvec iov[3]; |
| 615 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 616 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 617 | int ret; |
| 618 | |
| 619 | _enter(""); |
| 620 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 621 | msg.msg_name = &conn->params.peer->srx.transport.sin; |
| 622 | msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 623 | msg.msg_control = NULL; |
| 624 | msg.msg_controllen = 0; |
| 625 | msg.msg_flags = 0; |
| 626 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 627 | memset(&whdr, 0, sizeof(whdr)); |
| 628 | whdr.epoch = htonl(hdr->epoch); |
| 629 | whdr.cid = htonl(hdr->cid); |
| 630 | whdr.type = RXRPC_PACKET_TYPE_RESPONSE; |
| 631 | whdr.flags = conn->out_clientflag; |
| 632 | whdr.securityIndex = hdr->securityIndex; |
| 633 | whdr.serviceId = htons(hdr->serviceId); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 634 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 635 | iov[0].iov_base = &whdr; |
| 636 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 637 | iov[1].iov_base = resp; |
| 638 | iov[1].iov_len = sizeof(*resp); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 639 | iov[2].iov_base = (void *)s2->ticket; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 640 | iov[2].iov_len = s2->ticket_len; |
| 641 | |
| 642 | len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; |
| 643 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 644 | serial = atomic_inc_return(&conn->serial); |
| 645 | whdr.serial = htonl(serial); |
| 646 | _proto("Tx RESPONSE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 647 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 648 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 649 | if (ret < 0) { |
| 650 | _debug("sendmsg failed: %d", ret); |
| 651 | return -EAGAIN; |
| 652 | } |
| 653 | |
| 654 | _leave(" = 0"); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * calculate the response checksum |
| 660 | */ |
| 661 | static void rxkad_calc_response_checksum(struct rxkad_response *response) |
| 662 | { |
| 663 | u32 csum = 1000003; |
| 664 | int loop; |
| 665 | u8 *p = (u8 *) response; |
| 666 | |
| 667 | for (loop = sizeof(*response); loop > 0; loop--) |
| 668 | csum = csum * 0x10204081 + *p++; |
| 669 | |
| 670 | response->encrypted.checksum = htonl(csum); |
| 671 | } |
| 672 | |
| 673 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 674 | * encrypt the response packet |
| 675 | */ |
| 676 | static void rxkad_encrypt_response(struct rxrpc_connection *conn, |
| 677 | struct rxkad_response *resp, |
| 678 | const struct rxkad_key *s2) |
| 679 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 680 | SKCIPHER_REQUEST_ON_STACK(req, conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 681 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 682 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 683 | |
| 684 | /* continue encrypting from where we left off */ |
| 685 | memcpy(&iv, s2->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 686 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 687 | sg_init_table(sg, 1); |
| 688 | sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 689 | skcipher_request_set_tfm(req, conn->cipher); |
| 690 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 691 | skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 692 | crypto_skcipher_encrypt(req); |
| 693 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
| 697 | * respond to a challenge packet |
| 698 | */ |
| 699 | static int rxkad_respond_to_challenge(struct rxrpc_connection *conn, |
| 700 | struct sk_buff *skb, |
| 701 | u32 *_abort_code) |
| 702 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 703 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 704 | struct rxkad_challenge challenge; |
| 705 | struct rxkad_response resp |
| 706 | __attribute__((aligned(8))); /* must be aligned for crypto */ |
| 707 | struct rxrpc_skb_priv *sp; |
| 708 | u32 version, nonce, min_level, abort_code; |
| 709 | int ret; |
| 710 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 711 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 712 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 713 | if (!conn->params.key) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 714 | _leave(" = -EPROTO [no key]"); |
| 715 | return -EPROTO; |
| 716 | } |
| 717 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 718 | ret = key_validate(conn->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 719 | if (ret < 0) { |
| 720 | *_abort_code = RXKADEXPIRED; |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | abort_code = RXKADPACKETSHORT; |
| 725 | sp = rxrpc_skb(skb); |
| 726 | if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0) |
| 727 | goto protocol_error; |
| 728 | |
| 729 | version = ntohl(challenge.version); |
| 730 | nonce = ntohl(challenge.nonce); |
| 731 | min_level = ntohl(challenge.min_level); |
| 732 | |
| 733 | _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 734 | sp->hdr.serial, version, nonce, min_level); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 735 | |
| 736 | abort_code = RXKADINCONSISTENCY; |
| 737 | if (version != RXKAD_VERSION) |
| 738 | goto protocol_error; |
| 739 | |
| 740 | abort_code = RXKADLEVELFAIL; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 741 | if (conn->params.security_level < min_level) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 742 | goto protocol_error; |
| 743 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 744 | token = conn->params.key->payload.data[0]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 745 | |
| 746 | /* build the response packet */ |
| 747 | memset(&resp, 0, sizeof(resp)); |
| 748 | |
David Howells | 098a209 | 2016-03-04 15:59:00 +0000 | [diff] [blame] | 749 | resp.version = htonl(RXKAD_VERSION); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 750 | resp.encrypted.epoch = htonl(conn->proto.epoch); |
| 751 | resp.encrypted.cid = htonl(conn->proto.cid); |
David Howells | 098a209 | 2016-03-04 15:59:00 +0000 | [diff] [blame] | 752 | resp.encrypted.securityIndex = htonl(conn->security_ix); |
| 753 | resp.encrypted.inc_nonce = htonl(nonce + 1); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 754 | resp.encrypted.level = htonl(conn->params.security_level); |
David Howells | 098a209 | 2016-03-04 15:59:00 +0000 | [diff] [blame] | 755 | resp.kvno = htonl(token->kad->kvno); |
| 756 | resp.ticket_len = htonl(token->kad->ticket_len); |
| 757 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 758 | resp.encrypted.call_id[0] = htonl(conn->channels[0].call_counter); |
| 759 | resp.encrypted.call_id[1] = htonl(conn->channels[1].call_counter); |
| 760 | resp.encrypted.call_id[2] = htonl(conn->channels[2].call_counter); |
| 761 | resp.encrypted.call_id[3] = htonl(conn->channels[3].call_counter); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 762 | |
| 763 | /* calculate the response checksum and then do the encryption */ |
| 764 | rxkad_calc_response_checksum(&resp); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 765 | rxkad_encrypt_response(conn, &resp, token->kad); |
| 766 | return rxkad_send_response(conn, &sp->hdr, &resp, token->kad); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 767 | |
| 768 | protocol_error: |
| 769 | *_abort_code = abort_code; |
| 770 | _leave(" = -EPROTO [%d]", abort_code); |
| 771 | return -EPROTO; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * decrypt the kerberos IV ticket in the response |
| 776 | */ |
| 777 | static int rxkad_decrypt_ticket(struct rxrpc_connection *conn, |
| 778 | void *ticket, size_t ticket_len, |
| 779 | struct rxrpc_crypt *_session_key, |
| 780 | time_t *_expiry, |
| 781 | u32 *_abort_code) |
| 782 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 783 | struct skcipher_request *req; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 784 | struct rxrpc_crypt iv, key; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 785 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 786 | struct in_addr addr; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 787 | unsigned int life; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 788 | time_t issue, now; |
| 789 | bool little_endian; |
| 790 | int ret; |
| 791 | u8 *p, *q, *name, *end; |
| 792 | |
| 793 | _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key)); |
| 794 | |
| 795 | *_expiry = 0; |
| 796 | |
| 797 | ret = key_validate(conn->server_key); |
| 798 | if (ret < 0) { |
| 799 | switch (ret) { |
| 800 | case -EKEYEXPIRED: |
| 801 | *_abort_code = RXKADEXPIRED; |
| 802 | goto error; |
| 803 | default: |
| 804 | *_abort_code = RXKADNOAUTH; |
| 805 | goto error; |
| 806 | } |
| 807 | } |
| 808 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 809 | ASSERT(conn->server_key->payload.data[0] != NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 810 | ASSERTCMP((unsigned long) ticket & 7UL, ==, 0); |
| 811 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 812 | memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 813 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 814 | req = skcipher_request_alloc(conn->server_key->payload.data[0], |
| 815 | GFP_NOFS); |
| 816 | if (!req) { |
| 817 | *_abort_code = RXKADNOAUTH; |
| 818 | ret = -ENOMEM; |
| 819 | goto error; |
| 820 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 821 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 822 | sg_init_one(&sg[0], ticket, ticket_len); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 823 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 824 | skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 825 | crypto_skcipher_decrypt(req); |
| 826 | skcipher_request_free(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 827 | |
| 828 | p = ticket; |
| 829 | end = p + ticket_len; |
| 830 | |
| 831 | #define Z(size) \ |
| 832 | ({ \ |
| 833 | u8 *__str = p; \ |
| 834 | q = memchr(p, 0, end - p); \ |
| 835 | if (!q || q - p > (size)) \ |
| 836 | goto bad_ticket; \ |
| 837 | for (; p < q; p++) \ |
| 838 | if (!isprint(*p)) \ |
| 839 | goto bad_ticket; \ |
| 840 | p++; \ |
| 841 | __str; \ |
| 842 | }) |
| 843 | |
| 844 | /* extract the ticket flags */ |
| 845 | _debug("KIV FLAGS: %x", *p); |
| 846 | little_endian = *p & 1; |
| 847 | p++; |
| 848 | |
| 849 | /* extract the authentication name */ |
| 850 | name = Z(ANAME_SZ); |
| 851 | _debug("KIV ANAME: %s", name); |
| 852 | |
| 853 | /* extract the principal's instance */ |
| 854 | name = Z(INST_SZ); |
| 855 | _debug("KIV INST : %s", name); |
| 856 | |
| 857 | /* extract the principal's authentication domain */ |
| 858 | name = Z(REALM_SZ); |
| 859 | _debug("KIV REALM: %s", name); |
| 860 | |
| 861 | if (end - p < 4 + 8 + 4 + 2) |
| 862 | goto bad_ticket; |
| 863 | |
| 864 | /* get the IPv4 address of the entity that requested the ticket */ |
| 865 | memcpy(&addr, p, sizeof(addr)); |
| 866 | p += 4; |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 867 | _debug("KIV ADDR : %pI4", &addr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 868 | |
| 869 | /* get the session key from the ticket */ |
| 870 | memcpy(&key, p, sizeof(key)); |
| 871 | p += 8; |
| 872 | _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1])); |
| 873 | memcpy(_session_key, &key, sizeof(key)); |
| 874 | |
| 875 | /* get the ticket's lifetime */ |
| 876 | life = *p++ * 5 * 60; |
| 877 | _debug("KIV LIFE : %u", life); |
| 878 | |
| 879 | /* get the issue time of the ticket */ |
| 880 | if (little_endian) { |
| 881 | __le32 stamp; |
| 882 | memcpy(&stamp, p, 4); |
| 883 | issue = le32_to_cpu(stamp); |
| 884 | } else { |
| 885 | __be32 stamp; |
| 886 | memcpy(&stamp, p, 4); |
| 887 | issue = be32_to_cpu(stamp); |
| 888 | } |
| 889 | p += 4; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 890 | now = get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 891 | _debug("KIV ISSUE: %lx [%lx]", issue, now); |
| 892 | |
| 893 | /* check the ticket is in date */ |
| 894 | if (issue > now) { |
| 895 | *_abort_code = RXKADNOAUTH; |
| 896 | ret = -EKEYREJECTED; |
| 897 | goto error; |
| 898 | } |
| 899 | |
| 900 | if (issue < now - life) { |
| 901 | *_abort_code = RXKADEXPIRED; |
| 902 | ret = -EKEYEXPIRED; |
| 903 | goto error; |
| 904 | } |
| 905 | |
| 906 | *_expiry = issue + life; |
| 907 | |
| 908 | /* get the service name */ |
| 909 | name = Z(SNAME_SZ); |
| 910 | _debug("KIV SNAME: %s", name); |
| 911 | |
| 912 | /* get the service instance name */ |
| 913 | name = Z(INST_SZ); |
| 914 | _debug("KIV SINST: %s", name); |
| 915 | |
| 916 | ret = 0; |
| 917 | error: |
| 918 | _leave(" = %d", ret); |
| 919 | return ret; |
| 920 | |
| 921 | bad_ticket: |
| 922 | *_abort_code = RXKADBADTICKET; |
| 923 | ret = -EBADMSG; |
| 924 | goto error; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * decrypt the response packet |
| 929 | */ |
| 930 | static void rxkad_decrypt_response(struct rxrpc_connection *conn, |
| 931 | struct rxkad_response *resp, |
| 932 | const struct rxrpc_crypt *session_key) |
| 933 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 934 | SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 935 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 936 | struct rxrpc_crypt iv; |
| 937 | |
| 938 | _enter(",,%08x%08x", |
| 939 | ntohl(session_key->n[0]), ntohl(session_key->n[1])); |
| 940 | |
| 941 | ASSERT(rxkad_ci != NULL); |
| 942 | |
| 943 | mutex_lock(&rxkad_ci_mutex); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 944 | if (crypto_skcipher_setkey(rxkad_ci, session_key->x, |
| 945 | sizeof(*session_key)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 946 | BUG(); |
| 947 | |
| 948 | memcpy(&iv, session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 949 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 950 | sg_init_table(sg, 1); |
| 951 | sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 952 | skcipher_request_set_tfm(req, rxkad_ci); |
| 953 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 954 | skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 955 | crypto_skcipher_decrypt(req); |
| 956 | skcipher_request_zero(req); |
| 957 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 958 | mutex_unlock(&rxkad_ci_mutex); |
| 959 | |
| 960 | _leave(""); |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * verify a response |
| 965 | */ |
| 966 | static int rxkad_verify_response(struct rxrpc_connection *conn, |
| 967 | struct sk_buff *skb, |
| 968 | u32 *_abort_code) |
| 969 | { |
| 970 | struct rxkad_response response |
| 971 | __attribute__((aligned(8))); /* must be aligned for crypto */ |
| 972 | struct rxrpc_skb_priv *sp; |
| 973 | struct rxrpc_crypt session_key; |
| 974 | time_t expiry; |
| 975 | void *ticket; |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 976 | u32 abort_code, version, kvno, ticket_len, level; |
| 977 | __be32 csum; |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 978 | int ret, i; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 979 | |
| 980 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key)); |
| 981 | |
| 982 | abort_code = RXKADPACKETSHORT; |
| 983 | if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0) |
| 984 | goto protocol_error; |
| 985 | if (!pskb_pull(skb, sizeof(response))) |
| 986 | BUG(); |
| 987 | |
| 988 | version = ntohl(response.version); |
| 989 | ticket_len = ntohl(response.ticket_len); |
| 990 | kvno = ntohl(response.kvno); |
| 991 | sp = rxrpc_skb(skb); |
| 992 | _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 993 | sp->hdr.serial, version, kvno, ticket_len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 994 | |
| 995 | abort_code = RXKADINCONSISTENCY; |
| 996 | if (version != RXKAD_VERSION) |
David Howells | 4aa9cb3 | 2007-12-07 04:31:47 -0800 | [diff] [blame] | 997 | goto protocol_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 998 | |
| 999 | abort_code = RXKADTICKETLEN; |
| 1000 | if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN) |
| 1001 | goto protocol_error; |
| 1002 | |
| 1003 | abort_code = RXKADUNKNOWNKEY; |
| 1004 | if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5) |
| 1005 | goto protocol_error; |
| 1006 | |
| 1007 | /* extract the kerberos ticket and decrypt and decode it */ |
| 1008 | ticket = kmalloc(ticket_len, GFP_NOFS); |
| 1009 | if (!ticket) |
| 1010 | return -ENOMEM; |
| 1011 | |
| 1012 | abort_code = RXKADPACKETSHORT; |
| 1013 | if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0) |
| 1014 | goto protocol_error_free; |
| 1015 | |
| 1016 | ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key, |
| 1017 | &expiry, &abort_code); |
| 1018 | if (ret < 0) { |
| 1019 | *_abort_code = abort_code; |
| 1020 | kfree(ticket); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | /* use the session key from inside the ticket to decrypt the |
| 1025 | * response */ |
| 1026 | rxkad_decrypt_response(conn, &response, &session_key); |
| 1027 | |
| 1028 | abort_code = RXKADSEALEDINCON; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1029 | if (ntohl(response.encrypted.epoch) != conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1030 | goto protocol_error_free; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1031 | if (ntohl(response.encrypted.cid) != conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1032 | goto protocol_error_free; |
| 1033 | if (ntohl(response.encrypted.securityIndex) != conn->security_ix) |
| 1034 | goto protocol_error_free; |
| 1035 | csum = response.encrypted.checksum; |
| 1036 | response.encrypted.checksum = 0; |
| 1037 | rxkad_calc_response_checksum(&response); |
| 1038 | if (response.encrypted.checksum != csum) |
| 1039 | goto protocol_error_free; |
| 1040 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1041 | spin_lock(&conn->channel_lock); |
| 1042 | for (i = 0; i < RXRPC_MAXCALLS; i++) { |
| 1043 | struct rxrpc_call *call; |
| 1044 | u32 call_id = ntohl(response.encrypted.call_id[i]); |
| 1045 | |
| 1046 | if (call_id > INT_MAX) |
| 1047 | goto protocol_error_unlock; |
| 1048 | |
| 1049 | if (call_id < conn->channels[i].call_counter) |
| 1050 | goto protocol_error_unlock; |
| 1051 | if (call_id > conn->channels[i].call_counter) { |
| 1052 | call = rcu_dereference_protected( |
| 1053 | conn->channels[i].call, |
| 1054 | lockdep_is_held(&conn->channel_lock)); |
| 1055 | if (call && call->state < RXRPC_CALL_COMPLETE) |
| 1056 | goto protocol_error_unlock; |
| 1057 | conn->channels[i].call_counter = call_id; |
| 1058 | } |
| 1059 | } |
| 1060 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1061 | |
| 1062 | abort_code = RXKADOUTOFSEQUENCE; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1063 | if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1064 | goto protocol_error_free; |
| 1065 | |
| 1066 | abort_code = RXKADLEVELFAIL; |
| 1067 | level = ntohl(response.encrypted.level); |
| 1068 | if (level > RXRPC_SECURITY_ENCRYPT) |
| 1069 | goto protocol_error_free; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1070 | conn->params.security_level = level; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1071 | |
| 1072 | /* create a key to hold the security data and expiration time - after |
| 1073 | * this the connection security can be handled in exactly the same way |
| 1074 | * as for a client connection */ |
| 1075 | ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno); |
| 1076 | if (ret < 0) { |
| 1077 | kfree(ticket); |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | kfree(ticket); |
| 1082 | _leave(" = 0"); |
| 1083 | return 0; |
| 1084 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1085 | protocol_error_unlock: |
| 1086 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1087 | protocol_error_free: |
| 1088 | kfree(ticket); |
| 1089 | protocol_error: |
| 1090 | *_abort_code = abort_code; |
| 1091 | _leave(" = -EPROTO [%d]", abort_code); |
| 1092 | return -EPROTO; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * clear the connection security |
| 1097 | */ |
| 1098 | static void rxkad_clear(struct rxrpc_connection *conn) |
| 1099 | { |
| 1100 | _enter(""); |
| 1101 | |
| 1102 | if (conn->cipher) |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 1103 | crypto_free_skcipher(conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1107 | * Initialise the rxkad security service. |
| 1108 | */ |
| 1109 | static int rxkad_init(void) |
| 1110 | { |
| 1111 | /* pin the cipher we need so that the crypto layer doesn't invoke |
| 1112 | * keventd to go get it */ |
| 1113 | rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC); |
Wu Fengguang | fa54cc7 | 2016-06-05 07:17:19 +0800 | [diff] [blame] | 1114 | return PTR_ERR_OR_ZERO(rxkad_ci); |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * Clean up the rxkad security service. |
| 1119 | */ |
| 1120 | static void rxkad_exit(void) |
| 1121 | { |
| 1122 | if (rxkad_ci) |
| 1123 | crypto_free_skcipher(rxkad_ci); |
| 1124 | } |
| 1125 | |
| 1126 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1127 | * RxRPC Kerberos-based security |
| 1128 | */ |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1129 | const struct rxrpc_security rxkad = { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1130 | .name = "rxkad", |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 1131 | .security_index = RXRPC_SECURITY_RXKAD, |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1132 | .init = rxkad_init, |
| 1133 | .exit = rxkad_exit, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1134 | .init_connection_security = rxkad_init_connection_security, |
| 1135 | .prime_packet_security = rxkad_prime_packet_security, |
| 1136 | .secure_packet = rxkad_secure_packet, |
| 1137 | .verify_packet = rxkad_verify_packet, |
| 1138 | .issue_challenge = rxkad_issue_challenge, |
| 1139 | .respond_to_challenge = rxkad_respond_to_challenge, |
| 1140 | .verify_response = rxkad_verify_response, |
| 1141 | .clear = rxkad_clear, |
| 1142 | }; |