gss_krb5_unseal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 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/slab.h>
  59. #include <linux/jiffies.h>
  60. #include <linux/sunrpc/gss_krb5.h>
  61. #include <linux/crypto.h>
  62. #ifdef RPC_DEBUG
  63. # define RPCDBG_FACILITY RPCDBG_AUTH
  64. #endif
  65. /* message_buffer is an input if toktype is MIC and an output if it is WRAP:
  66. * If toktype is MIC: read_token is a mic token, and message_buffer is the
  67. * data that the mic was supposedly taken over.
  68. * If toktype is WRAP: read_token is a wrap token, and message_buffer is used
  69. * to return the decrypted data.
  70. */
  71. /* XXX will need to change prototype and/or just split into a separate function
  72. * when we add privacy (because read_token will be in pages too). */
  73. u32
  74. krb5_read_token(struct krb5_ctx *ctx,
  75. struct xdr_netobj *read_token,
  76. struct xdr_buf *message_buffer,
  77. int *qop_state, int toktype)
  78. {
  79. int signalg;
  80. int sealalg;
  81. s32 checksum_type;
  82. struct xdr_netobj md5cksum = {.len = 0, .data = NULL};
  83. s32 now;
  84. int direction;
  85. s32 seqnum;
  86. unsigned char *ptr = (unsigned char *)read_token->data;
  87. int bodysize;
  88. u32 ret = GSS_S_DEFECTIVE_TOKEN;
  89. dprintk("RPC: krb5_read_token\n");
  90. if (g_verify_token_header(&ctx->mech_used, &bodysize, &ptr,
  91. read_token->len))
  92. goto out;
  93. if ((*ptr++ != ((toktype>>8)&0xff)) || (*ptr++ != (toktype&0xff)))
  94. goto out;
  95. /* XXX sanity-check bodysize?? */
  96. if (toktype == KG_TOK_WRAP_MSG) {
  97. /* XXX gone */
  98. goto out;
  99. }
  100. /* get the sign and seal algorithms */
  101. signalg = ptr[0] + (ptr[1] << 8);
  102. sealalg = ptr[2] + (ptr[3] << 8);
  103. /* Sanity checks */
  104. if ((ptr[4] != 0xff) || (ptr[5] != 0xff))
  105. goto out;
  106. if (((toktype != KG_TOK_WRAP_MSG) && (sealalg != 0xffff)) ||
  107. ((toktype == KG_TOK_WRAP_MSG) && (sealalg == 0xffff)))
  108. goto out;
  109. /* in the current spec, there is only one valid seal algorithm per
  110. key type, so a simple comparison is ok */
  111. if ((toktype == KG_TOK_WRAP_MSG) && !(sealalg == ctx->sealalg))
  112. goto out;
  113. /* there are several mappings of seal algorithms to sign algorithms,
  114. but few enough that we can try them all. */
  115. if ((ctx->sealalg == SEAL_ALG_NONE && signalg > 1) ||
  116. (ctx->sealalg == SEAL_ALG_1 && signalg != SGN_ALG_3) ||
  117. (ctx->sealalg == SEAL_ALG_DES3KD &&
  118. signalg != SGN_ALG_HMAC_SHA1_DES3_KD))
  119. goto out;
  120. /* compute the checksum of the message */
  121. /* initialize the the cksum */
  122. switch (signalg) {
  123. case SGN_ALG_DES_MAC_MD5:
  124. checksum_type = CKSUMTYPE_RSA_MD5;
  125. break;
  126. default:
  127. ret = GSS_S_DEFECTIVE_TOKEN;
  128. goto out;
  129. }
  130. switch (signalg) {
  131. case SGN_ALG_DES_MAC_MD5:
  132. ret = make_checksum(checksum_type, ptr - 2, 8,
  133. message_buffer, &md5cksum);
  134. if (ret)
  135. goto out;
  136. ret = krb5_encrypt(ctx->seq, NULL, md5cksum.data,
  137. md5cksum.data, 16);
  138. if (ret)
  139. goto out;
  140. if (memcmp(md5cksum.data + 8, ptr + 14, 8)) {
  141. ret = GSS_S_BAD_SIG;
  142. goto out;
  143. }
  144. break;
  145. default:
  146. ret = GSS_S_DEFECTIVE_TOKEN;
  147. goto out;
  148. }
  149. /* it got through unscathed. Make sure the context is unexpired */
  150. if (qop_state)
  151. *qop_state = GSS_C_QOP_DEFAULT;
  152. now = get_seconds();
  153. ret = GSS_S_CONTEXT_EXPIRED;
  154. if (now > ctx->endtime)
  155. goto out;
  156. /* do sequencing checks */
  157. ret = GSS_S_BAD_SIG;
  158. if ((ret = krb5_get_seq_num(ctx->seq, ptr + 14, ptr + 6, &direction,
  159. &seqnum)))
  160. goto out;
  161. if ((ctx->initiate && direction != 0xff) ||
  162. (!ctx->initiate && direction != 0))
  163. goto out;
  164. ret = GSS_S_COMPLETE;
  165. out:
  166. if (md5cksum.data) kfree(md5cksum.data);
  167. return ret;
  168. }