gss_krb5_mech.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * linux/net/sunrpc/gss_krb5_mech.c
  3. *
  4. * Copyright (c) 2001 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/module.h>
  37. #include <linux/init.h>
  38. #include <linux/types.h>
  39. #include <linux/slab.h>
  40. #include <linux/sunrpc/auth.h>
  41. #include <linux/sunrpc/gss_krb5.h>
  42. #include <linux/sunrpc/xdr.h>
  43. #include <linux/crypto.h>
  44. #ifdef RPC_DEBUG
  45. # define RPCDBG_FACILITY RPCDBG_AUTH
  46. #endif
  47. static const void *
  48. simple_get_bytes(const void *p, const void *end, void *res, int len)
  49. {
  50. const void *q = (const void *)((const char *)p + len);
  51. if (unlikely(q > end || q < p))
  52. return ERR_PTR(-EFAULT);
  53. memcpy(res, p, len);
  54. return q;
  55. }
  56. static const void *
  57. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
  58. {
  59. const void *q;
  60. unsigned int len;
  61. p = simple_get_bytes(p, end, &len, sizeof(len));
  62. if (IS_ERR(p))
  63. return p;
  64. q = (const void *)((const char *)p + len);
  65. if (unlikely(q > end || q < p))
  66. return ERR_PTR(-EFAULT);
  67. res->data = kmalloc(len, GFP_KERNEL);
  68. if (unlikely(res->data == NULL))
  69. return ERR_PTR(-ENOMEM);
  70. memcpy(res->data, p, len);
  71. res->len = len;
  72. return q;
  73. }
  74. static inline const void *
  75. get_key(const void *p, const void *end, struct crypto_tfm **res)
  76. {
  77. struct xdr_netobj key;
  78. int alg, alg_mode;
  79. char *alg_name;
  80. p = simple_get_bytes(p, end, &alg, sizeof(alg));
  81. if (IS_ERR(p))
  82. goto out_err;
  83. p = simple_get_netobj(p, end, &key);
  84. if (IS_ERR(p))
  85. goto out_err;
  86. switch (alg) {
  87. case ENCTYPE_DES_CBC_RAW:
  88. alg_name = "des";
  89. alg_mode = CRYPTO_TFM_MODE_CBC;
  90. break;
  91. default:
  92. printk("gss_kerberos_mech: unsupported algorithm %d\n", alg);
  93. goto out_err_free_key;
  94. }
  95. if (!(*res = crypto_alloc_tfm(alg_name, alg_mode))) {
  96. printk("gss_kerberos_mech: unable to initialize crypto algorithm %s\n", alg_name);
  97. goto out_err_free_key;
  98. }
  99. if (crypto_cipher_setkey(*res, key.data, key.len)) {
  100. printk("gss_kerberos_mech: error setting key for crypto algorithm %s\n", alg_name);
  101. goto out_err_free_tfm;
  102. }
  103. kfree(key.data);
  104. return p;
  105. out_err_free_tfm:
  106. crypto_free_tfm(*res);
  107. out_err_free_key:
  108. kfree(key.data);
  109. p = ERR_PTR(-EINVAL);
  110. out_err:
  111. return p;
  112. }
  113. static int
  114. gss_import_sec_context_kerberos(const void *p,
  115. size_t len,
  116. struct gss_ctx *ctx_id)
  117. {
  118. const void *end = (const void *)((const char *)p + len);
  119. struct krb5_ctx *ctx;
  120. if (!(ctx = kzalloc(sizeof(*ctx), GFP_KERNEL)))
  121. goto out_err;
  122. p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
  123. if (IS_ERR(p))
  124. goto out_err_free_ctx;
  125. p = simple_get_bytes(p, end, &ctx->seed_init, sizeof(ctx->seed_init));
  126. if (IS_ERR(p))
  127. goto out_err_free_ctx;
  128. p = simple_get_bytes(p, end, ctx->seed, sizeof(ctx->seed));
  129. if (IS_ERR(p))
  130. goto out_err_free_ctx;
  131. p = simple_get_bytes(p, end, &ctx->signalg, sizeof(ctx->signalg));
  132. if (IS_ERR(p))
  133. goto out_err_free_ctx;
  134. p = simple_get_bytes(p, end, &ctx->sealalg, sizeof(ctx->sealalg));
  135. if (IS_ERR(p))
  136. goto out_err_free_ctx;
  137. p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
  138. if (IS_ERR(p))
  139. goto out_err_free_ctx;
  140. p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
  141. if (IS_ERR(p))
  142. goto out_err_free_ctx;
  143. p = simple_get_netobj(p, end, &ctx->mech_used);
  144. if (IS_ERR(p))
  145. goto out_err_free_ctx;
  146. p = get_key(p, end, &ctx->enc);
  147. if (IS_ERR(p))
  148. goto out_err_free_mech;
  149. p = get_key(p, end, &ctx->seq);
  150. if (IS_ERR(p))
  151. goto out_err_free_key1;
  152. if (p != end) {
  153. p = ERR_PTR(-EFAULT);
  154. goto out_err_free_key2;
  155. }
  156. ctx_id->internal_ctx_id = ctx;
  157. dprintk("RPC: Successfully imported new context.\n");
  158. return 0;
  159. out_err_free_key2:
  160. crypto_free_tfm(ctx->seq);
  161. out_err_free_key1:
  162. crypto_free_tfm(ctx->enc);
  163. out_err_free_mech:
  164. kfree(ctx->mech_used.data);
  165. out_err_free_ctx:
  166. kfree(ctx);
  167. out_err:
  168. return PTR_ERR(p);
  169. }
  170. static void
  171. gss_delete_sec_context_kerberos(void *internal_ctx) {
  172. struct krb5_ctx *kctx = internal_ctx;
  173. crypto_free_tfm(kctx->seq);
  174. crypto_free_tfm(kctx->enc);
  175. kfree(kctx->mech_used.data);
  176. kfree(kctx);
  177. }
  178. static struct gss_api_ops gss_kerberos_ops = {
  179. .gss_import_sec_context = gss_import_sec_context_kerberos,
  180. .gss_get_mic = gss_get_mic_kerberos,
  181. .gss_verify_mic = gss_verify_mic_kerberos,
  182. .gss_wrap = gss_wrap_kerberos,
  183. .gss_unwrap = gss_unwrap_kerberos,
  184. .gss_delete_sec_context = gss_delete_sec_context_kerberos,
  185. };
  186. static struct pf_desc gss_kerberos_pfs[] = {
  187. [0] = {
  188. .pseudoflavor = RPC_AUTH_GSS_KRB5,
  189. .service = RPC_GSS_SVC_NONE,
  190. .name = "krb5",
  191. },
  192. [1] = {
  193. .pseudoflavor = RPC_AUTH_GSS_KRB5I,
  194. .service = RPC_GSS_SVC_INTEGRITY,
  195. .name = "krb5i",
  196. },
  197. [2] = {
  198. .pseudoflavor = RPC_AUTH_GSS_KRB5P,
  199. .service = RPC_GSS_SVC_PRIVACY,
  200. .name = "krb5p",
  201. },
  202. };
  203. static struct gss_api_mech gss_kerberos_mech = {
  204. .gm_name = "krb5",
  205. .gm_owner = THIS_MODULE,
  206. .gm_ops = &gss_kerberos_ops,
  207. .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
  208. .gm_pfs = gss_kerberos_pfs,
  209. };
  210. static int __init init_kerberos_module(void)
  211. {
  212. int status;
  213. status = gss_mech_register(&gss_kerberos_mech);
  214. if (status)
  215. printk("Failed to register kerberos gss mechanism!\n");
  216. return status;
  217. }
  218. static void __exit cleanup_kerberos_module(void)
  219. {
  220. gss_mech_unregister(&gss_kerberos_mech);
  221. }
  222. MODULE_LICENSE("GPL");
  223. module_init(init_kerberos_module);
  224. module_exit(cleanup_kerberos_module);