Path: blob/main/crypto/heimdal/lib/gssapi/krb5/decapsulate.c
34923 views
/*1* Copyright (c) 1997 - 2001 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"3435/*36* return the length of the mechanism in token or -137* (which implies that the token was bad - GSS_S_DEFECTIVE_TOKEN38*/3940ssize_t41_gsskrb5_get_mech (const u_char *ptr,42size_t total_len,43const u_char **mech_ret)44{45size_t len, len_len, mech_len, foo;46const u_char *p = ptr;47int e;4849if (total_len < 1)50return -1;51if (*p++ != 0x60)52return -1;53e = der_get_length (p, total_len - 1, &len, &len_len);54if (e || 1 + len_len + len != total_len)55return -1;56if (total_len < 1 + len_len + 1)57return -1;58p += len_len;59if (*p++ != 0x06)60return -1;61e = der_get_length (p, total_len - 1 - len_len - 1,62&mech_len, &foo);63if (e)64return -1;65p += foo;66*mech_ret = p;67return mech_len;68}6970OM_uint3271_gssapi_verify_mech_header(u_char **str,72size_t total_len,73gss_OID mech)74{75const u_char *p;76ssize_t mech_len;7778mech_len = _gsskrb5_get_mech (*str, total_len, &p);79if (mech_len < 0)80return GSS_S_DEFECTIVE_TOKEN;8182if (mech_len != mech->length)83return GSS_S_BAD_MECH;84if (mech_len > total_len)85return GSS_S_BAD_MECH;86if (p - *str > total_len - mech_len)87return GSS_S_BAD_MECH;88if (ct_memcmp(p,89mech->elements,90mech->length) != 0)91return GSS_S_BAD_MECH;92p += mech_len;93*str = rk_UNCONST(p);94return GSS_S_COMPLETE;95}9697OM_uint3298_gsskrb5_verify_header(u_char **str,99size_t total_len,100const void *type,101gss_OID oid)102{103OM_uint32 ret;104size_t len;105u_char *p = *str;106107ret = _gssapi_verify_mech_header(str, total_len, oid);108if (ret)109return ret;110111len = total_len - (*str - p);112113if (len < 2)114return GSS_S_DEFECTIVE_TOKEN;115116if (ct_memcmp (*str, type, 2) != 0)117return GSS_S_DEFECTIVE_TOKEN;118*str += 2;119120return 0;121}122123/*124* Remove the GSS-API wrapping from `in_token' giving `out_data.125* Does not copy data, so just free `in_token'.126*/127128OM_uint32129_gssapi_decapsulate(130OM_uint32 *minor_status,131gss_buffer_t input_token_buffer,132krb5_data *out_data,133const gss_OID mech134)135{136u_char *p;137OM_uint32 ret;138139p = input_token_buffer->value;140ret = _gssapi_verify_mech_header(&p,141input_token_buffer->length,142mech);143if (ret) {144*minor_status = 0;145return ret;146}147148out_data->length = input_token_buffer->length -149(p - (u_char *)input_token_buffer->value);150out_data->data = p;151return GSS_S_COMPLETE;152}153154/*155* Remove the GSS-API wrapping from `in_token' giving `out_data.156* Does not copy data, so just free `in_token'.157*/158159OM_uint32160_gsskrb5_decapsulate(OM_uint32 *minor_status,161gss_buffer_t input_token_buffer,162krb5_data *out_data,163const void *type,164gss_OID oid)165{166u_char *p;167OM_uint32 ret;168169p = input_token_buffer->value;170ret = _gsskrb5_verify_header(&p,171input_token_buffer->length,172type,173oid);174if (ret) {175*minor_status = 0;176return ret;177}178179out_data->length = input_token_buffer->length -180(p - (u_char *)input_token_buffer->value);181out_data->data = p;182return GSS_S_COMPLETE;183}184185/*186* Verify padding of a gss wrapped message and return its length.187*/188189OM_uint32190_gssapi_verify_pad(gss_buffer_t wrapped_token,191size_t datalen,192size_t *padlen)193{194u_char *pad;195size_t padlength;196int i;197198pad = (u_char *)wrapped_token->value + wrapped_token->length;199padlength = pad[-1];200201if (padlength > datalen)202return GSS_S_BAD_MECH;203204for (i = padlength; i > 0 && *--pad == padlength; i--)205;206if (i != 0)207return GSS_S_BAD_MIC;208209*padlen = padlength;210211return 0;212}213214215