]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/context_spkm3.c
aclocal/autoconf/automake, properly this time.
[nfs-utils.git] / utils / gssd / context_spkm3.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_SPKM3_H
44
45 #include <spkm3.h>
46
47 /*
48  * Function: prepare_spkm3_ctx_buffer()
49  *
50  * Prepare spkm3 lucid context for the kernel
51  *
52  *      buf->length should be:
53  *
54  *      ctx_id 4 + 12
55  *      qop 4
56  *      mech_used 4 + 7
57  *      ret_fl  4
58  *      req_fl  4
59  *      share   4 + key_len
60  *      conf_alg 4 + oid_len
61  *      d_conf_key 4 + key_len
62  *      intg_alg 4 + oid_len
63  *      d_intg_key 4 + key_len
64  *      kyestb 4 + oid_len
65  *      owl alg 4 + oid_len
66 */
67 static int
68 prepare_spkm3_ctx_buffer(gss_spkm3_lucid_ctx_t *lctx, gss_buffer_desc *buf)
69 {
70         char *p, *end;
71         unsigned int buf_size = 0;
72
73         buf_size = lctx->ctx_id.length +
74                 sizeof(lctx->ctx_id.length) + sizeof(lctx->qop) +
75                 sizeof(lctx->mech_used.length) + lctx->mech_used.length +
76                 sizeof(lctx->ret_flags) + sizeof(lctx->req_flags) +
77                 sizeof(lctx->share_key.length) + lctx->share_key.length +
78                 sizeof(lctx->conf_alg.length) + lctx->conf_alg.length +
79                 sizeof(lctx->derived_conf_key.length) +
80                 lctx->derived_conf_key.length +
81                 sizeof(lctx->intg_alg.length) + lctx->intg_alg.length +
82                 sizeof(lctx->derived_integ_key.length) +
83                 lctx->derived_integ_key.length +
84                 sizeof(lctx->keyestb_alg.length) + lctx->keyestb_alg.length +
85                 sizeof(lctx->owf_alg.length) + lctx->owf_alg.length;
86
87         if (!(buf->value = calloc(1, buf_size)))
88                 goto out_err;
89         p = buf->value;
90         end = buf->value + buf_size;
91
92         if (write_buffer(&p, end, &lctx->ctx_id))
93                 goto out_err;
94
95         if (WRITE_BYTES(&p, end, lctx->qop))
96                 goto out_err;
97
98         if (write_buffer(&p, end, &lctx->mech_used))
99                 goto out_err;
100
101         if (WRITE_BYTES(&p, end, lctx->ret_flags))
102                 goto out_err;
103
104         if (WRITE_BYTES(&p, end, lctx->req_flags))
105                 goto out_err;
106
107         if (write_buffer(&p, end, &lctx->share_key))
108                 goto out_err;
109
110         if (write_buffer(&p, end, &lctx->conf_alg))
111                 goto out_err;
112
113         if (write_buffer(&p, end, &lctx->derived_conf_key))
114                 goto out_err;
115
116         if (write_buffer(&p, end, &lctx->intg_alg))
117                 goto out_err;
118
119         if (write_buffer(&p, end, &lctx->derived_integ_key))
120                 goto out_err;
121
122         if (write_buffer(&p, end, &lctx->keyestb_alg))
123                 goto out_err;
124
125         if (write_buffer(&p, end, &lctx->owf_alg))
126                 goto out_err;
127
128         buf->length = p - (char *)buf->value;
129         return 0;
130 out_err:
131         printerr(0, "ERROR: failed serializing spkm3 context for kernel\n");
132         if (buf->value) free(buf->value);
133         buf->length = 0;
134
135         return -1;
136 }
137
138 /* ANDROS: need to determine which fields of the spkm3_gss_ctx_id_desc_t
139  * are needed in the kernel for get_mic, validate, wrap, unwrap, and destroy
140  * and only export those fields to the kernel.
141  */
142 int
143 serialize_spkm3_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
144 {
145         OM_uint32 vers, ret, maj_stat, min_stat;
146         void *ret_ctx = 0;
147         gss_spkm3_lucid_ctx_t     *lctx;
148
149         printerr(1, "serialize_spkm3_ctx called\n");
150
151         printerr(2, "DEBUG: serialize_spkm3_ctx: lucid version!\n");
152         maj_stat = gss_export_lucid_sec_context(&min_stat, ctx, 1, &ret_ctx);
153         if (maj_stat != GSS_S_COMPLETE)
154                 goto out_err;
155
156         lctx = (gss_spkm3_lucid_ctx_t *)ret_ctx;
157
158         vers = lctx->version;
159         if (vers != 1) {
160                 printerr(0, "ERROR: unsupported spkm3 context version %d\n",
161                         vers);
162                 goto out_err;
163         }
164         ret = prepare_spkm3_ctx_buffer(lctx, buf);
165
166         maj_stat = gss_free_lucid_sec_context(&min_stat, ctx, ret_ctx);
167
168         if (maj_stat != GSS_S_COMPLETE)
169                 printerr(0, "WARN: failed to free lucid sec context\n");
170         if (ret)
171                 goto out_err;
172         printerr(2, "DEBUG: serialize_spkm3_ctx: success\n");
173         return 0;
174
175 out_err:
176         printerr(2, "DEBUG: serialize_spkm3_ctx: failed\n");
177         return -1;
178 }
179 #endif /* HAVE_SPKM3_H */