Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 #else
52 #include <liblustre.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <obd_support.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_net.h>
60 #include <lustre_import.h>
61 #include <lustre_sec.h>
62
63 #include "gss_err.h"
64 #include "gss_internal.h"
65 #include "gss_api.h"
66 #include "gss_krb5.h"
67 #include "gss_asn1.h"
68
69
70 /* TWRITE_STR from gssapiP_generic.h */
71 #define TWRITE_STR(ptr, str, len) \
72         memcpy((ptr), (char *) (str), (len)); \
73         (ptr) += (len);
74
75 /* XXXX this code currently makes the assumption that a mech oid will
76    never be longer than 127 bytes.  This assumption is not inherent in
77    the interfaces, so the code can be fixed if the OSI namespace
78    balloons unexpectedly. */
79
80 /* Each token looks like this:
81
82 0x60                                tag for APPLICATION 0, SEQUENCE
83                                         (constructed, definite-length)
84         <length>                possible multiple bytes, need to parse/generate
85         0x06                        tag for OBJECT IDENTIFIER
86                 <moid_length>        compile-time constant string (assume 1 byte)
87                 <moid_bytes>        compile-time constant string
88         <inner_bytes>                the ANY containing the application token
89                                         bytes 0,1 are the token type
90                                         bytes 2,n are the token data
91
92 For the purposes of this abstraction, the token "header" consists of
93 the sequence tag and length octets, the mech OID DER encoding, and the
94 first two inner bytes, which indicate the token type.  The token
95 "body" consists of everything else.
96
97 */
98
99 static
100 int der_length_size(int length)
101 {
102         if (length < (1 << 7))
103                 return 1;
104         else if (length < (1 << 8))
105                 return 2;
106 #if (SIZEOF_INT == 2)
107         else
108                 return 3;
109 #else
110         else if (length < (1 << 16))
111                 return 3;
112         else if (length < (1 << 24))
113                 return 4;
114         else
115                 return 5;
116 #endif
117 }
118
119 static
120 void der_write_length(unsigned char **buf, int length)
121 {
122         if (length < (1 << 7)) {
123                 *(*buf)++ = (unsigned char) length;
124         } else {
125                 *(*buf)++ = (unsigned char) (der_length_size(length) + 127);
126 #if (SIZEOF_INT > 2)
127                 if (length >= (1 << 24))
128                         *(*buf)++ = (unsigned char) (length >> 24);
129                 if (length >= (1 << 16))
130                         *(*buf)++ = (unsigned char) ((length >> 16) & 0xff);
131 #endif
132                 if (length >= (1 << 8))
133                         *(*buf)++ = (unsigned char) ((length >> 8) & 0xff);
134                 *(*buf)++ = (unsigned char) (length & 0xff);
135         }
136 }
137
138 /*
139  * returns decoded length, or < 0 on failure.  Advances buf and
140  * decrements bufsize
141  */
142 static
143 int der_read_length(unsigned char **buf, int *bufsize)
144 {
145         unsigned char sf;
146         int ret;
147
148         if (*bufsize < 1)
149                 return -1;
150         sf = *(*buf)++;
151         (*bufsize)--;
152         if (sf & 0x80) {
153                 if ((sf &= 0x7f) > ((*bufsize) - 1))
154                         return -1;
155                 if (sf > SIZEOF_INT)
156                         return -1;
157                 ret = 0;
158                 for (; sf; sf--) {
159                         ret = (ret << 8) + (*(*buf)++);
160                         (*bufsize)--;
161                 }
162         } else {
163                 ret = sf;
164         }
165
166         return ret;
167 }
168
169 /*
170  * returns the length of a token, given the mech oid and the body size
171  */
172 int g_token_size(rawobj_t *mech, unsigned int body_size)
173 {
174         /* set body_size to sequence contents size */
175         body_size += 4 + (int) mech->len; /* NEED overflow check */
176         return (1 + der_length_size(body_size) + body_size);
177 }
178
179 /*
180  * fills in a buffer with the token header.  The buffer is assumed to
181  * be the right size.  buf is advanced past the token header
182  */
183 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf)
184 {
185         *(*buf)++ = 0x60;
186         der_write_length(buf, 4 + mech->len + body_size);
187         *(*buf)++ = 0x06;
188         *(*buf)++ = (unsigned char) mech->len;
189         TWRITE_STR(*buf, mech->data, ((int) mech->len));
190 }
191
192 /*
193  * Given a buffer containing a token, reads and verifies the token,
194  * leaving buf advanced past the token header, and setting body_size
195  * to the number of remaining bytes.  Returns 0 on success,
196  * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
197  * mechanism in the token does not match the mech argument.  buf and
198  * *body_size are left unmodified on error.
199  */
200 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
201                             unsigned char **buf_in, int toksize)
202 {
203         unsigned char *buf = *buf_in;
204         int seqsize;
205         rawobj_t toid;
206         int ret = 0;
207
208         if ((toksize -= 1) < 0)
209                 return (G_BAD_TOK_HEADER);
210         if (*buf++ != 0x60)
211                 return (G_BAD_TOK_HEADER);
212
213         if ((seqsize = der_read_length(&buf, &toksize)) < 0)
214                 return(G_BAD_TOK_HEADER);
215
216         if (seqsize != toksize)
217                 return (G_BAD_TOK_HEADER);
218
219         if ((toksize -= 1) < 0)
220                 return (G_BAD_TOK_HEADER);
221         if (*buf++ != 0x06)
222                 return (G_BAD_TOK_HEADER);
223  
224         if ((toksize -= 1) < 0)
225                 return (G_BAD_TOK_HEADER);
226         toid.len = *buf++;
227
228         if ((toksize -= toid.len) < 0)
229                 return (G_BAD_TOK_HEADER);
230         toid.data = buf;
231         buf += toid.len;
232
233         if (!g_OID_equal(&toid, mech)) 
234                 ret = G_WRONG_MECH;
235  
236         /* G_WRONG_MECH is not returned immediately because it's more
237          * important to return G_BAD_TOK_HEADER if the token header is
238          * in fact bad
239          */
240         if ((toksize -= 2) < 0)
241                 return (G_BAD_TOK_HEADER);
242
243         if (ret)
244                 return (ret);
245
246         if (!ret) {
247                 *buf_in = buf;
248                 *body_size = toksize;
249         }
250
251         return (ret);
252 }
253
254 /*
255  * Given a buffer containing a token, returns a copy of the mech oid in
256  * the parameter mech.
257  */
258 __u32 g_get_mech_oid(rawobj_t *mech, rawobj_t *in_buf)
259 {
260         unsigned char *buf = in_buf->data;
261         int len = in_buf->len;
262         int ret = 0;
263         int seqsize;
264
265         if ((len -= 1) < 0)
266                 return (G_BAD_TOK_HEADER);
267         if (*buf++ != 0x60)
268                 return (G_BAD_TOK_HEADER);
269
270         if ((seqsize = der_read_length(&buf, &len)) < 0)
271                 return (G_BAD_TOK_HEADER);
272
273         if ((len -= 1) < 0)
274                 return (G_BAD_TOK_HEADER);
275         if (*buf++ != 0x06)
276                 return (G_BAD_TOK_HEADER);
277
278         if ((len -= 1) < 0)
279                 return (G_BAD_TOK_HEADER);
280         mech->len = *buf++;
281
282         if ((len -= mech->len) < 0)
283                 return (G_BAD_TOK_HEADER);
284         OBD_ALLOC(mech->data, mech->len);
285         if (!mech->data) 
286                 return (G_BUFFER_ALLOC);
287         memcpy(mech->data, buf, mech->len);
288
289         return ret;
290 }