Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 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: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
| 22 | * |
| 23 | * This file is part of Mbed TLS (https://tls.mbed.org) |
| 24 | */ |
| 25 | #ifndef MBEDTLS_PLATFORM_UTIL_H |
| 26 | #define MBEDTLS_PLATFORM_UTIL_H |
| 27 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 29 | #include "mbedtls/config.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 30 | #else |
| 31 | #include MBEDTLS_CONFIG_FILE |
| 32 | #endif |
| 33 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 34 | #include <stddef.h> |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 35 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 36 | #include "mbedtls/platform_time.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 37 | #include <time.h> |
| 38 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 44 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 45 | |
Gilles Peskine | c7ad122 | 2019-06-13 16:44:19 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_CHECK_PARAMS_ASSERT) |
| 47 | /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert |
| 48 | * (which is what our config.h suggests). */ |
| 49 | #include <assert.h> |
| 50 | #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */ |
| 51 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 52 | #if defined(MBEDTLS_PARAM_FAILED) |
| 53 | /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. |
| 54 | * |
| 55 | * This flag can be used to check whether it is safe to assume that |
| 56 | * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). |
| 57 | */ |
| 58 | #define MBEDTLS_PARAM_FAILED_ALT |
Gilles Peskine | c7ad122 | 2019-06-13 16:44:19 +0200 | [diff] [blame] | 59 | |
| 60 | #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT) |
| 61 | #define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) |
| 62 | #define MBEDTLS_PARAM_FAILED_ALT |
| 63 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 64 | #else /* MBEDTLS_PARAM_FAILED */ |
| 65 | #define MBEDTLS_PARAM_FAILED( cond ) \ |
| 66 | mbedtls_param_failed( #cond, __FILE__, __LINE__ ) |
| 67 | |
| 68 | /** |
| 69 | * \brief User supplied callback function for parameter validation failure. |
| 70 | * See #MBEDTLS_CHECK_PARAMS for context. |
| 71 | * |
| 72 | * This function will be called unless an alternative treatement |
| 73 | * is defined through the #MBEDTLS_PARAM_FAILED macro. |
| 74 | * |
| 75 | * This function can return, and the operation will be aborted, or |
| 76 | * alternatively, through use of setjmp()/longjmp() can resume |
| 77 | * execution in the application code. |
| 78 | * |
| 79 | * \param failure_condition The assertion that didn't hold. |
| 80 | * \param file The file where the assertion failed. |
| 81 | * \param line The line in the file where the assertion failed. |
| 82 | */ |
| 83 | void mbedtls_param_failed( const char *failure_condition, |
| 84 | const char *file, |
| 85 | int line ); |
| 86 | #endif /* MBEDTLS_PARAM_FAILED */ |
| 87 | |
| 88 | /* Internal macro meant to be called only from within the library. */ |
| 89 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ |
| 90 | do { \ |
| 91 | if( !(cond) ) \ |
| 92 | { \ |
| 93 | MBEDTLS_PARAM_FAILED( cond ); \ |
| 94 | return( ret ); \ |
| 95 | } \ |
| 96 | } while( 0 ) |
| 97 | |
| 98 | /* Internal macro meant to be called only from within the library. */ |
| 99 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) \ |
| 100 | do { \ |
| 101 | if( !(cond) ) \ |
| 102 | { \ |
| 103 | MBEDTLS_PARAM_FAILED( cond ); \ |
| 104 | return; \ |
| 105 | } \ |
| 106 | } while( 0 ) |
| 107 | |
| 108 | #else /* MBEDTLS_CHECK_PARAMS */ |
| 109 | |
| 110 | /* Internal macros meant to be called only from within the library. */ |
| 111 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) |
| 112 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) |
| 113 | |
| 114 | #endif /* MBEDTLS_CHECK_PARAMS */ |
| 115 | |
| 116 | /* Internal helper macros for deprecating API constants. */ |
| 117 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 118 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 119 | /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here |
| 120 | * to avoid conflict with other headers which define and use |
| 121 | * it, too. We might want to move all these definitions here at |
| 122 | * some point for uniformity. */ |
| 123 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 124 | MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; |
| 125 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ |
| 126 | ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) |
| 127 | MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; |
| 128 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ |
| 129 | ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) |
| 130 | #undef MBEDTLS_DEPRECATED |
| 131 | #else /* MBEDTLS_DEPRECATED_WARNING */ |
| 132 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL |
| 133 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL |
| 134 | #endif /* MBEDTLS_DEPRECATED_WARNING */ |
| 135 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 136 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 137 | /** |
| 138 | * \brief Securely zeroize a buffer |
| 139 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 140 | * The function is meant to wipe the data contained in a buffer so |
| 141 | * that it can no longer be recovered even if the program memory |
| 142 | * is later compromised. Call this function on sensitive data |
| 143 | * stored on the stack before returning from a function, and on |
| 144 | * sensitive data stored on the heap before freeing the heap |
| 145 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 146 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 147 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 148 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 149 | * compiler optimizations in a portable way. For this reason, Mbed |
| 150 | * TLS provides the configuration option |
| 151 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 152 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 153 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 154 | * |
| 155 | * \param buf Buffer to be zeroized |
| 156 | * \param len Length of the buffer in bytes |
| 157 | * |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 158 | */ |
| 159 | void mbedtls_platform_zeroize( void *buf, size_t len ); |
| 160 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 161 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 162 | /** |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 163 | * \brief Platform-specific implementation of gmtime_r() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 164 | * |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 165 | * The function is a thread-safe abstraction that behaves |
Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 166 | * similarly to the gmtime_r() function from Unix/POSIX. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 167 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 168 | * Mbed TLS will try to identify the underlying platform and |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 169 | * make use of an appropriate underlying implementation (e.g. |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 170 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 171 | * not possible, then gmtime() will be used. In this case, calls |
| 172 | * from the library to gmtime() will be guarded by the mutex |
| 173 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 174 | * enabled. It is recommended that calls from outside the library |
| 175 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 176 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 177 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 178 | * unconditionally use the alternative implementation for |
| 179 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 180 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 181 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 182 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 183 | * \param tm_buf Pointer to an object where the results will be stored |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 184 | * |
| 185 | * \return Pointer to an object of type struct tm on success, otherwise |
| 186 | * NULL |
| 187 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 188 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 189 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 190 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 191 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 192 | #ifdef __cplusplus |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |