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); |
| 79 | res->data = kmalloc(len, GFP_KERNEL); |
| 80 | if (unlikely(res->data == NULL)) |
| 81 | return ERR_PTR(-ENOMEM); |
| 82 | memcpy(res->data, p, len); |
| 83 | return q; |
| 84 | } |
| 85 | |
| 86 | static inline const void * |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 87 | get_key(const void *p, const void *end, struct crypto_blkcipher **res, |
| 88 | int *resalg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | struct xdr_netobj key = { 0 }; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 91 | int setkey = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | char *alg_name; |
| 93 | |
| 94 | p = simple_get_bytes(p, end, resalg, sizeof(*resalg)); |
| 95 | if (IS_ERR(p)) |
| 96 | goto out_err; |
| 97 | p = simple_get_netobj(p, end, &key); |
| 98 | if (IS_ERR(p)) |
| 99 | goto out_err; |
| 100 | |
| 101 | switch (*resalg) { |
| 102 | case NID_des_cbc: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 103 | alg_name = "cbc(des)"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | setkey = 1; |
| 105 | break; |
J. Bruce Fields | 0e19c1e | 2006-03-20 23:24:40 -0500 | [diff] [blame] | 106 | case NID_cast5_cbc: |
| 107 | /* XXXX here in name only, not used */ |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 108 | alg_name = "cbc(cast5)"; |
J. Bruce Fields | 0e19c1e | 2006-03-20 23:24:40 -0500 | [diff] [blame] | 109 | setkey = 0; /* XXX will need to set to 1 */ |
| 110 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | case NID_md5: |
| 112 | if (key.len == 0) { |
| 113 | dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n"); |
| 114 | } |
| 115 | alg_name = "md5"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | setkey = 0; |
| 117 | break; |
| 118 | default: |
J. Bruce Fields | 42181d4 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 119 | dprintk("gss_spkm3_mech: unsupported algorithm %d\n", *resalg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | goto out_err_free_key; |
| 121 | } |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 122 | *res = crypto_alloc_blkcipher(alg_name, 0, CRYPTO_ALG_ASYNC); |
| 123 | if (IS_ERR(*res)) { |
J. Bruce Fields | 42181d4 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 124 | printk("gss_spkm3_mech: unable to initialize crypto algorthm %s\n", alg_name); |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 125 | *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | goto out_err_free_key; |
J. Bruce Fields | 42181d4 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | if (setkey) { |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 129 | if (crypto_blkcipher_setkey(*res, key.data, key.len)) { |
J. Bruce Fields | 42181d4 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 130 | printk("gss_spkm3_mech: error setting key for crypto algorthm %s\n", alg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto out_err_free_tfm; |
J. Bruce Fields | 42181d4 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if(key.len > 0) |
| 136 | kfree(key.data); |
| 137 | return p; |
| 138 | |
| 139 | out_err_free_tfm: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 140 | crypto_free_blkcipher(*res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | out_err_free_key: |
| 142 | if(key.len > 0) |
| 143 | kfree(key.data); |
| 144 | p = ERR_PTR(-EINVAL); |
| 145 | out_err: |
| 146 | return p; |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | gss_import_sec_context_spkm3(const void *p, size_t len, |
| 151 | struct gss_ctx *ctx_id) |
| 152 | { |
| 153 | const void *end = (const void *)((const char *)p + len); |
| 154 | struct spkm3_ctx *ctx; |
| 155 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 156 | if (!(ctx = kzalloc(sizeof(*ctx), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | p = simple_get_netobj(p, end, &ctx->ctx_id); |
| 160 | if (IS_ERR(p)) |
| 161 | goto out_err_free_ctx; |
| 162 | |
| 163 | p = simple_get_bytes(p, end, &ctx->qop, sizeof(ctx->qop)); |
| 164 | if (IS_ERR(p)) |
| 165 | goto out_err_free_ctx_id; |
| 166 | |
| 167 | p = simple_get_netobj(p, end, &ctx->mech_used); |
| 168 | if (IS_ERR(p)) |
| 169 | goto out_err_free_mech; |
| 170 | |
| 171 | p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags)); |
| 172 | if (IS_ERR(p)) |
| 173 | goto out_err_free_mech; |
| 174 | |
| 175 | p = simple_get_bytes(p, end, &ctx->req_flags, sizeof(ctx->req_flags)); |
| 176 | if (IS_ERR(p)) |
| 177 | goto out_err_free_mech; |
| 178 | |
| 179 | p = simple_get_netobj(p, end, &ctx->share_key); |
| 180 | if (IS_ERR(p)) |
| 181 | goto out_err_free_s_key; |
| 182 | |
| 183 | p = get_key(p, end, &ctx->derived_conf_key, &ctx->conf_alg); |
| 184 | if (IS_ERR(p)) |
| 185 | goto out_err_free_s_key; |
| 186 | |
| 187 | p = get_key(p, end, &ctx->derived_integ_key, &ctx->intg_alg); |
| 188 | if (IS_ERR(p)) |
| 189 | goto out_err_free_key1; |
| 190 | |
| 191 | p = simple_get_bytes(p, end, &ctx->keyestb_alg, sizeof(ctx->keyestb_alg)); |
| 192 | if (IS_ERR(p)) |
| 193 | goto out_err_free_key2; |
| 194 | |
| 195 | p = simple_get_bytes(p, end, &ctx->owf_alg, sizeof(ctx->owf_alg)); |
| 196 | if (IS_ERR(p)) |
| 197 | goto out_err_free_key2; |
| 198 | |
| 199 | if (p != end) |
| 200 | goto out_err_free_key2; |
| 201 | |
| 202 | ctx_id->internal_ctx_id = ctx; |
| 203 | |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 204 | dprintk("Successfully imported new spkm context.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return 0; |
| 206 | |
| 207 | out_err_free_key2: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 208 | crypto_free_blkcipher(ctx->derived_integ_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | out_err_free_key1: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 210 | crypto_free_blkcipher(ctx->derived_conf_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | out_err_free_s_key: |
| 212 | kfree(ctx->share_key.data); |
| 213 | out_err_free_mech: |
| 214 | kfree(ctx->mech_used.data); |
| 215 | out_err_free_ctx_id: |
| 216 | kfree(ctx->ctx_id.data); |
| 217 | out_err_free_ctx: |
| 218 | kfree(ctx); |
| 219 | out_err: |
| 220 | return PTR_ERR(p); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | gss_delete_sec_context_spkm3(void *internal_ctx) { |
| 225 | struct spkm3_ctx *sctx = internal_ctx; |
| 226 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame^] | 227 | crypto_free_blkcipher(sctx->derived_integ_key); |
| 228 | crypto_free_blkcipher(sctx->derived_conf_key); |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 229 | kfree(sctx->share_key.data); |
| 230 | kfree(sctx->mech_used.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | kfree(sctx); |
| 232 | } |
| 233 | |
| 234 | static u32 |
| 235 | gss_verify_mic_spkm3(struct gss_ctx *ctx, |
| 236 | struct xdr_buf *signbuf, |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 237 | struct xdr_netobj *checksum) |
| 238 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | u32 maj_stat = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | struct spkm3_ctx *sctx = ctx->internal_ctx_id; |
| 241 | |
| 242 | dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n"); |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 243 | maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat); |
| 246 | return maj_stat; |
| 247 | } |
| 248 | |
| 249 | static u32 |
| 250 | gss_get_mic_spkm3(struct gss_ctx *ctx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | struct xdr_buf *message_buffer, |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 252 | struct xdr_netobj *message_token) |
| 253 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | u32 err = 0; |
| 255 | struct spkm3_ctx *sctx = ctx->internal_ctx_id; |
| 256 | |
| 257 | dprintk("RPC: gss_get_mic_spkm3\n"); |
| 258 | |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 259 | err = spkm3_make_token(sctx, message_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | message_token, SPKM_MIC_TOK); |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | static struct gss_api_ops gss_spkm3_ops = { |
| 265 | .gss_import_sec_context = gss_import_sec_context_spkm3, |
| 266 | .gss_get_mic = gss_get_mic_spkm3, |
| 267 | .gss_verify_mic = gss_verify_mic_spkm3, |
| 268 | .gss_delete_sec_context = gss_delete_sec_context_spkm3, |
| 269 | }; |
| 270 | |
| 271 | static struct pf_desc gss_spkm3_pfs[] = { |
J. Bruce Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 272 | {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"}, |
| 273 | {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | static struct gss_api_mech gss_spkm3_mech = { |
| 277 | .gm_name = "spkm3", |
| 278 | .gm_owner = THIS_MODULE, |
| 279 | .gm_ops = &gss_spkm3_ops, |
| 280 | .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs), |
| 281 | .gm_pfs = gss_spkm3_pfs, |
| 282 | }; |
| 283 | |
| 284 | static int __init init_spkm3_module(void) |
| 285 | { |
| 286 | int status; |
| 287 | |
| 288 | status = gss_mech_register(&gss_spkm3_mech); |
| 289 | if (status) |
| 290 | printk("Failed to register spkm3 gss mechanism!\n"); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static void __exit cleanup_spkm3_module(void) |
| 295 | { |
| 296 | gss_mech_unregister(&gss_spkm3_mech); |
| 297 | } |
| 298 | |
| 299 | MODULE_LICENSE("GPL"); |
| 300 | module_init(init_spkm3_module); |
| 301 | module_exit(cleanup_spkm3_module); |