]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/context_mit.c
c804f3a11bc00fe77d1cb9c5877b802f936ebc74
[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_buffer_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_buffer_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_buffer(&p, end, (gss_buffer_desc*)&krb5oid)) goto out_err;
187
188         /* derive the encryption key and copy it into buffer */
189         enc_key.type = lctx->rfc1964_kd.ctx_key.type;
190         enc_key.length = lctx->rfc1964_kd.ctx_key.length;
191         if ((enc_key.data = calloc(1, enc_key.length)) == NULL)
192                 goto out_err;
193         skd = (char *) lctx->rfc1964_kd.ctx_key.data;
194         dkd = (char *) enc_key.data;
195         for (i = 0; i < enc_key.length; i++)
196                 dkd[i] = skd[i] ^ 0xf0;
197         if (write_lucid_keyblock(&p, end, &enc_key)) {
198                 free(enc_key.data);
199                 goto out_err;
200         }
201         free(enc_key.data);
202
203         if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
204                 goto out_err;
205
206         buf->length = p - (char *)buf->value;
207         return 0;
208 out_err:
209         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
210         if (buf->value) free(buf->value);
211         buf->length = 0;
212         if (enc_key.data) free(enc_key.data);
213         return -1;
214 }
215
216 static int
217 prepare_krb5_rfc_cfx_buffer(gss_krb5_lucid_context_v1_t *lctx,
218         gss_buffer_desc *buf)
219 {
220         printerr(0, "ERROR: prepare_krb5_rfc_cfx_buffer: not implemented\n");
221         return -1;
222 }
223
224
225 int
226 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
227 {
228         OM_uint32 maj_stat, min_stat;
229         void *return_ctx = 0;
230         OM_uint32 vers;
231         gss_krb5_lucid_context_v1_t *lctx = 0;
232         int retcode = 0;
233
234         printerr(2, "DEBUG: serialize_krb5_ctx: lucid version!\n");
235         maj_stat = gss_export_lucid_sec_context(&min_stat, ctx,
236                                                 1, &return_ctx);
237         if (maj_stat != GSS_S_COMPLETE) {
238                 pgsserr("gss_export_lucid_sec_context",
239                         maj_stat, min_stat, &krb5oid);
240                 goto out_err;
241         }
242
243         /* Check the version returned, we only support v1 right now */
244         vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
245         switch (vers) {
246         case 1:
247                 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
248                 break;
249         default:
250                 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
251                         vers);
252                 goto out_err;
253                 break;
254         }
255
256         /* Now lctx points to a lucid context that we can send down to kernel */
257         if (lctx->protocol == 0)
258                 retcode = prepare_krb5_rfc1964_buffer(lctx, buf);
259         else
260                 retcode = prepare_krb5_rfc_cfx_buffer(lctx, buf);
261
262         maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
263         if (maj_stat != GSS_S_COMPLETE) {
264                 pgsserr("gss_export_lucid_sec_context",
265                         maj_stat, min_stat, &krb5oid);
266                 printerr(0, "WARN: failed to free lucid sec context\n");
267         }
268
269         if (retcode) {
270                 printerr(1, "serialize_krb5_ctx: prepare_krb5_*_buffer "
271                          "failed (retcode = %d)\n", retcode);
272                 goto out_err;
273         }
274
275         return 0;
276
277 out_err:
278         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
279         return -1;
280 }
281
282
283 #else /* HAVE_LUCID_CONTEXT_SUPPORT */
284
285 static int
286 write_keyblock(char **p, char *end, struct _krb5_keyblock *arg)
287 {
288         gss_buffer_desc tmp;
289
290         if (WRITE_BYTES(p, end, arg->enctype)) return -1;
291         tmp.length = arg->length;
292         tmp.value = arg->contents;
293         if (write_buffer(p, end, &tmp)) return -1;
294         return 0;
295 }
296
297 /*
298  * We really shouldn't know about glue-layer context structure, but
299  * we need to get at the real krb5 context pointer.  This should be
300  * removed as soon as we say there is no support for MIT Kerberos
301  * prior to 1.4 -- which gives us "legal" access to the context info.
302  */
303 typedef struct gss_union_ctx_id_t {
304         gss_OID         mech_type;
305         gss_ctx_id_t    internal_ctx_id;
306 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
307
308 int
309 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
310 {
311         krb5_gss_ctx_id_t kctx = ((gss_union_ctx_id_t)ctx)->internal_ctx_id;
312         char *p, *end;
313         static int constant_one = 1;
314         static int constant_zero = 0;
315         uint32_t word_seq_send;
316
317         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
318                 goto out_err;
319         p = buf->value;
320         end = buf->value + MAX_CTX_LEN;
321
322         if (kctx->initiate) {
323                 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
324         }
325         else {
326                 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
327         }
328         if (kctx->seed_init) {
329                 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
330         }
331         else {
332                 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
333         }
334         if (write_bytes(&p, end, &kctx->seed, sizeof(kctx->seed)))
335                 goto out_err;
336         if (WRITE_BYTES(&p, end, kctx->signalg)) goto out_err;
337         if (WRITE_BYTES(&p, end, kctx->sealalg)) goto out_err;
338         if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
339         word_seq_send = kctx->seq_send;
340         if (WRITE_BYTES(&p, end, word_seq_send)) goto out_err;
341         if (write_buffer(&p, end, kctx->mech_used)) goto out_err;
342         if (write_keyblock(&p, end, kctx->enc)) goto out_err;
343         if (write_keyblock(&p, end, kctx->seq)) goto out_err;
344
345         buf->length = p - (char *)buf->value;
346         return 0;
347 out_err:
348         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
349         if (buf->value) free(buf->value);
350         buf->length = 0;
351         return -1;
352 }
353 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */
354
355 #endif /* HAVE_KRB5 */