Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/gss_krb5_mech.c |
| 3 | * |
| 4 | * Copyright (c) 2001 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/sunrpc/gss_krb5.h> |
| 44 | #include <linux/sunrpc/xdr.h> |
| 45 | #include <linux/crypto.h> |
| 46 | |
| 47 | #ifdef RPC_DEBUG |
| 48 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 49 | #endif |
| 50 | |
| 51 | static const void * |
| 52 | simple_get_bytes(const void *p, const void *end, void *res, int len) |
| 53 | { |
| 54 | const void *q = (const void *)((const char *)p + len); |
| 55 | if (unlikely(q > end || q < p)) |
| 56 | return ERR_PTR(-EFAULT); |
| 57 | memcpy(res, p, len); |
| 58 | return q; |
| 59 | } |
| 60 | |
| 61 | static const void * |
| 62 | simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res) |
| 63 | { |
| 64 | const void *q; |
| 65 | unsigned int len; |
| 66 | |
| 67 | p = simple_get_bytes(p, end, &len, sizeof(len)); |
| 68 | if (IS_ERR(p)) |
| 69 | return p; |
| 70 | q = (const void *)((const char *)p + len); |
| 71 | if (unlikely(q > end || q < p)) |
| 72 | return ERR_PTR(-EFAULT); |
Trond Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 73 | res->data = kmemdup(p, len, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (unlikely(res->data == NULL)) |
| 75 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | res->len = len; |
| 77 | return q; |
| 78 | } |
| 79 | |
| 80 | static inline const void * |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 81 | get_key(const void *p, const void *end, struct crypto_blkcipher **res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | struct xdr_netobj key; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 84 | int alg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | char *alg_name; |
| 86 | |
| 87 | p = simple_get_bytes(p, end, &alg, sizeof(alg)); |
| 88 | if (IS_ERR(p)) |
| 89 | goto out_err; |
| 90 | p = simple_get_netobj(p, end, &key); |
| 91 | if (IS_ERR(p)) |
| 92 | goto out_err; |
| 93 | |
| 94 | switch (alg) { |
| 95 | case ENCTYPE_DES_CBC_RAW: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 96 | alg_name = "cbc(des)"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | break; |
| 98 | default: |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 99 | printk("gss_kerberos_mech: unsupported algorithm %d\n", alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | goto out_err_free_key; |
| 101 | } |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 102 | *res = crypto_alloc_blkcipher(alg_name, 0, CRYPTO_ALG_ASYNC); |
| 103 | if (IS_ERR(*res)) { |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 104 | printk("gss_kerberos_mech: unable to initialize crypto algorithm %s\n", alg_name); |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 105 | *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | goto out_err_free_key; |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 107 | } |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 108 | if (crypto_blkcipher_setkey(*res, key.data, key.len)) { |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 109 | printk("gss_kerberos_mech: error setting key for crypto algorithm %s\n", alg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | goto out_err_free_tfm; |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | kfree(key.data); |
| 114 | return p; |
| 115 | |
| 116 | out_err_free_tfm: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 117 | crypto_free_blkcipher(*res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | out_err_free_key: |
| 119 | kfree(key.data); |
| 120 | p = ERR_PTR(-EINVAL); |
| 121 | out_err: |
| 122 | return p; |
| 123 | } |
| 124 | |
| 125 | static int |
| 126 | gss_import_sec_context_kerberos(const void *p, |
| 127 | size_t len, |
| 128 | struct gss_ctx *ctx_id) |
| 129 | { |
| 130 | const void *end = (const void *)((const char *)p + len); |
| 131 | struct krb5_ctx *ctx; |
J. Bruce Fields | e678e06 | 2006-12-04 20:22:35 -0500 | [diff] [blame] | 132 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Trond Myklebust | 14ace02 | 2009-12-18 16:28:05 -0500 | [diff] [blame] | 134 | if (!(ctx = kzalloc(sizeof(*ctx), GFP_NOFS))) { |
| 135 | p = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | goto out_err; |
Trond Myklebust | 14ace02 | 2009-12-18 16:28:05 -0500 | [diff] [blame] | 137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate)); |
| 140 | if (IS_ERR(p)) |
| 141 | goto out_err_free_ctx; |
J. Bruce Fields | 717757a | 2006-12-04 20:22:41 -0500 | [diff] [blame] | 142 | /* The downcall format was designed before we completely understood |
| 143 | * the uses of the context fields; so it includes some stuff we |
| 144 | * just give some minimal sanity-checking, and some we ignore |
| 145 | * completely (like the next twenty bytes): */ |
| 146 | if (unlikely(p + 20 > end || p + 20 < p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | goto out_err_free_ctx; |
J. Bruce Fields | 717757a | 2006-12-04 20:22:41 -0500 | [diff] [blame] | 148 | p += 20; |
J. Bruce Fields | e678e06 | 2006-12-04 20:22:35 -0500 | [diff] [blame] | 149 | p = simple_get_bytes(p, end, &tmp, sizeof(tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (IS_ERR(p)) |
| 151 | goto out_err_free_ctx; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 152 | if (tmp != SGN_ALG_DES_MAC_MD5) { |
| 153 | p = ERR_PTR(-ENOSYS); |
J. Bruce Fields | e678e06 | 2006-12-04 20:22:35 -0500 | [diff] [blame] | 154 | goto out_err_free_ctx; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 155 | } |
J. Bruce Fields | d922a84 | 2006-12-04 20:22:40 -0500 | [diff] [blame] | 156 | p = simple_get_bytes(p, end, &tmp, sizeof(tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (IS_ERR(p)) |
| 158 | goto out_err_free_ctx; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 159 | if (tmp != SEAL_ALG_DES) { |
| 160 | p = ERR_PTR(-ENOSYS); |
J. Bruce Fields | d922a84 | 2006-12-04 20:22:40 -0500 | [diff] [blame] | 161 | goto out_err_free_ctx; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime)); |
| 164 | if (IS_ERR(p)) |
| 165 | goto out_err_free_ctx; |
| 166 | p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send)); |
| 167 | if (IS_ERR(p)) |
| 168 | goto out_err_free_ctx; |
| 169 | p = simple_get_netobj(p, end, &ctx->mech_used); |
| 170 | if (IS_ERR(p)) |
| 171 | goto out_err_free_ctx; |
| 172 | p = get_key(p, end, &ctx->enc); |
| 173 | if (IS_ERR(p)) |
| 174 | goto out_err_free_mech; |
| 175 | p = get_key(p, end, &ctx->seq); |
| 176 | if (IS_ERR(p)) |
| 177 | goto out_err_free_key1; |
| 178 | if (p != end) { |
| 179 | p = ERR_PTR(-EFAULT); |
| 180 | goto out_err_free_key2; |
| 181 | } |
| 182 | |
| 183 | ctx_id->internal_ctx_id = ctx; |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 184 | |
| 185 | dprintk("RPC: Successfully imported new context.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | |
| 188 | out_err_free_key2: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 189 | crypto_free_blkcipher(ctx->seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | out_err_free_key1: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 191 | crypto_free_blkcipher(ctx->enc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | out_err_free_mech: |
| 193 | kfree(ctx->mech_used.data); |
| 194 | out_err_free_ctx: |
| 195 | kfree(ctx); |
| 196 | out_err: |
| 197 | return PTR_ERR(p); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | gss_delete_sec_context_kerberos(void *internal_ctx) { |
| 202 | struct krb5_ctx *kctx = internal_ctx; |
| 203 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 204 | crypto_free_blkcipher(kctx->seq); |
| 205 | crypto_free_blkcipher(kctx->enc); |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 206 | kfree(kctx->mech_used.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | kfree(kctx); |
| 208 | } |
| 209 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 210 | static const struct gss_api_ops gss_kerberos_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | .gss_import_sec_context = gss_import_sec_context_kerberos, |
| 212 | .gss_get_mic = gss_get_mic_kerberos, |
| 213 | .gss_verify_mic = gss_verify_mic_kerberos, |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 214 | .gss_wrap = gss_wrap_kerberos, |
| 215 | .gss_unwrap = gss_unwrap_kerberos, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | .gss_delete_sec_context = gss_delete_sec_context_kerberos, |
| 217 | }; |
| 218 | |
| 219 | static struct pf_desc gss_kerberos_pfs[] = { |
| 220 | [0] = { |
| 221 | .pseudoflavor = RPC_AUTH_GSS_KRB5, |
| 222 | .service = RPC_GSS_SVC_NONE, |
| 223 | .name = "krb5", |
| 224 | }, |
| 225 | [1] = { |
| 226 | .pseudoflavor = RPC_AUTH_GSS_KRB5I, |
| 227 | .service = RPC_GSS_SVC_INTEGRITY, |
| 228 | .name = "krb5i", |
| 229 | }, |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 230 | [2] = { |
| 231 | .pseudoflavor = RPC_AUTH_GSS_KRB5P, |
| 232 | .service = RPC_GSS_SVC_PRIVACY, |
| 233 | .name = "krb5p", |
| 234 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | static struct gss_api_mech gss_kerberos_mech = { |
| 238 | .gm_name = "krb5", |
| 239 | .gm_owner = THIS_MODULE, |
Usha Ketineni | ae4c40b | 2007-07-17 04:04:50 -0700 | [diff] [blame] | 240 | .gm_oid = {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | .gm_ops = &gss_kerberos_ops, |
| 242 | .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs), |
| 243 | .gm_pfs = gss_kerberos_pfs, |
| 244 | }; |
| 245 | |
| 246 | static int __init init_kerberos_module(void) |
| 247 | { |
| 248 | int status; |
| 249 | |
| 250 | status = gss_mech_register(&gss_kerberos_mech); |
| 251 | if (status) |
| 252 | printk("Failed to register kerberos gss mechanism!\n"); |
| 253 | return status; |
| 254 | } |
| 255 | |
| 256 | static void __exit cleanup_kerberos_module(void) |
| 257 | { |
| 258 | gss_mech_unregister(&gss_kerberos_mech); |
| 259 | } |
| 260 | |
| 261 | MODULE_LICENSE("GPL"); |
| 262 | module_init(init_kerberos_module); |
| 263 | module_exit(cleanup_kerberos_module); |