gss_krb5_mech.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 = kmalloc(sizeof(*ctx), GFP_KERNEL)))
  121. goto out_err;
  122. memset(ctx, 0, sizeof(*ctx));
  123. p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
  124. if (IS_ERR(p))
  125. goto out_err_free_ctx;
  126. p = simple_get_bytes(p, end, &ctx->seed_init, sizeof(ctx->seed_init));
  127. if (IS_ERR(p))
  128. goto out_err_free_ctx;
  129. p = simple_get_bytes(p, end, ctx->seed, sizeof(ctx->seed));
  130. if (IS_ERR(p))
  131. goto out_err_free_ctx;
  132. p = simple_get_bytes(p, end, &ctx->signalg, sizeof(ctx->signalg));
  133. if (IS_ERR(p))
  134. goto out_err_free_ctx;
  135. p = simple_get_bytes(p, end, &ctx->sealalg, sizeof(ctx->sealalg));
  136. if (IS_ERR(p))
  137. goto out_err_free_ctx;
  138. p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
  139. if (IS_ERR(p))
  140. goto out_err_free_ctx;
  141. p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
  142. if (IS_ERR(p))
  143. goto out_err_free_ctx;
  144. p = simple_get_netobj(p, end, &ctx->mech_used);
  145. if (IS_ERR(p))
  146. goto out_err_free_ctx;
  147. p = get_key(p, end, &ctx->enc);
  148. if (IS_ERR(p))
  149. goto out_err_free_mech;
  150. p = get_key(p, end, &ctx->seq);
  151. if (IS_ERR(p))
  152. goto out_err_free_key1;
  153. if (p != end) {
  154. p = ERR_PTR(-EFAULT);
  155. goto out_err_free_key2;
  156. }
  157. ctx_id->internal_ctx_id = ctx;
  158. dprintk("RPC: Succesfully imported new context.\n");
  159. return 0;
  160. out_err_free_key2:
  161. crypto_free_tfm(ctx->seq);
  162. out_err_free_key1:
  163. crypto_free_tfm(ctx->enc);
  164. out_err_free_mech:
  165. kfree(ctx->mech_used.data);
  166. out_err_free_ctx:
  167. kfree(ctx);
  168. out_err:
  169. return PTR_ERR(p);
  170. }
  171. static void
  172. gss_delete_sec_context_kerberos(void *internal_ctx) {
  173. struct krb5_ctx *kctx = internal_ctx;
  174. crypto_free_tfm(kctx->seq);
  175. crypto_free_tfm(kctx->enc);
  176. kfree(kctx->mech_used.data);
  177. kfree(kctx);
  178. }
  179. static struct gss_api_ops gss_kerberos_ops = {
  180. .gss_import_sec_context = gss_import_sec_context_kerberos,
  181. .gss_get_mic = gss_get_mic_kerberos,
  182. .gss_verify_mic = gss_verify_mic_kerberos,
  183. .gss_wrap = gss_wrap_kerberos,
  184. .gss_unwrap = gss_unwrap_kerberos,
  185. .gss_delete_sec_context = gss_delete_sec_context_kerberos,
  186. };
  187. static struct pf_desc gss_kerberos_pfs[] = {
  188. [0] = {
  189. .pseudoflavor = RPC_AUTH_GSS_KRB5,
  190. .service = RPC_GSS_SVC_NONE,
  191. .name = "krb5",
  192. },
  193. [1] = {
  194. .pseudoflavor = RPC_AUTH_GSS_KRB5I,
  195. .service = RPC_GSS_SVC_INTEGRITY,
  196. .name = "krb5i",
  197. },
  198. [2] = {
  199. .pseudoflavor = RPC_AUTH_GSS_KRB5P,
  200. .service = RPC_GSS_SVC_PRIVACY,
  201. .name = "krb5p",
  202. },
  203. };
  204. static struct gss_api_mech gss_kerberos_mech = {
  205. .gm_name = "krb5",
  206. .gm_owner = THIS_MODULE,
  207. .gm_ops = &gss_kerberos_ops,
  208. .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
  209. .gm_pfs = gss_kerberos_pfs,
  210. };
  211. static int __init init_kerberos_module(void)
  212. {
  213. int status;
  214. status = gss_mech_register(&gss_kerberos_mech);
  215. if (status)
  216. printk("Failed to register kerberos gss mechanism!\n");
  217. return status;
  218. }
  219. static void __exit cleanup_kerberos_module(void)
  220. {
  221. gss_mech_unregister(&gss_kerberos_mech);
  222. }
  223. MODULE_LICENSE("GPL");
  224. module_init(init_kerberos_module);
  225. module_exit(cleanup_kerberos_module);