Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/gss_krb5_unseal.c |
| 3 | * |
| 4 | * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5unseal.c |
| 5 | * |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 6 | * Copyright (c) 2000-2008 The Regents of the University of Michigan. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * All rights reserved. |
| 8 | * |
| 9 | * Andy Adamson <andros@umich.edu> |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Copyright 1993 by OpenVision Technologies, Inc. |
| 14 | * |
| 15 | * Permission to use, copy, modify, distribute, and sell this software |
| 16 | * and its documentation for any purpose is hereby granted without fee, |
| 17 | * provided that the above copyright notice appears in all copies and |
| 18 | * that both that copyright notice and this permission notice appear in |
| 19 | * supporting documentation, and that the name of OpenVision not be used |
| 20 | * in advertising or publicity pertaining to distribution of the software |
| 21 | * without specific, written prior permission. OpenVision makes no |
| 22 | * representations about the suitability of this software for any |
| 23 | * purpose. It is provided "as is" without express or implied warranty. |
| 24 | * |
| 25 | * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 26 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 27 | * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 28 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 29 | * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 30 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 31 | * PERFORMANCE OF THIS SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * Copyright (C) 1998 by the FundsXpress, INC. |
| 36 | * |
| 37 | * All rights reserved. |
| 38 | * |
| 39 | * Export of this software from the United States of America may require |
| 40 | * a specific license from the United States Government. It is the |
| 41 | * responsibility of any person or organization contemplating export to |
| 42 | * obtain such a license before exporting. |
| 43 | * |
| 44 | * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and |
| 45 | * distribute this software and its documentation for any purpose and |
| 46 | * without fee is hereby granted, provided that the above copyright |
| 47 | * notice appear in all copies and that both that copyright notice and |
| 48 | * this permission notice appear in supporting documentation, and that |
| 49 | * the name of FundsXpress. not be used in advertising or publicity pertaining |
| 50 | * to distribution of the software without specific, written prior |
| 51 | * permission. FundsXpress makes no representations about the suitability of |
| 52 | * this software for any purpose. It is provided "as is" without express |
| 53 | * or implied warranty. |
| 54 | * |
| 55 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 56 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 57 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 58 | */ |
| 59 | |
| 60 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/jiffies.h> |
| 62 | #include <linux/sunrpc/gss_krb5.h> |
| 63 | #include <linux/crypto.h> |
| 64 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame^] | 65 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 67 | #endif |
| 68 | |
| 69 | |
J. Bruce Fields | bfa9151 | 2005-10-13 16:55:08 -0400 | [diff] [blame] | 70 | /* read_token is a mic token, and message_buffer is the data that the mic was |
| 71 | * supposedly taken over. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 73 | static u32 |
| 74 | gss_verify_mic_v1(struct krb5_ctx *ctx, |
J. Bruce Fields | a0857d0 | 2005-10-13 16:55:23 -0400 | [diff] [blame] | 75 | struct xdr_buf *message_buffer, struct xdr_netobj *read_token) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | int signalg; |
| 78 | int sealalg; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 79 | char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; |
| 80 | struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), |
| 81 | .data = cksumdata}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | s32 now; |
| 83 | int direction; |
Kevin Coffman | 5743d65 | 2008-03-31 10:31:33 -0400 | [diff] [blame] | 84 | u32 seqnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | unsigned char *ptr = (unsigned char *)read_token->data; |
| 86 | int bodysize; |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 87 | u8 *cksumkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 89 | dprintk("RPC: krb5_read_token\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | if (g_verify_token_header(&ctx->mech_used, &bodysize, &ptr, |
| 92 | read_token->len)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 93 | return GSS_S_DEFECTIVE_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 95 | if ((ptr[0] != ((KG_TOK_MIC_MSG >> 8) & 0xff)) || |
| 96 | (ptr[1] != (KG_TOK_MIC_MSG & 0xff))) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 97 | return GSS_S_DEFECTIVE_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* XXX sanity-check bodysize?? */ |
| 100 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 101 | signalg = ptr[2] + (ptr[3] << 8); |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 102 | if (signalg != ctx->gk5e->signalg) |
J. Bruce Fields | 94efa93 | 2006-12-04 20:22:42 -0500 | [diff] [blame] | 103 | return GSS_S_DEFECTIVE_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 105 | sealalg = ptr[4] + (ptr[5] << 8); |
J. Bruce Fields | 94efa93 | 2006-12-04 20:22:42 -0500 | [diff] [blame] | 106 | if (sealalg != SEAL_ALG_NONE) |
| 107 | return GSS_S_DEFECTIVE_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Kevin Coffman | d00953a | 2008-04-30 12:45:53 -0400 | [diff] [blame] | 109 | if ((ptr[6] != 0xff) || (ptr[7] != 0xff)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 110 | return GSS_S_DEFECTIVE_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 112 | if (ctx->gk5e->keyed_cksum) |
| 113 | cksumkey = ctx->cksum; |
| 114 | else |
| 115 | cksumkey = NULL; |
| 116 | |
| 117 | if (make_checksum(ctx, ptr, 8, message_buffer, 0, |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 118 | cksumkey, KG_USAGE_SIGN, &md5cksum)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 119 | return GSS_S_FAILURE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 121 | if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN, |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 122 | ctx->gk5e->cksumlength)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 123 | return GSS_S_BAD_SIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | /* it got through unscathed. Make sure the context is unexpired */ |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | now = get_seconds(); |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (now > ctx->endtime) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 130 | return GSS_S_CONTEXT_EXPIRED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* do sequencing checks */ |
| 133 | |
Kevin Coffman | 1dbd902 | 2010-03-17 13:03:04 -0400 | [diff] [blame] | 134 | if (krb5_get_seq_num(ctx, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8, |
| 135 | &direction, &seqnum)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 136 | return GSS_S_FAILURE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | if ((ctx->initiate && direction != 0xff) || |
| 139 | (!ctx->initiate && direction != 0)) |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 140 | return GSS_S_BAD_SIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
J. Bruce Fields | 39a21dd | 2006-12-04 20:22:39 -0500 | [diff] [blame] | 142 | return GSS_S_COMPLETE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 144 | |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 145 | static u32 |
| 146 | gss_verify_mic_v2(struct krb5_ctx *ctx, |
| 147 | struct xdr_buf *message_buffer, struct xdr_netobj *read_token) |
| 148 | { |
| 149 | char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; |
| 150 | struct xdr_netobj cksumobj = {.len = sizeof(cksumdata), |
| 151 | .data = cksumdata}; |
| 152 | s32 now; |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 153 | u8 *ptr = read_token->data; |
| 154 | u8 *cksumkey; |
| 155 | u8 flags; |
| 156 | int i; |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 157 | unsigned int cksum_usage; |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 158 | |
| 159 | dprintk("RPC: %s\n", __func__); |
| 160 | |
| 161 | if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_MIC) |
| 162 | return GSS_S_DEFECTIVE_TOKEN; |
| 163 | |
| 164 | flags = ptr[2]; |
| 165 | if ((!ctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) || |
| 166 | (ctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR))) |
| 167 | return GSS_S_BAD_SIG; |
| 168 | |
| 169 | if (flags & KG2_TOKEN_FLAG_SEALED) { |
| 170 | dprintk("%s: token has unexpected sealed flag\n", __func__); |
| 171 | return GSS_S_FAILURE; |
| 172 | } |
| 173 | |
| 174 | for (i = 3; i < 8; i++) |
| 175 | if (ptr[i] != 0xff) |
| 176 | return GSS_S_DEFECTIVE_TOKEN; |
| 177 | |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 178 | if (ctx->initiate) { |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 179 | cksumkey = ctx->acceptor_sign; |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 180 | cksum_usage = KG_USAGE_ACCEPTOR_SIGN; |
| 181 | } else { |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 182 | cksumkey = ctx->initiator_sign; |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 183 | cksum_usage = KG_USAGE_INITIATOR_SIGN; |
| 184 | } |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 185 | |
| 186 | if (make_checksum_v2(ctx, ptr, GSS_KRB5_TOK_HDR_LEN, message_buffer, 0, |
Kevin Coffman | 8b23707 | 2010-03-17 13:03:02 -0400 | [diff] [blame] | 187 | cksumkey, cksum_usage, &cksumobj)) |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 188 | return GSS_S_FAILURE; |
| 189 | |
| 190 | if (memcmp(cksumobj.data, ptr + GSS_KRB5_TOK_HDR_LEN, |
| 191 | ctx->gk5e->cksumlength)) |
| 192 | return GSS_S_BAD_SIG; |
| 193 | |
| 194 | /* it got through unscathed. Make sure the context is unexpired */ |
| 195 | now = get_seconds(); |
| 196 | if (now > ctx->endtime) |
| 197 | return GSS_S_CONTEXT_EXPIRED; |
| 198 | |
J. Bruce Fields | 5d6baef | 2013-10-09 15:59:29 -0400 | [diff] [blame] | 199 | /* |
| 200 | * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss |
| 201 | * doesn't want it checked; see page 6 of rfc 2203. |
| 202 | */ |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 203 | |
| 204 | return GSS_S_COMPLETE; |
| 205 | } |
| 206 | |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 207 | u32 |
| 208 | gss_verify_mic_kerberos(struct gss_ctx *gss_ctx, |
| 209 | struct xdr_buf *message_buffer, |
| 210 | struct xdr_netobj *read_token) |
| 211 | { |
| 212 | struct krb5_ctx *ctx = gss_ctx->internal_ctx_id; |
| 213 | |
| 214 | switch (ctx->enctype) { |
| 215 | default: |
| 216 | BUG(); |
| 217 | case ENCTYPE_DES_CBC_RAW: |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 218 | case ENCTYPE_DES3_CBC_RAW: |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 219 | case ENCTYPE_ARCFOUR_HMAC: |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 220 | return gss_verify_mic_v1(ctx, message_buffer, read_token); |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame] | 221 | case ENCTYPE_AES128_CTS_HMAC_SHA1_96: |
| 222 | case ENCTYPE_AES256_CTS_HMAC_SHA1_96: |
| 223 | return gss_verify_mic_v2(ctx, message_buffer, read_token); |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |