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 | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 278 | x = call->channel << (32 - RXRPC_CIDSHIFT); |
| 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 | */ |
| 319 | static int rxkad_verify_packet_auth(const struct rxrpc_call *call, |
| 320 | struct sk_buff *skb, |
| 321 | u32 *_abort_code) |
| 322 | { |
| 323 | struct rxkad_level1_hdr sechdr; |
| 324 | struct rxrpc_skb_priv *sp; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 325 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 326 | struct rxrpc_crypt iv; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 327 | struct scatterlist sg[16]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 328 | struct sk_buff *trailer; |
| 329 | u32 data_size, buf; |
| 330 | u16 check; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 331 | int nsg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 332 | |
| 333 | _enter(""); |
| 334 | |
| 335 | sp = rxrpc_skb(skb); |
| 336 | |
| 337 | /* we want to decrypt the skbuff in-place */ |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 338 | nsg = skb_cow_data(skb, 0, &trailer); |
| 339 | if (nsg < 0 || nsg > 16) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 340 | goto nomem; |
| 341 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 342 | sg_init_table(sg, nsg); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 343 | skb_to_sgvec(skb, sg, 0, 8); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 344 | |
| 345 | /* start the decryption afresh */ |
| 346 | memset(&iv, 0, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 347 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 348 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 349 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 350 | skcipher_request_set_crypt(req, sg, sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 351 | crypto_skcipher_decrypt(req); |
| 352 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 353 | |
| 354 | /* remove the decrypted packet length */ |
| 355 | if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) |
| 356 | goto datalen_error; |
| 357 | if (!skb_pull(skb, sizeof(sechdr))) |
| 358 | BUG(); |
| 359 | |
| 360 | buf = ntohl(sechdr.data_size); |
| 361 | data_size = buf & 0xffff; |
| 362 | |
| 363 | check = buf >> 16; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 364 | check ^= sp->hdr.seq ^ sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 365 | check &= 0xffff; |
| 366 | if (check != 0) { |
| 367 | *_abort_code = RXKADSEALEDINCON; |
| 368 | goto protocol_error; |
| 369 | } |
| 370 | |
| 371 | /* shorten the packet to remove the padding */ |
| 372 | if (data_size > skb->len) |
| 373 | goto datalen_error; |
| 374 | else if (data_size < skb->len) |
| 375 | skb->len = data_size; |
| 376 | |
| 377 | _leave(" = 0 [dlen=%x]", data_size); |
| 378 | return 0; |
| 379 | |
| 380 | datalen_error: |
| 381 | *_abort_code = RXKADDATALEN; |
| 382 | protocol_error: |
| 383 | _leave(" = -EPROTO"); |
| 384 | return -EPROTO; |
| 385 | |
| 386 | nomem: |
| 387 | _leave(" = -ENOMEM"); |
| 388 | return -ENOMEM; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * wholly decrypt a packet (level 2 security) |
| 393 | */ |
| 394 | static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call, |
| 395 | struct sk_buff *skb, |
| 396 | u32 *_abort_code) |
| 397 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 398 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 399 | struct rxkad_level2_hdr sechdr; |
| 400 | struct rxrpc_skb_priv *sp; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 401 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 402 | struct rxrpc_crypt iv; |
| 403 | struct scatterlist _sg[4], *sg; |
| 404 | struct sk_buff *trailer; |
| 405 | u32 data_size, buf; |
| 406 | u16 check; |
| 407 | int nsg; |
| 408 | |
| 409 | _enter(",{%d}", skb->len); |
| 410 | |
| 411 | sp = rxrpc_skb(skb); |
| 412 | |
| 413 | /* we want to decrypt the skbuff in-place */ |
| 414 | nsg = skb_cow_data(skb, 0, &trailer); |
| 415 | if (nsg < 0) |
| 416 | goto nomem; |
| 417 | |
| 418 | sg = _sg; |
| 419 | if (unlikely(nsg > 4)) { |
| 420 | sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO); |
| 421 | if (!sg) |
| 422 | goto nomem; |
| 423 | } |
| 424 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 425 | sg_init_table(sg, nsg); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 426 | skb_to_sgvec(skb, sg, 0, skb->len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 427 | |
| 428 | /* decrypt from the session key */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 429 | token = call->conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 430 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 431 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 432 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 433 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 434 | skcipher_request_set_crypt(req, sg, sg, skb->len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 435 | crypto_skcipher_decrypt(req); |
| 436 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 437 | if (sg != _sg) |
| 438 | kfree(sg); |
| 439 | |
| 440 | /* remove the decrypted packet length */ |
| 441 | if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) |
| 442 | goto datalen_error; |
| 443 | if (!skb_pull(skb, sizeof(sechdr))) |
| 444 | BUG(); |
| 445 | |
| 446 | buf = ntohl(sechdr.data_size); |
| 447 | data_size = buf & 0xffff; |
| 448 | |
| 449 | check = buf >> 16; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 450 | check ^= sp->hdr.seq ^ sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 451 | check &= 0xffff; |
| 452 | if (check != 0) { |
| 453 | *_abort_code = RXKADSEALEDINCON; |
| 454 | goto protocol_error; |
| 455 | } |
| 456 | |
| 457 | /* shorten the packet to remove the padding */ |
| 458 | if (data_size > skb->len) |
| 459 | goto datalen_error; |
| 460 | else if (data_size < skb->len) |
| 461 | skb->len = data_size; |
| 462 | |
| 463 | _leave(" = 0 [dlen=%x]", data_size); |
| 464 | return 0; |
| 465 | |
| 466 | datalen_error: |
| 467 | *_abort_code = RXKADDATALEN; |
| 468 | protocol_error: |
| 469 | _leave(" = -EPROTO"); |
| 470 | return -EPROTO; |
| 471 | |
| 472 | nomem: |
| 473 | _leave(" = -ENOMEM"); |
| 474 | return -ENOMEM; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * verify the security on a received packet |
| 479 | */ |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 480 | static int rxkad_verify_packet(struct rxrpc_call *call, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 481 | struct sk_buff *skb, |
| 482 | u32 *_abort_code) |
| 483 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 484 | SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 485 | struct rxrpc_skb_priv *sp; |
| 486 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 487 | struct scatterlist sg; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 488 | u16 cksum; |
| 489 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 490 | int ret; |
| 491 | |
| 492 | sp = rxrpc_skb(skb); |
| 493 | |
| 494 | _enter("{%d{%x}},{#%u}", |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 495 | call->debug_id, key_serial(call->conn->params.key), sp->hdr.seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 496 | |
| 497 | if (!call->conn->cipher) |
| 498 | return 0; |
| 499 | |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 500 | if (sp->hdr.securityIndex != RXRPC_SECURITY_RXKAD) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 501 | *_abort_code = RXKADINCONSISTENCY; |
| 502 | _leave(" = -EPROTO [not rxkad]"); |
| 503 | return -EPROTO; |
| 504 | } |
| 505 | |
| 506 | /* continue encrypting from where we left off */ |
| 507 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 508 | |
| 509 | /* validate the security checksum */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 510 | x = call->channel << (32 - RXRPC_CIDSHIFT); |
| 511 | x |= sp->hdr.seq & 0x3fffffff; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 512 | call->crypto_buf[0] = htonl(call->call_id); |
| 513 | call->crypto_buf[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 514 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 515 | sg_init_one(&sg, call->crypto_buf, 8); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 516 | skcipher_request_set_tfm(req, call->conn->cipher); |
| 517 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 518 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 519 | crypto_skcipher_encrypt(req); |
| 520 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 521 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 522 | y = ntohl(call->crypto_buf[1]); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 523 | cksum = (y >> 16) & 0xffff; |
| 524 | if (cksum == 0) |
| 525 | cksum = 1; /* zero checksums are not permitted */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 526 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 527 | if (sp->hdr.cksum != cksum) { |
| 528 | *_abort_code = RXKADSEALEDINCON; |
| 529 | _leave(" = -EPROTO [csum failed]"); |
| 530 | return -EPROTO; |
| 531 | } |
| 532 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 533 | switch (call->conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 534 | case RXRPC_SECURITY_PLAIN: |
| 535 | ret = 0; |
| 536 | break; |
| 537 | case RXRPC_SECURITY_AUTH: |
| 538 | ret = rxkad_verify_packet_auth(call, skb, _abort_code); |
| 539 | break; |
| 540 | case RXRPC_SECURITY_ENCRYPT: |
| 541 | ret = rxkad_verify_packet_encrypt(call, skb, _abort_code); |
| 542 | break; |
| 543 | default: |
| 544 | ret = -ENOANO; |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | _leave(" = %d", ret); |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * issue a challenge |
| 554 | */ |
| 555 | static int rxkad_issue_challenge(struct rxrpc_connection *conn) |
| 556 | { |
| 557 | struct rxkad_challenge challenge; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 558 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 559 | struct msghdr msg; |
| 560 | struct kvec iov[2]; |
| 561 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 562 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 563 | int ret; |
| 564 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 565 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 566 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 567 | ret = key_validate(conn->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 568 | if (ret < 0) |
| 569 | return ret; |
| 570 | |
| 571 | get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce)); |
| 572 | |
| 573 | challenge.version = htonl(2); |
| 574 | challenge.nonce = htonl(conn->security_nonce); |
| 575 | challenge.min_level = htonl(0); |
| 576 | challenge.__padding = 0; |
| 577 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 578 | msg.msg_name = &conn->params.peer->srx.transport.sin; |
| 579 | msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 580 | msg.msg_control = NULL; |
| 581 | msg.msg_controllen = 0; |
| 582 | msg.msg_flags = 0; |
| 583 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 584 | whdr.epoch = htonl(conn->proto.epoch); |
| 585 | whdr.cid = htonl(conn->proto.cid); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 586 | whdr.callNumber = 0; |
| 587 | whdr.seq = 0; |
| 588 | whdr.type = RXRPC_PACKET_TYPE_CHALLENGE; |
| 589 | whdr.flags = conn->out_clientflag; |
| 590 | whdr.userStatus = 0; |
| 591 | whdr.securityIndex = conn->security_ix; |
| 592 | whdr._rsvd = 0; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 593 | whdr.serviceId = htons(conn->params.service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 594 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 595 | iov[0].iov_base = &whdr; |
| 596 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 597 | iov[1].iov_base = &challenge; |
| 598 | iov[1].iov_len = sizeof(challenge); |
| 599 | |
| 600 | len = iov[0].iov_len + iov[1].iov_len; |
| 601 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 602 | serial = atomic_inc_return(&conn->serial); |
| 603 | whdr.serial = htonl(serial); |
| 604 | _proto("Tx CHALLENGE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 605 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 606 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 607 | if (ret < 0) { |
| 608 | _debug("sendmsg failed: %d", ret); |
| 609 | return -EAGAIN; |
| 610 | } |
| 611 | |
| 612 | _leave(" = 0"); |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * send a Kerberos security response |
| 618 | */ |
| 619 | static int rxkad_send_response(struct rxrpc_connection *conn, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 620 | struct rxrpc_host_header *hdr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 621 | struct rxkad_response *resp, |
| 622 | const struct rxkad_key *s2) |
| 623 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 624 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 625 | struct msghdr msg; |
| 626 | struct kvec iov[3]; |
| 627 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 628 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 629 | int ret; |
| 630 | |
| 631 | _enter(""); |
| 632 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 633 | msg.msg_name = &conn->params.peer->srx.transport.sin; |
| 634 | msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 635 | msg.msg_control = NULL; |
| 636 | msg.msg_controllen = 0; |
| 637 | msg.msg_flags = 0; |
| 638 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 639 | memset(&whdr, 0, sizeof(whdr)); |
| 640 | whdr.epoch = htonl(hdr->epoch); |
| 641 | whdr.cid = htonl(hdr->cid); |
| 642 | whdr.type = RXRPC_PACKET_TYPE_RESPONSE; |
| 643 | whdr.flags = conn->out_clientflag; |
| 644 | whdr.securityIndex = hdr->securityIndex; |
| 645 | whdr.serviceId = htons(hdr->serviceId); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 646 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 647 | iov[0].iov_base = &whdr; |
| 648 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 649 | iov[1].iov_base = resp; |
| 650 | iov[1].iov_len = sizeof(*resp); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 651 | iov[2].iov_base = (void *)s2->ticket; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 652 | iov[2].iov_len = s2->ticket_len; |
| 653 | |
| 654 | len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; |
| 655 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 656 | serial = atomic_inc_return(&conn->serial); |
| 657 | whdr.serial = htonl(serial); |
| 658 | _proto("Tx RESPONSE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 659 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 660 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 661 | if (ret < 0) { |
| 662 | _debug("sendmsg failed: %d", ret); |
| 663 | return -EAGAIN; |
| 664 | } |
| 665 | |
| 666 | _leave(" = 0"); |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * calculate the response checksum |
| 672 | */ |
| 673 | static void rxkad_calc_response_checksum(struct rxkad_response *response) |
| 674 | { |
| 675 | u32 csum = 1000003; |
| 676 | int loop; |
| 677 | u8 *p = (u8 *) response; |
| 678 | |
| 679 | for (loop = sizeof(*response); loop > 0; loop--) |
| 680 | csum = csum * 0x10204081 + *p++; |
| 681 | |
| 682 | response->encrypted.checksum = htonl(csum); |
| 683 | } |
| 684 | |
| 685 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 686 | * encrypt the response packet |
| 687 | */ |
| 688 | static void rxkad_encrypt_response(struct rxrpc_connection *conn, |
| 689 | struct rxkad_response *resp, |
| 690 | const struct rxkad_key *s2) |
| 691 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 692 | SKCIPHER_REQUEST_ON_STACK(req, conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 693 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 694 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 695 | |
| 696 | /* continue encrypting from where we left off */ |
| 697 | memcpy(&iv, s2->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 698 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 699 | sg_init_table(sg, 1); |
| 700 | sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 701 | skcipher_request_set_tfm(req, conn->cipher); |
| 702 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 703 | skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 704 | crypto_skcipher_encrypt(req); |
| 705 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* |
| 709 | * respond to a challenge packet |
| 710 | */ |
| 711 | static int rxkad_respond_to_challenge(struct rxrpc_connection *conn, |
| 712 | struct sk_buff *skb, |
| 713 | u32 *_abort_code) |
| 714 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 715 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 716 | struct rxkad_challenge challenge; |
| 717 | struct rxkad_response resp |
| 718 | __attribute__((aligned(8))); /* must be aligned for crypto */ |
| 719 | struct rxrpc_skb_priv *sp; |
| 720 | u32 version, nonce, min_level, abort_code; |
| 721 | int ret; |
| 722 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 723 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 724 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 725 | if (!conn->params.key) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 726 | _leave(" = -EPROTO [no key]"); |
| 727 | return -EPROTO; |
| 728 | } |
| 729 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 730 | ret = key_validate(conn->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 731 | if (ret < 0) { |
| 732 | *_abort_code = RXKADEXPIRED; |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | abort_code = RXKADPACKETSHORT; |
| 737 | sp = rxrpc_skb(skb); |
| 738 | if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0) |
| 739 | goto protocol_error; |
| 740 | |
| 741 | version = ntohl(challenge.version); |
| 742 | nonce = ntohl(challenge.nonce); |
| 743 | min_level = ntohl(challenge.min_level); |
| 744 | |
| 745 | _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 746 | sp->hdr.serial, version, nonce, min_level); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 747 | |
| 748 | abort_code = RXKADINCONSISTENCY; |
| 749 | if (version != RXKAD_VERSION) |
| 750 | goto protocol_error; |
| 751 | |
| 752 | abort_code = RXKADLEVELFAIL; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 753 | if (conn->params.security_level < min_level) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 754 | goto protocol_error; |
| 755 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 756 | token = conn->params.key->payload.data[0]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 757 | |
| 758 | /* build the response packet */ |
| 759 | memset(&resp, 0, sizeof(resp)); |
| 760 | |
David Howells | 098a209 | 2016-03-04 15:59:00 +0000 | [diff] [blame] | 761 | resp.version = htonl(RXKAD_VERSION); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 762 | resp.encrypted.epoch = htonl(conn->proto.epoch); |
| 763 | resp.encrypted.cid = htonl(conn->proto.cid); |
David Howells | 098a209 | 2016-03-04 15:59:00 +0000 | [diff] [blame] | 764 | resp.encrypted.securityIndex = htonl(conn->security_ix); |
| 765 | resp.encrypted.inc_nonce = htonl(nonce + 1); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 766 | resp.encrypted.level = htonl(conn->params.security_level); |
David Howells | 098a209 | 2016-03-04 15:59:00 +0000 | [diff] [blame] | 767 | resp.kvno = htonl(token->kad->kvno); |
| 768 | resp.ticket_len = htonl(token->kad->ticket_len); |
| 769 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 770 | resp.encrypted.call_id[0] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 771 | htonl(conn->channels[0] ? conn->channels[0]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 772 | resp.encrypted.call_id[1] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 773 | htonl(conn->channels[1] ? conn->channels[1]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 774 | resp.encrypted.call_id[2] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 775 | htonl(conn->channels[2] ? conn->channels[2]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 776 | resp.encrypted.call_id[3] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 777 | htonl(conn->channels[3] ? conn->channels[3]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 778 | |
| 779 | /* calculate the response checksum and then do the encryption */ |
| 780 | rxkad_calc_response_checksum(&resp); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 781 | rxkad_encrypt_response(conn, &resp, token->kad); |
| 782 | return rxkad_send_response(conn, &sp->hdr, &resp, token->kad); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 783 | |
| 784 | protocol_error: |
| 785 | *_abort_code = abort_code; |
| 786 | _leave(" = -EPROTO [%d]", abort_code); |
| 787 | return -EPROTO; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * decrypt the kerberos IV ticket in the response |
| 792 | */ |
| 793 | static int rxkad_decrypt_ticket(struct rxrpc_connection *conn, |
| 794 | void *ticket, size_t ticket_len, |
| 795 | struct rxrpc_crypt *_session_key, |
| 796 | time_t *_expiry, |
| 797 | u32 *_abort_code) |
| 798 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 799 | struct skcipher_request *req; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 800 | struct rxrpc_crypt iv, key; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 801 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 802 | struct in_addr addr; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 803 | unsigned int life; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 804 | time_t issue, now; |
| 805 | bool little_endian; |
| 806 | int ret; |
| 807 | u8 *p, *q, *name, *end; |
| 808 | |
| 809 | _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key)); |
| 810 | |
| 811 | *_expiry = 0; |
| 812 | |
| 813 | ret = key_validate(conn->server_key); |
| 814 | if (ret < 0) { |
| 815 | switch (ret) { |
| 816 | case -EKEYEXPIRED: |
| 817 | *_abort_code = RXKADEXPIRED; |
| 818 | goto error; |
| 819 | default: |
| 820 | *_abort_code = RXKADNOAUTH; |
| 821 | goto error; |
| 822 | } |
| 823 | } |
| 824 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 825 | ASSERT(conn->server_key->payload.data[0] != NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 826 | ASSERTCMP((unsigned long) ticket & 7UL, ==, 0); |
| 827 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 828 | memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 829 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 830 | req = skcipher_request_alloc(conn->server_key->payload.data[0], |
| 831 | GFP_NOFS); |
| 832 | if (!req) { |
| 833 | *_abort_code = RXKADNOAUTH; |
| 834 | ret = -ENOMEM; |
| 835 | goto error; |
| 836 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 837 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 838 | sg_init_one(&sg[0], ticket, ticket_len); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 839 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 840 | skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 841 | crypto_skcipher_decrypt(req); |
| 842 | skcipher_request_free(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 843 | |
| 844 | p = ticket; |
| 845 | end = p + ticket_len; |
| 846 | |
| 847 | #define Z(size) \ |
| 848 | ({ \ |
| 849 | u8 *__str = p; \ |
| 850 | q = memchr(p, 0, end - p); \ |
| 851 | if (!q || q - p > (size)) \ |
| 852 | goto bad_ticket; \ |
| 853 | for (; p < q; p++) \ |
| 854 | if (!isprint(*p)) \ |
| 855 | goto bad_ticket; \ |
| 856 | p++; \ |
| 857 | __str; \ |
| 858 | }) |
| 859 | |
| 860 | /* extract the ticket flags */ |
| 861 | _debug("KIV FLAGS: %x", *p); |
| 862 | little_endian = *p & 1; |
| 863 | p++; |
| 864 | |
| 865 | /* extract the authentication name */ |
| 866 | name = Z(ANAME_SZ); |
| 867 | _debug("KIV ANAME: %s", name); |
| 868 | |
| 869 | /* extract the principal's instance */ |
| 870 | name = Z(INST_SZ); |
| 871 | _debug("KIV INST : %s", name); |
| 872 | |
| 873 | /* extract the principal's authentication domain */ |
| 874 | name = Z(REALM_SZ); |
| 875 | _debug("KIV REALM: %s", name); |
| 876 | |
| 877 | if (end - p < 4 + 8 + 4 + 2) |
| 878 | goto bad_ticket; |
| 879 | |
| 880 | /* get the IPv4 address of the entity that requested the ticket */ |
| 881 | memcpy(&addr, p, sizeof(addr)); |
| 882 | p += 4; |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 883 | _debug("KIV ADDR : %pI4", &addr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 884 | |
| 885 | /* get the session key from the ticket */ |
| 886 | memcpy(&key, p, sizeof(key)); |
| 887 | p += 8; |
| 888 | _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1])); |
| 889 | memcpy(_session_key, &key, sizeof(key)); |
| 890 | |
| 891 | /* get the ticket's lifetime */ |
| 892 | life = *p++ * 5 * 60; |
| 893 | _debug("KIV LIFE : %u", life); |
| 894 | |
| 895 | /* get the issue time of the ticket */ |
| 896 | if (little_endian) { |
| 897 | __le32 stamp; |
| 898 | memcpy(&stamp, p, 4); |
| 899 | issue = le32_to_cpu(stamp); |
| 900 | } else { |
| 901 | __be32 stamp; |
| 902 | memcpy(&stamp, p, 4); |
| 903 | issue = be32_to_cpu(stamp); |
| 904 | } |
| 905 | p += 4; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 906 | now = get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 907 | _debug("KIV ISSUE: %lx [%lx]", issue, now); |
| 908 | |
| 909 | /* check the ticket is in date */ |
| 910 | if (issue > now) { |
| 911 | *_abort_code = RXKADNOAUTH; |
| 912 | ret = -EKEYREJECTED; |
| 913 | goto error; |
| 914 | } |
| 915 | |
| 916 | if (issue < now - life) { |
| 917 | *_abort_code = RXKADEXPIRED; |
| 918 | ret = -EKEYEXPIRED; |
| 919 | goto error; |
| 920 | } |
| 921 | |
| 922 | *_expiry = issue + life; |
| 923 | |
| 924 | /* get the service name */ |
| 925 | name = Z(SNAME_SZ); |
| 926 | _debug("KIV SNAME: %s", name); |
| 927 | |
| 928 | /* get the service instance name */ |
| 929 | name = Z(INST_SZ); |
| 930 | _debug("KIV SINST: %s", name); |
| 931 | |
| 932 | ret = 0; |
| 933 | error: |
| 934 | _leave(" = %d", ret); |
| 935 | return ret; |
| 936 | |
| 937 | bad_ticket: |
| 938 | *_abort_code = RXKADBADTICKET; |
| 939 | ret = -EBADMSG; |
| 940 | goto error; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * decrypt the response packet |
| 945 | */ |
| 946 | static void rxkad_decrypt_response(struct rxrpc_connection *conn, |
| 947 | struct rxkad_response *resp, |
| 948 | const struct rxrpc_crypt *session_key) |
| 949 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 950 | SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 951 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 952 | struct rxrpc_crypt iv; |
| 953 | |
| 954 | _enter(",,%08x%08x", |
| 955 | ntohl(session_key->n[0]), ntohl(session_key->n[1])); |
| 956 | |
| 957 | ASSERT(rxkad_ci != NULL); |
| 958 | |
| 959 | mutex_lock(&rxkad_ci_mutex); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 960 | if (crypto_skcipher_setkey(rxkad_ci, session_key->x, |
| 961 | sizeof(*session_key)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 962 | BUG(); |
| 963 | |
| 964 | memcpy(&iv, session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 965 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame^] | 966 | sg_init_table(sg, 1); |
| 967 | sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 968 | skcipher_request_set_tfm(req, rxkad_ci); |
| 969 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 970 | skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 971 | crypto_skcipher_decrypt(req); |
| 972 | skcipher_request_zero(req); |
| 973 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 974 | mutex_unlock(&rxkad_ci_mutex); |
| 975 | |
| 976 | _leave(""); |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * verify a response |
| 981 | */ |
| 982 | static int rxkad_verify_response(struct rxrpc_connection *conn, |
| 983 | struct sk_buff *skb, |
| 984 | u32 *_abort_code) |
| 985 | { |
| 986 | struct rxkad_response response |
| 987 | __attribute__((aligned(8))); /* must be aligned for crypto */ |
| 988 | struct rxrpc_skb_priv *sp; |
| 989 | struct rxrpc_crypt session_key; |
| 990 | time_t expiry; |
| 991 | void *ticket; |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 992 | u32 abort_code, version, kvno, ticket_len, level; |
| 993 | __be32 csum; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 994 | int ret; |
| 995 | |
| 996 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key)); |
| 997 | |
| 998 | abort_code = RXKADPACKETSHORT; |
| 999 | if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0) |
| 1000 | goto protocol_error; |
| 1001 | if (!pskb_pull(skb, sizeof(response))) |
| 1002 | BUG(); |
| 1003 | |
| 1004 | version = ntohl(response.version); |
| 1005 | ticket_len = ntohl(response.ticket_len); |
| 1006 | kvno = ntohl(response.kvno); |
| 1007 | sp = rxrpc_skb(skb); |
| 1008 | _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1009 | sp->hdr.serial, version, kvno, ticket_len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1010 | |
| 1011 | abort_code = RXKADINCONSISTENCY; |
| 1012 | if (version != RXKAD_VERSION) |
David Howells | 4aa9cb3 | 2007-12-07 04:31:47 -0800 | [diff] [blame] | 1013 | goto protocol_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1014 | |
| 1015 | abort_code = RXKADTICKETLEN; |
| 1016 | if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN) |
| 1017 | goto protocol_error; |
| 1018 | |
| 1019 | abort_code = RXKADUNKNOWNKEY; |
| 1020 | if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5) |
| 1021 | goto protocol_error; |
| 1022 | |
| 1023 | /* extract the kerberos ticket and decrypt and decode it */ |
| 1024 | ticket = kmalloc(ticket_len, GFP_NOFS); |
| 1025 | if (!ticket) |
| 1026 | return -ENOMEM; |
| 1027 | |
| 1028 | abort_code = RXKADPACKETSHORT; |
| 1029 | if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0) |
| 1030 | goto protocol_error_free; |
| 1031 | |
| 1032 | ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key, |
| 1033 | &expiry, &abort_code); |
| 1034 | if (ret < 0) { |
| 1035 | *_abort_code = abort_code; |
| 1036 | kfree(ticket); |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | /* use the session key from inside the ticket to decrypt the |
| 1041 | * response */ |
| 1042 | rxkad_decrypt_response(conn, &response, &session_key); |
| 1043 | |
| 1044 | abort_code = RXKADSEALEDINCON; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1045 | if (ntohl(response.encrypted.epoch) != conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1046 | goto protocol_error_free; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1047 | if (ntohl(response.encrypted.cid) != conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1048 | goto protocol_error_free; |
| 1049 | if (ntohl(response.encrypted.securityIndex) != conn->security_ix) |
| 1050 | goto protocol_error_free; |
| 1051 | csum = response.encrypted.checksum; |
| 1052 | response.encrypted.checksum = 0; |
| 1053 | rxkad_calc_response_checksum(&response); |
| 1054 | if (response.encrypted.checksum != csum) |
| 1055 | goto protocol_error_free; |
| 1056 | |
| 1057 | if (ntohl(response.encrypted.call_id[0]) > INT_MAX || |
| 1058 | ntohl(response.encrypted.call_id[1]) > INT_MAX || |
| 1059 | ntohl(response.encrypted.call_id[2]) > INT_MAX || |
| 1060 | ntohl(response.encrypted.call_id[3]) > INT_MAX) |
| 1061 | goto protocol_error_free; |
| 1062 | |
| 1063 | abort_code = RXKADOUTOFSEQUENCE; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1064 | if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1065 | goto protocol_error_free; |
| 1066 | |
| 1067 | abort_code = RXKADLEVELFAIL; |
| 1068 | level = ntohl(response.encrypted.level); |
| 1069 | if (level > RXRPC_SECURITY_ENCRYPT) |
| 1070 | goto protocol_error_free; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1071 | conn->params.security_level = level; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1072 | |
| 1073 | /* create a key to hold the security data and expiration time - after |
| 1074 | * this the connection security can be handled in exactly the same way |
| 1075 | * as for a client connection */ |
| 1076 | ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno); |
| 1077 | if (ret < 0) { |
| 1078 | kfree(ticket); |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | kfree(ticket); |
| 1083 | _leave(" = 0"); |
| 1084 | return 0; |
| 1085 | |
| 1086 | protocol_error_free: |
| 1087 | kfree(ticket); |
| 1088 | protocol_error: |
| 1089 | *_abort_code = abort_code; |
| 1090 | _leave(" = -EPROTO [%d]", abort_code); |
| 1091 | return -EPROTO; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * clear the connection security |
| 1096 | */ |
| 1097 | static void rxkad_clear(struct rxrpc_connection *conn) |
| 1098 | { |
| 1099 | _enter(""); |
| 1100 | |
| 1101 | if (conn->cipher) |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 1102 | crypto_free_skcipher(conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | /* |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1106 | * Initialise the rxkad security service. |
| 1107 | */ |
| 1108 | static int rxkad_init(void) |
| 1109 | { |
| 1110 | /* pin the cipher we need so that the crypto layer doesn't invoke |
| 1111 | * keventd to go get it */ |
| 1112 | rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC); |
Wu Fengguang | fa54cc7 | 2016-06-05 07:17:19 +0800 | [diff] [blame] | 1113 | return PTR_ERR_OR_ZERO(rxkad_ci); |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * Clean up the rxkad security service. |
| 1118 | */ |
| 1119 | static void rxkad_exit(void) |
| 1120 | { |
| 1121 | if (rxkad_ci) |
| 1122 | crypto_free_skcipher(rxkad_ci); |
| 1123 | } |
| 1124 | |
| 1125 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1126 | * RxRPC Kerberos-based security |
| 1127 | */ |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1128 | const struct rxrpc_security rxkad = { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1129 | .name = "rxkad", |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 1130 | .security_index = RXRPC_SECURITY_RXKAD, |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1131 | .init = rxkad_init, |
| 1132 | .exit = rxkad_exit, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1133 | .init_connection_security = rxkad_init_connection_security, |
| 1134 | .prime_packet_security = rxkad_prime_packet_security, |
| 1135 | .secure_packet = rxkad_secure_packet, |
| 1136 | .verify_packet = rxkad_verify_packet, |
| 1137 | .issue_challenge = rxkad_issue_challenge, |
| 1138 | .respond_to_challenge = rxkad_respond_to_challenge, |
| 1139 | .verify_response = rxkad_verify_response, |
| 1140 | .clear = rxkad_clear, |
| 1141 | }; |