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