gss_spkm3_mech.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. {
  84. const void *end = (const void *)((const char *)p + len);
  85. struct spkm3_ctx *ctx;
  86. int version;
  87. if (!(ctx = kzalloc(sizeof(*ctx), GFP_NOFS)))
  88. goto out_err;
  89. p = simple_get_bytes(p, end, &version, sizeof(version));
  90. if (IS_ERR(p))
  91. goto out_err_free_ctx;
  92. if (version != 1) {
  93. dprintk("RPC: unknown spkm3 token format: "
  94. "obsolete nfs-utils?\n");
  95. goto out_err_free_ctx;
  96. }
  97. p = simple_get_netobj(p, end, &ctx->ctx_id);
  98. if (IS_ERR(p))
  99. goto out_err_free_ctx;
  100. p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
  101. if (IS_ERR(p))
  102. goto out_err_free_ctx_id;
  103. p = simple_get_netobj(p, end, &ctx->mech_used);
  104. if (IS_ERR(p))
  105. goto out_err_free_ctx_id;
  106. p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags));
  107. if (IS_ERR(p))
  108. goto out_err_free_mech;
  109. p = simple_get_netobj(p, end, &ctx->conf_alg);
  110. if (IS_ERR(p))
  111. goto out_err_free_mech;
  112. p = simple_get_netobj(p, end, &ctx->derived_conf_key);
  113. if (IS_ERR(p))
  114. goto out_err_free_conf_alg;
  115. p = simple_get_netobj(p, end, &ctx->intg_alg);
  116. if (IS_ERR(p))
  117. goto out_err_free_conf_key;
  118. p = simple_get_netobj(p, end, &ctx->derived_integ_key);
  119. if (IS_ERR(p))
  120. goto out_err_free_intg_alg;
  121. if (p != end)
  122. goto out_err_free_intg_key;
  123. ctx_id->internal_ctx_id = ctx;
  124. dprintk("RPC: Successfully imported new spkm context.\n");
  125. return 0;
  126. out_err_free_intg_key:
  127. kfree(ctx->derived_integ_key.data);
  128. out_err_free_intg_alg:
  129. kfree(ctx->intg_alg.data);
  130. out_err_free_conf_key:
  131. kfree(ctx->derived_conf_key.data);
  132. out_err_free_conf_alg:
  133. kfree(ctx->conf_alg.data);
  134. out_err_free_mech:
  135. kfree(ctx->mech_used.data);
  136. out_err_free_ctx_id:
  137. kfree(ctx->ctx_id.data);
  138. out_err_free_ctx:
  139. kfree(ctx);
  140. out_err:
  141. return PTR_ERR(p);
  142. }
  143. static void
  144. gss_delete_sec_context_spkm3(void *internal_ctx)
  145. {
  146. struct spkm3_ctx *sctx = internal_ctx;
  147. kfree(sctx->derived_integ_key.data);
  148. kfree(sctx->intg_alg.data);
  149. kfree(sctx->derived_conf_key.data);
  150. kfree(sctx->conf_alg.data);
  151. kfree(sctx->mech_used.data);
  152. kfree(sctx->ctx_id.data);
  153. kfree(sctx);
  154. }
  155. static u32
  156. gss_verify_mic_spkm3(struct gss_ctx *ctx,
  157. struct xdr_buf *signbuf,
  158. struct xdr_netobj *checksum)
  159. {
  160. u32 maj_stat = 0;
  161. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  162. maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK);
  163. dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
  164. return maj_stat;
  165. }
  166. static u32
  167. gss_get_mic_spkm3(struct gss_ctx *ctx,
  168. struct xdr_buf *message_buffer,
  169. struct xdr_netobj *message_token)
  170. {
  171. u32 err = 0;
  172. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  173. err = spkm3_make_token(sctx, message_buffer,
  174. message_token, SPKM_MIC_TOK);
  175. dprintk("RPC: gss_get_mic_spkm3 returning %d\n", err);
  176. return err;
  177. }
  178. static const struct gss_api_ops gss_spkm3_ops = {
  179. .gss_import_sec_context = gss_import_sec_context_spkm3,
  180. .gss_get_mic = gss_get_mic_spkm3,
  181. .gss_verify_mic = gss_verify_mic_spkm3,
  182. .gss_delete_sec_context = gss_delete_sec_context_spkm3,
  183. };
  184. static struct pf_desc gss_spkm3_pfs[] = {
  185. {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"},
  186. {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
  187. };
  188. static struct gss_api_mech gss_spkm3_mech = {
  189. .gm_name = "spkm3",
  190. .gm_owner = THIS_MODULE,
  191. .gm_oid = {7, "\053\006\001\005\005\001\003"},
  192. .gm_ops = &gss_spkm3_ops,
  193. .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
  194. .gm_pfs = gss_spkm3_pfs,
  195. };
  196. static int __init init_spkm3_module(void)
  197. {
  198. int status;
  199. status = gss_mech_register(&gss_spkm3_mech);
  200. if (status)
  201. printk("Failed to register spkm3 gss mechanism!\n");
  202. return status;
  203. }
  204. static void __exit cleanup_spkm3_module(void)
  205. {
  206. gss_mech_unregister(&gss_spkm3_mech);
  207. }
  208. MODULE_LICENSE("GPL");
  209. module_init(init_spkm3_module);
  210. module_exit(cleanup_spkm3_module);