gss_krb5_mech.c 6.8 KB

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