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 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 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. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 22 | */ |
| 23 | #ifndef MBEDTLS_PLATFORM_UTIL_H |
| 24 | #define MBEDTLS_PLATFORM_UTIL_H |
| 25 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 27 | #include "mbedtls/config.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 28 | #else |
| 29 | #include MBEDTLS_CONFIG_FILE |
| 30 | #endif |
| 31 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 32 | #include <stddef.h> |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 33 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 34 | #include "mbedtls/platform_time.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 35 | #include <time.h> |
| 36 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 42 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 43 | |
Gilles Peskine | c7ad122 | 2019-06-13 16:44:19 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_CHECK_PARAMS_ASSERT) |
| 45 | /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert |
| 46 | * (which is what our config.h suggests). */ |
| 47 | #include <assert.h> |
| 48 | #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */ |
| 49 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 50 | #if defined(MBEDTLS_PARAM_FAILED) |
| 51 | /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. |
| 52 | * |
| 53 | * This flag can be used to check whether it is safe to assume that |
| 54 | * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). |
| 55 | */ |
| 56 | #define MBEDTLS_PARAM_FAILED_ALT |
Gilles Peskine | c7ad122 | 2019-06-13 16:44:19 +0200 | [diff] [blame] | 57 | |
| 58 | #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT) |
| 59 | #define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) |
| 60 | #define MBEDTLS_PARAM_FAILED_ALT |
| 61 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 62 | #else /* MBEDTLS_PARAM_FAILED */ |
| 63 | #define MBEDTLS_PARAM_FAILED( cond ) \ |
| 64 | mbedtls_param_failed( #cond, __FILE__, __LINE__ ) |
| 65 | |
| 66 | /** |
| 67 | * \brief User supplied callback function for parameter validation failure. |
| 68 | * See #MBEDTLS_CHECK_PARAMS for context. |
| 69 | * |
| 70 | * This function will be called unless an alternative treatement |
| 71 | * is defined through the #MBEDTLS_PARAM_FAILED macro. |
| 72 | * |
| 73 | * This function can return, and the operation will be aborted, or |
| 74 | * alternatively, through use of setjmp()/longjmp() can resume |
| 75 | * execution in the application code. |
| 76 | * |
| 77 | * \param failure_condition The assertion that didn't hold. |
| 78 | * \param file The file where the assertion failed. |
| 79 | * \param line The line in the file where the assertion failed. |
| 80 | */ |
| 81 | void mbedtls_param_failed( const char *failure_condition, |
| 82 | const char *file, |
| 83 | int line ); |
| 84 | #endif /* MBEDTLS_PARAM_FAILED */ |
| 85 | |
| 86 | /* Internal macro meant to be called only from within the library. */ |
| 87 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ |
| 88 | do { \ |
| 89 | if( !(cond) ) \ |
| 90 | { \ |
| 91 | MBEDTLS_PARAM_FAILED( cond ); \ |
| 92 | return( ret ); \ |
| 93 | } \ |
| 94 | } while( 0 ) |
| 95 | |
| 96 | /* Internal macro meant to be called only from within the library. */ |
| 97 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) \ |
| 98 | do { \ |
| 99 | if( !(cond) ) \ |
| 100 | { \ |
| 101 | MBEDTLS_PARAM_FAILED( cond ); \ |
| 102 | return; \ |
| 103 | } \ |
| 104 | } while( 0 ) |
| 105 | |
| 106 | #else /* MBEDTLS_CHECK_PARAMS */ |
| 107 | |
| 108 | /* Internal macros meant to be called only from within the library. */ |
| 109 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) |
| 110 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) |
| 111 | |
| 112 | #endif /* MBEDTLS_CHECK_PARAMS */ |
| 113 | |
| 114 | /* Internal helper macros for deprecating API constants. */ |
| 115 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 116 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 117 | /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here |
| 118 | * to avoid conflict with other headers which define and use |
| 119 | * it, too. We might want to move all these definitions here at |
| 120 | * some point for uniformity. */ |
| 121 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 122 | MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; |
| 123 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ |
| 124 | ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) |
| 125 | MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; |
| 126 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ |
| 127 | ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) |
| 128 | #undef MBEDTLS_DEPRECATED |
| 129 | #else /* MBEDTLS_DEPRECATED_WARNING */ |
| 130 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL |
| 131 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL |
| 132 | #endif /* MBEDTLS_DEPRECATED_WARNING */ |
| 133 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 134 | |
Gilles Peskine | ee0a443 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 135 | /* Implementation of the check-return facility. |
| 136 | * See the user documentation in config.h. |
Gilles Peskine | 7b8571f | 2021-07-07 21:02:36 +0200 | [diff] [blame] | 137 | * |
Gilles Peskine | ee0a443 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 138 | * Do not use this macro directly to annotate function: instead, |
| 139 | * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL |
| 140 | * depending on how important it is to check the return value. |
Gilles Peskine | 7b8571f | 2021-07-07 21:02:36 +0200 | [diff] [blame] | 141 | */ |
| 142 | #if !defined(MBEDTLS_CHECK_RETURN) |
| 143 | #if defined(__GNUC__) |
| 144 | #define MBEDTLS_CHECK_RETURN __attribute__((warn_unused_result)) |
| 145 | #elif defined(_MSC_VER) && _MSC_VER >= 1700 |
| 146 | #include <sal.h> |
| 147 | #define MBEDTLS_CHECK_RETURN _Check_return_ |
| 148 | #else |
| 149 | #define MBEDTLS_CHECK_RETURN |
| 150 | #endif |
| 151 | #endif |
| 152 | |
Gilles Peskine | ee0a443 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 153 | /** Critical-failure function |
| 154 | * |
| 155 | * This macro appearing at the beginning of the declaration of a function |
| 156 | * indicates that its return value should be checked in all applications. |
| 157 | * Omitting the check is very likely to indicate a bug in the application |
| 158 | * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN |
| 159 | * is implemented for the compiler in use. |
| 160 | * |
| 161 | * \note The use of this macro is a work in progress. |
| 162 | * This macro may be added to more functions in the future. |
| 163 | * Such an extension is not considered an API break, provided that |
| 164 | * there are near-unavoidable circumstances under which the function |
| 165 | * can fail. For example, signature/MAC/AEAD verification functions, |
| 166 | * and functions that require a random generator, are considered |
| 167 | * return-check-critical. |
| 168 | */ |
| 169 | #define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN |
| 170 | |
| 171 | /** Ordinary-failure function |
| 172 | * |
| 173 | * This macro appearing at the beginning of the declaration of a function |
| 174 | * indicates that its return value should be generally be checked in portable |
| 175 | * applications. Omitting the check will result in a compile-time warning if |
| 176 | * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use. |
| 177 | * |
| 178 | * \note The use of this macro is a work in progress. |
| 179 | * This macro will be added to more functions in the future. |
| 180 | * Eventually this should appear before most functions returning |
| 181 | * an error code (as \c int in the \c mbedtls_xxx API or |
| 182 | * as ::psa_status_t in the \c psa_xxx API). |
| 183 | */ |
| 184 | #define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN |
| 185 | |
| 186 | /** Benign-failure function |
| 187 | * |
| 188 | * This macro appearing at the beginning of the declaration of a function |
| 189 | * indicates that it is rarely useful to check its return value. |
| 190 | * |
| 191 | * This macro has an empty expansion. It exists for documentation purposes: |
| 192 | * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function |
| 193 | * has been analyzed for return-check usefuless, whereas the lack of |
| 194 | * an annotation indicates that the function has not been analyzed and its |
| 195 | * return-check usefulness is unknown. |
| 196 | */ |
| 197 | #define MBEDTLS_CHECK_RETURN_OPTIONAL |
| 198 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 199 | /** |
| 200 | * \brief Securely zeroize a buffer |
| 201 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 202 | * The function is meant to wipe the data contained in a buffer so |
| 203 | * that it can no longer be recovered even if the program memory |
| 204 | * is later compromised. Call this function on sensitive data |
| 205 | * stored on the stack before returning from a function, and on |
| 206 | * sensitive data stored on the heap before freeing the heap |
| 207 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 208 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 209 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 210 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 211 | * compiler optimizations in a portable way. For this reason, Mbed |
| 212 | * TLS provides the configuration option |
| 213 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 214 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 215 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 216 | * |
| 217 | * \param buf Buffer to be zeroized |
| 218 | * \param len Length of the buffer in bytes |
| 219 | * |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 220 | */ |
| 221 | void mbedtls_platform_zeroize( void *buf, size_t len ); |
| 222 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 223 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 224 | /** |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 225 | * \brief Platform-specific implementation of gmtime_r() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 226 | * |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 227 | * The function is a thread-safe abstraction that behaves |
Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 228 | * similarly to the gmtime_r() function from Unix/POSIX. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 229 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 230 | * Mbed TLS will try to identify the underlying platform and |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 231 | * make use of an appropriate underlying implementation (e.g. |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 232 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 233 | * not possible, then gmtime() will be used. In this case, calls |
| 234 | * from the library to gmtime() will be guarded by the mutex |
| 235 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 236 | * enabled. It is recommended that calls from outside the library |
| 237 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 238 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 239 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 240 | * unconditionally use the alternative implementation for |
| 241 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 242 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 243 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 244 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 245 | * \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] | 246 | * |
| 247 | * \return Pointer to an object of type struct tm on success, otherwise |
| 248 | * NULL |
| 249 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 250 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 251 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 252 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 253 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 254 | #ifdef __cplusplus |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |