Subversion Repositories Kolibri OS

Rev

Rev 8774 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Common and shared functions used by multiple modules in the Mbed TLS
  3.  * library.
  4.  *
  5.  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
  6.  *  SPDX-License-Identifier: GPL-2.0
  7.  *
  8.  *  This program is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2 of the License, or
  11.  *  (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License along
  19.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  20.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21.  *
  22.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  23.  */
  24.  
  25. /*
  26.  * Ensure gmtime_r is available even with -std=c99; must be defined before
  27.  * config.h, which pulls in glibc's features.h. Harmless on other platforms.
  28.  */
  29. #if !defined(_POSIX_C_SOURCE)
  30. #define _POSIX_C_SOURCE 200112L
  31. #endif
  32.  
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #include "mbedtls/config.h"
  35. #else
  36. #include MBEDTLS_CONFIG_FILE
  37. #endif
  38.  
  39. #include "mbedtls/platform_util.h"
  40. #include "mbedtls/platform.h"
  41. #include "mbedtls/threading.h"
  42.  
  43. #include <stddef.h>
  44. #include <string.h>
  45.  
  46. #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
  47. /*
  48.  * This implementation should never be optimized out by the compiler
  49.  *
  50.  * This implementation for mbedtls_platform_zeroize() was inspired from Colin
  51.  * Percival's blog article at:
  52.  *
  53.  * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
  54.  *
  55.  * It uses a volatile function pointer to the standard memset(). Because the
  56.  * pointer is volatile the compiler expects it to change at
  57.  * any time and will not optimize out the call that could potentially perform
  58.  * other operations on the input buffer instead of just setting it to 0.
  59.  * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
  60.  * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
  61.  * details), optimizations of the following form are still possible:
  62.  *
  63.  * if( memset_func != memset )
  64.  *     memset_func( buf, 0, len );
  65.  *
  66.  * Note that it is extremely difficult to guarantee that
  67.  * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
  68.  * in a portable way. For this reason, Mbed TLS also provides the configuration
  69.  * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
  70.  * mbedtls_platform_zeroize() to use a suitable implementation for their
  71.  * platform and needs.
  72.  */
  73. static void * (* const volatile memset_func)( void *, int, size_t ) = &memset;
  74.  
  75. void mbedtls_platform_zeroize( void *buf, size_t len )
  76. {
  77.     MBEDTLS_INTERNAL_VALIDATE( len == 0 || buf != NULL );
  78.  
  79.     if( len > 0 )
  80.         memset_func( buf, 0, len );
  81. }
  82. #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
  83.  
  84. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  85. #include <time.h>
  86. #if !defined(_WIN32) && (defined(unix) || \
  87.     defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
  88.     defined(__MACH__)))
  89. //#include <unistd.h>
  90. #endif /* !_WIN32 && (unix || __unix || __unix__ ||
  91.         * (__APPLE__ && __MACH__)) */
  92.  
  93. #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) ||     \
  94.        ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) &&                     \
  95.          _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
  96. /*
  97.  * This is a convenience shorthand macro to avoid checking the long
  98.  * preprocessor conditions above. Ideally, we could expose this macro in
  99.  * platform_util.h and simply use it in platform_util.c, threading.c and
  100.  * threading.h. However, this macro is not part of the Mbed TLS public API, so
  101.  * we keep it private by only defining it in this file
  102.  */
  103. #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
  104. #define PLATFORM_UTIL_USE_GMTIME
  105. #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
  106.  
  107. #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) ||     \
  108.              ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) &&                     \
  109.                 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
  110.  
  111. struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
  112.                                       struct tm *tm_buf )
  113. {
  114. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  115.     return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL );
  116. #elif !defined(PLATFORM_UTIL_USE_GMTIME)
  117.     return( gmtime_r( tt, tm_buf ) );
  118. #else
  119.     struct tm *lt;
  120.  
  121. #if defined(MBEDTLS_THREADING_C)
  122.     if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
  123.         return( NULL );
  124. #endif /* MBEDTLS_THREADING_C */
  125.  
  126.     lt = gmtime( tt );
  127.  
  128.     if( lt != NULL )
  129.     {
  130.         memcpy( tm_buf, lt, sizeof( struct tm ) );
  131.     }
  132.  
  133. #if defined(MBEDTLS_THREADING_C)
  134.     if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
  135.         return( NULL );
  136. #endif /* MBEDTLS_THREADING_C */
  137.  
  138.     return( ( lt == NULL ) ? NULL : tm_buf );
  139. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  140. }
  141. #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
  142.