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