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