gss_krb5_wrap.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. static u32
  115. gss_wrap_kerberos_v1(struct krb5_ctx *kctx, int offset,
  116. struct xdr_buf *buf, struct page **pages)
  117. {
  118. char cksumdata[16];
  119. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  120. int blocksize = 0, plainlen;
  121. unsigned char *ptr, *msg_start;
  122. s32 now;
  123. int headlen;
  124. struct page **tmp_pages;
  125. u32 seq_send;
  126. dprintk("RPC: gss_wrap_kerberos\n");
  127. now = get_seconds();
  128. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  129. gss_krb5_add_padding(buf, offset, blocksize);
  130. BUG_ON((buf->len - offset) % blocksize);
  131. plainlen = blocksize + buf->len - offset;
  132. headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
  133. (buf->len - offset);
  134. ptr = buf->head[0].iov_base + offset;
  135. /* shift data to make room for header. */
  136. xdr_extend_head(buf, offset, headlen);
  137. /* XXX Would be cleverer to encrypt while copying. */
  138. BUG_ON((buf->len - offset - headlen) % blocksize);
  139. g_make_token_header(&kctx->mech_used,
  140. GSS_KRB5_TOK_HDR_LEN + 8 + plainlen, &ptr);
  141. /* ptr now at header described in rfc 1964, section 1.2.1: */
  142. ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
  143. ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
  144. msg_start = ptr + 24;
  145. *(__be16 *)(ptr + 2) = htons(SGN_ALG_DES_MAC_MD5);
  146. memset(ptr + 4, 0xff, 4);
  147. *(__be16 *)(ptr + 4) = htons(SEAL_ALG_DES);
  148. make_confounder(msg_start, blocksize);
  149. /* XXXJBF: UGH!: */
  150. tmp_pages = buf->pages;
  151. buf->pages = pages;
  152. if (make_checksum("md5", ptr, 8, buf,
  153. offset + headlen - blocksize, &md5cksum))
  154. return GSS_S_FAILURE;
  155. buf->pages = tmp_pages;
  156. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  157. md5cksum.data, md5cksum.len))
  158. return GSS_S_FAILURE;
  159. memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data + md5cksum.len - 8, 8);
  160. spin_lock(&krb5_seq_lock);
  161. seq_send = kctx->seq_send++;
  162. spin_unlock(&krb5_seq_lock);
  163. /* XXX would probably be more efficient to compute checksum
  164. * and encrypt at the same time: */
  165. if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
  166. seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
  167. return GSS_S_FAILURE;
  168. if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
  169. pages))
  170. return GSS_S_FAILURE;
  171. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  172. }
  173. static u32
  174. gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
  175. {
  176. int signalg;
  177. int sealalg;
  178. char cksumdata[16];
  179. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  180. s32 now;
  181. int direction;
  182. s32 seqnum;
  183. unsigned char *ptr;
  184. int bodysize;
  185. void *data_start, *orig_start;
  186. int data_len;
  187. int blocksize;
  188. dprintk("RPC: gss_unwrap_kerberos\n");
  189. ptr = (u8 *)buf->head[0].iov_base + offset;
  190. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  191. buf->len - offset))
  192. return GSS_S_DEFECTIVE_TOKEN;
  193. if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
  194. (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
  195. return GSS_S_DEFECTIVE_TOKEN;
  196. /* XXX sanity-check bodysize?? */
  197. /* get the sign and seal algorithms */
  198. signalg = ptr[2] + (ptr[3] << 8);
  199. if (signalg != SGN_ALG_DES_MAC_MD5)
  200. return GSS_S_DEFECTIVE_TOKEN;
  201. sealalg = ptr[4] + (ptr[5] << 8);
  202. if (sealalg != SEAL_ALG_DES)
  203. return GSS_S_DEFECTIVE_TOKEN;
  204. if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
  205. return GSS_S_DEFECTIVE_TOKEN;
  206. if (gss_decrypt_xdr_buf(kctx->enc, buf,
  207. ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base))
  208. return GSS_S_DEFECTIVE_TOKEN;
  209. if (make_checksum("md5", ptr, 8, buf,
  210. ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
  211. return GSS_S_FAILURE;
  212. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  213. md5cksum.data, md5cksum.len))
  214. return GSS_S_FAILURE;
  215. if (memcmp(md5cksum.data + 8, ptr + GSS_KRB5_TOK_HDR_LEN, 8))
  216. return GSS_S_BAD_SIG;
  217. /* it got through unscathed. Make sure the context is unexpired */
  218. now = get_seconds();
  219. if (now > kctx->endtime)
  220. return GSS_S_CONTEXT_EXPIRED;
  221. /* do sequencing checks */
  222. if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
  223. &direction, &seqnum))
  224. return GSS_S_BAD_SIG;
  225. if ((kctx->initiate && direction != 0xff) ||
  226. (!kctx->initiate && direction != 0))
  227. return GSS_S_BAD_SIG;
  228. /* Copy the data back to the right position. XXX: Would probably be
  229. * better to copy and encrypt at the same time. */
  230. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  231. data_start = ptr + GSS_KRB5_TOK_HDR_LEN + 8 + blocksize;
  232. orig_start = buf->head[0].iov_base + offset;
  233. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  234. memmove(orig_start, data_start, data_len);
  235. buf->head[0].iov_len -= (data_start - orig_start);
  236. buf->len -= (data_start - orig_start);
  237. if (gss_krb5_remove_padding(buf, blocksize))
  238. return GSS_S_DEFECTIVE_TOKEN;
  239. return GSS_S_COMPLETE;
  240. }
  241. u32
  242. gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
  243. struct xdr_buf *buf, struct page **pages)
  244. {
  245. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  246. switch (kctx->enctype) {
  247. default:
  248. BUG();
  249. case ENCTYPE_DES_CBC_RAW:
  250. return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
  251. }
  252. }
  253. u32
  254. gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
  255. {
  256. struct krb5_ctx *kctx = gctx->internal_ctx_id;
  257. switch (kctx->enctype) {
  258. default:
  259. BUG();
  260. case ENCTYPE_DES_CBC_RAW:
  261. return gss_unwrap_kerberos_v1(kctx, offset, buf);
  262. }
  263. }