gss_krb5_seqnum.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * linux/net/sunrpc/gss_krb5_seqnum.c
  3. *
  4. * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/util_seqnum.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. #include <crypto/skcipher.h>
  33. #include <linux/types.h>
  34. #include <linux/sunrpc/gss_krb5.h>
  35. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  36. # define RPCDBG_FACILITY RPCDBG_AUTH
  37. #endif
  38. static s32
  39. krb5_make_rc4_seq_num(struct krb5_ctx *kctx, int direction, s32 seqnum,
  40. unsigned char *cksum, unsigned char *buf)
  41. {
  42. struct crypto_skcipher *cipher;
  43. unsigned char *plain;
  44. s32 code;
  45. dprintk("RPC: %s:\n", __func__);
  46. cipher = crypto_alloc_skcipher(kctx->gk5e->encrypt_name, 0,
  47. CRYPTO_ALG_ASYNC);
  48. if (IS_ERR(cipher))
  49. return PTR_ERR(cipher);
  50. plain = kmalloc(8, GFP_NOFS);
  51. if (!plain)
  52. return -ENOMEM;
  53. plain[0] = (unsigned char) ((seqnum >> 24) & 0xff);
  54. plain[1] = (unsigned char) ((seqnum >> 16) & 0xff);
  55. plain[2] = (unsigned char) ((seqnum >> 8) & 0xff);
  56. plain[3] = (unsigned char) ((seqnum >> 0) & 0xff);
  57. plain[4] = direction;
  58. plain[5] = direction;
  59. plain[6] = direction;
  60. plain[7] = direction;
  61. code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
  62. if (code)
  63. goto out;
  64. code = krb5_encrypt(cipher, cksum, plain, buf, 8);
  65. out:
  66. crypto_free_skcipher(cipher);
  67. kfree(plain);
  68. return code;
  69. }
  70. s32
  71. krb5_make_seq_num(struct krb5_ctx *kctx,
  72. struct crypto_skcipher *key,
  73. int direction,
  74. u32 seqnum,
  75. unsigned char *cksum, unsigned char *buf)
  76. {
  77. unsigned char *plain;
  78. s32 code;
  79. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
  80. return krb5_make_rc4_seq_num(kctx, direction, seqnum,
  81. cksum, buf);
  82. plain = kmalloc(8, GFP_NOFS);
  83. if (!plain)
  84. return -ENOMEM;
  85. plain[0] = (unsigned char) (seqnum & 0xff);
  86. plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
  87. plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
  88. plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
  89. plain[4] = direction;
  90. plain[5] = direction;
  91. plain[6] = direction;
  92. plain[7] = direction;
  93. code = krb5_encrypt(key, cksum, plain, buf, 8);
  94. kfree(plain);
  95. return code;
  96. }
  97. static s32
  98. krb5_get_rc4_seq_num(struct krb5_ctx *kctx, unsigned char *cksum,
  99. unsigned char *buf, int *direction, s32 *seqnum)
  100. {
  101. struct crypto_skcipher *cipher;
  102. unsigned char *plain;
  103. s32 code;
  104. dprintk("RPC: %s:\n", __func__);
  105. cipher = crypto_alloc_skcipher(kctx->gk5e->encrypt_name, 0,
  106. CRYPTO_ALG_ASYNC);
  107. if (IS_ERR(cipher))
  108. return PTR_ERR(cipher);
  109. code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
  110. if (code)
  111. goto out;
  112. plain = kmalloc(8, GFP_NOFS);
  113. if (!plain) {
  114. code = -ENOMEM;
  115. goto out;
  116. }
  117. code = krb5_decrypt(cipher, cksum, buf, plain, 8);
  118. if (code)
  119. goto out_plain;
  120. if ((plain[4] != plain[5]) || (plain[4] != plain[6])
  121. || (plain[4] != plain[7])) {
  122. code = (s32)KG_BAD_SEQ;
  123. goto out_plain;
  124. }
  125. *direction = plain[4];
  126. *seqnum = ((plain[0] << 24) | (plain[1] << 16) |
  127. (plain[2] << 8) | (plain[3]));
  128. out_plain:
  129. kfree(plain);
  130. out:
  131. crypto_free_skcipher(cipher);
  132. return code;
  133. }
  134. s32
  135. krb5_get_seq_num(struct krb5_ctx *kctx,
  136. unsigned char *cksum,
  137. unsigned char *buf,
  138. int *direction, u32 *seqnum)
  139. {
  140. s32 code;
  141. struct crypto_skcipher *key = kctx->seq;
  142. unsigned char *plain;
  143. dprintk("RPC: krb5_get_seq_num:\n");
  144. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
  145. return krb5_get_rc4_seq_num(kctx, cksum, buf,
  146. direction, seqnum);
  147. plain = kmalloc(8, GFP_NOFS);
  148. if (!plain)
  149. return -ENOMEM;
  150. if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
  151. goto out;
  152. if ((plain[4] != plain[5]) || (plain[4] != plain[6]) ||
  153. (plain[4] != plain[7])) {
  154. code = (s32)KG_BAD_SEQ;
  155. goto out;
  156. }
  157. *direction = plain[4];
  158. *seqnum = ((plain[0]) |
  159. (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
  160. out:
  161. kfree(plain);
  162. return code;
  163. }