Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/gss_krb5_mech.c |
| 3 | * |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 4 | * Copyright (c) 2001-2008 The Regents of the University of Michigan. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 51 | static struct gss_api_mech gss_kerberos_mech; /* forward declaration */ |
| 52 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 53 | static const struct gss_krb5_enctype supported_gss_krb5_enctypes[] = { |
| 54 | /* |
| 55 | * DES (All DES enctypes are mapped to the same gss functionality) |
| 56 | */ |
| 57 | { |
| 58 | .etype = ENCTYPE_DES_CBC_RAW, |
| 59 | .ctype = CKSUMTYPE_RSA_MD5, |
| 60 | .name = "des-cbc-crc", |
| 61 | .encrypt_name = "cbc(des)", |
| 62 | .cksum_name = "md5", |
| 63 | .encrypt = krb5_encrypt, |
| 64 | .decrypt = krb5_decrypt, |
Kevin Coffman | 4891f2d | 2010-03-17 13:02:53 -0400 | [diff] [blame] | 65 | .mk_key = NULL, |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 66 | .signalg = SGN_ALG_DES_MAC_MD5, |
| 67 | .sealalg = SEAL_ALG_DES, |
| 68 | .keybytes = 7, |
| 69 | .keylength = 8, |
| 70 | .blocksize = 8, |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 71 | .conflen = 8, |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 72 | .cksumlength = 8, |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 73 | .keyed_cksum = 0, |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 74 | }, |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 75 | /* |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 76 | * RC4-HMAC |
| 77 | */ |
| 78 | { |
| 79 | .etype = ENCTYPE_ARCFOUR_HMAC, |
| 80 | .ctype = CKSUMTYPE_HMAC_MD5_ARCFOUR, |
| 81 | .name = "rc4-hmac", |
| 82 | .encrypt_name = "ecb(arc4)", |
| 83 | .cksum_name = "hmac(md5)", |
| 84 | .encrypt = krb5_encrypt, |
| 85 | .decrypt = krb5_decrypt, |
| 86 | .mk_key = NULL, |
| 87 | .signalg = SGN_ALG_HMAC_MD5, |
| 88 | .sealalg = SEAL_ALG_MICROSOFT_RC4, |
| 89 | .keybytes = 16, |
| 90 | .keylength = 16, |
| 91 | .blocksize = 1, |
| 92 | .conflen = 8, |
| 93 | .cksumlength = 8, |
| 94 | .keyed_cksum = 1, |
| 95 | }, |
| 96 | /* |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 97 | * 3DES |
| 98 | */ |
| 99 | { |
| 100 | .etype = ENCTYPE_DES3_CBC_RAW, |
| 101 | .ctype = CKSUMTYPE_HMAC_SHA1_DES3, |
| 102 | .name = "des3-hmac-sha1", |
| 103 | .encrypt_name = "cbc(des3_ede)", |
| 104 | .cksum_name = "hmac(sha1)", |
| 105 | .encrypt = krb5_encrypt, |
| 106 | .decrypt = krb5_decrypt, |
| 107 | .mk_key = gss_krb5_des3_make_key, |
| 108 | .signalg = SGN_ALG_HMAC_SHA1_DES3_KD, |
| 109 | .sealalg = SEAL_ALG_DES3KD, |
| 110 | .keybytes = 21, |
| 111 | .keylength = 24, |
| 112 | .blocksize = 8, |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 113 | .conflen = 8, |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 114 | .cksumlength = 20, |
| 115 | .keyed_cksum = 1, |
| 116 | }, |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 117 | /* |
| 118 | * AES128 |
| 119 | */ |
| 120 | { |
| 121 | .etype = ENCTYPE_AES128_CTS_HMAC_SHA1_96, |
| 122 | .ctype = CKSUMTYPE_HMAC_SHA1_96_AES128, |
| 123 | .name = "aes128-cts", |
| 124 | .encrypt_name = "cts(cbc(aes))", |
| 125 | .cksum_name = "hmac(sha1)", |
| 126 | .encrypt = krb5_encrypt, |
| 127 | .decrypt = krb5_decrypt, |
| 128 | .mk_key = gss_krb5_aes_make_key, |
| 129 | .encrypt_v2 = gss_krb5_aes_encrypt, |
| 130 | .decrypt_v2 = gss_krb5_aes_decrypt, |
| 131 | .signalg = -1, |
| 132 | .sealalg = -1, |
| 133 | .keybytes = 16, |
| 134 | .keylength = 16, |
| 135 | .blocksize = 16, |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 136 | .conflen = 16, |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 137 | .cksumlength = 12, |
| 138 | .keyed_cksum = 1, |
| 139 | }, |
| 140 | /* |
| 141 | * AES256 |
| 142 | */ |
| 143 | { |
| 144 | .etype = ENCTYPE_AES256_CTS_HMAC_SHA1_96, |
| 145 | .ctype = CKSUMTYPE_HMAC_SHA1_96_AES256, |
| 146 | .name = "aes256-cts", |
| 147 | .encrypt_name = "cts(cbc(aes))", |
| 148 | .cksum_name = "hmac(sha1)", |
| 149 | .encrypt = krb5_encrypt, |
| 150 | .decrypt = krb5_decrypt, |
| 151 | .mk_key = gss_krb5_aes_make_key, |
| 152 | .encrypt_v2 = gss_krb5_aes_encrypt, |
| 153 | .decrypt_v2 = gss_krb5_aes_decrypt, |
| 154 | .signalg = -1, |
| 155 | .sealalg = -1, |
| 156 | .keybytes = 32, |
| 157 | .keylength = 32, |
| 158 | .blocksize = 16, |
Kevin Coffman | 5af4654 | 2010-03-17 13:03:05 -0400 | [diff] [blame] | 159 | .conflen = 16, |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 160 | .cksumlength = 12, |
| 161 | .keyed_cksum = 1, |
| 162 | }, |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static const int num_supported_enctypes = |
| 166 | ARRAY_SIZE(supported_gss_krb5_enctypes); |
| 167 | |
| 168 | static int |
| 169 | supported_gss_krb5_enctype(int etype) |
| 170 | { |
| 171 | int i; |
| 172 | for (i = 0; i < num_supported_enctypes; i++) |
| 173 | if (supported_gss_krb5_enctypes[i].etype == etype) |
| 174 | return 1; |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static const struct gss_krb5_enctype * |
| 179 | get_gss_krb5_enctype(int etype) |
| 180 | { |
| 181 | int i; |
| 182 | for (i = 0; i < num_supported_enctypes; i++) |
| 183 | if (supported_gss_krb5_enctypes[i].etype == etype) |
| 184 | return &supported_gss_krb5_enctypes[i]; |
| 185 | return NULL; |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static const void * |
| 189 | simple_get_bytes(const void *p, const void *end, void *res, int len) |
| 190 | { |
| 191 | const void *q = (const void *)((const char *)p + len); |
| 192 | if (unlikely(q > end || q < p)) |
| 193 | return ERR_PTR(-EFAULT); |
| 194 | memcpy(res, p, len); |
| 195 | return q; |
| 196 | } |
| 197 | |
| 198 | static const void * |
| 199 | simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res) |
| 200 | { |
| 201 | const void *q; |
| 202 | unsigned int len; |
| 203 | |
| 204 | p = simple_get_bytes(p, end, &len, sizeof(len)); |
| 205 | if (IS_ERR(p)) |
| 206 | return p; |
| 207 | q = (const void *)((const char *)p + len); |
| 208 | if (unlikely(q > end || q < p)) |
| 209 | return ERR_PTR(-EFAULT); |
Trond Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 210 | res->data = kmemdup(p, len, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (unlikely(res->data == NULL)) |
| 212 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | res->len = len; |
| 214 | return q; |
| 215 | } |
| 216 | |
| 217 | static inline const void * |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 218 | get_key(const void *p, const void *end, |
| 219 | struct krb5_ctx *ctx, struct crypto_blkcipher **res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | struct xdr_netobj key; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 222 | int alg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | p = simple_get_bytes(p, end, &alg, sizeof(alg)); |
| 225 | if (IS_ERR(p)) |
| 226 | goto out_err; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 227 | |
| 228 | switch (alg) { |
| 229 | case ENCTYPE_DES_CBC_CRC: |
| 230 | case ENCTYPE_DES_CBC_MD4: |
| 231 | case ENCTYPE_DES_CBC_MD5: |
| 232 | /* Map all these key types to ENCTYPE_DES_CBC_RAW */ |
| 233 | alg = ENCTYPE_DES_CBC_RAW; |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | if (!supported_gss_krb5_enctype(alg)) { |
| 238 | printk(KERN_WARNING "gss_kerberos_mech: unsupported " |
| 239 | "encryption key algorithm %d\n", alg); |
| 240 | goto out_err; |
| 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | p = simple_get_netobj(p, end, &key); |
| 243 | if (IS_ERR(p)) |
| 244 | goto out_err; |
| 245 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 246 | *res = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0, |
| 247 | CRYPTO_ALG_ASYNC); |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 248 | if (IS_ERR(*res)) { |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 249 | printk(KERN_WARNING "gss_kerberos_mech: unable to initialize " |
| 250 | "crypto algorithm %s\n", ctx->gk5e->encrypt_name); |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 251 | *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | goto out_err_free_key; |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 253 | } |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 254 | if (crypto_blkcipher_setkey(*res, key.data, key.len)) { |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 255 | printk(KERN_WARNING "gss_kerberos_mech: error setting key for " |
| 256 | "crypto algorithm %s\n", ctx->gk5e->encrypt_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto out_err_free_tfm; |
J. Bruce Fields | 9e56904 | 2006-01-03 09:56:01 +0100 | [diff] [blame] | 258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | kfree(key.data); |
| 261 | return p; |
| 262 | |
| 263 | out_err_free_tfm: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 264 | crypto_free_blkcipher(*res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | out_err_free_key: |
| 266 | kfree(key.data); |
| 267 | p = ERR_PTR(-EINVAL); |
| 268 | out_err: |
| 269 | return p; |
| 270 | } |
| 271 | |
| 272 | static int |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 273 | gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
J. Bruce Fields | e678e06 | 2006-12-04 20:22:35 -0500 | [diff] [blame] | 275 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate)); |
| 278 | if (IS_ERR(p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 279 | goto out_err; |
| 280 | |
| 281 | /* Old format supports only DES! Any other enctype uses new format */ |
Kevin Coffman | 1ac3719 | 2010-03-17 13:02:49 -0400 | [diff] [blame] | 282 | ctx->enctype = ENCTYPE_DES_CBC_RAW; |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 283 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 284 | ctx->gk5e = get_gss_krb5_enctype(ctx->enctype); |
| 285 | if (ctx->gk5e == NULL) |
| 286 | goto out_err; |
| 287 | |
J. Bruce Fields | 717757a | 2006-12-04 20:22:41 -0500 | [diff] [blame] | 288 | /* The downcall format was designed before we completely understood |
| 289 | * the uses of the context fields; so it includes some stuff we |
| 290 | * just give some minimal sanity-checking, and some we ignore |
| 291 | * completely (like the next twenty bytes): */ |
| 292 | if (unlikely(p + 20 > end || p + 20 < p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 293 | goto out_err; |
J. Bruce Fields | 717757a | 2006-12-04 20:22:41 -0500 | [diff] [blame] | 294 | p += 20; |
J. Bruce Fields | e678e06 | 2006-12-04 20:22:35 -0500 | [diff] [blame] | 295 | p = simple_get_bytes(p, end, &tmp, sizeof(tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (IS_ERR(p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 297 | goto out_err; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 298 | if (tmp != SGN_ALG_DES_MAC_MD5) { |
| 299 | p = ERR_PTR(-ENOSYS); |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 300 | goto out_err; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 301 | } |
J. Bruce Fields | d922a84 | 2006-12-04 20:22:40 -0500 | [diff] [blame] | 302 | p = simple_get_bytes(p, end, &tmp, sizeof(tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (IS_ERR(p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 304 | goto out_err; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 305 | if (tmp != SEAL_ALG_DES) { |
| 306 | p = ERR_PTR(-ENOSYS); |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 307 | goto out_err; |
Kevin Coffman | ef338be | 2007-11-09 18:42:09 -0500 | [diff] [blame] | 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime)); |
| 310 | if (IS_ERR(p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 311 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send)); |
| 313 | if (IS_ERR(p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 314 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | p = simple_get_netobj(p, end, &ctx->mech_used); |
| 316 | if (IS_ERR(p)) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 317 | goto out_err; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 318 | p = get_key(p, end, ctx, &ctx->enc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (IS_ERR(p)) |
| 320 | goto out_err_free_mech; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 321 | p = get_key(p, end, ctx, &ctx->seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (IS_ERR(p)) |
| 323 | goto out_err_free_key1; |
| 324 | if (p != end) { |
| 325 | p = ERR_PTR(-EFAULT); |
| 326 | goto out_err_free_key2; |
| 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | |
| 331 | out_err_free_key2: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 332 | crypto_free_blkcipher(ctx->seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | out_err_free_key1: |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 334 | crypto_free_blkcipher(ctx->enc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | out_err_free_mech: |
| 336 | kfree(ctx->mech_used.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | out_err: |
| 338 | return PTR_ERR(p); |
| 339 | } |
| 340 | |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 341 | struct crypto_blkcipher * |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 342 | context_v2_alloc_cipher(struct krb5_ctx *ctx, const char *cname, u8 *key) |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 343 | { |
| 344 | struct crypto_blkcipher *cp; |
| 345 | |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 346 | cp = crypto_alloc_blkcipher(cname, 0, CRYPTO_ALG_ASYNC); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 347 | if (IS_ERR(cp)) { |
| 348 | dprintk("gss_kerberos_mech: unable to initialize " |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 349 | "crypto algorithm %s\n", cname); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | if (crypto_blkcipher_setkey(cp, key, ctx->gk5e->keylength)) { |
| 353 | dprintk("gss_kerberos_mech: error setting key for " |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 354 | "crypto algorithm %s\n", cname); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 355 | crypto_free_blkcipher(cp); |
| 356 | return NULL; |
| 357 | } |
| 358 | return cp; |
| 359 | } |
| 360 | |
| 361 | static inline void |
| 362 | set_cdata(u8 cdata[GSS_KRB5_K5CLENGTH], u32 usage, u8 seed) |
| 363 | { |
| 364 | cdata[0] = (usage>>24)&0xff; |
| 365 | cdata[1] = (usage>>16)&0xff; |
| 366 | cdata[2] = (usage>>8)&0xff; |
| 367 | cdata[3] = usage&0xff; |
| 368 | cdata[4] = seed; |
| 369 | } |
| 370 | |
| 371 | static int |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 372 | context_derive_keys_des3(struct krb5_ctx *ctx, gfp_t gfp_mask) |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 373 | { |
| 374 | struct xdr_netobj c, keyin, keyout; |
| 375 | u8 cdata[GSS_KRB5_K5CLENGTH]; |
| 376 | u32 err; |
| 377 | |
| 378 | c.len = GSS_KRB5_K5CLENGTH; |
| 379 | c.data = cdata; |
| 380 | |
Kevin Coffman | fc263a9 | 2010-03-17 13:03:03 -0400 | [diff] [blame] | 381 | keyin.data = ctx->Ksess; |
| 382 | keyin.len = ctx->gk5e->keylength; |
| 383 | keyout.len = ctx->gk5e->keylength; |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 384 | |
| 385 | /* seq uses the raw key */ |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 386 | ctx->seq = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name, |
Kevin Coffman | fc263a9 | 2010-03-17 13:03:03 -0400 | [diff] [blame] | 387 | ctx->Ksess); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 388 | if (ctx->seq == NULL) |
| 389 | goto out_err; |
| 390 | |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 391 | ctx->enc = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name, |
Kevin Coffman | fc263a9 | 2010-03-17 13:03:03 -0400 | [diff] [blame] | 392 | ctx->Ksess); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 393 | if (ctx->enc == NULL) |
| 394 | goto out_free_seq; |
| 395 | |
| 396 | /* derive cksum */ |
| 397 | set_cdata(cdata, KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM); |
| 398 | keyout.data = ctx->cksum; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 399 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 400 | if (err) { |
| 401 | dprintk("%s: Error %d deriving cksum key\n", |
| 402 | __func__, err); |
| 403 | goto out_free_enc; |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | |
| 408 | out_free_enc: |
| 409 | crypto_free_blkcipher(ctx->enc); |
| 410 | out_free_seq: |
| 411 | crypto_free_blkcipher(ctx->seq); |
| 412 | out_err: |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 416 | /* |
| 417 | * Note that RC4 depends on deriving keys using the sequence |
| 418 | * number or the checksum of a token. Therefore, the final keys |
| 419 | * cannot be calculated until the token is being constructed! |
| 420 | */ |
| 421 | static int |
| 422 | context_derive_keys_rc4(struct krb5_ctx *ctx) |
| 423 | { |
| 424 | struct crypto_hash *hmac; |
| 425 | char sigkeyconstant[] = "signaturekey"; |
| 426 | int slen = strlen(sigkeyconstant) + 1; /* include null terminator */ |
| 427 | struct hash_desc desc; |
| 428 | struct scatterlist sg[1]; |
| 429 | int err; |
| 430 | |
| 431 | dprintk("RPC: %s: entered\n", __func__); |
| 432 | /* |
| 433 | * derive cksum (aka Ksign) key |
| 434 | */ |
| 435 | hmac = crypto_alloc_hash(ctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC); |
| 436 | if (IS_ERR(hmac)) { |
| 437 | dprintk("%s: error %ld allocating hash '%s'\n", |
| 438 | __func__, PTR_ERR(hmac), ctx->gk5e->cksum_name); |
| 439 | err = PTR_ERR(hmac); |
| 440 | goto out_err; |
| 441 | } |
| 442 | |
| 443 | err = crypto_hash_setkey(hmac, ctx->Ksess, ctx->gk5e->keylength); |
| 444 | if (err) |
| 445 | goto out_err_free_hmac; |
| 446 | |
| 447 | sg_init_table(sg, 1); |
| 448 | sg_set_buf(sg, sigkeyconstant, slen); |
| 449 | |
| 450 | desc.tfm = hmac; |
| 451 | desc.flags = 0; |
| 452 | |
| 453 | err = crypto_hash_init(&desc); |
| 454 | if (err) |
| 455 | goto out_err_free_hmac; |
| 456 | |
| 457 | err = crypto_hash_digest(&desc, sg, slen, ctx->cksum); |
| 458 | if (err) |
| 459 | goto out_err_free_hmac; |
| 460 | /* |
| 461 | * allocate hash, and blkciphers for data and seqnum encryption |
| 462 | */ |
| 463 | ctx->enc = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0, |
| 464 | CRYPTO_ALG_ASYNC); |
| 465 | if (IS_ERR(ctx->enc)) { |
| 466 | err = PTR_ERR(ctx->enc); |
| 467 | goto out_err_free_hmac; |
| 468 | } |
| 469 | |
| 470 | ctx->seq = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0, |
| 471 | CRYPTO_ALG_ASYNC); |
| 472 | if (IS_ERR(ctx->seq)) { |
| 473 | crypto_free_blkcipher(ctx->enc); |
| 474 | err = PTR_ERR(ctx->seq); |
| 475 | goto out_err_free_hmac; |
| 476 | } |
| 477 | |
| 478 | dprintk("RPC: %s: returning success\n", __func__); |
| 479 | |
| 480 | err = 0; |
| 481 | |
| 482 | out_err_free_hmac: |
| 483 | crypto_free_hash(hmac); |
| 484 | out_err: |
| 485 | dprintk("RPC: %s: returning %d\n", __func__, err); |
| 486 | return err; |
| 487 | } |
| 488 | |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 489 | static int |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 490 | context_derive_keys_new(struct krb5_ctx *ctx, gfp_t gfp_mask) |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 491 | { |
| 492 | struct xdr_netobj c, keyin, keyout; |
| 493 | u8 cdata[GSS_KRB5_K5CLENGTH]; |
| 494 | u32 err; |
| 495 | |
| 496 | c.len = GSS_KRB5_K5CLENGTH; |
| 497 | c.data = cdata; |
| 498 | |
Kevin Coffman | fc263a9 | 2010-03-17 13:03:03 -0400 | [diff] [blame] | 499 | keyin.data = ctx->Ksess; |
| 500 | keyin.len = ctx->gk5e->keylength; |
| 501 | keyout.len = ctx->gk5e->keylength; |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 502 | |
| 503 | /* initiator seal encryption */ |
| 504 | set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_ENCRYPTION); |
| 505 | keyout.data = ctx->initiator_seal; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 506 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 507 | if (err) { |
| 508 | dprintk("%s: Error %d deriving initiator_seal key\n", |
| 509 | __func__, err); |
| 510 | goto out_err; |
| 511 | } |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 512 | ctx->initiator_enc = context_v2_alloc_cipher(ctx, |
| 513 | ctx->gk5e->encrypt_name, |
| 514 | ctx->initiator_seal); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 515 | if (ctx->initiator_enc == NULL) |
| 516 | goto out_err; |
| 517 | |
| 518 | /* acceptor seal encryption */ |
| 519 | set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_ENCRYPTION); |
| 520 | keyout.data = ctx->acceptor_seal; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 521 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 522 | if (err) { |
| 523 | dprintk("%s: Error %d deriving acceptor_seal key\n", |
| 524 | __func__, err); |
| 525 | goto out_free_initiator_enc; |
| 526 | } |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 527 | ctx->acceptor_enc = context_v2_alloc_cipher(ctx, |
| 528 | ctx->gk5e->encrypt_name, |
| 529 | ctx->acceptor_seal); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 530 | if (ctx->acceptor_enc == NULL) |
| 531 | goto out_free_initiator_enc; |
| 532 | |
| 533 | /* initiator sign checksum */ |
| 534 | set_cdata(cdata, KG_USAGE_INITIATOR_SIGN, KEY_USAGE_SEED_CHECKSUM); |
| 535 | keyout.data = ctx->initiator_sign; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 536 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 537 | if (err) { |
| 538 | dprintk("%s: Error %d deriving initiator_sign key\n", |
| 539 | __func__, err); |
| 540 | goto out_free_acceptor_enc; |
| 541 | } |
| 542 | |
| 543 | /* acceptor sign checksum */ |
| 544 | set_cdata(cdata, KG_USAGE_ACCEPTOR_SIGN, KEY_USAGE_SEED_CHECKSUM); |
| 545 | keyout.data = ctx->acceptor_sign; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 546 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 547 | if (err) { |
| 548 | dprintk("%s: Error %d deriving acceptor_sign key\n", |
| 549 | __func__, err); |
| 550 | goto out_free_acceptor_enc; |
| 551 | } |
| 552 | |
| 553 | /* initiator seal integrity */ |
| 554 | set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_INTEGRITY); |
| 555 | keyout.data = ctx->initiator_integ; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 556 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 557 | if (err) { |
| 558 | dprintk("%s: Error %d deriving initiator_integ key\n", |
| 559 | __func__, err); |
| 560 | goto out_free_acceptor_enc; |
| 561 | } |
| 562 | |
| 563 | /* acceptor seal integrity */ |
| 564 | set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_INTEGRITY); |
| 565 | keyout.data = ctx->acceptor_integ; |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 566 | err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 567 | if (err) { |
| 568 | dprintk("%s: Error %d deriving acceptor_integ key\n", |
| 569 | __func__, err); |
| 570 | goto out_free_acceptor_enc; |
| 571 | } |
| 572 | |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 573 | switch (ctx->enctype) { |
| 574 | case ENCTYPE_AES128_CTS_HMAC_SHA1_96: |
| 575 | case ENCTYPE_AES256_CTS_HMAC_SHA1_96: |
| 576 | ctx->initiator_enc_aux = |
| 577 | context_v2_alloc_cipher(ctx, "cbc(aes)", |
| 578 | ctx->initiator_seal); |
| 579 | if (ctx->initiator_enc_aux == NULL) |
| 580 | goto out_free_acceptor_enc; |
| 581 | ctx->acceptor_enc_aux = |
| 582 | context_v2_alloc_cipher(ctx, "cbc(aes)", |
| 583 | ctx->acceptor_seal); |
| 584 | if (ctx->acceptor_enc_aux == NULL) { |
| 585 | crypto_free_blkcipher(ctx->initiator_enc_aux); |
| 586 | goto out_free_acceptor_enc; |
| 587 | } |
| 588 | } |
| 589 | |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 590 | return 0; |
| 591 | |
| 592 | out_free_acceptor_enc: |
| 593 | crypto_free_blkcipher(ctx->acceptor_enc); |
| 594 | out_free_initiator_enc: |
| 595 | crypto_free_blkcipher(ctx->initiator_enc); |
| 596 | out_err: |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | static int |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 601 | gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx, |
| 602 | gfp_t gfp_mask) |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 603 | { |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 604 | int keylen; |
| 605 | |
| 606 | p = simple_get_bytes(p, end, &ctx->flags, sizeof(ctx->flags)); |
| 607 | if (IS_ERR(p)) |
| 608 | goto out_err; |
| 609 | ctx->initiate = ctx->flags & KRB5_CTX_FLAG_INITIATOR; |
| 610 | |
| 611 | p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime)); |
| 612 | if (IS_ERR(p)) |
| 613 | goto out_err; |
| 614 | p = simple_get_bytes(p, end, &ctx->seq_send64, sizeof(ctx->seq_send64)); |
| 615 | if (IS_ERR(p)) |
| 616 | goto out_err; |
| 617 | /* set seq_send for use by "older" enctypes */ |
| 618 | ctx->seq_send = ctx->seq_send64; |
| 619 | if (ctx->seq_send64 != ctx->seq_send) { |
| 620 | dprintk("%s: seq_send64 %lx, seq_send %x overflow?\n", __func__, |
| 621 | (long unsigned)ctx->seq_send64, ctx->seq_send); |
| 622 | goto out_err; |
| 623 | } |
| 624 | p = simple_get_bytes(p, end, &ctx->enctype, sizeof(ctx->enctype)); |
| 625 | if (IS_ERR(p)) |
| 626 | goto out_err; |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 627 | /* Map ENCTYPE_DES3_CBC_SHA1 to ENCTYPE_DES3_CBC_RAW */ |
| 628 | if (ctx->enctype == ENCTYPE_DES3_CBC_SHA1) |
| 629 | ctx->enctype = ENCTYPE_DES3_CBC_RAW; |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 630 | ctx->gk5e = get_gss_krb5_enctype(ctx->enctype); |
| 631 | if (ctx->gk5e == NULL) { |
| 632 | dprintk("gss_kerberos_mech: unsupported krb5 enctype %u\n", |
| 633 | ctx->enctype); |
| 634 | p = ERR_PTR(-EINVAL); |
| 635 | goto out_err; |
| 636 | } |
| 637 | keylen = ctx->gk5e->keylength; |
| 638 | |
Kevin Coffman | fc263a9 | 2010-03-17 13:03:03 -0400 | [diff] [blame] | 639 | p = simple_get_bytes(p, end, ctx->Ksess, keylen); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 640 | if (IS_ERR(p)) |
| 641 | goto out_err; |
| 642 | |
| 643 | if (p != end) { |
| 644 | p = ERR_PTR(-EINVAL); |
| 645 | goto out_err; |
| 646 | } |
| 647 | |
| 648 | ctx->mech_used.data = kmemdup(gss_kerberos_mech.gm_oid.data, |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 649 | gss_kerberos_mech.gm_oid.len, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 650 | if (unlikely(ctx->mech_used.data == NULL)) { |
| 651 | p = ERR_PTR(-ENOMEM); |
| 652 | goto out_err; |
| 653 | } |
| 654 | ctx->mech_used.len = gss_kerberos_mech.gm_oid.len; |
| 655 | |
| 656 | switch (ctx->enctype) { |
| 657 | case ENCTYPE_DES3_CBC_RAW: |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 658 | return context_derive_keys_des3(ctx, gfp_mask); |
Kevin Coffman | fffdaef | 2010-03-17 13:03:06 -0400 | [diff] [blame] | 659 | case ENCTYPE_ARCFOUR_HMAC: |
| 660 | return context_derive_keys_rc4(ctx); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 661 | case ENCTYPE_AES128_CTS_HMAC_SHA1_96: |
| 662 | case ENCTYPE_AES256_CTS_HMAC_SHA1_96: |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 663 | return context_derive_keys_new(ctx, gfp_mask); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 664 | default: |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | |
| 668 | out_err: |
| 669 | return PTR_ERR(p); |
| 670 | } |
| 671 | |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 672 | static int |
| 673 | gss_import_sec_context_kerberos(const void *p, size_t len, |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 674 | struct gss_ctx *ctx_id, |
| 675 | gfp_t gfp_mask) |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 676 | { |
| 677 | const void *end = (const void *)((const char *)p + len); |
| 678 | struct krb5_ctx *ctx; |
| 679 | int ret; |
| 680 | |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 681 | ctx = kzalloc(sizeof(*ctx), gfp_mask); |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 682 | if (ctx == NULL) |
| 683 | return -ENOMEM; |
| 684 | |
| 685 | if (len == 85) |
| 686 | ret = gss_import_v1_context(p, end, ctx); |
| 687 | else |
Trond Myklebust | 1f4c86c | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 688 | ret = gss_import_v2_context(p, end, ctx, gfp_mask); |
Kevin Coffman | a8cc1cb | 2010-03-17 13:02:50 -0400 | [diff] [blame] | 689 | |
| 690 | if (ret == 0) |
| 691 | ctx_id->internal_ctx_id = ctx; |
| 692 | else |
| 693 | kfree(ctx); |
| 694 | |
| 695 | dprintk("RPC: %s: returning %d\n", __func__, ret); |
| 696 | return ret; |
| 697 | } |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | static void |
| 700 | gss_delete_sec_context_kerberos(void *internal_ctx) { |
| 701 | struct krb5_ctx *kctx = internal_ctx; |
| 702 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 703 | crypto_free_blkcipher(kctx->seq); |
| 704 | crypto_free_blkcipher(kctx->enc); |
Kevin Coffman | 47d8480 | 2010-03-17 13:02:54 -0400 | [diff] [blame] | 705 | crypto_free_blkcipher(kctx->acceptor_enc); |
| 706 | crypto_free_blkcipher(kctx->initiator_enc); |
Kevin Coffman | 934a95a | 2010-03-17 13:03:00 -0400 | [diff] [blame] | 707 | crypto_free_blkcipher(kctx->acceptor_enc_aux); |
| 708 | crypto_free_blkcipher(kctx->initiator_enc_aux); |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 709 | kfree(kctx->mech_used.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | kfree(kctx); |
| 711 | } |
| 712 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 713 | static const struct gss_api_ops gss_kerberos_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | .gss_import_sec_context = gss_import_sec_context_kerberos, |
| 715 | .gss_get_mic = gss_get_mic_kerberos, |
| 716 | .gss_verify_mic = gss_verify_mic_kerberos, |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 717 | .gss_wrap = gss_wrap_kerberos, |
| 718 | .gss_unwrap = gss_unwrap_kerberos, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | .gss_delete_sec_context = gss_delete_sec_context_kerberos, |
| 720 | }; |
| 721 | |
| 722 | static struct pf_desc gss_kerberos_pfs[] = { |
| 723 | [0] = { |
| 724 | .pseudoflavor = RPC_AUTH_GSS_KRB5, |
| 725 | .service = RPC_GSS_SVC_NONE, |
| 726 | .name = "krb5", |
| 727 | }, |
| 728 | [1] = { |
| 729 | .pseudoflavor = RPC_AUTH_GSS_KRB5I, |
| 730 | .service = RPC_GSS_SVC_INTEGRITY, |
| 731 | .name = "krb5i", |
| 732 | }, |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 733 | [2] = { |
| 734 | .pseudoflavor = RPC_AUTH_GSS_KRB5P, |
| 735 | .service = RPC_GSS_SVC_PRIVACY, |
| 736 | .name = "krb5p", |
| 737 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | }; |
| 739 | |
| 740 | static struct gss_api_mech gss_kerberos_mech = { |
| 741 | .gm_name = "krb5", |
| 742 | .gm_owner = THIS_MODULE, |
Usha Ketineni | ae4c40b | 2007-07-17 04:04:50 -0700 | [diff] [blame] | 743 | .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] | 744 | .gm_ops = &gss_kerberos_ops, |
| 745 | .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs), |
| 746 | .gm_pfs = gss_kerberos_pfs, |
Trond Myklebust | fc54a0c | 2010-04-08 14:25:20 -0400 | [diff] [blame] | 747 | .gm_upcall_enctypes = "enctypes=18,17,16,23,3,1,2 ", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | }; |
| 749 | |
| 750 | static int __init init_kerberos_module(void) |
| 751 | { |
| 752 | int status; |
| 753 | |
| 754 | status = gss_mech_register(&gss_kerberos_mech); |
| 755 | if (status) |
| 756 | printk("Failed to register kerberos gss mechanism!\n"); |
| 757 | return status; |
| 758 | } |
| 759 | |
| 760 | static void __exit cleanup_kerberos_module(void) |
| 761 | { |
| 762 | gss_mech_unregister(&gss_kerberos_mech); |
| 763 | } |
| 764 | |
| 765 | MODULE_LICENSE("GPL"); |
| 766 | module_init(init_kerberos_module); |
| 767 | module_exit(cleanup_kerberos_module); |