gss_krb5_mech.c 20 KB

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