Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * COPYRIGHT (c) 2008 |
| 3 | * The Regents of the University of Michigan |
| 4 | * ALL RIGHTS RESERVED |
| 5 | * |
| 6 | * Permission is granted to use, copy, create derivative works |
| 7 | * and redistribute this software and such derivative works |
| 8 | * for any purpose, so long as the name of The University of |
| 9 | * Michigan is not used in any advertising or publicity |
| 10 | * pertaining to the use of distribution of this software |
| 11 | * without specific, written prior authorization. If the |
| 12 | * above copyright notice or any other identification of the |
| 13 | * University of Michigan is included in any copy of any |
| 14 | * portion of this software, then the disclaimer below must |
| 15 | * also be included. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION |
| 18 | * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY |
| 19 | * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF |
| 20 | * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING |
| 21 | * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF |
| 22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 23 | * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE |
| 24 | * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR |
| 25 | * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING |
| 26 | * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN |
| 27 | * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGES. |
| 29 | */ |
| 30 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 31 | #include <linux/types.h> |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
| 33 | #include <linux/sunrpc/gss_krb5.h> |
| 34 | #include <linux/random.h> |
| 35 | #include <linux/pagemap.h> |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 36 | #include <linux/crypto.h> |
| 37 | |
| 38 | #ifdef RPC_DEBUG |
| 39 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 40 | #endif |
| 41 | |
| 42 | static inline int |
| 43 | gss_krb5_padding(int blocksize, int length) |
| 44 | { |
J. Bruce Fields | 54ec3d4 | 2010-03-17 13:02:48 -0400 | [diff] [blame] | 45 | return blocksize - (length % blocksize); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static inline void |
| 49 | gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize) |
| 50 | { |
| 51 | int padding = gss_krb5_padding(blocksize, buf->len - offset); |
| 52 | char *p; |
| 53 | struct kvec *iov; |
| 54 | |
| 55 | if (buf->page_len || buf->tail[0].iov_len) |
| 56 | iov = &buf->tail[0]; |
| 57 | else |
| 58 | iov = &buf->head[0]; |
| 59 | p = iov->iov_base + iov->iov_len; |
| 60 | iov->iov_len += padding; |
| 61 | buf->len += padding; |
| 62 | memset(p, padding, padding); |
| 63 | } |
| 64 | |
| 65 | static inline int |
| 66 | gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize) |
| 67 | { |
| 68 | u8 *ptr; |
| 69 | u8 pad; |
Chuck Lever | 67f97d8 | 2007-09-26 14:38:10 -0400 | [diff] [blame] | 70 | size_t len = buf->len; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 71 | |
| 72 | if (len <= buf->head[0].iov_len) { |
| 73 | pad = *(u8 *)(buf->head[0].iov_base + len - 1); |
| 74 | if (pad > buf->head[0].iov_len) |
| 75 | return -EINVAL; |
| 76 | buf->head[0].iov_len -= pad; |
| 77 | goto out; |
| 78 | } else |
| 79 | len -= buf->head[0].iov_len; |
| 80 | if (len <= buf->page_len) { |
Chuck Lever | 67f97d8 | 2007-09-26 14:38:10 -0400 | [diff] [blame] | 81 | unsigned int last = (buf->page_base + len - 1) |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 82 | >>PAGE_CACHE_SHIFT; |
Chuck Lever | 67f97d8 | 2007-09-26 14:38:10 -0400 | [diff] [blame] | 83 | unsigned int offset = (buf->page_base + len - 1) |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 84 | & (PAGE_CACHE_SIZE - 1); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 85 | ptr = kmap_atomic(buf->pages[last]); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 86 | pad = *(ptr + offset); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 87 | kunmap_atomic(ptr); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 88 | goto out; |
| 89 | } else |
| 90 | len -= buf->page_len; |
| 91 | BUG_ON(len > buf->tail[0].iov_len); |
| 92 | pad = *(u8 *)(buf->tail[0].iov_base + len - 1); |
| 93 | out: |
| 94 | /* XXX: NOTE: we do not adjust the page lengths--they represent |
| 95 | * a range of data in the real filesystem page cache, and we need |
| 96 | * to know that range so the xdr code can properly place read data. |
| 97 | * However adjusting the head length, as we do above, is harmless. |
| 98 | * In the case of a request that fits into a single page, the server |
| 99 | * also uses length and head length together to determine the original |
| 100 | * start of the request to copy the request for deferal; so it's |
| 101 | * easier on the server if we adjust head and tail length in tandem. |
| 102 | * It's not really a problem that we don't fool with the page and |
| 103 | * tail lengths, though--at worst badly formed xdr might lead the |
| 104 | * server to attempt to parse the padding. |
| 105 | * XXX: Document all these weird requirements for gss mechanism |
| 106 | * wrap/unwrap functions. */ |
| 107 | if (pad > blocksize) |
| 108 | return -EINVAL; |
| 109 | if (buf->len > pad) |
| 110 | buf->len -= pad; |
| 111 | else |
| 112 | return -EINVAL; |
| 113 | return 0; |
| 114 | } |
| 115 | |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 116 | void |
| 117 | gss_krb5_make_confounder(char *p, u32 conflen) |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 118 | { |
| 119 | static u64 i = 0; |
| 120 | u64 *q = (u64 *)p; |
| 121 | |
| 122 | /* rfc1964 claims this should be "random". But all that's really |
| 123 | * necessary is that it be unique. And not even that is necessary in |
| 124 | * our case since our "gssapi" implementation exists only to support |
| 125 | * rpcsec_gss, so we know that the only buffers we will ever encrypt |
| 126 | * already begin with a unique sequence number. Just to hedge my bets |
| 127 | * I'll make a half-hearted attempt at something unique, but ensuring |
| 128 | * uniqueness would mean worrying about atomicity and rollover, and I |
| 129 | * don't care enough. */ |
| 130 | |
Kevin Coffman | 863a248 | 2008-04-30 12:46:08 -0400 | [diff] [blame] | 131 | /* initialize to random value */ |
| 132 | if (i == 0) { |
Akinobu Mita | c86d2dd | 2013-04-29 16:21:37 -0700 | [diff] [blame] | 133 | i = prandom_u32(); |
| 134 | i = (i << 32) | prandom_u32(); |
Kevin Coffman | 863a248 | 2008-04-30 12:46:08 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | switch (conflen) { |
| 138 | case 16: |
| 139 | *q++ = i++; |
| 140 | /* fall through */ |
| 141 | case 8: |
| 142 | *q++ = i++; |
| 143 | break; |
| 144 | default: |
| 145 | BUG(); |
| 146 | } |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* Assumptions: the head and tail of inbuf are ours to play with. |
| 150 | * The pages, however, may be real pages in the page cache and we replace |
| 151 | * them with scratch pages from **pages before writing to them. */ |
| 152 | /* XXX: obviously the above should be documentation of wrap interface, |
| 153 | * and shouldn't be in this kerberos-specific file. */ |
| 154 | |
| 155 | /* XXX factor out common code with seal/unseal. */ |
| 156 | |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 157 | static u32 |
| 158 | gss_wrap_kerberos_v1(struct krb5_ctx *kctx, int offset, |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 159 | struct xdr_buf *buf, struct page **pages) |
| 160 | { |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 161 | char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; |
| 162 | struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), |
| 163 | .data = cksumdata}; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 164 | int blocksize = 0, plainlen; |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 165 | unsigned char *ptr, *msg_start; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 166 | s32 now; |
| 167 | int headlen; |
| 168 | struct page **tmp_pages; |
J. Bruce Fields | eaa82ed | 2006-03-20 23:24:04 -0500 | [diff] [blame] | 169 | u32 seq_send; |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 170 | u8 *cksumkey; |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 171 | u32 conflen = kctx->gk5e->conflen; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 172 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 173 | dprintk("RPC: %s\n", __func__); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 174 | |
| 175 | now = get_seconds(); |
| 176 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 177 | blocksize = crypto_blkcipher_blocksize(kctx->enc); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 178 | gss_krb5_add_padding(buf, offset, blocksize); |
| 179 | BUG_ON((buf->len - offset) % blocksize); |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 180 | plainlen = conflen + buf->len - offset; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 181 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 182 | headlen = g_token_size(&kctx->mech_used, |
| 183 | GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength + plainlen) - |
| 184 | (buf->len - offset); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 185 | |
| 186 | ptr = buf->head[0].iov_base + offset; |
| 187 | /* shift data to make room for header. */ |
Kevin Coffman | 725f286 | 2010-03-17 13:02:46 -0400 | [diff] [blame] | 188 | xdr_extend_head(buf, offset, headlen); |
| 189 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 190 | /* XXX Would be cleverer to encrypt while copying. */ |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 191 | BUG_ON((buf->len - offset - headlen) % blocksize); |
| 192 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 193 | g_make_token_header(&kctx->mech_used, |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 194 | GSS_KRB5_TOK_HDR_LEN + |
| 195 | kctx->gk5e->cksumlength + plainlen, &ptr); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 196 | |
| 197 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 198 | /* ptr now at header described in rfc 1964, section 1.2.1: */ |
| 199 | ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff); |
| 200 | ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 201 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 202 | msg_start = ptr + GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 203 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 204 | *(__be16 *)(ptr + 2) = cpu_to_le16(kctx->gk5e->signalg); |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 205 | memset(ptr + 4, 0xff, 4); |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 206 | *(__be16 *)(ptr + 4) = cpu_to_le16(kctx->gk5e->sealalg); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 207 | |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 208 | gss_krb5_make_confounder(msg_start, conflen); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 209 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 210 | if (kctx->gk5e->keyed_cksum) |
| 211 | cksumkey = kctx->cksum; |
| 212 | else |
| 213 | cksumkey = NULL; |
| 214 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 215 | /* XXXJBF: UGH!: */ |
| 216 | tmp_pages = buf->pages; |
| 217 | buf->pages = pages; |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 218 | if (make_checksum(kctx, ptr, 8, buf, offset + headlen - conflen, |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 219 | cksumkey, KG_USAGE_SEAL, &md5cksum)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 220 | return GSS_S_FAILURE; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 221 | buf->pages = tmp_pages; |
| 222 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 223 | memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 224 | |
J. Bruce Fields | eaa82ed | 2006-03-20 23:24:04 -0500 | [diff] [blame] | 225 | spin_lock(&krb5_seq_lock); |
| 226 | seq_send = kctx->seq_send++; |
| 227 | spin_unlock(&krb5_seq_lock); |
| 228 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 229 | /* XXX would probably be more efficient to compute checksum |
| 230 | * and encrypt at the same time: */ |
Kevin Coffman | 1dbd902 | 2010-03-17 13:03:04 -0400 | [diff] [blame] | 231 | if ((krb5_make_seq_num(kctx, kctx->seq, kctx->initiate ? 0 : 0xff, |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 232 | seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8))) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 233 | return GSS_S_FAILURE; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 234 | |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 235 | if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC) { |
| 236 | struct crypto_blkcipher *cipher; |
| 237 | int err; |
| 238 | cipher = crypto_alloc_blkcipher(kctx->gk5e->encrypt_name, 0, |
| 239 | CRYPTO_ALG_ASYNC); |
| 240 | if (IS_ERR(cipher)) |
| 241 | return GSS_S_FAILURE; |
| 242 | |
| 243 | krb5_rc4_setup_enc_key(kctx, cipher, seq_send); |
| 244 | |
| 245 | err = gss_encrypt_xdr_buf(cipher, buf, |
| 246 | offset + headlen - conflen, pages); |
| 247 | crypto_free_blkcipher(cipher); |
| 248 | if (err) |
| 249 | return GSS_S_FAILURE; |
| 250 | } else { |
| 251 | if (gss_encrypt_xdr_buf(kctx->enc, buf, |
| 252 | offset + headlen - conflen, pages)) |
| 253 | return GSS_S_FAILURE; |
| 254 | } |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 255 | |
J. Bruce Fields | 94efa93 | 2006-12-04 20:22:42 -0500 | [diff] [blame] | 256 | return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 259 | static u32 |
| 260 | gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf) |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 261 | { |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 262 | int signalg; |
| 263 | int sealalg; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 264 | char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; |
| 265 | struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), |
| 266 | .data = cksumdata}; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 267 | s32 now; |
| 268 | int direction; |
| 269 | s32 seqnum; |
| 270 | unsigned char *ptr; |
| 271 | int bodysize; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 272 | void *data_start, *orig_start; |
| 273 | int data_len; |
| 274 | int blocksize; |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 275 | u32 conflen = kctx->gk5e->conflen; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 276 | int crypt_offset; |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 277 | u8 *cksumkey; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 278 | |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 279 | dprintk("RPC: gss_unwrap_kerberos\n"); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 280 | |
| 281 | ptr = (u8 *)buf->head[0].iov_base + offset; |
| 282 | if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr, |
| 283 | buf->len - offset)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 284 | return GSS_S_DEFECTIVE_TOKEN; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 285 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 286 | if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) || |
| 287 | (ptr[1] != (KG_TOK_WRAP_MSG & 0xff))) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 288 | return GSS_S_DEFECTIVE_TOKEN; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 289 | |
| 290 | /* XXX sanity-check bodysize?? */ |
| 291 | |
| 292 | /* get the sign and seal algorithms */ |
| 293 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 294 | signalg = ptr[2] + (ptr[3] << 8); |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 295 | if (signalg != kctx->gk5e->signalg) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 296 | return GSS_S_DEFECTIVE_TOKEN; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 297 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 298 | sealalg = ptr[4] + (ptr[5] << 8); |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 299 | if (sealalg != kctx->gk5e->sealalg) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 300 | return GSS_S_DEFECTIVE_TOKEN; |
J. Bruce Fields | 94efa93 | 2006-12-04 20:22:42 -0500 | [diff] [blame] | 301 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 302 | if ((ptr[6] != 0xff) || (ptr[7] != 0xff)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 303 | return GSS_S_DEFECTIVE_TOKEN; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 304 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 305 | /* |
| 306 | * Data starts after token header and checksum. ptr points |
| 307 | * to the beginning of the token header |
| 308 | */ |
| 309 | crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) - |
| 310 | (unsigned char *)buf->head[0].iov_base; |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * Need plaintext seqnum to derive encryption key for arcfour-hmac |
| 314 | */ |
| 315 | if (krb5_get_seq_num(kctx, ptr + GSS_KRB5_TOK_HDR_LEN, |
| 316 | ptr + 8, &direction, &seqnum)) |
| 317 | return GSS_S_BAD_SIG; |
| 318 | |
| 319 | if ((kctx->initiate && direction != 0xff) || |
| 320 | (!kctx->initiate && direction != 0)) |
| 321 | return GSS_S_BAD_SIG; |
| 322 | |
| 323 | if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC) { |
| 324 | struct crypto_blkcipher *cipher; |
| 325 | int err; |
| 326 | |
| 327 | cipher = crypto_alloc_blkcipher(kctx->gk5e->encrypt_name, 0, |
| 328 | CRYPTO_ALG_ASYNC); |
| 329 | if (IS_ERR(cipher)) |
| 330 | return GSS_S_FAILURE; |
| 331 | |
| 332 | krb5_rc4_setup_enc_key(kctx, cipher, seqnum); |
| 333 | |
| 334 | err = gss_decrypt_xdr_buf(cipher, buf, crypt_offset); |
| 335 | crypto_free_blkcipher(cipher); |
| 336 | if (err) |
| 337 | return GSS_S_DEFECTIVE_TOKEN; |
| 338 | } else { |
| 339 | if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset)) |
| 340 | return GSS_S_DEFECTIVE_TOKEN; |
| 341 | } |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 342 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 343 | if (kctx->gk5e->keyed_cksum) |
| 344 | cksumkey = kctx->cksum; |
| 345 | else |
| 346 | cksumkey = NULL; |
| 347 | |
| 348 | if (make_checksum(kctx, ptr, 8, buf, crypt_offset, |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 349 | cksumkey, KG_USAGE_SEAL, &md5cksum)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 350 | return GSS_S_FAILURE; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 351 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 352 | if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN, |
| 353 | kctx->gk5e->cksumlength)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 354 | return GSS_S_BAD_SIG; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 355 | |
| 356 | /* it got through unscathed. Make sure the context is unexpired */ |
| 357 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 358 | now = get_seconds(); |
| 359 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 360 | if (now > kctx->endtime) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 361 | return GSS_S_CONTEXT_EXPIRED; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 362 | |
| 363 | /* do sequencing checks */ |
| 364 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 365 | /* Copy the data back to the right position. XXX: Would probably be |
| 366 | * better to copy and encrypt at the same time. */ |
| 367 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 368 | blocksize = crypto_blkcipher_blocksize(kctx->enc); |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 369 | data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) + |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 370 | conflen; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 371 | orig_start = buf->head[0].iov_base + offset; |
| 372 | data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start; |
| 373 | memmove(orig_start, data_start, data_len); |
| 374 | buf->head[0].iov_len -= (data_start - orig_start); |
| 375 | buf->len -= (data_start - orig_start); |
| 376 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 377 | if (gss_krb5_remove_padding(buf, blocksize)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 378 | return GSS_S_DEFECTIVE_TOKEN; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 379 | |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 380 | return GSS_S_COMPLETE; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 381 | } |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 382 | |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 383 | /* |
J. Bruce Fields | c52226d | 2012-04-11 20:08:45 -0400 | [diff] [blame] | 384 | * We can shift data by up to LOCAL_BUF_LEN bytes in a pass. If we need |
| 385 | * to do more than that, we shift repeatedly. Kevin Coffman reports |
| 386 | * seeing 28 bytes as the value used by Microsoft clients and servers |
| 387 | * with AES, so this constant is chosen to allow handling 28 in one pass |
| 388 | * without using too much stack space. |
| 389 | * |
| 390 | * If that proves to a problem perhaps we could use a more clever |
| 391 | * algorithm. |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 392 | */ |
J. Bruce Fields | c52226d | 2012-04-11 20:08:45 -0400 | [diff] [blame] | 393 | #define LOCAL_BUF_LEN 32u |
| 394 | |
| 395 | static void rotate_buf_a_little(struct xdr_buf *buf, unsigned int shift) |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 396 | { |
J. Bruce Fields | c52226d | 2012-04-11 20:08:45 -0400 | [diff] [blame] | 397 | char head[LOCAL_BUF_LEN]; |
| 398 | char tmp[LOCAL_BUF_LEN]; |
| 399 | unsigned int this_len, i; |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 400 | |
J. Bruce Fields | c52226d | 2012-04-11 20:08:45 -0400 | [diff] [blame] | 401 | BUG_ON(shift > LOCAL_BUF_LEN); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 402 | |
J. Bruce Fields | c52226d | 2012-04-11 20:08:45 -0400 | [diff] [blame] | 403 | read_bytes_from_xdr_buf(buf, 0, head, shift); |
| 404 | for (i = 0; i + shift < buf->len; i += LOCAL_BUF_LEN) { |
| 405 | this_len = min(LOCAL_BUF_LEN, buf->len - (i + shift)); |
| 406 | read_bytes_from_xdr_buf(buf, i+shift, tmp, this_len); |
| 407 | write_bytes_to_xdr_buf(buf, i, tmp, this_len); |
| 408 | } |
| 409 | write_bytes_to_xdr_buf(buf, buf->len - shift, head, shift); |
| 410 | } |
| 411 | |
| 412 | static void _rotate_left(struct xdr_buf *buf, unsigned int shift) |
| 413 | { |
| 414 | int shifted = 0; |
| 415 | int this_shift; |
| 416 | |
| 417 | shift %= buf->len; |
| 418 | while (shifted < shift) { |
| 419 | this_shift = min(shift - shifted, LOCAL_BUF_LEN); |
| 420 | rotate_buf_a_little(buf, this_shift); |
| 421 | shifted += this_shift; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void rotate_left(u32 base, struct xdr_buf *buf, unsigned int shift) |
| 426 | { |
| 427 | struct xdr_buf subbuf; |
| 428 | |
| 429 | xdr_buf_subsegment(buf, &subbuf, base, buf->len - base); |
| 430 | _rotate_left(&subbuf, shift); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static u32 |
| 434 | gss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset, |
| 435 | struct xdr_buf *buf, struct page **pages) |
| 436 | { |
| 437 | int blocksize; |
| 438 | u8 *ptr, *plainhdr; |
| 439 | s32 now; |
| 440 | u8 flags = 0x00; |
| 441 | __be16 *be16ptr, ec = 0; |
| 442 | __be64 *be64ptr; |
| 443 | u32 err; |
| 444 | |
| 445 | dprintk("RPC: %s\n", __func__); |
| 446 | |
| 447 | if (kctx->gk5e->encrypt_v2 == NULL) |
| 448 | return GSS_S_FAILURE; |
| 449 | |
| 450 | /* make room for gss token header */ |
| 451 | if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN)) |
| 452 | return GSS_S_FAILURE; |
| 453 | |
| 454 | /* construct gss token header */ |
| 455 | ptr = plainhdr = buf->head[0].iov_base + offset; |
| 456 | *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff); |
| 457 | *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff); |
| 458 | |
| 459 | if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0) |
| 460 | flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR; |
| 461 | if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0) |
| 462 | flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY; |
| 463 | /* We always do confidentiality in wrap tokens */ |
| 464 | flags |= KG2_TOKEN_FLAG_SEALED; |
| 465 | |
| 466 | *ptr++ = flags; |
| 467 | *ptr++ = 0xff; |
| 468 | be16ptr = (__be16 *)ptr; |
| 469 | |
| 470 | blocksize = crypto_blkcipher_blocksize(kctx->acceptor_enc); |
| 471 | *be16ptr++ = cpu_to_be16(ec); |
| 472 | /* "inner" token header always uses 0 for RRC */ |
| 473 | *be16ptr++ = cpu_to_be16(0); |
| 474 | |
| 475 | be64ptr = (__be64 *)be16ptr; |
| 476 | spin_lock(&krb5_seq_lock); |
| 477 | *be64ptr = cpu_to_be64(kctx->seq_send64++); |
| 478 | spin_unlock(&krb5_seq_lock); |
| 479 | |
| 480 | err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, ec, pages); |
| 481 | if (err) |
| 482 | return err; |
| 483 | |
| 484 | now = get_seconds(); |
| 485 | return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; |
| 486 | } |
| 487 | |
| 488 | static u32 |
| 489 | gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf) |
| 490 | { |
| 491 | s32 now; |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 492 | u8 *ptr; |
| 493 | u8 flags = 0x00; |
| 494 | u16 ec, rrc; |
| 495 | int err; |
| 496 | u32 headskip, tailskip; |
| 497 | u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN]; |
| 498 | unsigned int movelen; |
| 499 | |
| 500 | |
| 501 | dprintk("RPC: %s\n", __func__); |
| 502 | |
| 503 | if (kctx->gk5e->decrypt_v2 == NULL) |
| 504 | return GSS_S_FAILURE; |
| 505 | |
| 506 | ptr = buf->head[0].iov_base + offset; |
| 507 | |
| 508 | if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP) |
| 509 | return GSS_S_DEFECTIVE_TOKEN; |
| 510 | |
| 511 | flags = ptr[2]; |
| 512 | if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) || |
| 513 | (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR))) |
| 514 | return GSS_S_BAD_SIG; |
| 515 | |
| 516 | if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) { |
| 517 | dprintk("%s: token missing expected sealed flag\n", __func__); |
| 518 | return GSS_S_DEFECTIVE_TOKEN; |
| 519 | } |
| 520 | |
| 521 | if (ptr[3] != 0xff) |
| 522 | return GSS_S_DEFECTIVE_TOKEN; |
| 523 | |
| 524 | ec = be16_to_cpup((__be16 *)(ptr + 4)); |
| 525 | rrc = be16_to_cpup((__be16 *)(ptr + 6)); |
| 526 | |
J. Bruce Fields | 5d6baef | 2013-10-09 15:59:29 -0400 | [diff] [blame] | 527 | /* |
| 528 | * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss |
| 529 | * doesn't want it checked; see page 6 of rfc 2203. |
| 530 | */ |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 531 | |
J. Bruce Fields | c52226d | 2012-04-11 20:08:45 -0400 | [diff] [blame] | 532 | if (rrc != 0) |
| 533 | rotate_left(offset + 16, buf, rrc); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 534 | |
| 535 | err = (*kctx->gk5e->decrypt_v2)(kctx, offset, buf, |
| 536 | &headskip, &tailskip); |
| 537 | if (err) |
| 538 | return GSS_S_FAILURE; |
| 539 | |
| 540 | /* |
| 541 | * Retrieve the decrypted gss token header and verify |
| 542 | * it against the original |
| 543 | */ |
| 544 | err = read_bytes_from_xdr_buf(buf, |
| 545 | buf->len - GSS_KRB5_TOK_HDR_LEN - tailskip, |
| 546 | decrypted_hdr, GSS_KRB5_TOK_HDR_LEN); |
| 547 | if (err) { |
| 548 | dprintk("%s: error %u getting decrypted_hdr\n", __func__, err); |
| 549 | return GSS_S_FAILURE; |
| 550 | } |
| 551 | if (memcmp(ptr, decrypted_hdr, 6) |
| 552 | || memcmp(ptr + 8, decrypted_hdr + 8, 8)) { |
| 553 | dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__); |
| 554 | return GSS_S_FAILURE; |
| 555 | } |
| 556 | |
| 557 | /* do sequencing checks */ |
| 558 | |
| 559 | /* it got through unscathed. Make sure the context is unexpired */ |
| 560 | now = get_seconds(); |
| 561 | if (now > kctx->endtime) |
| 562 | return GSS_S_CONTEXT_EXPIRED; |
| 563 | |
| 564 | /* |
| 565 | * Move the head data back to the right position in xdr_buf. |
| 566 | * We ignore any "ec" data since it might be in the head or |
| 567 | * the tail, and we really don't need to deal with it. |
| 568 | * Note that buf->head[0].iov_len may indicate the available |
| 569 | * head buffer space rather than that actually occupied. |
| 570 | */ |
| 571 | movelen = min_t(unsigned int, buf->head[0].iov_len, buf->len); |
| 572 | movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip; |
| 573 | BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen > |
| 574 | buf->head[0].iov_len); |
| 575 | memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen); |
| 576 | buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip; |
| 577 | buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip; |
| 578 | |
Jeff Layton | cf4c024 | 2013-10-10 06:55:35 -0400 | [diff] [blame] | 579 | /* Trim off the trailing "extra count" and checksum blob */ |
| 580 | xdr_buf_trim(buf, ec + GSS_KRB5_TOK_HDR_LEN + tailskip); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 581 | return GSS_S_COMPLETE; |
| 582 | } |
| 583 | |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 584 | u32 |
| 585 | gss_wrap_kerberos(struct gss_ctx *gctx, int offset, |
| 586 | struct xdr_buf *buf, struct page **pages) |
| 587 | { |
| 588 | struct krb5_ctx *kctx = gctx->internal_ctx_id; |
| 589 | |
| 590 | switch (kctx->enctype) { |
| 591 | default: |
| 592 | BUG(); |
| 593 | case ENCTYPE_DES_CBC_RAW: |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 594 | case ENCTYPE_DES3_CBC_RAW: |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 595 | case ENCTYPE_ARCFOUR_HMAC: |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 596 | return gss_wrap_kerberos_v1(kctx, offset, buf, pages); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 597 | case ENCTYPE_AES128_CTS_HMAC_SHA1_96: |
| 598 | case ENCTYPE_AES256_CTS_HMAC_SHA1_96: |
| 599 | return gss_wrap_kerberos_v2(kctx, offset, buf, pages); |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | u32 |
| 604 | gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf) |
| 605 | { |
| 606 | struct krb5_ctx *kctx = gctx->internal_ctx_id; |
| 607 | |
| 608 | switch (kctx->enctype) { |
| 609 | default: |
| 610 | BUG(); |
| 611 | case ENCTYPE_DES_CBC_RAW: |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 612 | case ENCTYPE_DES3_CBC_RAW: |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 613 | case ENCTYPE_ARCFOUR_HMAC: |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 614 | return gss_unwrap_kerberos_v1(kctx, offset, buf); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 615 | case ENCTYPE_AES128_CTS_HMAC_SHA1_96: |
| 616 | case ENCTYPE_AES256_CTS_HMAC_SHA1_96: |
| 617 | return gss_unwrap_kerberos_v2(kctx, offset, buf); |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |