gss_krb5_wrap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * COPYRIGHT (c) 2008
  3. * The Regents of the University of Michigan
  4. * ALL RIGHTS RESERVED
  5. *
  6. * Permission is granted to use, copy, create derivative works
  7. * and redistribute this software and such derivative works
  8. * for any purpose, so long as the name of The University of
  9. * Michigan is not used in any advertising or publicity
  10. * pertaining to the use of distribution of this software
  11. * without specific, written prior authorization. If the
  12. * above copyright notice or any other identification of the
  13. * University of Michigan is included in any copy of any
  14. * portion of this software, then the disclaimer below must
  15. * also be included.
  16. *
  17. * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
  18. * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
  19. * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
  20. * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
  21. * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
  23. * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
  24. * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
  25. * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
  26. * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
  27. * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGES.
  29. */
  30. #include <linux/types.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/sunrpc/gss_krb5.h>
  33. #include <linux/random.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/crypto.h>
  36. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  37. # define RPCDBG_FACILITY RPCDBG_AUTH
  38. #endif
  39. static inline int
  40. gss_krb5_padding(int blocksize, int length)
  41. {
  42. return blocksize - (length % blocksize);
  43. }
  44. static inline void
  45. gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
  46. {
  47. int padding = gss_krb5_padding(blocksize, buf->len - offset);
  48. char *p;
  49. struct kvec *iov;
  50. if (buf->page_len || buf->tail[0].iov_len)
  51. iov = &buf->tail[0];
  52. else
  53. iov = &buf->head[0];
  54. p = iov->iov_base + iov->iov_len;
  55. iov->iov_len += padding;
  56. buf->len += padding;
  57. memset(p, padding, padding);
  58. }
  59. static inline int
  60. gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
  61. {
  62. u8 *ptr;
  63. u8 pad;
  64. size_t len = buf->len;
  65. if (len <= buf->head[0].iov_len) {
  66. pad = *(u8 *)(buf->head[0].iov_base + len - 1);
  67. if (pad > buf->head[0].iov_len)
  68. return -EINVAL;
  69. buf->head[0].iov_len -= pad;
  70. goto out;
  71. } else
  72. len -= buf->head[0].iov_len;
  73. if (len <= buf->page_len) {
  74. unsigned int last = (buf->page_base + len - 1)
  75. >>PAGE_CACHE_SHIFT;
  76. unsigned int offset = (buf->page_base + len - 1)
  77. & (PAGE_CACHE_SIZE - 1);
  78. ptr = kmap_atomic(buf->pages[last]);
  79. pad = *(ptr + offset);
  80. kunmap_atomic(ptr);
  81. goto out;
  82. } else
  83. len -= buf->page_len;
  84. BUG_ON(len > buf->tail[0].iov_len);
  85. pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
  86. out:
  87. /* XXX: NOTE: we do not adjust the page lengths--they represent
  88. * a range of data in the real filesystem page cache, and we need
  89. * to know that range so the xdr code can properly place read data.
  90. * However adjusting the head length, as we do above, is harmless.
  91. * In the case of a request that fits into a single page, the server
  92. * also uses length and head length together to determine the original
  93. * start of the request to copy the request for deferal; so it's
  94. * easier on the server if we adjust head and tail length in tandem.
  95. * It's not really a problem that we don't fool with the page and
  96. * tail lengths, though--at worst badly formed xdr might lead the
  97. * server to attempt to parse the padding.
  98. * XXX: Document all these weird requirements for gss mechanism
  99. * wrap/unwrap functions. */
  100. if (pad > blocksize)
  101. return -EINVAL;
  102. if (buf->len > pad)
  103. buf->len -= pad;
  104. else
  105. return -EINVAL;
  106. return 0;
  107. }
  108. void
  109. gss_krb5_make_confounder(char *p, u32 conflen)
  110. {
  111. static u64 i = 0;
  112. u64 *q = (u64 *)p;
  113. /* rfc1964 claims this should be "random". But all that's really
  114. * necessary is that it be unique. And not even that is necessary in
  115. * our case since our "gssapi" implementation exists only to support
  116. * rpcsec_gss, so we know that the only buffers we will ever encrypt
  117. * already begin with a unique sequence number. Just to hedge my bets
  118. * I'll make a half-hearted attempt at something unique, but ensuring
  119. * uniqueness would mean worrying about atomicity and rollover, and I
  120. * don't care enough. */
  121. /* initialize to random value */
  122. if (i == 0) {
  123. i = prandom_u32();
  124. i = (i << 32) | prandom_u32();
  125. }
  126. switch (conflen) {
  127. case 16:
  128. *q++ = i++;
  129. /* fall through */
  130. case 8:
  131. *q++ = i++;
  132. break;
  133. default:
  134. BUG();
  135. }
  136. }
  137. /* Assumptions: the head and tail of inbuf are ours to play with.
  138. * The pages, however, may be real pages in the page cache and we replace
  139. * them with scratch pages from **pages before writing to them. */
  140. /* XXX: obviously the above should be documentation of wrap interface,
  141. * and shouldn't be in this kerberos-specific file. */
  142. /* XXX factor out common code with seal/unseal. */
  143. static u32
  144. gss_wrap_kerberos_v1(struct krb5_ctx *kctx, int offset,
  145. struct xdr_buf *buf, struct page **pages)
  146. {
  147. char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  148. struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
  149. .data = cksumdata};
  150. int blocksize = 0, plainlen;
  151. unsigned char *ptr, *msg_start;
  152. s32 now;
  153. int headlen;
  154. struct page **tmp_pages;
  155. u32 seq_send;
  156. u8 *cksumkey;
  157. u32 conflen = kctx->gk5e->conflen;
  158. dprintk("RPC: %s\n", __func__);
  159. now = get_seconds();
  160. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  161. gss_krb5_add_padding(buf, offset, blocksize);
  162. BUG_ON((buf->len - offset) % blocksize);
  163. plainlen = conflen + buf->len - offset;
  164. headlen = g_token_size(&kctx->mech_used,
  165. GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength + plainlen) -
  166. (buf->len - offset);
  167. ptr = buf->head[0].iov_base + offset;
  168. /* shift data to make room for header. */
  169. xdr_extend_head(buf, offset, headlen);
  170. /* XXX Would be cleverer to encrypt while copying. */
  171. BUG_ON((buf->len - offset - headlen) % blocksize);
  172. g_make_token_header(&kctx->mech_used,
  173. GSS_KRB5_TOK_HDR_LEN +
  174. kctx->gk5e->cksumlength + plainlen, &ptr);
  175. /* ptr now at header described in rfc 1964, section 1.2.1: */
  176. ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
  177. ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
  178. msg_start = ptr + GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength;
  179. /*
  180. * signalg and sealalg are stored as if they were converted from LE
  181. * to host endian, even though they're opaque pairs of bytes according
  182. * to the RFC.
  183. */
  184. *(__le16 *)(ptr + 2) = cpu_to_le16(kctx->gk5e->signalg);
  185. *(__le16 *)(ptr + 4) = cpu_to_le16(kctx->gk5e->sealalg);
  186. ptr[6] = 0xff;
  187. ptr[7] = 0xff;
  188. gss_krb5_make_confounder(msg_start, conflen);
  189. if (kctx->gk5e->keyed_cksum)
  190. cksumkey = kctx->cksum;
  191. else
  192. cksumkey = NULL;
  193. /* XXXJBF: UGH!: */
  194. tmp_pages = buf->pages;
  195. buf->pages = pages;
  196. if (make_checksum(kctx, ptr, 8, buf, offset + headlen - conflen,
  197. cksumkey, KG_USAGE_SEAL, &md5cksum))
  198. return GSS_S_FAILURE;
  199. buf->pages = tmp_pages;
  200. memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);
  201. spin_lock(&krb5_seq_lock);
  202. seq_send = kctx->seq_send++;
  203. spin_unlock(&krb5_seq_lock);
  204. /* XXX would probably be more efficient to compute checksum
  205. * and encrypt at the same time: */
  206. if ((krb5_make_seq_num(kctx, kctx->seq, kctx->initiate ? 0 : 0xff,
  207. seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
  208. return GSS_S_FAILURE;
  209. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC) {
  210. struct crypto_blkcipher *cipher;
  211. int err;
  212. cipher = crypto_alloc_blkcipher(kctx->gk5e->encrypt_name, 0,
  213. CRYPTO_ALG_ASYNC);
  214. if (IS_ERR(cipher))
  215. return GSS_S_FAILURE;
  216. krb5_rc4_setup_enc_key(kctx, cipher, seq_send);
  217. err = gss_encrypt_xdr_buf(cipher, buf,
  218. offset + headlen - conflen, pages);
  219. crypto_free_blkcipher(cipher);
  220. if (err)
  221. return GSS_S_FAILURE;
  222. } else {
  223. if (gss_encrypt_xdr_buf(kctx->enc, buf,
  224. offset + headlen - conflen, pages))
  225. return GSS_S_FAILURE;
  226. }
  227. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  228. }
  229. static u32
  230. gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  231. {
  232. int signalg;
  233. int sealalg;
  234. char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  235. struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
  236. .data = cksumdata};
  237. s32 now;
  238. int direction;
  239. s32 seqnum;
  240. unsigned char *ptr;
  241. int bodysize;
  242. void *data_start, *orig_start;
  243. int data_len;
  244. int blocksize;
  245. u32 conflen = kctx->gk5e->conflen;
  246. int crypt_offset;
  247. u8 *cksumkey;
  248. dprintk("RPC: gss_unwrap_kerberos\n");
  249. ptr = (u8 *)buf->head[0].iov_base + offset;
  250. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  251. buf->len - offset))
  252. return GSS_S_DEFECTIVE_TOKEN;
  253. if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
  254. (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
  255. return GSS_S_DEFECTIVE_TOKEN;
  256. /* XXX sanity-check bodysize?? */
  257. /* get the sign and seal algorithms */
  258. signalg = ptr[2] + (ptr[3] << 8);
  259. if (signalg != kctx->gk5e->signalg)
  260. return GSS_S_DEFECTIVE_TOKEN;
  261. sealalg = ptr[4] + (ptr[5] << 8);
  262. if (sealalg != kctx->gk5e->sealalg)
  263. return GSS_S_DEFECTIVE_TOKEN;
  264. if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
  265. return GSS_S_DEFECTIVE_TOKEN;
  266. /*
  267. * Data starts after token header and checksum. ptr points
  268. * to the beginning of the token header
  269. */
  270. crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) -
  271. (unsigned char *)buf->head[0].iov_base;
  272. /*
  273. * Need plaintext seqnum to derive encryption key for arcfour-hmac
  274. */
  275. if (krb5_get_seq_num(kctx, ptr + GSS_KRB5_TOK_HDR_LEN,
  276. ptr + 8, &direction, &seqnum))
  277. return GSS_S_BAD_SIG;
  278. if ((kctx->initiate && direction != 0xff) ||
  279. (!kctx->initiate && direction != 0))
  280. return GSS_S_BAD_SIG;
  281. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC) {
  282. struct crypto_blkcipher *cipher;
  283. int err;
  284. cipher = crypto_alloc_blkcipher(kctx->gk5e->encrypt_name, 0,
  285. CRYPTO_ALG_ASYNC);
  286. if (IS_ERR(cipher))
  287. return GSS_S_FAILURE;
  288. krb5_rc4_setup_enc_key(kctx, cipher, seqnum);
  289. err = gss_decrypt_xdr_buf(cipher, buf, crypt_offset);
  290. crypto_free_blkcipher(cipher);
  291. if (err)
  292. return GSS_S_DEFECTIVE_TOKEN;
  293. } else {
  294. if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset))
  295. return GSS_S_DEFECTIVE_TOKEN;
  296. }
  297. if (kctx->gk5e->keyed_cksum)
  298. cksumkey = kctx->cksum;
  299. else
  300. cksumkey = NULL;
  301. if (make_checksum(kctx, ptr, 8, buf, crypt_offset,
  302. cksumkey, KG_USAGE_SEAL, &md5cksum))
  303. return GSS_S_FAILURE;
  304. if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,
  305. kctx->gk5e->cksumlength))
  306. return GSS_S_BAD_SIG;
  307. /* it got through unscathed. Make sure the context is unexpired */
  308. now = get_seconds();
  309. if (now > kctx->endtime)
  310. return GSS_S_CONTEXT_EXPIRED;
  311. /* do sequencing checks */
  312. /* Copy the data back to the right position. XXX: Would probably be
  313. * better to copy and encrypt at the same time. */
  314. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  315. data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) +
  316. conflen;
  317. orig_start = buf->head[0].iov_base + offset;
  318. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  319. memmove(orig_start, data_start, data_len);
  320. buf->head[0].iov_len -= (data_start - orig_start);
  321. buf->len -= (data_start - orig_start);
  322. if (gss_krb5_remove_padding(buf, blocksize))
  323. return GSS_S_DEFECTIVE_TOKEN;
  324. return GSS_S_COMPLETE;
  325. }
  326. /*
  327. * We can shift data by up to LOCAL_BUF_LEN bytes in a pass. If we need
  328. * to do more than that, we shift repeatedly. Kevin Coffman reports
  329. * seeing 28 bytes as the value used by Microsoft clients and servers
  330. * with AES, so this constant is chosen to allow handling 28 in one pass
  331. * without using too much stack space.
  332. *
  333. * If that proves to a problem perhaps we could use a more clever
  334. * algorithm.
  335. */
  336. #define LOCAL_BUF_LEN 32u
  337. static void rotate_buf_a_little(struct xdr_buf *buf, unsigned int shift)
  338. {
  339. char head[LOCAL_BUF_LEN];
  340. char tmp[LOCAL_BUF_LEN];
  341. unsigned int this_len, i;
  342. BUG_ON(shift > LOCAL_BUF_LEN);
  343. read_bytes_from_xdr_buf(buf, 0, head, shift);
  344. for (i = 0; i + shift < buf->len; i += LOCAL_BUF_LEN) {
  345. this_len = min(LOCAL_BUF_LEN, buf->len - (i + shift));
  346. read_bytes_from_xdr_buf(buf, i+shift, tmp, this_len);
  347. write_bytes_to_xdr_buf(buf, i, tmp, this_len);
  348. }
  349. write_bytes_to_xdr_buf(buf, buf->len - shift, head, shift);
  350. }
  351. static void _rotate_left(struct xdr_buf *buf, unsigned int shift)
  352. {
  353. int shifted = 0;
  354. int this_shift;
  355. shift %= buf->len;
  356. while (shifted < shift) {
  357. this_shift = min(shift - shifted, LOCAL_BUF_LEN);
  358. rotate_buf_a_little(buf, this_shift);
  359. shifted += this_shift;
  360. }
  361. }
  362. static void rotate_left(u32 base, struct xdr_buf *buf, unsigned int shift)
  363. {
  364. struct xdr_buf subbuf;
  365. xdr_buf_subsegment(buf, &subbuf, base, buf->len - base);
  366. _rotate_left(&subbuf, shift);
  367. }
  368. static u32
  369. gss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset,
  370. struct xdr_buf *buf, struct page **pages)
  371. {
  372. int blocksize;
  373. u8 *ptr, *plainhdr;
  374. s32 now;
  375. u8 flags = 0x00;
  376. __be16 *be16ptr;
  377. __be64 *be64ptr;
  378. u32 err;
  379. dprintk("RPC: %s\n", __func__);
  380. if (kctx->gk5e->encrypt_v2 == NULL)
  381. return GSS_S_FAILURE;
  382. /* make room for gss token header */
  383. if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN))
  384. return GSS_S_FAILURE;
  385. /* construct gss token header */
  386. ptr = plainhdr = buf->head[0].iov_base + offset;
  387. *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff);
  388. *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff);
  389. if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
  390. flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR;
  391. if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0)
  392. flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY;
  393. /* We always do confidentiality in wrap tokens */
  394. flags |= KG2_TOKEN_FLAG_SEALED;
  395. *ptr++ = flags;
  396. *ptr++ = 0xff;
  397. be16ptr = (__be16 *)ptr;
  398. blocksize = crypto_blkcipher_blocksize(kctx->acceptor_enc);
  399. *be16ptr++ = 0;
  400. /* "inner" token header always uses 0 for RRC */
  401. *be16ptr++ = 0;
  402. be64ptr = (__be64 *)be16ptr;
  403. spin_lock(&krb5_seq_lock);
  404. *be64ptr = cpu_to_be64(kctx->seq_send64++);
  405. spin_unlock(&krb5_seq_lock);
  406. err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, pages);
  407. if (err)
  408. return err;
  409. now = get_seconds();
  410. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  411. }
  412. static u32
  413. gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  414. {
  415. s32 now;
  416. u8 *ptr;
  417. u8 flags = 0x00;
  418. u16 ec, rrc;
  419. int err;
  420. u32 headskip, tailskip;
  421. u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN];
  422. unsigned int movelen;
  423. dprintk("RPC: %s\n", __func__);
  424. if (kctx->gk5e->decrypt_v2 == NULL)
  425. return GSS_S_FAILURE;
  426. ptr = buf->head[0].iov_base + offset;
  427. if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP)
  428. return GSS_S_DEFECTIVE_TOKEN;
  429. flags = ptr[2];
  430. if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
  431. (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
  432. return GSS_S_BAD_SIG;
  433. if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) {
  434. dprintk("%s: token missing expected sealed flag\n", __func__);
  435. return GSS_S_DEFECTIVE_TOKEN;
  436. }
  437. if (ptr[3] != 0xff)
  438. return GSS_S_DEFECTIVE_TOKEN;
  439. ec = be16_to_cpup((__be16 *)(ptr + 4));
  440. rrc = be16_to_cpup((__be16 *)(ptr + 6));
  441. /*
  442. * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss
  443. * doesn't want it checked; see page 6 of rfc 2203.
  444. */
  445. if (rrc != 0)
  446. rotate_left(offset + 16, buf, rrc);
  447. err = (*kctx->gk5e->decrypt_v2)(kctx, offset, buf,
  448. &headskip, &tailskip);
  449. if (err)
  450. return GSS_S_FAILURE;
  451. /*
  452. * Retrieve the decrypted gss token header and verify
  453. * it against the original
  454. */
  455. err = read_bytes_from_xdr_buf(buf,
  456. buf->len - GSS_KRB5_TOK_HDR_LEN - tailskip,
  457. decrypted_hdr, GSS_KRB5_TOK_HDR_LEN);
  458. if (err) {
  459. dprintk("%s: error %u getting decrypted_hdr\n", __func__, err);
  460. return GSS_S_FAILURE;
  461. }
  462. if (memcmp(ptr, decrypted_hdr, 6)
  463. || memcmp(ptr + 8, decrypted_hdr + 8, 8)) {
  464. dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__);
  465. return GSS_S_FAILURE;
  466. }
  467. /* do sequencing checks */
  468. /* it got through unscathed. Make sure the context is unexpired */
  469. now = get_seconds();
  470. if (now > kctx->endtime)
  471. return GSS_S_CONTEXT_EXPIRED;
  472. /*
  473. * Move the head data back to the right position in xdr_buf.
  474. * We ignore any "ec" data since it might be in the head or
  475. * the tail, and we really don't need to deal with it.
  476. * Note that buf->head[0].iov_len may indicate the available
  477. * head buffer space rather than that actually occupied.
  478. */
  479. movelen = min_t(unsigned int, buf->head[0].iov_len, buf->len);
  480. movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip;
  481. BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen >
  482. buf->head[0].iov_len);
  483. memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen);
  484. buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip;
  485. buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip;
  486. /* Trim off the trailing "extra count" and checksum blob */
  487. xdr_buf_trim(buf, ec + GSS_KRB5_TOK_HDR_LEN + tailskip);
  488. return GSS_S_COMPLETE;
  489. }
  490. u32
  491. gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
  492. struct xdr_buf *buf, struct page **pages)
  493. {
  494. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  495. switch (kctx->enctype) {
  496. default:
  497. BUG();
  498. case ENCTYPE_DES_CBC_RAW:
  499. case ENCTYPE_DES3_CBC_RAW:
  500. case ENCTYPE_ARCFOUR_HMAC:
  501. return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
  502. case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  503. case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  504. return gss_wrap_kerberos_v2(kctx, offset, buf, pages);
  505. }
  506. }
  507. u32
  508. gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
  509. {
  510. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  511. switch (kctx->enctype) {
  512. default:
  513. BUG();
  514. case ENCTYPE_DES_CBC_RAW:
  515. case ENCTYPE_DES3_CBC_RAW:
  516. case ENCTYPE_ARCFOUR_HMAC:
  517. return gss_unwrap_kerberos_v1(kctx, offset, buf);
  518. case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  519. case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  520. return gss_unwrap_kerberos_v2(kctx, offset, buf);
  521. }
  522. }