485212781dd1a6534c03bc886b85e20629f85c95
[mech_eap.orig] / util_cksum.c
1 /*
2  * Copyright (c) 2010, 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 #include "gssapiP_eap.h"
55
56 static int
57 gssEapChecksum(krb5_context context,
58                krb5_cksumtype type,
59                size_t rrc,
60                krb5_keyblock *key,
61                krb5_keyusage sign_usage,
62                gss_iov_buffer_desc *iov,
63                int iov_count,
64                int verify,
65                int *valid)
66 {
67     krb5_error_code code;
68     gss_iov_buffer_desc *header;
69     gss_iov_buffer_desc *trailer;
70     krb5_crypto_iov *kiov;
71     size_t kiov_count;
72     int i = 0, j;
73     unsigned int k5_checksumlen;
74
75     if (verify)
76         *valid = FALSE;
77
78     code = krb5_c_crypto_length(context, KRB_KEYTYPE(key), KRB5_CRYPTO_TYPE_CHECKSUM, &k5_checksumlen);
79     if (code != 0)
80         return code;
81
82     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
83     assert(header != NULL);
84
85     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
86     assert(rrc != 0 || trailer != NULL);
87
88     if (trailer == NULL) {
89         if (rrc != k5_checksumlen)
90             return KRB5_BAD_MSIZE;
91         if (header->buffer.length != 16 + k5_checksumlen)
92             return KRB5_BAD_MSIZE;
93     } else if (trailer->buffer.length != k5_checksumlen)
94         return KRB5_BAD_MSIZE;
95
96     kiov_count = 2 + iov_count;
97     kiov = (krb5_crypto_iov *)GSSEAP_MALLOC(kiov_count * sizeof(krb5_crypto_iov));
98     if (kiov == NULL)
99         return ENOMEM;
100
101     /* Checksum over ( Data | Header ) */
102
103     /* Data */
104     for (j = 0; j < iov_count; j++) {
105         kiov[i].flags = gssEapTranslateCryptoFlag(iov[j].type);
106         kiov[i].data.length = iov[j].buffer.length;
107         kiov[i].data.data = (char *)iov[j].buffer.value;
108         i++;
109     }
110
111     /* Header */
112     kiov[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY;
113     kiov[i].data.length = 16;
114     kiov[i].data.data = (char *)header->buffer.value;
115     i++;
116
117     /* Checksum */
118     kiov[i].flags = KRB5_CRYPTO_TYPE_CHECKSUM;
119     if (trailer == NULL) {
120         kiov[i].data.length = header->buffer.length - 16;
121         kiov[i].data.data = (char *)header->buffer.value + 16;
122     } else {
123         kiov[i].data.length = trailer->buffer.length;
124         kiov[i].data.data = (char *)trailer->buffer.value;
125     }
126     i++;
127
128     if (verify) {
129         krb5_boolean kvalid = FALSE;
130
131         code = krb5_c_verify_checksum_iov(context, type, key,
132                                           sign_usage, kiov, kiov_count, &kvalid);
133
134         *valid = kvalid;
135     } else {
136         code = krb5_c_make_checksum_iov(context, type, key,
137                                         sign_usage, kiov, kiov_count);
138     }
139
140     GSSEAP_FREE(kiov);
141
142     return code;
143 }
144
145 int
146 gssEapSign(krb5_context context,
147            krb5_cksumtype type,
148            size_t rrc,
149            krb5_keyblock *key,
150            krb5_keyusage sign_usage,
151            gss_iov_buffer_desc *iov,
152            int iov_count)
153 {
154     return gssEapChecksum(context, type, rrc, key,
155                           sign_usage, iov, iov_count, 0, NULL);
156 }
157
158 int
159 gssEapVerify(krb5_context context,
160              krb5_cksumtype type,
161              size_t rrc,
162              krb5_keyblock *key,
163              krb5_keyusage sign_usage,
164              gss_iov_buffer_desc *iov,
165              int iov_count,
166              int *valid)
167 {
168     return gssEapChecksum(context, type, rrc, key,
169                           sign_usage, iov, iov_count, 1, valid);
170 }