blob: 3308157436d2931dcef73de989a7ed85ae8824da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/gss_spkm3_token.c
3 *
4 * Copyright (c) 2003 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Andy Adamson <andros@umich.edu>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/types.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/sunrpc/gss_spkm3.h>
40#include <linux/random.h>
41#include <linux/crypto.h>
42
43#ifdef RPC_DEBUG
44# define RPCDBG_FACILITY RPCDBG_AUTH
45#endif
46
47/*
48 * asn1_bitstring_len()
49 *
50 * calculate the asn1 bitstring length of the xdr_netobject
51 */
52void
53asn1_bitstring_len(struct xdr_netobj *in, int *enclen, int *zerobits)
54{
55 int i, zbit = 0,elen = in->len;
56 char *ptr;
57
58 ptr = &in->data[in->len -1];
59
60 /* count trailing 0's */
61 for(i = in->len; i > 0; i--) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080062 if (*ptr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ptr--;
64 elen--;
65 } else
66 break;
67 }
68
69 /* count number of 0 bits in final octet */
70 ptr = &in->data[elen - 1];
71 for(i = 0; i < 8; i++) {
72 short mask = 0x01;
73
74 if (!((mask << i) & *ptr))
75 zbit++;
76 else
77 break;
78 }
79 *enclen = elen;
80 *zerobits = zbit;
81}
82
83/*
84 * decode_asn1_bitstring()
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080085 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * decode a bitstring into a buffer of the expected length.
87 * enclen = bit string length
88 * explen = expected length (define in rfc)
89 */
90int
91decode_asn1_bitstring(struct xdr_netobj *out, char *in, int enclen, int explen)
92{
Trond Myklebust0f38b872008-06-10 18:31:01 -040093 if (!(out->data = kzalloc(explen,GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95 out->len = explen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 memcpy(out->data, in, enclen);
97 return 1;
98}
99
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * SPKMInnerContextToken choice SPKM_MIC asn1 token layout
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800102 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * contextid is always 16 bytes plain data. max asn1 bitstring len = 17.
104 *
105 * tokenlen = pos[0] to end of token (max pos[45] with MD5 cksum)
106 *
107 * pos value
108 * ----------
109 * [0] a4 SPKM-MIC tag
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800110 * [1] ?? innertoken length (max 44)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800112 *
113 * tok_hdr piece of checksum data starts here
114 *
115 * the maximum mic-header len = 9 + 17 = 26
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * mic-header
117 * ----------
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800118 * [2] 30 SEQUENCE tag
119 * [3] ?? mic-header length: (max 23) = TokenID + ContextID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * TokenID - all fields constant and can be hardcoded
122 * -------
123 * [4] 02 Type 2
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800124 * [5] 02 Length 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * [6][7] 01 01 TokenID (SPKM_MIC_TOK)
126 *
127 * ContextID - encoded length not constant, calculated
128 * ---------
129 * [8] 03 Type 3
130 * [9] ?? encoded length
131 * [10] ?? ctxzbit
132 * [11] contextid
133 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800134 * mic_header piece of checksum data ends here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * int-cksum - encoded length not constant, calculated
137 * ---------
138 * [??] 03 Type 3
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800139 * [??] ?? encoded length
140 * [??] ?? md5zbit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * [??] int-cksum (NID_md5 = 16)
142 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800143 * maximum SPKM-MIC innercontext token length =
144 * 10 + encoded contextid_size(17 max) + 2 + encoded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * cksum_size (17 maxfor NID_md5) = 46
146 */
147
148/*
149 * spkm3_mic_header()
150 *
151 * Prepare the SPKM_MIC_TOK mic-header for check-sum calculation
152 * elen: 16 byte context id asn1 bitstring encoded length
153 */
154void
155spkm3_mic_header(unsigned char **hdrbuf, unsigned int *hdrlen, unsigned char *ctxdata, int elen, int zbit)
156{
157 char *hptr = *hdrbuf;
158 char *top = *hdrbuf;
159
160 *(u8 *)hptr++ = 0x30;
161 *(u8 *)hptr++ = elen + 7; /* on the wire header length */
162
163 /* tokenid */
164 *(u8 *)hptr++ = 0x02;
165 *(u8 *)hptr++ = 0x02;
166 *(u8 *)hptr++ = 0x01;
167 *(u8 *)hptr++ = 0x01;
168
169 /* coniextid */
170 *(u8 *)hptr++ = 0x03;
171 *(u8 *)hptr++ = elen + 1; /* add 1 to include zbit */
172 *(u8 *)hptr++ = zbit;
173 memcpy(hptr, ctxdata, elen);
174 hptr += elen;
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -0500175 *hdrlen = hptr - top;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -0500177
178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * spkm3_mic_innercontext_token()
180 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800181 * *tokp points to the beginning of the SPKM_MIC token described
182 * in rfc 2025, section 3.2.1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
J. Bruce Fields9eed1292006-01-03 09:56:00 +0100184 * toklen is the inner token length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
186void
187spkm3_make_mic_token(unsigned char **tokp, int toklen, struct xdr_netobj *mic_hdr, struct xdr_netobj *md5cksum, int md5elen, int md5zbit)
188{
189 unsigned char *ict = *tokp;
190
191 *(u8 *)ict++ = 0xa4;
J. Bruce Fields9eed1292006-01-03 09:56:00 +0100192 *(u8 *)ict++ = toklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 memcpy(ict, mic_hdr->data, mic_hdr->len);
194 ict += mic_hdr->len;
195
196 *(u8 *)ict++ = 0x03;
197 *(u8 *)ict++ = md5elen + 1; /* add 1 to include zbit */
198 *(u8 *)ict++ = md5zbit;
199 memcpy(ict, md5cksum->data, md5elen);
200}
201
202u32
203spkm3_verify_mic_token(unsigned char **tokp, int *mic_hdrlen, unsigned char **cksum)
204{
205 struct xdr_netobj spkm3_ctx_id = {.len =0, .data = NULL};
206 unsigned char *ptr = *tokp;
207 int ctxelen;
208 u32 ret = GSS_S_DEFECTIVE_TOKEN;
209
210 /* spkm3 innercontext token preamble */
211 if ((ptr[0] != 0xa4) || (ptr[2] != 0x30)) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500212 dprintk("RPC: BAD SPKM ictoken preamble\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
214 }
215
216 *mic_hdrlen = ptr[3];
217
218 /* token type */
219 if ((ptr[4] != 0x02) || (ptr[5] != 0x02)) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500220 dprintk("RPC: BAD asn1 SPKM3 token type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto out;
222 }
223
224 /* only support SPKM_MIC_TOK */
225 if((ptr[6] != 0x01) || (ptr[7] != 0x01)) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500226 dprintk("RPC: ERROR unsupported SPKM3 token \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto out;
228 }
229
230 /* contextid */
231 if (ptr[8] != 0x03) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500232 dprintk("RPC: BAD SPKM3 asn1 context-id type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto out;
234 }
235
236 ctxelen = ptr[9];
237 if (ctxelen > 17) { /* length includes asn1 zbit octet */
Chuck Lever8885cb32007-01-31 12:14:05 -0500238 dprintk("RPC: BAD SPKM3 contextid len %d\n", ctxelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
240 }
241
242 /* ignore ptr[10] */
243
244 if(!decode_asn1_bitstring(&spkm3_ctx_id, &ptr[11], ctxelen - 1, 16))
245 goto out;
246
247 /*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800248 * in the current implementation: the optional int-alg is not present
249 * so the default int-alg (md5) is used the optional snd-seq field is
250 * also not present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252
253 if (*mic_hdrlen != 6 + ctxelen) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500254 dprintk("RPC: BAD SPKM_ MIC_TOK header len %d: we only "
255 "support default int-alg (should be absent) "
256 "and do not support snd-seq\n", *mic_hdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto out;
258 }
259 /* checksum */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800260 *cksum = (&ptr[10] + ctxelen); /* ctxelen includes ptr[10] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 ret = GSS_S_COMPLETE;
263out:
Jesper Juhla51482b2005-11-08 09:41:34 -0800264 kfree(spkm3_ctx_id.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return ret;
266}
267