Fixes for Heimdal (macOS) builds from Stefan.
[mech_eap.git] / mech_eap / pseudo_random.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 2009 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 /*
57  * PRF
58  */
59
60 #include "gssapiP_eap.h"
61
62 OM_uint32
63 gssEapPseudoRandom(OM_uint32 *minor,
64                    gss_const_ctx_id_t ctx,
65                    int prf_key,
66                    const gss_buffer_t prf_in,
67                    gss_buffer_t prf_out)
68 {
69     krb5_error_code code;
70     int i;
71     OM_uint32 tmpMinor;
72     size_t prflen;
73     krb5_data t, ns;
74     unsigned char *p;
75     krb5_context krbContext;
76     ssize_t desired_output_len = prf_out->length;
77 #ifdef HAVE_HEIMDAL_VERSION
78     krb5_crypto krbCrypto = NULL;
79 #endif
80
81     *minor = 0;
82
83     GSSEAP_KRB_INIT(&krbContext);
84
85     KRB_DATA_INIT(&t);
86     KRB_DATA_INIT(&ns);
87
88     if (prf_key != GSS_C_PRF_KEY_PARTIAL &&
89         prf_key != GSS_C_PRF_KEY_FULL) {
90         code = GSSEAP_BAD_PRF_KEY;
91         goto cleanup;
92     }
93
94 #ifdef HAVE_HEIMDAL_VERSION
95     code = krb5_crypto_prf_length(krbContext, ctx->encryptionType, &prflen);
96 #else
97     code = krb5_c_prf_length(krbContext, ctx->encryptionType, &prflen);
98 #endif
99     if (code != 0)
100         goto cleanup;
101
102     ns.length = 4 + prf_in->length;
103     ns.data = GSSEAP_MALLOC(ns.length);
104     if (ns.data == NULL) {
105         code = ENOMEM;
106         goto cleanup;
107     }
108
109 #ifdef HAVE_HEIMDAL_VERSION
110     code = krb5_crypto_init(krbContext, &ctx->rfc3961Key, 0, &krbCrypto);
111     if (code != 0)
112         goto cleanup;
113 #else
114     t.length = prflen;
115     t.data = GSSEAP_MALLOC(t.length);
116     if (t.data == NULL) {
117         code = ENOMEM;
118         goto cleanup;
119     }
120 #endif
121
122     memcpy((unsigned char *)ns.data + 4, prf_in->value, prf_in->length);
123     i = 0;
124     p = (unsigned char *)prf_out->value;
125     while (desired_output_len > 0) {
126         store_uint32_be(i, ns.data);
127
128 #ifdef HAVE_HEIMDAL_VERSION
129         code = krb5_crypto_prf(krbContext, krbCrypto, &ns, &t);
130 #else
131         code = krb5_c_prf(krbContext, &ctx->rfc3961Key, &ns, &t);
132 #endif
133         if (code != 0)
134             goto cleanup;
135
136         memcpy(p, t.data, MIN(t.length, desired_output_len));
137
138         p += t.length;
139         desired_output_len -= t.length;
140         i++;
141     }
142
143 cleanup:
144     if (code != 0)
145         gss_release_buffer(&tmpMinor, prf_out);
146     if (ns.data != NULL) {
147         memset(ns.data, 0, ns.length);
148         GSSEAP_FREE(ns.data);
149     }
150 #ifdef HAVE_HEIMDAL_VERSION
151     krb5_crypto_destroy(krbContext, krbCrypto);
152     krb5_data_free(&t);
153 #else
154     if (t.data != NULL) {
155         memset(t.data, 0, t.length);
156         GSSEAP_FREE(t.data);
157     }
158 #endif
159
160     *minor = code;
161
162     return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;
163 }
164
165 OM_uint32 GSSAPI_CALLCONV
166 gss_pseudo_random(OM_uint32 *minor,
167                   gss_ctx_id_t ctx,
168                   int prf_key,
169                   const gss_buffer_t prf_in,
170                   ssize_t desired_output_len,
171                   gss_buffer_t prf_out)
172 {
173     OM_uint32 major;
174
175     if (ctx == GSS_C_NO_CONTEXT) {
176         *minor = EINVAL;
177         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
178     }
179
180     prf_out->length = 0;
181     prf_out->value = NULL;
182
183     *minor = 0;
184
185     GSSEAP_MUTEX_LOCK(&ctx->mutex);
186
187     if (!CTX_IS_ESTABLISHED(ctx)) {
188         major = GSS_S_NO_CONTEXT;
189         *minor = GSSEAP_CONTEXT_INCOMPLETE;
190         goto cleanup;
191     }
192
193     prf_out->value = GSSEAP_MALLOC(desired_output_len);
194     if (prf_out->value == NULL) {
195         major = GSS_S_FAILURE;
196         *minor = ENOMEM;
197         goto cleanup;
198     }
199
200     prf_out->length = desired_output_len;
201
202     major = gssEapPseudoRandom(minor, ctx, prf_key,
203                                prf_in, prf_out);
204
205 cleanup:
206     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
207
208     return major;
209 }