2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 * Copyright 2009 by the Massachusetts Institute of Technology.
34 * All Rights Reserved.
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.
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.
60 #include "gssapiP_eap.h"
63 gssEapPseudoRandom(OM_uint32 *minor,
66 const gss_buffer_t prf_in,
75 krb5_context krbContext;
76 ssize_t desired_output_len = prf_out->length;
80 GSSEAP_KRB_INIT(&krbContext);
85 if (prf_key != GSS_C_PRF_KEY_PARTIAL &&
86 prf_key != GSS_C_PRF_KEY_FULL) {
87 code = GSSEAP_BAD_PRF_KEY;
91 code = krb5_c_prf_length(krbContext,
97 ns.length = 4 + prf_in->length;
98 ns.data = GSSEAP_MALLOC(ns.length);
99 if (ns.data == NULL) {
104 #ifndef HAVE_HEIMDAL_VERSION
105 /* Same API, but different allocation rules, unfortunately. */
107 t.data = GSSEAP_MALLOC(t.length);
108 if (t.data == NULL) {
114 memcpy((unsigned char *)ns.data + 4, prf_in->value, prf_in->length);
116 p = (unsigned char *)prf_out->value;
117 while (desired_output_len > 0) {
118 store_uint32_be(i, ns.data);
120 code = krb5_c_prf(krbContext, &ctx->rfc3961Key, &ns, &t);
124 memcpy(p, t.data, MIN(t.length, desired_output_len));
127 desired_output_len -= t.length;
133 gss_release_buffer(&tmpMinor, prf_out);
134 if (ns.data != NULL) {
135 memset(ns.data, 0, ns.length);
136 GSSEAP_FREE(ns.data);
138 #ifdef HAVE_HEIMDAL_VERSION
141 if (t.data != NULL) {
142 memset(t.data, 0, t.length);
149 return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;
152 OM_uint32 GSSAPI_CALLCONV
153 gss_pseudo_random(OM_uint32 *minor,
156 const gss_buffer_t prf_in,
157 ssize_t desired_output_len,
158 gss_buffer_t prf_out)
162 if (ctx == GSS_C_NO_CONTEXT) {
164 return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
168 prf_out->value = NULL;
172 GSSEAP_MUTEX_LOCK(&ctx->mutex);
174 if (!CTX_IS_ESTABLISHED(ctx)) {
175 major = GSS_S_NO_CONTEXT;
176 *minor = GSSEAP_CONTEXT_INCOMPLETE;
180 prf_out->value = GSSEAP_MALLOC(desired_output_len);
181 if (prf_out->value == NULL) {
182 major = GSS_S_FAILURE;
187 prf_out->length = desired_output_len;
189 major = gssEapPseudoRandom(minor, ctx, prf_key,
193 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);