Fixes for Heimdal (macOS) builds from Stefan.
[mech_eap.git] / mech_eap / util_cksum.c
1 /*
2  * Copyright (c) 2011, JANET(UK)
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  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright 1993 by OpenVision Technologies, Inc.
34  *
35  * Permission to use, copy, modify, distribute, and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appears in all copies and
38  * that both that copyright notice and this permission notice appear in
39  * supporting documentation, and that the name of OpenVision not be used
40  * in advertising or publicity pertaining to distribution of the software
41  * without specific, written prior permission. OpenVision makes no
42  * representations about the suitability of this software for any
43  * purpose.  It is provided "as is" without express or implied warranty.
44  *
45  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
46  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
47  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
48  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
49  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51  * PERFORMANCE OF THIS SOFTWARE.
52  */
53
54 /*
55  * Message protection services: checksum helpers.
56  */
57
58 #include "gssapiP_eap.h"
59
60 static int
61 gssEapChecksum(krb5_context context,
62                krb5_cksumtype type,
63                size_t rrc,
64 #ifdef HAVE_HEIMDAL_VERSION
65                krb5_crypto crypto,
66 #else
67                krb5_keyblock *crypto,
68 #endif
69                krb5_keyusage sign_usage,
70                gss_iov_buffer_desc *iov,
71                int iov_count,
72                enum gss_eap_token_type toktype,
73                int verify,
74                int *valid)
75 {
76     krb5_error_code code;
77     gss_iov_buffer_desc *header;
78     gss_iov_buffer_desc *trailer;
79     krb5_crypto_iov *kiov;
80     size_t kiov_count;
81     int i = 0, j;
82     size_t k5_checksumlen;
83
84     if (verify)
85         *valid = FALSE;
86
87     code = krbCryptoLength(context, crypto, KRB5_CRYPTO_TYPE_CHECKSUM, &k5_checksumlen);
88     if (code != 0)
89         return code;
90
91     header = gssEapLocateHeaderIov(iov, iov_count, toktype);
92     GSSEAP_ASSERT(header != NULL);
93
94     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
95     GSSEAP_ASSERT(rrc != 0 || trailer != NULL);
96
97     if (trailer == NULL) {
98         if (rrc != k5_checksumlen)
99             return KRB5_BAD_MSIZE;
100         if (header->buffer.length != 16 + k5_checksumlen)
101             return KRB5_BAD_MSIZE;
102     } else if (trailer->buffer.length != k5_checksumlen)
103         return KRB5_BAD_MSIZE;
104
105     kiov_count = 2 + iov_count;
106     kiov = (krb5_crypto_iov *)GSSEAP_MALLOC(kiov_count * sizeof(krb5_crypto_iov));
107     if (kiov == NULL)
108         return ENOMEM;
109
110     /* Checksum over ( Data | Header ) */
111
112     /* Data */
113     for (j = 0; j < iov_count; j++) {
114         kiov[i].flags = gssEapMapCryptoFlag(iov[j].type);
115         kiov[i].data.length = iov[j].buffer.length;
116         kiov[i].data.data = (char *)iov[j].buffer.value;
117         i++;
118     }
119
120     /* Header */
121     kiov[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY;
122     kiov[i].data.length = 16;
123     kiov[i].data.data = (char *)header->buffer.value;
124     i++;
125
126     /* Checksum */
127     kiov[i].flags = KRB5_CRYPTO_TYPE_CHECKSUM;
128     if (trailer == NULL) {
129         kiov[i].data.length = header->buffer.length - 16;
130         kiov[i].data.data = (char *)header->buffer.value + 16;
131     } else {
132         kiov[i].data.length = trailer->buffer.length;
133         kiov[i].data.data = (char *)trailer->buffer.value;
134     }
135     i++;
136
137 #ifdef HAVE_HEIMDAL_VERSION
138     if (verify) {
139         code = krb5_verify_checksum_iov(context, crypto, sign_usage,
140                                         kiov, kiov_count, &type);
141         *valid = (code == 0);
142     } else {
143         code = krb5_create_checksum_iov(context, crypto, sign_usage,
144                                         kiov, kiov_count, &type);
145     }
146 #else
147     if (verify) {
148         krb5_boolean kvalid = FALSE;
149
150         code = krb5_c_verify_checksum_iov(context, type, crypto,
151                                           sign_usage, kiov, kiov_count, &kvalid);
152
153         *valid = kvalid;
154     } else {
155         code = krb5_c_make_checksum_iov(context, type, crypto,
156                                         sign_usage, kiov, kiov_count);
157     }
158 #endif /* HAVE_HEIMDAL_VERSION */
159
160     GSSEAP_FREE(kiov);
161
162     return code;
163 }
164
165 int
166 gssEapSign(krb5_context context,
167            krb5_cksumtype type,
168            size_t rrc,
169 #ifdef HAVE_HEIMDAL_VERSION
170            krb5_crypto crypto,
171 #else
172            krb5_keyblock *crypto,
173 #endif
174            krb5_keyusage sign_usage,
175            gss_iov_buffer_desc *iov,
176            int iov_count,
177            enum gss_eap_token_type toktype)
178 {
179     return gssEapChecksum(context, type, rrc, crypto,
180                           sign_usage, iov, iov_count, toktype, 0, NULL);
181 }
182
183 int
184 gssEapVerify(krb5_context context,
185              krb5_cksumtype type,
186              size_t rrc,
187 #ifdef HAVE_HEIMDAL_VERSION
188              krb5_crypto crypto,
189 #else
190              krb5_keyblock *crypto,
191 #endif
192              krb5_keyusage sign_usage,
193              gss_iov_buffer_desc *iov,
194              int iov_count,
195              enum gss_eap_token_type toktype,
196              int *valid)
197 {
198     return gssEapChecksum(context, type, rrc, crypto,
199                           sign_usage, iov, iov_count, toktype, 1, valid);
200 }
201
202 #if 0
203 OM_uint32
204 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
205                                gss_channel_bindings_t chanBindings,
206                                gss_buffer_t encodedBindings)
207 {
208     OM_uint32 major, tmpMinor;
209     size_t length;
210     unsigned char *p;
211
212     if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS) {
213         length = 24;
214         length += chanBindings->initiator_address.length;
215         length += chanBindings->acceptor_address.length;
216         length += chanBindings->application_data.length;
217
218         encodedBindings->value = GSSEAP_MALLOC(length);
219         if (encodedBindings->value == NULL) {
220             *minor = ENOMEM;
221             return GSS_S_FAILURE;
222         }
223
224         encodedBindings->length = length;
225         p = (unsigned char *)encodedBindings->value;
226
227         store_uint32_be(chanBindings->initiator_addrtype, p);
228         store_buffer(&chanBindings->initiator_address, p + 4, 0);
229         p += 4 + chanBindings->initiator_address.length;
230
231         store_uint32_be(chanBindings->acceptor_addrtype, p);
232         store_buffer(&chanBindings->acceptor_address, p + 4, 0);
233         p += 4 + chanBindings->acceptor_address.length;
234
235         store_buffer(&chanBindings->application_data, p, 1);
236         p += chanBindings->application_data.length;
237     } else {
238         encodedBindings->length = 0;
239         encodedBindings->value = NULL;
240     }
241
242     *minor = 0;
243     return GSS_S_COMPLETE;
244 }
245 #endif