gss_krb5_wrap.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_SKB_SUNRPC_DATA);
  55. pad = *(ptr + offset);
  56. kunmap_atomic(ptr, KM_SKB_SUNRPC_DATA);
  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. s32 checksum_type;
  112. char cksumdata[16];
  113. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  114. int blocksize = 0, plainlen;
  115. unsigned char *ptr, *krb5_hdr, *msg_start;
  116. s32 now;
  117. int headlen;
  118. struct page **tmp_pages;
  119. u32 seq_send;
  120. dprintk("RPC: gss_wrap_kerberos\n");
  121. now = get_seconds();
  122. switch (kctx->signalg) {
  123. case SGN_ALG_DES_MAC_MD5:
  124. checksum_type = CKSUMTYPE_RSA_MD5;
  125. break;
  126. default:
  127. dprintk("RPC: gss_krb5_seal: kctx->signalg %d not"
  128. " supported\n", kctx->signalg);
  129. goto out_err;
  130. }
  131. if (kctx->sealalg != SEAL_ALG_NONE && kctx->sealalg != SEAL_ALG_DES) {
  132. dprintk("RPC: gss_krb5_seal: kctx->sealalg %d not supported\n",
  133. kctx->sealalg);
  134. goto out_err;
  135. }
  136. blocksize = crypto_tfm_alg_blocksize(kctx->enc);
  137. gss_krb5_add_padding(buf, offset, blocksize);
  138. BUG_ON((buf->len - offset) % blocksize);
  139. plainlen = blocksize + buf->len - offset;
  140. headlen = g_token_size(&kctx->mech_used, 22 + plainlen) -
  141. (buf->len - offset);
  142. ptr = buf->head[0].iov_base + offset;
  143. /* shift data to make room for header. */
  144. /* XXX Would be cleverer to encrypt while copying. */
  145. /* XXX bounds checking, slack, etc. */
  146. memmove(ptr + headlen, ptr, buf->head[0].iov_len - offset);
  147. buf->head[0].iov_len += headlen;
  148. buf->len += headlen;
  149. BUG_ON((buf->len - offset - headlen) % blocksize);
  150. g_make_token_header(&kctx->mech_used, 22 + plainlen, &ptr);
  151. *ptr++ = (unsigned char) ((KG_TOK_WRAP_MSG>>8)&0xff);
  152. *ptr++ = (unsigned char) (KG_TOK_WRAP_MSG&0xff);
  153. /* ptr now at byte 2 of header described in rfc 1964, section 1.2.1: */
  154. krb5_hdr = ptr - 2;
  155. msg_start = krb5_hdr + 24;
  156. /* XXXJBF: */ BUG_ON(buf->head[0].iov_base + offset + headlen != msg_start + blocksize);
  157. *(u16 *)(krb5_hdr + 2) = htons(kctx->signalg);
  158. memset(krb5_hdr + 4, 0xff, 4);
  159. *(u16 *)(krb5_hdr + 4) = htons(kctx->sealalg);
  160. make_confounder(msg_start, blocksize);
  161. /* XXXJBF: UGH!: */
  162. tmp_pages = buf->pages;
  163. buf->pages = pages;
  164. if (make_checksum(checksum_type, krb5_hdr, 8, buf,
  165. offset + headlen - blocksize, &md5cksum))
  166. goto out_err;
  167. buf->pages = tmp_pages;
  168. switch (kctx->signalg) {
  169. case SGN_ALG_DES_MAC_MD5:
  170. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  171. md5cksum.data, md5cksum.len))
  172. goto out_err;
  173. memcpy(krb5_hdr + 16,
  174. md5cksum.data + md5cksum.len - KRB5_CKSUM_LENGTH,
  175. KRB5_CKSUM_LENGTH);
  176. dprintk("RPC: make_seal_token: cksum data: \n");
  177. print_hexl((u32 *) (krb5_hdr + 16), KRB5_CKSUM_LENGTH, 0);
  178. break;
  179. default:
  180. BUG();
  181. }
  182. spin_lock(&krb5_seq_lock);
  183. seq_send = kctx->seq_send++;
  184. spin_unlock(&krb5_seq_lock);
  185. /* XXX would probably be more efficient to compute checksum
  186. * and encrypt at the same time: */
  187. if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
  188. seq_send, krb5_hdr + 16, krb5_hdr + 8)))
  189. goto out_err;
  190. if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
  191. pages))
  192. goto out_err;
  193. return ((kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE);
  194. out_err:
  195. return GSS_S_FAILURE;
  196. }
  197. u32
  198. gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
  199. {
  200. struct krb5_ctx *kctx = ctx->internal_ctx_id;
  201. int signalg;
  202. int sealalg;
  203. s32 checksum_type;
  204. char cksumdata[16];
  205. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  206. s32 now;
  207. int direction;
  208. s32 seqnum;
  209. unsigned char *ptr;
  210. int bodysize;
  211. u32 ret = GSS_S_DEFECTIVE_TOKEN;
  212. void *data_start, *orig_start;
  213. int data_len;
  214. int blocksize;
  215. dprintk("RPC: gss_unwrap_kerberos\n");
  216. ptr = (u8 *)buf->head[0].iov_base + offset;
  217. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  218. buf->len - offset))
  219. goto out;
  220. if ((*ptr++ != ((KG_TOK_WRAP_MSG>>8)&0xff)) ||
  221. (*ptr++ != (KG_TOK_WRAP_MSG &0xff)) )
  222. goto out;
  223. /* XXX sanity-check bodysize?? */
  224. /* get the sign and seal algorithms */
  225. signalg = ptr[0] + (ptr[1] << 8);
  226. sealalg = ptr[2] + (ptr[3] << 8);
  227. /* Sanity checks */
  228. if ((ptr[4] != 0xff) || (ptr[5] != 0xff))
  229. goto out;
  230. if (sealalg == 0xffff)
  231. goto out;
  232. /* in the current spec, there is only one valid seal algorithm per
  233. key type, so a simple comparison is ok */
  234. if (sealalg != kctx->sealalg)
  235. goto out;
  236. /* there are several mappings of seal algorithms to sign algorithms,
  237. but few enough that we can try them all. */
  238. if ((kctx->sealalg == SEAL_ALG_NONE && signalg > 1) ||
  239. (kctx->sealalg == SEAL_ALG_1 && signalg != SGN_ALG_3) ||
  240. (kctx->sealalg == SEAL_ALG_DES3KD &&
  241. signalg != SGN_ALG_HMAC_SHA1_DES3_KD))
  242. goto out;
  243. if (gss_decrypt_xdr_buf(kctx->enc, buf,
  244. ptr + 22 - (unsigned char *)buf->head[0].iov_base))
  245. goto out;
  246. /* compute the checksum of the message */
  247. /* initialize the the cksum */
  248. switch (signalg) {
  249. case SGN_ALG_DES_MAC_MD5:
  250. checksum_type = CKSUMTYPE_RSA_MD5;
  251. break;
  252. default:
  253. ret = GSS_S_DEFECTIVE_TOKEN;
  254. goto out;
  255. }
  256. switch (signalg) {
  257. case SGN_ALG_DES_MAC_MD5:
  258. ret = make_checksum(checksum_type, ptr - 2, 8, buf,
  259. ptr + 22 - (unsigned char *)buf->head[0].iov_base, &md5cksum);
  260. if (ret)
  261. goto out;
  262. ret = krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  263. md5cksum.data, md5cksum.len);
  264. if (ret)
  265. goto out;
  266. if (memcmp(md5cksum.data + 8, ptr + 14, 8)) {
  267. ret = GSS_S_BAD_SIG;
  268. goto out;
  269. }
  270. break;
  271. default:
  272. ret = GSS_S_DEFECTIVE_TOKEN;
  273. goto out;
  274. }
  275. /* it got through unscathed. Make sure the context is unexpired */
  276. now = get_seconds();
  277. ret = GSS_S_CONTEXT_EXPIRED;
  278. if (now > kctx->endtime)
  279. goto out;
  280. /* do sequencing checks */
  281. ret = GSS_S_BAD_SIG;
  282. if ((ret = krb5_get_seq_num(kctx->seq, ptr + 14, ptr + 6, &direction,
  283. &seqnum)))
  284. goto out;
  285. if ((kctx->initiate && direction != 0xff) ||
  286. (!kctx->initiate && direction != 0))
  287. goto out;
  288. /* Copy the data back to the right position. XXX: Would probably be
  289. * better to copy and encrypt at the same time. */
  290. blocksize = crypto_tfm_alg_blocksize(kctx->enc);
  291. data_start = ptr + 22 + blocksize;
  292. orig_start = buf->head[0].iov_base + offset;
  293. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  294. memmove(orig_start, data_start, data_len);
  295. buf->head[0].iov_len -= (data_start - orig_start);
  296. buf->len -= (data_start - orig_start);
  297. ret = GSS_S_DEFECTIVE_TOKEN;
  298. if (gss_krb5_remove_padding(buf, blocksize))
  299. goto out;
  300. ret = GSS_S_COMPLETE;
  301. out:
  302. return ret;
  303. }