Path: blob/main/crypto/heimdal/lib/gssapi/krb5/prf.c
34923 views
/*1* Copyright (c) 2007 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "gsskrb5_locl.h"3435OM_uint32 GSSAPI_CALLCONV36_gsskrb5_pseudo_random(OM_uint32 *minor_status,37gss_ctx_id_t context_handle,38int prf_key,39const gss_buffer_t prf_in,40ssize_t desired_output_len,41gss_buffer_t prf_out)42{43gsskrb5_ctx ctx = (gsskrb5_ctx)context_handle;44krb5_context context;45krb5_error_code ret;46krb5_crypto crypto;47krb5_data input, output;48uint32_t num;49OM_uint32 junk;50unsigned char *p;51krb5_keyblock *key = NULL;52size_t dol;5354if (ctx == NULL) {55*minor_status = 0;56return GSS_S_NO_CONTEXT;57}5859if (desired_output_len <= 0 || prf_in->length + 4 < prf_in->length) {60*minor_status = 0;61return GSS_S_FAILURE;62}63dol = desired_output_len;6465GSSAPI_KRB5_INIT (&context);6667switch(prf_key) {68case GSS_C_PRF_KEY_FULL:69_gsskrb5i_get_acceptor_subkey(ctx, context, &key);70break;71case GSS_C_PRF_KEY_PARTIAL:72_gsskrb5i_get_initiator_subkey(ctx, context, &key);73break;74default:75_gsskrb5_set_status(EINVAL, "unknown kerberos prf_key");76*minor_status = EINVAL;77return GSS_S_FAILURE;78}7980if (key == NULL) {81_gsskrb5_set_status(EINVAL, "no prf_key found");82*minor_status = EINVAL;83return GSS_S_FAILURE;84}8586ret = krb5_crypto_init(context, key, 0, &crypto);87krb5_free_keyblock (context, key);88if (ret) {89*minor_status = ret;90return GSS_S_FAILURE;91}9293prf_out->value = malloc(dol);94if (prf_out->value == NULL) {95_gsskrb5_set_status(GSS_KRB5_S_KG_INPUT_TOO_LONG, "Out of memory");96*minor_status = GSS_KRB5_S_KG_INPUT_TOO_LONG;97krb5_crypto_destroy(context, crypto);98return GSS_S_FAILURE;99}100prf_out->length = dol;101102HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);103104input.length = prf_in->length + 4;105input.data = malloc(prf_in->length + 4);106if (input.data == NULL) {107_gsskrb5_set_status(GSS_KRB5_S_KG_INPUT_TOO_LONG, "Out of memory");108*minor_status = GSS_KRB5_S_KG_INPUT_TOO_LONG;109gss_release_buffer(&junk, prf_out);110krb5_crypto_destroy(context, crypto);111HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);112return GSS_S_FAILURE;113}114memcpy(((uint8_t *)input.data) + 4, prf_in->value, prf_in->length);115116num = 0;117p = prf_out->value;118while(dol > 0) {119size_t tsize;120121_gsskrb5_encode_be_om_uint32(num, input.data);122123ret = krb5_crypto_prf(context, crypto, &input, &output);124if (ret) {125*minor_status = ret;126free(input.data);127gss_release_buffer(&junk, prf_out);128krb5_crypto_destroy(context, crypto);129HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);130return GSS_S_FAILURE;131}132133tsize = min(dol, output.length);134memcpy(p, output.data, tsize);135p += tsize;136dol -= tsize;137krb5_data_free(&output);138num++;139}140free(input.data);141142krb5_crypto_destroy(context, crypto);143144HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);145146return GSS_S_COMPLETE;147}148149150