gss_krb5_wrap.c 8.4 KB

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