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