gss_krb5_seqnum.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_sync_skcipher *cipher;
  43. unsigned char plain[8];
  44. s32 code;
  45. dprintk("RPC: %s:\n", __func__);
  46. cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name, 0, 0);
  47. if (IS_ERR(cipher))
  48. return PTR_ERR(cipher);
  49. plain[0] = (unsigned char) ((seqnum >> 24) & 0xff);
  50. plain[1] = (unsigned char) ((seqnum >> 16) & 0xff);
  51. plain[2] = (unsigned char) ((seqnum >> 8) & 0xff);
  52. plain[3] = (unsigned char) ((seqnum >> 0) & 0xff);
  53. plain[4] = direction;
  54. plain[5] = direction;
  55. plain[6] = direction;
  56. plain[7] = direction;
  57. code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
  58. if (code)
  59. goto out;
  60. code = krb5_encrypt(cipher, cksum, plain, buf, 8);
  61. out:
  62. crypto_free_sync_skcipher(cipher);
  63. return code;
  64. }
  65. s32
  66. krb5_make_seq_num(struct krb5_ctx *kctx,
  67. struct crypto_sync_skcipher *key,
  68. int direction,
  69. u32 seqnum,
  70. unsigned char *cksum, unsigned char *buf)
  71. {
  72. unsigned char plain[8];
  73. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
  74. return krb5_make_rc4_seq_num(kctx, direction, seqnum,
  75. cksum, buf);
  76. plain[0] = (unsigned char) (seqnum & 0xff);
  77. plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
  78. plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
  79. plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
  80. plain[4] = direction;
  81. plain[5] = direction;
  82. plain[6] = direction;
  83. plain[7] = direction;
  84. return krb5_encrypt(key, cksum, plain, buf, 8);
  85. }
  86. static s32
  87. krb5_get_rc4_seq_num(struct krb5_ctx *kctx, unsigned char *cksum,
  88. unsigned char *buf, int *direction, s32 *seqnum)
  89. {
  90. struct crypto_sync_skcipher *cipher;
  91. unsigned char plain[8];
  92. s32 code;
  93. dprintk("RPC: %s:\n", __func__);
  94. cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name, 0, 0);
  95. if (IS_ERR(cipher))
  96. return PTR_ERR(cipher);
  97. code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
  98. if (code)
  99. goto out;
  100. code = krb5_decrypt(cipher, cksum, buf, plain, 8);
  101. if (code)
  102. goto out;
  103. if ((plain[4] != plain[5]) || (plain[4] != plain[6])
  104. || (plain[4] != plain[7])) {
  105. code = (s32)KG_BAD_SEQ;
  106. goto out;
  107. }
  108. *direction = plain[4];
  109. *seqnum = ((plain[0] << 24) | (plain[1] << 16) |
  110. (plain[2] << 8) | (plain[3]));
  111. out:
  112. crypto_free_sync_skcipher(cipher);
  113. return code;
  114. }
  115. s32
  116. krb5_get_seq_num(struct krb5_ctx *kctx,
  117. unsigned char *cksum,
  118. unsigned char *buf,
  119. int *direction, u32 *seqnum)
  120. {
  121. s32 code;
  122. unsigned char plain[8];
  123. struct crypto_sync_skcipher *key = kctx->seq;
  124. dprintk("RPC: krb5_get_seq_num:\n");
  125. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
  126. return krb5_get_rc4_seq_num(kctx, cksum, buf,
  127. direction, seqnum);
  128. if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
  129. return code;
  130. if ((plain[4] != plain[5]) || (plain[4] != plain[6]) ||
  131. (plain[4] != plain[7]))
  132. return (s32)KG_BAD_SEQ;
  133. *direction = plain[4];
  134. *seqnum = ((plain[0]) |
  135. (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
  136. return 0;
  137. }