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>
11 # define RPCDBG_FACILITY RPCDBG_AUTH
15 gss_krb5_padding(int blocksize
, int length
)
17 /* Most of the code is block-size independent but currently we
19 BUG_ON(blocksize
!= 8);
20 return 8 - (length
& 7);
24 gss_krb5_add_padding(struct xdr_buf
*buf
, int offset
, int blocksize
)
26 int padding
= gss_krb5_padding(blocksize
, buf
->len
- offset
);
30 if (buf
->page_len
|| buf
->tail
[0].iov_len
)
34 p
= iov
->iov_base
+ iov
->iov_len
;
35 iov
->iov_len
+= padding
;
37 memset(p
, padding
, padding
);
41 gss_krb5_remove_padding(struct xdr_buf
*buf
, int blocksize
)
47 if (len
<= buf
->head
[0].iov_len
) {
48 pad
= *(u8
*)(buf
->head
[0].iov_base
+ len
- 1);
49 if (pad
> buf
->head
[0].iov_len
)
51 buf
->head
[0].iov_len
-= pad
;
54 len
-= buf
->head
[0].iov_len
;
55 if (len
<= buf
->page_len
) {
56 int last
= (buf
->page_base
+ len
- 1)
58 int offset
= (buf
->page_base
+ len
- 1)
59 & (PAGE_CACHE_SIZE
- 1);
60 ptr
= kmap_atomic(buf
->pages
[last
], KM_SKB_SUNRPC_DATA
);
61 pad
= *(ptr
+ offset
);
62 kunmap_atomic(ptr
, KM_SKB_SUNRPC_DATA
);
66 BUG_ON(len
> buf
->tail
[0].iov_len
);
67 pad
= *(u8
*)(buf
->tail
[0].iov_base
+ len
- 1);
69 /* XXX: NOTE: we do not adjust the page lengths--they represent
70 * a range of data in the real filesystem page cache, and we need
71 * to know that range so the xdr code can properly place read data.
72 * However adjusting the head length, as we do above, is harmless.
73 * In the case of a request that fits into a single page, the server
74 * also uses length and head length together to determine the original
75 * start of the request to copy the request for deferal; so it's
76 * easier on the server if we adjust head and tail length in tandem.
77 * It's not really a problem that we don't fool with the page and
78 * tail lengths, though--at worst badly formed xdr might lead the
79 * server to attempt to parse the padding.
80 * XXX: Document all these weird requirements for gss mechanism
81 * wrap/unwrap functions. */
92 make_confounder(char *p
, int blocksize
)
97 /* rfc1964 claims this should be "random". But all that's really
98 * necessary is that it be unique. And not even that is necessary in
99 * our case since our "gssapi" implementation exists only to support
100 * rpcsec_gss, so we know that the only buffers we will ever encrypt
101 * already begin with a unique sequence number. Just to hedge my bets
102 * I'll make a half-hearted attempt at something unique, but ensuring
103 * uniqueness would mean worrying about atomicity and rollover, and I
104 * don't care enough. */
106 BUG_ON(blocksize
!= 8);
110 /* Assumptions: the head and tail of inbuf are ours to play with.
111 * The pages, however, may be real pages in the page cache and we replace
112 * them with scratch pages from **pages before writing to them. */
113 /* XXX: obviously the above should be documentation of wrap interface,
114 * and shouldn't be in this kerberos-specific file. */
116 /* XXX factor out common code with seal/unseal. */
119 gss_wrap_kerberos(struct gss_ctx
*ctx
, u32 qop
, int offset
,
120 struct xdr_buf
*buf
, struct page
**pages
)
122 struct krb5_ctx
*kctx
= ctx
->internal_ctx_id
;
124 struct xdr_netobj md5cksum
= {.len
= 0, .data
= NULL
};
125 int blocksize
= 0, plainlen
;
126 unsigned char *ptr
, *krb5_hdr
, *msg_start
;
129 struct page
**tmp_pages
;
131 dprintk("RPC: gss_wrap_kerberos\n");
138 switch (kctx
->signalg
) {
139 case SGN_ALG_DES_MAC_MD5
:
140 checksum_type
= CKSUMTYPE_RSA_MD5
;
143 dprintk("RPC: gss_krb5_seal: kctx->signalg %d not"
144 " supported\n", kctx
->signalg
);
147 if (kctx
->sealalg
!= SEAL_ALG_NONE
&& kctx
->sealalg
!= SEAL_ALG_DES
) {
148 dprintk("RPC: gss_krb5_seal: kctx->sealalg %d not supported\n",
153 blocksize
= crypto_tfm_alg_blocksize(kctx
->enc
);
154 gss_krb5_add_padding(buf
, offset
, blocksize
);
155 BUG_ON((buf
->len
- offset
) % blocksize
);
156 plainlen
= blocksize
+ buf
->len
- offset
;
158 headlen
= g_token_size(&kctx
->mech_used
, 22 + plainlen
) -
161 ptr
= buf
->head
[0].iov_base
+ offset
;
162 /* shift data to make room for header. */
163 /* XXX Would be cleverer to encrypt while copying. */
164 /* XXX bounds checking, slack, etc. */
165 memmove(ptr
+ headlen
, ptr
, buf
->head
[0].iov_len
- offset
);
166 buf
->head
[0].iov_len
+= headlen
;
168 BUG_ON((buf
->len
- offset
- headlen
) % blocksize
);
170 g_make_token_header(&kctx
->mech_used
, 22 + plainlen
, &ptr
);
173 *ptr
++ = (unsigned char) ((KG_TOK_WRAP_MSG
>>8)&0xff);
174 *ptr
++ = (unsigned char) (KG_TOK_WRAP_MSG
&0xff);
176 /* ptr now at byte 2 of header described in rfc 1964, section 1.2.1: */
178 msg_start
= krb5_hdr
+ 24;
179 /* XXXJBF: */ BUG_ON(buf
->head
[0].iov_base
+ offset
+ headlen
!= msg_start
+ blocksize
);
181 *(u16
*)(krb5_hdr
+ 2) = htons(kctx
->signalg
);
182 memset(krb5_hdr
+ 4, 0xff, 4);
183 *(u16
*)(krb5_hdr
+ 4) = htons(kctx
->sealalg
);
185 make_confounder(msg_start
, blocksize
);
188 tmp_pages
= buf
->pages
;
190 if (make_checksum(checksum_type
, krb5_hdr
, 8, buf
,
191 offset
+ headlen
- blocksize
, &md5cksum
))
193 buf
->pages
= tmp_pages
;
195 switch (kctx
->signalg
) {
196 case SGN_ALG_DES_MAC_MD5
:
197 if (krb5_encrypt(kctx
->seq
, NULL
, md5cksum
.data
,
198 md5cksum
.data
, md5cksum
.len
))
200 memcpy(krb5_hdr
+ 16,
201 md5cksum
.data
+ md5cksum
.len
- KRB5_CKSUM_LENGTH
,
204 dprintk("RPC: make_seal_token: cksum data: \n");
205 print_hexl((u32
*) (krb5_hdr
+ 16), KRB5_CKSUM_LENGTH
, 0);
211 kfree(md5cksum
.data
);
213 /* XXX would probably be more efficient to compute checksum
214 * and encrypt at the same time: */
215 if ((krb5_make_seq_num(kctx
->seq
, kctx
->initiate
? 0 : 0xff,
216 kctx
->seq_send
, krb5_hdr
+ 16, krb5_hdr
+ 8)))
219 if (gss_encrypt_xdr_buf(kctx
->enc
, buf
, offset
+ headlen
- blocksize
,
225 return ((kctx
->endtime
< now
) ? GSS_S_CONTEXT_EXPIRED
: GSS_S_COMPLETE
);
227 if (md5cksum
.data
) kfree(md5cksum
.data
);
228 return GSS_S_FAILURE
;
232 gss_unwrap_kerberos(struct gss_ctx
*ctx
, u32
*qop
, int offset
,
235 struct krb5_ctx
*kctx
= ctx
->internal_ctx_id
;
239 struct xdr_netobj md5cksum
= {.len
= 0, .data
= NULL
};
245 u32 ret
= GSS_S_DEFECTIVE_TOKEN
;
246 void *data_start
, *orig_start
;
250 dprintk("RPC: gss_unwrap_kerberos\n");
252 ptr
= (u8
*)buf
->head
[0].iov_base
+ offset
;
253 if (g_verify_token_header(&kctx
->mech_used
, &bodysize
, &ptr
,
257 if ((*ptr
++ != ((KG_TOK_WRAP_MSG
>>8)&0xff)) ||
258 (*ptr
++ != (KG_TOK_WRAP_MSG
&0xff)) )
261 /* XXX sanity-check bodysize?? */
263 /* get the sign and seal algorithms */
265 signalg
= ptr
[0] + (ptr
[1] << 8);
266 sealalg
= ptr
[2] + (ptr
[3] << 8);
270 if ((ptr
[4] != 0xff) || (ptr
[5] != 0xff))
273 if (sealalg
== 0xffff)
276 /* in the current spec, there is only one valid seal algorithm per
277 key type, so a simple comparison is ok */
279 if (sealalg
!= kctx
->sealalg
)
282 /* there are several mappings of seal algorithms to sign algorithms,
283 but few enough that we can try them all. */
285 if ((kctx
->sealalg
== SEAL_ALG_NONE
&& signalg
> 1) ||
286 (kctx
->sealalg
== SEAL_ALG_1
&& signalg
!= SGN_ALG_3
) ||
287 (kctx
->sealalg
== SEAL_ALG_DES3KD
&&
288 signalg
!= SGN_ALG_HMAC_SHA1_DES3_KD
))
291 if (gss_decrypt_xdr_buf(kctx
->enc
, buf
,
292 ptr
+ 22 - (unsigned char *)buf
->head
[0].iov_base
))
295 /* compute the checksum of the message */
297 /* initialize the the cksum */
299 case SGN_ALG_DES_MAC_MD5
:
300 checksum_type
= CKSUMTYPE_RSA_MD5
;
303 ret
= GSS_S_DEFECTIVE_TOKEN
;
308 case SGN_ALG_DES_MAC_MD5
:
309 ret
= make_checksum(checksum_type
, ptr
- 2, 8, buf
,
310 ptr
+ 22 - (unsigned char *)buf
->head
[0].iov_base
, &md5cksum
);
314 ret
= krb5_encrypt(kctx
->seq
, NULL
, md5cksum
.data
,
315 md5cksum
.data
, md5cksum
.len
);
319 if (memcmp(md5cksum
.data
+ 8, ptr
+ 14, 8)) {
325 ret
= GSS_S_DEFECTIVE_TOKEN
;
329 /* it got through unscathed. Make sure the context is unexpired */
332 *qop
= GSS_C_QOP_DEFAULT
;
336 ret
= GSS_S_CONTEXT_EXPIRED
;
337 if (now
> kctx
->endtime
)
340 /* do sequencing checks */
343 if ((ret
= krb5_get_seq_num(kctx
->seq
, ptr
+ 14, ptr
+ 6, &direction
,
347 if ((kctx
->initiate
&& direction
!= 0xff) ||
348 (!kctx
->initiate
&& direction
!= 0))
351 /* Copy the data back to the right position. XXX: Would probably be
352 * better to copy and encrypt at the same time. */
354 blocksize
= crypto_tfm_alg_blocksize(kctx
->enc
);
355 data_start
= ptr
+ 22 + blocksize
;
356 orig_start
= buf
->head
[0].iov_base
+ offset
;
357 data_len
= (buf
->head
[0].iov_base
+ buf
->head
[0].iov_len
) - data_start
;
358 memmove(orig_start
, data_start
, data_len
);
359 buf
->head
[0].iov_len
-= (data_start
- orig_start
);
360 buf
->len
-= (data_start
- orig_start
);
362 ret
= GSS_S_DEFECTIVE_TOKEN
;
363 if (gss_krb5_remove_padding(buf
, blocksize
))
366 ret
= GSS_S_COMPLETE
;
368 if (md5cksum
.data
) kfree(md5cksum
.data
);