gss_spkm3_seal.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * linux/net/sunrpc/gss_spkm3_seal.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/random.h>
  40. #include <linux/crypto.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/scatterlist.h>
  43. #include <linux/sunrpc/xdr.h>
  44. #ifdef RPC_DEBUG
  45. # define RPCDBG_FACILITY RPCDBG_AUTH
  46. #endif
  47. const struct xdr_netobj hmac_md5_oid = { 8, "\x2B\x06\x01\x05\x05\x08\x01\x01"};
  48. const struct xdr_netobj cast5_cbc_oid = {9, "\x2A\x86\x48\x86\xF6\x7D\x07\x42\x0A"};
  49. /*
  50. * spkm3_make_token()
  51. *
  52. * Only SPKM_MIC_TOK with md5 intg-alg is supported
  53. */
  54. u32
  55. spkm3_make_token(struct spkm3_ctx *ctx,
  56. struct xdr_buf * text, struct xdr_netobj * token,
  57. int toktype)
  58. {
  59. s32 checksum_type;
  60. char tokhdrbuf[25];
  61. char cksumdata[16];
  62. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  63. struct xdr_netobj mic_hdr = {.len = 0, .data = tokhdrbuf};
  64. int tokenlen = 0;
  65. unsigned char *ptr;
  66. s32 now;
  67. int ctxelen = 0, ctxzbit = 0;
  68. int md5elen = 0, md5zbit = 0;
  69. now = jiffies;
  70. if (ctx->ctx_id.len != 16) {
  71. dprintk("RPC: spkm3_make_token BAD ctx_id.len %d\n",
  72. ctx->ctx_id.len);
  73. goto out_err;
  74. }
  75. if (!g_OID_equal(&ctx->intg_alg, &hmac_md5_oid)) {
  76. dprintk("RPC: gss_spkm3_seal: unsupported I-ALG "
  77. "algorithm. only support hmac-md5 I-ALG.\n");
  78. goto out_err;
  79. } else
  80. checksum_type = CKSUMTYPE_HMAC_MD5;
  81. if (!g_OID_equal(&ctx->conf_alg, &cast5_cbc_oid)) {
  82. dprintk("RPC: gss_spkm3_seal: unsupported C-ALG "
  83. "algorithm\n");
  84. goto out_err;
  85. }
  86. if (toktype == SPKM_MIC_TOK) {
  87. /* Calculate checksum over the mic-header */
  88. asn1_bitstring_len(&ctx->ctx_id, &ctxelen, &ctxzbit);
  89. spkm3_mic_header(&mic_hdr.data, &mic_hdr.len, ctx->ctx_id.data,
  90. ctxelen, ctxzbit);
  91. if (make_spkm3_checksum(checksum_type, &ctx->derived_integ_key,
  92. (char *)mic_hdr.data, mic_hdr.len,
  93. text, 0, &md5cksum))
  94. goto out_err;
  95. asn1_bitstring_len(&md5cksum, &md5elen, &md5zbit);
  96. tokenlen = 10 + ctxelen + 1 + md5elen + 1;
  97. /* Create token header using generic routines */
  98. token->len = g_token_size(&ctx->mech_used, tokenlen);
  99. ptr = token->data;
  100. g_make_token_header(&ctx->mech_used, tokenlen, &ptr);
  101. spkm3_make_mic_token(&ptr, tokenlen, &mic_hdr, &md5cksum, md5elen, md5zbit);
  102. } else if (toktype == SPKM_WRAP_TOK) { /* Not Supported */
  103. dprintk("RPC: gss_spkm3_seal: SPKM_WRAP_TOK "
  104. "not supported\n");
  105. goto out_err;
  106. }
  107. /* XXX need to implement sequence numbers, and ctx->expired */
  108. return GSS_S_COMPLETE;
  109. out_err:
  110. if (md5cksum.data)
  111. kfree(md5cksum.data);
  112. token->data = NULL;
  113. token->len = 0;
  114. return GSS_S_FAILURE;
  115. }
  116. static int
  117. spkm3_checksummer(struct scatterlist *sg, void *data)
  118. {
  119. struct hash_desc *desc = data;
  120. return crypto_hash_update(desc, sg, sg->length);
  121. }
  122. /* checksum the plaintext data and hdrlen bytes of the token header */
  123. s32
  124. make_spkm3_checksum(s32 cksumtype, struct xdr_netobj *key, char *header,
  125. unsigned int hdrlen, struct xdr_buf *body,
  126. unsigned int body_offset, struct xdr_netobj *cksum)
  127. {
  128. char *cksumname;
  129. struct hash_desc desc; /* XXX add to ctx? */
  130. struct scatterlist sg[1];
  131. int err;
  132. switch (cksumtype) {
  133. case CKSUMTYPE_HMAC_MD5:
  134. cksumname = "md5";
  135. break;
  136. default:
  137. dprintk("RPC: spkm3_make_checksum:"
  138. " unsupported checksum %d", cksumtype);
  139. return GSS_S_FAILURE;
  140. }
  141. if (key->data == NULL || key->len <= 0) return GSS_S_FAILURE;
  142. desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC);
  143. if (IS_ERR(desc.tfm))
  144. return GSS_S_FAILURE;
  145. cksum->len = crypto_hash_digestsize(desc.tfm);
  146. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  147. err = crypto_hash_setkey(desc.tfm, key->data, key->len);
  148. if (err)
  149. goto out;
  150. sg_set_buf(sg, header, hdrlen);
  151. crypto_hash_update(&desc, sg, 1);
  152. xdr_process_buf(body, body_offset, body->len - body_offset,
  153. spkm3_checksummer, &desc);
  154. crypto_hash_final(&desc, cksum->data);
  155. out:
  156. crypto_free_hash(desc.tfm);
  157. return err ? GSS_S_FAILURE : 0;
  158. }
  159. EXPORT_SYMBOL(make_spkm3_checksum);