gss_krb5_unseal.c 6.7 KB

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