Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file platform_util.h
  3.  *
  4.  * \brief Common and shared functions used by multiple modules in the Mbed TLS
  5.  *        library.
  6.  */
  7. /*
  8.  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
  9.  *  SPDX-License-Identifier: GPL-2.0
  10.  *
  11.  *  This program is free software; you can redistribute it and/or modify
  12.  *  it under the terms of the GNU General Public License as published by
  13.  *  the Free Software Foundation; either version 2 of the License, or
  14.  *  (at your option) any later version.
  15.  *
  16.  *  This program is distributed in the hope that it will be useful,
  17.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  *  GNU General Public License for more details.
  20.  *
  21.  *  You should have received a copy of the GNU General Public License along
  22.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  23.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24.  *
  25.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  26.  */
  27. #ifndef MBEDTLS_PLATFORM_UTIL_H
  28. #define MBEDTLS_PLATFORM_UTIL_H
  29.  
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35.  
  36. #include <stddef.h>
  37. #if defined(MBEDTLS_HAVE_TIME_DATE)
  38. #include "platform_time.h"
  39. #include <time.h>
  40. #endif /* MBEDTLS_HAVE_TIME_DATE */
  41.  
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45.  
  46. #if defined(MBEDTLS_CHECK_PARAMS)
  47.  
  48. #if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
  49. /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
  50.  * (which is what our config.h suggests). */
  51. #include <assert.h>
  52. #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
  53.  
  54. #if defined(MBEDTLS_PARAM_FAILED)
  55. /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
  56.  *
  57.  * This flag can be used to check whether it is safe to assume that
  58.  * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
  59.  */
  60. #define MBEDTLS_PARAM_FAILED_ALT
  61.  
  62. #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
  63. #define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
  64. #define MBEDTLS_PARAM_FAILED_ALT
  65.  
  66. #else /* MBEDTLS_PARAM_FAILED */
  67. #define MBEDTLS_PARAM_FAILED( cond ) \
  68.     mbedtls_param_failed( #cond, __FILE__, __LINE__ )
  69.  
  70. /**
  71.  * \brief       User supplied callback function for parameter validation failure.
  72.  *              See #MBEDTLS_CHECK_PARAMS for context.
  73.  *
  74.  *              This function will be called unless an alternative treatement
  75.  *              is defined through the #MBEDTLS_PARAM_FAILED macro.
  76.  *
  77.  *              This function can return, and the operation will be aborted, or
  78.  *              alternatively, through use of setjmp()/longjmp() can resume
  79.  *              execution in the application code.
  80.  *
  81.  * \param failure_condition The assertion that didn't hold.
  82.  * \param file  The file where the assertion failed.
  83.  * \param line  The line in the file where the assertion failed.
  84.  */
  85. void mbedtls_param_failed( const char *failure_condition,
  86.                            const char *file,
  87.                            int line );
  88. #endif /* MBEDTLS_PARAM_FAILED */
  89.  
  90. /* Internal macro meant to be called only from within the library. */
  91. #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret )  \
  92.     do {                                            \
  93.         if( !(cond) )                               \
  94.         {                                           \
  95.             MBEDTLS_PARAM_FAILED( cond );           \
  96.             return( ret );                          \
  97.         }                                           \
  98.     } while( 0 )
  99.  
  100. /* Internal macro meant to be called only from within the library. */
  101. #define MBEDTLS_INTERNAL_VALIDATE( cond )           \
  102.     do {                                            \
  103.         if( !(cond) )                               \
  104.         {                                           \
  105.             MBEDTLS_PARAM_FAILED( cond );           \
  106.             return;                                 \
  107.         }                                           \
  108.     } while( 0 )
  109.  
  110. #else /* MBEDTLS_CHECK_PARAMS */
  111.  
  112. /* Internal macros meant to be called only from within the library. */
  113. #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret )  do { } while( 0 )
  114. #define MBEDTLS_INTERNAL_VALIDATE( cond )           do { } while( 0 )
  115.  
  116. #endif /* MBEDTLS_CHECK_PARAMS */
  117.  
  118. /* Internal helper macros for deprecating API constants. */
  119. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  120. #if defined(MBEDTLS_DEPRECATED_WARNING)
  121. /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
  122.  * to avoid conflict with other headers which define and use
  123.  * it, too. We might want to move all these definitions here at
  124.  * some point for uniformity. */
  125. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  126. MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
  127. #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL )       \
  128.     ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
  129. MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
  130. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL )       \
  131.     ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
  132. #undef MBEDTLS_DEPRECATED
  133. #else /* MBEDTLS_DEPRECATED_WARNING */
  134. #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
  135. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
  136. #endif /* MBEDTLS_DEPRECATED_WARNING */
  137. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  138.  
  139. /**
  140.  * \brief       Securely zeroize a buffer
  141.  *
  142.  *              The function is meant to wipe the data contained in a buffer so
  143.  *              that it can no longer be recovered even if the program memory
  144.  *              is later compromised. Call this function on sensitive data
  145.  *              stored on the stack before returning from a function, and on
  146.  *              sensitive data stored on the heap before freeing the heap
  147.  *              object.
  148.  *
  149.  *              It is extremely difficult to guarantee that calls to
  150.  *              mbedtls_platform_zeroize() are not removed by aggressive
  151.  *              compiler optimizations in a portable way. For this reason, Mbed
  152.  *              TLS provides the configuration option
  153.  *              MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
  154.  *              mbedtls_platform_zeroize() to use a suitable implementation for
  155.  *              their platform and needs
  156.  *
  157.  * \param buf   Buffer to be zeroized
  158.  * \param len   Length of the buffer in bytes
  159.  *
  160.  */
  161. void mbedtls_platform_zeroize( void *buf, size_t len );
  162.  
  163. #if defined(MBEDTLS_HAVE_TIME_DATE)
  164. /**
  165.  * \brief      Platform-specific implementation of gmtime_r()
  166.  *
  167.  *             The function is a thread-safe abstraction that behaves
  168.  *             similarly to the gmtime_r() function from Unix/POSIX.
  169.  *
  170.  *             Mbed TLS will try to identify the underlying platform and
  171.  *             make use of an appropriate underlying implementation (e.g.
  172.  *             gmtime_r() for POSIX and gmtime_s() for Windows). If this is
  173.  *             not possible, then gmtime() will be used. In this case, calls
  174.  *             from the library to gmtime() will be guarded by the mutex
  175.  *             mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
  176.  *             enabled. It is recommended that calls from outside the library
  177.  *             are also guarded by this mutex.
  178.  *
  179.  *             If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
  180.  *             unconditionally use the alternative implementation for
  181.  *             mbedtls_platform_gmtime_r() supplied by the user at compile time.
  182.  *
  183.  * \param tt     Pointer to an object containing time (in seconds) since the
  184.  *               epoch to be converted
  185.  * \param tm_buf Pointer to an object where the results will be stored
  186.  *
  187.  * \return      Pointer to an object of type struct tm on success, otherwise
  188.  *              NULL
  189.  */
  190. struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
  191.                                       struct tm *tm_buf );
  192. #endif /* MBEDTLS_HAVE_TIME_DATE */
  193.  
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197.  
  198. #endif /* MBEDTLS_PLATFORM_UTIL_H */
  199.