LCOV - code coverage report
Current view: top level - source4/heimdal/lib/gssapi/mech - gss_accept_sec_context.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 105 136 77.2 %
Date: 2021-09-23 10:06:22 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*-
       2             :  * Copyright (c) 2005 Doug Rabson
       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             :  * 1. Redistributions of source code must retain the above copyright
       9             :  *    notice, this list of conditions and the following disclaimer.
      10             :  * 2. Redistributions in binary form must reproduce the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer in the
      12             :  *    documentation and/or other materials provided with the distribution.
      13             :  *
      14             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
      15             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      16             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      17             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      18             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      19             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      20             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      21             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      22             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      23             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      24             :  * SUCH DAMAGE.
      25             :  *
      26             :  *      $FreeBSD: src/lib/libgssapi/gss_accept_sec_context.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
      27             :  */
      28             : 
      29             : #include "mech_locl.h"
      30             : 
      31             : static OM_uint32
      32       44781 : parse_header(const gss_buffer_t input_token, gss_OID mech_oid)
      33             : {
      34       44781 :         unsigned char *p = input_token->value;
      35       44781 :         size_t len = input_token->length;
      36             :         size_t a, b;
      37             : 
      38             :         /*
      39             :          * Token must start with [APPLICATION 0] SEQUENCE.
      40             :          * But if it doesn't assume it is DCE-STYLE Kerberos!
      41             :          */
      42       44781 :         if (len == 0)
      43           0 :                 return (GSS_S_DEFECTIVE_TOKEN);
      44             : 
      45       44781 :         p++;
      46       44781 :         len--;
      47             : 
      48             :         /*
      49             :          * Decode the length and make sure it agrees with the
      50             :          * token length.
      51             :          */
      52       44781 :         if (len == 0)
      53           0 :                 return (GSS_S_DEFECTIVE_TOKEN);
      54       44781 :         if ((*p & 0x80) == 0) {
      55           2 :                 a = *p;
      56           2 :                 p++;
      57           2 :                 len--;
      58             :         } else {
      59       44779 :                 b = *p & 0x7f;
      60       44779 :                 p++;
      61       44779 :                 len--;
      62       44779 :                 if (len < b)
      63           0 :                     return (GSS_S_DEFECTIVE_TOKEN);
      64       44041 :                 a = 0;
      65      178378 :                 while (b) {
      66       89558 :                     a = (a << 8) | *p;
      67       89558 :                     p++;
      68       89558 :                     len--;
      69       89558 :                     b--;
      70             :                 }
      71             :         }
      72       44781 :         if (a != len)
      73           0 :                 return (GSS_S_DEFECTIVE_TOKEN);
      74             : 
      75             :         /*
      76             :          * Decode the OID for the mechanism. Simplify life by
      77             :          * assuming that the OID length is less than 128 bytes.
      78             :          */
      79       44781 :         if (len < 2 || *p != 0x06)
      80       28027 :                 return (GSS_S_DEFECTIVE_TOKEN);
      81       16658 :         if ((p[1] & 0x80) || p[1] > (len - 2))
      82           0 :                 return (GSS_S_DEFECTIVE_TOKEN);
      83       16658 :         mech_oid->length = p[1];
      84       16658 :         p += 2;
      85       16658 :         len -= 2;
      86       16658 :         mech_oid->elements = p;
      87             : 
      88       16658 :         return GSS_S_COMPLETE;
      89             : }
      90             : 
      91             : static gss_OID_desc krb5_mechanism =
      92             :     {9, rk_UNCONST("\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};
      93             : static gss_OID_desc ntlm_mechanism =
      94             :     {10, rk_UNCONST("\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a")};
      95             : static gss_OID_desc spnego_mechanism =
      96             :     {6, rk_UNCONST("\x2b\x06\x01\x05\x05\x02")};
      97             : 
      98             : static OM_uint32
      99       44781 : choose_mech(const gss_buffer_t input, gss_OID mech_oid)
     100             : {
     101             :         OM_uint32 status;
     102             : 
     103             :         /*
     104             :          * First try to parse the gssapi token header and see if it's a
     105             :          * correct header, use that in the first hand.
     106             :          */
     107             : 
     108       44781 :         status = parse_header(input, mech_oid);
     109       44781 :         if (status == GSS_S_COMPLETE)
     110       16016 :             return GSS_S_COMPLETE;
     111             : 
     112             :         /*
     113             :          * Lets guess what mech is really is, callback function to mech ??
     114             :          */
     115             : 
     116       56246 :         if (input->length > 8 &&
     117       28123 :             memcmp((const char *)input->value, "NTLMSSP\x00", 8) == 0)
     118             :         {
     119           0 :                 *mech_oid = ntlm_mechanism;
     120           0 :                 return GSS_S_COMPLETE;
     121       56246 :         } else if (input->length != 0 &&
     122       28123 :                    ((const char *)input->value)[0] == 0x6E)
     123             :         {
     124             :                 /* Could be a raw AP-REQ (check for APPLICATION tag) */
     125       28121 :                 *mech_oid = krb5_mechanism;
     126       28121 :                 return GSS_S_COMPLETE;
     127           2 :         } else if (input->length == 0) {
     128             :                 /*
     129             :                  * There is the a wierd mode of SPNEGO (in CIFS and
     130             :                  * SASL GSS-SPENGO where the first token is zero
     131             :                  * length and the acceptor returns a mech_list, lets
     132             :                  * hope that is what is happening now.
     133             :                  *
     134             :                  * http://msdn.microsoft.com/en-us/library/cc213114.aspx
     135             :                  * "NegTokenInit2 Variation for Server-Initiation"
     136             :                  */
     137           0 :                 *mech_oid = spnego_mechanism;
     138           0 :                 return GSS_S_COMPLETE;
     139             :         }
     140           2 :         return status;
     141             : }
     142             : 
     143             : 
     144             : GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
     145       72865 : gss_accept_sec_context(OM_uint32 *minor_status,
     146             :     gss_ctx_id_t *context_handle,
     147             :     const gss_cred_id_t acceptor_cred_handle,
     148             :     const gss_buffer_t input_token,
     149             :     const gss_channel_bindings_t input_chan_bindings,
     150             :     gss_name_t *src_name,
     151             :     gss_OID *mech_type,
     152             :     gss_buffer_t output_token,
     153             :     OM_uint32 *ret_flags,
     154             :     OM_uint32 *time_rec,
     155             :     gss_cred_id_t *delegated_cred_handle)
     156             : {
     157             :         OM_uint32 major_status, mech_ret_flags, junk;
     158             :         gssapi_mech_interface m;
     159       72865 :         struct _gss_context *ctx = (struct _gss_context *) *context_handle;
     160       72865 :         struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
     161             :         struct _gss_mechanism_cred *mc;
     162             :         gss_cred_id_t acceptor_mc, delegated_mc;
     163             :         gss_name_t src_mn;
     164       72865 :         gss_OID mech_ret_type = NULL;
     165             : 
     166       72865 :         *minor_status = 0;
     167       72865 :         if (src_name)
     168       72865 :             *src_name = GSS_C_NO_NAME;
     169       72865 :         if (mech_type)
     170       72865 :             *mech_type = GSS_C_NO_OID;
     171       72865 :         if (ret_flags)
     172       72865 :             *ret_flags = 0;
     173       72865 :         if (time_rec)
     174       72865 :             *time_rec = 0;
     175       72865 :         if (delegated_cred_handle)
     176       72865 :             *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
     177       72865 :         _mg_buffer_zero(output_token);
     178             : 
     179             : 
     180             :         /*
     181             :          * If this is the first call (*context_handle is NULL), we must
     182             :          * parse the input token to figure out the mechanism to use.
     183             :          */
     184       72865 :         if (*context_handle == GSS_C_NO_CONTEXT) {
     185             :                 gss_OID_desc mech_oid;
     186             : 
     187       44781 :                 major_status = choose_mech(input_token, &mech_oid);
     188       44781 :                 if (major_status != GSS_S_COMPLETE)
     189           4 :                         return major_status;
     190             : 
     191             :                 /*
     192             :                  * Now that we have a mechanism, we can find the
     193             :                  * implementation.
     194             :                  */
     195       44779 :                 ctx = malloc(sizeof(struct _gss_context));
     196       44779 :                 if (!ctx) {
     197           0 :                         *minor_status = ENOMEM;
     198           0 :                         return (GSS_S_DEFECTIVE_TOKEN);
     199             :                 }
     200       44779 :                 memset(ctx, 0, sizeof(struct _gss_context));
     201       44779 :                 m = ctx->gc_mech = __gss_get_mechanism(&mech_oid);
     202       44779 :                 if (!m) {
     203           0 :                         free(ctx);
     204           0 :                         return (GSS_S_BAD_MECH);
     205             :                 }
     206       44779 :                 *context_handle = (gss_ctx_id_t) ctx;
     207             :         } else {
     208       28084 :                 m = ctx->gc_mech;
     209             :         }
     210             : 
     211       72863 :         if (cred) {
     212       72863 :                 HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
     213       72863 :                         if (mc->gmc_mech == m)
     214       72029 :                                 break;
     215       72863 :                 if (!mc) {
     216           0 :                         gss_delete_sec_context(&junk, context_handle, NULL);
     217           0 :                         return (GSS_S_BAD_MECH);
     218             :                 }
     219       72863 :                 acceptor_mc = mc->gmc_cred;
     220             :         } else {
     221           0 :                 acceptor_mc = GSS_C_NO_CREDENTIAL;
     222             :         }
     223       72863 :         delegated_mc = GSS_C_NO_CREDENTIAL;
     224             : 
     225       72863 :         mech_ret_flags = 0;
     226       72863 :         major_status = m->gm_accept_sec_context(minor_status,
     227             :             &ctx->gc_ctx,
     228             :             acceptor_mc,
     229             :             input_token,
     230             :             input_chan_bindings,
     231             :             &src_mn,
     232             :             &mech_ret_type,
     233             :             output_token,
     234             :             &mech_ret_flags,
     235             :             time_rec,
     236             :             &delegated_mc);
     237       72863 :         if (major_status != GSS_S_COMPLETE &&
     238             :             major_status != GSS_S_CONTINUE_NEEDED)
     239             :         {
     240          39 :                 _gss_mg_error(m, major_status, *minor_status);
     241          39 :                 gss_delete_sec_context(&junk, context_handle, NULL);
     242          39 :                 return (major_status);
     243             :         }
     244             : 
     245       72824 :         if (mech_type)
     246       72824 :             *mech_type = mech_ret_type;
     247             : 
     248      145648 :         if (src_name && src_mn) {
     249             :                 /*
     250             :                  * Make a new name and mark it as an MN.
     251             :                  */
     252       72824 :                 struct _gss_name *name = _gss_make_name(m, src_mn);
     253             : 
     254       72824 :                 if (!name) {
     255           0 :                         m->gm_release_name(minor_status, &src_mn);
     256           0 :                         gss_delete_sec_context(&junk, context_handle, NULL);
     257           0 :                         return (GSS_S_FAILURE);
     258             :                 }
     259       72824 :                 *src_name = (gss_name_t) name;
     260           0 :         } else if (src_mn) {
     261           0 :                 m->gm_release_name(minor_status, &src_mn);
     262             :         }
     263             : 
     264       72824 :         if (mech_ret_flags & GSS_C_DELEG_FLAG) {
     265       42518 :                 if (!delegated_cred_handle) {
     266           0 :                         m->gm_release_cred(minor_status, &delegated_mc);
     267           0 :                         mech_ret_flags &=
     268             :                             ~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);
     269       42518 :                 } else if (gss_oid_equal(mech_ret_type, &m->gm_mech_oid) == 0) {
     270             :                         /*
     271             :                          * If the returned mech_type is not the same
     272             :                          * as the mech, assume its pseudo mech type
     273             :                          * and the returned type is already a
     274             :                          * mech-glue object
     275             :                          */
     276           0 :                         *delegated_cred_handle = delegated_mc;
     277             : 
     278       42518 :                 } else if (delegated_mc) {
     279             :                         struct _gss_cred *dcred;
     280             :                         struct _gss_mechanism_cred *dmc;
     281             : 
     282       42166 :                         dcred = malloc(sizeof(struct _gss_cred));
     283       42166 :                         if (!dcred) {
     284           0 :                                 *minor_status = ENOMEM;
     285           0 :                                 gss_delete_sec_context(&junk, context_handle, NULL);
     286           0 :                                 return (GSS_S_FAILURE);
     287             :                         }
     288       42166 :                         HEIM_SLIST_INIT(&dcred->gc_mc);
     289       42166 :                         dmc = malloc(sizeof(struct _gss_mechanism_cred));
     290       42166 :                         if (!dmc) {
     291           0 :                                 free(dcred);
     292           0 :                                 *minor_status = ENOMEM;
     293           0 :                                 gss_delete_sec_context(&junk, context_handle, NULL);
     294           0 :                                 return (GSS_S_FAILURE);
     295             :                         }
     296       42166 :                         dmc->gmc_mech = m;
     297       42166 :                         dmc->gmc_mech_oid = &m->gm_mech_oid;
     298       42166 :                         dmc->gmc_cred = delegated_mc;
     299       42166 :                         HEIM_SLIST_INSERT_HEAD(&dcred->gc_mc, dmc, gmc_link);
     300             : 
     301       42166 :                         *delegated_cred_handle = (gss_cred_id_t) dcred;
     302             :                 }
     303             :         }
     304             : 
     305       72824 :         if (ret_flags)
     306       72824 :             *ret_flags = mech_ret_flags;
     307       71990 :         return (major_status);
     308             : }

Generated by: LCOV version 1.13