gss_krb5_wrap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. #ifdef RPC_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], KM_USER0);
  79. pad = *(ptr + offset);
  80. kunmap_atomic(ptr, KM_USER0);
  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 = random32();
  124. i = (i << 32) | random32();
  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. dprintk("RPC: %s\n", __func__);
  158. now = get_seconds();
  159. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  160. gss_krb5_add_padding(buf, offset, blocksize);
  161. BUG_ON((buf->len - offset) % blocksize);
  162. plainlen = blocksize + buf->len - offset;
  163. headlen = g_token_size(&kctx->mech_used,
  164. GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength + plainlen) -
  165. (buf->len - offset);
  166. ptr = buf->head[0].iov_base + offset;
  167. /* shift data to make room for header. */
  168. xdr_extend_head(buf, offset, headlen);
  169. /* XXX Would be cleverer to encrypt while copying. */
  170. BUG_ON((buf->len - offset - headlen) % blocksize);
  171. g_make_token_header(&kctx->mech_used,
  172. GSS_KRB5_TOK_HDR_LEN +
  173. kctx->gk5e->cksumlength + plainlen, &ptr);
  174. /* ptr now at header described in rfc 1964, section 1.2.1: */
  175. ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
  176. ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
  177. msg_start = ptr + GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength;
  178. *(__be16 *)(ptr + 2) = cpu_to_le16(kctx->gk5e->signalg);
  179. memset(ptr + 4, 0xff, 4);
  180. *(__be16 *)(ptr + 4) = cpu_to_le16(kctx->gk5e->sealalg);
  181. gss_krb5_make_confounder(msg_start, blocksize);
  182. if (kctx->gk5e->keyed_cksum)
  183. cksumkey = kctx->cksum;
  184. else
  185. cksumkey = NULL;
  186. /* XXXJBF: UGH!: */
  187. tmp_pages = buf->pages;
  188. buf->pages = pages;
  189. if (make_checksum(kctx, ptr, 8, buf, offset + headlen - blocksize,
  190. cksumkey, KG_USAGE_SEAL, &md5cksum))
  191. return GSS_S_FAILURE;
  192. buf->pages = tmp_pages;
  193. memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);
  194. spin_lock(&krb5_seq_lock);
  195. seq_send = kctx->seq_send++;
  196. spin_unlock(&krb5_seq_lock);
  197. /* XXX would probably be more efficient to compute checksum
  198. * and encrypt at the same time: */
  199. if ((krb5_make_seq_num(kctx, kctx->seq, kctx->initiate ? 0 : 0xff,
  200. seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
  201. return GSS_S_FAILURE;
  202. if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
  203. pages))
  204. return GSS_S_FAILURE;
  205. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  206. }
  207. static u32
  208. gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  209. {
  210. int signalg;
  211. int sealalg;
  212. char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  213. struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
  214. .data = cksumdata};
  215. s32 now;
  216. int direction;
  217. s32 seqnum;
  218. unsigned char *ptr;
  219. int bodysize;
  220. void *data_start, *orig_start;
  221. int data_len;
  222. int blocksize;
  223. int crypt_offset;
  224. u8 *cksumkey;
  225. dprintk("RPC: gss_unwrap_kerberos\n");
  226. ptr = (u8 *)buf->head[0].iov_base + offset;
  227. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  228. buf->len - offset))
  229. return GSS_S_DEFECTIVE_TOKEN;
  230. if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
  231. (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
  232. return GSS_S_DEFECTIVE_TOKEN;
  233. /* XXX sanity-check bodysize?? */
  234. /* get the sign and seal algorithms */
  235. signalg = ptr[2] + (ptr[3] << 8);
  236. if (signalg != kctx->gk5e->signalg)
  237. return GSS_S_DEFECTIVE_TOKEN;
  238. sealalg = ptr[4] + (ptr[5] << 8);
  239. if (sealalg != kctx->gk5e->sealalg)
  240. return GSS_S_DEFECTIVE_TOKEN;
  241. if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
  242. return GSS_S_DEFECTIVE_TOKEN;
  243. /*
  244. * Data starts after token header and checksum. ptr points
  245. * to the beginning of the token header
  246. */
  247. crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) -
  248. (unsigned char *)buf->head[0].iov_base;
  249. if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset))
  250. return GSS_S_DEFECTIVE_TOKEN;
  251. if (kctx->gk5e->keyed_cksum)
  252. cksumkey = kctx->cksum;
  253. else
  254. cksumkey = NULL;
  255. if (make_checksum(kctx, ptr, 8, buf, crypt_offset,
  256. cksumkey, KG_USAGE_SEAL, &md5cksum))
  257. return GSS_S_FAILURE;
  258. if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,
  259. kctx->gk5e->cksumlength))
  260. return GSS_S_BAD_SIG;
  261. /* it got through unscathed. Make sure the context is unexpired */
  262. now = get_seconds();
  263. if (now > kctx->endtime)
  264. return GSS_S_CONTEXT_EXPIRED;
  265. /* do sequencing checks */
  266. if (krb5_get_seq_num(kctx, ptr + GSS_KRB5_TOK_HDR_LEN,
  267. ptr + 8, &direction, &seqnum))
  268. return GSS_S_BAD_SIG;
  269. if ((kctx->initiate && direction != 0xff) ||
  270. (!kctx->initiate && direction != 0))
  271. return GSS_S_BAD_SIG;
  272. /* Copy the data back to the right position. XXX: Would probably be
  273. * better to copy and encrypt at the same time. */
  274. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  275. data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) +
  276. blocksize;
  277. orig_start = buf->head[0].iov_base + offset;
  278. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  279. memmove(orig_start, data_start, data_len);
  280. buf->head[0].iov_len -= (data_start - orig_start);
  281. buf->len -= (data_start - orig_start);
  282. if (gss_krb5_remove_padding(buf, blocksize))
  283. return GSS_S_DEFECTIVE_TOKEN;
  284. return GSS_S_COMPLETE;
  285. }
  286. /*
  287. * We cannot currently handle tokens with rotated data. We need a
  288. * generalized routine to rotate the data in place. It is anticipated
  289. * that we won't encounter rotated data in the general case.
  290. */
  291. static u32
  292. rotate_left(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf, u16 rrc)
  293. {
  294. unsigned int realrrc = rrc % (buf->len - offset - GSS_KRB5_TOK_HDR_LEN);
  295. if (realrrc == 0)
  296. return 0;
  297. dprintk("%s: cannot process token with rotated data: "
  298. "rrc %u, realrrc %u\n", __func__, rrc, realrrc);
  299. return 1;
  300. }
  301. static u32
  302. gss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset,
  303. struct xdr_buf *buf, struct page **pages)
  304. {
  305. int blocksize;
  306. u8 *ptr, *plainhdr;
  307. s32 now;
  308. u8 flags = 0x00;
  309. __be16 *be16ptr, ec = 0;
  310. __be64 *be64ptr;
  311. u32 err;
  312. dprintk("RPC: %s\n", __func__);
  313. if (kctx->gk5e->encrypt_v2 == NULL)
  314. return GSS_S_FAILURE;
  315. /* make room for gss token header */
  316. if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN))
  317. return GSS_S_FAILURE;
  318. /* construct gss token header */
  319. ptr = plainhdr = buf->head[0].iov_base + offset;
  320. *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff);
  321. *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff);
  322. if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
  323. flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR;
  324. if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0)
  325. flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY;
  326. /* We always do confidentiality in wrap tokens */
  327. flags |= KG2_TOKEN_FLAG_SEALED;
  328. *ptr++ = flags;
  329. *ptr++ = 0xff;
  330. be16ptr = (__be16 *)ptr;
  331. blocksize = crypto_blkcipher_blocksize(kctx->acceptor_enc);
  332. *be16ptr++ = cpu_to_be16(ec);
  333. /* "inner" token header always uses 0 for RRC */
  334. *be16ptr++ = cpu_to_be16(0);
  335. be64ptr = (__be64 *)be16ptr;
  336. spin_lock(&krb5_seq_lock);
  337. *be64ptr = cpu_to_be64(kctx->seq_send64++);
  338. spin_unlock(&krb5_seq_lock);
  339. err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, ec, pages);
  340. if (err)
  341. return err;
  342. now = get_seconds();
  343. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  344. }
  345. static u32
  346. gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  347. {
  348. s32 now;
  349. u64 seqnum;
  350. u8 *ptr;
  351. u8 flags = 0x00;
  352. u16 ec, rrc;
  353. int err;
  354. u32 headskip, tailskip;
  355. u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN];
  356. unsigned int movelen;
  357. dprintk("RPC: %s\n", __func__);
  358. if (kctx->gk5e->decrypt_v2 == NULL)
  359. return GSS_S_FAILURE;
  360. ptr = buf->head[0].iov_base + offset;
  361. if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP)
  362. return GSS_S_DEFECTIVE_TOKEN;
  363. flags = ptr[2];
  364. if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
  365. (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
  366. return GSS_S_BAD_SIG;
  367. if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) {
  368. dprintk("%s: token missing expected sealed flag\n", __func__);
  369. return GSS_S_DEFECTIVE_TOKEN;
  370. }
  371. if (ptr[3] != 0xff)
  372. return GSS_S_DEFECTIVE_TOKEN;
  373. ec = be16_to_cpup((__be16 *)(ptr + 4));
  374. rrc = be16_to_cpup((__be16 *)(ptr + 6));
  375. seqnum = be64_to_cpup((__be64 *)(ptr + 8));
  376. if (rrc != 0) {
  377. err = rotate_left(kctx, offset, buf, rrc);
  378. if (err)
  379. return GSS_S_FAILURE;
  380. }
  381. err = (*kctx->gk5e->decrypt_v2)(kctx, offset, buf,
  382. &headskip, &tailskip);
  383. if (err)
  384. return GSS_S_FAILURE;
  385. /*
  386. * Retrieve the decrypted gss token header and verify
  387. * it against the original
  388. */
  389. err = read_bytes_from_xdr_buf(buf,
  390. buf->len - GSS_KRB5_TOK_HDR_LEN - tailskip,
  391. decrypted_hdr, GSS_KRB5_TOK_HDR_LEN);
  392. if (err) {
  393. dprintk("%s: error %u getting decrypted_hdr\n", __func__, err);
  394. return GSS_S_FAILURE;
  395. }
  396. if (memcmp(ptr, decrypted_hdr, 6)
  397. || memcmp(ptr + 8, decrypted_hdr + 8, 8)) {
  398. dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__);
  399. return GSS_S_FAILURE;
  400. }
  401. /* do sequencing checks */
  402. /* it got through unscathed. Make sure the context is unexpired */
  403. now = get_seconds();
  404. if (now > kctx->endtime)
  405. return GSS_S_CONTEXT_EXPIRED;
  406. /*
  407. * Move the head data back to the right position in xdr_buf.
  408. * We ignore any "ec" data since it might be in the head or
  409. * the tail, and we really don't need to deal with it.
  410. * Note that buf->head[0].iov_len may indicate the available
  411. * head buffer space rather than that actually occupied.
  412. */
  413. movelen = min_t(unsigned int, buf->head[0].iov_len, buf->len);
  414. movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip;
  415. BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen >
  416. buf->head[0].iov_len);
  417. memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen);
  418. buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip;
  419. buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip;
  420. return GSS_S_COMPLETE;
  421. }
  422. u32
  423. gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
  424. struct xdr_buf *buf, struct page **pages)
  425. {
  426. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  427. switch (kctx->enctype) {
  428. default:
  429. BUG();
  430. case ENCTYPE_DES_CBC_RAW:
  431. case ENCTYPE_DES3_CBC_RAW:
  432. return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
  433. case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  434. case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  435. return gss_wrap_kerberos_v2(kctx, offset, buf, pages);
  436. }
  437. }
  438. u32
  439. gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
  440. {
  441. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  442. switch (kctx->enctype) {
  443. default:
  444. BUG();
  445. case ENCTYPE_DES_CBC_RAW:
  446. case ENCTYPE_DES3_CBC_RAW:
  447. return gss_unwrap_kerberos_v1(kctx, offset, buf);
  448. case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
  449. case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
  450. return gss_unwrap_kerberos_v2(kctx, offset, buf);
  451. }
  452. }