gss_spkm3_mech.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/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/in.h>
  42. #include <linux/sunrpc/svcauth_gss.h>
  43. #include <linux/sunrpc/gss_spkm3.h>
  44. #include <linux/sunrpc/xdr.h>
  45. #include <linux/crypto.h>
  46. #ifdef RPC_DEBUG
  47. # define RPCDBG_FACILITY RPCDBG_AUTH
  48. #endif
  49. static const void *
  50. simple_get_bytes(const void *p, const void *end, void *res, int len)
  51. {
  52. const void *q = (const void *)((const char *)p + len);
  53. if (unlikely(q > end || q < p))
  54. return ERR_PTR(-EFAULT);
  55. memcpy(res, p, len);
  56. return q;
  57. }
  58. static const void *
  59. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
  60. {
  61. const void *q;
  62. unsigned int len;
  63. p = simple_get_bytes(p, end, &len, sizeof(len));
  64. if (IS_ERR(p))
  65. return p;
  66. res->len = len;
  67. if (len == 0) {
  68. res->data = NULL;
  69. return p;
  70. }
  71. q = (const void *)((const char *)p + len);
  72. if (unlikely(q > end || q < p))
  73. return ERR_PTR(-EFAULT);
  74. res->data = kmalloc(len, GFP_KERNEL);
  75. if (unlikely(res->data == NULL))
  76. return ERR_PTR(-ENOMEM);
  77. memcpy(res->data, p, len);
  78. return q;
  79. }
  80. static inline const void *
  81. get_key(const void *p, const void *end, struct crypto_tfm **res, int *resalg)
  82. {
  83. struct xdr_netobj key = { 0 };
  84. int alg_mode,setkey = 0;
  85. char *alg_name;
  86. p = simple_get_bytes(p, end, resalg, sizeof(*resalg));
  87. if (IS_ERR(p))
  88. goto out_err;
  89. p = simple_get_netobj(p, end, &key);
  90. if (IS_ERR(p))
  91. goto out_err;
  92. switch (*resalg) {
  93. case NID_des_cbc:
  94. alg_name = "des";
  95. alg_mode = CRYPTO_TFM_MODE_CBC;
  96. setkey = 1;
  97. break;
  98. case NID_md5:
  99. if (key.len == 0) {
  100. dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n");
  101. }
  102. alg_name = "md5";
  103. alg_mode = 0;
  104. setkey = 0;
  105. break;
  106. default:
  107. dprintk("gss_spkm3_mech: unsupported algorithm %d\n", *resalg);
  108. goto out_err_free_key;
  109. }
  110. if (!(*res = crypto_alloc_tfm(alg_name, alg_mode))) {
  111. printk("gss_spkm3_mech: unable to initialize crypto algorthm %s\n", alg_name);
  112. goto out_err_free_key;
  113. }
  114. if (setkey) {
  115. if (crypto_cipher_setkey(*res, key.data, key.len)) {
  116. printk("gss_spkm3_mech: error setting key for crypto algorthm %s\n", alg_name);
  117. goto out_err_free_tfm;
  118. }
  119. }
  120. if(key.len > 0)
  121. kfree(key.data);
  122. return p;
  123. out_err_free_tfm:
  124. crypto_free_tfm(*res);
  125. out_err_free_key:
  126. if(key.len > 0)
  127. kfree(key.data);
  128. p = ERR_PTR(-EINVAL);
  129. out_err:
  130. return p;
  131. }
  132. static int
  133. gss_import_sec_context_spkm3(const void *p, size_t len,
  134. struct gss_ctx *ctx_id)
  135. {
  136. const void *end = (const void *)((const char *)p + len);
  137. struct spkm3_ctx *ctx;
  138. if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
  139. goto out_err;
  140. memset(ctx, 0, sizeof(*ctx));
  141. p = simple_get_netobj(p, end, &ctx->ctx_id);
  142. if (IS_ERR(p))
  143. goto out_err_free_ctx;
  144. p = simple_get_bytes(p, end, &ctx->qop, sizeof(ctx->qop));
  145. if (IS_ERR(p))
  146. goto out_err_free_ctx_id;
  147. p = simple_get_netobj(p, end, &ctx->mech_used);
  148. if (IS_ERR(p))
  149. goto out_err_free_mech;
  150. p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags));
  151. if (IS_ERR(p))
  152. goto out_err_free_mech;
  153. p = simple_get_bytes(p, end, &ctx->req_flags, sizeof(ctx->req_flags));
  154. if (IS_ERR(p))
  155. goto out_err_free_mech;
  156. p = simple_get_netobj(p, end, &ctx->share_key);
  157. if (IS_ERR(p))
  158. goto out_err_free_s_key;
  159. p = get_key(p, end, &ctx->derived_conf_key, &ctx->conf_alg);
  160. if (IS_ERR(p))
  161. goto out_err_free_s_key;
  162. p = get_key(p, end, &ctx->derived_integ_key, &ctx->intg_alg);
  163. if (IS_ERR(p))
  164. goto out_err_free_key1;
  165. p = simple_get_bytes(p, end, &ctx->keyestb_alg, sizeof(ctx->keyestb_alg));
  166. if (IS_ERR(p))
  167. goto out_err_free_key2;
  168. p = simple_get_bytes(p, end, &ctx->owf_alg, sizeof(ctx->owf_alg));
  169. if (IS_ERR(p))
  170. goto out_err_free_key2;
  171. if (p != end)
  172. goto out_err_free_key2;
  173. ctx_id->internal_ctx_id = ctx;
  174. dprintk("Succesfully imported new spkm context.\n");
  175. return 0;
  176. out_err_free_key2:
  177. crypto_free_tfm(ctx->derived_integ_key);
  178. out_err_free_key1:
  179. crypto_free_tfm(ctx->derived_conf_key);
  180. out_err_free_s_key:
  181. kfree(ctx->share_key.data);
  182. out_err_free_mech:
  183. kfree(ctx->mech_used.data);
  184. out_err_free_ctx_id:
  185. kfree(ctx->ctx_id.data);
  186. out_err_free_ctx:
  187. kfree(ctx);
  188. out_err:
  189. return PTR_ERR(p);
  190. }
  191. static void
  192. gss_delete_sec_context_spkm3(void *internal_ctx) {
  193. struct spkm3_ctx *sctx = internal_ctx;
  194. crypto_free_tfm(sctx->derived_integ_key);
  195. crypto_free_tfm(sctx->derived_conf_key);
  196. kfree(sctx->share_key.data);
  197. kfree(sctx->mech_used.data);
  198. kfree(sctx);
  199. }
  200. static u32
  201. gss_verify_mic_spkm3(struct gss_ctx *ctx,
  202. struct xdr_buf *signbuf,
  203. struct xdr_netobj *checksum)
  204. {
  205. u32 maj_stat = 0;
  206. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  207. dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n");
  208. maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK);
  209. dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
  210. return maj_stat;
  211. }
  212. static u32
  213. gss_get_mic_spkm3(struct gss_ctx *ctx,
  214. struct xdr_buf *message_buffer,
  215. struct xdr_netobj *message_token)
  216. {
  217. u32 err = 0;
  218. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  219. dprintk("RPC: gss_get_mic_spkm3\n");
  220. err = spkm3_make_token(sctx, message_buffer,
  221. message_token, SPKM_MIC_TOK);
  222. return err;
  223. }
  224. static struct gss_api_ops gss_spkm3_ops = {
  225. .gss_import_sec_context = gss_import_sec_context_spkm3,
  226. .gss_get_mic = gss_get_mic_spkm3,
  227. .gss_verify_mic = gss_verify_mic_spkm3,
  228. .gss_delete_sec_context = gss_delete_sec_context_spkm3,
  229. };
  230. static struct pf_desc gss_spkm3_pfs[] = {
  231. {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"},
  232. {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
  233. };
  234. static struct gss_api_mech gss_spkm3_mech = {
  235. .gm_name = "spkm3",
  236. .gm_owner = THIS_MODULE,
  237. .gm_ops = &gss_spkm3_ops,
  238. .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
  239. .gm_pfs = gss_spkm3_pfs,
  240. };
  241. static int __init init_spkm3_module(void)
  242. {
  243. int status;
  244. status = gss_mech_register(&gss_spkm3_mech);
  245. if (status)
  246. printk("Failed to register spkm3 gss mechanism!\n");
  247. return 0;
  248. }
  249. static void __exit cleanup_spkm3_module(void)
  250. {
  251. gss_mech_unregister(&gss_spkm3_mech);
  252. }
  253. MODULE_LICENSE("GPL");
  254. module_init(init_spkm3_module);
  255. module_exit(cleanup_spkm3_module);