Skip to content

Commit fe93bc4

Browse files
authored
Merge pull request #187 from Patater/tls-development-20190722
Bring in changes from Mbed TLS as of 2019-07-22
2 parents aad3dab + 8dd1690 commit fe93bc4

File tree

95 files changed

+802
-608
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+802
-608
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ massif-*
3131
# Python build artifacts:
3232
*.pyc
3333

34+
# CMake generates *.dir/ folders for in-tree builds (used by MSVC projects), ignore all of those:
35+
*.dir/
36+
37+
# Visual Studio artifacts
38+
/visualc/VS2010/.localhistory/
39+
/visualc/VS2010/.vs/
40+
/visualc/VS2010/Debug/
41+
/visualc/VS2010/Release/
42+
/visualc/VS2010/*.vcxproj.filters
43+
/visualc/VS2010/*.vcxproj.user
44+
3445
# Generated documentation:
3546
/apidoc
3647

CMakeLists.txt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ if(CMAKE_COMPILER_IS_GNU)
124124
# note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
125125
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
126126
OUTPUT_VARIABLE GCC_VERSION)
127-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings")
127+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings")
128+
if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
129+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla")
130+
endif()
128131
if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
129132
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
130133
endif()
@@ -141,7 +144,7 @@ if(CMAKE_COMPILER_IS_GNU)
141144
endif(CMAKE_COMPILER_IS_GNU)
142145

143146
if(CMAKE_COMPILER_IS_CLANG)
144-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow")
147+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla")
145148
set(CMAKE_C_FLAGS_RELEASE "-O2")
146149
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
147150
set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
@@ -219,13 +222,13 @@ if(ENABLE_TESTING)
219222
COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
220223
)
221224
endif(UNIX)
222-
endif()
223225

224-
# Make scripts needed for testing available in an out-of-source build.
225-
if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
226-
link_to_source(scripts)
227-
# Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
228-
# keep things simple with the sed commands in the memcheck target.
229-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
230-
${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
226+
# Make scripts needed for testing available in an out-of-source build.
227+
if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
228+
link_to_source(scripts)
229+
# Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
230+
# keep things simple with the sed commands in the memcheck target.
231+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
232+
${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
233+
endif()
231234
endif()

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
DESTDIR=/usr/local
32
PREFIX=mbedtls_
43

include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if(INSTALL_MBEDTLS_HEADERS)
1616
endif(INSTALL_MBEDTLS_HEADERS)
1717

1818
# Make config.h available in an out-of-source build.
19-
if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
19+
if (ENABLE_TESTING AND NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
2020
link_to_source(mbedtls)
2121
link_to_source(psa)
2222
endif()

include/mbedtls/config.h

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -277,28 +277,52 @@
277277
* For example, when a function accepts as input a pointer to a buffer that may
278278
* contain untrusted data, and its documentation mentions that this pointer
279279
* must not be NULL:
280-
* - the pointer is checked to be non-NULL only if this option is enabled
281-
* - the content of the buffer is always validated
280+
* - The pointer is checked to be non-NULL only if this option is enabled.
281+
* - The content of the buffer is always validated.
282282
*
283283
* When this flag is defined, if a library function receives a parameter that
284-
* is invalid, it will:
285-
* - invoke the macro MBEDTLS_PARAM_FAILED() which by default expands to a
286-
* call to the function mbedtls_param_failed()
287-
* - immediately return (with a specific error code unless the function
288-
* returns void and can't communicate an error).
289-
*
290-
* When defining this flag, you also need to:
291-
* - either provide a definition of the function mbedtls_param_failed() in
292-
* your application (see platform_util.h for its prototype) as the library
293-
* calls that function, but does not provide a default definition for it,
294-
* - or provide a different definition of the macro MBEDTLS_PARAM_FAILED()
295-
* below if the above mechanism is not flexible enough to suit your needs.
296-
* See the documentation of this macro later in this file.
284+
* is invalid:
285+
* 1. The function will invoke the macro MBEDTLS_PARAM_FAILED().
286+
* 2. If MBEDTLS_PARAM_FAILED() did not terminate the program, the function
287+
* will immediately return. If the function returns an Mbed TLS error code,
288+
* the error code in this case is MBEDTLS_ERR_xxx_BAD_INPUT_DATA.
289+
*
290+
* When defining this flag, you also need to arrange a definition for
291+
* MBEDTLS_PARAM_FAILED(). You can do this by any of the following methods:
292+
* - By default, the library defines MBEDTLS_PARAM_FAILED() to call a
293+
* function mbedtls_param_failed(), but the library does not define this
294+
* function. If you do not make any other arrangements, you must provide
295+
* the function mbedtls_param_failed() in your application.
296+
* See `platform_util.h` for its prototype.
297+
* - If you enable the macro #MBEDTLS_CHECK_PARAMS_ASSERT, then the
298+
* library defines MBEDTLS_PARAM_FAILED(\c cond) to be `assert(cond)`.
299+
* You can still supply an alternative definition of
300+
* MBEDTLS_PARAM_FAILED(), which may call `assert`.
301+
* - If you define a macro MBEDTLS_PARAM_FAILED() before including `config.h`
302+
* or you uncomment the definition of MBEDTLS_PARAM_FAILED() in `config.h`,
303+
* the library will call the macro that you defined and will not supply
304+
* its own version. Note that if MBEDTLS_PARAM_FAILED() calls `assert`,
305+
* you need to enable #MBEDTLS_CHECK_PARAMS_ASSERT so that library source
306+
* files include `<assert.h>`.
297307
*
298308
* Uncomment to enable validation of application-controlled parameters.
299309
*/
300310
//#define MBEDTLS_CHECK_PARAMS
301311

312+
/**
313+
* \def MBEDTLS_CHECK_PARAMS_ASSERT
314+
*
315+
* Allow MBEDTLS_PARAM_FAILED() to call `assert`, and make it default to
316+
* `assert`. This macro is only used if #MBEDTLS_CHECK_PARAMS is defined.
317+
*
318+
* If this macro is not defined, then MBEDTLS_PARAM_FAILED() defaults to
319+
* calling a function mbedtls_param_failed(). See the documentation of
320+
* #MBEDTLS_CHECK_PARAMS for details.
321+
*
322+
* Uncomment to allow MBEDTLS_PARAM_FAILED() to call `assert`.
323+
*/
324+
//#define MBEDTLS_CHECK_PARAMS_ASSERT
325+
302326
/* \} name SECTION: System support */
303327

304328
/**
@@ -1007,18 +1031,27 @@
10071031
/**
10081032
* \def MBEDTLS_USE_PSA_CRYPTO
10091033
*
1010-
* Make the X.509 and TLS library use PSA for cryptographic operations, see
1011-
* #MBEDTLS_PSA_CRYPTO_C.
1034+
* Make the X.509 and TLS library use PSA for cryptographic operations, and
1035+
* enable new APIs for using keys handled by PSA Crypto.
10121036
*
1013-
* Note: this option is still in progress, the full X.509 and TLS modules are
1014-
* not covered yet, but parts that are not ported to PSA yet will still work
1015-
* as usual, so enabling this option should not break backwards compatibility.
1037+
* \note Development of this option is currently in progress, and parts of Mbed
1038+
* TLS's X.509 and TLS modules are not ported to PSA yet. However, these parts
1039+
* will still continue to work as usual, so enabling this option should not
1040+
* break backwards compatibility.
10161041
*
1017-
* \warning Support for PSA is still an experimental feature.
1018-
* Any public API that depends on this option may change
1019-
* at any time until this warning is removed.
1042+
* \warning Support for PSA is still an experimental feature.
1043+
* Any public API that depends on this option may change
1044+
* at any time until this warning is removed.
1045+
*
1046+
* \warning This option enables new Mbed TLS APIs that are dependent on the
1047+
* PSA Crypto API, so can't come with the same stability guarantees as the
1048+
* rest of the Mbed TLS APIs. You're welcome to experiment with them, but for
1049+
* now, access to these APIs is opt-in (via enabling the present option), in
1050+
* order to clearly differentiate them from the stable Mbed TLS APIs.
10201051
*
10211052
* Requires: MBEDTLS_PSA_CRYPTO_C.
1053+
*
1054+
* Uncomment this to enable internal use of PSA Crypto and new associated APIs.
10221055
*/
10231056
//#define MBEDTLS_USE_PSA_CRYPTO
10241057

@@ -1702,6 +1735,10 @@
17021735
*
17031736
* Enable the Platform Security Architecture cryptography API.
17041737
*
1738+
* \warning The PSA Crypto API is still beta status. While you're welcome to
1739+
* experiment using it, incompatible API changes are still possible, and some
1740+
* parts may not have reached the same quality as the rest of Mbed TLS yet.
1741+
*
17051742
* Module: library/psa_crypto.c
17061743
*
17071744
* Requires: MBEDTLS_CTR_DRBG_C, MBEDTLS_ENTROPY_C
@@ -1950,6 +1987,42 @@
19501987
//#define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO mbedtls_platform_std_nv_seed_read /**< Default nv_seed_read function to use, can be undefined */
19511988
//#define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO mbedtls_platform_std_nv_seed_write /**< Default nv_seed_write function to use, can be undefined */
19521989

1990+
/**
1991+
* \brief This macro is invoked by the library when an invalid parameter
1992+
* is detected that is only checked with #MBEDTLS_CHECK_PARAMS
1993+
* (see the documentation of that option for context).
1994+
*
1995+
* When you leave this undefined here, the library provides
1996+
* a default definition. If the macro #MBEDTLS_CHECK_PARAMS_ASSERT
1997+
* is defined, the default definition is `assert(cond)`,
1998+
* otherwise the default definition calls a function
1999+
* mbedtls_param_failed(). This function is declared in
2000+
* `platform_util.h` for the benefit of the library, but
2001+
* you need to define in your application.
2002+
*
2003+
* When you define this here, this replaces the default
2004+
* definition in platform_util.h (which no longer declares the
2005+
* function mbedtls_param_failed()) and it is your responsibility
2006+
* to make sure this macro expands to something suitable (in
2007+
* particular, that all the necessary declarations are visible
2008+
* from within the library - you can ensure that by providing
2009+
* them in this file next to the macro definition).
2010+
* If you define this macro to call `assert`, also define
2011+
* #MBEDTLS_CHECK_PARAMS_ASSERT so that library source files
2012+
* include `<assert.h>`.
2013+
*
2014+
* Note that you may define this macro to expand to nothing, in
2015+
* which case you don't have to worry about declarations or
2016+
* definitions. However, you will then be notified about invalid
2017+
* parameters only in non-void functions, and void function will
2018+
* just silently return early on invalid parameters, which
2019+
* partially negates the benefits of enabling
2020+
* #MBEDTLS_CHECK_PARAMS in the first place, so is discouraged.
2021+
*
2022+
* \param cond The expression that should evaluate to true, but doesn't.
2023+
*/
2024+
//#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
2025+
19532026
/**
19542027
* Uncomment the macro to let mbed TLS use your alternate implementation of
19552028
* mbedtls_platform_zeroize(). This replaces the default implementation in

include/mbedtls/error.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
* ECP 4 10 (Started from top)
101101
* MD 5 5
102102
* HKDF 5 1 (Started from top)
103-
* CIPHER 6 8
104-
* SSL 6 23 (Started from top)
103+
* CIPHER 6 8 (Started from 0x6080)
104+
* SSL 6 24 (Started from top, plus 0x6000)
105105
* SSL 7 32
106106
*
107107
* Module dependent error code (5 bits 0x.00.-0x.F8.)

include/mbedtls/pem.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,27 @@ void mbedtls_pem_free( mbedtls_pem_context *ctx );
112112
* \brief Write a buffer of PEM information from a DER encoded
113113
* buffer.
114114
*
115-
* \param header header string to write
116-
* \param footer footer string to write
117-
* \param der_data DER data to write
118-
* \param der_len length of the DER data
119-
* \param buf buffer to write to
120-
* \param buf_len length of output buffer
121-
* \param olen total length written / required (if buf_len is not enough)
115+
* \param header The header string to write.
116+
* \param footer The footer string to write.
117+
* \param der_data The DER data to encode.
118+
* \param der_len The length of the DER data \p der_data in Bytes.
119+
* \param buf The buffer to write to.
120+
* \param buf_len The length of the output buffer \p buf in Bytes.
121+
* \param olen The address at which to store the total length written
122+
* or required (if \p buf_len is not enough).
122123
*
123-
* \return 0 on success, or a specific PEM or BASE64 error code. On
124-
* MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required
125-
* size.
124+
* \note You may pass \c NULL for \p buf and \c 0 for \p buf_len
125+
* to request the length of the resulting PEM buffer in
126+
* `*olen`.
127+
*
128+
* \note This function may be called with overlapping \p der_data
129+
* and \p buf buffers.
130+
*
131+
* \return \c 0 on success.
132+
* \return #MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL if \p buf isn't large
133+
* enough to hold the PEM buffer. In this case, `*olen` holds
134+
* the required minimum size of \p buf.
135+
* \return Another PEM or BASE64 error code on other kinds of failure.
126136
*/
127137
int mbedtls_pem_write_buffer( const char *header, const char *footer,
128138
const unsigned char *der_data, size_t der_len,

include/mbedtls/platform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
256256
* the destination buffer is too short.
257257
*/
258258
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
259+
#include <stdarg.h>
259260
/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
260261
int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
261262
#endif

include/mbedtls/platform_util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,24 @@ extern "C" {
4343

4444
#if defined(MBEDTLS_CHECK_PARAMS)
4545

46+
#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
47+
/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
48+
* (which is what our config.h suggests). */
49+
#include <assert.h>
50+
#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
51+
4652
#if defined(MBEDTLS_PARAM_FAILED)
4753
/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
4854
*
4955
* This flag can be used to check whether it is safe to assume that
5056
* MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
5157
*/
5258
#define MBEDTLS_PARAM_FAILED_ALT
59+
60+
#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
61+
#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
62+
#define MBEDTLS_PARAM_FAILED_ALT
63+
5364
#else /* MBEDTLS_PARAM_FAILED */
5465
#define MBEDTLS_PARAM_FAILED( cond ) \
5566
mbedtls_param_failed( #cond, __FILE__, __LINE__ )

library/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Also see "include/mbedtls/config.h"
33

44
CFLAGS ?= -O2
5-
WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement
5+
WARNING_CFLAGS ?= -Wall -Wextra
66
LDFLAGS ?=
77

88
CRYPTO_INCLUDES ?= -I../include

library/timing.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
5252

5353
#include <windows.h>
54-
#include <winbase.h>
5554
#include <process.h>
5655

5756
struct _hr_time

library/version_features.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include <string.h>
3333

34-
static const char *features[] = {
34+
static const char * const features[] = {
3535
#if defined(MBEDTLS_VERSION_FEATURES)
3636
#if defined(MBEDTLS_HAVE_ASM)
3737
"MBEDTLS_HAVE_ASM",
@@ -90,6 +90,9 @@ static const char *features[] = {
9090
#if defined(MBEDTLS_CHECK_PARAMS)
9191
"MBEDTLS_CHECK_PARAMS",
9292
#endif /* MBEDTLS_CHECK_PARAMS */
93+
#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
94+
"MBEDTLS_CHECK_PARAMS_ASSERT",
95+
#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
9396
#if defined(MBEDTLS_TIMING_ALT)
9497
"MBEDTLS_TIMING_ALT",
9598
#endif /* MBEDTLS_TIMING_ALT */
@@ -594,7 +597,7 @@ static const char *features[] = {
594597

595598
int mbedtls_version_check_feature( const char *feature )
596599
{
597-
const char **idx = features;
600+
const char * const *idx = features;
598601

599602
if( *idx == NULL )
600603
return( -2 );

programs/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
33

44
CFLAGS ?= -O2
5-
WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement
6-
WARNING_CXXFLAGS ?= -Wall -W
5+
WARNING_CFLAGS ?= -Wall -Wextra
6+
WARNING_CXXFLAGS ?= -Wall -Wextra
77
LDFLAGS ?=
88

99
LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64
@@ -244,6 +244,7 @@ psa/crypto_examples$(EXEXT): psa/crypto_examples.c $(DEP)
244244
clean:
245245
ifndef WINDOWS
246246
rm -f $(APPS) $(EXTRA_GENERATED)
247+
-rm -f test/cpp_dummy_build$(EXEXT)
247248
else
248249
if exist *.o del /S /Q /F *.o
249250
if exist *.exe del /S /Q /F *.exe

programs/aes/aescrypt2.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@ int main( void )
8080
}
8181
#else
8282

83-
#if defined(MBEDTLS_CHECK_PARAMS)
84-
#include "mbedtls/platform_util.h"
85-
void mbedtls_param_failed( const char *failure_condition,
86-
const char *file,
87-
int line )
88-
{
89-
mbedtls_printf( "%s:%i: Input param failed - %s\n",
90-
file, line, failure_condition );
91-
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
92-
}
93-
#endif
9483

9584
int main( int argc, char *argv[] )
9685
{

0 commit comments

Comments
 (0)