LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/gssapi/mech - gss_utils.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 77 141 54.6 %
Date: 2024-06-13 04:01:37 Functions: 9 19 47.4 %

          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_utils.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       45988 : _gss_copy_oid(OM_uint32 *minor_status,
      33             :               gss_const_OID from_oid,
      34             :               gss_OID to_oid)
      35             : {
      36       45988 :         size_t len = from_oid->length;
      37             : 
      38       45988 :         *minor_status = 0;
      39       45988 :         to_oid->elements = malloc(len);
      40       45988 :         if (!to_oid->elements) {
      41           0 :                 to_oid->length = 0;
      42           0 :                 *minor_status = ENOMEM;
      43           0 :                 return GSS_S_FAILURE;
      44             :         }
      45       45988 :         to_oid->length = (OM_uint32)len;
      46       45988 :         memcpy(to_oid->elements, from_oid->elements, len);
      47       45988 :         return (GSS_S_COMPLETE);
      48             : }
      49             : 
      50             : OM_uint32
      51           0 : _gss_free_oid(OM_uint32 *minor_status, gss_OID oid)
      52             : {
      53           0 :         *minor_status = 0;
      54           0 :         if (oid->elements) {
      55           0 :             free(oid->elements);
      56           0 :             oid->elements = NULL;
      57           0 :             oid->length = 0;
      58             :         }
      59           0 :         return (GSS_S_COMPLETE);
      60             : }
      61             : 
      62             : struct _gss_interned_oid {
      63             :     HEIM_SLIST_ATOMIC_ENTRY(_gss_interned_oid) gio_link;
      64             :     gss_OID_desc gio_oid;
      65             : };
      66             : 
      67             : static HEIM_SLIST_ATOMIC_HEAD(_gss_interned_oid_list, _gss_interned_oid) interned_oids =
      68             : HEIM_SLIST_HEAD_INITIALIZER(interned_oids);
      69             : 
      70             : extern gss_OID _gss_ot_internal[];
      71             : extern size_t _gss_ot_internal_count;
      72             : 
      73             : static OM_uint32
      74      235180 : intern_oid_static(OM_uint32 *minor_status,
      75             :                   gss_const_OID from_oid,
      76             :                   gss_OID *to_oid)
      77             : {
      78             :     size_t i;
      79             : 
      80             :     /* statically allocated OIDs */
      81    13110253 :     for (i = 0; i < _gss_ot_internal_count; i++) {
      82    13046082 :         if (gss_oid_equal(_gss_ot_internal[i], from_oid)) {
      83      171009 :             *minor_status = 0;
      84      171009 :             *to_oid = _gss_ot_internal[i];
      85      171009 :             return GSS_S_COMPLETE;
      86             :         }
      87             :     }
      88             : 
      89       64171 :     return GSS_S_CONTINUE_NEEDED;
      90             : }
      91             : 
      92             : OM_uint32
      93      235180 : _gss_intern_oid(OM_uint32 *minor_status,
      94             :                 gss_const_OID from_oid,
      95             :                 gss_OID *to_oid)
      96             : {
      97             :     OM_uint32 major_status;
      98             :     struct _gss_interned_oid *iop;
      99             : 
     100      235180 :     major_status = intern_oid_static(minor_status, from_oid, to_oid);
     101      235180 :     if (major_status != GSS_S_CONTINUE_NEEDED)
     102      171009 :         return major_status;
     103             : 
     104      169502 :     HEIM_SLIST_ATOMIC_FOREACH(iop, &interned_oids, gio_link) {
     105      123514 :         if (gss_oid_equal(&iop->gio_oid, from_oid)) {
     106       18183 :             *minor_status = 0;
     107       18183 :             *to_oid = &iop->gio_oid;
     108       18183 :             return GSS_S_COMPLETE;
     109             :         }
     110             :     }
     111             : 
     112       45988 :     iop = malloc(sizeof(*iop));
     113       45988 :     if (iop == NULL) {
     114           0 :         *minor_status = ENOMEM;
     115           0 :         return GSS_S_FAILURE;
     116             :     }
     117             : 
     118       45988 :     major_status = _gss_copy_oid(minor_status, from_oid, &iop->gio_oid);
     119       45988 :     if (GSS_ERROR(major_status)) {
     120           0 :         free(iop);
     121           0 :         return major_status;
     122             :     }
     123             : 
     124       45988 :     HEIM_SLIST_ATOMIC_INSERT_HEAD(&interned_oids, iop, gio_link);
     125             : 
     126       45988 :     *minor_status = 0;
     127       45988 :     *to_oid = &iop->gio_oid;
     128             : 
     129       45988 :     return GSS_S_COMPLETE;
     130             : }
     131             : 
     132             : OM_uint32
     133       18305 : _gss_copy_buffer(OM_uint32 *minor_status,
     134             :     const gss_buffer_t from_buf, gss_buffer_t to_buf)
     135             : {
     136       18305 :         size_t len = from_buf->length;
     137             : 
     138       18305 :         *minor_status = 0;
     139       18305 :         to_buf->value = malloc(len);
     140       18305 :         if (!to_buf->value) {
     141           0 :                 *minor_status = ENOMEM;
     142           0 :                 to_buf->length = 0;
     143           0 :                 return GSS_S_FAILURE;
     144             :         }
     145       18305 :         to_buf->length = len;
     146       18305 :         memcpy(to_buf->value, from_buf->value, len);
     147       18305 :         return (GSS_S_COMPLETE);
     148             : }
     149             : 
     150             : OM_uint32
     151       65069 : _gss_secure_release_buffer(OM_uint32 *minor_status,
     152             :                            gss_buffer_t buffer)
     153             : {
     154       65069 :     if (buffer->value)
     155       65069 :         memset_s(buffer->value, buffer->length, 0, buffer->length);
     156             : 
     157       65069 :     return gss_release_buffer(minor_status, buffer);
     158             : }
     159             : 
     160             : OM_uint32
     161       64218 : _gss_secure_release_buffer_set(OM_uint32 *minor_status,
     162             :                                gss_buffer_set_t *buffer_set)
     163             : {
     164             :     size_t i;
     165             :     OM_uint32 minor;
     166             : 
     167       64218 :     *minor_status = 0;
     168             : 
     169       64218 :     if (*buffer_set == GSS_C_NO_BUFFER_SET)
     170           0 :         return GSS_S_COMPLETE;
     171             : 
     172      128436 :     for (i = 0; i < (*buffer_set)->count; i++)
     173       64218 :         _gss_secure_release_buffer(&minor, &((*buffer_set)->elements[i]));
     174             : 
     175       64218 :     (*buffer_set)->count = 0;
     176             : 
     177       64218 :     return gss_release_buffer_set(minor_status, buffer_set);
     178             : }
     179             : 
     180             : void
     181           0 : _gss_mg_encode_le_uint32(uint32_t n, uint8_t *p)
     182             : {
     183           0 :     p[0] = (n >> 0 ) & 0xFF;
     184           0 :     p[1] = (n >> 8 ) & 0xFF;
     185           0 :     p[2] = (n >> 16) & 0xFF;
     186           0 :     p[3] = (n >> 24) & 0xFF;
     187           0 : }
     188             : 
     189             : void
     190           0 : _gss_mg_decode_le_uint32(const void *ptr, uint32_t *n)
     191             : {
     192           0 :     const uint8_t *p = ptr;
     193           0 :     *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
     194           0 : }
     195             : 
     196             : void
     197           0 : _gss_mg_encode_be_uint32(uint32_t n, uint8_t *p)
     198             : {
     199           0 :     p[0] = (n >> 24) & 0xFF;
     200           0 :     p[1] = (n >> 16) & 0xFF;
     201           0 :     p[2] = (n >> 8 ) & 0xFF;
     202           0 :     p[3] = (n >> 0 ) & 0xFF;
     203           0 : }
     204             : 
     205             : void
     206           0 : _gss_mg_decode_be_uint32(const void *ptr, uint32_t *n)
     207             : {
     208           0 :     const uint8_t *p = ptr;
     209           0 :     *n = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
     210           0 : }
     211             : 
     212             : void
     213           0 : _gss_mg_encode_le_uint16(uint16_t n, uint8_t *p)
     214             : {
     215           0 :     p[0] = (n >> 0 ) & 0xFF;
     216           0 :     p[1] = (n >> 8 ) & 0xFF;
     217           0 : }
     218             : 
     219             : void
     220           0 : _gss_mg_decode_le_uint16(const void *ptr, uint16_t *n)
     221             : {
     222           0 :     const uint8_t *p = ptr;
     223           0 :     *n = (p[0] << 0) | (p[1] << 8);
     224           0 : }
     225             : 
     226             : void
     227           0 : _gss_mg_encode_be_uint16(uint16_t n, uint8_t *p)
     228             : {
     229           0 :     p[0] = (n >> 8) & 0xFF;
     230           0 :     p[1] = (n >> 0) & 0xFF;
     231           0 : }
     232             : 
     233             : void
     234           0 : _gss_mg_decode_be_uint16(const void *ptr, uint16_t *n)
     235             : {
     236           0 :     const uint8_t *p = ptr;
     237           0 :     *n = (p[0] << 24) | (p[1] << 16);
     238           0 : }
     239             : 
     240             : OM_uint32
     241       47514 : _gss_mg_ret_oid(OM_uint32 *minor,
     242             :                 krb5_storage *sp,
     243             :                 gss_OID *oidp)
     244             : {
     245             :     krb5_data data;
     246             :     gss_OID_desc oid;
     247             :     OM_uint32 major;
     248             : 
     249       47514 :     *minor = 0;
     250       47514 :     *oidp = GSS_C_NO_OID;
     251             : 
     252       47514 :     *minor = krb5_ret_data(sp, &data);
     253       47514 :     if (*minor)
     254       23757 :         return GSS_S_FAILURE;
     255             : 
     256       23757 :     if (data.length) {
     257       23757 :         oid.length = data.length;
     258       23757 :         oid.elements = data.data;
     259             : 
     260       23757 :         major = _gss_intern_oid(minor, &oid, oidp);
     261             :     } else
     262           0 :         major = GSS_S_COMPLETE;
     263             : 
     264       23757 :     krb5_data_free(&data);
     265             : 
     266       23757 :     return major;
     267             : }
     268             : 
     269             : OM_uint32
     270         851 : _gss_mg_store_oid(OM_uint32 *minor,
     271             :                   krb5_storage *sp,
     272             :                   gss_const_OID oid)
     273             : {
     274             :     krb5_data data;
     275             : 
     276         851 :     if (oid) {
     277         851 :         data.length = oid->length;
     278         851 :         data.data = oid->elements;
     279             :     } else
     280           0 :         krb5_data_zero(&data);
     281             : 
     282         851 :     *minor = krb5_store_data(sp, data);
     283             : 
     284         851 :     return *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
     285             : }
     286             : 
     287             : OM_uint32
     288       23757 : _gss_mg_ret_buffer(OM_uint32 *minor,
     289             :                    krb5_storage *sp,
     290             :                    gss_buffer_t buffer)
     291             : {
     292             :     krb5_data data;
     293             : 
     294       23757 :     _mg_buffer_zero(buffer);
     295             : 
     296       23757 :     *minor = krb5_ret_data(sp, &data);
     297       23757 :     if (*minor == 0) {
     298       23757 :         if (data.length) {
     299       23757 :             buffer->length = data.length;
     300       23757 :             buffer->value = data.data;
     301             :         } else
     302           0 :             krb5_data_free(&data);
     303             :     }
     304             : 
     305       23757 :     return *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
     306             : }
     307             : 
     308             : OM_uint32
     309           0 : _gss_mg_store_buffer(OM_uint32 *minor,
     310             :                      krb5_storage *sp,
     311             :                      gss_const_buffer_t buffer)
     312             : {
     313             :     krb5_data data;
     314             : 
     315           0 :     if (buffer) {
     316           0 :         data.length = buffer->length;
     317           0 :         data.data = buffer->value;
     318             :     } else
     319           0 :         krb5_data_zero(&data);
     320             : 
     321           0 :     *minor =  krb5_store_data(sp, data);
     322             : 
     323           0 :     return *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
     324             : }

Generated by: LCOV version 1.13