Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 37 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/module.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/types.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/sunrpc/auth.h> |
| 43 | #include <linux/in.h> |
| 44 | #include <linux/sunrpc/svcauth_gss.h> |
| 45 | #include <linux/sunrpc/gss_spkm3.h> |
| 46 | #include <linux/sunrpc/xdr.h> |
| 47 | #include <linux/crypto.h> |
| 48 | |
| 49 | #ifdef RPC_DEBUG |
| 50 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 51 | #endif |
| 52 | |
| 53 | static const void * |
| 54 | simple_get_bytes(const void *p, const void *end, void *res, int len) |
| 55 | { |
| 56 | const void *q = (const void *)((const char *)p + len); |
| 57 | if (unlikely(q > end || q < p)) |
| 58 | return ERR_PTR(-EFAULT); |
| 59 | memcpy(res, p, len); |
| 60 | return q; |
| 61 | } |
| 62 | |
| 63 | static const void * |
| 64 | simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res) |
| 65 | { |
| 66 | const void *q; |
| 67 | unsigned int len; |
| 68 | p = simple_get_bytes(p, end, &len, sizeof(len)); |
| 69 | if (IS_ERR(p)) |
| 70 | return p; |
| 71 | res->len = len; |
| 72 | if (len == 0) { |
| 73 | res->data = NULL; |
| 74 | return p; |
| 75 | } |
| 76 | q = (const void *)((const char *)p + len); |
| 77 | if (unlikely(q > end || q < p)) |
| 78 | return ERR_PTR(-EFAULT); |
Arnaldo Carvalho de Melo | e69062b | 2006-11-21 01:21:34 -0200 | [diff] [blame] | 79 | res->data = kmemdup(p, len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (unlikely(res->data == NULL)) |
| 81 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return q; |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | static int |
| 86 | gss_import_sec_context_spkm3(const void *p, size_t len, |
| 87 | struct gss_ctx *ctx_id) |
| 88 | { |
| 89 | const void *end = (const void *)((const char *)p + len); |
| 90 | struct spkm3_ctx *ctx; |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 91 | int version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 93 | if (!(ctx = kzalloc(sizeof(*ctx), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 96 | p = simple_get_bytes(p, end, &version, sizeof(version)); |
| 97 | if (IS_ERR(p)) |
| 98 | goto out_err_free_ctx; |
| 99 | if (version != 1) { |
| 100 | dprintk("RPC: unknown spkm3 token format: obsolete nfs-utils?\n"); |
| 101 | goto out_err_free_ctx; |
| 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | p = simple_get_netobj(p, end, &ctx->ctx_id); |
| 105 | if (IS_ERR(p)) |
| 106 | goto out_err_free_ctx; |
| 107 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 108 | p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (IS_ERR(p)) |
| 110 | goto out_err_free_ctx_id; |
| 111 | |
| 112 | p = simple_get_netobj(p, end, &ctx->mech_used); |
| 113 | if (IS_ERR(p)) |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 114 | goto out_err_free_ctx_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags)); |
| 117 | if (IS_ERR(p)) |
| 118 | goto out_err_free_mech; |
| 119 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 120 | p = simple_get_netobj(p, end, &ctx->conf_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (IS_ERR(p)) |
| 122 | goto out_err_free_mech; |
| 123 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 124 | p = simple_get_netobj(p, end, &ctx->derived_conf_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (IS_ERR(p)) |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 126 | goto out_err_free_conf_alg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 128 | p = simple_get_netobj(p, end, &ctx->intg_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (IS_ERR(p)) |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 130 | goto out_err_free_conf_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 132 | p = simple_get_netobj(p, end, &ctx->derived_integ_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (IS_ERR(p)) |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 134 | goto out_err_free_intg_alg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | if (p != end) |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 137 | goto out_err_free_intg_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | ctx_id->internal_ctx_id = ctx; |
| 140 | |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 141 | dprintk("Successfully imported new spkm context.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 144 | out_err_free_intg_key: |
| 145 | kfree(ctx->derived_integ_key.data); |
| 146 | out_err_free_intg_alg: |
| 147 | kfree(ctx->intg_alg.data); |
| 148 | out_err_free_conf_key: |
| 149 | kfree(ctx->derived_conf_key.data); |
| 150 | out_err_free_conf_alg: |
| 151 | kfree(ctx->conf_alg.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | out_err_free_mech: |
| 153 | kfree(ctx->mech_used.data); |
| 154 | out_err_free_ctx_id: |
| 155 | kfree(ctx->ctx_id.data); |
| 156 | out_err_free_ctx: |
| 157 | kfree(ctx); |
| 158 | out_err: |
| 159 | return PTR_ERR(p); |
| 160 | } |
| 161 | |
| 162 | static void |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 163 | gss_delete_sec_context_spkm3(void *internal_ctx) |
| 164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | struct spkm3_ctx *sctx = internal_ctx; |
| 166 | |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 167 | kfree(sctx->derived_integ_key.data); |
| 168 | kfree(sctx->intg_alg.data); |
| 169 | kfree(sctx->derived_conf_key.data); |
| 170 | kfree(sctx->conf_alg.data); |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 171 | kfree(sctx->mech_used.data); |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 172 | kfree(sctx->ctx_id.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | kfree(sctx); |
| 174 | } |
| 175 | |
| 176 | static u32 |
| 177 | gss_verify_mic_spkm3(struct gss_ctx *ctx, |
| 178 | struct xdr_buf *signbuf, |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 179 | struct xdr_netobj *checksum) |
| 180 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | u32 maj_stat = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | struct spkm3_ctx *sctx = ctx->internal_ctx_id; |
| 183 | |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 184 | maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat); |
| 187 | return maj_stat; |
| 188 | } |
| 189 | |
| 190 | static u32 |
| 191 | gss_get_mic_spkm3(struct gss_ctx *ctx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | struct xdr_buf *message_buffer, |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 193 | struct xdr_netobj *message_token) |
| 194 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | u32 err = 0; |
| 196 | struct spkm3_ctx *sctx = ctx->internal_ctx_id; |
| 197 | |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 198 | err = spkm3_make_token(sctx, message_buffer, |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 199 | message_token, SPKM_MIC_TOK); |
| 200 | dprintk("RPC: gss_get_mic_spkm3 returning %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static struct gss_api_ops gss_spkm3_ops = { |
| 205 | .gss_import_sec_context = gss_import_sec_context_spkm3, |
| 206 | .gss_get_mic = gss_get_mic_spkm3, |
| 207 | .gss_verify_mic = gss_verify_mic_spkm3, |
| 208 | .gss_delete_sec_context = gss_delete_sec_context_spkm3, |
| 209 | }; |
| 210 | |
| 211 | static struct pf_desc gss_spkm3_pfs[] = { |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 212 | {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"}, |
| 213 | {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | static struct gss_api_mech gss_spkm3_mech = { |
| 217 | .gm_name = "spkm3", |
| 218 | .gm_owner = THIS_MODULE, |
| 219 | .gm_ops = &gss_spkm3_ops, |
| 220 | .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs), |
| 221 | .gm_pfs = gss_spkm3_pfs, |
| 222 | }; |
| 223 | |
| 224 | static int __init init_spkm3_module(void) |
| 225 | { |
| 226 | int status; |
| 227 | |
| 228 | status = gss_mech_register(&gss_spkm3_mech); |
| 229 | if (status) |
| 230 | printk("Failed to register spkm3 gss mechanism!\n"); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static void __exit cleanup_spkm3_module(void) |
| 235 | { |
| 236 | gss_mech_unregister(&gss_spkm3_mech); |
| 237 | } |
| 238 | |
| 239 | MODULE_LICENSE("GPL"); |
| 240 | module_init(init_spkm3_module); |
| 241 | module_exit(cleanup_spkm3_module); |