]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/context_mit.c
Fix misc warning messages
[nfs-utils.git] / utils / gssd / context_mit.c
1 /*
2   Copyright (c) 2004 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 #include <stdio.h>
33 #include <syslog.h>
34 #include <string.h>
35 #include <gssapi/gssapi.h>
36 #include <rpc/rpc.h>
37 #include <rpc/auth_gss.h>
38 #include "gss_util.h"
39 #include "gss_oids.h"
40 #include "err_util.h"
41 #include "context.h"
42
43 #ifdef HAVE_KRB5
44 #include <krb5.h>
45
46 /* XXX spkm3 seems to actually want it this big, yipes. */
47 #define MAX_CTX_LEN 4096
48
49 #ifdef HAVE_LUCID_CONTEXT_SUPPORT
50
51 /* Don't use the private structure, use the exported lucid structure */
52 #include <gssapi/gssapi_krb5.h>
53
54 #elif (KRB5_VERSION > 131)
55 /* XXX argggg, there's gotta be a better way than just duplicating this
56  * whole struct.  Unfortunately, this is in a "private" header file,
57  * so this is our best choice at this point :-/
58  *
59  * XXX Does this match the Heimdal definition?  */
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 #ifdef HAVE_LUCID_CONTEXT_SUPPORT /* Lucid context support */
135 static int
136 write_lucid_keyblock(char **p, char *end, gss_krb5_lucid_key_t *key)
137 {
138         gss_buffer_desc tmp;
139
140         if (WRITE_BYTES(p, end, key->type)) return -1;
141         tmp.length = key->length;
142         tmp.value = key->data;
143         if (write_buffer(p, end, &tmp)) return -1;
144         return 0;
145 }
146
147 static int
148 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
149         gss_buffer_desc *buf)
150 {
151         char *p, *end;
152         static int constant_zero = 0;
153         unsigned char fakeseed[16];
154         uint32_t word_send_seq;
155         gss_krb5_lucid_key_t enc_key;
156         int i;
157         char *skd, *dkd;
158         gss_buffer_desc fakeoid;
159
160         /*
161          * The new Kerberos interface to get the gss context
162          * does not include the seed or seed_init fields
163          * because we never really use them.  But for now,
164          * send down a fake buffer so we can use the same
165          * interface to the kernel.
166          */
167         memset(&enc_key, 0, sizeof(enc_key));
168         memset(&fakeoid, 0, sizeof(fakeoid));
169
170         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
171                 goto out_err;
172         p = buf->value;
173         end = buf->value + MAX_CTX_LEN;
174
175         if (WRITE_BYTES(&p, end, lctx->initiate)) goto out_err;
176
177         /* seed_init and seed not used by kernel anyway */
178         if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
179         if (write_bytes(&p, end, &fakeseed, 16)) goto out_err;
180
181         if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.sign_alg)) goto out_err;
182         if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.seal_alg)) goto out_err;
183         if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
184         word_send_seq = lctx->send_seq; /* XXX send_seq is 64-bit */
185         if (WRITE_BYTES(&p, end, word_send_seq)) goto out_err;
186         if (write_oid(&p, end, &krb5oid)) goto out_err;
187
188         printerr(2, "prepare_krb5_rfc1964_buffer: serializing keys with "
189                  "enctype %d and length %d\n",
190                  lctx->rfc1964_kd.ctx_key.type,
191                  lctx->rfc1964_kd.ctx_key.length);
192
193         /* derive the encryption key and copy it into buffer */
194         enc_key.type = lctx->rfc1964_kd.ctx_key.type;
195         enc_key.length = lctx->rfc1964_kd.ctx_key.length;
196         if ((enc_key.data = calloc(1, enc_key.length)) == NULL)
197                 goto out_err;
198         skd = (char *) lctx->rfc1964_kd.ctx_key.data;
199         dkd = (char *) enc_key.data;
200         for (i = 0; i < enc_key.length; i++)
201                 dkd[i] = skd[i] ^ 0xf0;
202         if (write_lucid_keyblock(&p, end, &enc_key)) {
203                 free(enc_key.data);
204                 goto out_err;
205         }
206         free(enc_key.data);
207
208         if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
209                 goto out_err;
210
211         buf->length = p - (char *)buf->value;
212         return 0;
213 out_err:
214         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
215         if (buf->value) free(buf->value);
216         buf->length = 0;
217         if (enc_key.data) free(enc_key.data);
218         return -1;
219 }
220
221 static int
222 prepare_krb5_rfc_cfx_buffer(gss_krb5_lucid_context_v1_t *lctx,
223         gss_buffer_desc *buf)
224 {
225         printerr(0, "ERROR: prepare_krb5_rfc_cfx_buffer: not implemented\n");
226         return -1;
227 }
228
229
230 int
231 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
232 {
233         OM_uint32 maj_stat, min_stat;
234         void *return_ctx = 0;
235         OM_uint32 vers;
236         gss_krb5_lucid_context_v1_t *lctx = 0;
237         int retcode = 0;
238
239         printerr(2, "DEBUG: serialize_krb5_ctx: lucid version!\n");
240         maj_stat = gss_export_lucid_sec_context(&min_stat, &ctx,
241                                                 1, &return_ctx);
242         if (maj_stat != GSS_S_COMPLETE) {
243                 pgsserr("gss_export_lucid_sec_context",
244                         maj_stat, min_stat, &krb5oid);
245                 goto out_err;
246         }
247
248         /* Check the version returned, we only support v1 right now */
249         vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
250         switch (vers) {
251         case 1:
252                 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
253                 break;
254         default:
255                 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
256                         vers);
257                 goto out_err;
258                 break;
259         }
260
261         /* Now lctx points to a lucid context that we can send down to kernel */
262         if (lctx->protocol == 0)
263                 retcode = prepare_krb5_rfc1964_buffer(lctx, buf);
264         else
265                 retcode = prepare_krb5_rfc_cfx_buffer(lctx, buf);
266
267         maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
268         if (maj_stat != GSS_S_COMPLETE) {
269                 pgsserr("gss_export_lucid_sec_context",
270                         maj_stat, min_stat, &krb5oid);
271                 printerr(0, "WARN: failed to free lucid sec context\n");
272         }
273
274         if (retcode) {
275                 printerr(1, "serialize_krb5_ctx: prepare_krb5_*_buffer "
276                          "failed (retcode = %d)\n", retcode);
277                 goto out_err;
278         }
279
280         return 0;
281
282 out_err:
283         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
284         return -1;
285 }
286
287
288 #else /* HAVE_LUCID_CONTEXT_SUPPORT */
289
290 static int
291 write_keyblock(char **p, char *end, struct _krb5_keyblock *arg)
292 {
293         gss_buffer_desc tmp;
294
295         if (WRITE_BYTES(p, end, arg->enctype)) return -1;
296         tmp.length = arg->length;
297         tmp.value = arg->contents;
298         if (write_buffer(p, end, &tmp)) return -1;
299         return 0;
300 }
301
302 /*
303  * We really shouldn't know about glue-layer context structure, but
304  * we need to get at the real krb5 context pointer.  This should be
305  * removed as soon as we say there is no support for MIT Kerberos
306  * prior to 1.4 -- which gives us "legal" access to the context info.
307  */
308 typedef struct gss_union_ctx_id_t {
309         gss_OID         mech_type;
310         gss_ctx_id_t    internal_ctx_id;
311 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
312
313 int
314 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
315 {
316         krb5_gss_ctx_id_t kctx = ((gss_union_ctx_id_t)ctx)->internal_ctx_id;
317         char *p, *end;
318         static int constant_one = 1;
319         static int constant_zero = 0;
320         uint32_t word_seq_send;
321
322         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
323                 goto out_err;
324         p = buf->value;
325         end = buf->value + MAX_CTX_LEN;
326
327         if (kctx->initiate) {
328                 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
329         }
330         else {
331                 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
332         }
333         if (kctx->seed_init) {
334                 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
335         }
336         else {
337                 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
338         }
339         if (write_bytes(&p, end, &kctx->seed, sizeof(kctx->seed)))
340                 goto out_err;
341         if (WRITE_BYTES(&p, end, kctx->signalg)) goto out_err;
342         if (WRITE_BYTES(&p, end, kctx->sealalg)) goto out_err;
343         if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
344         word_seq_send = kctx->seq_send;
345         if (WRITE_BYTES(&p, end, word_seq_send)) goto out_err;
346         if (write_oid(&p, end, kctx->mech_used)) goto out_err;
347
348         printerr(2, "serialize_krb5_ctx: serializing keys with "
349                  "enctype %d and length %d\n",
350                  kctx->enc->enctype, kctx->enc->length);
351
352         if (write_keyblock(&p, end, kctx->enc)) goto out_err;
353         if (write_keyblock(&p, end, kctx->seq)) goto out_err;
354
355         buf->length = p - (char *)buf->value;
356         return 0;
357 out_err:
358         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
359         if (buf->value) free(buf->value);
360         buf->length = 0;
361         return -1;
362 }
363 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */
364
365 #endif /* HAVE_KRB5 */