]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/context_lucid.c
Merge branch 'sid'
[nfs-utils.git] / utils / gssd / context_lucid.c
1 /*
2  * COPYRIGHT (c) 2006
3  * The Regents of the University of Michigan
4  * ALL RIGHTS RESERVED
5  *
6  * Permission is granted to use, copy, create derivative works
7  * and redistribute this software and such derivative works
8  * for any purpose, so long as the name of The University of
9  * Michigan is not used in any advertising or publicity
10  * pertaining to the use of distribution of this software
11  * without specific, written prior authorization.  If the
12  * above copyright notice or any other identification of the
13  * University of Michigan is included in any copy of any
14  * portion of this software, then the disclaimer below must
15  * also be included.
16  *
17  * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18  * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19  * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
20  * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21  * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23  * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24  * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25  * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26  * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27  * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGES.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif  /* HAVE_CONFIG_H */
34
35 #ifdef HAVE_LUCID_CONTEXT_SUPPORT
36
37 /*
38  * Newer versions of MIT and Heimdal have lucid context support.
39  * We can use common code if it is supported.
40  */
41
42 #include <stdio.h>
43 #include <syslog.h>
44 #include <string.h>
45 #include <errno.h>
46
47 #include <gssapi/gssapi_krb5.h>
48
49 #include "gss_util.h"
50 #include "gss_oids.h"
51 #include "err_util.h"
52 #include "context.h"
53
54 #ifndef OM_uint64
55 typedef uint64_t OM_uint64;
56 #endif
57
58 static int
59 write_lucid_keyblock(char **p, char *end, gss_krb5_lucid_key_t *key)
60 {
61         gss_buffer_desc tmp;
62
63         if (WRITE_BYTES(p, end, key->type)) return -1;
64         tmp.length = key->length;
65         tmp.value = key->data;
66         if (write_buffer(p, end, &tmp)) return -1;
67         return 0;
68 }
69
70 static int
71 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
72         gss_buffer_desc *buf, int32_t *endtime)
73 {
74 #define FAKESEED_SIZE 16
75         char *p, *end;
76         static int constant_zero = 0;
77         unsigned char fakeseed[FAKESEED_SIZE];
78         uint32_t word_send_seq;
79         gss_krb5_lucid_key_t enc_key;
80         uint32_t i;
81         char *skd, *dkd;
82         gss_buffer_desc fakeoid;
83         int err;
84
85         /*
86          * The new Kerberos interface to get the gss context
87          * does not include the seed or seed_init fields
88          * because we never really use them.  But for now,
89          * send down a fake buffer so we can use the same
90          * interface to the kernel.
91          */
92         memset(&enc_key, 0, sizeof(enc_key));
93         memset(&fakeoid, 0, sizeof(fakeoid));
94         memset(fakeseed, 0, FAKESEED_SIZE);
95
96         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
97                 goto out_err;
98         p = buf->value;
99         end = buf->value + MAX_CTX_LEN;
100
101         if (WRITE_BYTES(&p, end, lctx->initiate)) goto out_err;
102
103         /* seed_init and seed not used by kernel anyway */
104         if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
105         if (write_bytes(&p, end, &fakeseed, FAKESEED_SIZE)) goto out_err;
106
107         if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.sign_alg)) goto out_err;
108         if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.seal_alg)) goto out_err;
109         if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
110         if (endtime)
111                 *endtime = lctx->endtime;
112         word_send_seq = lctx->send_seq; /* XXX send_seq is 64-bit */
113         if (WRITE_BYTES(&p, end, word_send_seq)) goto out_err;
114         if (write_oid(&p, end, &krb5oid)) goto out_err;
115
116 #ifdef HAVE_HEIMDAL
117         /*
118          * The kernel gss code expects des-cbc-raw for all flavors of des.
119          * The keytype from MIT has this type, but Heimdal does not.
120          * Force the Heimdal keytype to 4 (des-cbc-raw).
121          * Note that the rfc1964 version only supports DES enctypes.
122          */
123         if (lctx->rfc1964_kd.ctx_key.type != 4) {
124                 printerr(2, "%s: overriding heimdal keytype (%d => %d)\n",
125                          __FUNCTION__, lctx->rfc1964_kd.ctx_key.type, 4);
126                 lctx->rfc1964_kd.ctx_key.type = 4;
127         }
128 #endif
129         printerr(2, "%s: serializing keys with enctype %d and length %d\n",
130                  __FUNCTION__, lctx->rfc1964_kd.ctx_key.type,
131                  lctx->rfc1964_kd.ctx_key.length);
132
133         /* derive the encryption key and copy it into buffer */
134         enc_key.type = lctx->rfc1964_kd.ctx_key.type;
135         enc_key.length = lctx->rfc1964_kd.ctx_key.length;
136         if ((enc_key.data = calloc(1, enc_key.length)) == NULL)
137                 goto out_err;
138         skd = (char *) lctx->rfc1964_kd.ctx_key.data;
139         dkd = (char *) enc_key.data;
140         for (i = 0; i < enc_key.length; i++)
141                 dkd[i] = skd[i] ^ 0xf0;
142         err = write_lucid_keyblock(&p, end, &enc_key);
143         free(enc_key.data);
144         if (err)
145                 goto out_err;
146
147         if (write_lucid_keyblock(&p, end, &lctx->rfc1964_kd.ctx_key))
148                 goto out_err;
149
150         buf->length = p - (char *)buf->value;
151         return 0;
152 out_err:
153         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
154         if (buf->value) free(buf->value);
155         buf->length = 0;
156         return -1;
157 }
158
159 /* Flags for version 2 context flags */
160 #define KRB5_CTX_FLAG_INITIATOR         0x00000001
161 #define KRB5_CTX_FLAG_CFX               0x00000002
162 #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY   0x00000004
163
164 /*
165  * Prepare a new-style buffer, as defined in rfc4121 (a.k.a. cfx),
166  * to send to the kernel for newer encryption types -- or for DES3.
167  *
168  * The new format is:
169  *
170  *      u32 flags;
171  *      #define KRB5_CTX_FLAG_INITIATOR         0x00000001
172  *      #define KRB5_CTX_FLAG_CFX               0x00000002
173  *      #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY   0x00000004
174  *      s32 endtime;
175  *      u64 seq_send;
176  *      u32  enctype;                   ( encrption type of key )
177  *      raw key;                        ( raw key bytes (kernel will derive))
178  *
179  */
180 static int
181 prepare_krb5_rfc4121_buffer(gss_krb5_lucid_context_v1_t *lctx,
182         gss_buffer_desc *buf, int32_t *endtime)
183 {
184         char *p, *end;
185         uint32_t v2_flags = 0;
186         uint32_t enctype;
187         uint32_t keysize;
188
189         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
190                 goto out_err;
191         p = buf->value;
192         end = buf->value + MAX_CTX_LEN;
193
194         /* Version 2 */
195         if (lctx->initiate)
196                 v2_flags |= KRB5_CTX_FLAG_INITIATOR;
197         if (lctx->protocol != 0)
198                 v2_flags |= KRB5_CTX_FLAG_CFX;
199         if (lctx->protocol != 0 && lctx->cfx_kd.have_acceptor_subkey == 1)
200                 v2_flags |= KRB5_CTX_FLAG_ACCEPTOR_SUBKEY;
201
202         if (WRITE_BYTES(&p, end, v2_flags)) goto out_err;
203         if (WRITE_BYTES(&p, end, lctx->endtime)) goto out_err;
204         if (endtime)
205                 *endtime = lctx->endtime;
206         if (WRITE_BYTES(&p, end, lctx->send_seq)) goto out_err;
207
208         /* Protocol 0 here implies DES3 or RC4 */
209         printerr(2, "%s: protocol %d\n", __FUNCTION__, lctx->protocol);
210         if (lctx->protocol == 0) {
211                 enctype = lctx->rfc1964_kd.ctx_key.type;
212                 keysize = lctx->rfc1964_kd.ctx_key.length;
213         } else {
214                 if (lctx->cfx_kd.have_acceptor_subkey) {
215                         enctype = lctx->cfx_kd.acceptor_subkey.type;
216                         keysize = lctx->cfx_kd.acceptor_subkey.length;
217                 } else {
218                         enctype = lctx->cfx_kd.ctx_key.type;
219                         keysize = lctx->cfx_kd.ctx_key.length;
220                 }
221         }
222         printerr(2, "%s: serializing key with enctype %d and size %d\n",
223                  __FUNCTION__, enctype, keysize);
224
225         if (WRITE_BYTES(&p, end, enctype)) goto out_err;
226
227         if (lctx->protocol == 0) {
228                 if (write_bytes(&p, end, lctx->rfc1964_kd.ctx_key.data,
229                                 lctx->rfc1964_kd.ctx_key.length))
230                         goto out_err;
231         } else {
232                 if (lctx->cfx_kd.have_acceptor_subkey) {
233                         if (write_bytes(&p, end,
234                                         lctx->cfx_kd.acceptor_subkey.data,
235                                         lctx->cfx_kd.acceptor_subkey.length))
236                                 goto out_err;
237                 } else {
238                         if (write_bytes(&p, end, lctx->cfx_kd.ctx_key.data,
239                                         lctx->cfx_kd.ctx_key.length))
240                                 goto out_err;
241                 }
242         }
243
244         buf->length = p - (char *)buf->value;
245         return 0;
246
247 out_err:
248         printerr(0, "ERROR: %s: failed serializing krb5 context for kernel\n",
249                  __FUNCTION__);
250         if (buf->value) {
251                 free(buf->value);
252                 buf->value = NULL;
253         }
254         buf->length = 0;
255         return -1;
256 }
257
258
259 int
260 serialize_krb5_ctx(gss_ctx_id_t *ctx, gss_buffer_desc *buf, int32_t *endtime)
261 {
262         OM_uint32 maj_stat, min_stat;
263         void *return_ctx = 0;
264         OM_uint32 vers;
265         gss_krb5_lucid_context_v1_t *lctx = 0;
266         int retcode = 0;
267
268         printerr(2, "DEBUG: %s: lucid version!\n", __FUNCTION__);
269         maj_stat = gss_export_lucid_sec_context(&min_stat, ctx,
270                                                 1, &return_ctx);
271         if (maj_stat != GSS_S_COMPLETE) {
272                 pgsserr("gss_export_lucid_sec_context",
273                         maj_stat, min_stat, &krb5oid);
274                 goto out_err;
275         }
276
277         /* Check the version returned, we only support v1 right now */
278         vers = ((gss_krb5_lucid_context_version_t *)return_ctx)->version;
279         switch (vers) {
280         case 1:
281                 lctx = (gss_krb5_lucid_context_v1_t *) return_ctx;
282                 break;
283         default:
284                 printerr(0, "ERROR: unsupported lucid sec context version %d\n",
285                         vers);
286                 goto out_err;
287                 break;
288         }
289
290         /*
291          * Now lctx points to a lucid context that we can send down to kernel
292          *
293          * Note: we send down different information to the kernel depending
294          * on the protocol version and the enctyption type.
295          * For protocol version 0 with all enctypes besides DES3, we use
296          * the original format.  For protocol version != 0 or DES3, we
297          * send down the new style information.
298          */
299
300         if (lctx->protocol == 0 && lctx->rfc1964_kd.ctx_key.type <= 4)
301                 retcode = prepare_krb5_rfc1964_buffer(lctx, buf, endtime);
302         else
303                 retcode = prepare_krb5_rfc4121_buffer(lctx, buf, endtime);
304
305         maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, return_ctx);
306         if (maj_stat != GSS_S_COMPLETE) {
307                 pgsserr("gss_free_lucid_sec_context",
308                         maj_stat, min_stat, &krb5oid);
309                 printerr(0, "WARN: failed to free lucid sec context\n");
310         }
311
312         if (retcode) {
313                 printerr(1, "%s: prepare_krb5_*_buffer failed (retcode = %d)\n",
314                          __FUNCTION__, retcode);
315                 goto out_err;
316         }
317
318         return 0;
319
320 out_err:
321         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
322         return -1;
323 }
324
325
326
327 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */