Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_generic_token.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modifications for Lustre
5  * Copyright 2004 - 2006, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 /*
11  *  linux/net/sunrpc/gss_generic_token.c
12  *
13  *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/generic/util_token.c
14  *
15  *  Copyright (c) 2000 The Regents of the University of Michigan.
16  *  All rights reserved.
17  *
18  *  Andy Adamson   <andros@umich.edu>
19  */
20
21 /*
22  * Copyright 1993 by OpenVision Technologies, Inc.
23  *
24  * Permission to use, copy, modify, distribute, and sell this software
25  * and its documentation for any purpose is hereby granted without fee,
26  * provided that the above copyright notice appears in all copies and
27  * that both that copyright notice and this permission notice appear in
28  * supporting documentation, and that the name of OpenVision not be used
29  * in advertising or publicity pertaining to distribution of the software
30  * without specific, written prior permission. OpenVision makes no
31  * representations about the suitability of this software for any
32  * purpose.  It is provided "as is" without express or implied warranty.
33  *
34  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
35  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
36  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
37  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
38  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
39  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
40  * PERFORMANCE OF THIS SOFTWARE.
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_SEC
47 #ifdef __KERNEL__
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
51 #include <linux/mutex.h>
52 #else
53 #include <liblustre.h>
54 #endif
55
56 #include <obd.h>
57 #include <obd_class.h>
58 #include <obd_support.h>
59 #include <lustre/lustre_idl.h>
60 #include <lustre_net.h>
61 #include <lustre_import.h>
62 #include <lustre_sec.h>
63
64 #include "gss_err.h"
65 #include "gss_internal.h"
66 #include "gss_api.h"
67 #include "gss_krb5.h"
68 #include "gss_asn1.h"
69
70
71 /* TWRITE_STR from gssapiP_generic.h */
72 #define TWRITE_STR(ptr, str, len) \
73         memcpy((ptr), (char *) (str), (len)); \
74         (ptr) += (len);
75
76 /* XXXX this code currently makes the assumption that a mech oid will
77    never be longer than 127 bytes.  This assumption is not inherent in
78    the interfaces, so the code can be fixed if the OSI namespace
79    balloons unexpectedly. */
80
81 /* Each token looks like this:
82
83 0x60                                tag for APPLICATION 0, SEQUENCE
84                                         (constructed, definite-length)
85         <length>                possible multiple bytes, need to parse/generate
86         0x06                        tag for OBJECT IDENTIFIER
87                 <moid_length>        compile-time constant string (assume 1 byte)
88                 <moid_bytes>        compile-time constant string
89         <inner_bytes>                the ANY containing the application token
90                                         bytes 0,1 are the token type
91                                         bytes 2,n are the token data
92
93 For the purposes of this abstraction, the token "header" consists of
94 the sequence tag and length octets, the mech OID DER encoding, and the
95 first two inner bytes, which indicate the token type.  The token
96 "body" consists of everything else.
97
98 */
99
100 static
101 int der_length_size(int length)
102 {
103         if (length < (1 << 7))
104                 return 1;
105         else if (length < (1 << 8))
106                 return 2;
107 #if (SIZEOF_INT == 2)
108         else
109                 return 3;
110 #else
111         else if (length < (1 << 16))
112                 return 3;
113         else if (length < (1 << 24))
114                 return 4;
115         else
116                 return 5;
117 #endif
118 }
119
120 static
121 void der_write_length(unsigned char **buf, int length)
122 {
123         if (length < (1 << 7)) {
124                 *(*buf)++ = (unsigned char) length;
125         } else {
126                 *(*buf)++ = (unsigned char) (der_length_size(length) + 127);
127 #if (SIZEOF_INT > 2)
128                 if (length >= (1 << 24))
129                         *(*buf)++ = (unsigned char) (length >> 24);
130                 if (length >= (1 << 16))
131                         *(*buf)++ = (unsigned char) ((length >> 16) & 0xff);
132 #endif
133                 if (length >= (1 << 8))
134                         *(*buf)++ = (unsigned char) ((length >> 8) & 0xff);
135                 *(*buf)++ = (unsigned char) (length & 0xff);
136         }
137 }
138
139 /*
140  * returns decoded length, or < 0 on failure.  Advances buf and
141  * decrements bufsize
142  */
143 static
144 int der_read_length(unsigned char **buf, int *bufsize)
145 {
146         unsigned char sf;
147         int ret;
148
149         if (*bufsize < 1)
150                 return -1;
151         sf = *(*buf)++;
152         (*bufsize)--;
153         if (sf & 0x80) {
154                 if ((sf &= 0x7f) > ((*bufsize) - 1))
155                         return -1;
156                 if (sf > SIZEOF_INT)
157                         return -1;
158                 ret = 0;
159                 for (; sf; sf--) {
160                         ret = (ret << 8) + (*(*buf)++);
161                         (*bufsize)--;
162                 }
163         } else {
164                 ret = sf;
165         }
166
167         return ret;
168 }
169
170 /*
171  * returns the length of a token, given the mech oid and the body size
172  */
173 int g_token_size(rawobj_t *mech, unsigned int body_size)
174 {
175         /* set body_size to sequence contents size */
176         body_size += 4 + (int) mech->len; /* NEED overflow check */
177         return (1 + der_length_size(body_size) + body_size);
178 }
179
180 /*
181  * fills in a buffer with the token header.  The buffer is assumed to
182  * be the right size.  buf is advanced past the token header
183  */
184 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf)
185 {
186         *(*buf)++ = 0x60;
187         der_write_length(buf, 4 + mech->len + body_size);
188         *(*buf)++ = 0x06;
189         *(*buf)++ = (unsigned char) mech->len;
190         TWRITE_STR(*buf, mech->data, ((int) mech->len));
191 }
192
193 /*
194  * Given a buffer containing a token, reads and verifies the token,
195  * leaving buf advanced past the token header, and setting body_size
196  * to the number of remaining bytes.  Returns 0 on success,
197  * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
198  * mechanism in the token does not match the mech argument.  buf and
199  * *body_size are left unmodified on error.
200  */
201 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
202                             unsigned char **buf_in, int toksize)
203 {
204         unsigned char *buf = *buf_in;
205         int seqsize;
206         rawobj_t toid;
207         int ret = 0;
208
209         if ((toksize -= 1) < 0)
210                 return (G_BAD_TOK_HEADER);
211         if (*buf++ != 0x60)
212                 return (G_BAD_TOK_HEADER);
213
214         if ((seqsize = der_read_length(&buf, &toksize)) < 0)
215                 return(G_BAD_TOK_HEADER);
216
217         if (seqsize != toksize)
218                 return (G_BAD_TOK_HEADER);
219
220         if ((toksize -= 1) < 0)
221                 return (G_BAD_TOK_HEADER);
222         if (*buf++ != 0x06)
223                 return (G_BAD_TOK_HEADER);
224  
225         if ((toksize -= 1) < 0)
226                 return (G_BAD_TOK_HEADER);
227         toid.len = *buf++;
228
229         if ((toksize -= toid.len) < 0)
230                 return (G_BAD_TOK_HEADER);
231         toid.data = buf;
232         buf += toid.len;
233
234         if (!g_OID_equal(&toid, mech)) 
235                 ret = G_WRONG_MECH;
236  
237         /* G_WRONG_MECH is not returned immediately because it's more
238          * important to return G_BAD_TOK_HEADER if the token header is
239          * in fact bad
240          */
241         if ((toksize -= 2) < 0)
242                 return (G_BAD_TOK_HEADER);
243
244         if (ret)
245                 return (ret);
246
247         if (!ret) {
248                 *buf_in = buf;
249                 *body_size = toksize;
250         }
251
252         return (ret);
253 }
254
255 /*
256  * Given a buffer containing a token, returns a copy of the mech oid in
257  * the parameter mech.
258  */
259 __u32 g_get_mech_oid(rawobj_t *mech, rawobj_t *in_buf)
260 {
261         unsigned char *buf = in_buf->data;
262         int len = in_buf->len;
263         int ret = 0;
264         int seqsize;
265
266         if ((len -= 1) < 0)
267                 return (G_BAD_TOK_HEADER);
268         if (*buf++ != 0x60)
269                 return (G_BAD_TOK_HEADER);
270
271         if ((seqsize = der_read_length(&buf, &len)) < 0)
272                 return (G_BAD_TOK_HEADER);
273
274         if ((len -= 1) < 0)
275                 return (G_BAD_TOK_HEADER);
276         if (*buf++ != 0x06)
277                 return (G_BAD_TOK_HEADER);
278
279         if ((len -= 1) < 0)
280                 return (G_BAD_TOK_HEADER);
281         mech->len = *buf++;
282
283         if ((len -= mech->len) < 0)
284                 return (G_BAD_TOK_HEADER);
285         OBD_ALLOC(mech->data, mech->len);
286         if (!mech->data) 
287                 return (G_BUFFER_ALLOC);
288         memcpy(mech->data, buf, mech->len);
289
290         return ret;
291 }