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