gss_spkm3_unseal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * linux/net/sunrpc/gss_spkm3_unseal.c
  3. *
  4. * Copyright (c) 2003 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Andy Adamson <andros@umich.edu>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include <linux/types.h>
  36. #include <linux/slab.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/sunrpc/gss_spkm3.h>
  39. #include <linux/crypto.h>
  40. #ifdef RPC_DEBUG
  41. # define RPCDBG_FACILITY RPCDBG_AUTH
  42. #endif
  43. /*
  44. * spkm3_read_token()
  45. *
  46. * only SPKM_MIC_TOK with md5 intg-alg is supported
  47. */
  48. u32
  49. spkm3_read_token(struct spkm3_ctx *ctx,
  50. struct xdr_netobj *read_token, /* checksum */
  51. struct xdr_buf *message_buffer, /* signbuf */
  52. int toktype)
  53. {
  54. s32 checksum_type;
  55. s32 code;
  56. struct xdr_netobj wire_cksum = {.len =0, .data = NULL};
  57. char cksumdata[16];
  58. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  59. unsigned char *ptr = (unsigned char *)read_token->data;
  60. unsigned char *cksum;
  61. int bodysize, md5elen;
  62. int mic_hdrlen;
  63. u32 ret = GSS_S_DEFECTIVE_TOKEN;
  64. if (g_verify_token_header((struct xdr_netobj *) &ctx->mech_used,
  65. &bodysize, &ptr, read_token->len))
  66. goto out;
  67. /* decode the token */
  68. if (toktype != SPKM_MIC_TOK) {
  69. dprintk("RPC: BAD SPKM3 token type: %d\n", toktype);
  70. goto out;
  71. }
  72. if ((ret = spkm3_verify_mic_token(&ptr, &mic_hdrlen, &cksum)))
  73. goto out;
  74. if (*cksum++ != 0x03) {
  75. dprintk("RPC: spkm3_read_token BAD checksum type\n");
  76. goto out;
  77. }
  78. md5elen = *cksum++;
  79. cksum++; /* move past the zbit */
  80. if (!decode_asn1_bitstring(&wire_cksum, cksum, md5elen - 1, 16))
  81. goto out;
  82. /* HARD CODED FOR MD5 */
  83. /* compute the checksum of the message.
  84. * ptr + 2 = start of header piece of checksum
  85. * mic_hdrlen + 2 = length of header piece of checksum
  86. */
  87. ret = GSS_S_DEFECTIVE_TOKEN;
  88. if (!g_OID_equal(&ctx->intg_alg, &hmac_md5_oid)) {
  89. dprintk("RPC: gss_spkm3_seal: unsupported I-ALG "
  90. "algorithm\n");
  91. goto out;
  92. }
  93. checksum_type = CKSUMTYPE_HMAC_MD5;
  94. code = make_spkm3_checksum(checksum_type,
  95. &ctx->derived_integ_key, ptr + 2, mic_hdrlen + 2,
  96. message_buffer, 0, &md5cksum);
  97. if (code)
  98. goto out;
  99. ret = GSS_S_BAD_SIG;
  100. code = memcmp(md5cksum.data, wire_cksum.data, wire_cksum.len);
  101. if (code) {
  102. dprintk("RPC: bad MIC checksum\n");
  103. goto out;
  104. }
  105. /* XXX: need to add expiration and sequencing */
  106. ret = GSS_S_COMPLETE;
  107. out:
  108. kfree(wire_cksum.data);
  109. return ret;
  110. }