ReactOS 0.4.15-dev-7654-g6bc40d3
platform_util.c
Go to the documentation of this file.
1/*
2 * Common and shared functions used by multiple modules in the Mbed TLS
3 * library.
4 *
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 *
8 * This file is provided under the Apache License 2.0, or the
9 * GNU General Public License v2.0 or later.
10 *
11 * **********
12 * Apache License 2.0:
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *
26 * **********
27 *
28 * **********
29 * GNU General Public License v2.0 or later:
30 *
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44 *
45 * **********
46 */
47
48/*
49 * Ensure gmtime_r is available even with -std=c99; must be defined before
50 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
51 */
52#if !defined(_POSIX_C_SOURCE)
53#define _POSIX_C_SOURCE 200112L
54#endif
55
56#if !defined(MBEDTLS_CONFIG_FILE)
57#include "mbedtls/config.h"
58#else
59#include MBEDTLS_CONFIG_FILE
60#endif
61
63#include "mbedtls/platform.h"
64#include "mbedtls/threading.h"
65
66#include <stddef.h>
67#include <string.h>
68
69#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
70/*
71 * This implementation should never be optimized out by the compiler
72 *
73 * This implementation for mbedtls_platform_zeroize() was inspired from Colin
74 * Percival's blog article at:
75 *
76 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
77 *
78 * It uses a volatile function pointer to the standard memset(). Because the
79 * pointer is volatile the compiler expects it to change at
80 * any time and will not optimize out the call that could potentially perform
81 * other operations on the input buffer instead of just setting it to 0.
82 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
83 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
84 * details), optimizations of the following form are still possible:
85 *
86 * if( memset_func != memset )
87 * memset_func( buf, 0, len );
88 *
89 * Note that it is extremely difficult to guarantee that
90 * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
91 * in a portable way. For this reason, Mbed TLS also provides the configuration
92 * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
93 * mbedtls_platform_zeroize() to use a suitable implementation for their
94 * platform and needs.
95 */
96static void * (* const volatile memset_func)( void *, int, size_t ) = memset;
97
98void mbedtls_platform_zeroize( void *buf, size_t len )
99{
101
102 if( len > 0 )
103 memset_func( buf, 0, len );
104}
105#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
106
107#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
108#include <time.h>
109#if !defined(_WIN32) && (defined(unix) || \
110 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
111 defined(__MACH__)))
112#include <unistd.h>
113#endif /* !_WIN32 && (unix || __unix || __unix__ ||
114 * (__APPLE__ && __MACH__)) */
115
116#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
117 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
118 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) )
119/*
120 * This is a convenience shorthand macro to avoid checking the long
121 * preprocessor conditions above. Ideally, we could expose this macro in
122 * platform_util.h and simply use it in platform_util.c, threading.c and
123 * threading.h. However, this macro is not part of the Mbed TLS public API, so
124 * we keep it private by only defining it in this file
125 */
126#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
127#define PLATFORM_UTIL_USE_GMTIME
128#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
129
130#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
131 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
132 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
133
134struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
135 struct tm *tm_buf )
136{
137#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
138 return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL );
139#elif !defined(PLATFORM_UTIL_USE_GMTIME)
140 return( gmtime_r( tt, tm_buf ) );
141#else
142 struct tm *lt;
143
144#if defined(MBEDTLS_THREADING_C)
145 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
146 return( NULL );
147#endif /* MBEDTLS_THREADING_C */
148
149 lt = gmtime( tt );
150
151 if( lt != NULL )
152 {
153 memcpy( tm_buf, lt, sizeof( struct tm ) );
154 }
155
156#if defined(MBEDTLS_THREADING_C)
157 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
158 return( NULL );
159#endif /* MBEDTLS_THREADING_C */
160
161 return( ( lt == NULL ) ? NULL : tm_buf );
162#endif /* _WIN32 && !EFIX64 && !EFI32 */
163}
164#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
#define NULL
Definition: types.h:112
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLsizei len
Definition: glext.h:6722
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
time_t mbedtls_time_t
Definition: platform_time.h:78
static void *(*constvolatile memset_func)(void *, int, size_t)
Definition: platform_util.c:96
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
Definition: platform_util.c:98
Common and shared functions used by multiple modules in the Mbed TLS library.
#define MBEDTLS_INTERNAL_VALIDATE(cond)
_CRTIMP struct tm *__cdecl gmtime(const time_t *_Time)
Definition: time.h:415
Configuration options (set of defines)
This file contains the definitions and functions of the Mbed TLS platform abstraction layer.
#define memset(x, y, z)
Definition: compat.h:39
Definition: time.h:68
Threading abstraction layer.