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 master 2f515e9b Lines: 105 197 53.3 %
Date: 2024-04-21 15:09:00 Functions: 13 23 56.5 %

          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       90344 : _gss_copy_oid(OM_uint32 *minor_status,
      33             :               gss_const_OID from_oid,
      34             :               gss_OID to_oid)
      35             : {
      36       90344 :         size_t len = from_oid->length;
      37             : 
      38       90344 :         *minor_status = 0;
      39       90344 :         to_oid->elements = malloc(len);
      40       90344 :         if (!to_oid->elements) {
      41           0 :                 to_oid->length = 0;
      42           0 :                 *minor_status = ENOMEM;
      43           0 :                 return GSS_S_FAILURE;
      44             :         }
      45       90344 :         to_oid->length = (OM_uint32)len;
      46       90344 :         memcpy(to_oid->elements, from_oid->elements, len);
      47       90344 :         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      354519 : intern_oid_static(OM_uint32 *minor_status,
      75             :                   gss_const_OID from_oid,
      76             :                   gss_OID *to_oid)
      77             : {
      78       11810 :     size_t i;
      79             : 
      80             :     /* statically allocated OIDs */
      81    20404318 :     for (i = 0; i < _gss_ot_internal_count; i++) {
      82    20289858 :         if (gss_oid_equal(_gss_ot_internal[i], from_oid)) {
      83      240059 :             *minor_status = 0;
      84      240059 :             *to_oid = _gss_ot_internal[i];
      85      240059 :             return GSS_S_COMPLETE;
      86             :         }
      87             :     }
      88             : 
      89      109849 :     return GSS_S_CONTINUE_NEEDED;
      90             : }
      91             : 
      92             : OM_uint32
      93      354519 : _gss_intern_oid(OM_uint32 *minor_status,
      94             :                 gss_const_OID from_oid,
      95             :                 gss_OID *to_oid)
      96             : {
      97       11810 :     OM_uint32 major_status;
      98       11810 :     struct _gss_interned_oid *iop;
      99             : 
     100      354519 :     major_status = intern_oid_static(minor_status, from_oid, to_oid);
     101      354519 :     if (major_status != GSS_S_CONTINUE_NEEDED)
     102      232860 :         return major_status;
     103             : 
     104      298187 :     HEIM_SLIST_ATOMIC_FOREACH(iop, &interned_oids, gio_link) {
     105      207843 :         if (gss_oid_equal(&iop->gio_oid, from_oid)) {
     106       24116 :             *minor_status = 0;
     107       24116 :             *to_oid = &iop->gio_oid;
     108       24116 :             return GSS_S_COMPLETE;
     109             :         }
     110             :     }
     111             : 
     112       90344 :     iop = malloc(sizeof(*iop));
     113       90344 :     if (iop == NULL) {
     114           0 :         *minor_status = ENOMEM;
     115           0 :         return GSS_S_FAILURE;
     116             :     }
     117             : 
     118       90344 :     major_status = _gss_copy_oid(minor_status, from_oid, &iop->gio_oid);
     119       90344 :     if (GSS_ERROR(major_status)) {
     120           0 :         free(iop);
     121           0 :         return major_status;
     122             :     }
     123             : 
     124       90344 :     HEIM_SLIST_ATOMIC_INSERT_HEAD(&interned_oids, iop, gio_link);
     125             : 
     126       90344 :     *minor_status = 0;
     127       90344 :     *to_oid = &iop->gio_oid;
     128             : 
     129       90344 :     return GSS_S_COMPLETE;
     130             : }
     131             : 
     132             : OM_uint32
     133       24237 : _gss_copy_buffer(OM_uint32 *minor_status,
     134             :     const gss_buffer_t from_buf, gss_buffer_t to_buf)
     135             : {
     136       24237 :         size_t len = from_buf->length;
     137             : 
     138       24237 :         *minor_status = 0;
     139       24237 :         to_buf->value = malloc(len);
     140       24237 :         if (!to_buf->value) {
     141           0 :                 *minor_status = ENOMEM;
     142           0 :                 to_buf->length = 0;
     143           0 :                 return GSS_S_FAILURE;
     144             :         }
     145       24237 :         to_buf->length = len;
     146       24237 :         memcpy(to_buf->value, from_buf->value, len);
     147       24237 :         return (GSS_S_COMPLETE);
     148             : }
     149             : 
     150             : OM_uint32
     151       73288 : _gss_secure_release_buffer(OM_uint32 *minor_status,
     152             :                            gss_buffer_t buffer)
     153             : {
     154       73288 :     if (buffer->value)
     155       73288 :         memset_s(buffer->value, buffer->length, 0, buffer->length);
     156             : 
     157       73288 :     return gss_release_buffer(minor_status, buffer);
     158             : }
     159             : 
     160             : OM_uint32
     161       72500 : _gss_secure_release_buffer_set(OM_uint32 *minor_status,
     162             :                                gss_buffer_set_t *buffer_set)
     163             : {
     164         978 :     size_t i;
     165         978 :     OM_uint32 minor;
     166             : 
     167       72500 :     *minor_status = 0;
     168             : 
     169       72500 :     if (*buffer_set == GSS_C_NO_BUFFER_SET)
     170           0 :         return GSS_S_COMPLETE;
     171             : 
     172      145000 :     for (i = 0; i < (*buffer_set)->count; i++)
     173       72500 :         _gss_secure_release_buffer(&minor, &((*buffer_set)->elements[i]));
     174             : 
     175       72500 :     (*buffer_set)->count = 0;
     176             : 
     177       72500 :     return gss_release_buffer_set(minor_status, buffer_set);
     178             : }
     179             : 
     180             : void
     181           0 : _gss_mg_encode_le_uint64(uint64_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 :     p[4] = (n >> 32) & 0xFF;
     188           0 :     p[5] = (n >> 40) & 0xFF;
     189           0 :     p[6] = (n >> 48) & 0xFF;
     190           0 :     p[7] = (n >> 56) & 0xFF;
     191           0 : }
     192             : 
     193             : void
     194           0 : _gss_mg_decode_le_uint64(const void *ptr, uint64_t *n)
     195             : {
     196           0 :     const uint8_t *p = ptr;
     197           0 :     *n = ((uint64_t)p[0] << 0)
     198           0 :        | ((uint64_t)p[1] << 8)
     199           0 :        | ((uint64_t)p[2] << 16)
     200           0 :        | ((uint64_t)p[3] << 24)
     201           0 :        | ((uint64_t)p[4] << 32)
     202           0 :        | ((uint64_t)p[5] << 40)
     203           0 :        | ((uint64_t)p[6] << 48)
     204           0 :        | ((uint64_t)p[7] << 56);
     205           0 : }
     206             : 
     207             : void
     208           0 : _gss_mg_encode_be_uint64(uint64_t n, uint8_t *p)
     209             : {
     210           0 :     p[0] = (n >> 56) & 0xFF;
     211           0 :     p[1] = (n >> 48) & 0xFF;
     212           0 :     p[2] = (n >> 40) & 0xFF;
     213           0 :     p[3] = (n >> 32) & 0xFF;
     214           0 :     p[4] = (n >> 24) & 0xFF;
     215           0 :     p[5] = (n >> 16) & 0xFF;
     216           0 :     p[6] = (n >> 8 ) & 0xFF;
     217           0 :     p[7] = (n >> 0 ) & 0xFF;
     218           0 : }
     219             : 
     220             : void
     221           0 : _gss_mg_decode_be_uint64(const void *ptr, uint64_t *n)
     222             : {
     223           0 :     const uint8_t *p = ptr;
     224           0 :     *n = ((uint64_t)p[0] << 56)
     225           0 :        | ((uint64_t)p[1] << 48)
     226           0 :        | ((uint64_t)p[2] << 40)
     227           0 :        | ((uint64_t)p[3] << 32)
     228           0 :        | ((uint64_t)p[4] << 24)
     229           0 :        | ((uint64_t)p[5] << 16)
     230           0 :        | ((uint64_t)p[6] << 8)
     231           0 :        | ((uint64_t)p[7] << 0);
     232           0 : }
     233             : 
     234             : void
     235       48424 : _gss_mg_encode_le_uint32(uint32_t n, uint8_t *p)
     236             : {
     237       48424 :     p[0] = (n >> 0 ) & 0xFF;
     238       48424 :     p[1] = (n >> 8 ) & 0xFF;
     239       48424 :     p[2] = (n >> 16) & 0xFF;
     240       48424 :     p[3] = (n >> 24) & 0xFF;
     241       48424 : }
     242             : 
     243             : void
     244      104260 : _gss_mg_decode_le_uint32(const void *ptr, uint32_t *n)
     245             : {
     246      104260 :     const uint8_t *p = ptr;
     247      104260 :     *n = ((uint32_t)p[0] << 0)
     248      104260 :        | ((uint32_t)p[1] << 8)
     249      104260 :        | ((uint32_t)p[2] << 16)
     250      104260 :        | ((uint32_t)p[3] << 24);
     251      104260 : }
     252             : 
     253             : void
     254     4227534 : _gss_mg_encode_be_uint32(uint32_t n, uint8_t *p)
     255             : {
     256     4227534 :     p[0] = (n >> 24) & 0xFF;
     257     4227534 :     p[1] = (n >> 16) & 0xFF;
     258     4227534 :     p[2] = (n >> 8 ) & 0xFF;
     259     4227534 :     p[3] = (n >> 0 ) & 0xFF;
     260     4227534 : }
     261             : 
     262             : void
     263     2789056 : _gss_mg_decode_be_uint32(const void *ptr, uint32_t *n)
     264             : {
     265     2789056 :     const uint8_t *p = ptr;
     266     2789056 :     *n = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
     267     2789056 : }
     268             : 
     269             : void
     270           0 : _gss_mg_encode_le_uint16(uint16_t n, uint8_t *p)
     271             : {
     272           0 :     p[0] = (n >> 0 ) & 0xFF;
     273           0 :     p[1] = (n >> 8 ) & 0xFF;
     274           0 : }
     275             : 
     276             : void
     277           0 : _gss_mg_decode_le_uint16(const void *ptr, uint16_t *n)
     278             : {
     279           0 :     const uint8_t *p = ptr;
     280           0 :     *n = (p[0] << 0) | (p[1] << 8);
     281           0 : }
     282             : 
     283             : void
     284           0 : _gss_mg_encode_be_uint16(uint16_t n, uint8_t *p)
     285             : {
     286           0 :     p[0] = (n >> 8) & 0xFF;
     287           0 :     p[1] = (n >> 0) & 0xFF;
     288           0 : }
     289             : 
     290             : void
     291           0 : _gss_mg_decode_be_uint16(const void *ptr, uint16_t *n)
     292             : {
     293           0 :     const uint8_t *p = ptr;
     294           0 :     *n = (p[0] << 24) | (p[1] << 16);
     295           0 : }
     296             : 
     297             : OM_uint32
     298       46214 : _gss_mg_ret_oid(OM_uint32 *minor,
     299             :                 krb5_storage *sp,
     300             :                 gss_OID *oidp)
     301             : {
     302           0 :     krb5_data data;
     303           0 :     gss_OID_desc oid;
     304           0 :     OM_uint32 major;
     305             : 
     306       46214 :     *minor = 0;
     307       46214 :     *oidp = GSS_C_NO_OID;
     308             : 
     309       46214 :     *minor = krb5_ret_data(sp, &data);
     310       46214 :     if (*minor)
     311       23107 :         return GSS_S_FAILURE;
     312             : 
     313       23107 :     if (data.length) {
     314       23107 :         oid.length = data.length;
     315       23107 :         oid.elements = data.data;
     316             : 
     317       23107 :         major = _gss_intern_oid(minor, &oid, oidp);
     318             :     } else
     319           0 :         major = GSS_S_COMPLETE;
     320             : 
     321       23107 :     krb5_data_free(&data);
     322             : 
     323       23107 :     return major;
     324             : }
     325             : 
     326             : OM_uint32
     327         788 : _gss_mg_store_oid(OM_uint32 *minor,
     328             :                   krb5_storage *sp,
     329             :                   gss_const_OID oid)
     330             : {
     331           0 :     krb5_data data;
     332             : 
     333         788 :     if (oid) {
     334         788 :         data.length = oid->length;
     335         788 :         data.data = oid->elements;
     336             :     } else
     337           0 :         krb5_data_zero(&data);
     338             : 
     339         788 :     *minor = krb5_store_data(sp, data);
     340             : 
     341         788 :     return *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
     342             : }
     343             : 
     344             : OM_uint32
     345       23107 : _gss_mg_ret_buffer(OM_uint32 *minor,
     346             :                    krb5_storage *sp,
     347             :                    gss_buffer_t buffer)
     348             : {
     349           0 :     krb5_data data;
     350             : 
     351       23107 :     _mg_buffer_zero(buffer);
     352             : 
     353       23107 :     *minor = krb5_ret_data(sp, &data);
     354       23107 :     if (*minor == 0) {
     355       23107 :         if (data.length) {
     356       23107 :             buffer->length = data.length;
     357       23107 :             buffer->value = data.data;
     358             :         } else
     359           0 :             krb5_data_free(&data);
     360             :     }
     361             : 
     362       23107 :     return *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
     363             : }
     364             : 
     365             : OM_uint32
     366           0 : _gss_mg_store_buffer(OM_uint32 *minor,
     367             :                      krb5_storage *sp,
     368             :                      gss_const_buffer_t buffer)
     369             : {
     370           0 :     krb5_data data;
     371             : 
     372           0 :     if (buffer) {
     373           0 :         data.length = buffer->length;
     374           0 :         data.data = buffer->value;
     375             :     } else
     376           0 :         krb5_data_zero(&data);
     377             : 
     378           0 :     *minor =  krb5_store_data(sp, data);
     379             : 
     380           0 :     return *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
     381             : }

Generated by: LCOV version 1.14