gss_krb5_mech.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * linux/net/sunrpc/gss_krb5_mech.c
  3. *
  4. * Copyright (c) 2001-2008 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 struct gss_krb5_enctype supported_gss_krb5_enctypes[] = {
  49. /*
  50. * DES (All DES enctypes are mapped to the same gss functionality)
  51. */
  52. {
  53. .etype = ENCTYPE_DES_CBC_RAW,
  54. .ctype = CKSUMTYPE_RSA_MD5,
  55. .name = "des-cbc-crc",
  56. .encrypt_name = "cbc(des)",
  57. .cksum_name = "md5",
  58. .encrypt = krb5_encrypt,
  59. .decrypt = krb5_decrypt,
  60. .signalg = SGN_ALG_DES_MAC_MD5,
  61. .sealalg = SEAL_ALG_DES,
  62. .keybytes = 7,
  63. .keylength = 8,
  64. .blocksize = 8,
  65. .cksumlength = 8,
  66. .keyed_cksum = 0,
  67. },
  68. };
  69. static const int num_supported_enctypes =
  70. ARRAY_SIZE(supported_gss_krb5_enctypes);
  71. static int
  72. supported_gss_krb5_enctype(int etype)
  73. {
  74. int i;
  75. for (i = 0; i < num_supported_enctypes; i++)
  76. if (supported_gss_krb5_enctypes[i].etype == etype)
  77. return 1;
  78. return 0;
  79. }
  80. static const struct gss_krb5_enctype *
  81. get_gss_krb5_enctype(int etype)
  82. {
  83. int i;
  84. for (i = 0; i < num_supported_enctypes; i++)
  85. if (supported_gss_krb5_enctypes[i].etype == etype)
  86. return &supported_gss_krb5_enctypes[i];
  87. return NULL;
  88. }
  89. static const void *
  90. simple_get_bytes(const void *p, const void *end, void *res, int len)
  91. {
  92. const void *q = (const void *)((const char *)p + len);
  93. if (unlikely(q > end || q < p))
  94. return ERR_PTR(-EFAULT);
  95. memcpy(res, p, len);
  96. return q;
  97. }
  98. static const void *
  99. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
  100. {
  101. const void *q;
  102. unsigned int len;
  103. p = simple_get_bytes(p, end, &len, sizeof(len));
  104. if (IS_ERR(p))
  105. return p;
  106. q = (const void *)((const char *)p + len);
  107. if (unlikely(q > end || q < p))
  108. return ERR_PTR(-EFAULT);
  109. res->data = kmemdup(p, len, GFP_NOFS);
  110. if (unlikely(res->data == NULL))
  111. return ERR_PTR(-ENOMEM);
  112. res->len = len;
  113. return q;
  114. }
  115. static inline const void *
  116. get_key(const void *p, const void *end,
  117. struct krb5_ctx *ctx, struct crypto_blkcipher **res)
  118. {
  119. struct xdr_netobj key;
  120. int alg;
  121. p = simple_get_bytes(p, end, &alg, sizeof(alg));
  122. if (IS_ERR(p))
  123. goto out_err;
  124. switch (alg) {
  125. case ENCTYPE_DES_CBC_CRC:
  126. case ENCTYPE_DES_CBC_MD4:
  127. case ENCTYPE_DES_CBC_MD5:
  128. /* Map all these key types to ENCTYPE_DES_CBC_RAW */
  129. alg = ENCTYPE_DES_CBC_RAW;
  130. break;
  131. }
  132. if (!supported_gss_krb5_enctype(alg)) {
  133. printk(KERN_WARNING "gss_kerberos_mech: unsupported "
  134. "encryption key algorithm %d\n", alg);
  135. goto out_err;
  136. }
  137. p = simple_get_netobj(p, end, &key);
  138. if (IS_ERR(p))
  139. goto out_err;
  140. *res = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0,
  141. CRYPTO_ALG_ASYNC);
  142. if (IS_ERR(*res)) {
  143. printk(KERN_WARNING "gss_kerberos_mech: unable to initialize "
  144. "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
  145. *res = NULL;
  146. goto out_err_free_key;
  147. }
  148. if (crypto_blkcipher_setkey(*res, key.data, key.len)) {
  149. printk(KERN_WARNING "gss_kerberos_mech: error setting key for "
  150. "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
  151. goto out_err_free_tfm;
  152. }
  153. kfree(key.data);
  154. return p;
  155. out_err_free_tfm:
  156. crypto_free_blkcipher(*res);
  157. out_err_free_key:
  158. kfree(key.data);
  159. p = ERR_PTR(-EINVAL);
  160. out_err:
  161. return p;
  162. }
  163. static int
  164. gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
  165. {
  166. int tmp;
  167. p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
  168. if (IS_ERR(p))
  169. goto out_err;
  170. /* Old format supports only DES! Any other enctype uses new format */
  171. ctx->enctype = ENCTYPE_DES_CBC_RAW;
  172. ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
  173. if (ctx->gk5e == NULL)
  174. goto out_err;
  175. /* The downcall format was designed before we completely understood
  176. * the uses of the context fields; so it includes some stuff we
  177. * just give some minimal sanity-checking, and some we ignore
  178. * completely (like the next twenty bytes): */
  179. if (unlikely(p + 20 > end || p + 20 < p))
  180. goto out_err;
  181. p += 20;
  182. p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
  183. if (IS_ERR(p))
  184. goto out_err;
  185. if (tmp != SGN_ALG_DES_MAC_MD5) {
  186. p = ERR_PTR(-ENOSYS);
  187. goto out_err;
  188. }
  189. p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
  190. if (IS_ERR(p))
  191. goto out_err;
  192. if (tmp != SEAL_ALG_DES) {
  193. p = ERR_PTR(-ENOSYS);
  194. goto out_err;
  195. }
  196. p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
  197. if (IS_ERR(p))
  198. goto out_err;
  199. p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
  200. if (IS_ERR(p))
  201. goto out_err;
  202. p = simple_get_netobj(p, end, &ctx->mech_used);
  203. if (IS_ERR(p))
  204. goto out_err;
  205. p = get_key(p, end, ctx, &ctx->enc);
  206. if (IS_ERR(p))
  207. goto out_err_free_mech;
  208. p = get_key(p, end, ctx, &ctx->seq);
  209. if (IS_ERR(p))
  210. goto out_err_free_key1;
  211. if (p != end) {
  212. p = ERR_PTR(-EFAULT);
  213. goto out_err_free_key2;
  214. }
  215. return 0;
  216. out_err_free_key2:
  217. crypto_free_blkcipher(ctx->seq);
  218. out_err_free_key1:
  219. crypto_free_blkcipher(ctx->enc);
  220. out_err_free_mech:
  221. kfree(ctx->mech_used.data);
  222. out_err:
  223. return PTR_ERR(p);
  224. }
  225. static int
  226. gss_import_sec_context_kerberos(const void *p, size_t len,
  227. struct gss_ctx *ctx_id)
  228. {
  229. const void *end = (const void *)((const char *)p + len);
  230. struct krb5_ctx *ctx;
  231. int ret;
  232. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  233. if (ctx == NULL)
  234. return -ENOMEM;
  235. if (len == 85)
  236. ret = gss_import_v1_context(p, end, ctx);
  237. else
  238. ret = -EINVAL;
  239. if (ret == 0)
  240. ctx_id->internal_ctx_id = ctx;
  241. else
  242. kfree(ctx);
  243. dprintk("RPC: %s: returning %d\n", __func__, ret);
  244. return ret;
  245. }
  246. static void
  247. gss_delete_sec_context_kerberos(void *internal_ctx) {
  248. struct krb5_ctx *kctx = internal_ctx;
  249. crypto_free_blkcipher(kctx->seq);
  250. crypto_free_blkcipher(kctx->enc);
  251. kfree(kctx->mech_used.data);
  252. kfree(kctx);
  253. }
  254. static const struct gss_api_ops gss_kerberos_ops = {
  255. .gss_import_sec_context = gss_import_sec_context_kerberos,
  256. .gss_get_mic = gss_get_mic_kerberos,
  257. .gss_verify_mic = gss_verify_mic_kerberos,
  258. .gss_wrap = gss_wrap_kerberos,
  259. .gss_unwrap = gss_unwrap_kerberos,
  260. .gss_delete_sec_context = gss_delete_sec_context_kerberos,
  261. };
  262. static struct pf_desc gss_kerberos_pfs[] = {
  263. [0] = {
  264. .pseudoflavor = RPC_AUTH_GSS_KRB5,
  265. .service = RPC_GSS_SVC_NONE,
  266. .name = "krb5",
  267. },
  268. [1] = {
  269. .pseudoflavor = RPC_AUTH_GSS_KRB5I,
  270. .service = RPC_GSS_SVC_INTEGRITY,
  271. .name = "krb5i",
  272. },
  273. [2] = {
  274. .pseudoflavor = RPC_AUTH_GSS_KRB5P,
  275. .service = RPC_GSS_SVC_PRIVACY,
  276. .name = "krb5p",
  277. },
  278. };
  279. static struct gss_api_mech gss_kerberos_mech = {
  280. .gm_name = "krb5",
  281. .gm_owner = THIS_MODULE,
  282. .gm_oid = {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"},
  283. .gm_ops = &gss_kerberos_ops,
  284. .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
  285. .gm_pfs = gss_kerberos_pfs,
  286. };
  287. static int __init init_kerberos_module(void)
  288. {
  289. int status;
  290. status = gss_mech_register(&gss_kerberos_mech);
  291. if (status)
  292. printk("Failed to register kerberos gss mechanism!\n");
  293. return status;
  294. }
  295. static void __exit cleanup_kerberos_module(void)
  296. {
  297. gss_mech_unregister(&gss_kerberos_mech);
  298. }
  299. MODULE_LICENSE("GPL");
  300. module_init(init_kerberos_module);
  301. module_exit(cleanup_kerberos_module);