gss_krb5_mech.c 20 KB

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