gss_spkm3_mech.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * linux/net/sunrpc/gss_spkm3_mech.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. * J. Bruce Fields <bfields@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include <linux/err.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/sunrpc/auth.h>
  42. #include <linux/in.h>
  43. #include <linux/sunrpc/svcauth_gss.h>
  44. #include <linux/sunrpc/gss_spkm3.h>
  45. #include <linux/sunrpc/xdr.h>
  46. #include <linux/crypto.h>
  47. #ifdef RPC_DEBUG
  48. # define RPCDBG_FACILITY RPCDBG_AUTH
  49. #endif
  50. static const void *
  51. simple_get_bytes(const void *p, const void *end, void *res, int len)
  52. {
  53. const void *q = (const void *)((const char *)p + len);
  54. if (unlikely(q > end || q < p))
  55. return ERR_PTR(-EFAULT);
  56. memcpy(res, p, len);
  57. return q;
  58. }
  59. static const void *
  60. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
  61. {
  62. const void *q;
  63. unsigned int len;
  64. p = simple_get_bytes(p, end, &len, sizeof(len));
  65. if (IS_ERR(p))
  66. return p;
  67. res->len = len;
  68. if (len == 0) {
  69. res->data = NULL;
  70. return p;
  71. }
  72. q = (const void *)((const char *)p + len);
  73. if (unlikely(q > end || q < p))
  74. return ERR_PTR(-EFAULT);
  75. res->data = kmemdup(p, len, GFP_NOFS);
  76. if (unlikely(res->data == NULL))
  77. return ERR_PTR(-ENOMEM);
  78. return q;
  79. }
  80. static int
  81. gss_import_sec_context_spkm3(const void *p, size_t len,
  82. struct gss_ctx *ctx_id,
  83. gfp_t gfp_mask)
  84. {
  85. const void *end = (const void *)((const char *)p + len);
  86. struct spkm3_ctx *ctx;
  87. int version;
  88. if (!(ctx = kzalloc(sizeof(*ctx), gfp_mask)))
  89. goto out_err;
  90. p = simple_get_bytes(p, end, &version, sizeof(version));
  91. if (IS_ERR(p))
  92. goto out_err_free_ctx;
  93. if (version != 1) {
  94. dprintk("RPC: unknown spkm3 token format: "
  95. "obsolete nfs-utils?\n");
  96. goto out_err_free_ctx;
  97. }
  98. p = simple_get_netobj(p, end, &ctx->ctx_id);
  99. if (IS_ERR(p))
  100. goto out_err_free_ctx;
  101. p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
  102. if (IS_ERR(p))
  103. goto out_err_free_ctx_id;
  104. p = simple_get_netobj(p, end, &ctx->mech_used);
  105. if (IS_ERR(p))
  106. goto out_err_free_ctx_id;
  107. p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags));
  108. if (IS_ERR(p))
  109. goto out_err_free_mech;
  110. p = simple_get_netobj(p, end, &ctx->conf_alg);
  111. if (IS_ERR(p))
  112. goto out_err_free_mech;
  113. p = simple_get_netobj(p, end, &ctx->derived_conf_key);
  114. if (IS_ERR(p))
  115. goto out_err_free_conf_alg;
  116. p = simple_get_netobj(p, end, &ctx->intg_alg);
  117. if (IS_ERR(p))
  118. goto out_err_free_conf_key;
  119. p = simple_get_netobj(p, end, &ctx->derived_integ_key);
  120. if (IS_ERR(p))
  121. goto out_err_free_intg_alg;
  122. if (p != end)
  123. goto out_err_free_intg_key;
  124. ctx_id->internal_ctx_id = ctx;
  125. dprintk("RPC: Successfully imported new spkm context.\n");
  126. return 0;
  127. out_err_free_intg_key:
  128. kfree(ctx->derived_integ_key.data);
  129. out_err_free_intg_alg:
  130. kfree(ctx->intg_alg.data);
  131. out_err_free_conf_key:
  132. kfree(ctx->derived_conf_key.data);
  133. out_err_free_conf_alg:
  134. kfree(ctx->conf_alg.data);
  135. out_err_free_mech:
  136. kfree(ctx->mech_used.data);
  137. out_err_free_ctx_id:
  138. kfree(ctx->ctx_id.data);
  139. out_err_free_ctx:
  140. kfree(ctx);
  141. out_err:
  142. return PTR_ERR(p);
  143. }
  144. static void
  145. gss_delete_sec_context_spkm3(void *internal_ctx)
  146. {
  147. struct spkm3_ctx *sctx = internal_ctx;
  148. kfree(sctx->derived_integ_key.data);
  149. kfree(sctx->intg_alg.data);
  150. kfree(sctx->derived_conf_key.data);
  151. kfree(sctx->conf_alg.data);
  152. kfree(sctx->mech_used.data);
  153. kfree(sctx->ctx_id.data);
  154. kfree(sctx);
  155. }
  156. static u32
  157. gss_verify_mic_spkm3(struct gss_ctx *ctx,
  158. struct xdr_buf *signbuf,
  159. struct xdr_netobj *checksum)
  160. {
  161. u32 maj_stat = 0;
  162. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  163. maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK);
  164. dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
  165. return maj_stat;
  166. }
  167. static u32
  168. gss_get_mic_spkm3(struct gss_ctx *ctx,
  169. struct xdr_buf *message_buffer,
  170. struct xdr_netobj *message_token)
  171. {
  172. u32 err = 0;
  173. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  174. err = spkm3_make_token(sctx, message_buffer,
  175. message_token, SPKM_MIC_TOK);
  176. dprintk("RPC: gss_get_mic_spkm3 returning %d\n", err);
  177. return err;
  178. }
  179. static const struct gss_api_ops gss_spkm3_ops = {
  180. .gss_import_sec_context = gss_import_sec_context_spkm3,
  181. .gss_get_mic = gss_get_mic_spkm3,
  182. .gss_verify_mic = gss_verify_mic_spkm3,
  183. .gss_delete_sec_context = gss_delete_sec_context_spkm3,
  184. };
  185. static struct pf_desc gss_spkm3_pfs[] = {
  186. {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"},
  187. {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
  188. };
  189. static struct gss_api_mech gss_spkm3_mech = {
  190. .gm_name = "spkm3",
  191. .gm_owner = THIS_MODULE,
  192. .gm_oid = {7, "\053\006\001\005\005\001\003"},
  193. .gm_ops = &gss_spkm3_ops,
  194. .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
  195. .gm_pfs = gss_spkm3_pfs,
  196. };
  197. static int __init init_spkm3_module(void)
  198. {
  199. int status;
  200. status = gss_mech_register(&gss_spkm3_mech);
  201. if (status)
  202. printk("Failed to register spkm3 gss mechanism!\n");
  203. return status;
  204. }
  205. static void __exit cleanup_spkm3_module(void)
  206. {
  207. gss_mech_unregister(&gss_spkm3_mech);
  208. }
  209. MODULE_LICENSE("GPL");
  210. module_init(init_spkm3_module);
  211. module_exit(cleanup_spkm3_module);