gss_krb5_wrap.c 8.4 KB

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