1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/gssapi/t_iov.c - Test program for IOV functions */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2013 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert #include <stdio.h>
34*7f2fe78bSCy Schubert #include <stdlib.h>
35*7f2fe78bSCy Schubert #include <string.h>
36*7f2fe78bSCy Schubert #include <stddef.h>
37*7f2fe78bSCy Schubert #include "common.h"
38*7f2fe78bSCy Schubert
39*7f2fe78bSCy Schubert /* Concatenate iov (except for sign-only buffers) into a contiguous token. */
40*7f2fe78bSCy Schubert static void
concat_iov(gss_iov_buffer_desc * iov,size_t iovlen,char ** buf_out,size_t * len_out)41*7f2fe78bSCy Schubert concat_iov(gss_iov_buffer_desc *iov, size_t iovlen, char **buf_out,
42*7f2fe78bSCy Schubert size_t *len_out)
43*7f2fe78bSCy Schubert {
44*7f2fe78bSCy Schubert size_t len, i;
45*7f2fe78bSCy Schubert char *buf;
46*7f2fe78bSCy Schubert
47*7f2fe78bSCy Schubert /* Concatenate the result into a contiguous buffer. */
48*7f2fe78bSCy Schubert len = 0;
49*7f2fe78bSCy Schubert for (i = 0; i < iovlen; i++) {
50*7f2fe78bSCy Schubert if (GSS_IOV_BUFFER_TYPE(iov[i].type) != GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
51*7f2fe78bSCy Schubert len += iov[i].buffer.length;
52*7f2fe78bSCy Schubert }
53*7f2fe78bSCy Schubert buf = malloc(len);
54*7f2fe78bSCy Schubert if (buf == NULL)
55*7f2fe78bSCy Schubert errout("malloc failed");
56*7f2fe78bSCy Schubert len = 0;
57*7f2fe78bSCy Schubert for (i = 0; i < iovlen; i++) {
58*7f2fe78bSCy Schubert if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
59*7f2fe78bSCy Schubert continue;
60*7f2fe78bSCy Schubert memcpy(buf + len, iov[i].buffer.value, iov[i].buffer.length);
61*7f2fe78bSCy Schubert len += iov[i].buffer.length;
62*7f2fe78bSCy Schubert }
63*7f2fe78bSCy Schubert *buf_out = buf;
64*7f2fe78bSCy Schubert *len_out = len;
65*7f2fe78bSCy Schubert }
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert static void
check_encrypted(const char * msg,int conf,const char * buf,const char * plain)68*7f2fe78bSCy Schubert check_encrypted(const char *msg, int conf, const char *buf, const char *plain)
69*7f2fe78bSCy Schubert {
70*7f2fe78bSCy Schubert int same = memcmp(buf, plain, strlen(plain)) == 0;
71*7f2fe78bSCy Schubert
72*7f2fe78bSCy Schubert if ((conf && same) || (!conf && !same))
73*7f2fe78bSCy Schubert errout(msg);
74*7f2fe78bSCy Schubert }
75*7f2fe78bSCy Schubert
76*7f2fe78bSCy Schubert /*
77*7f2fe78bSCy Schubert * Wrap str in standard form (HEADER | DATA | PADDING | TRAILER) using the
78*7f2fe78bSCy Schubert * caller-provided array iov, which must have space for four elements. Library
79*7f2fe78bSCy Schubert * allocation will be used for the header/padding/trailer buffers, so the
80*7f2fe78bSCy Schubert * caller must check and free them.
81*7f2fe78bSCy Schubert */
82*7f2fe78bSCy Schubert static void
wrap_std(gss_ctx_id_t ctx,char * str,gss_iov_buffer_desc * iov,int conf)83*7f2fe78bSCy Schubert wrap_std(gss_ctx_id_t ctx, char *str, gss_iov_buffer_desc *iov, int conf)
84*7f2fe78bSCy Schubert {
85*7f2fe78bSCy Schubert OM_uint32 minor, major;
86*7f2fe78bSCy Schubert int oconf;
87*7f2fe78bSCy Schubert
88*7f2fe78bSCy Schubert /* Lay out iov array. */
89*7f2fe78bSCy Schubert iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
90*7f2fe78bSCy Schubert iov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
91*7f2fe78bSCy Schubert iov[1].buffer.value = str;
92*7f2fe78bSCy Schubert iov[1].buffer.length = strlen(str);
93*7f2fe78bSCy Schubert iov[2].type = GSS_IOV_BUFFER_TYPE_PADDING | GSS_IOV_BUFFER_FLAG_ALLOCATE;
94*7f2fe78bSCy Schubert iov[3].type = GSS_IOV_BUFFER_TYPE_TRAILER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
95*7f2fe78bSCy Schubert
96*7f2fe78bSCy Schubert /* Wrap. This will allocate header/padding/trailer buffers as necessary
97*7f2fe78bSCy Schubert * and encrypt str in place. */
98*7f2fe78bSCy Schubert major = gss_wrap_iov(&minor, ctx, conf, GSS_C_QOP_DEFAULT, &oconf, iov, 4);
99*7f2fe78bSCy Schubert check_gsserr("gss_wrap_iov(std)", major, minor);
100*7f2fe78bSCy Schubert if (oconf != conf)
101*7f2fe78bSCy Schubert errout("gss_wrap_iov(std) conf");
102*7f2fe78bSCy Schubert }
103*7f2fe78bSCy Schubert
104*7f2fe78bSCy Schubert /* Create standard tokens using gss_wrap_iov and ctx1, and make sure we can
105*7f2fe78bSCy Schubert * unwrap them using ctx2 in all of the supported ways. */
106*7f2fe78bSCy Schubert static void
test_standard_wrap(gss_ctx_id_t ctx1,gss_ctx_id_t ctx2,int conf)107*7f2fe78bSCy Schubert test_standard_wrap(gss_ctx_id_t ctx1, gss_ctx_id_t ctx2, int conf)
108*7f2fe78bSCy Schubert {
109*7f2fe78bSCy Schubert OM_uint32 major, minor;
110*7f2fe78bSCy Schubert gss_iov_buffer_desc iov[4], stiov[2];
111*7f2fe78bSCy Schubert gss_qop_t qop;
112*7f2fe78bSCy Schubert gss_buffer_desc input, output;
113*7f2fe78bSCy Schubert const char *string1 = "The swift brown fox jumped over the lazy dog.";
114*7f2fe78bSCy Schubert const char *string2 = "Now is the time!";
115*7f2fe78bSCy Schubert const char *string3 = "x";
116*7f2fe78bSCy Schubert const char *string4 = "!@#";
117*7f2fe78bSCy Schubert char data[1024], *fulltoken;
118*7f2fe78bSCy Schubert size_t len;
119*7f2fe78bSCy Schubert int oconf;
120*7f2fe78bSCy Schubert ptrdiff_t offset;
121*7f2fe78bSCy Schubert
122*7f2fe78bSCy Schubert /* Wrap a standard token and unwrap it using the iov array. */
123*7f2fe78bSCy Schubert memcpy(data, string1, strlen(string1) + 1);
124*7f2fe78bSCy Schubert wrap_std(ctx1, data, iov, conf);
125*7f2fe78bSCy Schubert check_encrypted("gss_wrap_iov(std1) encryption", conf, data, string1);
126*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, iov, 4);
127*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(std1)", major, minor);
128*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
129*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std1) conf/qop");
130*7f2fe78bSCy Schubert if (iov[1].buffer.value != data || iov[1].buffer.length != strlen(string1))
131*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std1) data buffer");
132*7f2fe78bSCy Schubert if (memcmp(data, string1, iov[1].buffer.length) != 0)
133*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std1) decryption");
134*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, iov, 4);
135*7f2fe78bSCy Schubert
136*7f2fe78bSCy Schubert /* Wrap a standard token and unwrap it using gss_unwrap(). */
137*7f2fe78bSCy Schubert memcpy(data, string2, strlen(string2) + 1);
138*7f2fe78bSCy Schubert wrap_std(ctx1, data, iov, conf);
139*7f2fe78bSCy Schubert concat_iov(iov, 4, &fulltoken, &len);
140*7f2fe78bSCy Schubert input.value = fulltoken;
141*7f2fe78bSCy Schubert input.length = len;
142*7f2fe78bSCy Schubert major = gss_unwrap(&minor, ctx2, &input, &output, &oconf, &qop);
143*7f2fe78bSCy Schubert check_gsserr("gss_unwrap(std2)", major, minor);
144*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
145*7f2fe78bSCy Schubert errout("gss_unwrap(std2) conf/qop");
146*7f2fe78bSCy Schubert if (output.length != strlen(string2) ||
147*7f2fe78bSCy Schubert memcmp(output.value, string2, output.length) != 0)
148*7f2fe78bSCy Schubert errout("gss_unwrap(std2) decryption");
149*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &output);
150*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, iov, 4);
151*7f2fe78bSCy Schubert free(fulltoken);
152*7f2fe78bSCy Schubert
153*7f2fe78bSCy Schubert /* Wrap a standard token and unwrap it using a stream buffer. */
154*7f2fe78bSCy Schubert memcpy(data, string3, strlen(string3) + 1);
155*7f2fe78bSCy Schubert wrap_std(ctx1, data, iov, conf);
156*7f2fe78bSCy Schubert concat_iov(iov, 4, &fulltoken, &len);
157*7f2fe78bSCy Schubert stiov[0].type = GSS_IOV_BUFFER_TYPE_STREAM;
158*7f2fe78bSCy Schubert stiov[0].buffer.value = fulltoken;
159*7f2fe78bSCy Schubert stiov[0].buffer.length = len;
160*7f2fe78bSCy Schubert stiov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
161*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, stiov, 2);
162*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(std3)", major, minor);
163*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
164*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std3) conf/qop");
165*7f2fe78bSCy Schubert if (stiov[1].buffer.length != strlen(string3) ||
166*7f2fe78bSCy Schubert memcmp(stiov[1].buffer.value, string3, strlen(string3)) != 0)
167*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std3) decryption");
168*7f2fe78bSCy Schubert offset = (char *)stiov[1].buffer.value - fulltoken;
169*7f2fe78bSCy Schubert if (offset < 0 || (size_t)offset > len)
170*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std3) offset");
171*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, iov, 4);
172*7f2fe78bSCy Schubert free(fulltoken);
173*7f2fe78bSCy Schubert
174*7f2fe78bSCy Schubert /* Wrap a token using gss_wrap and unwrap it using a stream buffer with
175*7f2fe78bSCy Schubert * allocation and copying. */
176*7f2fe78bSCy Schubert input.value = (char *)string4;
177*7f2fe78bSCy Schubert input.length = strlen(string4);
178*7f2fe78bSCy Schubert major = gss_wrap(&minor, ctx1, conf, GSS_C_QOP_DEFAULT, &input, &oconf,
179*7f2fe78bSCy Schubert &output);
180*7f2fe78bSCy Schubert check_gsserr("gss_wrap(std4)", major, minor);
181*7f2fe78bSCy Schubert if (oconf != conf)
182*7f2fe78bSCy Schubert errout("gss_wrap(std4) conf");
183*7f2fe78bSCy Schubert stiov[0].type = GSS_IOV_BUFFER_TYPE_STREAM;
184*7f2fe78bSCy Schubert stiov[0].buffer = output;
185*7f2fe78bSCy Schubert stiov[1].type = GSS_IOV_BUFFER_TYPE_DATA | GSS_IOV_BUFFER_FLAG_ALLOCATE;
186*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, stiov, 2);
187*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(std4)", major, minor);
188*7f2fe78bSCy Schubert if (!(GSS_IOV_BUFFER_FLAGS(stiov[1].type) & GSS_IOV_BUFFER_FLAG_ALLOCATED))
189*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std4) allocated");
190*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
191*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std4) conf/qop");
192*7f2fe78bSCy Schubert if (stiov[1].buffer.length != strlen(string4) ||
193*7f2fe78bSCy Schubert memcmp(stiov[1].buffer.value, string4, strlen(string4)) != 0)
194*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std4) decryption");
195*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &output);
196*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, stiov, 2);
197*7f2fe78bSCy Schubert }
198*7f2fe78bSCy Schubert
199*7f2fe78bSCy Schubert /*
200*7f2fe78bSCy Schubert * Wrap an AEAD token (HEADER | SIGN_ONLY | DATA | PADDING | TRAILER) using the
201*7f2fe78bSCy Schubert * caller-provided array iov, which must have space for five elements, and the
202*7f2fe78bSCy Schubert * caller-provided buffer data, which must be big enough to handle the test
203*7f2fe78bSCy Schubert * inputs. Library allocation will not be used.
204*7f2fe78bSCy Schubert */
205*7f2fe78bSCy Schubert static void
wrap_aead(gss_ctx_id_t ctx,const char * sign,const char * wrap,gss_iov_buffer_desc * iov,char * data,int conf)206*7f2fe78bSCy Schubert wrap_aead(gss_ctx_id_t ctx, const char *sign, const char *wrap,
207*7f2fe78bSCy Schubert gss_iov_buffer_desc *iov, char *data, int conf)
208*7f2fe78bSCy Schubert {
209*7f2fe78bSCy Schubert OM_uint32 major, minor;
210*7f2fe78bSCy Schubert int oconf;
211*7f2fe78bSCy Schubert char *ptr;
212*7f2fe78bSCy Schubert
213*7f2fe78bSCy Schubert /* Lay out iov array. */
214*7f2fe78bSCy Schubert iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER;
215*7f2fe78bSCy Schubert iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
216*7f2fe78bSCy Schubert iov[1].buffer.value = (char *)sign;
217*7f2fe78bSCy Schubert iov[1].buffer.length = strlen(sign);
218*7f2fe78bSCy Schubert iov[2].type = GSS_IOV_BUFFER_TYPE_DATA;
219*7f2fe78bSCy Schubert iov[2].buffer.value = (char *)wrap;
220*7f2fe78bSCy Schubert iov[2].buffer.length = strlen(wrap);
221*7f2fe78bSCy Schubert iov[3].type = GSS_IOV_BUFFER_TYPE_PADDING;
222*7f2fe78bSCy Schubert iov[4].type = GSS_IOV_BUFFER_TYPE_TRAILER;
223*7f2fe78bSCy Schubert
224*7f2fe78bSCy Schubert /* Get header/padding/trailer lengths. */
225*7f2fe78bSCy Schubert major = gss_wrap_iov_length(&minor, ctx, conf, GSS_C_QOP_DEFAULT, &oconf,
226*7f2fe78bSCy Schubert iov, 5);
227*7f2fe78bSCy Schubert check_gsserr("gss_wrap_iov_length(aead)", major, minor);
228*7f2fe78bSCy Schubert if (oconf != conf)
229*7f2fe78bSCy Schubert errout("gss_wrap_iov_length(aead) conf");
230*7f2fe78bSCy Schubert if (iov[1].buffer.value != sign || iov[1].buffer.length != strlen(sign))
231*7f2fe78bSCy Schubert errout("gss_wrap_iov_length(aead) sign-only buffer");
232*7f2fe78bSCy Schubert if (iov[2].buffer.value != wrap || iov[2].buffer.length != strlen(wrap))
233*7f2fe78bSCy Schubert errout("gss_wrap_iov_length(aead) data buffer");
234*7f2fe78bSCy Schubert
235*7f2fe78bSCy Schubert /* Set iov buffer pointers using returned lengths. */
236*7f2fe78bSCy Schubert iov[0].buffer.value = data;
237*7f2fe78bSCy Schubert ptr = data + iov[0].buffer.length;
238*7f2fe78bSCy Schubert memcpy(ptr, wrap, strlen(wrap));
239*7f2fe78bSCy Schubert iov[2].buffer.value = ptr;
240*7f2fe78bSCy Schubert ptr += iov[2].buffer.length;
241*7f2fe78bSCy Schubert iov[3].buffer.value = ptr;
242*7f2fe78bSCy Schubert ptr += iov[3].buffer.length;
243*7f2fe78bSCy Schubert iov[4].buffer.value = ptr;
244*7f2fe78bSCy Schubert
245*7f2fe78bSCy Schubert /* Wrap the AEAD token. */
246*7f2fe78bSCy Schubert major = gss_wrap_iov(&minor, ctx, conf, GSS_C_QOP_DEFAULT, &oconf, iov, 5);
247*7f2fe78bSCy Schubert check_gsserr("gss_wrap_iov(aead)", major, minor);
248*7f2fe78bSCy Schubert if (oconf != conf)
249*7f2fe78bSCy Schubert errout("gss_wrap_iov(aead) conf");
250*7f2fe78bSCy Schubert if (iov[1].buffer.value != sign || iov[1].buffer.length != strlen(sign))
251*7f2fe78bSCy Schubert errout("gss_wrap_iov(aead) sign-only buffer");
252*7f2fe78bSCy Schubert if (iov[2].buffer.length != strlen(wrap))
253*7f2fe78bSCy Schubert errout("gss_wrap_iov(aead) data buffer");
254*7f2fe78bSCy Schubert check_encrypted("gss_wrap_iov(aead) encryption", conf, iov[2].buffer.value,
255*7f2fe78bSCy Schubert wrap);
256*7f2fe78bSCy Schubert }
257*7f2fe78bSCy Schubert
258*7f2fe78bSCy Schubert /* Create AEAD tokens using gss_wrap_iov and ctx1, and make sure we can unwrap
259*7f2fe78bSCy Schubert * them using ctx2 in all of the supported ways. */
260*7f2fe78bSCy Schubert static void
test_aead(gss_ctx_id_t ctx1,gss_ctx_id_t ctx2,int conf)261*7f2fe78bSCy Schubert test_aead(gss_ctx_id_t ctx1, gss_ctx_id_t ctx2, int conf)
262*7f2fe78bSCy Schubert {
263*7f2fe78bSCy Schubert OM_uint32 major, minor;
264*7f2fe78bSCy Schubert gss_iov_buffer_desc iov[5], stiov[3];
265*7f2fe78bSCy Schubert gss_qop_t qop;
266*7f2fe78bSCy Schubert gss_buffer_desc input, assoc, output;
267*7f2fe78bSCy Schubert const char *sign = "This data is only signed.";
268*7f2fe78bSCy Schubert const char *wrap = "This data is wrapped in-place.";
269*7f2fe78bSCy Schubert char data[1024], *fulltoken;
270*7f2fe78bSCy Schubert size_t len;
271*7f2fe78bSCy Schubert int oconf;
272*7f2fe78bSCy Schubert ptrdiff_t offset;
273*7f2fe78bSCy Schubert
274*7f2fe78bSCy Schubert /* Wrap an AEAD token and unwrap it using the IOV array. */
275*7f2fe78bSCy Schubert wrap_aead(ctx1, sign, wrap, iov, data, conf);
276*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, iov, 5);
277*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(aead1)", major, minor);
278*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
279*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead1) conf/qop");
280*7f2fe78bSCy Schubert if (iov[1].buffer.value != sign || iov[1].buffer.length != strlen(sign))
281*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead1) sign-only buffer");
282*7f2fe78bSCy Schubert if (iov[2].buffer.length != strlen(wrap) ||
283*7f2fe78bSCy Schubert memcmp(iov[2].buffer.value, wrap, iov[2].buffer.length) != 0)
284*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead1) decryption");
285*7f2fe78bSCy Schubert
286*7f2fe78bSCy Schubert /* Wrap an AEAD token and unwrap it using gss_unwrap_aead. */
287*7f2fe78bSCy Schubert wrap_aead(ctx1, sign, wrap, iov, data, conf);
288*7f2fe78bSCy Schubert concat_iov(iov, 5, &fulltoken, &len);
289*7f2fe78bSCy Schubert input.value = fulltoken;
290*7f2fe78bSCy Schubert input.length = len;
291*7f2fe78bSCy Schubert assoc.value = (char *)sign;
292*7f2fe78bSCy Schubert assoc.length = strlen(sign);
293*7f2fe78bSCy Schubert major = gss_unwrap_aead(&minor, ctx2, &input, &assoc, &output, &oconf,
294*7f2fe78bSCy Schubert &qop);
295*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_aead(aead2)", major, minor);
296*7f2fe78bSCy Schubert if (output.length != strlen(wrap) ||
297*7f2fe78bSCy Schubert memcmp(output.value, wrap, output.length) != 0)
298*7f2fe78bSCy Schubert errout("gss_unwrap_aead(aead2) decryption");
299*7f2fe78bSCy Schubert free(fulltoken);
300*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &output);
301*7f2fe78bSCy Schubert
302*7f2fe78bSCy Schubert /* Wrap an AEAD token and unwrap it using a stream buffer. */
303*7f2fe78bSCy Schubert wrap_aead(ctx1, sign, wrap, iov, data, conf);
304*7f2fe78bSCy Schubert concat_iov(iov, 5, &fulltoken, &len);
305*7f2fe78bSCy Schubert stiov[0].type = GSS_IOV_BUFFER_TYPE_STREAM;
306*7f2fe78bSCy Schubert stiov[0].buffer.value = fulltoken;
307*7f2fe78bSCy Schubert stiov[0].buffer.length = len;
308*7f2fe78bSCy Schubert stiov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
309*7f2fe78bSCy Schubert stiov[1].buffer.value = (char *)sign;
310*7f2fe78bSCy Schubert stiov[1].buffer.length = strlen(sign);
311*7f2fe78bSCy Schubert stiov[2].type = GSS_IOV_BUFFER_TYPE_DATA;
312*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, stiov, 3);
313*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(aead3)", major, minor);
314*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
315*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead3) conf/qop");
316*7f2fe78bSCy Schubert if (stiov[2].buffer.length != strlen(wrap) ||
317*7f2fe78bSCy Schubert memcmp(stiov[2].buffer.value, wrap, strlen(wrap)) != 0)
318*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead3) decryption");
319*7f2fe78bSCy Schubert offset = (char *)stiov[2].buffer.value - fulltoken;
320*7f2fe78bSCy Schubert if (offset < 0 || (size_t)offset > len)
321*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead3) offset");
322*7f2fe78bSCy Schubert free(fulltoken);
323*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, iov, 4);
324*7f2fe78bSCy Schubert
325*7f2fe78bSCy Schubert /* Wrap a token using gss_wrap_aead and unwrap it using a stream buffer
326*7f2fe78bSCy Schubert * with allocation and copying. */
327*7f2fe78bSCy Schubert input.value = (char *)wrap;
328*7f2fe78bSCy Schubert input.length = strlen(wrap);
329*7f2fe78bSCy Schubert assoc.value = (char *)sign;
330*7f2fe78bSCy Schubert assoc.length = strlen(sign);
331*7f2fe78bSCy Schubert major = gss_wrap_aead(&minor, ctx1, conf, GSS_C_QOP_DEFAULT, &assoc,
332*7f2fe78bSCy Schubert &input, &oconf, &output);
333*7f2fe78bSCy Schubert check_gsserr("gss_wrap_aead(aead4)", major, minor);
334*7f2fe78bSCy Schubert if (oconf != conf)
335*7f2fe78bSCy Schubert errout("gss_wrap(aead4) conf");
336*7f2fe78bSCy Schubert stiov[0].type = GSS_IOV_BUFFER_TYPE_STREAM;
337*7f2fe78bSCy Schubert stiov[0].buffer = output;
338*7f2fe78bSCy Schubert stiov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
339*7f2fe78bSCy Schubert stiov[1].buffer = assoc;
340*7f2fe78bSCy Schubert stiov[2].type = GSS_IOV_BUFFER_TYPE_DATA | GSS_IOV_BUFFER_FLAG_ALLOCATE;
341*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, stiov, 3);
342*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(aead4)", major, minor);
343*7f2fe78bSCy Schubert if (!(GSS_IOV_BUFFER_FLAGS(stiov[2].type) & GSS_IOV_BUFFER_FLAG_ALLOCATED))
344*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead4) allocated");
345*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
346*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead4) conf/qop");
347*7f2fe78bSCy Schubert if (stiov[2].buffer.length != strlen(wrap) ||
348*7f2fe78bSCy Schubert memcmp(stiov[2].buffer.value, wrap, strlen(wrap)) != 0)
349*7f2fe78bSCy Schubert errout("gss_unwrap_iov(aead4) decryption");
350*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &output);
351*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, stiov, 3);
352*7f2fe78bSCy Schubert }
353*7f2fe78bSCy Schubert
354*7f2fe78bSCy Schubert /*
355*7f2fe78bSCy Schubert * Get a MIC for sign1, sign2, and sign3 using the caller-provided array iov,
356*7f2fe78bSCy Schubert * which must have space for four elements, and the caller-provided buffer
357*7f2fe78bSCy Schubert * data, which must be big enough for the MIC. If data is NULL, the library
358*7f2fe78bSCy Schubert * will be asked to allocate the MIC buffer. The MIC will be located in
359*7f2fe78bSCy Schubert * iov[3].buffer.
360*7f2fe78bSCy Schubert */
361*7f2fe78bSCy Schubert static void
mic(gss_ctx_id_t ctx,const char * sign1,const char * sign2,const char * sign3,gss_iov_buffer_desc * iov,char * data)362*7f2fe78bSCy Schubert mic(gss_ctx_id_t ctx, const char *sign1, const char *sign2, const char *sign3,
363*7f2fe78bSCy Schubert gss_iov_buffer_desc *iov, char *data)
364*7f2fe78bSCy Schubert {
365*7f2fe78bSCy Schubert OM_uint32 minor, major;
366*7f2fe78bSCy Schubert krb5_boolean allocated;
367*7f2fe78bSCy Schubert
368*7f2fe78bSCy Schubert /* Lay out iov array. */
369*7f2fe78bSCy Schubert iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
370*7f2fe78bSCy Schubert iov[0].buffer.value = (char *)sign1;
371*7f2fe78bSCy Schubert iov[0].buffer.length = strlen(sign1);
372*7f2fe78bSCy Schubert iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
373*7f2fe78bSCy Schubert iov[1].buffer.value = (char *)sign2;
374*7f2fe78bSCy Schubert iov[1].buffer.length = strlen(sign2);
375*7f2fe78bSCy Schubert iov[2].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
376*7f2fe78bSCy Schubert iov[2].buffer.value = (char *)sign3;
377*7f2fe78bSCy Schubert iov[2].buffer.length = strlen(sign3);
378*7f2fe78bSCy Schubert iov[3].type = GSS_IOV_BUFFER_TYPE_MIC_TOKEN;
379*7f2fe78bSCy Schubert if (data == NULL) {
380*7f2fe78bSCy Schubert /* Ask the library to allocate the MIC buffer. */
381*7f2fe78bSCy Schubert iov[3].type |= GSS_IOV_BUFFER_FLAG_ALLOCATE;
382*7f2fe78bSCy Schubert } else {
383*7f2fe78bSCy Schubert /* Get the MIC length and use the caller-provided buffer. */
384*7f2fe78bSCy Schubert major = gss_get_mic_iov_length(&minor, ctx, GSS_C_QOP_DEFAULT, iov, 4);
385*7f2fe78bSCy Schubert check_gsserr("gss_get_mic_iov_length", major, minor);
386*7f2fe78bSCy Schubert iov[3].buffer.value = data;
387*7f2fe78bSCy Schubert }
388*7f2fe78bSCy Schubert major = gss_get_mic_iov(&minor, ctx, GSS_C_QOP_DEFAULT, iov, 4);
389*7f2fe78bSCy Schubert check_gsserr("gss_get_mic_iov", major, minor);
390*7f2fe78bSCy Schubert allocated = (GSS_IOV_BUFFER_FLAGS(iov[3].type) &
391*7f2fe78bSCy Schubert GSS_IOV_BUFFER_FLAG_ALLOCATED) != 0;
392*7f2fe78bSCy Schubert if (allocated != (data == NULL))
393*7f2fe78bSCy Schubert errout("gss_get_mic_iov allocated");
394*7f2fe78bSCy Schubert }
395*7f2fe78bSCy Schubert
396*7f2fe78bSCy Schubert static void
test_mic(gss_ctx_id_t ctx1,gss_ctx_id_t ctx2)397*7f2fe78bSCy Schubert test_mic(gss_ctx_id_t ctx1, gss_ctx_id_t ctx2)
398*7f2fe78bSCy Schubert {
399*7f2fe78bSCy Schubert OM_uint32 major, minor;
400*7f2fe78bSCy Schubert gss_iov_buffer_desc iov[4];
401*7f2fe78bSCy Schubert gss_qop_t qop;
402*7f2fe78bSCy Schubert gss_buffer_desc concatbuf, micbuf;
403*7f2fe78bSCy Schubert const char *sign1 = "Data and sign-only ";
404*7f2fe78bSCy Schubert const char *sign2 = "buffers are treated ";
405*7f2fe78bSCy Schubert const char *sign3 = "equally by gss_get_mic_iov";
406*7f2fe78bSCy Schubert char concat[1024], data[1024];
407*7f2fe78bSCy Schubert
408*7f2fe78bSCy Schubert (void)snprintf(concat, sizeof(concat), "%s%s%s", sign1, sign2, sign3);
409*7f2fe78bSCy Schubert concatbuf.value = concat;
410*7f2fe78bSCy Schubert concatbuf.length = strlen(concat);
411*7f2fe78bSCy Schubert
412*7f2fe78bSCy Schubert /* MIC with a caller-provided buffer and verify with the IOV array. */
413*7f2fe78bSCy Schubert mic(ctx1, sign1, sign2, sign3, iov, data);
414*7f2fe78bSCy Schubert major = gss_verify_mic_iov(&minor, ctx2, &qop, iov, 4);
415*7f2fe78bSCy Schubert check_gsserr("gss_verify_mic_iov(mic1)", major, minor);
416*7f2fe78bSCy Schubert if (qop != GSS_C_QOP_DEFAULT)
417*7f2fe78bSCy Schubert errout("gss_verify_mic_iov(mic1) qop");
418*7f2fe78bSCy Schubert
419*7f2fe78bSCy Schubert /* MIC with an allocated buffer and verify with gss_verify_mic. */
420*7f2fe78bSCy Schubert mic(ctx1, sign1, sign2, sign3, iov, NULL);
421*7f2fe78bSCy Schubert major = gss_verify_mic(&minor, ctx2, &concatbuf, &iov[3].buffer, &qop);
422*7f2fe78bSCy Schubert check_gsserr("gss_verify_mic(mic2)", major, minor);
423*7f2fe78bSCy Schubert if (qop != GSS_C_QOP_DEFAULT)
424*7f2fe78bSCy Schubert errout("gss_verify_mic(mic2) qop");
425*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, iov, 4);
426*7f2fe78bSCy Schubert
427*7f2fe78bSCy Schubert /* MIC with gss_c_get_mic and verify using the IOV array (which is still
428*7f2fe78bSCy Schubert * mostly set up from the last call to mic(). */
429*7f2fe78bSCy Schubert major = gss_get_mic(&minor, ctx1, GSS_C_QOP_DEFAULT, &concatbuf, &micbuf);
430*7f2fe78bSCy Schubert check_gsserr("gss_get_mic(mic3)", major, minor);
431*7f2fe78bSCy Schubert iov[3].buffer = micbuf;
432*7f2fe78bSCy Schubert major = gss_verify_mic_iov(&minor, ctx2, &qop, iov, 4);
433*7f2fe78bSCy Schubert check_gsserr("gss_verify_mic_iov(mic3)", major, minor);
434*7f2fe78bSCy Schubert if (qop != GSS_C_QOP_DEFAULT)
435*7f2fe78bSCy Schubert errout("gss_verify_mic_iov(mic3) qop");
436*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &micbuf);
437*7f2fe78bSCy Schubert }
438*7f2fe78bSCy Schubert
439*7f2fe78bSCy Schubert /* Create a DCE-style token and make sure we can unwrap it. */
440*7f2fe78bSCy Schubert static void
test_dce(gss_ctx_id_t ctx1,gss_ctx_id_t ctx2,int conf)441*7f2fe78bSCy Schubert test_dce(gss_ctx_id_t ctx1, gss_ctx_id_t ctx2, int conf)
442*7f2fe78bSCy Schubert {
443*7f2fe78bSCy Schubert OM_uint32 major, minor;
444*7f2fe78bSCy Schubert gss_iov_buffer_desc iov[4];
445*7f2fe78bSCy Schubert gss_qop_t qop;
446*7f2fe78bSCy Schubert const char *sign1 = "First data to be signed";
447*7f2fe78bSCy Schubert const char *sign2 = "Second data to be signed";
448*7f2fe78bSCy Schubert const char *wrap = "This data must align to 16 bytes";
449*7f2fe78bSCy Schubert int oconf;
450*7f2fe78bSCy Schubert char data[1024];
451*7f2fe78bSCy Schubert
452*7f2fe78bSCy Schubert /* Wrap a SIGN_ONLY_1 | DATA | SIGN_ONLY_2 | HEADER token. */
453*7f2fe78bSCy Schubert memcpy(data, wrap, strlen(wrap) + 1);
454*7f2fe78bSCy Schubert iov[0].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
455*7f2fe78bSCy Schubert iov[0].buffer.value = (char *)sign1;
456*7f2fe78bSCy Schubert iov[0].buffer.length = strlen(sign1);
457*7f2fe78bSCy Schubert iov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
458*7f2fe78bSCy Schubert iov[1].buffer.value = data;
459*7f2fe78bSCy Schubert iov[1].buffer.length = strlen(wrap);
460*7f2fe78bSCy Schubert iov[2].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY;
461*7f2fe78bSCy Schubert iov[2].buffer.value = (char *)sign2;
462*7f2fe78bSCy Schubert iov[2].buffer.length = strlen(sign2);
463*7f2fe78bSCy Schubert iov[3].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
464*7f2fe78bSCy Schubert major = gss_wrap_iov(&minor, ctx1, conf, GSS_C_QOP_DEFAULT, &oconf, iov,
465*7f2fe78bSCy Schubert 4);
466*7f2fe78bSCy Schubert check_gsserr("gss_wrap_iov(dce)", major, minor);
467*7f2fe78bSCy Schubert if (oconf != conf)
468*7f2fe78bSCy Schubert errout("gss_wrap_iov(dce) conf");
469*7f2fe78bSCy Schubert if (iov[0].buffer.value != sign1 || iov[0].buffer.length != strlen(sign1))
470*7f2fe78bSCy Schubert errout("gss_wrap_iov(dce) sign1 buffer");
471*7f2fe78bSCy Schubert if (iov[1].buffer.value != data || iov[1].buffer.length != strlen(wrap))
472*7f2fe78bSCy Schubert errout("gss_wrap_iov(dce) data buffer");
473*7f2fe78bSCy Schubert if (iov[2].buffer.value != sign2 || iov[2].buffer.length != strlen(sign2))
474*7f2fe78bSCy Schubert errout("gss_wrap_iov(dce) sign2 buffer");
475*7f2fe78bSCy Schubert check_encrypted("gss_wrap_iov(dce) encryption", conf, data, wrap);
476*7f2fe78bSCy Schubert
477*7f2fe78bSCy Schubert /* Make sure we can unwrap it. */
478*7f2fe78bSCy Schubert major = gss_unwrap_iov(&minor, ctx2, &oconf, &qop, iov, 4);
479*7f2fe78bSCy Schubert check_gsserr("gss_unwrap_iov(std1)", major, minor);
480*7f2fe78bSCy Schubert if (oconf != conf || qop != GSS_C_QOP_DEFAULT)
481*7f2fe78bSCy Schubert errout("gss_unwrap_iov(std1) conf/qop");
482*7f2fe78bSCy Schubert if (iov[0].buffer.value != sign1 || iov[0].buffer.length != strlen(sign1))
483*7f2fe78bSCy Schubert errout("gss_unwrap_iov(dce) sign1 buffer");
484*7f2fe78bSCy Schubert if (iov[1].buffer.value != data || iov[1].buffer.length != strlen(wrap))
485*7f2fe78bSCy Schubert errout("gss_unwrap_iov(dce) data buffer");
486*7f2fe78bSCy Schubert if (iov[2].buffer.value != sign2 || iov[2].buffer.length != strlen(sign2))
487*7f2fe78bSCy Schubert errout("gss_unwrap_iov(dce) sign2 buffer");
488*7f2fe78bSCy Schubert if (memcmp(data, wrap, iov[1].buffer.length) != 0)
489*7f2fe78bSCy Schubert errout("gss_unwrap_iov(dce) decryption");
490*7f2fe78bSCy Schubert (void)gss_release_iov_buffer(&minor, iov, 4);
491*7f2fe78bSCy Schubert }
492*7f2fe78bSCy Schubert
493*7f2fe78bSCy Schubert int
main(int argc,char * argv[])494*7f2fe78bSCy Schubert main(int argc, char *argv[])
495*7f2fe78bSCy Schubert {
496*7f2fe78bSCy Schubert OM_uint32 minor, flags;
497*7f2fe78bSCy Schubert gss_OID mech = &mech_krb5;
498*7f2fe78bSCy Schubert gss_name_t tname;
499*7f2fe78bSCy Schubert gss_ctx_id_t ictx, actx;
500*7f2fe78bSCy Schubert
501*7f2fe78bSCy Schubert /* Parse arguments. */
502*7f2fe78bSCy Schubert argv++;
503*7f2fe78bSCy Schubert if (*argv != NULL && strcmp(*argv, "-s") == 0) {
504*7f2fe78bSCy Schubert mech = &mech_spnego;
505*7f2fe78bSCy Schubert argv++;
506*7f2fe78bSCy Schubert }
507*7f2fe78bSCy Schubert if (*argv == NULL || *(argv + 1) != NULL)
508*7f2fe78bSCy Schubert errout("Usage: t_iov [-s] targetname");
509*7f2fe78bSCy Schubert tname = import_name(*argv);
510*7f2fe78bSCy Schubert
511*7f2fe78bSCy Schubert flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_MUTUAL_FLAG;
512*7f2fe78bSCy Schubert establish_contexts(mech, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL, tname,
513*7f2fe78bSCy Schubert flags, &ictx, &actx, NULL, NULL, NULL);
514*7f2fe78bSCy Schubert
515*7f2fe78bSCy Schubert /* Test standard token wrapping and unwrapping in both directions, with and
516*7f2fe78bSCy Schubert * without confidentiality. */
517*7f2fe78bSCy Schubert test_standard_wrap(ictx, actx, 0);
518*7f2fe78bSCy Schubert test_standard_wrap(ictx, actx, 1);
519*7f2fe78bSCy Schubert test_standard_wrap(actx, ictx, 0);
520*7f2fe78bSCy Schubert test_standard_wrap(actx, ictx, 1);
521*7f2fe78bSCy Schubert
522*7f2fe78bSCy Schubert /* Test AEAD wrapping. */
523*7f2fe78bSCy Schubert test_aead(ictx, actx, 0);
524*7f2fe78bSCy Schubert test_aead(ictx, actx, 1);
525*7f2fe78bSCy Schubert test_aead(actx, ictx, 0);
526*7f2fe78bSCy Schubert test_aead(actx, ictx, 1);
527*7f2fe78bSCy Schubert
528*7f2fe78bSCy Schubert /* Test MIC tokens. */
529*7f2fe78bSCy Schubert test_mic(ictx, actx);
530*7f2fe78bSCy Schubert test_mic(actx, ictx);
531*7f2fe78bSCy Schubert
532*7f2fe78bSCy Schubert /* Test DCE wrapping with DCE-style contexts. */
533*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &ictx, NULL);
534*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &actx, NULL);
535*7f2fe78bSCy Schubert flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DCE_STYLE;
536*7f2fe78bSCy Schubert establish_contexts(mech, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL, tname,
537*7f2fe78bSCy Schubert flags, &ictx, &actx, NULL, NULL, NULL);
538*7f2fe78bSCy Schubert test_dce(ictx, actx, 0);
539*7f2fe78bSCy Schubert test_dce(ictx, actx, 1);
540*7f2fe78bSCy Schubert test_dce(actx, ictx, 0);
541*7f2fe78bSCy Schubert test_dce(actx, ictx, 1);
542*7f2fe78bSCy Schubert
543*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &tname);
544*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &ictx, NULL);
545*7f2fe78bSCy Schubert (void)gss_delete_sec_context(&minor, &actx, NULL);
546*7f2fe78bSCy Schubert return 0;
547*7f2fe78bSCy Schubert }
548