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) |
| 29 | #include "mbedtls/config.h" |
| 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) |
Hanno Becker | 5a7fe14 | 2018-09-05 16:24:34 +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 | |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 44 | #if defined( MBEDTLS_CHECK_PARAMS ) |
| 45 | |
| 46 | #if defined(MBEDTLS_PARAM_FAILED) |
| 47 | /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. |
| 48 | * |
| 49 | * This flag can be used to check whether it is safe to assume that |
| 50 | * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). |
| 51 | */ |
| 52 | #define MBEDTLS_PARAM_FAILED_ALT |
| 53 | #else |
Manuel Pégourié-Gonnard | 8e661bf | 2018-12-10 12:41:46 +0100 | [diff] [blame] | 54 | #define MBEDTLS_PARAM_FAILED( cond ) \ |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 55 | mbedtls_param_failed( #cond, __FILE__, __LINE__ ) |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * \brief User supplied callback function for parameter validation failure. |
| 59 | * |
| 60 | * When the MBEDTLS_CHECK_PARAMS option is enabled, the library |
| 61 | * provides additional validation of all input parameters to |
| 62 | * confirm that they conform to what the interface can accept. |
| 63 | * For example - NULL paramater checks. |
| 64 | * |
| 65 | * These checks are designed to check programmatic issues in the |
| 66 | * application software using Mbed TLS, or catch other runtime |
| 67 | * errors which may be due to issues in the application software. |
| 68 | * |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 69 | * This function will be called unless an alternative treatement |
Manuel Pégourié-Gonnard | 22028a0 | 2018-12-11 10:29:25 +0100 | [diff] [blame] | 70 | * is defined through the MBEDTLS_PARAM_FAILED() macro. |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 71 | * |
| 72 | * This function can return, and the operation will be aborted, or |
| 73 | * alternatively, through use of setjmp()/longjmp() can resume |
| 74 | * execution in the application code. |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 75 | * |
| 76 | * \param failure_condition The assertion that didn't hold. |
| 77 | * \param file The file where the assertion failed. |
| 78 | * \param line The line in the file where the assertion failed. |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 79 | */ |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 80 | void mbedtls_param_failed( const char *failure_condition, |
| 81 | const char *file, |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 82 | int line ); |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 83 | #endif /* MBEDTLS_PARAM_FAILED */ |
Manuel Pégourié-Gonnard | 0e9cddb | 2018-12-10 16:37:51 +0100 | [diff] [blame] | 84 | |
| 85 | /* Internal macro meant to be called only from within the library. */ |
| 86 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ |
| 87 | do { \ |
| 88 | if( !(cond) ) \ |
| 89 | { \ |
Manuel Pégourié-Gonnard | 0e17cc9 | 2018-12-11 09:26:54 +0100 | [diff] [blame] | 90 | MBEDTLS_PARAM_FAILED( cond ); \ |
Manuel Pégourié-Gonnard | 0e9cddb | 2018-12-10 16:37:51 +0100 | [diff] [blame] | 91 | return( ret ); \ |
| 92 | } \ |
| 93 | } while( 0 ) |
| 94 | |
| 95 | /* Internal macro meant to be called only from within the library. */ |
| 96 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) \ |
| 97 | do { \ |
| 98 | if( !(cond) ) \ |
| 99 | { \ |
Manuel Pégourié-Gonnard | 0e17cc9 | 2018-12-11 09:26:54 +0100 | [diff] [blame] | 100 | MBEDTLS_PARAM_FAILED( cond ); \ |
Manuel Pégourié-Gonnard | 0e9cddb | 2018-12-10 16:37:51 +0100 | [diff] [blame] | 101 | return; \ |
| 102 | } \ |
| 103 | } while( 0 ) |
| 104 | |
| 105 | #else /* MBEDTLS_CHECK_PARAMS */ |
| 106 | |
| 107 | /* Internal macros meant to be called only from within the library. */ |
| 108 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) |
| 109 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) |
| 110 | |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 111 | #endif /* MBEDTLS_CHECK_PARAMS */ |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 112 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 113 | /** |
| 114 | * \brief Securely zeroize a buffer |
| 115 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 116 | * The function is meant to wipe the data contained in a buffer so |
| 117 | * that it can no longer be recovered even if the program memory |
| 118 | * is later compromised. Call this function on sensitive data |
| 119 | * stored on the stack before returning from a function, and on |
| 120 | * sensitive data stored on the heap before freeing the heap |
| 121 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 122 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 123 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 124 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 125 | * compiler optimizations in a portable way. For this reason, Mbed |
| 126 | * TLS provides the configuration option |
| 127 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 128 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 129 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 130 | * |
| 131 | * \param buf Buffer to be zeroized |
| 132 | * \param len Length of the buffer in bytes |
| 133 | * |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 134 | */ |
| 135 | void mbedtls_platform_zeroize( void *buf, size_t len ); |
| 136 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 137 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 138 | /** |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 139 | * \brief Platform-specific implementation of gmtime_r() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 140 | * |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 141 | * The function is a thread-safe abstraction that behaves |
Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 142 | * similarly to the gmtime_r() function from Unix/POSIX. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 143 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 144 | * Mbed TLS will try to identify the underlying platform and |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 145 | * make use of an appropriate underlying implementation (e.g. |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 146 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 147 | * not possible, then gmtime() will be used. In this case, calls |
| 148 | * from the library to gmtime() will be guarded by the mutex |
| 149 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 150 | * enabled. It is recommended that calls from outside the library |
| 151 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 152 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 153 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 154 | * unconditionally use the alternative implementation for |
| 155 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 156 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 157 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 158 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 159 | * \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] | 160 | * |
| 161 | * \return Pointer to an object of type struct tm on success, otherwise |
| 162 | * NULL |
| 163 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 164 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 165 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 166 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 167 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 168 | #ifdef __cplusplus |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |