gss_krb5_wrap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 <crypto/skcipher.h>
  31. #include <linux/types.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/sunrpc/gss_krb5.h>
  34. #include <linux/random.h>
  35. #include <linux/pagemap.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_SHIFT;
  76. unsigned int offset = (buf->page_base + len - 1)
  77. & (PAGE_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_sync_skcipher_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. seq_send = atomic_fetch_inc(&kctx->seq_send);
  202. /* XXX would probably be more efficient to compute checksum
  203. * and encrypt at the same time: */
  204. if ((krb5_make_seq_num(kctx, kctx->seq, kctx->initiate ? 0 : 0xff,
  205. seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
  206. return GSS_S_FAILURE;
  207. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC) {
  208. struct crypto_sync_skcipher *cipher;
  209. int err;
  210. cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name,
  211. 0, 0);
  212. if (IS_ERR(cipher))
  213. return GSS_S_FAILURE;
  214. krb5_rc4_setup_enc_key(kctx, cipher, seq_send);
  215. err = gss_encrypt_xdr_buf(cipher, buf,
  216. offset + headlen - conflen, pages);
  217. crypto_free_sync_skcipher(cipher);
  218. if (err)
  219. return GSS_S_FAILURE;
  220. } else {
  221. if (gss_encrypt_xdr_buf(kctx->enc, buf,
  222. offset + headlen - conflen, pages))
  223. return GSS_S_FAILURE;
  224. }
  225. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  226. }
  227. static u32
  228. gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  229. {
  230. int signalg;
  231. int sealalg;
  232. char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  233. struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
  234. .data = cksumdata};
  235. s32 now;
  236. int direction;
  237. s32 seqnum;
  238. unsigned char *ptr;
  239. int bodysize;
  240. void *data_start, *orig_start;
  241. int data_len;
  242. int blocksize;
  243. u32 conflen = kctx->gk5e->conflen;
  244. int crypt_offset;
  245. u8 *cksumkey;
  246. dprintk("RPC: gss_unwrap_kerberos\n");
  247. ptr = (u8 *)buf->head[0].iov_base + offset;
  248. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  249. buf->len - offset))
  250. return GSS_S_DEFECTIVE_TOKEN;
  251. if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
  252. (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
  253. return GSS_S_DEFECTIVE_TOKEN;
  254. /* XXX sanity-check bodysize?? */
  255. /* get the sign and seal algorithms */
  256. signalg = ptr[2] + (ptr[3] << 8);
  257. if (signalg != kctx->gk5e->signalg)
  258. return GSS_S_DEFECTIVE_TOKEN;
  259. sealalg = ptr[4] + (ptr[5] << 8);
  260. if (sealalg != kctx->gk5e->sealalg)
  261. return GSS_S_DEFECTIVE_TOKEN;
  262. if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
  263. return GSS_S_DEFECTIVE_TOKEN;
  264. /*
  265. * Data starts after token header and checksum. ptr points
  266. * to the beginning of the token header
  267. */
  268. crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) -
  269. (unsigned char *)buf->head[0].iov_base;
  270. /*
  271. * Need plaintext seqnum to derive encryption key for arcfour-hmac
  272. */
  273. if (krb5_get_seq_num(kctx, ptr + GSS_KRB5_TOK_HDR_LEN,
  274. ptr + 8, &direction, &seqnum))
  275. return GSS_S_BAD_SIG;
  276. if ((kctx->initiate && direction != 0xff) ||
  277. (!kctx->initiate && direction != 0))
  278. return GSS_S_BAD_SIG;
  279. if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC) {
  280. struct crypto_sync_skcipher *cipher;
  281. int err;
  282. cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name,
  283. 0, 0);
  284. if (IS_ERR(cipher))
  285. return GSS_S_FAILURE;
  286. krb5_rc4_setup_enc_key(kctx, cipher, seqnum);
  287. err = gss_decrypt_xdr_buf(cipher, buf, crypt_offset);
  288. crypto_free_sync_skcipher(cipher);
  289. if (err)
  290. return GSS_S_DEFECTIVE_TOKEN;
  291. } else {
  292. if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset))
  293. return GSS_S_DEFECTIVE_TOKEN;
  294. }
  295. if (kctx->gk5e->keyed_cksum)
  296. cksumkey = kctx->cksum;
  297. else
  298. cksumkey = NULL;
  299. if (make_checksum(kctx, ptr, 8, buf, crypt_offset,
  300. cksumkey, KG_USAGE_SEAL, &md5cksum))
  301. return GSS_S_FAILURE;
  302. if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,
  303. kctx->gk5e->cksumlength))
  304. return GSS_S_BAD_SIG;
  305. /* it got through unscathed. Make sure the context is unexpired */
  306. now = get_seconds();
  307. if (now > kctx->endtime)
  308. return GSS_S_CONTEXT_EXPIRED;
  309. /* do sequencing checks */
  310. /* Copy the data back to the right position. XXX: Would probably be
  311. * better to copy and encrypt at the same time. */
  312. blocksize = crypto_sync_skcipher_blocksize(kctx->enc);
  313. data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) +
  314. conflen;
  315. orig_start = buf->head[0].iov_base + offset;
  316. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  317. memmove(orig_start, data_start, data_len);
  318. buf->head[0].iov_len -= (data_start - orig_start);
  319. buf->len -= (data_start - orig_start);
  320. if (gss_krb5_remove_padding(buf, blocksize))
  321. return GSS_S_DEFECTIVE_TOKEN;
  322. return GSS_S_COMPLETE;
  323. }
  324. /*
  325. * We can shift data by up to LOCAL_BUF_LEN bytes in a pass. If we need
  326. * to do more than that, we shift repeatedly. Kevin Coffman reports
  327. * seeing 28 bytes as the value used by Microsoft clients and servers
  328. * with AES, so this constant is chosen to allow handling 28 in one pass
  329. * without using too much stack space.
  330. *
  331. * If that proves to a problem perhaps we could use a more clever
  332. * algorithm.
  333. */
  334. #define LOCAL_BUF_LEN 32u
  335. static void rotate_buf_a_little(struct xdr_buf *buf, unsigned int shift)
  336. {
  337. char head[LOCAL_BUF_LEN];
  338. char tmp[LOCAL_BUF_LEN];
  339. unsigned int this_len, i;
  340. BUG_ON(shift > LOCAL_BUF_LEN);
  341. read_bytes_from_xdr_buf(buf, 0, head, shift);
  342. for (i = 0; i + shift < buf->len; i += LOCAL_BUF_LEN) {
  343. this_len = min(LOCAL_BUF_LEN, buf->len - (i + shift));
  344. read_bytes_from_xdr_buf(buf, i+shift, tmp, this_len);
  345. write_bytes_to_xdr_buf(buf, i, tmp, this_len);
  346. }
  347. write_bytes_to_xdr_buf(buf, buf->len - shift, head, shift);
  348. }
  349. static void _rotate_left(struct xdr_buf *buf, unsigned int shift)
  350. {
  351. int shifted = 0;
  352. int this_shift;
  353. shift %= buf->len;
  354. while (shifted < shift) {
  355. this_shift = min(shift - shifted, LOCAL_BUF_LEN);
  356. rotate_buf_a_little(buf, this_shift);
  357. shifted += this_shift;
  358. }
  359. }
  360. static void rotate_left(u32 base, struct xdr_buf *buf, unsigned int shift)
  361. {
  362. struct xdr_buf subbuf;
  363. xdr_buf_subsegment(buf, &subbuf, base, buf->len - base);
  364. _rotate_left(&subbuf, shift);
  365. }
  366. static u32
  367. gss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset,
  368. struct xdr_buf *buf, struct page **pages)
  369. {
  370. u8 *ptr, *plainhdr;
  371. s32 now;
  372. u8 flags = 0x00;
  373. __be16 *be16ptr;
  374. __be64 *be64ptr;
  375. u32 err;
  376. dprintk("RPC: %s\n", __func__);
  377. if (kctx->gk5e->encrypt_v2 == NULL)
  378. return GSS_S_FAILURE;
  379. /* make room for gss token header */
  380. if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN))
  381. return GSS_S_FAILURE;
  382. /* construct gss token header */
  383. ptr = plainhdr = buf->head[0].iov_base + offset;
  384. *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff);
  385. *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff);
  386. if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
  387. flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR;
  388. if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0)
  389. flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY;
  390. /* We always do confidentiality in wrap tokens */
  391. flags |= KG2_TOKEN_FLAG_SEALED;
  392. *ptr++ = flags;
  393. *ptr++ = 0xff;
  394. be16ptr = (__be16 *)ptr;
  395. *be16ptr++ = 0;
  396. /* "inner" token header always uses 0 for RRC */
  397. *be16ptr++ = 0;
  398. be64ptr = (__be64 *)be16ptr;
  399. *be64ptr = cpu_to_be64(atomic64_fetch_inc(&kctx->seq_send64));
  400. err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, pages);
  401. if (err)
  402. return err;
  403. now = get_seconds();
  404. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  405. }
  406. static u32
  407. gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  408. {
  409. s32 now;
  410. u8 *ptr;
  411. u8 flags = 0x00;
  412. u16 ec, rrc;
  413. int err;
  414. u32 headskip, tailskip;
  415. u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN];
  416. unsigned int movelen;
  417. dprintk("RPC: %s\n", __func__);
  418. if (kctx->gk5e->decrypt_v2 == NULL)
  419. return GSS_S_FAILURE;
  420. ptr = buf->head[0].iov_base + offset;
  421. if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP)
  422. return GSS_S_DEFECTIVE_TOKEN;
  423. flags = ptr[2];
  424. if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
  425. (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
  426. return GSS_S_BAD_SIG;
  427. if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) {
  428. dprintk("%s: token missing expected sealed flag\n", __func__);
  429. return GSS_S_DEFECTIVE_TOKEN;
  430. }
  431. if (ptr[3] != 0xff)
  432. return GSS_S_DEFECTIVE_TOKEN;
  433. ec = be16_to_cpup((__be16 *)(ptr + 4));
  434. rrc = be16_to_cpup((__be16 *)(ptr + 6));
  435. /*
  436. * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss
  437. * doesn't want it checked; see page 6 of rfc 2203.
  438. */
  439. if (rrc != 0)
  440. rotate_left(offset + 16, buf, rrc);
  441. err = (*kctx->gk5e->decrypt_v2)(kctx, offset, buf,
  442. &headskip, &tailskip);
  443. if (err)
  444. return GSS_S_FAILURE;
  445. /*
  446. * Retrieve the decrypted gss token header and verify
  447. * it against the original
  448. */
  449. err = read_bytes_from_xdr_buf(buf,
  450. buf->len - GSS_KRB5_TOK_HDR_LEN - tailskip,
  451. decrypted_hdr, GSS_KRB5_TOK_HDR_LEN);
  452. if (err) {
  453. dprintk("%s: error %u getting decrypted_hdr\n", __func__, err);
  454. return GSS_S_FAILURE;
  455. }
  456. if (memcmp(ptr, decrypted_hdr, 6)
  457. || memcmp(ptr + 8, decrypted_hdr + 8, 8)) {
  458. dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__);
  459. return GSS_S_FAILURE;
  460. }
  461. /* do sequencing checks */
  462. /* it got through unscathed. Make sure the context is unexpired */
  463. now = get_seconds();
  464. if (now > kctx->endtime)
  465. return GSS_S_CONTEXT_EXPIRED;
  466. /*
  467. * Move the head data back to the right position in xdr_buf.
  468. * We ignore any "ec" data since it might be in the head or
  469. * the tail, and we really don't need to deal with it.
  470. * Note that buf->head[0].iov_len may indicate the available
  471. * head buffer space rather than that actually occupied.
  472. */
  473. movelen = min_t(unsigned int, buf->head[0].iov_len, buf->len);
  474. movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip;
  475. BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen >
  476. buf->head[0].iov_len);
  477. memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen);
  478. buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip;
  479. buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip;
  480. /* Trim off the trailing "extra count" and checksum blob */
  481. xdr_buf_trim(buf, ec + GSS_KRB5_TOK_HDR_LEN + tailskip);
  482. return GSS_S_COMPLETE;
  483. }
  484. u32
  485. gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
  486. struct xdr_buf *buf, struct page **pages)
  487. {
  488. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  489. switch (kctx->enctype) {
  490. default:
  491. BUG();
  492. case ENCTYPE_DES_CBC_RAW:
  493. case ENCTYPE_DES3_CBC_RAW:
  494. case ENCTYPE_ARCFOUR_HMAC:
  495. return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
  496. case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  497. case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  498. return gss_wrap_kerberos_v2(kctx, offset, buf, pages);
  499. }
  500. }
  501. u32
  502. gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
  503. {
  504. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  505. switch (kctx->enctype) {
  506. default:
  507. BUG();
  508. case ENCTYPE_DES_CBC_RAW:
  509. case ENCTYPE_DES3_CBC_RAW:
  510. case ENCTYPE_ARCFOUR_HMAC:
  511. return gss_unwrap_kerberos_v1(kctx, offset, buf);
  512. case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  513. case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  514. return gss_unwrap_kerberos_v2(kctx, offset, buf);
  515. }
  516. }