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 The Mbed TLS Contributors 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: 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 * 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 */ 50 #ifndef MBEDTLS_PLATFORM_UTIL_H 51 #define MBEDTLS_PLATFORM_UTIL_H 52 53 #if !defined(MBEDTLS_CONFIG_FILE) 54 #include "config.h" 55 #else 56 #include MBEDTLS_CONFIG_FILE 57 #endif 58 59 #include <stddef.h> 60 #if defined(MBEDTLS_HAVE_TIME_DATE) 61 #include "platform_time.h" 62 #include <time.h> 63 #endif /* MBEDTLS_HAVE_TIME_DATE */ 64 65 #ifdef __cplusplus 66 extern "C" { 67 #endif 68 69 #if defined(MBEDTLS_CHECK_PARAMS) 70 71 #if defined(MBEDTLS_CHECK_PARAMS_ASSERT) 72 /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert 73 * (which is what our config.h suggests). */ 74 #include <assert.h> 75 #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */ 76 77 #if defined(MBEDTLS_PARAM_FAILED) 78 /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. 79 * 80 * This flag can be used to check whether it is safe to assume that 81 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). 82 */ 83 #define MBEDTLS_PARAM_FAILED_ALT 84 85 #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT) 86 #define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) 87 #define MBEDTLS_PARAM_FAILED_ALT 88 89 #else /* MBEDTLS_PARAM_FAILED */ 90 #define MBEDTLS_PARAM_FAILED( cond ) \ 91 mbedtls_param_failed( #cond, __FILE__, __LINE__ ) 92 93 /** 94 * \brief User supplied callback function for parameter validation failure. 95 * See #MBEDTLS_CHECK_PARAMS for context. 96 * 97 * This function will be called unless an alternative treatement 98 * is defined through the #MBEDTLS_PARAM_FAILED macro. 99 * 100 * This function can return, and the operation will be aborted, or 101 * alternatively, through use of setjmp()/longjmp() can resume 102 * execution in the application code. 103 * 104 * \param failure_condition The assertion that didn't hold. 105 * \param file The file where the assertion failed. 106 * \param line The line in the file where the assertion failed. 107 */ 108 void mbedtls_param_failed( const char *failure_condition, 109 const char *file, 110 int line ); 111 #endif /* MBEDTLS_PARAM_FAILED */ 112 113 /* Internal macro meant to be called only from within the library. */ 114 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ 115 do { \ 116 if( !(cond) ) \ 117 { \ 118 MBEDTLS_PARAM_FAILED( cond ); \ 119 return( ret ); \ 120 } \ 121 } while( 0 ) 122 123 /* Internal macro meant to be called only from within the library. */ 124 #define MBEDTLS_INTERNAL_VALIDATE( cond ) \ 125 do { \ 126 if( !(cond) ) \ 127 { \ 128 MBEDTLS_PARAM_FAILED( cond ); \ 129 return; \ 130 } \ 131 } while( 0 ) 132 133 #else /* MBEDTLS_CHECK_PARAMS */ 134 135 /* Internal macros meant to be called only from within the library. */ 136 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) 137 #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) 138 139 #endif /* MBEDTLS_CHECK_PARAMS */ 140 141 /* Internal helper macros for deprecating API constants. */ 142 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 143 #if defined(MBEDTLS_DEPRECATED_WARNING) 144 /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here 145 * to avoid conflict with other headers which define and use 146 * it, too. We might want to move all these definitions here at 147 * some point for uniformity. */ 148 #define MBEDTLS_DEPRECATED __attribute__((deprecated)) 149 MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; 150 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ 151 ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) 152 MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; 153 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ 154 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) 155 #undef MBEDTLS_DEPRECATED 156 #else /* MBEDTLS_DEPRECATED_WARNING */ 157 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL 158 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL 159 #endif /* MBEDTLS_DEPRECATED_WARNING */ 160 #endif /* MBEDTLS_DEPRECATED_REMOVED */ 161 162 /** 163 * \brief Securely zeroize a buffer 164 * 165 * The function is meant to wipe the data contained in a buffer so 166 * that it can no longer be recovered even if the program memory 167 * is later compromised. Call this function on sensitive data 168 * stored on the stack before returning from a function, and on 169 * sensitive data stored on the heap before freeing the heap 170 * object. 171 * 172 * It is extremely difficult to guarantee that calls to 173 * mbedtls_platform_zeroize() are not removed by aggressive 174 * compiler optimizations in a portable way. For this reason, Mbed 175 * TLS provides the configuration option 176 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure 177 * mbedtls_platform_zeroize() to use a suitable implementation for 178 * their platform and needs 179 * 180 * \param buf Buffer to be zeroized 181 * \param len Length of the buffer in bytes 182 * 183 */ 184 void mbedtls_platform_zeroize( void *buf, size_t len ); 185 186 #if defined(MBEDTLS_HAVE_TIME_DATE) 187 /** 188 * \brief Platform-specific implementation of gmtime_r() 189 * 190 * The function is a thread-safe abstraction that behaves 191 * similarly to the gmtime_r() function from Unix/POSIX. 192 * 193 * Mbed TLS will try to identify the underlying platform and 194 * make use of an appropriate underlying implementation (e.g. 195 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is 196 * not possible, then gmtime() will be used. In this case, calls 197 * from the library to gmtime() will be guarded by the mutex 198 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is 199 * enabled. It is recommended that calls from outside the library 200 * are also guarded by this mutex. 201 * 202 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will 203 * unconditionally use the alternative implementation for 204 * mbedtls_platform_gmtime_r() supplied by the user at compile time. 205 * 206 * \param tt Pointer to an object containing time (in seconds) since the 207 * epoch to be converted 208 * \param tm_buf Pointer to an object where the results will be stored 209 * 210 * \return Pointer to an object of type struct tm on success, otherwise 211 * NULL 212 */ 213 struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, 214 struct tm *tm_buf ); 215 #endif /* MBEDTLS_HAVE_TIME_DATE */ 216 217 #ifdef __cplusplus 218 } 219 #endif 220 221 #endif /* MBEDTLS_PLATFORM_UTIL_H */ 222