gss_krb5_wrap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <linux/types.h>
  2. #include <linux/slab.h>
  3. #include <linux/jiffies.h>
  4. #include <linux/sunrpc/gss_krb5.h>
  5. #include <linux/random.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/crypto.h>
  8. #ifdef RPC_DEBUG
  9. # define RPCDBG_FACILITY RPCDBG_AUTH
  10. #endif
  11. static inline int
  12. gss_krb5_padding(int blocksize, int length)
  13. {
  14. /* Most of the code is block-size independent but currently we
  15. * use only 8: */
  16. BUG_ON(blocksize != 8);
  17. return 8 - (length & 7);
  18. }
  19. static inline void
  20. gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
  21. {
  22. int padding = gss_krb5_padding(blocksize, buf->len - offset);
  23. char *p;
  24. struct kvec *iov;
  25. if (buf->page_len || buf->tail[0].iov_len)
  26. iov = &buf->tail[0];
  27. else
  28. iov = &buf->head[0];
  29. p = iov->iov_base + iov->iov_len;
  30. iov->iov_len += padding;
  31. buf->len += padding;
  32. memset(p, padding, padding);
  33. }
  34. static inline int
  35. gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
  36. {
  37. u8 *ptr;
  38. u8 pad;
  39. size_t len = buf->len;
  40. if (len <= buf->head[0].iov_len) {
  41. pad = *(u8 *)(buf->head[0].iov_base + len - 1);
  42. if (pad > buf->head[0].iov_len)
  43. return -EINVAL;
  44. buf->head[0].iov_len -= pad;
  45. goto out;
  46. } else
  47. len -= buf->head[0].iov_len;
  48. if (len <= buf->page_len) {
  49. unsigned int last = (buf->page_base + len - 1)
  50. >>PAGE_CACHE_SHIFT;
  51. unsigned int offset = (buf->page_base + len - 1)
  52. & (PAGE_CACHE_SIZE - 1);
  53. ptr = kmap_atomic(buf->pages[last], KM_USER0);
  54. pad = *(ptr + offset);
  55. kunmap_atomic(ptr, KM_USER0);
  56. goto out;
  57. } else
  58. len -= buf->page_len;
  59. BUG_ON(len > buf->tail[0].iov_len);
  60. pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
  61. out:
  62. /* XXX: NOTE: we do not adjust the page lengths--they represent
  63. * a range of data in the real filesystem page cache, and we need
  64. * to know that range so the xdr code can properly place read data.
  65. * However adjusting the head length, as we do above, is harmless.
  66. * In the case of a request that fits into a single page, the server
  67. * also uses length and head length together to determine the original
  68. * start of the request to copy the request for deferal; so it's
  69. * easier on the server if we adjust head and tail length in tandem.
  70. * It's not really a problem that we don't fool with the page and
  71. * tail lengths, though--at worst badly formed xdr might lead the
  72. * server to attempt to parse the padding.
  73. * XXX: Document all these weird requirements for gss mechanism
  74. * wrap/unwrap functions. */
  75. if (pad > blocksize)
  76. return -EINVAL;
  77. if (buf->len > pad)
  78. buf->len -= pad;
  79. else
  80. return -EINVAL;
  81. return 0;
  82. }
  83. static void
  84. make_confounder(char *p, u32 conflen)
  85. {
  86. static u64 i = 0;
  87. u64 *q = (u64 *)p;
  88. /* rfc1964 claims this should be "random". But all that's really
  89. * necessary is that it be unique. And not even that is necessary in
  90. * our case since our "gssapi" implementation exists only to support
  91. * rpcsec_gss, so we know that the only buffers we will ever encrypt
  92. * already begin with a unique sequence number. Just to hedge my bets
  93. * I'll make a half-hearted attempt at something unique, but ensuring
  94. * uniqueness would mean worrying about atomicity and rollover, and I
  95. * don't care enough. */
  96. /* initialize to random value */
  97. if (i == 0) {
  98. i = random32();
  99. i = (i << 32) | random32();
  100. }
  101. switch (conflen) {
  102. case 16:
  103. *q++ = i++;
  104. /* fall through */
  105. case 8:
  106. *q++ = i++;
  107. break;
  108. default:
  109. BUG();
  110. }
  111. }
  112. /* Assumptions: the head and tail of inbuf are ours to play with.
  113. * The pages, however, may be real pages in the page cache and we replace
  114. * them with scratch pages from **pages before writing to them. */
  115. /* XXX: obviously the above should be documentation of wrap interface,
  116. * and shouldn't be in this kerberos-specific file. */
  117. /* XXX factor out common code with seal/unseal. */
  118. u32
  119. gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
  120. struct xdr_buf *buf, struct page **pages)
  121. {
  122. struct krb5_ctx *kctx = ctx->internal_ctx_id;
  123. char cksumdata[16];
  124. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  125. int blocksize = 0, plainlen;
  126. unsigned char *ptr, *msg_start;
  127. s32 now;
  128. int headlen;
  129. struct page **tmp_pages;
  130. u32 seq_send;
  131. dprintk("RPC: gss_wrap_kerberos\n");
  132. now = get_seconds();
  133. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  134. gss_krb5_add_padding(buf, offset, blocksize);
  135. BUG_ON((buf->len - offset) % blocksize);
  136. plainlen = blocksize + buf->len - offset;
  137. headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
  138. (buf->len - offset);
  139. ptr = buf->head[0].iov_base + offset;
  140. /* shift data to make room for header. */
  141. /* XXX Would be cleverer to encrypt while copying. */
  142. /* XXX bounds checking, slack, etc. */
  143. memmove(ptr + headlen, ptr, buf->head[0].iov_len - offset);
  144. buf->head[0].iov_len += headlen;
  145. buf->len += headlen;
  146. BUG_ON((buf->len - offset - headlen) % blocksize);
  147. g_make_token_header(&kctx->mech_used,
  148. GSS_KRB5_TOK_HDR_LEN + 8 + plainlen, &ptr);
  149. /* ptr now at header described in rfc 1964, section 1.2.1: */
  150. ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
  151. ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
  152. msg_start = ptr + 24;
  153. *(__be16 *)(ptr + 2) = htons(SGN_ALG_DES_MAC_MD5);
  154. memset(ptr + 4, 0xff, 4);
  155. *(__be16 *)(ptr + 4) = htons(SEAL_ALG_DES);
  156. make_confounder(msg_start, blocksize);
  157. /* XXXJBF: UGH!: */
  158. tmp_pages = buf->pages;
  159. buf->pages = pages;
  160. if (make_checksum("md5", ptr, 8, buf,
  161. offset + headlen - blocksize, &md5cksum))
  162. return GSS_S_FAILURE;
  163. buf->pages = tmp_pages;
  164. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  165. md5cksum.data, md5cksum.len))
  166. return GSS_S_FAILURE;
  167. memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data + md5cksum.len - 8, 8);
  168. spin_lock(&krb5_seq_lock);
  169. seq_send = kctx->seq_send++;
  170. spin_unlock(&krb5_seq_lock);
  171. /* XXX would probably be more efficient to compute checksum
  172. * and encrypt at the same time: */
  173. if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
  174. seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
  175. return GSS_S_FAILURE;
  176. if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
  177. pages))
  178. return GSS_S_FAILURE;
  179. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  180. }
  181. u32
  182. gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
  183. {
  184. struct krb5_ctx *kctx = ctx->internal_ctx_id;
  185. int signalg;
  186. int sealalg;
  187. char cksumdata[16];
  188. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  189. s32 now;
  190. int direction;
  191. s32 seqnum;
  192. unsigned char *ptr;
  193. int bodysize;
  194. void *data_start, *orig_start;
  195. int data_len;
  196. int blocksize;
  197. dprintk("RPC: gss_unwrap_kerberos\n");
  198. ptr = (u8 *)buf->head[0].iov_base + offset;
  199. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  200. buf->len - offset))
  201. return GSS_S_DEFECTIVE_TOKEN;
  202. if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
  203. (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
  204. return GSS_S_DEFECTIVE_TOKEN;
  205. /* XXX sanity-check bodysize?? */
  206. /* get the sign and seal algorithms */
  207. signalg = ptr[2] + (ptr[3] << 8);
  208. if (signalg != SGN_ALG_DES_MAC_MD5)
  209. return GSS_S_DEFECTIVE_TOKEN;
  210. sealalg = ptr[4] + (ptr[5] << 8);
  211. if (sealalg != SEAL_ALG_DES)
  212. return GSS_S_DEFECTIVE_TOKEN;
  213. if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
  214. return GSS_S_DEFECTIVE_TOKEN;
  215. if (gss_decrypt_xdr_buf(kctx->enc, buf,
  216. ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base))
  217. return GSS_S_DEFECTIVE_TOKEN;
  218. if (make_checksum("md5", ptr, 8, buf,
  219. ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
  220. return GSS_S_FAILURE;
  221. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  222. md5cksum.data, md5cksum.len))
  223. return GSS_S_FAILURE;
  224. if (memcmp(md5cksum.data + 8, ptr + GSS_KRB5_TOK_HDR_LEN, 8))
  225. return GSS_S_BAD_SIG;
  226. /* it got through unscathed. Make sure the context is unexpired */
  227. now = get_seconds();
  228. if (now > kctx->endtime)
  229. return GSS_S_CONTEXT_EXPIRED;
  230. /* do sequencing checks */
  231. if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
  232. &direction, &seqnum))
  233. return GSS_S_BAD_SIG;
  234. if ((kctx->initiate && direction != 0xff) ||
  235. (!kctx->initiate && direction != 0))
  236. return GSS_S_BAD_SIG;
  237. /* Copy the data back to the right position. XXX: Would probably be
  238. * better to copy and encrypt at the same time. */
  239. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  240. data_start = ptr + GSS_KRB5_TOK_HDR_LEN + 8 + blocksize;
  241. orig_start = buf->head[0].iov_base + offset;
  242. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  243. memmove(orig_start, data_start, data_len);
  244. buf->head[0].iov_len -= (data_start - orig_start);
  245. buf->len -= (data_start - orig_start);
  246. if (gss_krb5_remove_padding(buf, blocksize))
  247. return GSS_S_DEFECTIVE_TOKEN;
  248. return GSS_S_COMPLETE;
  249. }