Better error reporting through com_err
[mech_eap.git] / wrap_iov_length.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 2008 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  */
55
56 #include "gssapiP_eap.h"
57
58 #define INIT_IOV_DATA(_iov)     do { (_iov)->buffer.value = NULL;       \
59         (_iov)->buffer.length = 0; }                                    \
60     while (0)
61
62 OM_uint32
63 gssEapWrapIovLength(OM_uint32 *minor,
64                     gss_ctx_id_t ctx,
65                     int conf_req_flag,
66                     gss_qop_t qop_req,
67                     int *conf_state,
68                     gss_iov_buffer_desc *iov,
69                     int iov_count)
70 {
71     gss_iov_buffer_t header, trailer, padding;
72     size_t dataLength, assocDataLength;
73     size_t gssHeaderLen, gssPadLen, gssTrailerLen;
74     unsigned int krbHeaderLen = 0, krbTrailerLen = 0, krbPadLen = 0;
75     krb5_error_code code;
76     krb5_context krbContext;
77     int dce_style;
78     size_t ec;
79
80     if (qop_req != GSS_C_QOP_DEFAULT)
81         return GSS_S_FAILURE;
82
83     if (ctx->encryptionType == ENCTYPE_NULL)
84         return GSS_S_UNAVAILABLE;
85
86     GSSEAP_KRB_INIT(&krbContext);
87
88     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
89     if (header == NULL) {
90         *minor = GSSEAP_MISSING_IOV;
91         return GSS_S_FAILURE;
92     }
93     INIT_IOV_DATA(header);
94
95     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
96     if (trailer != NULL) {
97         INIT_IOV_DATA(trailer);
98     }
99
100     dce_style = ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0);
101
102     /* For CFX, EC is used instead of padding, and is placed in header or trailer */
103     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
104     if (padding != NULL) {
105         INIT_IOV_DATA(padding);
106     }
107
108     gssEapIovMessageLength(iov, iov_count, &dataLength, &assocDataLength);
109
110     if (conf_req_flag && gssEapIsIntegrityOnly(iov, iov_count))
111         conf_req_flag = FALSE;
112
113     gssHeaderLen = gssPadLen = gssTrailerLen = 0;
114
115     code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
116                                 conf_req_flag ?
117                                 KRB5_CRYPTO_TYPE_TRAILER : KRB5_CRYPTO_TYPE_CHECKSUM,
118                                 &krbTrailerLen);
119     if (code != 0) {
120         *minor = code;
121         return GSS_S_FAILURE;
122     }
123
124     if (conf_req_flag) {
125         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
126                                     KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
127         if (code != 0) {
128             *minor = code;
129             return GSS_S_FAILURE;
130         }
131     }
132
133     gssHeaderLen = 16; /* Header */
134     if (conf_req_flag) {
135         gssHeaderLen += krbHeaderLen; /* Kerb-Header */
136         gssTrailerLen = 16 /* E(Header) */ + krbTrailerLen; /* Kerb-Trailer */
137
138         code = krb5_c_padding_length(krbContext, ctx->encryptionType,
139                                      dataLength - assocDataLength + 16 /* E(Header) */,
140                                      &krbPadLen);
141         if (code != 0) {
142             *minor = code;
143             return GSS_S_FAILURE;
144         }
145
146         if (krbPadLen == 0 && dce_style) {
147             /* Windows rejects AEAD tokens with non-zero EC */
148             code = krb5_c_block_size(krbContext, ctx->encryptionType, &ec);
149             if (code != 0) {
150                 *minor = code;
151                 return GSS_S_FAILURE;
152             }
153         } else
154             ec = krbPadLen;
155
156         gssTrailerLen += ec;
157     } else {
158         gssTrailerLen = krbTrailerLen; /* Kerb-Checksum */
159     }
160
161     dataLength += gssPadLen;
162
163     if (trailer == NULL)
164         gssHeaderLen += gssTrailerLen;
165     else
166         trailer->buffer.length = gssTrailerLen;
167
168     assert(gssPadLen == 0 || padding != NULL);
169
170     if (padding != NULL)
171         padding->buffer.length = gssPadLen;
172
173     header->buffer.length = gssHeaderLen;
174
175     if (conf_state != NULL)
176         *conf_state = conf_req_flag;
177
178     *minor = 0;
179     return GSS_S_COMPLETE;
180 }
181
182 OM_uint32
183 gss_wrap_iov_length(OM_uint32 *minor,
184                     gss_ctx_id_t ctx,
185                     int conf_req_flag,
186                     gss_qop_t qop_req,
187                     int *conf_state,
188                     gss_iov_buffer_desc *iov,
189                     int iov_count)
190 {
191     OM_uint32 major;
192
193     if (ctx == GSS_C_NO_CONTEXT) {
194         *minor = EINVAL;
195         return GSS_S_NO_CONTEXT;
196     }
197
198     *minor = 0;
199
200     GSSEAP_MUTEX_LOCK(&ctx->mutex);
201
202     if (!CTX_IS_ESTABLISHED(ctx)) {
203         *minor = GSSEAP_CONTEXT_INCOMPLETE;
204         major = GSS_S_NO_CONTEXT;
205         goto cleanup;
206     }
207
208     major = gssEapWrapIovLength(minor, ctx, conf_req_flag, qop_req,
209                                 conf_state, iov, iov_count);
210     if (GSS_ERROR(major))
211         goto cleanup;
212
213 cleanup:
214     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
215
216     return major;
217 }