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