Whamcloud - gitweb
- make HEAD from b_post_cmd3
[fs/lustre-release.git] / lustre / utils / gss / context_mit.c
1 /*
2   Copyright (c) 2004-2006 The Regents of the University of Michigan.
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14   3. Neither the name of the University nor the names of its
15      contributors may be used to endorse or promote products derived
16      from this software without specific prior written permission.
17
18   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #ifndef HAVE_LUCID_CONTEXT_SUPPORT
34 #ifdef HAVE_KRB5
35
36 #include <stdio.h>
37 #include <syslog.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <gssapi/gssapi.h>
41 #include <rpc/rpc.h>
42 #include "gss_util.h"
43 #include "gss_oids.h"
44 #include "err_util.h"
45 #include "context.h"
46
47 #include <krb5.h>
48
49 #if (KRB5_VERSION > 131)
50 /* XXX argggg, there's gotta be a better way than just duplicating this
51  * whole struct.  Unfortunately, this is in a "private" header file,
52  * so this is our best choice at this point :-/
53  */
54
55 typedef struct _krb5_gss_ctx_id_rec {
56    unsigned int initiate : 1;   /* nonzero if initiating, zero if accepting */
57    unsigned int established : 1;
58    unsigned int big_endian : 1;
59    unsigned int have_acceptor_subkey : 1;
60    unsigned int seed_init : 1;  /* XXX tested but never actually set */
61 #ifdef CFX_EXERCISE
62    unsigned int testing_unknown_tokid : 1; /* for testing only */
63 #endif
64    OM_uint32 gss_flags;
65    unsigned char seed[16];
66    krb5_principal here;
67    krb5_principal there;
68    krb5_keyblock *subkey;
69    int signalg;
70    size_t cksum_size;
71    int sealalg;
72    krb5_keyblock *enc;
73    krb5_keyblock *seq;
74    krb5_timestamp endtime;
75    krb5_flags krb_flags;
76    /* XXX these used to be signed.  the old spec is inspecific, and
77       the new spec specifies unsigned.  I don't believe that the change
78       affects the wire encoding. */
79    uint64_t seq_send;           /* gssint_uint64 */
80    uint64_t seq_recv;           /* gssint_uint64 */
81    void *seqstate;
82    krb5_auth_context auth_context;
83    gss_OID_desc *mech_used;     /* gss_OID_desc */
84     /* Protocol spec revision
85        0 => RFC 1964 with 3DES and RC4 enhancements
86        1 => draft-ietf-krb-wg-gssapi-cfx-01
87        No others defined so far.  */
88    int proto;
89    krb5_cksumtype cksumtype;    /* for "main" subkey */
90    krb5_keyblock *acceptor_subkey; /* CFX only */
91    krb5_cksumtype acceptor_subkey_cksumtype;
92 #ifdef CFX_EXERCISE
93     gss_buffer_desc init_token;
94 #endif
95 } krb5_gss_ctx_id_rec, *krb5_gss_ctx_id_t;
96
97 #else   /* KRB5_VERSION > 131 */
98
99 typedef struct _krb5_gss_ctx_id_rec {
100         int initiate;
101         u_int32_t gss_flags;
102         int seed_init;
103         unsigned char seed[16];
104         krb5_principal here;
105         krb5_principal there;
106         krb5_keyblock *subkey;
107         int signalg;
108         int cksum_size;
109         int sealalg;
110         krb5_keyblock *enc;
111         krb5_keyblock *seq;
112         krb5_timestamp endtime;
113         krb5_flags krb_flags;
114         krb5_ui_4 seq_send;
115         krb5_ui_4 seq_recv;
116         void *seqstate;
117         int established;
118         int big_endian;
119         krb5_auth_context auth_context;
120         gss_OID_desc *mech_used;
121         int nctypes;
122         krb5_cksumtype *ctypes;
123 } krb5_gss_ctx_id_rec, *krb5_gss_ctx_id_t;
124
125 #endif /* KRB5_VERSION */
126
127
128 static int
129 write_keyblock(char **p, char *end, struct _krb5_keyblock *arg)
130 {
131         gss_buffer_desc tmp;
132
133         if (WRITE_BYTES(p, end, arg->enctype)) return -1;
134         tmp.length = arg->length;
135         tmp.value = arg->contents;
136         if (write_buffer(p, end, &tmp)) return -1;
137         return 0;
138 }
139
140 /*
141  * XXX Hack alert! XXX Do NOT submit upstream!
142  * XXX Hack alert! XXX Do NOT submit upstream!
143  *
144  * We shouldn't be using these definitions
145  *
146  * XXX Hack alert! XXX Do NOT submit upstream!
147  * XXX Hack alert! XXX Do NOT submit upstream!
148  */
149 /* for 3DES */
150 #define KG_USAGE_SEAL 22
151 #define KG_USAGE_SIGN 23
152 #define KG_USAGE_SEQ  24
153
154 /* for rfc???? */
155 #define KG_USAGE_ACCEPTOR_SEAL  22
156 #define KG_USAGE_ACCEPTOR_SIGN  23
157 #define KG_USAGE_INITIATOR_SEAL 24
158 #define KG_USAGE_INITIATOR_SIGN 25
159
160 /* Lifted from mit src/lib/gssapi/krb5/gssapiP_krb5.h */
161 enum seal_alg {
162   SEAL_ALG_NONE            = 0xffff,
163   SEAL_ALG_DES             = 0x0000,
164   SEAL_ALG_1               = 0x0001, /* not published */
165   SEAL_ALG_MICROSOFT_RC4   = 0x0010, /* microsoft w2k;  */
166   SEAL_ALG_DES3KD          = 0x0002
167 };
168
169 #define KEY_USAGE_SEED_ENCRYPTION       0xAA
170 #define KEY_USAGE_SEED_INTEGRITY        0x55
171 #define KEY_USAGE_SEED_CHECKSUM         0x99
172 #define K5CLENGTH 5
173
174 extern void krb5_enc_des3;
175 extern void krb5int_enc_des3;
176 extern void krb5int_enc_arcfour;
177 extern void krb5int_enc_aes128;
178 extern void krb5int_enc_aes256;
179 extern int krb5_derive_key();
180
181 /*
182  * XXX Hack alert! XXX Do NOT submit upstream!
183  * XXX Hack alert! XXX Do NOT submit upstream!
184  *
185  * We should be passing down a single key to the kernel
186  * and it should be deriving the other keys.  We cannot
187  * depend on any of this stuff being accessible in the
188  * future.
189  *
190  * XXX Hack alert! XXX Do NOT submit upstream!
191  * XXX Hack alert! XXX Do NOT submit upstream!
192  */
193 /*
194  * Function to derive a new key from a given key and given constant data.
195  */
196 static krb5_error_code
197 derive_key(const krb5_keyblock *in, krb5_keyblock *out, int usage, char extra)
198 {
199         krb5_error_code code;
200         unsigned char constant_data[K5CLENGTH];
201         krb5_data datain;
202         int keylength;
203         void *enc;
204
205         switch (in->enctype) {
206 #ifdef ENCTYPE_DES3_CBC_RAW
207         case ENCTYPE_DES3_CBC_RAW:
208                 keylength = 24;
209 /* Extra hack, the structure was renamed as rc4 was added... */
210 #if defined(ENCTYPE_ARCFOUR_HMAC)
211                 enc = &krb5int_enc_des3;
212 #else
213                 enc = &krb5_enc_des3;
214 #endif
215                 break;
216 #endif
217 #ifdef ENCTYPE_ARCFOUR_HMAC
218         case ENCTYPE_ARCFOUR_HMAC:
219                 keylength = 16;
220                 enc = &krb5int_enc_arcfour;
221                 break;
222 #endif
223         default:
224                 code = KRB5_BAD_ENCTYPE;
225                 goto out;
226         }
227
228         /* allocate memory for output key */
229         if ((out->contents = malloc(keylength)) == NULL) {
230                 code = ENOMEM;
231                 goto out;
232         }
233         out->length = keylength;
234         out->enctype = in->enctype;
235
236         datain.data = (char *) constant_data;
237         datain.length = K5CLENGTH;
238
239         datain.data[0] = (usage>>24)&0xff;
240         datain.data[1] = (usage>>16)&0xff;
241         datain.data[2] = (usage>>8)&0xff;
242         datain.data[3] = usage&0xff;
243
244         datain.data[4] = (char) extra;
245
246         if ((code = krb5_derive_key(enc, in, out, &datain))) {
247                 free(out->contents);
248                 out->contents = NULL;
249         }
250
251   out:
252         if (code)
253                 printerr(0, "ERROR: derive_key returning error %d (%s)\n",
254                          code, error_message(code));
255         return (code);
256 }
257
258 /*
259  * We really shouldn't know about glue-layer context structure, but
260  * we need to get at the real krb5 context pointer.  This should be
261  * removed as soon as we say there is no support for MIT Kerberos
262  * prior to 1.4 -- which gives us "legal" access to the context info.
263  */
264 typedef struct gss_union_ctx_id_t {
265         gss_OID         mech_type;
266         gss_ctx_id_t    internal_ctx_id;
267 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
268
269 int
270 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
271 {
272         krb5_gss_ctx_id_t kctx = ((gss_union_ctx_id_t)ctx)->internal_ctx_id;
273         char *p, *end;
274         static int constant_zero = 0;
275         static int constant_one = 1;
276         static int constant_two = 2;
277         uint32_t word_seq_send;
278         u_int64_t seq_send_64bit;
279         uint32_t v2_flags = 0;
280         krb5_keyblock derived_key;
281         uint32_t numkeys;
282
283         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
284                 goto out_err;
285         p = buf->value;
286         end = buf->value + MAX_CTX_LEN;
287
288         switch (kctx->sealalg) {
289         case SEAL_ALG_DES:
290                 /* Old format of context to the kernel */
291                 if (kctx->initiate) {
292                         if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
293                 }
294                 else {
295                         if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
296                 }
297                 if (kctx->seed_init) {
298                         if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
299                 }
300                 else {
301                         if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
302                 }
303                 if (write_bytes(&p, end, &kctx->seed, sizeof(kctx->seed)))
304                         goto out_err;
305                 if (WRITE_BYTES(&p, end, kctx->signalg)) goto out_err;
306                 if (WRITE_BYTES(&p, end, kctx->sealalg)) goto out_err;
307                 if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
308                 word_seq_send = kctx->seq_send;
309                 if (WRITE_BYTES(&p, end, word_seq_send)) goto out_err;
310                 if (write_oid(&p, end, kctx->mech_used)) goto out_err;
311
312                 printerr(2, "serialize_krb5_ctx: serializing keys with "
313                          "enctype %d and length %d\n",
314                          kctx->enc->enctype, kctx->enc->length);
315
316                 if (write_keyblock(&p, end, kctx->enc)) goto out_err;
317                 if (write_keyblock(&p, end, kctx->seq)) goto out_err;
318                 break;
319         case SEAL_ALG_MICROSOFT_RC4:
320         case SEAL_ALG_DES3KD:
321                 /* New format of context to the kernel */
322                 /* s32 endtime;
323                  * u32 flags;
324                  * #define KRB5_CTX_FLAG_INITIATOR        0x00000001
325                  * #define KRB5_CTX_FLAG_CFX              0x00000002
326                  * #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY  0x00000004
327                  * u64 seq_send;
328                  * u32  enctype;
329                  * u32  size_of_each_key;    (  size in bytes )
330                  * u32  number_of_keys;      (  N (assumed to be 3 for now) )
331                  * keydata-1;                (  Ke  (Kenc for DES3) )
332                  * keydata-2;                (  Ki  (Kseq for DES3) )
333                  * keydata-3;                (  Kc (derived checksum key) )
334                  */
335                 if (WRITE_BYTES(&p, end, constant_two)) goto out_err;
336                 if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
337
338                 /* Only applicable flag for this is initiator */
339                 if (kctx->initiate) v2_flags |= KRB5_CTX_FLAG_INITIATOR;
340                 if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
341
342                 seq_send_64bit = kctx->seq_send;
343                 if (WRITE_BYTES(&p, end, seq_send_64bit)) goto out_err;
344
345                 if (WRITE_BYTES(&p, end, kctx->enc->enctype)) goto out_err;
346                 if (WRITE_BYTES(&p, end, kctx->enc->length)) goto out_err;
347                 numkeys = 3;
348                 if (WRITE_BYTES(&p, end, numkeys)) goto out_err;
349                 printerr(2, "serialize_krb5_ctx: serializing %d keys with "
350                          "enctype %d and size %d\n",
351                          numkeys, kctx->enc->enctype, kctx->enc->length);
352
353                 /* Ke */
354                 if (write_bytes(&p, end, kctx->enc->contents,
355                                 kctx->enc->length))
356                         goto out_err;
357
358                 /* Ki */
359                 if (write_bytes(&p, end, kctx->enc->contents,
360                                 kctx->enc->length))
361                         goto out_err;
362
363                 /* Kc */
364                 if (derive_key(kctx->seq, &derived_key,
365                                KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM))
366                         goto out_err;
367                 if (write_bytes(&p, end, derived_key.contents,
368                                 derived_key.length))
369                         goto out_err;
370                 free(derived_key.contents);
371                 break;
372         default:
373                 printerr(0, "ERROR: serialize_krb5_ctx: unsupported seal "
374                          "algorithm %d\n", kctx->sealalg);
375                 goto out_err;
376         }
377
378         buf->length = p - (char *)buf->value;
379         return 0;
380
381 out_err:
382         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
383         if (buf->value) {
384                 free(buf->value);
385         }
386         buf->value = NULL;
387         buf->length = 0;
388         return -1;
389 }
390
391 #endif /* HAVE_KRB5 */
392 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */