Path: blob/main/crypto/heimdal/lib/gssapi/krb5/encapsulate.c
34923 views
/*1* Copyright (c) 1997 - 2003 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"3435void36_gssapi_encap_length (size_t data_len,37size_t *len,38size_t *total_len,39const gss_OID mech)40{41size_t len_len;4243*len = 1 + 1 + mech->length + data_len;4445len_len = der_length_len(*len);4647*total_len = 1 + len_len + *len;48}4950void51_gsskrb5_encap_length (size_t data_len,52size_t *len,53size_t *total_len,54const gss_OID mech)55{56_gssapi_encap_length(data_len + 2, len, total_len, mech);57}5859void *60_gsskrb5_make_header (void *ptr,61size_t len,62const void *type,63const gss_OID mech)64{65u_char *p = ptr;66p = _gssapi_make_mech_header(p, len, mech);67memcpy (p, type, 2);68p += 2;69return p;70}7172void *73_gssapi_make_mech_header(void *ptr,74size_t len,75const gss_OID mech)76{77u_char *p = ptr;78int e;79size_t len_len, foo;8081*p++ = 0x60;82len_len = der_length_len(len);83e = der_put_length (p + len_len - 1, len_len, len, &foo);84if(e || foo != len_len)85abort ();86p += len_len;87*p++ = 0x06;88*p++ = mech->length;89memcpy (p, mech->elements, mech->length);90p += mech->length;91return p;92}9394/*95* Give it a krb5_data and it will encapsulate with extra GSS-API wrappings.96*/9798OM_uint3299_gssapi_encapsulate(100OM_uint32 *minor_status,101const krb5_data *in_data,102gss_buffer_t output_token,103const gss_OID mech104)105{106size_t len, outer_len;107void *p;108109_gssapi_encap_length (in_data->length, &len, &outer_len, mech);110111output_token->length = outer_len;112output_token->value = malloc (outer_len);113if (output_token->value == NULL) {114*minor_status = ENOMEM;115return GSS_S_FAILURE;116}117118p = _gssapi_make_mech_header (output_token->value, len, mech);119memcpy (p, in_data->data, in_data->length);120return GSS_S_COMPLETE;121}122123/*124* Give it a krb5_data and it will encapsulate with extra GSS-API krb5125* wrappings.126*/127128OM_uint32129_gsskrb5_encapsulate(130OM_uint32 *minor_status,131const krb5_data *in_data,132gss_buffer_t output_token,133const void *type,134const gss_OID mech135)136{137size_t len, outer_len;138u_char *p;139140_gsskrb5_encap_length (in_data->length, &len, &outer_len, mech);141142output_token->length = outer_len;143output_token->value = malloc (outer_len);144if (output_token->value == NULL) {145*minor_status = ENOMEM;146return GSS_S_FAILURE;147}148149p = _gsskrb5_make_header (output_token->value, len, type, mech);150memcpy (p, in_data->data, in_data->length);151return GSS_S_COMPLETE;152}153154155