gss_spkm3_mech.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_cast5_cbc:
  99. /* XXXX here in name only, not used */
  100. alg_name = "cast5";
  101. alg_mode = CRYPTO_TFM_MODE_CBC;
  102. setkey = 0; /* XXX will need to set to 1 */
  103. break;
  104. case NID_md5:
  105. if (key.len == 0) {
  106. dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n");
  107. }
  108. alg_name = "md5";
  109. alg_mode = 0;
  110. setkey = 0;
  111. break;
  112. default:
  113. dprintk("gss_spkm3_mech: unsupported algorithm %d\n", *resalg);
  114. goto out_err_free_key;
  115. }
  116. if (!(*res = crypto_alloc_tfm(alg_name, alg_mode))) {
  117. printk("gss_spkm3_mech: unable to initialize crypto algorthm %s\n", alg_name);
  118. goto out_err_free_key;
  119. }
  120. if (setkey) {
  121. if (crypto_cipher_setkey(*res, key.data, key.len)) {
  122. printk("gss_spkm3_mech: error setting key for crypto algorthm %s\n", alg_name);
  123. goto out_err_free_tfm;
  124. }
  125. }
  126. if(key.len > 0)
  127. kfree(key.data);
  128. return p;
  129. out_err_free_tfm:
  130. crypto_free_tfm(*res);
  131. out_err_free_key:
  132. if(key.len > 0)
  133. kfree(key.data);
  134. p = ERR_PTR(-EINVAL);
  135. out_err:
  136. return p;
  137. }
  138. static int
  139. gss_import_sec_context_spkm3(const void *p, size_t len,
  140. struct gss_ctx *ctx_id)
  141. {
  142. const void *end = (const void *)((const char *)p + len);
  143. struct spkm3_ctx *ctx;
  144. if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
  145. goto out_err;
  146. memset(ctx, 0, sizeof(*ctx));
  147. p = simple_get_netobj(p, end, &ctx->ctx_id);
  148. if (IS_ERR(p))
  149. goto out_err_free_ctx;
  150. p = simple_get_bytes(p, end, &ctx->qop, sizeof(ctx->qop));
  151. if (IS_ERR(p))
  152. goto out_err_free_ctx_id;
  153. p = simple_get_netobj(p, end, &ctx->mech_used);
  154. if (IS_ERR(p))
  155. goto out_err_free_mech;
  156. p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags));
  157. if (IS_ERR(p))
  158. goto out_err_free_mech;
  159. p = simple_get_bytes(p, end, &ctx->req_flags, sizeof(ctx->req_flags));
  160. if (IS_ERR(p))
  161. goto out_err_free_mech;
  162. p = simple_get_netobj(p, end, &ctx->share_key);
  163. if (IS_ERR(p))
  164. goto out_err_free_s_key;
  165. p = get_key(p, end, &ctx->derived_conf_key, &ctx->conf_alg);
  166. if (IS_ERR(p))
  167. goto out_err_free_s_key;
  168. p = get_key(p, end, &ctx->derived_integ_key, &ctx->intg_alg);
  169. if (IS_ERR(p))
  170. goto out_err_free_key1;
  171. p = simple_get_bytes(p, end, &ctx->keyestb_alg, sizeof(ctx->keyestb_alg));
  172. if (IS_ERR(p))
  173. goto out_err_free_key2;
  174. p = simple_get_bytes(p, end, &ctx->owf_alg, sizeof(ctx->owf_alg));
  175. if (IS_ERR(p))
  176. goto out_err_free_key2;
  177. if (p != end)
  178. goto out_err_free_key2;
  179. ctx_id->internal_ctx_id = ctx;
  180. dprintk("Succesfully imported new spkm context.\n");
  181. return 0;
  182. out_err_free_key2:
  183. crypto_free_tfm(ctx->derived_integ_key);
  184. out_err_free_key1:
  185. crypto_free_tfm(ctx->derived_conf_key);
  186. out_err_free_s_key:
  187. kfree(ctx->share_key.data);
  188. out_err_free_mech:
  189. kfree(ctx->mech_used.data);
  190. out_err_free_ctx_id:
  191. kfree(ctx->ctx_id.data);
  192. out_err_free_ctx:
  193. kfree(ctx);
  194. out_err:
  195. return PTR_ERR(p);
  196. }
  197. static void
  198. gss_delete_sec_context_spkm3(void *internal_ctx) {
  199. struct spkm3_ctx *sctx = internal_ctx;
  200. crypto_free_tfm(sctx->derived_integ_key);
  201. crypto_free_tfm(sctx->derived_conf_key);
  202. kfree(sctx->share_key.data);
  203. kfree(sctx->mech_used.data);
  204. kfree(sctx);
  205. }
  206. static u32
  207. gss_verify_mic_spkm3(struct gss_ctx *ctx,
  208. struct xdr_buf *signbuf,
  209. struct xdr_netobj *checksum)
  210. {
  211. u32 maj_stat = 0;
  212. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  213. dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n");
  214. maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK);
  215. dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
  216. return maj_stat;
  217. }
  218. static u32
  219. gss_get_mic_spkm3(struct gss_ctx *ctx,
  220. struct xdr_buf *message_buffer,
  221. struct xdr_netobj *message_token)
  222. {
  223. u32 err = 0;
  224. struct spkm3_ctx *sctx = ctx->internal_ctx_id;
  225. dprintk("RPC: gss_get_mic_spkm3\n");
  226. err = spkm3_make_token(sctx, message_buffer,
  227. message_token, SPKM_MIC_TOK);
  228. return err;
  229. }
  230. static struct gss_api_ops gss_spkm3_ops = {
  231. .gss_import_sec_context = gss_import_sec_context_spkm3,
  232. .gss_get_mic = gss_get_mic_spkm3,
  233. .gss_verify_mic = gss_verify_mic_spkm3,
  234. .gss_delete_sec_context = gss_delete_sec_context_spkm3,
  235. };
  236. static struct pf_desc gss_spkm3_pfs[] = {
  237. {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"},
  238. {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
  239. };
  240. static struct gss_api_mech gss_spkm3_mech = {
  241. .gm_name = "spkm3",
  242. .gm_owner = THIS_MODULE,
  243. .gm_ops = &gss_spkm3_ops,
  244. .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
  245. .gm_pfs = gss_spkm3_pfs,
  246. };
  247. static int __init init_spkm3_module(void)
  248. {
  249. int status;
  250. status = gss_mech_register(&gss_spkm3_mech);
  251. if (status)
  252. printk("Failed to register spkm3 gss mechanism!\n");
  253. return 0;
  254. }
  255. static void __exit cleanup_spkm3_module(void)
  256. {
  257. gss_mech_unregister(&gss_spkm3_mech);
  258. }
  259. MODULE_LICENSE("GPL");
  260. module_init(init_spkm3_module);
  261. module_exit(cleanup_spkm3_module);