gss_krb5_wrap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. static void
  109. 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. 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, &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->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, &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->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
  267. &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. u32
  287. gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
  288. struct xdr_buf *buf, struct page **pages)
  289. {
  290. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  291. switch (kctx->enctype) {
  292. default:
  293. BUG();
  294. case ENCTYPE_DES_CBC_RAW:
  295. return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
  296. }
  297. }
  298. u32
  299. gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
  300. {
  301. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  302. switch (kctx->enctype) {
  303. default:
  304. BUG();
  305. case ENCTYPE_DES_CBC_RAW:
  306. return gss_unwrap_kerberos_v1(kctx, offset, buf);
  307. }
  308. }