From: Kees Monshouwer Date: Fri, 14 Aug 2015 09:51:05 +0000 (+0200) Subject: update polarssl to mbedtls 1.3.12 X-Git-Tag: dnsdist-1.0.0-alpha1~248^2~62^2~10^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa6cd6087a9b0891cfbb2a2e3cedbc7397e0ba3e;p=thirdparty%2Fpdns.git update polarssl to mbedtls 1.3.12 --- diff --git a/build-scripts/debian-authoritative/copyright b/build-scripts/debian-authoritative/copyright index d64cbcbc3d..35ca39f529 100644 --- a/build-scripts/debian-authoritative/copyright +++ b/build-scripts/debian-authoritative/copyright @@ -89,7 +89,7 @@ Copyright: 2011 Milo Yip License: Expat Files: pdns/ext/polarssl* -Copyright: 2006-2014, Brainspark B.V. +Copyright (C) 2006-2014, ARM Limited License: GPL-2+ License: Expat diff --git a/build-scripts/debian-recursor/copyright b/build-scripts/debian-recursor/copyright index da18f6e82d..363ae7b27e 100644 --- a/build-scripts/debian-recursor/copyright +++ b/build-scripts/debian-recursor/copyright @@ -38,7 +38,7 @@ Copyright: 2011 Milo Yip License: Expat Files: pdns/ext/polarssl* -Copyright: 2006-2010, Brainspark B.V. +Copyright (C) 2006-2014, ARM Limited License: GPL-2+ Files: pdns/ext/yahttp* diff --git a/build-scripts/dist-recursor b/build-scripts/dist-recursor index c5b5aae7da..bedf8fddf8 100755 --- a/build-scripts/dist-recursor +++ b/build-scripts/dist-recursor @@ -65,10 +65,10 @@ EOF mkdir -p $DIRNAME/ext/rapidjson/include/rapidjson/internal cp -a ../ext/rapidjson/include/rapidjson/*.h $DIRNAME/ext/rapidjson/include/rapidjson/ cp -a ../ext/rapidjson/include/rapidjson/internal/*.h $DIRNAME/ext/rapidjson/include/rapidjson/internal -mkdir -p $DIRNAME/ext/polarssl/include/polarssl -cp -a ../ext/polarssl/include/polarssl/config.h ../ext/polarssl/include/polarssl/check_config.h ../ext/polarssl/include/polarssl/aes.h ../ext/polarssl/include/polarssl/base64.h ../ext/polarssl/include/polarssl/platform.h ../ext/polarssl/include/polarssl/version.h $DIRNAME/ext/polarssl/include/polarssl -mkdir -p $DIRNAME/ext/polarssl/library -cp -a ../ext/polarssl/library/aes.c ../ext/polarssl/library/base64.c $DIRNAME/ext/polarssl/library +mkdir -p $DIRNAME/ext/mbedtls/include/polarssl +cp -a ../ext/mbedtls/include/polarssl/config.h ../ext/mbedtls/include/polarssl/check_config.h ../ext/mbedtls/include/polarssl/aes.h ../ext/mbedtls/include/polarssl/base64.h ../ext/mbedtls/include/polarssl/platform.h ../ext/mbedtls/include/polarssl/version.h $DIRNAME/ext/mbedtls/include/polarssl +mkdir -p $DIRNAME/ext/mbedtls/library +cp -a ../ext/mbedtls/library/aes.c ../ext/mbedtls/library/base64.c $DIRNAME/ext/mbedtls/library cp -a ../ext/yahttp/ $DIRNAME/ext/yahttp echo '#include "../../../config.h"' > $DIRNAME/ext/yahttp/yahttp/yahttp-config.h mkdir $DIRNAME/rrd diff --git a/configure.ac b/configure.ac index 4de9c8900c..c68a07b2ae 100644 --- a/configure.ac +++ b/configure.ac @@ -350,8 +350,8 @@ AC_CONFIG_FILES([ ext/Makefile ext/yahttp/Makefile ext/yahttp/yahttp/Makefile - ext/polarssl/Makefile - ext/polarssl/library/Makefile + ext/mbedtls/Makefile + ext/mbedtls/library/Makefile ext/rapidjson/Makefile ext/json11/Makefile modules/bindbackend/Makefile diff --git a/ext/Makefile.am b/ext/Makefile.am index 88fb81f660..a06bda7327 100644 --- a/ext/Makefile.am +++ b/ext/Makefile.am @@ -5,7 +5,7 @@ SUBDIRS = \ json11 DIST_SUBDIRS = \ - polarssl \ + mbedtls \ yahttp \ rapidjson \ json11 diff --git a/ext/polarssl/.gitignore b/ext/mbedtls/.gitignore similarity index 100% rename from ext/polarssl/.gitignore rename to ext/mbedtls/.gitignore diff --git a/ext/mbedtls/CMakeLists.txt b/ext/mbedtls/CMakeLists.txt new file mode 100644 index 0000000000..bd0367d5a8 --- /dev/null +++ b/ext/mbedtls/CMakeLists.txt @@ -0,0 +1,124 @@ +cmake_minimum_required(VERSION 2.6) +project(MBEDTLS C) + +string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") + +if(CMAKE_COMPILER_IS_GNUCC) + # some warnings we want are not available with old GCC versions + # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION + execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion + OUTPUT_VARIABLE GCC_VERSION) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings") + if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op") + endif() + set(CMAKE_C_FLAGS_RELEASE "-O2") + set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") + set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") + set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -O3") + set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ") + set(CMAKE_C_FLAGS_CHECK "-Werror -O1") + set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") +endif(CMAKE_COMPILER_IS_GNUCC) + +if(CMAKE_COMPILER_IS_CLANG) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith") + set(CMAKE_C_FLAGS_RELEASE "-O2") + set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") + set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") + set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover -O3") + set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ") + set(CMAKE_C_FLAGS_MEMSAN "-Werror -fsanitize=memory -O3") + set(CMAKE_C_FLAGS_MEMSANDBG "-Werror -fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2") + set(CMAKE_C_FLAGS_CHECK "-Werror -O1") +endif(CMAKE_COMPILER_IS_CLANG) + +set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} + CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull" + FORCE) + +if(CMAKE_BUILD_TYPE STREQUAL "Coverage") + if(CMAKE_COMPILER_IS_GNUCC) + set(CMAKE_SHARED_LINKER_FLAGS "--coverage") + endif(CMAKE_COMPILER_IS_GNUCC) + if(CMAKE_COMPILER_IS_CLANG) + set(CMAKE_SHARED_LINKER_FLAGS "--coverage") + endif(CMAKE_COMPILER_IS_CLANG) +endif(CMAKE_BUILD_TYPE STREQUAL "Coverage") + +option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF) + +option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF) +option(ENABLE_PROGRAMS "Build mbed TLS programs." ON) +option(ENABLE_TESTING "Build mbed TLS tests." ON) + +if(ENABLE_TESTING) + enable_testing() +endif() + +if(LIB_INSTALL_DIR) +else() +set(LIB_INSTALL_DIR lib) +endif() + +include_directories(include/) + +if(ENABLE_ZLIB_SUPPORT) + find_package(ZLIB) + + if(ZLIB_FOUND) + include_directories(${ZLIB_INCLUDE_DIR}) + endif(ZLIB_FOUND) +endif(ENABLE_ZLIB_SUPPORT) + +add_subdirectory(library) +add_subdirectory(include) + +if(ENABLE_TESTING) + if(CMAKE_COMPILER_IS_GNUCC) + add_subdirectory(tests) + endif(CMAKE_COMPILER_IS_GNUCC) + if(CMAKE_COMPILER_IS_CLANG) + add_subdirectory(tests) + endif(CMAKE_COMPILER_IS_CLANG) +endif() + +if(ENABLE_PROGRAMS) + add_subdirectory(programs) +endif() + +ADD_CUSTOM_TARGET(apidoc + COMMAND doxygen doxygen/mbedtls.doxyfile + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + +if(ENABLE_TESTING) + ADD_CUSTOM_TARGET(test-ref-config + COMMAND tests/scripts/test-ref-configs.pl + ) + + ADD_CUSTOM_TARGET(covtest + COMMAND make test + COMMAND programs/test/selftest + COMMAND cd tests && ./compat.sh + COMMAND cd tests && ./ssl-opt.sh + ) + + ADD_CUSTOM_TARGET(lcov + COMMAND rm -rf Coverage + COMMAND lcov --capture --initial --directory library/CMakeFiles/mbedtls.dir -o files.info + COMMAND lcov --capture --directory library/CMakeFiles/mbedtls.dir -o tests.info + COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info + COMMAND lcov --remove all.info -o final.info '*.h' + COMMAND gendesc tests/Descriptions.txt -o descriptions + COMMAND genhtml --title "mbed TLS" --description-file descriptions --keep-descriptions --legend --no-branch-coverage -o Coverage final.info + COMMAND rm -f files.info tests.info all.info final.info descriptions + ) + + ADD_CUSTOM_TARGET(memcheck + COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl + COMMAND ctest -O memcheck.log -D ExperimentalMemCheck + COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null + COMMAND rm -f memcheck.log + COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl + ) +endif() diff --git a/ext/polarssl/ChangeLog b/ext/mbedtls/ChangeLog similarity index 79% rename from ext/polarssl/ChangeLog rename to ext/mbedtls/ChangeLog index bef1abea59..2da6a2ad39 100644 --- a/ext/polarssl/ChangeLog +++ b/ext/mbedtls/ChangeLog @@ -1,4 +1,196 @@ -PolarSSL ChangeLog (Sorted per branch, date) +mbed TLS ChangeLog (Sorted per branch, date) + += mbed TLS 1.3.12 released 2015-08-11 + +Security + * Increase the minimum size of Diffie-Hellman parameters accepted by the + client to 1024 bits, to protect against Logjam attack. + * Increase the size of default Diffie-Hellman parameters on the server to + 2048 bits. This can be changed with ssl_set_dh_params(). + +Bugfix + * Fix thread-safety issue in SSL debug module (found by Edwin van Vliet). + * Some example programs were not built using make, not included in Visual + Studio projects (found by Kristian Bendiksen). + * Fix build error with CMake and pre-4.5 versions of GCC (found by Hugo + Leisink). + * Fix missing -static-ligcc when building shared libraries for Windows with + make. + * Fix compile error with armcc5 --gnu. + +Changes + * Add SSL_MIN_DHM_BYTES configuration parameter in config.h to choose the + minimum size of Diffie-Hellman parameters accepted by the client. + * The PEM parser now accepts a trailing space at end of lines (#226). + += mbed TLS 1.3.11 released 2015-06-04 + +Security + * With authmode set to SSL_VERIFY_OPTIONAL, verification of keyUsage and + extendedKeyUsage on the leaf certificate was lost (results not accessible + via ssl_get_verify_results()). + * Add countermeasure against "Lucky 13 strikes back" cache-based attack, + https://dl.acm.org/citation.cfm?id=2714625 + +Features + * Improve ECC performance by using more efficient doubling formulas + (contributed by Peter Dettman). + * Add x509_crt_verify_info() to display certificate verification results. + * Add support for reading DH parameters with privateValueLength included + (contributed by Daniel Kahn Gillmor). + * Add support for bit strings in X.509 names (request by Fredrik Axelsson). + * Add support for id-at-uniqueIdentifier in X.509 names. + * Add support for overriding snprintf() (except on Windows) and exit() in + the platform layer. + * Add an option to use macros instead of function pointers in the platform + layer (helps get rid of unwanted references). + * Improved Makefiles for Windows targets by fixing library targets and making + cross-compilation easier (thanks to Alon Bar-Lev). + * The benchmark program also prints heap usage for public-key primitives + if POLARSSL_MEMORY_BUFFER_ALLOC_C and POLARSSL_MEMORY_DEBUG are defined. + * New script ecc-heap.sh helps measuring the impact of ECC parameters on + speed and RAM (heap only for now) usage. + * New script memory.sh helps measuring the ROM and RAM requirements of two + reduced configurations (PSK-CCM and NSA suite B). + * Add config flag POLARSSL_DEPRECATED_WARNING (off by default) to produce + warnings on use of deprecated functions (with GCC and Clang only). + * Add config flag POLARSSL_DEPRECATED_REMOVED (off by default) to produce + errors on use of deprecated functions. + +Bugfix + * Fix compile errors with PLATFORM_NO_STD_FUNCTIONS. + * Fix compile error with PLATFORM_EXIT_ALT (thanks to Rafał Przywara). + * Fix bug in entropy.c when THREADING_C is also enabled that caused + entropy_free() to crash (thanks to Rafał Przywara). + * Fix memory leak when gcm_setkey() and ccm_setkey() are used more than + once on the same context. + * Fix bug in ssl_mail_client when password is longer that username (found + by Bruno Pape). + * Fix undefined behaviour (memcmp( NULL, NULL, 0 );) in X.509 modules + (detected by Clang's 3.6 UBSan). + * mpi_size() and mpi_msb() would segfault when called on an mpi that is + initialized but not set (found by pravic). + * Fix detection of support for getrandom() on Linux (reported by syzzer) by + doing it at runtime (using uname) rather that compile time. + * Fix handling of symlinks by "make install" (found by Gaël PORTAY). + * Fix potential NULL pointer dereference (not trigerrable remotely) when + ssl_write() is called before the handshake is finished (introduced in + 1.3.10) (first reported by Martin Blumenstingl). + * Fix bug in pk_parse_key() that caused some valid private EC keys to be + rejected. + * Fix bug in Via Padlock support (found by Nikos Mavrogiannopoulos). + * Fix thread safety bug in RSA operations (found by Fredrik Axelsson). + * Fix hardclock() (only used in the benchmarking program) with some + versions of mingw64 (found by kxjhlele). + * Fix warnings from mingw64 in timing.c (found by kxjklele). + * Fix potential unintended sign extension in asn1_get_len() on 64-bit + platforms. + * Fix potential memory leak in ssl_set_psk() (found by Mansour Moufid). + * Fix compile error when POLARSSL_SSL_DISABLE_RENEGOTATION and + POLARSSL_SSL_SSESSION_TICKETS where both enabled in config.h (introduced + in 1.3.10). + * Add missing extern "C" guard in aesni.h (reported by amir zamani). + * Add missing dependency on SHA-256 in some x509 programs (reported by + Gergely Budai). + * Fix bug related to ssl_set_curves(): the client didn't check that the + curve picked by the server was actually allowed. + +Changes + * Remove bias in mpi_gen_prime (contributed by Pascal Junod). + * Remove potential sources of timing variations (some contributed by Pascal + Junod). + * Options POLARSSL_HAVE_INT8 and POLARSSL_HAVE_INT16 are deprecated. + * Enabling POLARSSL_NET_C without POLARSSL_HAVE_IPV6 is deprecated. + * compat-1.2.h and openssl.h are deprecated. + * Adjusting/overriding CFLAGS and LDFLAGS with the make build system is now + more flexible (warning: OFLAGS is not used any more) (see the README) + (contributed by Alon Bar-Lev). + * ssl_set_own_cert() no longer calls pk_check_pair() since the + performance impact was bad for some users (this was introduced in 1.3.10). + * Move from SHA-1 to SHA-256 in example programs using signatures + (suggested by Thorsten Mühlfelder). + * Remove some unneeded inclusions of header files from the standard library + "minimize" others (eg use stddef.h if only size_t is needed). + * Change #include lines in test files to use double quotes instead of angle + brackets for uniformity with the rest of the code. + * Remove dependency on sscanf() in X.509 parsing modules. + += mbed TLS 1.3.10 released 2015-02-09 +Security + * NULL pointer dereference in the buffer-based allocator when the buffer is + full and polarssl_free() is called (found by Mark Hasemeyer) + (only possible if POLARSSL_MEMORY_BUFFER_ALLOC_C is enabled, which it is + not by default). + * Fix remotely-triggerable uninitialised pointer dereference caused by + crafted X.509 certificate (TLS server is not affected if it doesn't ask for a + client certificate) (found using Codenomicon Defensics). + * Fix remotely-triggerable memory leak caused by crafted X.509 certificates + (TLS server is not affected if it doesn't ask for a client certificate) + (found using Codenomicon Defensics). + * Fix potential stack overflow while parsing crafted X.509 certificates + (TLS server is not affected if it doesn't ask for a client certificate) + (found using Codenomicon Defensics). + * Fix timing difference that could theoretically lead to a + Bleichenbacher-style attack in the RSA and RSA-PSK key exchanges + (reported by Sebastian Schinzel). + +Features + * Add support for FALLBACK_SCSV (draft-ietf-tls-downgrade-scsv). + * Add support for Extended Master Secret (draft-ietf-tls-session-hash). + * Add support for Encrypt-then-MAC (RFC 7366). + * Add function pk_check_pair() to test if public and private keys match. + * Add x509_crl_parse_der(). + * Add compile-time option POLARSSL_X509_MAX_INTERMEDIATE_CA to limit the + length of an X.509 verification chain. + * Support for renegotiation can now be disabled at compile-time + * Support for 1/n-1 record splitting, a countermeasure against BEAST. + * Certificate selection based on signature hash, preferring SHA-1 over SHA-2 + for pre-1.2 clients when multiple certificates are available. + * Add support for getrandom() syscall on recent Linux kernels with Glibc or + a compatible enough libc (eg uClibc). + * Add ssl_set_arc4_support() to make it easier to disable RC4 at runtime + while using the default ciphersuite list. + * Added new error codes and debug messages about selection of + ciphersuite/certificate. + +Bugfix + * Stack buffer overflow if ctr_drbg_update() is called with too large + add_len (found by Jean-Philippe Aumasson) (not triggerable remotely). + * Possible buffer overflow of length at most POLARSSL_MEMORY_ALIGN_MULTIPLE + if memory_buffer_alloc_init() was called with buf not aligned and len not + a multiple of POLARSSL_MEMORY_ALIGN_MULTIPLE (not triggerable remotely). + * User set CFLAGS were ignored by Cmake with gcc (introduced in 1.3.9, found + by Julian Ospald). + * Fix potential undefined behaviour in Camellia. + * Fix potential failure in ECDSA signatures when POLARSSL_ECP_MAX_BITS is a + multiple of 8 (found by Gergely Budai). + * Fix unchecked return code in x509_crt_parse_path() on Windows (found by + Peter Vaskovic). + * Fix assembly selection for MIPS64 (thanks to James Cowgill). + * ssl_get_verify_result() now works even if the handshake was aborted due + to a failed verification (found by Fredrik Axelsson). + * Skip writing and parsing signature_algorithm extension if none of the + key exchanges enabled needs certificates. This fixes a possible interop + issue with some servers when a zero-length extension was sent. (Reported + by Peter Dettman.) + * On a 0-length input, base64_encode() did not correctly set output length + (found by Hendrik van den Boogaard). + +Changes + * Use deterministic nonces for AEAD ciphers in TLS by default (possible to + switch back to random with POLARSSL_SSL_AEAD_RANDOM_IV in config.h). + * Blind RSA private operations even when POLARSSL_RSA_NO_CRT is defined. + * ssl_set_own_cert() now returns an error on key-certificate mismatch. + * Forbid repeated extensions in X.509 certificates. + * debug_print_buf() now prints a text view in addition to hexadecimal. + * A specific error is now returned when there are ciphersuites in common + but none of them is usable due to external factors such as no certificate + with a suitable (extended)KeyUsage or curve or no PSK set. + * It is now possible to disable negotiation of truncated HMAC server-side + at runtime with ssl_set_truncated_hmac(). + * Example programs for SSL client and server now disable SSLv3 by default. + * Example programs for SSL client and server now disable RC4 by default. + * Use platform.h in all test suites and programs. = PolarSSL 1.3.9 released 2014-10-20 Security @@ -427,6 +619,94 @@ Security * RSA blinding on CRT operations to counter timing attacks (found by Cyril Arnaud and Pierre-Alain Fouque) + += Version 1.2.14 released 2015-05-?? + +Security + * Fix potential invalid memory read in the server, that allows a client to + crash it remotely (found by Caj Larsson). + * Fix potential invalid memory read in certificate parsing, that allows a + client to crash the server remotely if client authentication is enabled + (found using Codenomicon Defensics). + * Add countermeasure against "Lucky 13 strikes back" cache-based attack, + https://dl.acm.org/citation.cfm?id=2714625 + +Bugfix + * Fix bug in Via Padlock support (found by Nikos Mavrogiannopoulos). + * Fix hardclock() (only used in the benchmarking program) with some + versions of mingw64 (found by kxjhlele). + * Fix warnings from mingw64 in timing.c (found by kxjklele). + * Fix potential unintended sign extension in asn1_get_len() on 64-bit + platforms (found with Coverity Scan). + += Version 1.2.13 released 2015-02-16 +Note: Although PolarSSL has been renamed to mbed TLS, no changes reflecting + this will be made in the 1.2 branch at this point. + +Security + * Fix remotely-triggerable uninitialised pointer dereference caused by + crafted X.509 certificate (TLS server is not affected if it doesn't ask + for a client certificate) (found using Codenomicon Defensics). + * Fix remotely-triggerable memory leak caused by crafted X.509 certificates + (TLS server is not affected if it doesn't ask for a client certificate) + (found using Codenomicon Defensics). + * Fix potential stack overflow while parsing crafted X.509 certificates + (TLS server is not affected if it doesn't ask for a client certificate) + found using Codenomicon Defensics). + * Fix buffer overread of size 1 when parsing crafted X.509 certificates + (TLS server is not affected if it doesn't ask for a client certificate). + +Bugfix + * Fix potential undefined behaviour in Camellia. + * Fix memory leaks in PKCS#5 and PKCS#12. + * Stack buffer overflow if ctr_drbg_update() is called with too large + add_len (found by Jean-Philippe Aumasson) (not triggerable remotely). + * Fix bug in MPI/bignum on s390/s390x (reported by Dan Horák) (introduced + in 1.2.12). + * Fix unchecked return code in x509_crt_parse_path() on Windows (found by + Peter Vaskovic). + * Fix assembly selection for MIPS64 (thanks to James Cowgill). + * ssl_get_verify_result() now works even if the handshake was aborted due + to a failed verification (found by Fredrik Axelsson). + * Skip writing and parsing signature_algorithm extension if none of the + key exchanges enabled needs certificates. This fixes a possible interop + issue with some servers when a zero-length extension was sent. (Reported + by Peter Dettman.) + * On a 0-length input, base64_encode() did not correctly set output length + (found by Hendrik van den Boogaard). + +Changes + * Blind RSA private operations even when POLARSSL_RSA_NO_CRT is defined. + * Forbid repeated extensions in X.509 certificates. + * Add compile-time option POLARSSL_X509_MAX_INTERMEDIATE_CA to limit the + length of an X.509 verification chain (default = 8). += Version 1.2.12 released 2014-10-24 + +Security + * Remotely-triggerable memory leak when parsing some X.509 certificates + (server is not affected if it doesn't ask for a client certificate). + (Found using Codenomicon Defensics.) + +Bugfix + * Fix potential bad read in parsing ServerHello (found by Adrien + Vialletelle). + * ssl_close_notify() could send more than one message in some circumstances + with non-blocking I/O. + * x509_crt_parse() did not increase total_failed on PEM error + * Fix compiler warnings on iOS (found by Sander Niemeijer). + * Don't print uninitialised buffer in ssl_mail_client (found by Marc Abel). + * Fix net_accept() regarding non-blocking sockets (found by Luca Pesce). + * ssl_read() could return non-application data records on server while + renegotation was pending, and on client when a HelloRequest was received. + * Fix warnings from Clang's scan-build (contributed by Alfred Klomp). + +Changes + * X.509 certificates with more than one AttributeTypeAndValue per + RelativeDistinguishedName are not accepted any more. + * ssl_read() now returns POLARSSL_ERR_NET_WANT_READ rather than + POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE on harmless alerts. + * Accept spaces at end of line or end of buffer in base64_decode(). + = Version 1.2.11 released 2014-07-11 Features * Entropy module now supports seed writing and reading diff --git a/ext/polarssl/DartConfiguration.tcl b/ext/mbedtls/DartConfiguration.tcl similarity index 77% rename from ext/polarssl/DartConfiguration.tcl rename to ext/mbedtls/DartConfiguration.tcl index da4c741a66..dfa0f07181 100644 --- a/ext/polarssl/DartConfiguration.tcl +++ b/ext/mbedtls/DartConfiguration.tcl @@ -1,4 +1,4 @@ Site: localhost -BuildName: PolarSSL-test +BuildName: mbed TLS-test CoverageCommand: /usr/bin/gcov MemoryCheckCommand: /usr/bin/valgrind diff --git a/ext/polarssl/LICENSE b/ext/mbedtls/LICENSE similarity index 100% rename from ext/polarssl/LICENSE rename to ext/mbedtls/LICENSE diff --git a/ext/polarssl/Makefile.am b/ext/mbedtls/Makefile.am similarity index 100% rename from ext/polarssl/Makefile.am rename to ext/mbedtls/Makefile.am diff --git a/ext/polarssl/README.rst b/ext/mbedtls/README.rst similarity index 54% rename from ext/polarssl/README.rst rename to ext/mbedtls/README.rst index 8dd823c9c2..59ca2c67da 100644 --- a/ext/polarssl/README.rst +++ b/ext/mbedtls/README.rst @@ -1,11 +1,20 @@ =================== -README for PolarSSL +README for mbed TLS =================== +Configuration +============= + +mbed TLS should build out of the box on most systems. Some platform specific options are available in the fully-documented configuration file *include/polarssl/config.h*, which is also the place where features can be selected. +This file can be edited manually, or in a more programmatic way using the Perl +script *scripts/config.pl* (use *--help* for usage instructions). + +Compiler options can be set using standard variables such as *CC* and *CFLAGS* when using the Make and CMake build system (see below). + Compiling ========= -There are currently three active build systems within the PolarSSL releases: +There are currently three active build systems within the mbed TLS releases: - Make - CMake @@ -26,7 +35,11 @@ In order to run the tests, enter:: make check -Depending on your platform, you might run into some issues. Please check the Makefiles in *library/*, *programs/* and *tests/* for options to manually add or remove for specific platforms. You can also check `the PolarSSL Knowledge Base `_ for articles on your platform or issue. +In order to build for a Windows platform, you should use WINDOWS_BUILD=1 if the target is Windows but the build environment is Unix-like (eg when cross-compiling, or compiling from an MSYS shell), and WINDOWS=1 if the build environment is a Windows shell. + +Setting the variable SHARED in your environment will build a shared library in addition to the static library. Setting DEBUG gives you a debug build. You can override CFLAGS and LDFLAGS by setting them in your environment or on the make command line; if you do so, essential parts such as -I will still be preserved. Warning options may be overridden separately using WARNING_CFLAGS. + +Depending on your platform, you might run into some issues. Please check the Makefiles in *library/*, *programs/* and *tests/* for options to manually add or remove for specific platforms. You can also check `the mbed TLS Knowledge Base `_ for articles on your platform or issue. In case you find that you need to do something else as well, please let us know what, so we can add it to the KB. @@ -39,7 +52,7 @@ In order to build the source using CMake, just enter at the command line:: make -There are 5 different active build modes specified within the CMake buildsystem: +There are many different build modes available within the CMake buildsystem. Most of them are available for gcc and clang, though some are compiler-specific: - Release. This generates the default code without any unnecessary information in the binary files. @@ -49,13 +62,30 @@ There are 5 different active build modes specified within the CMake buildsystem: This generates code coverage information in addition to debug information. - ASan. This instruments the code with AddressSanitizer to check for memory errors. + (This includes LeakSanitizer, with recent version of gcc and clang.) + (With recent version of clang, this mode also instruments the code with + UndefinedSanitizer to check for undefined behaviour.) +- ASanDbg. + Same as ASan but slower, with debug information and better stack traces. +- MemSan. + This instruments the code with MemorySanitizer to check for uninitialised + memory reads. Experimental, needs recent clang on Linux/x86_64. +- MemSanDbg. + Same as ASan but slower, with debug information, better stack traces and + origin tracking. - Check. - This activates more compiler warnings and treats them as errors. + This activates the compiler warnings that depend on optimization and treats + all warnings as errors. Switching build modes in CMake is simple. For debug mode, enter at the command line: cmake -D CMAKE_BUILD_TYPE:String="Debug" . +Note that, with CMake, if you want to change the compiler or its options after you already ran CMake, you need to clear its cache first, eg (using GNU find):: + + find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} + + CC=gcc CFLAGS='-fstack-protector-strong -Wa,--noexecstack' cmake . + In order to run the tests, enter:: make test @@ -75,7 +105,14 @@ We've included example programs for a lot of different features and uses in *pro Tests ===== -PolarSSL includes an elaborate test suite in *tests/* that initially requires Perl to generate the tests files (e.g. *test_suite_mpi.c*). These files are generates from a **function file** (e.g. *suites/test_suite_mpi.function*) and a **data file** (e.g. *suites/test_suite_mpi.data*). The **function file** contains the template for each test function. The **data file** contains the test cases, specified as parameters that should be pushed into a template function. +mbed TLS includes an elaborate test suite in *tests/* that initially requires Perl to generate the tests files (e.g. *test_suite_mpi.c*). These files are generates from a **function file** (e.g. *suites/test_suite_mpi.function*) and a **data file** (e.g. *suites/test_suite_mpi.data*). The **function file** contains the template for each test function. The **data file** contains the test cases, specified as parameters that should be pushed into a template function. + +For machines with a Unix shell and OpenSSL (and optionally GnuTLS) installed, additional test scripts are available: + +- *tests/ssl-opt.sh* runs integration tests for various TLS options (renegotiation, resumption, etc.) and tests interoperability of these options with other implementations. +- *tests/compat.sh* tests interoperability of every ciphersuite with other implementations. +- *tests/scripts/test-ref-configs.pl* test builds in various reduced configurations. +- *tests/scripts/all.sh* runs a combination of the above tests with various build options (eg ASan). Configurations ============== @@ -93,10 +130,10 @@ For larger contributions, e.g. a new feature, the code possible falls under copy Process ------- -#. `Check for open issues `_ or - `start a discussion `_ around a feature +#. `Check for open issues `_ or + `start a discussion `_ around a feature idea or a bug. -#. Fork the `PolarSSL repository on Github `_ +#. Fork the `mbed TLS repository on Github `_ to start making your changes. #. Write a test which shows that the bug was fixed or that the feature works as expected. diff --git a/ext/mbedtls/include/.gitignore b/ext/mbedtls/include/.gitignore new file mode 100644 index 0000000000..53a36d4463 --- /dev/null +++ b/ext/mbedtls/include/.gitignore @@ -0,0 +1,4 @@ +Makefile +*.sln +*.vcxproj +polarssl/check_config diff --git a/ext/polarssl/include/CMakeLists.txt b/ext/mbedtls/include/CMakeLists.txt similarity index 57% rename from ext/polarssl/include/CMakeLists.txt rename to ext/mbedtls/include/CMakeLists.txt index 8468871dc5..2c568584f3 100644 --- a/ext/polarssl/include/CMakeLists.txt +++ b/ext/mbedtls/include/CMakeLists.txt @@ -1,6 +1,6 @@ -option(INSTALL_POLARSSL_HEADERS "Install PolarSSL headers." ON) +option(INSTALL_MBEDTLS_HEADERS "Install mbed TLS headers." ON) -if(INSTALL_POLARSSL_HEADERS) +if(INSTALL_MBEDTLS_HEADERS) file(GLOB headers "polarssl/*.h") @@ -8,4 +8,4 @@ install(FILES ${headers} DESTINATION include/polarssl PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) -endif(INSTALL_POLARSSL_HEADERS) +endif(INSTALL_MBEDTLS_HEADERS) diff --git a/ext/polarssl/include/polarssl/aes.h b/ext/mbedtls/include/polarssl/aes.h similarity index 82% rename from ext/polarssl/include/polarssl/aes.h rename to ext/mbedtls/include/polarssl/aes.h index 2e9092f95c..8bcc039362 100644 --- a/ext/polarssl/include/polarssl/aes.h +++ b/ext/mbedtls/include/polarssl/aes.h @@ -3,12 +3,9 @@ * * \brief AES block cipher * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include @@ -132,6 +129,14 @@ int aes_crypt_ecb( aes_context *ctx, * Length should be a multiple of the block * size (16 bytes) * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx AES context * \param mode AES_ENCRYPT or AES_DECRYPT * \param length length of the input data @@ -157,6 +162,14 @@ int aes_crypt_cbc( aes_context *ctx, * both encryption and decryption. So a context initialized with * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx AES context * \param mode AES_ENCRYPT or AES_DECRYPT * \param length length of the input data @@ -182,6 +195,14 @@ int aes_crypt_cfb128( aes_context *ctx, * both encryption and decryption. So a context initialized with * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx AES context * \param mode AES_ENCRYPT or AES_DECRYPT * \param length length of the input data diff --git a/ext/polarssl/include/polarssl/aesni.h b/ext/mbedtls/include/polarssl/aesni.h similarity index 94% rename from ext/polarssl/include/polarssl/aesni.h rename to ext/mbedtls/include/polarssl/aesni.h index 92b23cd6b9..02419eda5a 100644 --- a/ext/polarssl/include/polarssl/aesni.h +++ b/ext/mbedtls/include/polarssl/aesni.h @@ -3,12 +3,9 @@ * * \brief AES-NI for hardware AES acceleration on some Intel processors * - * Copyright (C) 2013, Brainspark B.V. + * Copyright (C) 2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,6 +37,10 @@ #if defined(POLARSSL_HAVE_X86_64) +#ifdef __cplusplus +extern "C" { +#endif + /** * \brief AES-NI features detection routine * @@ -102,6 +103,10 @@ int aesni_setkey_enc( unsigned char *rk, const unsigned char *key, size_t bits ); +#ifdef __cplusplus +} +#endif + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_H */ diff --git a/ext/polarssl/include/polarssl/arc4.h b/ext/mbedtls/include/polarssl/arc4.h similarity index 92% rename from ext/polarssl/include/polarssl/arc4.h rename to ext/mbedtls/include/polarssl/arc4.h index 555f54fab3..9de0577099 100644 --- a/ext/polarssl/include/polarssl/arc4.h +++ b/ext/mbedtls/include/polarssl/arc4.h @@ -3,12 +3,9 @@ * * \brief The ARCFOUR stream cipher * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if !defined(POLARSSL_ARC4_ALT) // Regular implementation diff --git a/ext/polarssl/include/polarssl/asn1.h b/ext/mbedtls/include/polarssl/asn1.h similarity index 98% rename from ext/polarssl/include/polarssl/asn1.h rename to ext/mbedtls/include/polarssl/asn1.h index eacdd082bd..0bfa929348 100644 --- a/ext/polarssl/include/polarssl/asn1.h +++ b/ext/mbedtls/include/polarssl/asn1.h @@ -3,12 +3,9 @@ * * \brief Generic ASN.1 parsing * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,12 +30,12 @@ #include POLARSSL_CONFIG_FILE #endif +#include + #if defined(POLARSSL_BIGNUM_C) #include "bignum.h" #endif -#include - /** * \addtogroup asn1_module * \{ @@ -158,6 +155,7 @@ typedef struct _asn1_named_data asn1_buf oid; /**< The object identifier. */ asn1_buf val; /**< The named value. */ struct _asn1_named_data *next; /**< The next entry in the sequence. */ + unsigned char next_merged; /**< Merge next item into the current one? */ } asn1_named_data; diff --git a/ext/polarssl/include/polarssl/asn1write.h b/ext/mbedtls/include/polarssl/asn1write.h similarity index 97% rename from ext/polarssl/include/polarssl/asn1write.h rename to ext/mbedtls/include/polarssl/asn1write.h index 7a7fbf7b3f..154a6ba201 100644 --- a/ext/polarssl/include/polarssl/asn1write.h +++ b/ext/mbedtls/include/polarssl/asn1write.h @@ -3,12 +3,9 @@ * * \brief ASN.1 buffer writing functionality * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/base64.h b/ext/mbedtls/include/polarssl/base64.h similarity index 92% rename from ext/polarssl/include/polarssl/base64.h rename to ext/mbedtls/include/polarssl/base64.h index d04149344e..0f1e8549b7 100644 --- a/ext/polarssl/include/polarssl/base64.h +++ b/ext/mbedtls/include/polarssl/base64.h @@ -3,12 +3,9 @@ * * \brief RFC 1521 base64 encoding/decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +24,7 @@ #ifndef POLARSSL_BASE64_H #define POLARSSL_BASE64_H -#include +#include #define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */ #define POLARSSL_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */ diff --git a/ext/polarssl/include/polarssl/bignum.h b/ext/mbedtls/include/polarssl/bignum.h similarity index 98% rename from ext/polarssl/include/polarssl/bignum.h rename to ext/mbedtls/include/polarssl/bignum.h index 992ed8e6f3..8e1687b976 100644 --- a/ext/polarssl/include/polarssl/bignum.h +++ b/ext/mbedtls/include/polarssl/bignum.h @@ -3,12 +3,9 @@ * * \brief Multi-precision integer library * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,15 +24,18 @@ #ifndef POLARSSL_BIGNUM_H #define POLARSSL_BIGNUM_H -#include -#include - #if !defined(POLARSSL_CONFIG_FILE) #include "config.h" #else #include POLARSSL_CONFIG_FILE #endif +#include + +#if defined(POLARSSL_FS_IO) +#include +#endif + #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include #if (_MSC_VER <= 1200) @@ -148,7 +148,7 @@ typedef uint32_t t_udbl; defined(__ppc64__) || defined(__powerpc64__) || \ defined(__ia64__) || defined(__alpha__) || \ (defined(__sparc__) && defined(__arch64__)) || \ - defined(__s390x__) ) ) + defined(__s390x__) || defined(__mips64) ) ) #define POLARSSL_HAVE_INT64 typedef int64_t t_sint; typedef uint64_t t_uint; @@ -188,7 +188,9 @@ typedef struct mpi; /** - * \brief Initialize one MPI + * \brief Initialize one MPI (make internal references valid) + * This just makes it ready to be set or freed, + * but does not define a value for the MPI. * * \param X One MPI to initialize. */ diff --git a/ext/polarssl/include/polarssl/blowfish.h b/ext/mbedtls/include/polarssl/blowfish.h similarity index 84% rename from ext/polarssl/include/polarssl/blowfish.h rename to ext/mbedtls/include/polarssl/blowfish.h index c652b463d1..21ba7d463f 100644 --- a/ext/polarssl/include/polarssl/blowfish.h +++ b/ext/mbedtls/include/polarssl/blowfish.h @@ -3,12 +3,9 @@ * * \brief Blowfish block cipher * - * Copyright (C) 2012-2014, Brainspark B.V. + * Copyright (C) 2012-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include @@ -117,6 +114,14 @@ int blowfish_crypt_ecb( blowfish_context *ctx, * Length should be a multiple of the block * size (8 bytes) * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx Blowfish context * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT * \param length length of the input data @@ -139,6 +144,14 @@ int blowfish_crypt_cbc( blowfish_context *ctx, /** * \brief Blowfish CFB buffer encryption/decryption. * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx Blowfish context * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT * \param length length of the input data diff --git a/ext/polarssl/include/polarssl/bn_mul.h b/ext/mbedtls/include/polarssl/bn_mul.h similarity index 99% rename from ext/polarssl/include/polarssl/bn_mul.h rename to ext/mbedtls/include/polarssl/bn_mul.h index a8f0d22a01..b2eb1e8c7f 100644 --- a/ext/polarssl/include/polarssl/bn_mul.h +++ b/ext/mbedtls/include/polarssl/bn_mul.h @@ -3,12 +3,9 @@ * * \brief Multi-precision integer library * - * Copyright (C) 2006-2010, Brainspark B.V. + * Copyright (C) 2006-2010, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,7 +42,9 @@ #if defined(POLARSSL_HAVE_ASM) -#if defined(__GNUC__) +/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ +#if defined(__GNUC__) && \ + ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 ) #if defined(__i386__) #define MULADDC_INIT \ @@ -683,7 +682,7 @@ ); #endif /* Alpha */ -#if defined(__mips__) && !defined(__mips64__) +#if defined(__mips__) && !defined(__mips64) #define MULADDC_INIT \ asm( \ diff --git a/ext/polarssl/include/polarssl/camellia.h b/ext/mbedtls/include/polarssl/camellia.h similarity index 86% rename from ext/polarssl/include/polarssl/camellia.h rename to ext/mbedtls/include/polarssl/camellia.h index 8488d1df83..03218d18e0 100644 --- a/ext/polarssl/include/polarssl/camellia.h +++ b/ext/mbedtls/include/polarssl/camellia.h @@ -3,12 +3,9 @@ * * \brief Camellia block cipher * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include @@ -125,6 +122,14 @@ int camellia_crypt_ecb( camellia_context *ctx, * Length should be a multiple of the block * size (16 bytes) * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx CAMELLIA context * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT * \param length length of the input data @@ -151,6 +156,14 @@ int camellia_crypt_cbc( camellia_context *ctx, * both encryption and decryption. So a context initialized with * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx CAMELLIA context * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT * \param length length of the input data diff --git a/ext/polarssl/include/polarssl/ccm.h b/ext/mbedtls/include/polarssl/ccm.h similarity index 95% rename from ext/polarssl/include/polarssl/ccm.h rename to ext/mbedtls/include/polarssl/ccm.h index 439152f9f0..f1f24bda05 100644 --- a/ext/polarssl/include/polarssl/ccm.h +++ b/ext/mbedtls/include/polarssl/ccm.h @@ -3,12 +3,9 @@ * * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers * - * Copyright (C) 2014, Brainspark B.V. + * Copyright (C) 2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/certs.h b/ext/mbedtls/include/polarssl/certs.h similarity index 90% rename from ext/polarssl/include/polarssl/certs.h rename to ext/mbedtls/include/polarssl/certs.h index ba7c028afa..d2216602f6 100644 --- a/ext/polarssl/include/polarssl/certs.h +++ b/ext/mbedtls/include/polarssl/certs.h @@ -3,12 +3,9 @@ * * \brief Sample certificates and DHM parameters for testing * - * Copyright (C) 2006-2010, Brainspark B.V. + * Copyright (C) 2006-2010, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/check_config.h b/ext/mbedtls/include/polarssl/check_config.h similarity index 60% rename from ext/polarssl/include/polarssl/check_config.h rename to ext/mbedtls/include/polarssl/check_config.h index 328b881ea3..bd126a68b6 100644 --- a/ext/polarssl/include/polarssl/check_config.h +++ b/ext/mbedtls/include/polarssl/check_config.h @@ -3,12 +3,9 @@ * * \brief Consistency checks for configuration options * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +30,65 @@ #ifndef POLARSSL_CHECK_CONFIG_H #define POLARSSL_CHECK_CONFIG_H +#if defined(POLARSSL_DEPRECATED_WARNING) && \ + !defined(__GNUC__) && !defined(__clang__) +#error "POLARSSL_DEPRECATED_WARNING only works with GCC and Clang" +#endif + +#if defined(POLARSSL_NET_C) && !defined(POLARSSL_HAVE_IPV6) +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "Using POLARSSL_NET_C without POLARSSL_HAVE_IPV6 is deprecated" +#endif +#if defined(POLARSSL_DEPRECATED_REMOVED) +#define POLARSSL_HAVE_IPV6 +#endif +#endif /* POLARSSL_NET_C && !POLARSSL_HAVE_IPV6 */ + +#if defined(POLARSSL_ERROR_STRERROR_BC) +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "POLARSSL_ERROR_STRERROR_BC is deprecated" +#endif +#if defined(POLARSSL_DEPRECATED_REMOVED) +#error "POLARSSL_ERROR_STRERROR_BC is deprecated" +#endif +#endif /* POLARSSL_ERROR_STRERROR_BC */ + +#if defined(POLARSSL_MEMORY_C) +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "POLARSSL_MEMORY_C is deprecated" +#endif +#if defined(POLARSSL_DEPRECATED_REMOVED) +#error "POLARSSL_MEMORY_C is deprecated" +#endif +#endif /* POLARSSL_MEMORY_C */ + +#if defined(POLARSSL_PBKDF2_C) +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "POLARSSL_PBKDF2_C is deprecated" +#endif +#if defined(POLARSSL_DEPRECATED_REMOVED) +#error "POLARSSL_PBKDF2_C is deprecated" +#endif +#endif /* POLARSSL_PBKDF2_C */ + +#if defined(POLARSSL_HAVE_INT8) +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "POLARSSL_HAVE_INT8 is deprecated" +#endif +#if defined(POLARSSL_DEPRECATED_REMOVED) +#error "POLARSSL_HAVE_INT8 is deprecated" +#endif +#endif /* POLARSSL_HAVE_INT8 */ + +#if defined(POLARSSL_HAVE_INT16) +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "POLARSSL_HAVE_INT16 is deprecated" +#endif +#if defined(POLARSSL_DEPRECATED_REMOVED) +#error "POLARSSL_HAVE_INT16 is deprecated" +#endif +#endif /* POLARSSL_HAVE_INT16 */ + #if defined(POLARSSL_AESNI_C) && !defined(POLARSSL_HAVE_ASM) #error "POLARSSL_AESNI_C defined, but not all prerequisites" #endif @@ -148,17 +204,21 @@ #endif #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \ - ( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) ||\ + ( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \ !defined(POLARSSL_PKCS1_V15) ) #error "POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites" #endif #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \ - ( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) ||\ + ( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \ !defined(POLARSSL_PKCS1_V15) ) #error "POLARSSL_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites" #endif +#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_MEMORY_C defined, but not all prerequisites" +#endif + #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && \ ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) #error "POLARSSL_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites" @@ -180,6 +240,11 @@ #error "POLARSSL_PEM_WRITE_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_PK_C) && \ + ( !defined(POLARSSL_RSA_C) && !defined(POLARSSL_ECP_C) ) +#error "POLARSSL_PK_C defined, but not all prerequisites" +#endif + #if defined(POLARSSL_PK_PARSE_C) && !defined(POLARSSL_PK_C) #error "POLARSSL_PK_PARSE_C defined, but not all prerequisites" #endif @@ -192,6 +257,136 @@ #error "POLARSSL_PKCS11_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_PLATFORM_EXIT_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_EXIT_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_EXIT_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_EXIT) ||\ + defined(POLARSSL_PLATFORM_EXIT_ALT) ) +#error "POLARSSL_PLATFORM_EXIT_MACRO and POLARSSL_PLATFORM_STD_EXIT/POLARSSL_PLATFORM_EXIT_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_FPRINTF_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_FPRINTF) ||\ + defined(POLARSSL_PLATFORM_FPRINTF_ALT) ) +#error "POLARSSL_PLATFORM_FPRINTF_MACRO and POLARSSL_PLATFORM_STD_FPRINTF/POLARSSL_PLATFORM_FPRINTF_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) +#error "POLARSSL_PLATFORM_FREE_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\ + defined(POLARSSL_PLATFORM_STD_FREE) +#error "POLARSSL_PLATFORM_FREE_MACRO and POLARSSL_PLATFORM_STD_FREE cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_FREE_MACRO) && !defined(POLARSSL_PLATFORM_MALLOC_MACRO) +#error "POLARSSL_PLATFORM_MALLOC_MACRO must be defined if POLARSSL_PLATFORM_FREE_MACRO is" +#endif + +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) +#error "POLARSSL_PLATFORM_MALLOC_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\ + defined(POLARSSL_PLATFORM_STD_MALLOC) +#error "POLARSSL_PLATFORM_MALLOC_MACRO and POLARSSL_PLATFORM_STD_MALLOC cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) && !defined(POLARSSL_PLATFORM_FREE_MACRO) +#error "POLARSSL_PLATFORM_FREE_MACRO must be defined if POLARSSL_PLATFORM_MALLOC_MACRO is" +#endif + +#if defined(POLARSSL_PLATFORM_MEMORY) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_MEMORY defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_PRINTF_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_PRINTF_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_PRINTF) ||\ + defined(POLARSSL_PLATFORM_PRINTF_ALT) ) +#error "POLARSSL_PLATFORM_PRINTF_MACRO and POLARSSL_PLATFORM_STD_PRINTF/POLARSSL_PLATFORM_PRINTF_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && ( defined(_WIN32)\ + && !defined(EFIX64) && !defined(EFI32) ) +#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_SNPRINTF) ||\ + defined(POLARSSL_PLATFORM_SNPRINTF_ALT) ) +#error "POLARSSL_PLATFORM_SNPRINTF_MACRO and POLARSSL_PLATFORM_STD_SNPRINTF/POLARSSL_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_STD_MEM_HDR) &&\ + !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS) +#error "POLARSSL_PLATFORM_STD_MEM_HDR defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY) +#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY) +#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_FREE) && !defined(POLARSSL_PLATFORM_MEMORY) +#error "POLARSSL_PLATFORM_STD_FREE defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_EXIT) &&\ + !defined(POLARSSL_PLATFORM_EXIT_ALT) +#error "POLARSSL_PLATFORM_STD_EXIT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_FPRINTF) &&\ + !defined(POLARSSL_PLATFORM_FPRINTF_ALT) +#error "POLARSSL_PLATFORM_STD_FPRINTF defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_PRINTF) &&\ + !defined(POLARSSL_PLATFORM_PRINTF_ALT) +#error "POLARSSL_PLATFORM_STD_PRINTF defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_SNPRINTF) &&\ + !defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +#error "POLARSSL_PLATFORM_STD_SNPRINTF defined, but not all prerequisites" +#endif + #if defined(POLARSSL_RSA_C) && ( !defined(POLARSSL_BIGNUM_C) || \ !defined(POLARSSL_OID_C) ) #error "POLARSSL_RSA_C defined, but not all prerequisites" @@ -257,12 +452,31 @@ #error "Illegal protocol selection" #endif +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) && \ + !defined(POLARSSL_SSL_PROTO_TLS1) && \ + !defined(POLARSSL_SSL_PROTO_TLS1_1) && \ + !defined(POLARSSL_SSL_PROTO_TLS1_2) +#error "POLARSSL_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites" +#endif + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) && \ + !defined(POLARSSL_SSL_PROTO_TLS1) && \ + !defined(POLARSSL_SSL_PROTO_TLS1_1) && \ + !defined(POLARSSL_SSL_PROTO_TLS1_2) +#error "POLARSSL_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites" +#endif + #if defined(POLARSSL_SSL_SESSION_TICKETS) && defined(POLARSSL_SSL_TLS_C) && \ ( !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) || \ !defined(POLARSSL_CIPHER_MODE_CBC) ) #error "POLARSSL_SSL_SESSION_TICKETS_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) && \ + !defined(POLARSSL_SSL_PROTO_SSL3) && !defined(POLARSSL_SSL_PROTO_TLS1) +#error "POLARSSL_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites" +#endif + #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && \ !defined(POLARSSL_X509_CRT_PARSE_C) #error "POLARSSL_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites" diff --git a/ext/polarssl/include/polarssl/cipher.h b/ext/mbedtls/include/polarssl/cipher.h similarity index 96% rename from ext/polarssl/include/polarssl/cipher.h rename to ext/mbedtls/include/polarssl/cipher.h index 087e590680..00c42c1d4d 100644 --- a/ext/polarssl/include/polarssl/cipher.h +++ b/ext/mbedtls/include/polarssl/cipher.h @@ -5,12 +5,9 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,6 +33,8 @@ #include POLARSSL_CONFIG_FILE #endif +#include + #if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C) #define POLARSSL_CIPHER_MODE_AEAD #endif @@ -44,7 +43,9 @@ #define POLARSSL_CIPHER_MODE_WITH_PADDING #endif -#include +#if defined(POLARSSL_ARC4_C) +#define POLARSSL_CIPHER_MODE_STREAM +#endif #if defined(_MSC_VER) && !defined(inline) #define inline _inline @@ -73,7 +74,7 @@ typedef enum { POLARSSL_CIPHER_ID_NULL, POLARSSL_CIPHER_ID_AES, POLARSSL_CIPHER_ID_DES, - POLARSSL_CIPHER_ID_3DES, + POLARSSL_CIPHER_ID_3DES, /* Unused! */ POLARSSL_CIPHER_ID_CAMELLIA, POLARSSL_CIPHER_ID_BLOWFISH, POLARSSL_CIPHER_ID_ARC4, @@ -185,24 +186,32 @@ typedef struct { int (*ecb_func)( void *ctx, operation_t mode, const unsigned char *input, unsigned char *output ); +#if defined(POLARSSL_CIPHER_MODE_CBC) /** Encrypt using CBC */ int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ); +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) /** Encrypt using CFB (Full length) */ int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ); +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) /** Encrypt using CTR */ int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, const unsigned char *input, unsigned char *output ); +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) /** Encrypt using STREAM */ int (*stream_func)( void *ctx, size_t length, const unsigned char *input, unsigned char *output ); +#endif /** Set key for encryption purposes */ int (*setkey_enc_func)( void *ctx, const unsigned char *key, @@ -265,9 +274,11 @@ typedef struct { /** Operation that the context's key has been initialised for */ operation_t operation; +#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) /** Padding functions to use, if relevant for cipher mode */ void (*add_padding)( unsigned char *output, size_t olen, size_t data_len ); int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len ); +#endif /** Buffer for data that hasn't been encrypted yet */ unsigned char unprocessed_data[POLARSSL_MAX_BLOCK_LENGTH]; @@ -361,17 +372,25 @@ void cipher_free( cipher_context_t *ctx ); */ int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ); +#if ! defined(POLARSSL_DEPRECATED_REMOVED) +#if defined(POLARSSL_DEPRECATED_WARNING) +#define DEPRECATED __attribute__((deprecated)) +#else +#define DEPRECATED +#endif /** * \brief Free the cipher-specific context of ctx. Freeing ctx * itself remains the responsibility of the caller. * - * \note Deprecated: Redirects to cipher_free() + * \deprecated Use cipher_free() instead * * \param ctx Free the cipher-specific context * * \returns 0 */ -int cipher_free_ctx( cipher_context_t *ctx ); +int cipher_free_ctx( cipher_context_t *ctx ) DEPRECATED; +#undef DEPRECATED +#endif /* POLARSSL_DEPRECATED_REMOVED */ /** * \brief Returns the block size of the given cipher. @@ -466,7 +485,7 @@ static inline const char *cipher_get_name( const cipher_context_t *ctx ) * POLARSSL_KEY_LENGTH_NONE if ctx has not been * initialised. */ -static inline int cipher_get_key_size ( const cipher_context_t *ctx ) +static inline int cipher_get_key_size( const cipher_context_t *ctx ) { if( NULL == ctx || NULL == ctx->cipher_info ) return POLARSSL_KEY_LENGTH_NONE; diff --git a/ext/polarssl/include/polarssl/cipher_wrap.h b/ext/mbedtls/include/polarssl/cipher_wrap.h similarity index 86% rename from ext/polarssl/include/polarssl/cipher_wrap.h rename to ext/mbedtls/include/polarssl/cipher_wrap.h index 46bc757a8e..3e685b13ee 100644 --- a/ext/polarssl/include/polarssl/cipher_wrap.h +++ b/ext/mbedtls/include/polarssl/cipher_wrap.h @@ -5,12 +5,9 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,6 +31,7 @@ #else #include POLARSSL_CONFIG_FILE #endif + #include "cipher.h" #ifdef __cplusplus diff --git a/ext/polarssl/include/polarssl/compat-1.2.h b/ext/mbedtls/include/polarssl/compat-1.2.h similarity index 97% rename from ext/polarssl/include/polarssl/compat-1.2.h rename to ext/mbedtls/include/polarssl/compat-1.2.h index 15b5aa1f2a..d694015e4b 100644 --- a/ext/polarssl/include/polarssl/compat-1.2.h +++ b/ext/mbedtls/include/polarssl/compat-1.2.h @@ -3,12 +3,11 @@ * * \brief Backwards compatibility header for PolarSSL-1.2 from PolarSSL-1.3 * - * Copyright (C) 2006-2013, Brainspark B.V. + * \deprecated Use native PolarSSL 1.3 functions instead. * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +23,12 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#if ! defined(POLARSSL_DEPRECATED_REMOVED) + +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "Including compat-1.2.h is deprecated" +#endif + #ifndef POLARSSL_COMPAT_1_2_H #define POLARSSL_COMPAT_1_2_H @@ -387,3 +392,4 @@ static inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_contex } #endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */ #endif /* compat-1.2.h */ +#endif /* POLARSSL_DEPRECATED_REMOVED */ diff --git a/ext/polarssl/include/polarssl/config.h b/ext/mbedtls/include/polarssl/config.h similarity index 86% rename from ext/polarssl/include/polarssl/config.h rename to ext/mbedtls/include/polarssl/config.h index 09a6ce0efe..422c8be95a 100644 --- a/ext/polarssl/include/polarssl/config.h +++ b/ext/mbedtls/include/polarssl/config.h @@ -3,12 +3,9 @@ * * \brief Configuration options (set of defines) * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,6 +44,10 @@ * * The system uses 8-bit wide native integers. * + * \deprecated The compiler should be able to generate code for 32-bit + * arithmetic (required by C89). This code is likely to be at least as + * efficient as ours. + * * Uncomment if native integers are 8-bit wide. */ //#define POLARSSL_HAVE_INT8 @@ -56,6 +57,10 @@ * * The system uses 16-bit wide native integers. * + * \deprecated The compiler should be able to generate code for 32-bit + * arithmetic (required by C89). This code is likely to be at least as + * efficient as ours. + * * Uncomment if native integers are 16-bit wide. */ //#define POLARSSL_HAVE_INT16 @@ -110,6 +115,9 @@ * * Note: on Windows/MingW, XP or higher is required. * + * \warning As of 1.3.11, *not* using this flag when POLARSSL_NET_C is + * defined, is deprecated. The alternative legacy code will be removed in 2.0. + * * Comment if your system does not support the IPv6 socket interface */ #define POLARSSL_HAVE_IPV6 @@ -119,12 +127,18 @@ * * Enable the memory allocation layer. * - * By default PolarSSL uses the system-provided malloc() and free(). + * By default mbed TLS uses the system-provided malloc() and free(). * This allows different allocators (self-implemented or provided) to be * provided to the platform abstraction layer. * - * Enabling POLARSSL_PLATFORM_MEMORY will provide "platform_set_malloc_free()" - * to allow you to set an alternative malloc() and free() function pointer. + * Enabling POLARSSL_PLATFORM_MEMORY without the + * POLARSSL_PLATFORM_{FREE,MALLOC}_MACROs will provide + * "platform_set_malloc_free()" allowing you to set an alternative malloc() and + * free() function pointer at runtime. + * + * Enabling POLARSSL_PLATFORM_MEMORY and specifying + * POLARSSL_PLATFORM_{MALLOC,FREE}_MACROs will allow you to specify the + * alternate function at compile time. * * Requires: POLARSSL_PLATFORM_C * @@ -141,7 +155,8 @@ * This makes sure there are no linking errors on platforms that do not support * these functions. You will HAVE to provide alternatives, either at runtime * via the platform_set_xxx() functions or at compile time by setting - * the POLARSSL_PLATFORM_STD_XXX defines. + * the POLARSSL_PLATFORM_STD_XXX defines, or enabling a + * POLARSSL_PLATFORM_XXX_MACRO. * * Requires: POLARSSL_PLATFORM_C * @@ -153,24 +168,60 @@ /** * \def POLARSSL_PLATFORM_XXX_ALT * - * Uncomment a macro to let PolarSSL support the function in the platform + * Uncomment a macro to let mbed TLS support the function in the platform * abstraction layer. * - * Example: In case you uncomment POLARSSL_PLATFORM_PRINTF_ALT, PolarSSL will + * Example: In case you uncomment POLARSSL_PLATFORM_PRINTF_ALT, mbed TLS will * provide a function "platform_set_printf()" that allows you to set an * alternative printf function pointer. * * All these define require POLARSSL_PLATFORM_C to be defined! * + * WARNING: POLARSSL_PLATFORM_SNPRINTF_ALT is not available on Windows + * for compatibility reasons. + * + * WARNING: POLARSSL_PLATFORM_XXX_ALT cannot be defined at the same time as + * POLARSSL_PLATFORM_XXX_MACRO! + * * Uncomment a macro to enable alternate implementation of specific base * platform function */ -//#define POLARSSL_PLATFORM_PRINTF_ALT +//#define POLARSSL_PLATFORM_EXIT_ALT //#define POLARSSL_PLATFORM_FPRINTF_ALT +//#define POLARSSL_PLATFORM_PRINTF_ALT +//#define POLARSSL_PLATFORM_SNPRINTF_ALT + +/** + * \def POLARSSL_DEPRECATED_WARNING + * + * Mark deprecated functions so that they generate a warning if used. + * Functions deprecated in one version will usually be removed in the next + * version. You can enable this to help you prepare the transition to a new + * major version by making sure your code is not using these functions. + * + * This only works with GCC and Clang. With other compilers, you may want to + * use POLARSSL_DEPRECATED_REMOVED + * + * Uncomment to get warnings on using deprecated functions. + */ +//#define POLARSSL_DEPRECATED_WARNING + +/** + * \def POLARSSL_DEPRECATED_REMOVED + * + * Remove deprecated functions so that they generate an error if used. + * Functions deprecated in one version will usually be removed in the next + * version. You can enable this to help you prepare the transition to a new + * major version by making sure your code is not using these functions. + * + * Uncomment to get errors on using deprecated functions. + */ +//#define POLARSSL_DEPRECATED_REMOVED + /* \} name SECTION: System support */ /** - * \name SECTION: PolarSSL feature support + * \name SECTION: mbed TLS feature support * * This section sets support for features that are or are not needed * within the modules that are enabled. @@ -193,12 +244,12 @@ /** * \def POLARSSL_XXX_ALT * - * Uncomment a macro to let PolarSSL use your alternate core implementation of + * Uncomment a macro to let mbed TLS use your alternate core implementation of * a symmetric or hash algorithm (e.g. platform specific assembly optimized * implementations). Keep in mind that the function prototypes should remain * the same. * - * Example: In case you uncomment POLARSSL_AES_ALT, PolarSSL will no longer + * Example: In case you uncomment POLARSSL_AES_ALT, mbed TLS will no longer * provide the "struct aes_context" definition and omit the base function * declarations and implementations. "aes_alt.h" will be included from * "aes.h" to include the new function definitions. @@ -226,10 +277,18 @@ * Store the AES tables in ROM. * * Uncomment this macro to store the AES tables in ROM. - * */ //#define POLARSSL_AES_ROM_TABLES +/** + * \def POLARSSL_CAMELLIA_SMALL_MEMORY + * + * Use less ROM for the Camellia implementation (saves about 768 bytes). + * + * Uncomment this macro to use less memory for Camellia. + */ +//#define POLARSSL_CAMELLIA_SMALL_MEMORY + /** * \def POLARSSL_CIPHER_MODE_CBC * @@ -638,11 +697,9 @@ * Make available the backward compatible error_strerror() next to the * current polarssl_strerror(). * - * For new code, it is recommended to use polarssl_strerror() instead and - * disable this. + * \deprecated Do not define this and use polarssl_strerror() instead * - * Disable if you run into name conflicts and want to really remove the - * error_strerror() + * Disable if you want to really remove the error_strerror() name */ #define POLARSSL_ERROR_STRERROR_BC @@ -781,11 +838,23 @@ */ #define POLARSSL_SELF_TEST +/** + * \def POLARSSL_SSL_AEAD_RANDOM_IV + * + * Generate a random IV rather than using the record sequence number as a + * nonce for ciphersuites using and AEAD algorithm (GCM or CCM). + * + * Using the sequence number is generally recommended. + * + * Uncomment this macro to always use random IVs with AEAD ciphersuites. + */ +//#define POLARSSL_SSL_AEAD_RANDOM_IV + /** * \def POLARSSL_SSL_ALL_ALERT_MESSAGES * * Enable sending of alert messages in case of encountered errors as per RFC. - * If you choose not to send the alert messages, PolarSSL can still communicate + * If you choose not to send the alert messages, mbed TLS can still communicate * with other servers, only debugging of failures is harder. * * The advantage of not sending alert messages, is that no information is given @@ -811,6 +880,59 @@ */ //#define POLARSSL_SSL_DEBUG_ALL +/** \def POLARSSL_SSL_ENCRYPT_THEN_MAC + * + * Enable support for Encrypt-then-MAC, RFC 7366. + * + * This allows peers that both support it to use a more robust protection for + * ciphersuites using CBC, providing deep resistance against timing attacks + * on the padding or underlying cipher. + * + * This only affects CBC ciphersuites, and is useless if none is defined. + * + * Requires: POLARSSL_SSL_PROTO_TLS1 or + * POLARSSL_SSL_PROTO_TLS1_1 or + * POLARSSL_SSL_PROTO_TLS1_2 + * + * Comment this macro to disable support for Encrypt-then-MAC + */ +#define POLARSSL_SSL_ENCRYPT_THEN_MAC + +/** \def POLARSSL_SSL_EXTENDED_MASTER_SECRET + * + * Enable support for Extended Master Secret, aka Session Hash + * (draft-ietf-tls-session-hash-02). + * + * This was introduced as "the proper fix" to the Triple Handshake familiy of + * attacks, but it is recommended to always use it (even if you disable + * renegotiation), since it actually fixes a more fundamental issue in the + * original SSL/TLS design, and has implications beyond Triple Handshake. + * + * Requires: POLARSSL_SSL_PROTO_TLS1 or + * POLARSSL_SSL_PROTO_TLS1_1 or + * POLARSSL_SSL_PROTO_TLS1_2 + * + * Comment this macro to disable support for Extended Master Secret. + */ +#define POLARSSL_SSL_EXTENDED_MASTER_SECRET + +/** + * \def POLARSSL_SSL_FALLBACK_SCSV + * + * Enable support for FALLBACK_SCSV (draft-ietf-tls-downgrade-scsv-00). + * + * For servers, it is recommended to always enable this, unless you support + * only one version of TLS, or know for sure that none of your clients + * implements a fallback strategy. + * + * For clients, you only need this if you're using a fallback strategy, which + * is not recommended in the first place, unless you absolutely need it to + * interoperate with buggy (version-intolerant) servers. + * + * Comment this macro to disable support for FALLBACK_SCSV + */ +#define POLARSSL_SSL_FALLBACK_SCSV + /** * \def POLARSSL_SSL_HW_RECORD_ACCEL * @@ -821,6 +943,36 @@ */ //#define POLARSSL_SSL_HW_RECORD_ACCEL +/** + * \def POLARSSL_SSL_CBC_RECORD_SPLITTING + * + * Enable 1/n-1 record splitting for CBC mode in SSLv3 and TLS 1.0. + * + * This is a countermeasure to the BEAST attack, which also minimizes the risk + * of interoperability issues compared to sending 0-length records. + * + * Comment this macro to disable 1/n-1 record splitting. + */ +#define POLARSSL_SSL_CBC_RECORD_SPLITTING + +/** + * \def POLARSSL_SSL_DISABLE_RENEGOTIATION + * + * Disable support for TLS renegotiation. + * + * The two main uses of renegotiation are (1) refresh keys on long-lived + * connections and (2) client authentication after the initial handshake. + * If you don't need renegotiation, it's probably better to disable it, since + * it has been associated with security issues in the past and is easy to + * misuse/misunderstand. + * + * Warning: in the next stable branch, this switch will be replaced by + * POLARSSL_SSL_RENEGOTIATION to enable support for renegotiation. + * + * Uncomment this to disable support for renegotiation. + */ +//#define POLARSSL_SSL_DISABLE_RENEGOTIATION + /** * \def POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO * @@ -901,8 +1053,7 @@ /** * \def POLARSSL_SSL_ALPN * - * Enable support for Application Layer Protocol Negotiation. - * draft-ietf-tls-applayerprotoneg-05 + * Enable support for RFC 7301 Application Layer Protocol Negotiation. * * Comment this macro to disable support for ALPN. */ @@ -926,6 +1077,8 @@ * * Enable support for RFC 6066 server name indication (SNI) in SSL. * + * Requires: POLARSSL_X509_CRT_PARSE_C + * * Comment this macro to disable support for server name indication in SSL */ #define POLARSSL_SSL_SERVER_NAME_INDICATION @@ -1064,12 +1217,12 @@ * Uncomment to enable use of ZLIB */ //#define POLARSSL_ZLIB_SUPPORT -/* \} name SECTION: PolarSSL feature support */ +/* \} name SECTION: mbed TLS feature support */ /** - * \name SECTION: PolarSSL modules + * \name SECTION: mbed TLS modules * - * This section enables or disables entire modules in PolarSSL + * This section enables or disables entire modules in mbed TLS * \{ */ @@ -1085,7 +1238,7 @@ * * This modules adds support for the AES-NI instructions on x86-64 */ -//#define POLARSSL_AESNI_C +#define POLARSSL_AESNI_C /** * \def POLARSSL_AES_C @@ -1587,7 +1740,10 @@ /** * \def POLARSSL_MEMORY_C - * Deprecated since 1.3.5. Please use POLARSSL_PLATFORM_MEMORY instead. + * + * \deprecated Use POLARSSL_PLATFORM_MEMORY instead. + * + * Depends on: POLARSSL_PLATFORM_C */ //#define POLARSSL_MEMORY_C @@ -1601,7 +1757,7 @@ * Module: library/memory_buffer_alloc.c * * Requires: POLARSSL_PLATFORM_C - * POLARSSL_PLATFORM_MEMORY (to use it within PolarSSL) + * POLARSSL_PLATFORM_MEMORY (to use it within mbed TLS) * * Enable this module to enable the buffer memory allocator. */ @@ -1612,6 +1768,9 @@ * * Enable the TCP/IP networking routines. * + * \warning As of 1.3.11, it is deprecated to enable this module without + * POLARSSL_HAVE_IPV6. The alternative legacy code will be removed in 2.0. + * * Module: library/net.c * * This module provides TCP/IP networking routines. @@ -1653,13 +1812,14 @@ * * This modules adds support for the VIA PadLock on x86. */ -//#define POLARSSL_PADLOCK_C +#define POLARSSL_PADLOCK_C /** * \def POLARSSL_PBKDF2_C * * Enable PKCS#5 PBKDF2 key derivation function. - * DEPRECATED: Use POLARSSL_PKCS5_C instead + * + * \deprecated Use POLARSSL_PKCS5_C instead * * Module: library/pbkdf2.c * @@ -1796,7 +1956,11 @@ * \def POLARSSL_PLATFORM_C * * Enable the platform abstraction layer that allows you to re-assign - * functions like malloc(), free(), printf(), fprintf() + * functions like malloc(), free(), snprintf(), printf(), fprintf(), exit() + * + * Enabling POLARSSL_PLATFORM_C enables to use of POLARSSL_PLATFORM_XXX_ALT + * or POLARSSL_PLATFORM_XXX_MACRO directives, allowing the functions mentioned + * above to be specified at runtime or compile time respectively. * * Module: library/platform.c * Caller: Most other .c files @@ -1944,7 +2108,7 @@ * \def POLARSSL_THREADING_C * * Enable the threading abstraction layer. - * By default PolarSSL assumes it is used in a non-threaded environment or that + * By default mbed TLS assumes it is used in a non-threaded environment or that * contexts are not shared between threads. If you do intend to use contexts * between threads, you will need to enable this layer to prevent race * conditions. @@ -1957,7 +2121,7 @@ * You will have to enable either POLARSSL_THREADING_ALT or * POLARSSL_THREADING_PTHREAD. * - * Enable this layer to allow use of mutexes within PolarSSL + * Enable this layer to allow use of mutexes within mbed TLS */ //#define POLARSSL_THREADING_C @@ -2094,7 +2258,7 @@ */ #define POLARSSL_XTEA_C -/* \} name SECTION: PolarSSL modules */ +/* \} name SECTION: mbed TLS modules */ /** * \name SECTION: Module configuration options @@ -2141,11 +2305,22 @@ //#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ /* Platform options */ -//#define POLARSSL_PLATFORM_STD_MEM_HDR /**< Header to include if POLARSSL_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ -//#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ -//#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ -//#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ -//#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_MEM_HDR /**< Header to include if POLARSSL_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ +//#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */ + +/* To Use Function Macros POLARSSL_PLATFORM_C must be enabled */ +/* POLARSSL_PLATFORM_XXX_MACRO and POLARSSL_PLATFORM_XXX_ALT cannot both be defined */ +//#define POLARSSL_PLATFORM_MALLOC_MACRO malloc /**< Default allocator macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_FREE_MACRO free /**< Default free macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_PRINTF_MACRO printf /**< Default printf macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_SNPRINTF_MACRO snprintf /**< Default snprintf macro to use, can be undefined */ /* SSL Cache options */ //#define SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ @@ -2153,6 +2328,7 @@ /* SSL options */ //#define SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */ +//#define SSL_MIN_DHM_BYTES 128 /**< Min size of the Diffie-Hellman prime */ //#define SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */ //#define POLARSSL_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */ @@ -2173,6 +2349,9 @@ /* Debug options */ //#define POLARSSL_DEBUG_DFL_MODE POLARSSL_DEBUG_LOG_FULL /**< Default log: Full or Raw */ +/* X509 options */ +//#define POLARSSL_X509_MAX_INTERMEDIATE_CA 8 /**< Maximum number of intermediate CAs in a verification chain. */ + /* \} name SECTION: Module configuration options */ #include "check_config.h" diff --git a/ext/polarssl/include/polarssl/ctr_drbg.h b/ext/mbedtls/include/polarssl/ctr_drbg.h similarity index 96% rename from ext/polarssl/include/polarssl/ctr_drbg.h rename to ext/mbedtls/include/polarssl/ctr_drbg.h index bebbfe9311..ebab55f29f 100644 --- a/ext/polarssl/include/polarssl/ctr_drbg.h +++ b/ext/mbedtls/include/polarssl/ctr_drbg.h @@ -3,12 +3,9 @@ * * \brief CTR_DRBG based on AES-256 (NIST SP 800-90) * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,8 +24,6 @@ #ifndef POLARSSL_CTR_DRBG_H #define POLARSSL_CTR_DRBG_H -#include - #include "aes.h" #define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */ @@ -188,6 +183,10 @@ int ctr_drbg_reseed( ctr_drbg_context *ctx, * \param ctx CTR_DRBG context * \param additional Additional data to update state with * \param add_len Length of additional data + * + * \note If add_len is greater than CTR_DRBG_MAX_SEED_INPUT, + * only the first CTR_DRBG_MAX_SEED_INPUT bytes are used, + * the remaining ones are silently discarded. */ void ctr_drbg_update( ctr_drbg_context *ctx, const unsigned char *additional, size_t add_len ); diff --git a/ext/polarssl/include/polarssl/debug.h b/ext/mbedtls/include/polarssl/debug.h similarity index 93% rename from ext/polarssl/include/polarssl/debug.h rename to ext/mbedtls/include/polarssl/debug.h index 0dd79d52f0..2dd89a2276 100644 --- a/ext/polarssl/include/polarssl/debug.h +++ b/ext/mbedtls/include/polarssl/debug.h @@ -3,12 +3,9 @@ * * \brief Debug functions * - * Copyright (C) 2006-2011, Brainspark B.V. + * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,7 +29,9 @@ #else #include POLARSSL_CONFIG_FILE #endif + #include "ssl.h" + #if defined(POLARSSL_ECP_C) #include "ecp.h" #endif @@ -58,7 +57,7 @@ #define SSL_DEBUG_MSG( level, args ) \ - debug_print_msg( ssl, level, __FILE__, __LINE__, debug_fmt args ); + debug_print_msg_free( ssl, level, __FILE__, __LINE__, debug_fmt args ); #define SSL_DEBUG_RET( level, text, ret ) \ debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret ); @@ -116,6 +115,9 @@ void debug_set_threshold( int threshold ); char *debug_fmt( const char *format, ... ); +void debug_print_msg_free( const ssl_context *ssl, int level, + const char *file, int line, char *text ); + void debug_print_msg( const ssl_context *ssl, int level, const char *file, int line, const char *text ); diff --git a/ext/polarssl/include/polarssl/des.h b/ext/mbedtls/include/polarssl/des.h similarity index 86% rename from ext/polarssl/include/polarssl/des.h rename to ext/mbedtls/include/polarssl/des.h index 89bb394e01..3f7236c1ca 100644 --- a/ext/polarssl/include/polarssl/des.h +++ b/ext/mbedtls/include/polarssl/des.h @@ -3,12 +3,9 @@ * * \brief DES block cipher * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include @@ -217,6 +214,14 @@ int des_crypt_ecb( des_context *ctx, /** * \brief DES-CBC buffer encryption/decryption * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx DES context * \param mode DES_ENCRYPT or DES_DECRYPT * \param length length of the input data @@ -249,6 +254,14 @@ int des3_crypt_ecb( des3_context *ctx, /** * \brief 3DES-CBC buffer encryption/decryption * + * \note Upon exit, the content of the IV is updated so that you can + * call the function same function again on the following + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If on the other hand you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * * \param ctx 3DES context * \param mode DES_ENCRYPT or DES_DECRYPT * \param length length of the input data diff --git a/ext/polarssl/include/polarssl/dhm.h b/ext/mbedtls/include/polarssl/dhm.h similarity index 98% rename from ext/polarssl/include/polarssl/dhm.h rename to ext/mbedtls/include/polarssl/dhm.h index 064472f358..8d64a5f913 100644 --- a/ext/polarssl/include/polarssl/dhm.h +++ b/ext/mbedtls/include/polarssl/dhm.h @@ -3,12 +3,9 @@ * * \brief Diffie-Hellman-Merkle key exchange * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/ecdh.h b/ext/mbedtls/include/polarssl/ecdh.h similarity index 97% rename from ext/polarssl/include/polarssl/ecdh.h rename to ext/mbedtls/include/polarssl/ecdh.h index 525cade982..60f913d98c 100644 --- a/ext/polarssl/include/polarssl/ecdh.h +++ b/ext/mbedtls/include/polarssl/ecdh.h @@ -3,12 +3,9 @@ * * \brief Elliptic curve Diffie-Hellman * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/ecdsa.h b/ext/mbedtls/include/polarssl/ecdsa.h similarity index 96% rename from ext/polarssl/include/polarssl/ecdsa.h rename to ext/mbedtls/include/polarssl/ecdsa.h index d99a17a289..5eb83d9d27 100644 --- a/ext/polarssl/include/polarssl/ecdsa.h +++ b/ext/mbedtls/include/polarssl/ecdsa.h @@ -3,12 +3,9 @@ * * \brief Elliptic curve DSA * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,10 +25,7 @@ #define POLARSSL_ECDSA_H #include "ecp.h" - -#if defined(POLARSSL_ECDSA_DETERMINISTIC) #include "md.h" -#endif /** * \brief ECDSA context structure diff --git a/ext/polarssl/include/polarssl/ecp.h b/ext/mbedtls/include/polarssl/ecp.h similarity index 96% rename from ext/polarssl/include/polarssl/ecp.h rename to ext/mbedtls/include/polarssl/ecp.h index 7192f1e6c3..094a8407cb 100644 --- a/ext/polarssl/include/polarssl/ecp.h +++ b/ext/mbedtls/include/polarssl/ecp.h @@ -3,12 +3,9 @@ * * \brief Elliptic curves over GF(p) * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -413,7 +410,9 @@ int ecp_point_read_binary( const ecp_group *grp, ecp_point *P, * \param buf $(Start of input buffer) * \param len Buffer length * - * \return O if successful, + * \note buf is updated to point right after the ECPoint on exit + * + * \return 0 if successful, * POLARSSL_ERR_MPI_XXX if initialization failed * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid */ @@ -463,7 +462,7 @@ int ecp_group_read_string( ecp_group *grp, int radix, * \param grp Destination group * \param index Index in the list of well-known domain parameters * - * \return O if successful, + * \return 0 if successful, * POLARSSL_ERR_MPI_XXX if initialization failed * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups * @@ -479,7 +478,9 @@ int ecp_use_known_dp( ecp_group *grp, ecp_group_id index ); * \param buf &(Start of input buffer) * \param len Buffer length * - * \return O if successful, + * \note buf is updated to point right after ECParameters on exit + * + * \return 0 if successful, * POLARSSL_ERR_MPI_XXX if initialization failed * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid */ @@ -635,6 +636,18 @@ int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q, int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); +/** + * \brief Check a public-private key pair + * + * \param pub Keypair structure holding a public key + * \param prv Keypair structure holding a private (plus public) key + * + * \return 0 if successful (keys are valid and match), or + * POLARSSL_ERR_ECP_BAD_INPUT_DATA, or + * a POLARSSL_ERR_ECP_XXX or POLARSSL_ERR_MPI_XXX code. + */ +int ecp_check_pub_priv( const ecp_keypair *pub, const ecp_keypair *prv ); + #if defined(POLARSSL_SELF_TEST) /** * \brief Checkup routine diff --git a/ext/polarssl/include/polarssl/entropy.h b/ext/mbedtls/include/polarssl/entropy.h similarity index 97% rename from ext/polarssl/include/polarssl/entropy.h rename to ext/mbedtls/include/polarssl/entropy.h index f5fa92808f..aa23a0372c 100644 --- a/ext/polarssl/include/polarssl/entropy.h +++ b/ext/mbedtls/include/polarssl/entropy.h @@ -3,12 +3,9 @@ * * \brief Entropy accumulator implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,14 +24,14 @@ #ifndef POLARSSL_ENTROPY_H #define POLARSSL_ENTROPY_H -#include - #if !defined(POLARSSL_CONFIG_FILE) #include "config.h" #else #include POLARSSL_CONFIG_FILE #endif +#include + #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256) #include "sha512.h" #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR diff --git a/ext/polarssl/include/polarssl/entropy_poll.h b/ext/mbedtls/include/polarssl/entropy_poll.h similarity index 90% rename from ext/polarssl/include/polarssl/entropy_poll.h rename to ext/mbedtls/include/polarssl/entropy_poll.h index 92efa0045b..94b5a6eff9 100644 --- a/ext/polarssl/include/polarssl/entropy_poll.h +++ b/ext/mbedtls/include/polarssl/entropy_poll.h @@ -3,12 +3,9 @@ * * \brief Platform-specific and custom entropy polling functions * - * Copyright (C) 2006-2011, Brainspark B.V. + * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,14 +24,14 @@ #ifndef POLARSSL_ENTROPY_POLL_H #define POLARSSL_ENTROPY_POLL_H -#include - #if !defined(POLARSSL_CONFIG_FILE) #include "config.h" #else #include POLARSSL_CONFIG_FILE #endif +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/ext/polarssl/include/polarssl/error.h b/ext/mbedtls/include/polarssl/error.h similarity index 91% rename from ext/polarssl/include/polarssl/error.h rename to ext/mbedtls/include/polarssl/error.h index 7ce2828b27..3041155456 100644 --- a/ext/polarssl/include/polarssl/error.h +++ b/ext/mbedtls/include/polarssl/error.h @@ -3,12 +3,9 @@ * * \brief Error to string translation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +24,7 @@ #ifndef POLARSSL_ERROR_H #define POLARSSL_ERROR_H -#include +#include /** * Error code layout. @@ -91,7 +88,7 @@ * ECP 4 8 (Started from top) * MD 5 4 * CIPHER 6 6 - * SSL 6 10 (Started from top) + * SSL 6 11 (Started from top) * SSL 7 31 * * Module dependent error code (5 bits 0x.00.-0x.F8.) @@ -102,7 +99,7 @@ extern "C" { #endif /** - * \brief Translate a PolarSSL error code into a string representation, + * \brief Translate a mbed TLS error code into a string representation, * Result is truncated if necessary and always includes a terminating * null byte. * diff --git a/ext/polarssl/include/polarssl/gcm.h b/ext/mbedtls/include/polarssl/gcm.h similarity index 97% rename from ext/polarssl/include/polarssl/gcm.h rename to ext/mbedtls/include/polarssl/gcm.h index c2829a0092..5a9472298e 100644 --- a/ext/polarssl/include/polarssl/gcm.h +++ b/ext/mbedtls/include/polarssl/gcm.h @@ -3,12 +3,9 @@ * * \brief Galois/Counter mode for 128-bit block ciphers * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/havege.h b/ext/mbedtls/include/polarssl/havege.h similarity index 88% rename from ext/polarssl/include/polarssl/havege.h rename to ext/mbedtls/include/polarssl/havege.h index 536eb08822..825a1daab9 100644 --- a/ext/polarssl/include/polarssl/havege.h +++ b/ext/mbedtls/include/polarssl/havege.h @@ -3,12 +3,9 @@ * * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +24,7 @@ #ifndef POLARSSL_HAVEGE_H #define POLARSSL_HAVEGE_H -#include +#include #define COLLECT_SIZE 1024 diff --git a/ext/polarssl/include/polarssl/hmac_drbg.h b/ext/mbedtls/include/polarssl/hmac_drbg.h similarity index 98% rename from ext/polarssl/include/polarssl/hmac_drbg.h rename to ext/mbedtls/include/polarssl/hmac_drbg.h index 2d765d5193..0fe7b0311b 100644 --- a/ext/polarssl/include/polarssl/hmac_drbg.h +++ b/ext/mbedtls/include/polarssl/hmac_drbg.h @@ -3,12 +3,9 @@ * * \brief HMAC_DRBG (NIST SP 800-90A) * - * Copyright (C) 2014, Brainspark B.V. + * Copyright (C) 2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/md.h b/ext/mbedtls/include/polarssl/md.h similarity index 96% rename from ext/polarssl/include/polarssl/md.h rename to ext/mbedtls/include/polarssl/md.h index 81d8a2e5c2..303aee8209 100644 --- a/ext/polarssl/include/polarssl/md.h +++ b/ext/mbedtls/include/polarssl/md.h @@ -5,12 +5,9 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,7 +26,7 @@ #ifndef POLARSSL_MD_H #define POLARSSL_MD_H -#include +#include #if defined(_MSC_VER) && !defined(inline) #define inline _inline @@ -203,17 +200,25 @@ void md_free( md_context_t *ctx ); */ int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ); +#if ! defined(POLARSSL_DEPRECATED_REMOVED) +#if defined(POLARSSL_DEPRECATED_WARNING) +#define DEPRECATED __attribute__((deprecated)) +#else +#define DEPRECATED +#endif /** * \brief Free the message-specific context of ctx. Freeing ctx itself * remains the responsibility of the caller. * - * \note Deprecated: Redirects to md_free() + * \deprecated Use md_free() instead * * \param ctx Free the message-specific context * * \returns 0 */ -int md_free_ctx( md_context_t *ctx ); +int md_free_ctx( md_context_t *ctx ) DEPRECATED; +#undef DEPRECATED +#endif /* POLARSSL_DEPRECATED_REMOVED */ /** * \brief Returns the size of the message digest output. diff --git a/ext/polarssl/include/polarssl/md2.h b/ext/mbedtls/include/polarssl/md2.h similarity index 95% rename from ext/polarssl/include/polarssl/md2.h rename to ext/mbedtls/include/polarssl/md2.h index 952b0bfceb..53c85f84fb 100644 --- a/ext/polarssl/include/polarssl/md2.h +++ b/ext/mbedtls/include/polarssl/md2.h @@ -3,12 +3,9 @@ * * \brief MD2 message digest algorithm (hash function) * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #define POLARSSL_ERR_MD2_FILE_IO_ERROR -0x0070 /**< Read/write error in file. */ diff --git a/ext/polarssl/include/polarssl/md4.h b/ext/mbedtls/include/polarssl/md4.h similarity index 95% rename from ext/polarssl/include/polarssl/md4.h rename to ext/mbedtls/include/polarssl/md4.h index fc5a5cd27a..68ac6c1396 100644 --- a/ext/polarssl/include/polarssl/md4.h +++ b/ext/mbedtls/include/polarssl/md4.h @@ -3,12 +3,9 @@ * * \brief MD4 message digest algorithm (hash function) * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include diff --git a/ext/polarssl/include/polarssl/md5.h b/ext/mbedtls/include/polarssl/md5.h similarity index 95% rename from ext/polarssl/include/polarssl/md5.h rename to ext/mbedtls/include/polarssl/md5.h index 2f378f6cd7..d5ce136a2b 100644 --- a/ext/polarssl/include/polarssl/md5.h +++ b/ext/mbedtls/include/polarssl/md5.h @@ -3,12 +3,9 @@ * * \brief MD5 message digest algorithm (hash function) * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include diff --git a/ext/polarssl/include/polarssl/md_wrap.h b/ext/mbedtls/include/polarssl/md_wrap.h similarity index 89% rename from ext/polarssl/include/polarssl/md_wrap.h rename to ext/mbedtls/include/polarssl/md_wrap.h index eb1db0f1ef..dd5fd6cf93 100644 --- a/ext/polarssl/include/polarssl/md_wrap.h +++ b/ext/mbedtls/include/polarssl/md_wrap.h @@ -5,12 +5,9 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2011, Brainspark B.V. + * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,6 +31,7 @@ #else #include POLARSSL_CONFIG_FILE #endif + #include "md.h" #ifdef __cplusplus diff --git a/ext/polarssl/include/polarssl/memory.h b/ext/mbedtls/include/polarssl/memory.h similarity index 65% rename from ext/polarssl/include/polarssl/memory.h rename to ext/mbedtls/include/polarssl/memory.h index 3af39511b4..8b8ac5dc4b 100644 --- a/ext/polarssl/include/polarssl/memory.h +++ b/ext/mbedtls/include/polarssl/memory.h @@ -1,14 +1,13 @@ /** * \file memory.h * - * \brief Memory allocation layer (Deprecated to platform layer) + * \brief Memory allocation layer * - * Copyright (C) 2006-2014, Brainspark B.V. + * \deprecated Use the platform layer instead * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,18 +34,29 @@ #include -#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_MEMORY) -#define POLARSSL_PLATFORM_MEMORY -#endif - #include "platform.h" #include "memory_buffer_alloc.h" +#if ! defined(POLARSSL_DEPRECATED_REMOVED) +#if defined(POLARSSL_DEPRECATED_WARNING) +#define DEPRECATED __attribute__((deprecated)) +#else +#define DEPRECATED +#endif +/** + * \brief Set malloc() / free() callback + * + * \deprecated Use platform_set_malloc_free instead + */ +int memory_set_own( void * (*malloc_func)( size_t ), + void (*free_func)( void * ) ) DEPRECATED; int memory_set_own( void * (*malloc_func)( size_t ), void (*free_func)( void * ) ) { return platform_set_malloc_free( malloc_func, free_func ); } +#undef DEPRECATED +#endif /* POLARSSL_DEPRECATED_REMOVED */ #endif /* memory.h */ diff --git a/ext/polarssl/include/polarssl/memory_buffer_alloc.h b/ext/mbedtls/include/polarssl/memory_buffer_alloc.h similarity index 80% rename from ext/polarssl/include/polarssl/memory_buffer_alloc.h rename to ext/mbedtls/include/polarssl/memory_buffer_alloc.h index c449752587..19abe55fa5 100644 --- a/ext/polarssl/include/polarssl/memory_buffer_alloc.h +++ b/ext/mbedtls/include/polarssl/memory_buffer_alloc.h @@ -3,12 +3,9 @@ * * \brief Buffer-based memory allocator * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include /** * \name SECTION: Module settings @@ -100,6 +97,27 @@ void memory_buffer_set_verify( int verify ); * trace if POLARSSL_MEMORY_BACKTRACE is defined. */ void memory_buffer_alloc_status( void ); + +/** + * \brief Get the peak heap usage so far + * + * \param max_used Peak number of bytes reauested by the application + * \param max_blocks Peak number of blocks reauested by the application + */ +void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks ); + +/** + * \brief Reset peak statistics + */ +void memory_buffer_alloc_max_reset( void ); + +/** + * \brief Get the current heap usage + * + * \param cur_used Number of bytes reauested by the application + * \param cur_blocks Number of blocks reauested by the application + */ +void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks ); #endif /* POLARSSL_MEMORY_DEBUG */ /** @@ -115,6 +133,15 @@ void memory_buffer_alloc_status( void ); */ int memory_buffer_alloc_verify( void ); +#if defined(POLARSSL_SELF_TEST) +/** + * \brief Checkup routine + * + * \return 0 if successful, or 1 if a test failed + */ +int memory_buffer_alloc_self_test( int verbose ); +#endif + #ifdef __cplusplus } #endif diff --git a/ext/polarssl/include/polarssl/net.h b/ext/mbedtls/include/polarssl/net.h similarity index 96% rename from ext/polarssl/include/polarssl/net.h rename to ext/mbedtls/include/polarssl/net.h index 22698b4ce4..d70639d1fb 100644 --- a/ext/polarssl/include/polarssl/net.h +++ b/ext/mbedtls/include/polarssl/net.h @@ -3,12 +3,9 @@ * * \brief Network communication functions * - * Copyright (C) 2006-2011, Brainspark B.V. + * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +24,7 @@ #ifndef POLARSSL_NET_H #define POLARSSL_NET_H -#include +#include #define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0056 /**< Failed to get an IP address for the given hostname. */ #define POLARSSL_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */ diff --git a/ext/polarssl/include/polarssl/oid.h b/ext/mbedtls/include/polarssl/oid.h similarity index 98% rename from ext/polarssl/include/polarssl/oid.h rename to ext/mbedtls/include/polarssl/oid.h index c4d5c3fba1..11330f251e 100644 --- a/ext/polarssl/include/polarssl/oid.h +++ b/ext/mbedtls/include/polarssl/oid.h @@ -3,12 +3,9 @@ * * \brief Object Identifier (OID) database * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,14 +24,17 @@ #ifndef POLARSSL_OID_H #define POLARSSL_OID_H -#include #if !defined(POLARSSL_CONFIG_FILE) #include "config.h" #else #include POLARSSL_CONFIG_FILE #endif + #include "asn1.h" #include "pk.h" + +#include + #if defined(POLARSSL_CIPHER_C) #include "cipher.h" #endif @@ -122,6 +122,7 @@ #define OID_AT_GIVEN_NAME OID_AT "\x2A" /**< id-at-givenName AttributeType:= {id-at 42} */ #define OID_AT_INITIALS OID_AT "\x2B" /**< id-at-initials AttributeType:= {id-at 43} */ #define OID_AT_GENERATION_QUALIFIER OID_AT "\x2C" /**< id-at-generationQualifier AttributeType:= {id-at 44} */ +#define OID_AT_UNIQUE_IDENTIFIER OID_AT "\x2D" /**< id-at-uniqueIdentifier AttributType:= {id-at 45} */ #define OID_AT_DN_QUALIFIER OID_AT "\x2E" /**< id-at-dnQualifier AttributeType:= {id-at 46} */ #define OID_AT_PSEUDONYM OID_AT "\x41" /**< id-at-pseudonym AttributeType:= {id-at 65} */ @@ -395,7 +396,7 @@ typedef struct { * \param oid OID to translate * * \return Length of the string written (excluding final NULL) or - * POLARSSL_ERR_OID_BUF_TO_SMALL in case of error + * POLARSSL_ERR_OID_BUF_TOO_SMALL in case of error */ int oid_get_numeric_string( char *buf, size_t size, const asn1_buf *oid ); diff --git a/ext/polarssl/include/polarssl/openssl.h b/ext/mbedtls/include/polarssl/openssl.h similarity index 93% rename from ext/polarssl/include/polarssl/openssl.h rename to ext/mbedtls/include/polarssl/openssl.h index b77e7dacb7..233a093249 100644 --- a/ext/polarssl/include/polarssl/openssl.h +++ b/ext/mbedtls/include/polarssl/openssl.h @@ -3,12 +3,11 @@ * * \brief OpenSSL wrapper (definitions, inline functions). * - * Copyright (C) 2006-2010, Brainspark B.V. + * \deprecated Use native mbed TLS functions instead * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker + * Copyright (C) 2006-2010, ARM Limited, All Rights Reserved * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,9 +23,17 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + /* * OpenSSL wrapper contributed by David Barett */ + +#if ! defined(POLARSSL_DEPRECATED_REMOVED) + +#if defined(POLARSSL_DEPRECATED_WARNING) +#warning "Including openssl.h is deprecated" +#endif + #ifndef POLARSSL_OPENSSL_H #define POLARSSL_OPENSSL_H @@ -138,3 +145,4 @@ inline int RSA_private_encrypt( int size, unsigned char* input, unsigned char* o #endif #endif /* openssl.h */ +#endif /* POLARSSL_DEPRECATED_REMOVED */ diff --git a/ext/polarssl/include/polarssl/padlock.h b/ext/mbedtls/include/polarssl/padlock.h similarity index 89% rename from ext/polarssl/include/polarssl/padlock.h rename to ext/mbedtls/include/polarssl/padlock.h index 3c5f7258b4..c4ac54cb97 100644 --- a/ext/polarssl/include/polarssl/padlock.h +++ b/ext/mbedtls/include/polarssl/padlock.h @@ -4,12 +4,9 @@ * \brief VIA PadLock ACE for HW encryption/decryption supported by some * processors * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,7 +29,15 @@ #define POLARSSL_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */ -#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) +#if defined(__has_feature) +#if __has_feature(address_sanitizer) +#define POLARSSL_HAVE_ASAN +#endif +#endif + +/* Some versions of ASan result in errors about not enough registers */ +#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \ + !defined(POLARSSL_HAVE_ASAN) #ifndef POLARSSL_HAVE_X86 #define POLARSSL_HAVE_X86 @@ -45,7 +50,6 @@ typedef INT32 int32_t; #include #endif - #define PADLOCK_RNG 0x000C #define PADLOCK_ACE 0x00C0 #define PADLOCK_PHE 0x0C00 diff --git a/ext/polarssl/include/polarssl/pbkdf2.h b/ext/mbedtls/include/polarssl/pbkdf2.h similarity index 74% rename from ext/polarssl/include/polarssl/pbkdf2.h rename to ext/mbedtls/include/polarssl/pbkdf2.h index 5ccb2fa9f2..28987b3f7f 100644 --- a/ext/polarssl/include/polarssl/pbkdf2.h +++ b/ext/mbedtls/include/polarssl/pbkdf2.h @@ -2,16 +2,14 @@ * \file pbkdf2.h * * \brief Password-Based Key Derivation Function 2 (from PKCS#5) - * DEPRECATED: use pkcs5.h instead. * - * \author Mathias Olsson + * \deprecated Use pkcs5.h instead. * - * Copyright (C) 2006-2012, Brainspark B.V. + * \author Mathias Olsson * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker + * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,10 +28,10 @@ #ifndef POLARSSL_PBKDF2_H #define POLARSSL_PBKDF2_H -#include - #include "md.h" +#include + #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include typedef UINT32 uint32_t; @@ -47,9 +45,16 @@ typedef UINT32 uint32_t; extern "C" { #endif +#if ! defined(POLARSSL_DEPRECATED_REMOVED) +#if defined(POLARSSL_DEPRECATED_WARNING) +#define DEPRECATED __attribute__((deprecated)) +#else +#define DEPRECATED +#endif /** * \brief PKCS#5 PBKDF2 using HMAC - * DEPRECATED: Use pkcs5_pbkdf2_hmac() instead! + * + * \deprecated Use pkcs5_pbkdf2_hmac() instead * * \param ctx Generic HMAC context * \param password Password to use when generating key @@ -60,20 +65,23 @@ extern "C" { * \param key_length Length of generated key * \param output Generated key. Must be at least as big as key_length * - * \returns 0 on success, or a PolarSSL error code if verification fails. + * \returns 0 on success, or a POLARSSL_ERR_xxx code if verification fails. */ int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen, const unsigned char *salt, size_t slen, unsigned int iteration_count, - uint32_t key_length, unsigned char *output ); + uint32_t key_length, unsigned char *output ) DEPRECATED; /** * \brief Checkup routine - * DEPRECATED: Use pkcs5_self_test() instead! + * + * \deprecated Use pkcs5_self_test() instead * * \return 0 if successful, or 1 if the test failed */ -int pbkdf2_self_test( int verbose ); +int pbkdf2_self_test( int verbose ) DEPRECATED; +#undef DEPRECATED +#endif /* POLARSSL_DEPRECATED_REMOVED */ #ifdef __cplusplus } diff --git a/ext/polarssl/include/polarssl/pem.h b/ext/mbedtls/include/polarssl/pem.h similarity index 95% rename from ext/polarssl/include/polarssl/pem.h rename to ext/mbedtls/include/polarssl/pem.h index e606cf08eb..b47e0e9fc5 100644 --- a/ext/polarssl/include/polarssl/pem.h +++ b/ext/mbedtls/include/polarssl/pem.h @@ -3,12 +3,9 @@ * * \brief Privacy Enhanced Mail (PEM) decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +24,7 @@ #ifndef POLARSSL_PEM_H #define POLARSSL_PEM_H -#include +#include /** * \name PEM Error codes diff --git a/ext/polarssl/include/polarssl/pk.h b/ext/mbedtls/include/polarssl/pk.h similarity index 96% rename from ext/polarssl/include/polarssl/pk.h rename to ext/mbedtls/include/polarssl/pk.h index 754dda2190..8d9407c417 100644 --- a/ext/polarssl/include/polarssl/pk.h +++ b/ext/mbedtls/include/polarssl/pk.h @@ -3,12 +3,9 @@ * * \brief Public Key abstraction layer * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -177,6 +174,9 @@ typedef struct int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); + /** Check public-private key pair */ + int (*check_pair_func)( const void *pub, const void *prv ); + /** Allocate a new context */ void * (*ctx_alloc_func)( void ); @@ -426,6 +426,16 @@ int pk_encrypt( pk_context *ctx, unsigned char *output, size_t *olen, size_t osize, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); +/** + * \brief Check if a public-private pair of keys matches. + * + * \param pub Context holding a public key. + * \param prv Context holding a private (and public) key. + * + * \return 0 on success or POLARSSL_ERR_PK_BAD_INPUT_DATA + */ +int pk_check_pair( const pk_context *pub, const pk_context *prv ); + /** * \brief Export debug information * @@ -625,6 +635,14 @@ int pk_write_pubkey( unsigned char **p, unsigned char *start, const pk_context *key ); #endif /* POLARSSL_PK_WRITE_C */ +/* + * Internal module functions. You probably do not want to use these unless you + * know you do. + */ +#if defined(POLARSSL_FS_IO) +int pk_load_file( const char *path, unsigned char **buf, size_t *n ); +#endif + #ifdef __cplusplus } #endif diff --git a/ext/polarssl/include/polarssl/pk_wrap.h b/ext/mbedtls/include/polarssl/pk_wrap.h similarity index 88% rename from ext/polarssl/include/polarssl/pk_wrap.h rename to ext/mbedtls/include/polarssl/pk_wrap.h index 7baafb95da..3677250073 100644 --- a/ext/polarssl/include/polarssl/pk_wrap.h +++ b/ext/mbedtls/include/polarssl/pk_wrap.h @@ -3,12 +3,9 @@ * * \brief Public Key abstraction layer: wrapper functions * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/pkcs11.h b/ext/mbedtls/include/polarssl/pkcs11.h similarity index 95% rename from ext/polarssl/include/polarssl/pkcs11.h rename to ext/mbedtls/include/polarssl/pkcs11.h index 84f862dd90..18c3370fce 100644 --- a/ext/polarssl/include/polarssl/pkcs11.h +++ b/ext/mbedtls/include/polarssl/pkcs11.h @@ -5,12 +5,9 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -62,7 +59,7 @@ typedef struct { } pkcs11_context; /** - * Fill in a PolarSSL certificate, based on the given PKCS11 helper certificate. + * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate. * * \param cert X.509 certificate to fill * \param pkcs11h_cert PKCS #11 helper certificate diff --git a/ext/polarssl/include/polarssl/pkcs12.h b/ext/mbedtls/include/polarssl/pkcs12.h similarity index 93% rename from ext/polarssl/include/polarssl/pkcs12.h rename to ext/mbedtls/include/polarssl/pkcs12.h index 4bd5018af5..cb53fe2472 100644 --- a/ext/polarssl/include/polarssl/pkcs12.h +++ b/ext/mbedtls/include/polarssl/pkcs12.h @@ -3,12 +3,9 @@ * * \brief PKCS#12 Personal Information Exchange Syntax * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,12 +24,12 @@ #ifndef POLARSSL_PKCS12_H #define POLARSSL_PKCS12_H -#include - #include "md.h" #include "cipher.h" #include "asn1.h" +#include + #define POLARSSL_ERR_PKCS12_BAD_INPUT_DATA -0x1F80 /**< Bad input parameters to function. */ #define POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00 /**< Feature not available, e.g. unsupported encryption scheme. */ #define POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80 /**< PBE ASN.1 data not as expected. */ @@ -61,7 +58,7 @@ extern "C" { * \param len data length * \param output the output buffer * - * \return 0 if successful, or a PolarSSL error code + * \return 0 if successful, or a POLARSSL_ERR_xxx code */ int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode, const unsigned char *pwd, size_t pwdlen, @@ -82,7 +79,7 @@ int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode, * \param len data length * \param output the output buffer * - * \return 0 if successful, or a PolarSSL error code + * \return 0 if successful, or a POLARSSL_ERR_xxx code */ int pkcs12_pbe( asn1_buf *pbe_params, int mode, cipher_type_t cipher_type, md_type_t md_type, diff --git a/ext/polarssl/include/polarssl/pkcs5.h b/ext/mbedtls/include/polarssl/pkcs5.h similarity index 89% rename from ext/polarssl/include/polarssl/pkcs5.h rename to ext/mbedtls/include/polarssl/pkcs5.h index 34e824b73f..6a21af10b4 100644 --- a/ext/polarssl/include/polarssl/pkcs5.h +++ b/ext/mbedtls/include/polarssl/pkcs5.h @@ -5,12 +5,9 @@ * * \author Mathias Olsson * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,11 +26,11 @@ #ifndef POLARSSL_PKCS5_H #define POLARSSL_PKCS5_H -#include - #include "asn1.h" #include "md.h" +#include + #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include typedef UINT32 uint32_t; @@ -64,7 +61,7 @@ extern "C" { * \param datalen length of data * \param output output buffer * - * \returns 0 on success, or a PolarSSL error code if verification fails. + * \returns 0 on success, or a POLARSSL_ERR_xxx code if verification fails. */ int pkcs5_pbes2( asn1_buf *pbe_params, int mode, const unsigned char *pwd, size_t pwdlen, @@ -83,7 +80,7 @@ int pkcs5_pbes2( asn1_buf *pbe_params, int mode, * \param key_length Length of generated key * \param output Generated key. Must be at least as big as key_length * - * \returns 0 on success, or a PolarSSL error code if verification fails. + * \returns 0 on success, or a POLARSSL_ERR_xxx code if verification fails. */ int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen, const unsigned char *salt, size_t slen, diff --git a/ext/polarssl/include/polarssl/platform.h b/ext/mbedtls/include/polarssl/platform.h similarity index 54% rename from ext/polarssl/include/polarssl/platform.h rename to ext/mbedtls/include/polarssl/platform.h index eae887afc7..639b180e33 100644 --- a/ext/polarssl/include/polarssl/platform.h +++ b/ext/mbedtls/include/polarssl/platform.h @@ -1,14 +1,11 @@ /** * \file platform.h * - * \brief PolarSSL Platform abstraction layer + * \brief mbed TLS Platform abstraction layer * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,10 @@ #include POLARSSL_CONFIG_FILE #endif -#include +/* Temporary compatibility hack for to keep MEMORY_C working */ +#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_MEMORY) +#define POLARSSL_PLATFORM_MEMORY +#endif #ifdef __cplusplus extern "C" { @@ -48,7 +48,11 @@ extern "C" { */ #if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS) +#include #include +#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF) +#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */ +#endif #if !defined(POLARSSL_PLATFORM_STD_PRINTF) #define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ #endif @@ -61,6 +65,9 @@ extern "C" { #if !defined(POLARSSL_PLATFORM_STD_FREE) #define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */ #endif +#if !defined(POLARSSL_PLATFORM_STD_EXIT) +#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default free to use */ +#endif #else /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */ #if defined(POLARSSL_PLATFORM_STD_MEM_HDR) #include POLARSSL_PLATFORM_STD_MEM_HDR @@ -73,6 +80,13 @@ extern "C" { * The function pointers for malloc and free */ #if defined(POLARSSL_PLATFORM_MEMORY) +#if defined(POLARSSL_PLATFORM_FREE_MACRO) && \ + defined(POLARSSL_PLATFORM_MALLOC_MACRO) +#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO +#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO +#else +/* For size_t */ +#include extern void * (*polarssl_malloc)( size_t len ); extern void (*polarssl_free)( void *ptr ); @@ -86,10 +100,36 @@ extern void (*polarssl_free)( void *ptr ); */ int platform_set_malloc_free( void * (*malloc_func)( size_t ), void (*free_func)( void * ) ); -#else /* POLARSSL_PLATFORM_ENTROPY */ -#define polarssl_malloc malloc +#endif /* POLARSSL_PLATFORM_FREE_MACRO && POLARSSL_PLATFORM_MALLOC_MACRO */ +#else /* !POLARSSL_PLATFORM_MEMORY */ #define polarssl_free free -#endif /* POLARSSL_PLATFORM_ENTROPY */ +#define polarssl_malloc malloc +#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */ + +/* + * The function pointers for fprintf + */ +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) +/* We need FILE * */ +#include +extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); + +/** + * \brief Set your own fprintf function pointer + * + * \param fprintf_func the fprintf function implementation + * + * \return 0 + */ +int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, + ... ) ); +#else +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) +#define polarssl_fprintf POLARSSL_PLATFORM_FPRINTF_MACRO +#else +#define polarssl_fprintf fprintf +#endif /* POLARSSL_PLATFORM_FPRINTF_MACRO */ +#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ /* * The function pointers for printf @@ -105,21 +145,58 @@ extern int (*polarssl_printf)( const char *format, ... ); * \return 0 */ int platform_set_printf( int (*printf_func)( const char *, ... ) ); -#else /* POLARSSL_PLATFORM_PRINTF_ALT */ +#else /* !POLARSSL_PLATFORM_PRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) +#define polarssl_printf POLARSSL_PLATFORM_PRINTF_MACRO +#else #define polarssl_printf printf +#endif /* POLARSSL_PLATFORM_PRINTF_MACRO */ #endif /* POLARSSL_PLATFORM_PRINTF_ALT */ /* - * The function pointers for fprintf + * The function pointers for snprintf */ -#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) -extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... ); -int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, - ... ) ); +/** + * \brief Set your own snprintf function pointer + * + * \param snprintf_func the snprintf function implementation + * + * \return 0 + */ +int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, ... ) ); +#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) +#define polarssl_snprintf POLARSSL_PLATFORM_SNPRINTF_MACRO #else -#define polarssl_fprintf fprintf -#endif +#define polarssl_snprintf snprintf +#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */ +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + +/* + * The function pointers for exit + */ +#if defined(POLARSSL_PLATFORM_EXIT_ALT) +extern void (*polarssl_exit)( int status ); + +/** + * \brief Set your own exit function pointer + * + * \param exit_func the exit function implementation + * + * \return 0 + */ +int platform_set_exit( void (*exit_func)( int status ) ); +#else +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) +#define polarssl_exit POLARSSL_PLATFORM_EXIT_MACRO +#else +#define polarssl_exit exit +#endif /* POLARSSL_PLATFORM_EXIT_MACRO */ +#endif /* POLARSSL_PLATFORM_EXIT_ALT */ #ifdef __cplusplus } diff --git a/ext/polarssl/include/polarssl/ripemd160.h b/ext/mbedtls/include/polarssl/ripemd160.h similarity index 95% rename from ext/polarssl/include/polarssl/ripemd160.h rename to ext/mbedtls/include/polarssl/ripemd160.h index e3b66c90f6..2555eb407f 100644 --- a/ext/polarssl/include/polarssl/ripemd160.h +++ b/ext/mbedtls/include/polarssl/ripemd160.h @@ -3,12 +3,9 @@ * * \brief RIPE MD-160 message digest * - * Copyright (C) 2014-2014, Brainspark B.V. + * Copyright (C) 2014-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include diff --git a/ext/polarssl/include/polarssl/rsa.h b/ext/mbedtls/include/polarssl/rsa.h similarity index 94% rename from ext/polarssl/include/polarssl/rsa.h rename to ext/mbedtls/include/polarssl/rsa.h index c06c7d5050..e80bb4da08 100644 --- a/ext/polarssl/include/polarssl/rsa.h +++ b/ext/mbedtls/include/polarssl/rsa.h @@ -3,12 +3,9 @@ * * \brief The RSA public-key cryptosystem * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,7 +43,7 @@ #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */ #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */ #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */ -#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */ +#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */ #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */ #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */ #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */ @@ -99,10 +96,8 @@ typedef struct mpi RP; /*!< cached R^2 mod P */ mpi RQ; /*!< cached R^2 mod Q */ -#if !defined(POLARSSL_RSA_NO_CRT) mpi Vi; /*!< cached blinding value */ mpi Vf; /*!< cached un-blinding value */ -#endif int padding; /*!< RSA_PKCS_V15 for 1.5 padding and RSA_PKCS_v21 for OAEP/PSS */ @@ -191,8 +186,20 @@ int rsa_check_pubkey( const rsa_context *ctx ); */ int rsa_check_privkey( const rsa_context *ctx ); +/** + * \brief Check a public-private RSA key pair. + * Check each of the contexts, and make sure they match. + * + * \param pub RSA context holding the public key + * \param prv RSA context holding the private key + * + * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code + */ +int rsa_check_pub_priv( const rsa_context *pub, const rsa_context *prv ); + /** * \brief Do an RSA public key operation + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param input input buffer @@ -213,6 +220,7 @@ int rsa_public( rsa_context *ctx, /** * \brief Do an RSA private key operation + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Needed for blinding) @@ -235,6 +243,7 @@ int rsa_private( rsa_context *ctx, * \brief Generic wrapper to perform a PKCS#1 encryption using the * mode from the context. Add the message padding, then do an * RSA operation. + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding @@ -259,6 +268,7 @@ int rsa_pkcs1_encrypt( rsa_context *ctx, /** * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Needed for padding and RSA_PRIVATE) @@ -282,6 +292,7 @@ int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx, /** * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding @@ -312,6 +323,7 @@ int rsa_rsaes_oaep_encrypt( rsa_context *ctx, * \brief Generic wrapper to perform a PKCS#1 decryption using the * mode from the context. Do an RSA operation, then remove * the message padding + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Only needed for RSA_PRIVATE) @@ -338,6 +350,7 @@ int rsa_pkcs1_decrypt( rsa_context *ctx, /** * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Only needed for RSA_PRIVATE) @@ -364,6 +377,7 @@ int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx, /** * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Only needed for RSA_PRIVATE) @@ -396,6 +410,7 @@ int rsa_rsaes_oaep_decrypt( rsa_context *ctx, * \brief Generic wrapper to perform a PKCS#1 signature using the * mode from the context. Do a private RSA operation to sign * a message digest + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for @@ -454,6 +469,7 @@ int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx, /** * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx RSA context * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for @@ -489,6 +505,7 @@ int rsa_rsassa_pss_sign( rsa_context *ctx, * \brief Generic wrapper to perform a PKCS#1 verification using the * mode from the context. Do a public RSA operation and check * the message digest + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx points to an RSA public key * \param f_rng RNG function (Only needed for RSA_PRIVATE) @@ -519,6 +536,7 @@ int rsa_pkcs1_verify( rsa_context *ctx, /** * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx points to an RSA public key * \param f_rng RNG function (Only needed for RSA_PRIVATE) @@ -547,6 +565,7 @@ int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx, /** * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) * (This is the "simple" version.) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx points to an RSA public key * \param f_rng RNG function (Only needed for RSA_PRIVATE) @@ -581,6 +600,7 @@ int rsa_rsassa_pss_verify( rsa_context *ctx, /** * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) * (This is the version with "full" options.) + * (Thread-safe if POLARSSL_THREADING_C is enabled) * * \param ctx points to an RSA public key * \param f_rng RNG function (Only needed for RSA_PRIVATE) diff --git a/ext/polarssl/include/polarssl/sha1.h b/ext/mbedtls/include/polarssl/sha1.h similarity index 95% rename from ext/polarssl/include/polarssl/sha1.h rename to ext/mbedtls/include/polarssl/sha1.h index cb0c4367f9..e9e5a8e72b 100644 --- a/ext/polarssl/include/polarssl/sha1.h +++ b/ext/mbedtls/include/polarssl/sha1.h @@ -3,12 +3,9 @@ * * \brief SHA-1 cryptographic hash function * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include diff --git a/ext/polarssl/include/polarssl/sha256.h b/ext/mbedtls/include/polarssl/sha256.h similarity index 96% rename from ext/polarssl/include/polarssl/sha256.h rename to ext/mbedtls/include/polarssl/sha256.h index b143674100..f8917ff82f 100644 --- a/ext/polarssl/include/polarssl/sha256.h +++ b/ext/mbedtls/include/polarssl/sha256.h @@ -3,12 +3,9 @@ * * \brief SHA-224 and SHA-256 cryptographic hash function * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include diff --git a/ext/polarssl/include/polarssl/sha512.h b/ext/mbedtls/include/polarssl/sha512.h similarity index 95% rename from ext/polarssl/include/polarssl/sha512.h rename to ext/mbedtls/include/polarssl/sha512.h index dfbae4a734..15d266cd47 100644 --- a/ext/polarssl/include/polarssl/sha512.h +++ b/ext/mbedtls/include/polarssl/sha512.h @@ -3,12 +3,9 @@ * * \brief SHA-384 and SHA-512 cryptographic hash function * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,14 +30,12 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) || defined(__WATCOMC__) - #define UL64(x) x##ui64 typedef unsigned __int64 uint64_t; #else #include - #define UL64(x) x##ULL #endif #define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */ diff --git a/ext/polarssl/include/polarssl/ssl.h b/ext/mbedtls/include/polarssl/ssl.h similarity index 87% rename from ext/polarssl/include/polarssl/ssl.h rename to ext/mbedtls/include/polarssl/ssl.h index 194e944718..9c6a0c5b0d 100644 --- a/ext/polarssl/include/polarssl/ssl.h +++ b/ext/mbedtls/include/polarssl/ssl.h @@ -3,12 +3,9 @@ * * \brief SSL/TLS functions. * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,6 +29,12 @@ #else #include POLARSSL_CONFIG_FILE #endif + +/* Temporary compatibility trick for the current stable branch */ +#if !defined(POLARSSL_SSL_DISABLE_RENEGOTIATION) +#define POLARSSL_SSL_RENEGOTIATION +#endif + #include "net.h" #include "bignum.h" #include "ecp.h" @@ -114,7 +117,7 @@ #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */ #define POLARSSL_ERR_SSL_NO_RNG -0x7400 /**< No RNG was provided to the SSL module. */ #define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */ -#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message.*/ +#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message. */ #define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */ #define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key or pre-shared key is not set, but needed. */ #define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */ @@ -146,6 +149,7 @@ #define POLARSSL_ERR_SSL_INTERNAL_ERROR -0x6C00 /**< Internal error (eg, unexpected failure in lower-level module) */ #define POLARSSL_ERR_SSL_COUNTER_WRAPPING -0x6B80 /**< A counter would wrap (eg, too many messages exchanged). */ #define POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00 /**< Unexpected message at ServerHello in renegotiation. */ +#define POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6A80 /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */ /* * Various constants @@ -206,6 +210,15 @@ #define SSL_IS_CLIENT 0 #define SSL_IS_SERVER 1 +#define SSL_IS_NOT_FALLBACK 0 +#define SSL_IS_FALLBACK 1 + +#define SSL_EXTENDED_MS_DISABLED 0 +#define SSL_EXTENDED_MS_ENABLED 1 + +#define SSL_ETM_DISABLED 0 +#define SSL_ETM_ENABLED 1 + #define SSL_COMPRESS_NULL 0 #define SSL_COMPRESS_DEFLATE 1 @@ -238,6 +251,12 @@ #define SSL_SESSION_TICKETS_DISABLED 0 #define SSL_SESSION_TICKETS_ENABLED 1 +#define SSL_CBC_RECORD_SPLITTING_DISABLED -1 +#define SSL_CBC_RECORD_SPLITTING_ENABLED 0 + +#define SSL_ARC4_ENABLED 0 +#define SSL_ARC4_DISABLED 1 + /** * \name SECTION: Module settings * @@ -263,6 +282,12 @@ #define SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */ #endif +/* + * Minimum size of the Diffie-Hellman parameters to accept from a server. + * The default is 1024 bits (128 bytes) for compatibility reasons. + * From a purely security perspective, 2048 bits would be better. + */ +#define SSL_MIN_DHM_BYTES 128 /**< Min size of the Diffie-Hellman prime */ /* \} name SECTION: Module settings */ /* @@ -304,10 +329,20 @@ + SSL_PADDING_ADD \ ) +/* + * Length of the verify data for secure renegotiation + */ +#if defined(POLARSSL_SSL_PROTO_SSL3) +#define SSL_VERIFY_DATA_MAX_LEN 36 +#else +#define SSL_VERIFY_DATA_MAX_LEN 12 +#endif + /* * Signaling ciphersuite values (SCSV) */ #define SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */ +#define SSL_FALLBACK_SCSV 0x5600 /**< draft-ietf-tls-downgrade-scsv-00 */ /* * Supported Signature and Hash algorithms (For TLS 1.2) @@ -365,6 +400,7 @@ #define SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */ #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */ #define SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */ +#define SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */ #define SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */ #define SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */ #define SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */ @@ -401,6 +437,9 @@ #define TLS_EXT_ALPN 16 +#define TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */ +#define TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */ + #define TLS_EXT_SESSION_TICKET 35 #define TLS_EXT_RENEGOTIATION_INFO 0xFF01 @@ -444,7 +483,7 @@ union _ssl_premaster_secret #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) unsigned char _pms_rsa_psk[52 + POLARSSL_PSK_MAX_LEN]; /* RFC 4279 4 */ #endif -#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) +#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) unsigned char _pms_ecdhe_psk[4 + POLARSSL_ECP_MAX_BYTES + POLARSSL_PSK_MAX_LEN]; /* RFC 5489 2 */ #endif @@ -538,6 +577,10 @@ struct _ssl_session #if defined(POLARSSL_SSL_TRUNCATED_HMAC) int trunc_hmac; /*!< flag for truncated hmac activation */ #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ + +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + int encrypt_then_mac; /*!< flag for EtM activation */ +#endif }; /* @@ -652,6 +695,9 @@ struct _ssl_handshake_params #if defined(POLARSSL_SSL_SESSION_TICKETS) int new_session_ticket; /*!< use NewSessionTicket? */ #endif /* POLARSSL_SSL_SESSION_TICKETS */ +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + int extended_ms; /*!< use Extended Master Secret? */ +#endif }; #if defined(POLARSSL_SSL_SESSION_TICKETS) @@ -687,7 +733,9 @@ struct _ssl_context */ int state; /*!< SSL handshake: current state */ int renegotiation; /*!< Initial or renegotiation */ +#if defined(POLARSSL_SSL_RENEGOTIATION) int renego_records_seen; /*!< Records since renego request */ +#endif int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */ int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */ @@ -697,6 +745,17 @@ struct _ssl_context int min_major_ver; /*!< min. major version used */ int min_minor_ver; /*!< min. minor version used */ +#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C) + char fallback; /*!< flag for fallback connections */ +#endif +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + char encrypt_then_mac; /*!< flag for encrypt-then-mac */ +#endif +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + char extended_ms; /*!< flag for extended master secret */ +#endif + char arc4_disabled; /*!< flag for disabling RC4 */ + /* * Callbacks (RNG, debug, I/O, verification) */ @@ -784,6 +843,10 @@ struct _ssl_context #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) unsigned char mfl_code; /*!< MaxFragmentLength chosen by us */ #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) + signed char split_done; /*!< flag for record splitting: + -1 disabled, 0 todo, 1 done */ +#endif /* * PKI layer @@ -810,9 +873,13 @@ struct _ssl_context int authmode; /*!< verification mode */ int client_auth; /*!< flag for client auth. */ int verify_result; /*!< verification result */ +#if defined(POLARSSL_SSL_RENEGOTIATION) int disable_renegotiation; /*!< enable/disable renegotiation */ - int allow_legacy_renegotiation; /*!< allow legacy renegotiation */ int renego_max_records; /*!< grace period for renegotiation */ + unsigned char renego_period[8]; /*!< value of the record counters + that triggers renegotiation */ +#endif + int allow_legacy_renegotiation; /*!< allow legacy renegotiation */ const int *ciphersuite_list[4]; /*!< allowed ciphersuites / version */ #if defined(POLARSSL_SSL_SET_CURVES) const ecp_group_id *curve_list; /*!< allowed curves */ @@ -861,9 +928,11 @@ struct _ssl_context */ int secure_renegotiation; /*!< does peer support legacy or secure renegotiation */ +#if defined(POLARSSL_SSL_RENEGOTIATION) size_t verify_data_len; /*!< length of verify data stored */ - char own_verify_data[36]; /*!< previous handshake verify data */ - char peer_verify_data[36]; /*!< previous handshake verify data */ + char own_verify_data[SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */ + char peer_verify_data[SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */ +#endif }; #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) @@ -1024,9 +1093,11 @@ void ssl_set_bio( ssl_context *ssl, int (*f_recv)(void *, unsigned char *, size_t), void *p_recv, int (*f_send)(void *, const unsigned char *, size_t), void *p_send ); +#if defined(POLARSSL_SSL_SRV_C) /** * \brief Set the session cache callbacks (server-side only) - * If not set, no session resuming is done. + * If not set, no session resuming is done (except if session + * tickets are enabled too). * * The session cache has the responsibility to check for stale * entries based on timeout. See RFC 5246 for recommendations. @@ -1064,7 +1135,9 @@ void ssl_set_bio( ssl_context *ssl, void ssl_set_session_cache( ssl_context *ssl, int (*f_get_cache)(void *, ssl_session *), void *p_get_cache, int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache ); +#endif /* POLARSSL_SSL_SRV_C */ +#if defined(POLARSSL_SSL_CLI_C) /** * \brief Request resumption of session (client-side only) * Session data is copied from presented session structure. @@ -1080,14 +1153,18 @@ void ssl_set_session_cache( ssl_context *ssl, * \sa ssl_get_session() */ int ssl_set_session( ssl_context *ssl, const ssl_session *session ); +#endif /* POLARSSL_SSL_CLI_C */ /** * \brief Set the list of allowed ciphersuites and the preference * order. First in the list has the highest preference. * (Overrides all version specific lists) * - * Note: The PolarSSL SSL server uses its own preferences - * over the preference of the connection SSL client unless + * The ciphersuites array is not copied, and must remain + * valid for the lifetime of the ssl_context. + * + * Note: The server uses its own preferences + * over the preference of the client unless * POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined! * * \param ssl SSL context @@ -1145,6 +1222,12 @@ void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain, int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert, pk_context *pk_key ); +#if ! defined(POLARSSL_DEPRECATED_REMOVED) +#if defined(POLARSSL_DEPRECATED_WARNING) +#define DEPRECATED __attribute__((deprecated)) +#else +#define DEPRECATED +#endif #if defined(POLARSSL_RSA_C) /** * \brief Set own certificate chain and private RSA key @@ -1153,8 +1236,7 @@ int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert, * up your certificate chain. The top certificate (self-signed) * can be omitted. * - * \warning This backwards-compatibility function is deprecated! - * Please use \c ssl_set_own_cert() instead. + * \deprecated Please use \c ssl_set_own_cert() instead. * * \param ssl SSL context * \param own_cert own public certificate chain @@ -1163,11 +1245,11 @@ int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert, * \return 0 on success, or a specific error code. */ int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert, - rsa_context *rsa_key ); + rsa_context *rsa_key ) DEPRECATED; #endif /* POLARSSL_RSA_C */ /** - * \brief Set own certificate and alternate non-PolarSSL RSA private + * \brief Set own certificate and external RSA private * key and handling callbacks, such as the PKCS#11 wrappers * or any other external private key handler. * (see the respective RSA functions in rsa.h for documentation @@ -1178,8 +1260,7 @@ int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert, * up your certificate chain. The top certificate (self-signed) * can be omitted. * - * \warning This backwards-compatibility function is deprecated! - * Please use \c pk_init_ctx_rsa_alt() + * \deprecated Please use \c pk_init_ctx_rsa_alt() * and \c ssl_set_own_cert() instead. * * \param ssl SSL context @@ -1195,7 +1276,9 @@ int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert, void *rsa_key, rsa_decrypt_func rsa_decrypt, rsa_sign_func rsa_sign, - rsa_key_len_func rsa_key_len ); + rsa_key_len_func rsa_key_len ) DEPRECATED; +#undef DEPRECATED +#endif /* POLARSSL_DEPRECATED_REMOVED */ #endif /* POLARSSL_X509_CRT_PARSE_C */ #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) @@ -1244,7 +1327,7 @@ void ssl_set_psk_cb( ssl_context *ssl, /** * \brief Set the Diffie-Hellman public P and G values, * read as hexadecimal strings (server-side only) - * (Default: POLARSSL_DHM_RFC5114_MODP_1024_[PG]) + * (Default: POLARSSL_DHM_RFC5114_MODP_2048_[PG]) * * \param ssl SSL context * \param dhm_P Diffie-Hellman-Merkle modulus @@ -1367,13 +1450,14 @@ const char *ssl_get_alpn_protocol( const ssl_context *ssl ); */ void ssl_set_max_version( ssl_context *ssl, int major, int minor ); - /** * \brief Set the minimum accepted SSL/TLS protocol version * (Default: SSL_MIN_MAJOR_VERSION, SSL_MIN_MINOR_VERSION) * - * Note: Input outside of the SSL_MAX_XXXXX_VERSION and - * SSL_MIN_XXXXX_VERSION range is ignored. + * \note Input outside of the SSL_MAX_XXXXX_VERSION and + * SSL_MIN_XXXXX_VERSION range is ignored. + * + * \note SSL_MINOR_VERSION_0 (SSL v3) should be avoided. * * \param ssl SSL context * \param major Major version number (only SSL_MAJOR_VERSION_3 supported) @@ -1383,6 +1467,74 @@ void ssl_set_max_version( ssl_context *ssl, int major, int minor ); */ void ssl_set_min_version( ssl_context *ssl, int major, int minor ); +#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C) +/** + * \brief Set the fallback flag (client-side only). + * (Default: SSL_IS_NOT_FALLBACK). + * + * \note Set to SSL_IS_FALLBACK when preparing a fallback + * connection, that is a connection with max_version set to a + * lower value than the value you're willing to use. Such + * fallback connections are not recommended but are sometimes + * necessary to interoperate with buggy (version-intolerant) + * servers. + * + * \warning You should NOT set this to SSL_IS_FALLBACK for + * non-fallback connections! This would appear to work for a + * while, then cause failures when the server is upgraded to + * support a newer TLS version. + * + * \param ssl SSL context + * \param fallback SSL_IS_NOT_FALLBACK or SSL_IS_FALLBACK + */ +void ssl_set_fallback( ssl_context *ssl, char fallback ); +#endif /* POLARSSL_SSL_FALLBACK_SCSV && POLARSSL_SSL_CLI_C */ + +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) +/** + * \brief Enable or disable Encrypt-then-MAC + * (Default: SSL_ETM_ENABLED) + * + * \note This should always be enabled, it is a security + * improvement, and should not cause any interoperability + * issue (used only if the peer supports it too). + * + * \param ssl SSL context + * \param etm SSL_ETM_ENABLED or SSL_ETM_DISABLED + */ +void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm ); +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) +/** + * \brief Enable or disable Extended Master Secret negotiation. + * (Default: SSL_EXTENDED_MS_ENABLED) + * + * \note This should always be enabled, it is a security fix to the + * protocol, and should not cause any interoperability issue + * (used only if the peer supports it too). + * + * \param ssl SSL context + * \param ems SSL_EXTENDED_MS_ENABLED or SSL_EXTENDED_MS_DISABLED + */ +void ssl_set_extended_master_secret( ssl_context *ssl, char ems ); +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + +/** + * \brief Disable or enable support for RC4 + * (Default: SSL_ARC4_ENABLED) + * + * \note Though the default is RC4 for compatibility reasons in the + * 1.3 branch, the recommended value is SSL_ARC4_DISABLED. + * + * \note This function will likely be removed in future versions as + * RC4 will then be disabled by default at compile time. + * + * \param ssl SSL context + * \param arc4 SSL_ARC4_ENABLED or SSL_ARC4_DISABLED + */ +void ssl_set_arc4_support( ssl_context *ssl, char arc4 ); + #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) /** * \brief Set the maximum fragment length to emit and/or negotiate @@ -1397,26 +1549,41 @@ void ssl_set_min_version( ssl_context *ssl, int major, int minor ); * SSL_MAX_FRAG_LEN_512, SSL_MAX_FRAG_LEN_1024, * SSL_MAX_FRAG_LEN_2048, SSL_MAX_FRAG_LEN_4096) * - * \return O if successful or POLARSSL_ERR_SSL_BAD_INPUT_DATA + * \return 0 if successful or POLARSSL_ERR_SSL_BAD_INPUT_DATA */ int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code ); #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ #if defined(POLARSSL_SSL_TRUNCATED_HMAC) /** - * \brief Activate negotiation of truncated HMAC (Client only) - * (Default: SSL_TRUNC_HMAC_ENABLED) + * \brief Activate negotiation of truncated HMAC + * (Default: SSL_TRUNC_HMAC_DISABLED on client, + * SSL_TRUNC_HMAC_ENABLED on server.) * * \param ssl SSL context * \param truncate Enable or disable (SSL_TRUNC_HMAC_ENABLED or * SSL_TRUNC_HMAC_DISABLED) * - * \return O if successful, - * POLARSSL_ERR_SSL_BAD_INPUT_DATA if used server-side + * \return Always 0. */ int ssl_set_truncated_hmac( ssl_context *ssl, int truncate ); #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) +/** + * \brief Enable / Disable 1/n-1 record splitting + * (Default: SSL_CBC_RECORD_SPLITTING_ENABLED) + * + * \note Only affects SSLv3 and TLS 1.0, not higher versions. + * Does not affect non-CBC ciphersuites in any version. + * + * \param ssl SSL context + * \param split SSL_CBC_RECORD_SPLITTING_ENABLED or + * SSL_CBC_RECORD_SPLITTING_DISABLED + */ +void ssl_set_cbc_record_splitting( ssl_context *ssl, char split ); +#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) /** * \brief Enable / Disable session tickets @@ -1431,7 +1598,7 @@ int ssl_set_truncated_hmac( ssl_context *ssl, int truncate ); * \param use_tickets Enable or disable (SSL_SESSION_TICKETS_ENABLED or * SSL_SESSION_TICKETS_DISABLED) * - * \return O if successful, + * \return 0 if successful, * or a specific error code (server only). */ int ssl_set_session_tickets( ssl_context *ssl, int use_tickets ); @@ -1446,6 +1613,7 @@ int ssl_set_session_tickets( ssl_context *ssl, int use_tickets ); void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime ); #endif /* POLARSSL_SSL_SESSION_TICKETS */ +#if defined(POLARSSL_SSL_RENEGOTIATION) /** * \brief Enable / Disable renegotiation support for connection when * initiated by peer @@ -1460,6 +1628,7 @@ void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime ); * SSL_RENEGOTIATION_DISABLED) */ void ssl_set_renegotiation( ssl_context *ssl, int renegotiation ); +#endif /* POLARSSL_SSL_RENEGOTIATION */ /** * \brief Prevent or allow legacy renegotiation. @@ -1490,8 +1659,9 @@ void ssl_set_renegotiation( ssl_context *ssl, int renegotiation ); */ void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy ); +#if defined(POLARSSL_SSL_RENEGOTIATION) /** - * \brief Enforce server-requested renegotiation. + * \brief Enforce requested renegotiation. * (Default: enforced, max_records = 16) * * When we request a renegotiation, the peer can comply or @@ -1520,6 +1690,27 @@ void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy ); */ void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records ); +/** + * \brief Set record counter threshold for periodic renegotiation. + * (Default: 2^64 - 256.) + * + * Renegotiation is automatically triggered when a record + * counter (outgoing or ingoing) crosses the defined + * threshold. The default value is meant to prevent the + * connection from being closed when the counter is about to + * reached its maximal value (it is not allowed to wrap). + * + * Lower values can be used to enforce policies such as "keys + * must be refreshed every N packets with cipher X". + * + * \param ssl SSL context + * \param period The threshold value: a big-endian 64-bit number. + * Set to 2^64 - 1 to disable periodic renegotiation + */ +void ssl_set_renegotiation_period( ssl_context *ssl, + const unsigned char period[8] ); +#endif /* POLARSSL_SSL_RENEGOTIATION */ + /** * \brief Return the number of data bytes available to read * @@ -1534,11 +1725,11 @@ size_t ssl_get_bytes_avail( const ssl_context *ssl ); * * \param ssl SSL context * - * \return 0 if successful, or a combination of: - * BADCERT_EXPIRED - * BADCERT_REVOKED - * BADCERT_CN_MISMATCH - * BADCERT_NOT_TRUSTED + * \return 0 if successful, + * -1 if result is not available (eg because the handshake was + * aborted too early), or + * a combination of BADCERT_xxx and BADCRL_xxx flags, see + * x509.h */ int ssl_get_verify_result( const ssl_context *ssl ); @@ -1578,6 +1769,7 @@ const char *ssl_get_version( const ssl_context *ssl ); const x509_crt *ssl_get_peer_cert( const ssl_context *ssl ); #endif /* POLARSSL_X509_CRT_PARSE_C */ +#if defined(POLARSSL_SSL_CLI_C) /** * \brief Save session in order to resume it later (client-side only) * Session data is copied to presented session structure. @@ -1595,6 +1787,7 @@ const x509_crt *ssl_get_peer_cert( const ssl_context *ssl ); * \sa ssl_set_session() */ int ssl_get_session( const ssl_context *ssl, ssl_session *session ); +#endif /* POLARSSL_SSL_CLI_C */ /** * \brief Perform the SSL handshake @@ -1620,6 +1813,7 @@ int ssl_handshake( ssl_context *ssl ); */ int ssl_handshake_step( ssl_context *ssl ); +#if defined(POLARSSL_SSL_RENEGOTIATION) /** * \brief Initiate an SSL renegotiation on the running connection. * Client: perform the renegotiation right now. @@ -1631,6 +1825,7 @@ int ssl_handshake_step( ssl_context *ssl ); * \return 0 if successful, or any ssl_handshake() return value. */ int ssl_renegotiate( ssl_context *ssl ); +#endif /* POLARSSL_SSL_RENEGOTIATION */ /** * \brief Read at most 'len' application data bytes @@ -1657,6 +1852,10 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ); * \note When this function returns POLARSSL_ERR_NET_WANT_WRITE, * it must be called later with the *same* arguments, * until it returns a positive value. + * + * \note This function may write less than the number of bytes + * requested if len is greater than the maximum record length. + * For arbitrary-sized messages, it should be called in a loop. */ int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len ); @@ -1790,7 +1989,8 @@ static inline x509_crt *ssl_own_cert( ssl_context *ssl ) */ int ssl_check_cert_usage( const x509_crt *cert, const ssl_ciphersuite_t *ciphersuite, - int cert_endpoint ); + int cert_endpoint, + int *flags ); #endif /* POLARSSL_X509_CRT_PARSE_C */ /* constant-time buffer comparison */ diff --git a/ext/polarssl/include/polarssl/ssl_cache.h b/ext/mbedtls/include/polarssl/ssl_cache.h similarity index 95% rename from ext/polarssl/include/polarssl/ssl_cache.h rename to ext/mbedtls/include/polarssl/ssl_cache.h index 918fb608eb..063d7d7b1e 100644 --- a/ext/polarssl/include/polarssl/ssl_cache.h +++ b/ext/mbedtls/include/polarssl/ssl_cache.h @@ -3,12 +3,9 @@ * * \brief SSL session cache implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/ssl_ciphersuites.h b/ext/mbedtls/include/polarssl/ssl_ciphersuites.h similarity index 94% rename from ext/polarssl/include/polarssl/ssl_ciphersuites.h rename to ext/mbedtls/include/polarssl/ssl_ciphersuites.h index c4f1ffe644..1fcf4c70da 100644 --- a/ext/polarssl/include/polarssl/ssl_ciphersuites.h +++ b/ext/mbedtls/include/polarssl/ssl_ciphersuites.h @@ -1,14 +1,11 @@ /** * \file ssl_ciphersuites.h * - * \brief SSL Ciphersuites for PolarSSL + * \brief SSL Ciphersuites for mbed TLS * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -233,7 +230,9 @@ extern "C" { #define TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE /**< TLS 1.2 */ #define TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 0xC0AF /**< TLS 1.2 */ -/* Reminder: update _ssl_premaster_secret when adding a new key exchange */ +/* Reminder: update _ssl_premaster_secret when adding a new key exchange. + * Reminder: update POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED below. + */ typedef enum { POLARSSL_KEY_EXCHANGE_NONE = 0, POLARSSL_KEY_EXCHANGE_RSA, @@ -248,6 +247,17 @@ typedef enum { POLARSSL_KEY_EXCHANGE_ECDH_ECDSA, } key_exchange_type_t; +#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ + defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) +#define POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED +#endif + typedef struct _ssl_ciphersuite_t ssl_ciphersuite_t; #define POLARSSL_CIPHERSUITE_WEAK 0x01 /**< Weak ciphersuite flag */ diff --git a/ext/polarssl/include/polarssl/threading.h b/ext/mbedtls/include/polarssl/threading.h similarity index 93% rename from ext/polarssl/include/polarssl/threading.h rename to ext/mbedtls/include/polarssl/threading.h index 1fc9f9826d..293da38c3a 100644 --- a/ext/polarssl/include/polarssl/threading.h +++ b/ext/mbedtls/include/polarssl/threading.h @@ -3,12 +3,9 @@ * * \brief Threading abstraction layer * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/include/polarssl/timing.h b/ext/mbedtls/include/polarssl/timing.h similarity index 87% rename from ext/polarssl/include/polarssl/timing.h rename to ext/mbedtls/include/polarssl/timing.h index 383120efea..2dae5d9c9d 100644 --- a/ext/polarssl/include/polarssl/timing.h +++ b/ext/mbedtls/include/polarssl/timing.h @@ -3,12 +3,9 @@ * * \brief Portable interface to the CPU cycle counter * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -68,6 +65,10 @@ unsigned long get_timer( struct hr_time *val, int reset ); * \brief Setup an alarm clock * * \param seconds delay before the "alarmed" flag is set + * + * \warning Only one alarm at a time is supported. In a threaded + * context, this means one for the whole process, not one per + * thread. */ void set_alarm( int seconds ); diff --git a/ext/polarssl/include/polarssl/version.h b/ext/mbedtls/include/polarssl/version.h similarity index 81% rename from ext/polarssl/include/polarssl/version.h rename to ext/mbedtls/include/polarssl/version.h index b00687f123..f6697770f5 100644 --- a/ext/polarssl/include/polarssl/version.h +++ b/ext/mbedtls/include/polarssl/version.h @@ -3,12 +3,9 @@ * * \brief Run-time version information * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,7 +23,7 @@ */ /* * This set of compile-time defines and run-time variables can be used to - * determine the version number of the PolarSSL library used. + * determine the version number of the mbed TLS library used. */ #ifndef POLARSSL_VERSION_H #define POLARSSL_VERSION_H @@ -43,16 +40,16 @@ */ #define POLARSSL_VERSION_MAJOR 1 #define POLARSSL_VERSION_MINOR 3 -#define POLARSSL_VERSION_PATCH 9 +#define POLARSSL_VERSION_PATCH 12 /** * The single version number has the following structure: * MMNNPP00 * Major version | Minor version | Patch version */ -#define POLARSSL_VERSION_NUMBER 0x01030900 -#define POLARSSL_VERSION_STRING "1.3.9" -#define POLARSSL_VERSION_STRING_FULL "PolarSSL 1.3.9" +#define POLARSSL_VERSION_NUMBER 0x01030C00 +#define POLARSSL_VERSION_STRING "1.3.12" +#define POLARSSL_VERSION_STRING_FULL "mbed TLS 1.3.12" #if defined(POLARSSL_VERSION_C) @@ -77,9 +74,9 @@ unsigned int version_get_number( void ); void version_get_string( char *string ); /** - * Get the full version string ("PolarSSL x.y.z"). + * Get the full version string ("mbed TLS x.y.z"). * - * \param string The string that will receive the value. The PolarSSL version + * \param string The string that will receive the value. The mbed TLS version * string will use 18 bytes AT MOST including a terminating * null byte. * (So the buffer should be at least 18 bytes to receive this @@ -89,12 +86,12 @@ void version_get_string_full( char *string ); /** * \brief Check if support for a feature was compiled into this - * PolarSSL binary. This allows you to see at runtime if the + * mbed TLS binary. This allows you to see at runtime if the * library was for instance compiled with or without * Multi-threading support. * * Note: only checks against defines in the sections "System - * support", "PolarSSL modules" and "PolarSSL feature + * support", "mbed TLS modules" and "mbed TLS feature * support" in config.h * * \param feature The string for the define to check (e.g. "POLARSSL_AES_C") diff --git a/ext/polarssl/include/polarssl/x509.h b/ext/mbedtls/include/polarssl/x509.h similarity index 88% rename from ext/polarssl/include/polarssl/x509.h rename to ext/mbedtls/include/polarssl/x509.h index 9b0bcb78a9..cd01539b99 100644 --- a/ext/polarssl/include/polarssl/x509.h +++ b/ext/mbedtls/include/polarssl/x509.h @@ -3,12 +3,9 @@ * * \brief X.509 generic defines and structures * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,6 +42,18 @@ * \{ */ +#if !defined(POLARSSL_X509_MAX_INTERMEDIATE_CA) +/** + * Maximum number of intermediate CAs in a verification chain. + * That is, maximum length of the chain, excluding the end-entity certificate + * and the trusted root certificate. + * + * Set this to a low value to prevent an adversary from making you waste + * resources verifying an overlong certificate chain. + */ +#define POLARSSL_X509_MAX_INTERMEDIATE_CA 8 +#endif + /** * \name X509 Error codes * \{ @@ -73,17 +82,21 @@ * \name X509 Verify codes * \{ */ +/* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */ #define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */ #define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */ #define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */ #define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */ -#define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */ -#define BADCRL_EXPIRED 0x20 /**< CRL is expired. */ +#define BADCRL_NOT_TRUSTED 0x10 /**< The CRL is not correctly signed by the trusted CA. */ +#define BADCRL_EXPIRED 0x20 /**< The CRL is expired. */ #define BADCERT_MISSING 0x40 /**< Certificate was missing. */ #define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */ #define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */ #define BADCERT_FUTURE 0x0200 /**< The certificate validity starts in the future. */ #define BADCRL_FUTURE 0x0400 /**< The CRL is from the future */ +#define BADCERT_KEY_USAGE 0x0800 /**< Usage does not match the keyUsage extension. */ +#define BADCERT_EXT_KEY_USAGE 0x1000 /**< Usage does not match the extendedKeyUsage extension. */ +#define BADCERT_NS_CERT_TYPE 0x2000 /**< Usage does not match the nsCertType extension. */ /* \} name */ /* \} addtogroup x509_module */ @@ -216,21 +229,30 @@ int x509_dn_gets( char *buf, size_t size, const x509_name *dn ); */ int x509_serial_gets( char *buf, size_t size, const x509_buf *serial ); +#if ! defined(POLARSSL_DEPRECATED_REMOVED) +#if defined(POLARSSL_DEPRECATED_WARNING) +#define DEPRECATED __attribute__((deprecated)) +#else +#define DEPRECATED +#endif /** * \brief Give an known OID, return its descriptive string. - * (Deprecated. Use oid_get_extended_key_usage() instead.) - * Warning: only works for extended_key_usage OIDs! + * + * \deprecated Use oid_get_extended_key_usage() instead. + * + * \warning Only works for extended_key_usage OIDs! * * \param oid buffer containing the oid * * \return Return a string if the OID is known, * or NULL otherwise. */ -const char *x509_oid_get_description( x509_buf *oid ); +const char *x509_oid_get_description( x509_buf *oid ) DEPRECATED; /** * \brief Give an OID, return a string version of its OID number. - * (Deprecated. Use oid_get_numeric_string() instead) + * + * \deprecated Use oid_get_numeric_string() instead. * * \param buf Buffer to write to * \param size Maximum size of buffer @@ -239,7 +261,9 @@ const char *x509_oid_get_description( x509_buf *oid ); * \return Length of the string written (excluding final NULL) or * POLARSSL_ERR_OID_BUF_TO_SMALL in case of error */ -int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid ); +int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid ) DEPRECATED; +#undef DEPRECATED +#endif /* POLARSSL_DEPRECATED_REMOVED */ /** * \brief Check a given x509_time against the system time and check @@ -295,7 +319,6 @@ int x509_get_serial( unsigned char **p, const unsigned char *end, x509_buf *serial ); int x509_get_ext( unsigned char **p, const unsigned char *end, x509_buf *ext, int tag ); -int x509_load_file( const char *path, unsigned char **buf, size_t *n ); int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, pk_type_t pk_alg, md_type_t md_alg, const void *sig_opts ); diff --git a/ext/polarssl/include/polarssl/x509_crl.h b/ext/mbedtls/include/polarssl/x509_crl.h similarity index 80% rename from ext/polarssl/include/polarssl/x509_crl.h rename to ext/mbedtls/include/polarssl/x509_crl.h index 9f597a855a..6f4b65d56f 100644 --- a/ext/polarssl/include/polarssl/x509_crl.h +++ b/ext/mbedtls/include/polarssl/x509_crl.h @@ -3,12 +3,9 @@ * * \brief X.509 certificate revocation list parsing * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -100,11 +97,23 @@ typedef struct _x509_crl x509_crl; /** - * \brief Parse one or more CRLs and add them - * to the chained list + * \brief Parse a DER-encoded CRL and append it to the chained list + * + * \param chain points to the start of the chain + * \param buf buffer holding the CRL data in DER format + * \param buflen size of the buffer + * + * \return 0 if successful, or a specific X509 or PEM error code + */ +int x509_crl_parse_der( x509_crl *chain, + const unsigned char *buf, size_t buflen ); +/** + * \brief Parse one or more CRLs and append them to the chained list + * + * \note Mutliple CRLs are accepted only if using PEM format * * \param chain points to the start of the chain - * \param buf buffer holding the CRL data + * \param buf buffer holding the CRL data in PEM or DER format * \param buflen size of the buffer * * \return 0 if successful, or a specific X509 or PEM error code @@ -113,11 +122,12 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ); #if defined(POLARSSL_FS_IO) /** - * \brief Load one or more CRLs and add them - * to the chained list + * \brief Load one or more CRLs and append them to the chained list + * + * \note Mutliple CRLs are accepted only if using PEM format * * \param chain points to the start of the chain - * \param path filename to read the CRLs from + * \param path filename to read the CRLs from (in PEM or DER encoding) * * \return 0 if successful, or a specific X509 or PEM error code */ diff --git a/ext/polarssl/include/polarssl/x509_crt.h b/ext/mbedtls/include/polarssl/x509_crt.h similarity index 95% rename from ext/polarssl/include/polarssl/x509_crt.h rename to ext/mbedtls/include/polarssl/x509_crt.h index 4bf8e568f1..24f7c7ab6b 100644 --- a/ext/polarssl/include/polarssl/x509_crt.h +++ b/ext/mbedtls/include/polarssl/x509_crt.h @@ -3,12 +3,9 @@ * * \brief X.509 certificate parsing and writing * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,7 +31,6 @@ #endif #include "x509.h" - #include "x509_crl.h" /** @@ -206,6 +202,21 @@ int x509_crt_parse_path( x509_crt *chain, const char *path ); int x509_crt_info( char *buf, size_t size, const char *prefix, const x509_crt *crt ); +/** + * \brief Returns an informational string about the + * verification status of a certificate. + * + * \param buf Buffer to write to + * \param size Maximum size of buffer + * \param prefix A line prefix + * \param flags Verification flags created by x509_crt_verify() + * + * \return The amount of data written to the buffer, or -1 in + * case of an error. + */ +int x509_crt_verify_info( char *buf, size_t size, const char *prefix, + int flags ); + /** * \brief Verify the certificate signature * @@ -223,6 +234,9 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, * are also returned to the application. The function should * return 0 for anything but a fatal error. * + * \note In case verification failed, the results can be displayed + * using \c x509_crt_verify_info() + * * \param crt a certificate to be verified * \param trust_ca the trusted CA chain * \param ca_crl the CRL chain for trusted CA's @@ -233,12 +247,8 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, * \param p_vrfy verification parameter * * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED, - * in which case *flags will have one or more of - * the following values set: - * BADCERT_EXPIRED -- - * BADCERT_REVOKED -- - * BADCERT_CN_MISMATCH -- - * BADCERT_NOT_TRUSTED + * in which case *flags will have one or more BADCERT_XXX or + * BADCRL_XXX flags set, * or another error in case of a fatal error encountered * during the verification process. */ @@ -367,7 +377,7 @@ int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before, * \brief Set the issuer name for a Certificate * Issuer names should contain a comma-separated list * of OID types and values: - * e.g. "C=NL,O=Offspark,CN=PolarSSL CA" + * e.g. "C=UK,O=ARM,CN=mbed TLS CA" * * \param ctx CRT context to use * \param issuer_name issuer name to set @@ -382,7 +392,7 @@ int x509write_crt_set_issuer_name( x509write_cert *ctx, * \brief Set the subject name for a Certificate * Subject names should contain a comma-separated list * of OID types and values: - * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1" + * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1" * * \param ctx CRT context to use * \param subject_name subject name to set diff --git a/ext/polarssl/include/polarssl/x509_csr.h b/ext/mbedtls/include/polarssl/x509_csr.h similarity index 97% rename from ext/polarssl/include/polarssl/x509_csr.h rename to ext/mbedtls/include/polarssl/x509_csr.h index 6591e382eb..34f527b8e4 100644 --- a/ext/polarssl/include/polarssl/x509_csr.h +++ b/ext/mbedtls/include/polarssl/x509_csr.h @@ -3,12 +3,9 @@ * * \brief X.509 certificate signing request parsing and writing * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -164,7 +161,7 @@ void x509write_csr_init( x509write_csr *ctx ); * \brief Set the subject name for a CSR * Subject names should contain a comma-separated list * of OID types and values: - * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1" + * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1" * * \param ctx CSR context to use * \param subject_name subject name to set diff --git a/ext/polarssl/include/polarssl/xtea.h b/ext/mbedtls/include/polarssl/xtea.h similarity index 93% rename from ext/polarssl/include/polarssl/xtea.h rename to ext/mbedtls/include/polarssl/xtea.h index 794c5efa31..02cd8d6f0a 100644 --- a/ext/polarssl/include/polarssl/xtea.h +++ b/ext/mbedtls/include/polarssl/xtea.h @@ -3,12 +3,9 @@ * * \brief XTEA block cipher (32-bit) * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +30,7 @@ #include POLARSSL_CONFIG_FILE #endif -#include +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include diff --git a/ext/mbedtls/library/.gitignore b/ext/mbedtls/library/.gitignore new file mode 100644 index 0000000000..f749c34590 --- /dev/null +++ b/ext/mbedtls/library/.gitignore @@ -0,0 +1,6 @@ +*.o +libpolarssl.* +libmbedtls.* +*.sln +*.vcxproj +Makefile.am \ No newline at end of file diff --git a/ext/mbedtls/library/CMakeLists.txt b/ext/mbedtls/library/CMakeLists.txt new file mode 100644 index 0000000000..9cbac7b018 --- /dev/null +++ b/ext/mbedtls/library/CMakeLists.txt @@ -0,0 +1,156 @@ +option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON) +option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF) +option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF) + +set(src + aes.c + aesni.c + arc4.c + asn1parse.c + asn1write.c + base64.c + bignum.c + blowfish.c + camellia.c + ccm.c + certs.c + cipher.c + cipher_wrap.c + ctr_drbg.c + debug.c + des.c + dhm.c + ecp.c + ecp_curves.c + ecdh.c + ecdsa.c + entropy.c + entropy_poll.c + error.c + gcm.c + havege.c + hmac_drbg.c + md.c + md_wrap.c + md2.c + md4.c + md5.c + memory_buffer_alloc.c + net.c + oid.c + padlock.c + pbkdf2.c + pem.c + pkcs5.c + pkcs11.c + pkcs12.c + pk.c + pk_wrap.c + pkparse.c + pkwrite.c + platform.c + ripemd160.c + rsa.c + sha1.c + sha256.c + sha512.c + ssl_cache.c + ssl_ciphersuites.c + ssl_cli.c + ssl_srv.c + ssl_tls.c + threading.c + timing.c + version.c + version_features.c + x509.c + x509_crt.c + x509_crl.c + x509_csr.c + x509_create.c + x509write_crt.c + x509write_csr.c + xtea.c +) + +if(WIN32) +set(libs ws2_32) +endif(WIN32) + +if(CMAKE_COMPILER_IS_GNUCC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes") +endif(CMAKE_COMPILER_IS_GNUCC) + +if(CMAKE_COMPILER_IS_CLANG) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") +endif(CMAKE_COMPILER_IS_CLANG) + +if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY) + message(FATAL_ERROR "Need to choose static or shared mbedtls build!") +endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY) + +if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY) + # if we build both static an shared, then let + # tests and programs link to the shared lib target + set(mbedtls_static_target "mbedtls_static") +elseif(USE_STATIC_MBEDTLS_LIBRARY) + set(mbedtls_static_target "mbedtls") +endif() + +if(USE_STATIC_MBEDTLS_LIBRARY) + add_library(${mbedtls_static_target} STATIC ${src}) + set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls) + target_link_libraries(${mbedtls_static_target} ${libs}) + + if(ZLIB_FOUND) + target_link_libraries(${mbedtls_static_target} ${ZLIB_LIBRARIES}) + endif(ZLIB_FOUND) + + if(LINK_WITH_PTHREAD) + target_link_libraries(${mbedtls_static_target} pthread) + endif() + + install(TARGETS ${mbedtls_static_target} + DESTINATION ${LIB_INSTALL_DIR} + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) +endif() + +if(USE_SHARED_MBEDTLS_LIBRARY) + add_library(mbedtls SHARED ${src}) + set_target_properties(mbedtls PROPERTIES VERSION 1.3.12 SOVERSION 9) + + target_link_libraries(mbedtls ${libs}) + + if(ZLIB_FOUND) + target_link_libraries(mbedtls ${ZLIB_LIBRARIES}) + endif(ZLIB_FOUND) + + if(LINK_WITH_PTHREAD) + target_link_libraries(mbedtls pthread) + endif() + + install(TARGETS mbedtls + DESTINATION ${LIB_INSTALL_DIR} + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) +endif(USE_SHARED_MBEDTLS_LIBRARY) + +if(UNIX) + add_custom_target(polarssl + DEPENDS mbedtls # TODO: and mbedtls_static is shared is defined + COMMAND ${CMAKE_SOURCE_DIR}/scripts/polarssl_symlinks.sh ${CMAKE_BINARY_DIR}/library + ) + + if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY) + add_dependencies(polarssl mbedtls_static) + endif() + + add_custom_target(polarssl-clean + COMMAND make clean + COMMAND rm -f ${CMAKE_BINARY_DIR}/library/libpolarssl.* + ) + + add_custom_target(polarssl-install + COMMAND make install + COMMAND ${CMAKE_SOURCE_DIR}/scripts/polarssl_symlinks.sh ${DESTDIR}/${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR} + ) +endif(UNIX) diff --git a/ext/polarssl/library/Makefile.am b/ext/mbedtls/library/Makefile.am similarity index 100% rename from ext/polarssl/library/Makefile.am rename to ext/mbedtls/library/Makefile.am diff --git a/ext/polarssl/library/aes.c b/ext/mbedtls/library/aes.c similarity index 98% rename from ext/polarssl/library/aes.c rename to ext/mbedtls/library/aes.c index f295747c59..9d96064ffe 100644 --- a/ext/polarssl/library/aes.c +++ b/ext/mbedtls/library/aes.c @@ -1,12 +1,9 @@ /* * FIPS-197 compliant AES implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,6 +34,8 @@ #if defined(POLARSSL_AES_C) +#include + #include "polarssl/aes.h" #if defined(POLARSSL_PADLOCK_C) #include "polarssl/padlock.h" @@ -45,11 +44,14 @@ #include "polarssl/aesni.h" #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ #if !defined(POLARSSL_AES_ALT) @@ -72,12 +74,12 @@ static void polarssl_zeroize( void *v, size_t n ) { #endif #ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ +#define PUT_UINT32_LE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ } #endif @@ -929,7 +931,6 @@ int aes_crypt_cfb128( aes_context *ctx, /* * AES-CFB8 buffer encryption/decryption */ -#include int aes_crypt_cfb8( aes_context *ctx, int mode, size_t length, @@ -999,9 +1000,6 @@ int aes_crypt_ctr( aes_context *ctx, #endif /* !POLARSSL_AES_ALT */ #if defined(POLARSSL_SELF_TEST) - -#include - /* * AES test vectors from: * diff --git a/ext/polarssl/library/aesni.c b/ext/mbedtls/library/aesni.c similarity index 98% rename from ext/polarssl/library/aesni.c rename to ext/mbedtls/library/aesni.c index 97f646ea4f..5315aba223 100644 --- a/ext/polarssl/library/aesni.c +++ b/ext/mbedtls/library/aesni.c @@ -1,12 +1,9 @@ /* * AES-NI support functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,7 +34,8 @@ #if defined(POLARSSL_AESNI_C) #include "polarssl/aesni.h" -#include + +#include #if defined(POLARSSL_HAVE_X86_64) diff --git a/ext/polarssl/library/arc4.c b/ext/mbedtls/library/arc4.c similarity index 95% rename from ext/polarssl/library/arc4.c rename to ext/mbedtls/library/arc4.c index 54e89ea888..309d16a85d 100644 --- a/ext/polarssl/library/arc4.c +++ b/ext/mbedtls/library/arc4.c @@ -1,12 +1,9 @@ /* * An implementation of the ARCFOUR algorithm * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,11 +35,16 @@ #include "polarssl/arc4.h" +#include + +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ #if !defined(POLARSSL_ARC4_ALT) @@ -129,10 +131,6 @@ int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input, #endif /* !POLARSSL_ARC4_ALT */ #if defined(POLARSSL_SELF_TEST) - -#include -#include - /* * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: * diff --git a/ext/polarssl/library/asn1parse.c b/ext/mbedtls/library/asn1parse.c similarity index 94% rename from ext/polarssl/library/asn1parse.c rename to ext/mbedtls/library/asn1parse.c index a3a2b56a2b..e4f46eb0a3 100644 --- a/ext/polarssl/library/asn1parse.c +++ b/ext/mbedtls/library/asn1parse.c @@ -1,12 +1,9 @@ /* * Generic ASN.1 parsing * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +30,8 @@ #include "polarssl/asn1.h" +#include + #if defined(POLARSSL_BIGNUM_C) #include "polarssl/bignum.h" #endif @@ -40,13 +39,11 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include -#include - /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -80,7 +77,7 @@ int asn1_get_len( unsigned char **p, if( ( end - *p ) < 3 ) return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); - *len = ( (*p)[1] << 8 ) | (*p)[2]; + *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2]; (*p) += 3; break; @@ -88,7 +85,8 @@ int asn1_get_len( unsigned char **p, if( ( end - *p ) < 4 ) return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); - *len = ( (*p)[1] << 16 ) | ( (*p)[2] << 8 ) | (*p)[3]; + *len = ( (size_t)(*p)[1] << 16 ) | + ( (size_t)(*p)[2] << 8 ) | (*p)[3]; (*p) += 4; break; @@ -96,8 +94,8 @@ int asn1_get_len( unsigned char **p, if( ( end - *p ) < 5 ) return( POLARSSL_ERR_ASN1_OUT_OF_DATA ); - *len = ( (*p)[1] << 24 ) | ( (*p)[2] << 16 ) | ( (*p)[3] << 8 ) | - (*p)[4]; + *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) | + ( (size_t)(*p)[3] << 8 ) | (*p)[4]; (*p) += 5; break; @@ -272,12 +270,13 @@ int asn1_get_sequence_of( unsigned char **p, /* Allocate and assign next pointer */ if( *p < end ) { - cur->next = (asn1_sequence *) polarssl_malloc( - sizeof( asn1_sequence ) ); + cur->next = polarssl_malloc( sizeof( asn1_sequence ) ); if( cur->next == NULL ) return( POLARSSL_ERR_ASN1_MALLOC_FAILED ); + memset( cur->next, 0, sizeof( asn1_sequence ) ); + cur = cur->next; } } diff --git a/ext/polarssl/library/asn1write.c b/ext/mbedtls/library/asn1write.c similarity index 97% rename from ext/polarssl/library/asn1write.c rename to ext/mbedtls/library/asn1write.c index ebc0e97660..df9442ef85 100644 --- a/ext/polarssl/library/asn1write.c +++ b/ext/mbedtls/library/asn1write.c @@ -1,12 +1,9 @@ /* * ASN.1 buffer writing functionality * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +30,8 @@ #include "polarssl/asn1write.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else @@ -327,6 +326,8 @@ asn1_named_data *asn1_store_named_data( asn1_named_data **head, return( NULL ); } + memcpy( cur->oid.p, oid, oid_len ); + cur->val.len = val_len; cur->val.p = polarssl_malloc( val_len ); if( cur->val.p == NULL ) @@ -336,8 +337,6 @@ asn1_named_data *asn1_store_named_data( asn1_named_data **head, return( NULL ); } - memcpy( cur->oid.p, oid, oid_len ); - cur->next = *head; *head = cur; } diff --git a/ext/polarssl/library/base64.c b/ext/mbedtls/library/base64.c similarity index 96% rename from ext/polarssl/library/base64.c rename to ext/mbedtls/library/base64.c index e6c3569b89..ac922a4744 100644 --- a/ext/polarssl/library/base64.c +++ b/ext/mbedtls/library/base64.c @@ -1,12 +1,9 @@ /* * RFC 1521 base64 encoding/decoding * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,11 +37,15 @@ typedef UINT32 uint32_t; #include #endif +#if defined(POLARSSL_SELF_TEST) +#include #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ static const unsigned char base64_enc_map[64] = { @@ -85,7 +86,10 @@ int base64_encode( unsigned char *dst, size_t *dlen, unsigned char *p; if( slen == 0 ) + { + *dlen = 0; return( 0 ); + } n = ( slen << 3 ) / 6; @@ -221,9 +225,6 @@ int base64_decode( unsigned char *dst, size_t *dlen, #if defined(POLARSSL_SELF_TEST) -#include -#include - static const unsigned char base64_test_dec[64] = { 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD, diff --git a/ext/polarssl/library/bignum.c b/ext/mbedtls/library/bignum.c similarity index 97% rename from ext/polarssl/library/bignum.c rename to ext/mbedtls/library/bignum.c index 448e743d4c..f479bc9edc 100644 --- a/ext/polarssl/library/bignum.c +++ b/ext/mbedtls/library/bignum.c @@ -1,12 +1,9 @@ /* * Multi-precision integer library * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,16 +38,18 @@ #include "polarssl/bignum.h" #include "polarssl/bn_mul.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include +#include #define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free #endif -#include - /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -110,7 +109,7 @@ int mpi_grow( mpi *X, size_t nblimbs ) if( X->n < nblimbs ) { - if( ( p = (t_uint *) polarssl_malloc( nblimbs * ciL ) ) == NULL ) + if( ( p = polarssl_malloc( nblimbs * ciL ) ) == NULL ) return( POLARSSL_ERR_MPI_MALLOC_FAILED ); memset( p, 0, nblimbs * ciL ); @@ -150,7 +149,7 @@ int mpi_shrink( mpi *X, size_t nblimbs ) if( i < nblimbs ) i = nblimbs; - if( ( p = (t_uint *) polarssl_malloc( i * ciL ) ) == NULL ) + if( ( p = polarssl_malloc( i * ciL ) ) == NULL ) return( POLARSSL_ERR_MPI_MALLOC_FAILED ); memset( p, 0, i * ciL ); @@ -224,8 +223,8 @@ int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign ) int ret = 0; size_t i; - /* make sure assign is 0 or 1 */ - assign = ( assign != 0 ); + /* make sure assign is 0 or 1 in a time-constant manner */ + assign = (assign | (unsigned char)-assign) >> 7; MPI_CHK( mpi_grow( X, Y->n ) ); @@ -256,8 +255,8 @@ int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char swap ) if( X == Y ) return( 0 ); - /* make sure swap is 0 or 1 */ - swap = ( swap != 0 ); + /* make sure swap is 0 or 1 in a time-constant manner */ + swap = (swap | (unsigned char)-swap) >> 7; MPI_CHK( mpi_grow( X, Y->n ) ); MPI_CHK( mpi_grow( Y, X->n ) ); @@ -357,6 +356,9 @@ size_t mpi_msb( const mpi *X ) { size_t i, j; + if( X->n == 0 ) + return( 0 ); + for( i = X->n - 1; i > 0; i-- ) if( X->p[i] != 0 ) break; @@ -1241,17 +1243,7 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ) Z.p[i - t - 1] = ~0; else { - /* - * The version of Clang shipped by Apple with Mavericks around - * 2014-03 can't handle 128-bit division properly. Disable - * 128-bits division for this version. Let's be optimistic and - * assume it'll be fixed in the next minor version (next - * patchlevel is probably a bit too optimistic). - */ -#if defined(POLARSSL_HAVE_UDBL) && \ - ! ( defined(__x86_64__) && defined(__APPLE__) && \ - defined(__clang_major__) && __clang_major__ == 5 && \ - defined(__clang_minor__) && __clang_minor__ == 0 ) +#if defined(POLARSSL_HAVE_UDBL) t_udbl r; r = (t_udbl) X.p[i] << biL; @@ -1969,8 +1961,8 @@ static int mpi_miller_rabin( const mpi *X, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) { - int ret; - size_t i, j, n, s; + int ret, count; + size_t i, j, k, n, s; mpi W, R, T, A, RR; mpi_init( &W ); mpi_init( &R ); mpi_init( &T ); mpi_init( &A ); @@ -1998,14 +1990,23 @@ static int mpi_miller_rabin( const mpi *X, /* * pick a random A, 1 < A < |X| - 1 */ - MPI_CHK( mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); - if( mpi_cmp_mpi( &A, &W ) >= 0 ) - { - j = mpi_msb( &A ) - mpi_msb( &W ); - MPI_CHK( mpi_shift_r( &A, j + 1 ) ); - } - A.p[0] |= 3; + count = 0; + do { + MPI_CHK( mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); + + j = mpi_msb( &A ); + k = mpi_msb( &W ); + if (j > k) { + MPI_CHK( mpi_shift_r( &A, j - k ) ); + } + + if (count++ > 30) { + return POLARSSL_ERR_MPI_NOT_ACCEPTABLE; + } + + } while ( (mpi_cmp_mpi( &A, &W ) >= 0) || + (mpi_cmp_int( &A, 1 ) <= 0) ); /* * A = A^R mod |X| @@ -2103,10 +2104,11 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, MPI_CHK( mpi_fill_random( X, n * ciL, f_rng, p_rng ) ); k = mpi_msb( X ); - if( k < nbits ) MPI_CHK( mpi_shift_l( X, nbits - k ) ); - if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits ) ); + if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits + 1 ) ); + + mpi_set_bit( X, nbits-1, 1 ); - X->p[0] |= 3; + X->p[0] |= 1; if( dh_flag == 0 ) { @@ -2125,6 +2127,9 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3). * Make sure it is satisfied, while keeping X = 3 mod 4 */ + + X->p[0] |= 2; + MPI_CHK( mpi_mod_int( &r, X, 3 ) ); if( r == 0 ) MPI_CHK( mpi_add_int( X, X, 8 ) ); diff --git a/ext/polarssl/library/blowfish.c b/ext/mbedtls/library/blowfish.c similarity index 99% rename from ext/polarssl/library/blowfish.c rename to ext/mbedtls/library/blowfish.c index 87396dc22a..77191e7eaa 100644 --- a/ext/polarssl/library/blowfish.c +++ b/ext/mbedtls/library/blowfish.c @@ -1,12 +1,9 @@ /* * Blowfish implementation * - * Copyright (C) 2012-2014, Brainspark B.V. + * Copyright (C) 2012-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +36,8 @@ #include "polarssl/blowfish.h" +#include + #if !defined(POLARSSL_BLOWFISH_ALT) /* Implementation that should never be optimized out by the compiler */ diff --git a/ext/polarssl/library/camellia.c b/ext/mbedtls/library/camellia.c similarity index 97% rename from ext/polarssl/library/camellia.c rename to ext/mbedtls/library/camellia.c index a4968f411e..d80f51f319 100644 --- a/ext/polarssl/library/camellia.c +++ b/ext/mbedtls/library/camellia.c @@ -1,12 +1,9 @@ /* * Camellia implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,11 +36,15 @@ #include "polarssl/camellia.h" +#if defined(POLARSSL_SELF_TEST) +#include #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ #if !defined(POLARSSL_CAMELLIA_ALT) @@ -304,14 +305,14 @@ static void camellia_feistel( const uint32_t x[2], const uint32_t k[2], I0 = x[0] ^ k[0]; I1 = x[1] ^ k[1]; - I0 = (SBOX1((I0 >> 24) & 0xFF) << 24) | - (SBOX2((I0 >> 16) & 0xFF) << 16) | - (SBOX3((I0 >> 8) & 0xFF) << 8) | - (SBOX4((I0 ) & 0xFF) ); - I1 = (SBOX2((I1 >> 24) & 0xFF) << 24) | - (SBOX3((I1 >> 16) & 0xFF) << 16) | - (SBOX4((I1 >> 8) & 0xFF) << 8) | - (SBOX1((I1 ) & 0xFF) ); + I0 = ((uint32_t) SBOX1((I0 >> 24) & 0xFF) << 24) | + ((uint32_t) SBOX2((I0 >> 16) & 0xFF) << 16) | + ((uint32_t) SBOX3((I0 >> 8) & 0xFF) << 8) | + ((uint32_t) SBOX4((I0 ) & 0xFF) ); + I1 = ((uint32_t) SBOX2((I1 >> 24) & 0xFF) << 24) | + ((uint32_t) SBOX3((I1 >> 16) & 0xFF) << 16) | + ((uint32_t) SBOX4((I1 >> 8) & 0xFF) << 8) | + ((uint32_t) SBOX1((I1 ) & 0xFF) ); I0 ^= (I1 << 8) | (I1 >> 24); I1 ^= (I0 << 16) | (I0 >> 16); @@ -455,7 +456,7 @@ int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, camellia_init( &cty ); /* Also checks keysize */ - if( ( ret = camellia_setkey_enc( &cty, key, keysize ) ) ) + if( ( ret = camellia_setkey_enc( &cty, key, keysize ) ) != 0 ) goto exit; ctx->nr = cty.nr; @@ -692,8 +693,6 @@ int camellia_crypt_ctr( camellia_context *ctx, #if defined(POLARSSL_SELF_TEST) -#include - /* * Camellia test vectors from: * diff --git a/ext/polarssl/library/ccm.c b/ext/mbedtls/library/ccm.c similarity index 97% rename from ext/polarssl/library/ccm.c rename to ext/mbedtls/library/ccm.c index 72d766b9a5..e397e0a428 100644 --- a/ext/polarssl/library/ccm.c +++ b/ext/mbedtls/library/ccm.c @@ -1,12 +1,9 @@ /* * NIST SP800-38C compliant CCM implementation * - * Copyright (C) 2014, Brainspark B.V. + * Copyright (C) 2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,6 +39,17 @@ #include "polarssl/ccm.h" +#include + +#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#include +#define polarssl_printf printf +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */ + /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -70,6 +78,8 @@ int ccm_init( ccm_context *ctx, cipher_id_t cipher, if( cipher_info->block_size != 16 ) return( POLARSSL_ERR_CCM_BAD_INPUT ); + cipher_free( &ctx->cipher_ctx ); + if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 ) return( ret ); @@ -336,14 +346,6 @@ int ccm_auth_decrypt( ccm_context *ctx, size_t length, #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) - -#if defined(POLARSSL_PLATFORM_C) -#include "polarssl/platform.h" -#else -#include -#define polarssl_printf printf -#endif - /* * Examples 1 to 3 from SP800-38C Appendix C */ diff --git a/ext/polarssl/library/certs.c b/ext/mbedtls/library/certs.c similarity index 98% rename from ext/polarssl/library/certs.c rename to ext/mbedtls/library/certs.c index a782bc1144..95fc8cc77d 100644 --- a/ext/polarssl/library/certs.c +++ b/ext/mbedtls/library/certs.c @@ -1,12 +1,9 @@ /* * X.509 test certificates * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/library/cipher.c b/ext/mbedtls/library/cipher.c similarity index 98% rename from ext/polarssl/library/cipher.c rename to ext/mbedtls/library/cipher.c index 5cd30f8ad9..b69d331060 100644 --- a/ext/polarssl/library/cipher.c +++ b/ext/mbedtls/library/cipher.c @@ -1,16 +1,13 @@ /** * \file cipher.c * - * \brief Generic cipher wrapper for PolarSSL + * \brief Generic cipher wrapper for mbed TLS * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +35,9 @@ #include "polarssl/cipher.h" #include "polarssl/cipher_wrap.h" +#include +#include + #if defined(POLARSSL_GCM_C) #include "polarssl/gcm.h" #endif @@ -46,8 +46,6 @@ #include "polarssl/ccm.h" #endif -#include - #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) #define POLARSSL_CIPHER_MODE_STREAM #endif @@ -167,13 +165,14 @@ int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ) return( 0 ); } -/* Deprecated, redirects to cipher_free() */ +#if ! defined(POLARSSL_DEPRECATED_REMOVED) int cipher_free_ctx( cipher_context_t *ctx ) { cipher_free( ctx ); return( 0 ); } +#endif int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation ) diff --git a/ext/polarssl/library/cipher_wrap.c b/ext/mbedtls/library/cipher_wrap.c similarity index 83% rename from ext/polarssl/library/cipher_wrap.c rename to ext/mbedtls/library/cipher_wrap.c index ab2f9bc6ee..4c170bc377 100644 --- a/ext/polarssl/library/cipher_wrap.c +++ b/ext/mbedtls/library/cipher_wrap.c @@ -1,16 +1,13 @@ /** * \file cipher_wrap.c * - * \brief Generic cipher wrapper for PolarSSL + * \brief Generic cipher wrapper for mbed TLS * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -65,15 +62,18 @@ #include "polarssl/ccm.h" #endif +#if defined(POLARSSL_CIPHER_NULL_CIPHER) +#include +#endif + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include - #if defined(POLARSSL_GCM_C) /* shared by all GCM ciphers */ static void *gcm_ctx_alloc( void ) @@ -110,63 +110,34 @@ static int aes_crypt_ecb_wrap( void *ctx, operation_t operation, return aes_crypt_ecb( (aes_context *) ctx, operation, input, output ); } +#if defined(POLARSSL_CIPHER_MODE_CBC) static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CBC) return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CBC */ } +#endif /* POLARSSL_CIPHER_MODE_CBC */ +#if defined(POLARSSL_CIPHER_MODE_CFB) static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length, size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CFB) return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv_off); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CFB */ } +#endif /* POLARSSL_CIPHER_MODE_CFB */ +#if defined(POLARSSL_CIPHER_MODE_CTR) static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CTR) return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter, stream_block, input, output ); -#else - ((void) ctx); - ((void) length); - ((void) nc_off); - ((void) nonce_counter); - ((void) stream_block); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CTR */ } +#endif /* POLARSSL_CIPHER_MODE_CTR */ static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) @@ -182,7 +153,7 @@ static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, static void * aes_ctx_alloc( void ) { - aes_context *aes = (aes_context *) polarssl_malloc( sizeof( aes_context ) ); + aes_context *aes = polarssl_malloc( sizeof( aes_context ) ); if( aes == NULL ) return( NULL ); @@ -198,20 +169,28 @@ static void aes_ctx_free( void *ctx ) polarssl_free( ctx ); } -const cipher_base_t aes_info = { +static const cipher_base_t aes_info = { POLARSSL_CIPHER_ID_AES, aes_crypt_ecb_wrap, +#if defined(POLARSSL_CIPHER_MODE_CBC) aes_crypt_cbc_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) aes_crypt_cfb128_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) aes_crypt_ctr_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif aes_setkey_enc_wrap, aes_setkey_dec_wrap, aes_ctx_alloc, aes_ctx_free }; -const cipher_info_t aes_128_ecb_info = { +static const cipher_info_t aes_128_ecb_info = { POLARSSL_CIPHER_AES_128_ECB, POLARSSL_MODE_ECB, 128, @@ -222,7 +201,7 @@ const cipher_info_t aes_128_ecb_info = { &aes_info }; -const cipher_info_t aes_192_ecb_info = { +static const cipher_info_t aes_192_ecb_info = { POLARSSL_CIPHER_AES_192_ECB, POLARSSL_MODE_ECB, 192, @@ -233,7 +212,7 @@ const cipher_info_t aes_192_ecb_info = { &aes_info }; -const cipher_info_t aes_256_ecb_info = { +static const cipher_info_t aes_256_ecb_info = { POLARSSL_CIPHER_AES_256_ECB, POLARSSL_MODE_ECB, 256, @@ -245,7 +224,7 @@ const cipher_info_t aes_256_ecb_info = { }; #if defined(POLARSSL_CIPHER_MODE_CBC) -const cipher_info_t aes_128_cbc_info = { +static const cipher_info_t aes_128_cbc_info = { POLARSSL_CIPHER_AES_128_CBC, POLARSSL_MODE_CBC, 128, @@ -256,7 +235,7 @@ const cipher_info_t aes_128_cbc_info = { &aes_info }; -const cipher_info_t aes_192_cbc_info = { +static const cipher_info_t aes_192_cbc_info = { POLARSSL_CIPHER_AES_192_CBC, POLARSSL_MODE_CBC, 192, @@ -267,7 +246,7 @@ const cipher_info_t aes_192_cbc_info = { &aes_info }; -const cipher_info_t aes_256_cbc_info = { +static const cipher_info_t aes_256_cbc_info = { POLARSSL_CIPHER_AES_256_CBC, POLARSSL_MODE_CBC, 256, @@ -280,7 +259,7 @@ const cipher_info_t aes_256_cbc_info = { #endif /* POLARSSL_CIPHER_MODE_CBC */ #if defined(POLARSSL_CIPHER_MODE_CFB) -const cipher_info_t aes_128_cfb128_info = { +static const cipher_info_t aes_128_cfb128_info = { POLARSSL_CIPHER_AES_128_CFB128, POLARSSL_MODE_CFB, 128, @@ -291,7 +270,7 @@ const cipher_info_t aes_128_cfb128_info = { &aes_info }; -const cipher_info_t aes_192_cfb128_info = { +static const cipher_info_t aes_192_cfb128_info = { POLARSSL_CIPHER_AES_192_CFB128, POLARSSL_MODE_CFB, 192, @@ -302,7 +281,7 @@ const cipher_info_t aes_192_cfb128_info = { &aes_info }; -const cipher_info_t aes_256_cfb128_info = { +static const cipher_info_t aes_256_cfb128_info = { POLARSSL_CIPHER_AES_256_CFB128, POLARSSL_MODE_CFB, 256, @@ -315,7 +294,7 @@ const cipher_info_t aes_256_cfb128_info = { #endif /* POLARSSL_CIPHER_MODE_CFB */ #if defined(POLARSSL_CIPHER_MODE_CTR) -const cipher_info_t aes_128_ctr_info = { +static const cipher_info_t aes_128_ctr_info = { POLARSSL_CIPHER_AES_128_CTR, POLARSSL_MODE_CTR, 128, @@ -326,7 +305,7 @@ const cipher_info_t aes_128_ctr_info = { &aes_info }; -const cipher_info_t aes_192_ctr_info = { +static const cipher_info_t aes_192_ctr_info = { POLARSSL_CIPHER_AES_192_CTR, POLARSSL_MODE_CTR, 192, @@ -337,7 +316,7 @@ const cipher_info_t aes_192_ctr_info = { &aes_info }; -const cipher_info_t aes_256_ctr_info = { +static const cipher_info_t aes_256_ctr_info = { POLARSSL_CIPHER_AES_256_CTR, POLARSSL_MODE_CTR, 256, @@ -357,20 +336,28 @@ static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key, key, key_length ); } -const cipher_base_t gcm_aes_info = { +static const cipher_base_t gcm_aes_info = { POLARSSL_CIPHER_ID_AES, NULL, +#if defined(POLARSSL_CIPHER_MODE_CBC) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif gcm_aes_setkey_wrap, gcm_aes_setkey_wrap, gcm_ctx_alloc, gcm_ctx_free, }; -const cipher_info_t aes_128_gcm_info = { +static const cipher_info_t aes_128_gcm_info = { POLARSSL_CIPHER_AES_128_GCM, POLARSSL_MODE_GCM, 128, @@ -381,7 +368,7 @@ const cipher_info_t aes_128_gcm_info = { &gcm_aes_info }; -const cipher_info_t aes_192_gcm_info = { +static const cipher_info_t aes_192_gcm_info = { POLARSSL_CIPHER_AES_192_GCM, POLARSSL_MODE_GCM, 192, @@ -392,7 +379,7 @@ const cipher_info_t aes_192_gcm_info = { &gcm_aes_info }; -const cipher_info_t aes_256_gcm_info = { +static const cipher_info_t aes_256_gcm_info = { POLARSSL_CIPHER_AES_256_GCM, POLARSSL_MODE_GCM, 256, @@ -412,20 +399,28 @@ static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key, key, key_length ); } -const cipher_base_t ccm_aes_info = { +static const cipher_base_t ccm_aes_info = { POLARSSL_CIPHER_ID_AES, NULL, +#if defined(POLARSSL_CIPHER_MODE_CBC) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif ccm_aes_setkey_wrap, ccm_aes_setkey_wrap, ccm_ctx_alloc, ccm_ctx_free, }; -const cipher_info_t aes_128_ccm_info = { +static const cipher_info_t aes_128_ccm_info = { POLARSSL_CIPHER_AES_128_CCM, POLARSSL_MODE_CCM, 128, @@ -436,7 +431,7 @@ const cipher_info_t aes_128_ccm_info = { &ccm_aes_info }; -const cipher_info_t aes_192_ccm_info = { +static const cipher_info_t aes_192_ccm_info = { POLARSSL_CIPHER_AES_192_CCM, POLARSSL_MODE_CCM, 192, @@ -447,7 +442,7 @@ const cipher_info_t aes_192_ccm_info = { &ccm_aes_info }; -const cipher_info_t aes_256_ccm_info = { +static const cipher_info_t aes_256_ccm_info = { POLARSSL_CIPHER_AES_256_CCM, POLARSSL_MODE_CCM, 256, @@ -470,64 +465,35 @@ static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation, output ); } +#if defined(POLARSSL_CIPHER_MODE_CBC) static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CBC) return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CBC */ } +#endif /* POLARSSL_CIPHER_MODE_CBC */ +#if defined(POLARSSL_CIPHER_MODE_CFB) static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length, size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CFB) return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv_off); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CFB */ } +#endif /* POLARSSL_CIPHER_MODE_CFB */ +#if defined(POLARSSL_CIPHER_MODE_CTR) static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CTR) return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter, stream_block, input, output ); -#else - ((void) ctx); - ((void) length); - ((void) nc_off); - ((void) nonce_counter); - ((void) stream_block); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CTR */ } +#endif /* POLARSSL_CIPHER_MODE_CTR */ static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) @@ -544,7 +510,7 @@ static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, static void * camellia_ctx_alloc( void ) { camellia_context *ctx; - ctx = (camellia_context *) polarssl_malloc( sizeof( camellia_context ) ); + ctx = polarssl_malloc( sizeof( camellia_context ) ); if( ctx == NULL ) return( NULL ); @@ -560,20 +526,28 @@ static void camellia_ctx_free( void *ctx ) polarssl_free( ctx ); } -const cipher_base_t camellia_info = { +static const cipher_base_t camellia_info = { POLARSSL_CIPHER_ID_CAMELLIA, camellia_crypt_ecb_wrap, +#if defined(POLARSSL_CIPHER_MODE_CBC) camellia_crypt_cbc_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) camellia_crypt_cfb128_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) camellia_crypt_ctr_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif camellia_setkey_enc_wrap, camellia_setkey_dec_wrap, camellia_ctx_alloc, camellia_ctx_free }; -const cipher_info_t camellia_128_ecb_info = { +static const cipher_info_t camellia_128_ecb_info = { POLARSSL_CIPHER_CAMELLIA_128_ECB, POLARSSL_MODE_ECB, 128, @@ -584,7 +558,7 @@ const cipher_info_t camellia_128_ecb_info = { &camellia_info }; -const cipher_info_t camellia_192_ecb_info = { +static const cipher_info_t camellia_192_ecb_info = { POLARSSL_CIPHER_CAMELLIA_192_ECB, POLARSSL_MODE_ECB, 192, @@ -595,7 +569,7 @@ const cipher_info_t camellia_192_ecb_info = { &camellia_info }; -const cipher_info_t camellia_256_ecb_info = { +static const cipher_info_t camellia_256_ecb_info = { POLARSSL_CIPHER_CAMELLIA_256_ECB, POLARSSL_MODE_ECB, 256, @@ -607,7 +581,7 @@ const cipher_info_t camellia_256_ecb_info = { }; #if defined(POLARSSL_CIPHER_MODE_CBC) -const cipher_info_t camellia_128_cbc_info = { +static const cipher_info_t camellia_128_cbc_info = { POLARSSL_CIPHER_CAMELLIA_128_CBC, POLARSSL_MODE_CBC, 128, @@ -618,7 +592,7 @@ const cipher_info_t camellia_128_cbc_info = { &camellia_info }; -const cipher_info_t camellia_192_cbc_info = { +static const cipher_info_t camellia_192_cbc_info = { POLARSSL_CIPHER_CAMELLIA_192_CBC, POLARSSL_MODE_CBC, 192, @@ -629,7 +603,7 @@ const cipher_info_t camellia_192_cbc_info = { &camellia_info }; -const cipher_info_t camellia_256_cbc_info = { +static const cipher_info_t camellia_256_cbc_info = { POLARSSL_CIPHER_CAMELLIA_256_CBC, POLARSSL_MODE_CBC, 256, @@ -642,7 +616,7 @@ const cipher_info_t camellia_256_cbc_info = { #endif /* POLARSSL_CIPHER_MODE_CBC */ #if defined(POLARSSL_CIPHER_MODE_CFB) -const cipher_info_t camellia_128_cfb128_info = { +static const cipher_info_t camellia_128_cfb128_info = { POLARSSL_CIPHER_CAMELLIA_128_CFB128, POLARSSL_MODE_CFB, 128, @@ -653,7 +627,7 @@ const cipher_info_t camellia_128_cfb128_info = { &camellia_info }; -const cipher_info_t camellia_192_cfb128_info = { +static const cipher_info_t camellia_192_cfb128_info = { POLARSSL_CIPHER_CAMELLIA_192_CFB128, POLARSSL_MODE_CFB, 192, @@ -664,7 +638,7 @@ const cipher_info_t camellia_192_cfb128_info = { &camellia_info }; -const cipher_info_t camellia_256_cfb128_info = { +static const cipher_info_t camellia_256_cfb128_info = { POLARSSL_CIPHER_CAMELLIA_256_CFB128, POLARSSL_MODE_CFB, 256, @@ -677,7 +651,7 @@ const cipher_info_t camellia_256_cfb128_info = { #endif /* POLARSSL_CIPHER_MODE_CFB */ #if defined(POLARSSL_CIPHER_MODE_CTR) -const cipher_info_t camellia_128_ctr_info = { +static const cipher_info_t camellia_128_ctr_info = { POLARSSL_CIPHER_CAMELLIA_128_CTR, POLARSSL_MODE_CTR, 128, @@ -688,7 +662,7 @@ const cipher_info_t camellia_128_ctr_info = { &camellia_info }; -const cipher_info_t camellia_192_ctr_info = { +static const cipher_info_t camellia_192_ctr_info = { POLARSSL_CIPHER_CAMELLIA_192_CTR, POLARSSL_MODE_CTR, 192, @@ -699,7 +673,7 @@ const cipher_info_t camellia_192_ctr_info = { &camellia_info }; -const cipher_info_t camellia_256_ctr_info = { +static const cipher_info_t camellia_256_ctr_info = { POLARSSL_CIPHER_CAMELLIA_256_CTR, POLARSSL_MODE_CTR, 256, @@ -719,20 +693,28 @@ static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key, key, key_length ); } -const cipher_base_t gcm_camellia_info = { +static const cipher_base_t gcm_camellia_info = { POLARSSL_CIPHER_ID_CAMELLIA, NULL, +#if defined(POLARSSL_CIPHER_MODE_CBC) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif gcm_camellia_setkey_wrap, gcm_camellia_setkey_wrap, gcm_ctx_alloc, gcm_ctx_free, }; -const cipher_info_t camellia_128_gcm_info = { +static const cipher_info_t camellia_128_gcm_info = { POLARSSL_CIPHER_CAMELLIA_128_GCM, POLARSSL_MODE_GCM, 128, @@ -743,7 +725,7 @@ const cipher_info_t camellia_128_gcm_info = { &gcm_camellia_info }; -const cipher_info_t camellia_192_gcm_info = { +static const cipher_info_t camellia_192_gcm_info = { POLARSSL_CIPHER_CAMELLIA_192_GCM, POLARSSL_MODE_GCM, 192, @@ -754,7 +736,7 @@ const cipher_info_t camellia_192_gcm_info = { &gcm_camellia_info }; -const cipher_info_t camellia_256_gcm_info = { +static const cipher_info_t camellia_256_gcm_info = { POLARSSL_CIPHER_CAMELLIA_256_GCM, POLARSSL_MODE_GCM, 256, @@ -774,20 +756,28 @@ static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key, key, key_length ); } -const cipher_base_t ccm_camellia_info = { +static const cipher_base_t ccm_camellia_info = { POLARSSL_CIPHER_ID_CAMELLIA, NULL, +#if defined(POLARSSL_CIPHER_MODE_CBC) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif ccm_camellia_setkey_wrap, ccm_camellia_setkey_wrap, ccm_ctx_alloc, ccm_ctx_free, }; -const cipher_info_t camellia_128_ccm_info = { +static const cipher_info_t camellia_128_ccm_info = { POLARSSL_CIPHER_CAMELLIA_128_CCM, POLARSSL_MODE_CCM, 128, @@ -798,7 +788,7 @@ const cipher_info_t camellia_128_ccm_info = { &ccm_camellia_info }; -const cipher_info_t camellia_192_ccm_info = { +static const cipher_info_t camellia_192_ccm_info = { POLARSSL_CIPHER_CAMELLIA_192_CCM, POLARSSL_MODE_CCM, 192, @@ -809,7 +799,7 @@ const cipher_info_t camellia_192_ccm_info = { &ccm_camellia_info }; -const cipher_info_t camellia_256_ccm_info = { +static const cipher_info_t camellia_256_ccm_info = { POLARSSL_CIPHER_CAMELLIA_256_CCM, POLARSSL_MODE_CCM, 256, @@ -839,41 +829,23 @@ static int des3_crypt_ecb_wrap( void *ctx, operation_t operation, return des3_crypt_ecb( (des3_context *) ctx, input, output ); } +#if defined(POLARSSL_CIPHER_MODE_CBC) static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CBC) return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CBC */ } +#endif /* POLARSSL_CIPHER_MODE_CBC */ +#if defined(POLARSSL_CIPHER_MODE_CBC) static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CBC) return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CBC */ } +#endif /* POLARSSL_CIPHER_MODE_CBC */ static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) @@ -925,7 +897,7 @@ static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, static void * des_ctx_alloc( void ) { - des_context *des = (des_context *) polarssl_malloc( sizeof( des_context ) ); + des_context *des = polarssl_malloc( sizeof( des_context ) ); if( des == NULL ) return( NULL ); @@ -944,7 +916,7 @@ static void des_ctx_free( void *ctx ) static void * des3_ctx_alloc( void ) { des3_context *des3; - des3 = (des3_context *) polarssl_malloc( sizeof( des3_context ) ); + des3 = polarssl_malloc( sizeof( des3_context ) ); if( des3 == NULL ) return( NULL ); @@ -960,20 +932,28 @@ static void des3_ctx_free( void *ctx ) polarssl_free( ctx ); } -const cipher_base_t des_info = { +static const cipher_base_t des_info = { POLARSSL_CIPHER_ID_DES, des_crypt_ecb_wrap, +#if defined(POLARSSL_CIPHER_MODE_CBC) des_crypt_cbc_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif des_setkey_enc_wrap, des_setkey_dec_wrap, des_ctx_alloc, des_ctx_free }; -const cipher_info_t des_ecb_info = { +static const cipher_info_t des_ecb_info = { POLARSSL_CIPHER_DES_ECB, POLARSSL_MODE_ECB, POLARSSL_KEY_LENGTH_DES, @@ -985,7 +965,7 @@ const cipher_info_t des_ecb_info = { }; #if defined(POLARSSL_CIPHER_MODE_CBC) -const cipher_info_t des_cbc_info = { +static const cipher_info_t des_cbc_info = { POLARSSL_CIPHER_DES_CBC, POLARSSL_MODE_CBC, POLARSSL_KEY_LENGTH_DES, @@ -997,20 +977,28 @@ const cipher_info_t des_cbc_info = { }; #endif /* POLARSSL_CIPHER_MODE_CBC */ -const cipher_base_t des_ede_info = { +static const cipher_base_t des_ede_info = { POLARSSL_CIPHER_ID_DES, des3_crypt_ecb_wrap, +#if defined(POLARSSL_CIPHER_MODE_CBC) des3_crypt_cbc_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif des3_set2key_enc_wrap, des3_set2key_dec_wrap, des3_ctx_alloc, des3_ctx_free }; -const cipher_info_t des_ede_ecb_info = { +static const cipher_info_t des_ede_ecb_info = { POLARSSL_CIPHER_DES_EDE_ECB, POLARSSL_MODE_ECB, POLARSSL_KEY_LENGTH_DES_EDE, @@ -1022,7 +1010,7 @@ const cipher_info_t des_ede_ecb_info = { }; #if defined(POLARSSL_CIPHER_MODE_CBC) -const cipher_info_t des_ede_cbc_info = { +static const cipher_info_t des_ede_cbc_info = { POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MODE_CBC, POLARSSL_KEY_LENGTH_DES_EDE, @@ -1034,20 +1022,28 @@ const cipher_info_t des_ede_cbc_info = { }; #endif /* POLARSSL_CIPHER_MODE_CBC */ -const cipher_base_t des_ede3_info = { +static const cipher_base_t des_ede3_info = { POLARSSL_CIPHER_ID_DES, des3_crypt_ecb_wrap, +#if defined(POLARSSL_CIPHER_MODE_CBC) des3_crypt_cbc_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif des3_set3key_enc_wrap, des3_set3key_dec_wrap, des3_ctx_alloc, des3_ctx_free }; -const cipher_info_t des_ede3_ecb_info = { +static const cipher_info_t des_ede3_ecb_info = { POLARSSL_CIPHER_DES_EDE3_ECB, POLARSSL_MODE_ECB, POLARSSL_KEY_LENGTH_DES_EDE3, @@ -1058,7 +1054,7 @@ const cipher_info_t des_ede3_ecb_info = { &des_ede3_info }; #if defined(POLARSSL_CIPHER_MODE_CBC) -const cipher_info_t des_ede3_cbc_info = { +static const cipher_info_t des_ede3_cbc_info = { POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MODE_CBC, POLARSSL_KEY_LENGTH_DES_EDE3, @@ -1080,64 +1076,35 @@ static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation, output ); } +#if defined(POLARSSL_CIPHER_MODE_CBC) static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CBC) return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CBC */ } +#endif /* POLARSSL_CIPHER_MODE_CBC */ +#if defined(POLARSSL_CIPHER_MODE_CFB) static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length, size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CFB) return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output ); -#else - ((void) ctx); - ((void) operation); - ((void) length); - ((void) iv_off); - ((void) iv); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CFB */ } +#endif /* POLARSSL_CIPHER_MODE_CFB */ +#if defined(POLARSSL_CIPHER_MODE_CTR) static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, const unsigned char *input, unsigned char *output ) { -#if defined(POLARSSL_CIPHER_MODE_CTR) return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter, stream_block, input, output ); -#else - ((void) ctx); - ((void) length); - ((void) nc_off); - ((void) nonce_counter); - ((void) stream_block); - ((void) input); - ((void) output); - - return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE ); -#endif /* POLARSSL_CIPHER_MODE_CTR */ } +#endif /* POLARSSL_CIPHER_MODE_CTR */ static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) @@ -1148,7 +1115,7 @@ static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, static void * blowfish_ctx_alloc( void ) { blowfish_context *ctx; - ctx = (blowfish_context *) polarssl_malloc( sizeof( blowfish_context ) ); + ctx = polarssl_malloc( sizeof( blowfish_context ) ); if( ctx == NULL ) return( NULL ); @@ -1164,20 +1131,28 @@ static void blowfish_ctx_free( void *ctx ) polarssl_free( ctx ); } -const cipher_base_t blowfish_info = { +static const cipher_base_t blowfish_info = { POLARSSL_CIPHER_ID_BLOWFISH, blowfish_crypt_ecb_wrap, +#if defined(POLARSSL_CIPHER_MODE_CBC) blowfish_crypt_cbc_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) blowfish_crypt_cfb64_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) blowfish_crypt_ctr_wrap, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) NULL, +#endif blowfish_setkey_wrap, blowfish_setkey_wrap, blowfish_ctx_alloc, blowfish_ctx_free }; -const cipher_info_t blowfish_ecb_info = { +static const cipher_info_t blowfish_ecb_info = { POLARSSL_CIPHER_BLOWFISH_ECB, POLARSSL_MODE_ECB, 128, @@ -1189,7 +1164,7 @@ const cipher_info_t blowfish_ecb_info = { }; #if defined(POLARSSL_CIPHER_MODE_CBC) -const cipher_info_t blowfish_cbc_info = { +static const cipher_info_t blowfish_cbc_info = { POLARSSL_CIPHER_BLOWFISH_CBC, POLARSSL_MODE_CBC, 128, @@ -1202,7 +1177,7 @@ const cipher_info_t blowfish_cbc_info = { #endif /* POLARSSL_CIPHER_MODE_CBC */ #if defined(POLARSSL_CIPHER_MODE_CFB) -const cipher_info_t blowfish_cfb64_info = { +static const cipher_info_t blowfish_cfb64_info = { POLARSSL_CIPHER_BLOWFISH_CFB64, POLARSSL_MODE_CFB, 128, @@ -1215,7 +1190,7 @@ const cipher_info_t blowfish_cfb64_info = { #endif /* POLARSSL_CIPHER_MODE_CFB */ #if defined(POLARSSL_CIPHER_MODE_CTR) -const cipher_info_t blowfish_ctr_info = { +static const cipher_info_t blowfish_ctr_info = { POLARSSL_CIPHER_BLOWFISH_CTR, POLARSSL_MODE_CTR, 128, @@ -1250,7 +1225,7 @@ static int arc4_setkey_wrap( void *ctx, const unsigned char *key, static void * arc4_ctx_alloc( void ) { arc4_context *ctx; - ctx = (arc4_context *) polarssl_malloc( sizeof( arc4_context ) ); + ctx = polarssl_malloc( sizeof( arc4_context ) ); if( ctx == NULL ) return( NULL ); @@ -1266,20 +1241,28 @@ static void arc4_ctx_free( void *ctx ) polarssl_free( ctx ); } -const cipher_base_t arc4_base_info = { +static const cipher_base_t arc4_base_info = { POLARSSL_CIPHER_ID_ARC4, NULL, +#if defined(POLARSSL_CIPHER_MODE_CBC) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) arc4_crypt_stream_wrap, +#endif arc4_setkey_wrap, arc4_setkey_wrap, arc4_ctx_alloc, arc4_ctx_free }; -const cipher_info_t arc4_128_info = { +static const cipher_info_t arc4_128_info = { POLARSSL_CIPHER_ARC4_128, POLARSSL_MODE_STREAM, 128, @@ -1321,20 +1304,28 @@ static void null_ctx_free( void *ctx ) ((void) ctx); } -const cipher_base_t null_base_info = { +static const cipher_base_t null_base_info = { POLARSSL_CIPHER_ID_NULL, NULL, +#if defined(POLARSSL_CIPHER_MODE_CBC) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CFB) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_CTR) NULL, +#endif +#if defined(POLARSSL_CIPHER_MODE_STREAM) null_crypt_stream, +#endif null_setkey, null_setkey, null_ctx_alloc, null_ctx_free }; -const cipher_info_t null_cipher_info = { +static const cipher_info_t null_cipher_info = { POLARSSL_CIPHER_NULL, POLARSSL_MODE_STREAM, 0, @@ -1442,7 +1433,7 @@ const cipher_definition_t cipher_definitions[] = { POLARSSL_CIPHER_NULL, &null_cipher_info }, #endif /* POLARSSL_CIPHER_NULL_CIPHER */ - { 0, NULL } + { POLARSSL_CIPHER_NONE, NULL } }; #define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0] diff --git a/ext/polarssl/library/ctr_drbg.c b/ext/mbedtls/library/ctr_drbg.c similarity index 94% rename from ext/polarssl/library/ctr_drbg.c rename to ext/mbedtls/library/ctr_drbg.c index 96ee4f1625..24adff08f9 100644 --- a/ext/polarssl/library/ctr_drbg.c +++ b/ext/mbedtls/library/ctr_drbg.c @@ -1,12 +1,9 @@ /* * CTR_DRBG implementation based on AES-256 (NIST SP 800-90) * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,15 +35,20 @@ #include "polarssl/ctr_drbg.h" +#include + #if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -137,6 +139,9 @@ static int block_cipher_df( unsigned char *output, int i, j; size_t buf_len, use_len; + if( data_len > CTR_DRBG_MAX_SEED_INPUT ) + return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG ); + memset( buf, 0, CTR_DRBG_MAX_SEED_INPUT + CTR_DRBG_BLOCKSIZE + 16 ); aes_init( &aes_ctx ); @@ -256,6 +261,11 @@ void ctr_drbg_update( ctr_drbg_context *ctx, if( add_len > 0 ) { + /* MAX_INPUT would be more logical here, but we have to match + * block_cipher_df()'s limits since we can't propagate errors */ + if( add_len > CTR_DRBG_MAX_SEED_INPUT ) + add_len = CTR_DRBG_MAX_SEED_INPUT; + block_cipher_df( add_input, additional, add_len ); ctr_drbg_update_internal( ctx, add_input ); } @@ -438,9 +448,7 @@ int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ) #if defined(POLARSSL_SELF_TEST) -#include - -static unsigned char entropy_source_pr[96] = +static const unsigned char entropy_source_pr[96] = { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16, 0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02, 0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b, @@ -454,7 +462,7 @@ static unsigned char entropy_source_pr[96] = 0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56, 0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 }; -static unsigned char entropy_source_nopr[64] = +static const unsigned char entropy_source_nopr[64] = { 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14, 0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe, 0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d, @@ -513,7 +521,7 @@ int ctr_drbg_self_test( int verbose ) test_offset = 0; CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy, - entropy_source_pr, nonce_pers_pr, 16, 32 ) ); + (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) ); ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON ); CHK( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) ); CHK( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) ); @@ -530,7 +538,7 @@ int ctr_drbg_self_test( int verbose ) test_offset = 0; CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy, - entropy_source_nopr, nonce_pers_nopr, 16, 32 ) ); + (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) ); CHK( ctr_drbg_random( &ctx, buf, 16 ) ); CHK( ctr_drbg_reseed( &ctx, NULL, 0 ) ); CHK( ctr_drbg_random( &ctx, buf, 16 ) ); diff --git a/ext/polarssl/library/debug.c b/ext/mbedtls/library/debug.c similarity index 72% rename from ext/polarssl/library/debug.c rename to ext/mbedtls/library/debug.c index a81f502bfb..9adcf07bef 100644 --- a/ext/polarssl/library/debug.c +++ b/ext/mbedtls/library/debug.c @@ -1,12 +1,9 @@ /* * Debugging routines * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,11 +31,8 @@ #include "polarssl/debug.h" #include -#include - -#if defined(EFIX64) || defined(EFI32) #include -#endif +#include #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #if !defined snprintf @@ -50,6 +44,16 @@ #endif #endif /* _MSC_VER */ +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#define polarssl_malloc malloc +#define polarssl_free free +#endif + +#define DEBUG_BUF_SIZE 512 + static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE; static int debug_threshold = 0; @@ -66,17 +70,34 @@ void debug_set_threshold( int threshold ) char *debug_fmt( const char *format, ... ) { va_list argp; - static char str[512]; - int maxlen = sizeof( str ) - 1; +#if defined(POLARSSL_THREADING_C) + char *str = polarssl_malloc( DEBUG_BUF_SIZE ); + + if( str == NULL ) + return( NULL ); +#else + static char str[DEBUG_BUF_SIZE]; +#endif va_start( argp, format ); - vsnprintf( str, maxlen, format, argp ); + vsnprintf( str, DEBUG_BUF_SIZE - 1, format, argp ); va_end( argp ); - str[maxlen] = '\0'; + str[DEBUG_BUF_SIZE - 1] = '\0'; return( str ); } +void debug_print_msg_free( const ssl_context *ssl, int level, + const char *file, int line, char *text ) +{ + if( text != NULL ) + debug_print_msg( ssl, level, file, line, text ); + +#if defined(POLARSSL_THREADING_C) + polarssl_free( text ); +#endif +} + void debug_print_msg( const ssl_context *ssl, int level, const char *file, int line, const char *text ) { @@ -92,7 +113,7 @@ void debug_print_msg( const ssl_context *ssl, int level, return; } - snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); + polarssl_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); str[maxlen] = '\0'; ssl->f_dbg( ssl->p_dbg, level, str ); } @@ -109,9 +130,9 @@ void debug_print_ret( const ssl_context *ssl, int level, return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n", + polarssl_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n", text, ret, -ret ); str[maxlen] = '\0'; @@ -123,21 +144,23 @@ void debug_print_buf( const ssl_context *ssl, int level, unsigned char *buf, size_t len ) { char str[512]; + char txt[17]; size_t i, maxlen = sizeof( str ) - 1, idx = 0; if( ssl->f_dbg == NULL || level > debug_threshold ) return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n", + polarssl_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n", text, (unsigned int) len ); str[maxlen] = '\0'; ssl->f_dbg( ssl->p_dbg, level, str ); idx = 0; + memset( txt, 0, sizeof( txt ) ); for( i = 0; i < len; i++ ) { if( i >= 4096 ) @@ -147,26 +170,32 @@ void debug_print_buf( const ssl_context *ssl, int level, { if( i > 0 ) { - snprintf( str + idx, maxlen - idx, "\n" ); + polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt ); ssl->f_dbg( ssl->p_dbg, level, str ); + idx = 0; + memset( txt, 0, sizeof( txt ) ); } if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - idx += snprintf( str + idx, maxlen - idx, "%04x: ", + idx += polarssl_snprintf( str + idx, maxlen - idx, "%04x: ", (unsigned int) i ); } - idx += snprintf( str + idx, maxlen - idx, " %02x", + idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) buf[i] ); + txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; } if( len > 0 ) { - snprintf( str + idx, maxlen - idx, "\n" ); + for( /* i = i */; i % 16 != 0; i++ ) + idx += polarssl_snprintf( str + idx, maxlen - idx, " " ); + + polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt ); ssl->f_dbg( ssl->p_dbg, level, str ); } } @@ -182,11 +211,11 @@ void debug_print_ecp( const ssl_context *ssl, int level, if( ssl->f_dbg == NULL || level > debug_threshold ) return; - snprintf( str, maxlen, "%s(X)", text ); + polarssl_snprintf( str, maxlen, "%s(X)", text ); str[maxlen] = '\0'; debug_print_mpi( ssl, level, file, line, str, &X->X ); - snprintf( str, maxlen, "%s(Y)", text ); + polarssl_snprintf( str, maxlen, "%s(Y)", text ); str[maxlen] = '\0'; debug_print_mpi( ssl, level, file, line, str, &X->Y ); } @@ -213,9 +242,9 @@ void debug_print_mpi( const ssl_context *ssl, int level, break; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n", + polarssl_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n", text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) ); str[maxlen] = '\0'; @@ -238,16 +267,16 @@ void debug_print_mpi( const ssl_context *ssl, int level, { if( j > 0 ) { - snprintf( str + idx, maxlen - idx, "\n" ); + polarssl_snprintf( str + idx, maxlen - idx, "\n" ); ssl->f_dbg( ssl->p_dbg, level, str ); idx = 0; } if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); } - idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) + idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); j++; @@ -259,13 +288,13 @@ void debug_print_mpi( const ssl_context *ssl, int level, { if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) { - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); } - idx += snprintf( str + idx, maxlen - idx, " 00" ); + idx += polarssl_snprintf( str + idx, maxlen - idx, " 00" ); } - snprintf( str + idx, maxlen - idx, "\n" ); + polarssl_snprintf( str + idx, maxlen - idx, "\n" ); ssl->f_dbg( ssl->p_dbg, level, str ); } #endif /* POLARSSL_BIGNUM_C */ @@ -292,7 +321,7 @@ static void debug_print_pk( const ssl_context *ssl, int level, if( items[i].type == POLARSSL_PK_DEBUG_NONE ) return; - snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); + polarssl_snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); name[sizeof( name ) - 1] = '\0'; if( items[i].type == POLARSSL_PK_DEBUG_MPI ) @@ -319,7 +348,7 @@ void debug_print_crt( const ssl_context *ssl, int level, if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) { - snprintf( prefix, maxlen, "%s(%04d): ", file, line ); + polarssl_snprintf( prefix, maxlen, "%s(%04d): ", file, line ); prefix[maxlen] = '\0'; } else @@ -333,9 +362,9 @@ void debug_print_crt( const ssl_context *ssl, int level, x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt ); if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "%s #%d:\n%s", + polarssl_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s", text, ++i, buf ); str[maxlen] = '\0'; diff --git a/ext/polarssl/library/des.c b/ext/mbedtls/library/des.c similarity index 99% rename from ext/polarssl/library/des.c rename to ext/mbedtls/library/des.c index 12fe4f46a6..080e11392e 100644 --- a/ext/polarssl/library/des.c +++ b/ext/mbedtls/library/des.c @@ -1,12 +1,9 @@ /* * FIPS-46-3 compliant Triple-DES implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,11 +36,16 @@ #include "polarssl/des.h" +#include + +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ #if !defined(POLARSSL_DES_ALT) @@ -805,9 +807,6 @@ int des3_crypt_cbc( des3_context *ctx, #endif /* !POLARSSL_DES_ALT */ #if defined(POLARSSL_SELF_TEST) - -#include - /* * DES and 3DES test vectors from: * diff --git a/ext/polarssl/library/dhm.c b/ext/mbedtls/library/dhm.c similarity index 94% rename from ext/polarssl/library/dhm.c rename to ext/mbedtls/library/dhm.c index 089c11b3d9..0a4f820286 100644 --- a/ext/polarssl/library/dhm.c +++ b/ext/mbedtls/library/dhm.c @@ -1,12 +1,9 @@ /* * Diffie-Hellman-Merkle key exchange * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +35,8 @@ #include "polarssl/dhm.h" +#include + #if defined(POLARSSL_PEM_PARSE_C) #include "polarssl/pem.h" #endif @@ -50,6 +49,7 @@ #include "polarssl/platform.h" #else #include +#include #define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free @@ -444,8 +444,9 @@ int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, /* * DHParams ::= SEQUENCE { - * prime INTEGER, -- P - * generator INTEGER, -- g + * prime INTEGER, -- P + * generator INTEGER, -- g + * privateValueLength INTEGER OPTIONAL * } */ if( ( ret = asn1_get_tag( &p, end, &len, @@ -466,9 +467,23 @@ int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, if( p != end ) { - ret = POLARSSL_ERR_DHM_INVALID_FORMAT + - POLARSSL_ERR_ASN1_LENGTH_MISMATCH; - goto exit; + /* this might be the optional privateValueLength; If so, we + can cleanly discard it; */ + mpi rec; + mpi_init( &rec ); + ret = asn1_get_mpi( &p, end, &rec ); + mpi_free( &rec ); + if ( ret != 0 ) + { + ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret; + goto exit; + } + if ( p != end ) + { + ret = POLARSSL_ERR_DHM_INVALID_FORMAT + + POLARSSL_ERR_ASN1_LENGTH_MISMATCH; + goto exit; + } } ret = 0; @@ -508,7 +523,7 @@ static int load_file( const char *path, unsigned char **buf, size_t *n ) *n = (size_t) size; if( *n + 1 == 0 || - ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) + ( *buf = polarssl_malloc( *n + 1 ) ) == NULL ) { fclose( f ); return( POLARSSL_ERR_DHM_MALLOC_FAILED ); diff --git a/ext/polarssl/library/ecdh.c b/ext/mbedtls/library/ecdh.c similarity index 97% rename from ext/polarssl/library/ecdh.c rename to ext/mbedtls/library/ecdh.c index b93d82ec6b..82fff276a2 100644 --- a/ext/polarssl/library/ecdh.c +++ b/ext/mbedtls/library/ecdh.c @@ -1,12 +1,9 @@ /* * Elliptic curve Diffie-Hellman * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,6 +37,8 @@ #include "polarssl/ecdh.h" +#include + /* * Generate public key: simple wrapper around ecp_gen_keypair */ diff --git a/ext/polarssl/library/ecdsa.c b/ext/mbedtls/library/ecdsa.c similarity index 97% rename from ext/polarssl/library/ecdsa.c rename to ext/mbedtls/library/ecdsa.c index 5af7f6b53e..3f72d857c3 100644 --- a/ext/polarssl/library/ecdsa.c +++ b/ext/mbedtls/library/ecdsa.c @@ -1,12 +1,9 @@ /* * Elliptic curve DSA * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,6 +37,8 @@ #include "polarssl/ecdsa.h" #include "polarssl/asn1write.h" +#include + #if defined(POLARSSL_ECDSA_DETERMINISTIC) #include "polarssl/hmac_drbg.h" #endif @@ -60,7 +59,7 @@ static const md_info_t *md_info_by_size( size_t min_size ) for( md_alg = md_list(); *md_alg != 0; md_alg++ ) { - if( ( md_cur = md_info_from_type( *md_alg ) ) == NULL || + if( ( md_cur = md_info_from_type( (md_type_t) *md_alg ) ) == NULL || (size_t) md_cur->size < min_size || ( md_picked != NULL && md_cur->size > md_picked->size ) ) continue; @@ -333,7 +332,7 @@ cleanup: #if POLARSSL_ECP_MAX_BYTES > 124 #error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN" #endif -#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) ) +#define MAX_SIG_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) ) /* * Convert a signature (given by context) to ASN.1 diff --git a/ext/polarssl/library/ecp.c b/ext/mbedtls/library/ecp.c similarity index 93% rename from ext/polarssl/library/ecp.c rename to ext/mbedtls/library/ecp.c index ece781d5ed..adef09ea87 100644 --- a/ext/polarssl/library/ecp.c +++ b/ext/mbedtls/library/ecp.c @@ -1,12 +1,9 @@ /* * Elliptic curves over GF(p): generic functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -54,16 +51,18 @@ #include "polarssl/ecp.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include +#include #define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free #endif -#include - #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ !defined(EFI32) #define strcasecmp _stricmp @@ -815,7 +814,7 @@ static int ecp_normalize_jac_many( const ecp_group *grp, if( t_len < 2 ) return( ecp_normalize_jac( grp, *T ) ); - if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) + if( ( c = polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) return( POLARSSL_ERR_ECP_MALLOC_FAILED ); mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi ); @@ -912,70 +911,86 @@ cleanup: /* * Point doubling R = 2 P, Jacobian coordinates * - * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3 - * with heavy variable renaming, some reordering and one minor modification - * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b) - * in order to use a lot less intermediate variables (6 vs 25). + * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 . * - * Cost: 1D := 2M + 8S + * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR + * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring. + * + * Standard optimizations are applied when curve parameter A is one of { 0, -3 }. + * + * Cost: 1D := 3M + 4S (A == 0) + * 4M + 4S (A == -3) + * 3M + 6S + 1a otherwise */ static int ecp_double_jac( const ecp_group *grp, ecp_point *R, const ecp_point *P ) { int ret; - mpi T1, T2, T3, X3, Y3, Z3; + mpi M, S, T, U; #if defined(POLARSSL_SELF_TEST) dbl_count++; #endif - mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); - mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 ); - - MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 ); - MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 ); - MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 ); - MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 ); - MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 ); - MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 ); - MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 ); - MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 ); - MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 ); - MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 ); - MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 ); + mpi_init( &M ); mpi_init( &S ); mpi_init( &T ); mpi_init( &U ); /* Special case for A = -3 */ if( grp->A.p == NULL ) { - MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) ); - X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */ - MOD_SUB( X3 ); + /* M = 3(X + Z^2)(X - Z^2) */ + MPI_CHK( mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S ); + MPI_CHK( mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T ); + MPI_CHK( mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U ); + MPI_CHK( mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S ); + MPI_CHK( mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M ); } else { - MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 ); + /* M = 3.X^2 */ + MPI_CHK( mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S ); + MPI_CHK( mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M ); + + /* Optimize away for "koblitz" curves with A = 0 */ + if( mpi_cmp_int( &grp->A, 0 ) != 0 ) + { + /* M += A.Z^4 */ + MPI_CHK( mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S ); + MPI_CHK( mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T ); + MPI_CHK( mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S ); + MPI_CHK( mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M ); + } } - MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 ); - MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 ); - MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 ); - MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 ); - MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 ); - MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 ); - MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 ); - MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 ); - MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 ); - MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 ); - MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 ); - MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 ); - - MPI_CHK( mpi_copy( &R->X, &X3 ) ); - MPI_CHK( mpi_copy( &R->Y, &Y3 ) ); - MPI_CHK( mpi_copy( &R->Z, &Z3 ) ); + /* S = 4.X.Y^2 */ + MPI_CHK( mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T ); + MPI_CHK( mpi_shift_l( &T, 1 ) ); MOD_ADD( T ); + MPI_CHK( mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S ); + MPI_CHK( mpi_shift_l( &S, 1 ) ); MOD_ADD( S ); + + /* U = 8.Y^4 */ + MPI_CHK( mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U ); + MPI_CHK( mpi_shift_l( &U, 1 ) ); MOD_ADD( U ); + + /* T = M^2 - 2.S */ + MPI_CHK( mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T ); + MPI_CHK( mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T ); + MPI_CHK( mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T ); + + /* S = M(S - T) - U */ + MPI_CHK( mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S ); + MPI_CHK( mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S ); + MPI_CHK( mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S ); + + /* U = 2.Y.Z */ + MPI_CHK( mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U ); + MPI_CHK( mpi_shift_l( &U, 1 ) ); MOD_ADD( U ); + + MPI_CHK( mpi_copy( &R->X, &T ) ); + MPI_CHK( mpi_copy( &R->Y, &S ) ); + MPI_CHK( mpi_copy( &R->Z, &U ) ); cleanup: - mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); - mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 ); + mpi_free( &M ); mpi_free( &S ); mpi_free( &T ); mpi_free( &U ); return( ret ); } @@ -1418,7 +1433,7 @@ static int ecp_mul_comb( ecp_group *grp, ecp_point *R, if( T == NULL ) { - T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) ); + T = polarssl_malloc( pre_len * sizeof( ecp_point ) ); if( T == NULL ) { ret = POLARSSL_ERR_ECP_MALLOC_FAILED; @@ -1897,6 +1912,48 @@ int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key, return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) ); } +/* + * Check a public-private key pair + */ +int ecp_check_pub_priv( const ecp_keypair *pub, const ecp_keypair *prv ) +{ + int ret; + ecp_point Q; + ecp_group grp; + + if( pub->grp.id == POLARSSL_ECP_DP_NONE || + pub->grp.id != prv->grp.id || + mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) || + mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) || + mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) ) + { + return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); + } + + ecp_point_init( &Q ); + ecp_group_init( &grp ); + + /* ecp_mul() needs a non-const group... */ + ecp_group_copy( &grp, &prv->grp ); + + /* Also checks d is valid */ + MPI_CHK( ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) ); + + if( mpi_cmp_mpi( &Q.X, &prv->Q.X ) || + mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) || + mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) ) + { + ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; + goto cleanup; + } + +cleanup: + ecp_point_free( &Q ); + ecp_group_free( &grp ); + + return( ret ); +} + #if defined(POLARSSL_SELF_TEST) /* diff --git a/ext/polarssl/library/ecp_curves.c b/ext/mbedtls/library/ecp_curves.c similarity index 99% rename from ext/polarssl/library/ecp_curves.c rename to ext/mbedtls/library/ecp_curves.c index 4c0018cc73..3786356c3e 100644 --- a/ext/polarssl/library/ecp_curves.c +++ b/ext/mbedtls/library/ecp_curves.c @@ -1,12 +1,9 @@ /* * Elliptic curves over GF(p): curve-specific data and functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +30,8 @@ #include "polarssl/ecp.h" +#include + #if defined(_MSC_VER) && !defined(inline) #define inline _inline #else diff --git a/ext/polarssl/library/entropy.c b/ext/mbedtls/library/entropy.c similarity index 98% rename from ext/polarssl/library/entropy.c rename to ext/mbedtls/library/entropy.c index bc7fb0fe46..540a27c57a 100644 --- a/ext/polarssl/library/entropy.c +++ b/ext/mbedtls/library/entropy.c @@ -1,12 +1,9 @@ /* * Entropy accumulator implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,10 +31,21 @@ #include "polarssl/entropy.h" #include "polarssl/entropy_poll.h" +#include + #if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#include +#define polarssl_printf printf +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ + #if defined(POLARSSL_HAVEGE_C) #include "polarssl/havege.h" #endif @@ -86,10 +94,10 @@ void entropy_free( entropy_context *ctx ) #if defined(POLARSSL_HAVEGE_C) havege_free( &ctx->havege_data ); #endif - polarssl_zeroize( ctx, sizeof( entropy_context ) ); #if defined(POLARSSL_THREADING_C) polarssl_mutex_free( &ctx->mutex ); #endif + polarssl_zeroize( ctx, sizeof( entropy_context ) ); } int entropy_add_source( entropy_context *ctx, @@ -381,14 +389,6 @@ int entropy_update_seed_file( entropy_context *ctx, const char *path ) #endif /* POLARSSL_FS_IO */ #if defined(POLARSSL_SELF_TEST) - -#if defined(POLARSSL_PLATFORM_C) -#include "polarssl/platform.h" -#else -#include -#define polarssl_printf printf -#endif - /* * Dummy source function */ diff --git a/ext/polarssl/library/entropy_poll.c b/ext/mbedtls/library/entropy_poll.c similarity index 64% rename from ext/polarssl/library/entropy_poll.c rename to ext/mbedtls/library/entropy_poll.c index 9ca9e95199..e0f9ae2868 100644 --- a/ext/polarssl/library/entropy_poll.c +++ b/ext/mbedtls/library/entropy_poll.c @@ -1,12 +1,9 @@ /* * Platform-specific and custom entropy polling functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,6 +32,7 @@ #include "polarssl/entropy_poll.h" #if defined(POLARSSL_TIMING_C) +#include #include "polarssl/timing.h" #endif #if defined(POLARSSL_HAVEGE_C) @@ -73,6 +71,62 @@ int platform_entropy_poll( void *data, unsigned char *output, size_t len, } #else /* _WIN32 && !EFIX64 && !EFI32 */ +/* + * Test for Linux getrandom() support. + * Since there is no wrapper in the libc yet, use the generic syscall wrapper + * available in GNU libc and compatible libc's (eg uClibc). + */ +#if defined(__linux__) && defined(__GLIBC__) +#include +#include +#include +#if defined(SYS_getrandom) +#define HAVE_GETRANDOM +static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) +{ + return( syscall( SYS_getrandom, buf, buflen, flags ) ); +} + +#include +/* Check if version is at least 3.17.0 */ +static int check_version_3_17_plus( void ) +{ + int minor; + struct utsname un; + const char *ver; + + /* Get version information */ + uname(&un); + ver = un.release; + + /* Check major version; assume a single digit */ + if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' ) + return( -1 ); + + if( ver[0] - '0' > 3 ) + return( 0 ); + + /* Ok, so now we know major == 3, check minor. + * Assume 1 or 2 digits. */ + if( ver[2] < '0' || ver[2] > '9' ) + return( -1 ); + + minor = ver[2] - '0'; + + if( ver[3] >= '0' && ver[3] <= '9' ) + minor = 10 * minor + ver[3] - '0'; + else if( ver [3] != '.' ) + return( -1 ); + + if( minor < 17 ) + return( -1 ); + + return( 0 ); +} +static int has_getrandom = -1; +#endif /* SYS_getrandom */ +#endif /* __linux__ */ + #include int platform_entropy_poll( void *data, @@ -82,6 +136,22 @@ int platform_entropy_poll( void *data, size_t ret; ((void) data); +#if defined(HAVE_GETRANDOM) + if( has_getrandom == -1 ) + has_getrandom = ( check_version_3_17_plus() == 0 ); + + if( has_getrandom ) + { + int ret; + + if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) + return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); + + *olen = ret; + return( 0 ); + } +#endif /* HAVE_GETRANDOM */ + *olen = 0; file = fopen( "/dev/urandom", "rb" ); diff --git a/ext/polarssl/library/error.c b/ext/mbedtls/library/error.c similarity index 50% rename from ext/polarssl/library/error.c rename to ext/mbedtls/library/error.c index 73504d450d..98fba5cbdf 100644 --- a/ext/polarssl/library/error.c +++ b/ext/mbedtls/library/error.c @@ -1,12 +1,9 @@ /* * Error message information * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,10 +28,19 @@ #if defined(POLARSSL_ERROR_C) || defined(POLARSSL_ERROR_STRERROR_DUMMY) #include "polarssl/error.h" +#include +#endif + +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf #endif #if defined(POLARSSL_ERROR_C) +#include + #if defined(POLARSSL_AES_C) #include "polarssl/aes.h" #endif @@ -175,9 +181,6 @@ #include "polarssl/xtea.h" #endif - -#include - #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \ !defined(EFI32) #define snprintf _snprintf @@ -207,295 +210,297 @@ void polarssl_strerror( int ret, char *buf, size_t buflen ) // BEGIN generated code #if defined(POLARSSL_CIPHER_C) if( use_ret == -(POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "CIPHER - The selected feature is not available" ); + polarssl_snprintf( buf, buflen, "CIPHER - The selected feature is not available" ); if( use_ret == -(POLARSSL_ERR_CIPHER_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "CIPHER - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "CIPHER - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_CIPHER_ALLOC_FAILED) ) - snprintf( buf, buflen, "CIPHER - Failed to allocate memory" ); + polarssl_snprintf( buf, buflen, "CIPHER - Failed to allocate memory" ); if( use_ret == -(POLARSSL_ERR_CIPHER_INVALID_PADDING) ) - snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" ); + polarssl_snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" ); if( use_ret == -(POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED) ) - snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" ); + polarssl_snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" ); if( use_ret == -(POLARSSL_ERR_CIPHER_AUTH_FAILED) ) - snprintf( buf, buflen, "CIPHER - Authentication failed (for AEAD modes)" ); + polarssl_snprintf( buf, buflen, "CIPHER - Authentication failed (for AEAD modes)" ); #endif /* POLARSSL_CIPHER_C */ #if defined(POLARSSL_DHM_C) if( use_ret == -(POLARSSL_ERR_DHM_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "DHM - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "DHM - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_DHM_READ_PARAMS_FAILED) ) - snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" ); + polarssl_snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" ); if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED) ) - snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" ); + polarssl_snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" ); if( use_ret == -(POLARSSL_ERR_DHM_READ_PUBLIC_FAILED) ) - snprintf( buf, buflen, "DHM - Reading of the public values failed" ); + polarssl_snprintf( buf, buflen, "DHM - Reading of the public values failed" ); if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED) ) - snprintf( buf, buflen, "DHM - Making of the public value failed" ); + polarssl_snprintf( buf, buflen, "DHM - Making of the public value failed" ); if( use_ret == -(POLARSSL_ERR_DHM_CALC_SECRET_FAILED) ) - snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" ); + polarssl_snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" ); if( use_ret == -(POLARSSL_ERR_DHM_INVALID_FORMAT) ) - snprintf( buf, buflen, "DHM - The ASN.1 data is not formatted correctly" ); + polarssl_snprintf( buf, buflen, "DHM - The ASN.1 data is not formatted correctly" ); if( use_ret == -(POLARSSL_ERR_DHM_MALLOC_FAILED) ) - snprintf( buf, buflen, "DHM - Allocation of memory failed" ); + polarssl_snprintf( buf, buflen, "DHM - Allocation of memory failed" ); if( use_ret == -(POLARSSL_ERR_DHM_FILE_IO_ERROR) ) - snprintf( buf, buflen, "DHM - Read/write of file failed" ); + polarssl_snprintf( buf, buflen, "DHM - Read/write of file failed" ); #endif /* POLARSSL_DHM_C */ #if defined(POLARSSL_ECP_C) if( use_ret == -(POLARSSL_ERR_ECP_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "ECP - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "ECP - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_ECP_BUFFER_TOO_SMALL) ) - snprintf( buf, buflen, "ECP - The buffer is too small to write to" ); + polarssl_snprintf( buf, buflen, "ECP - The buffer is too small to write to" ); if( use_ret == -(POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "ECP - Requested curve not available" ); + polarssl_snprintf( buf, buflen, "ECP - Requested curve not available" ); if( use_ret == -(POLARSSL_ERR_ECP_VERIFY_FAILED) ) - snprintf( buf, buflen, "ECP - The signature is not valid" ); + polarssl_snprintf( buf, buflen, "ECP - The signature is not valid" ); if( use_ret == -(POLARSSL_ERR_ECP_MALLOC_FAILED) ) - snprintf( buf, buflen, "ECP - Memory allocation failed" ); + polarssl_snprintf( buf, buflen, "ECP - Memory allocation failed" ); if( use_ret == -(POLARSSL_ERR_ECP_RANDOM_FAILED) ) - snprintf( buf, buflen, "ECP - Generation of random value, such as (ephemeral) key, failed" ); + polarssl_snprintf( buf, buflen, "ECP - Generation of random value, such as (ephemeral) key, failed" ); if( use_ret == -(POLARSSL_ERR_ECP_INVALID_KEY) ) - snprintf( buf, buflen, "ECP - Invalid private or public key" ); + polarssl_snprintf( buf, buflen, "ECP - Invalid private or public key" ); if( use_ret == -(POLARSSL_ERR_ECP_SIG_LEN_MISMATCH) ) - snprintf( buf, buflen, "ECP - Signature is valid but shorter than the user-supplied length" ); + polarssl_snprintf( buf, buflen, "ECP - Signature is valid but shorter than the user-supplied length" ); #endif /* POLARSSL_ECP_C */ #if defined(POLARSSL_MD_C) if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "MD - The selected feature is not available" ); + polarssl_snprintf( buf, buflen, "MD - The selected feature is not available" ); if( use_ret == -(POLARSSL_ERR_MD_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "MD - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "MD - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_MD_ALLOC_FAILED) ) - snprintf( buf, buflen, "MD - Failed to allocate memory" ); + polarssl_snprintf( buf, buflen, "MD - Failed to allocate memory" ); if( use_ret == -(POLARSSL_ERR_MD_FILE_IO_ERROR) ) - snprintf( buf, buflen, "MD - Opening or reading of file failed" ); + polarssl_snprintf( buf, buflen, "MD - Opening or reading of file failed" ); #endif /* POLARSSL_MD_C */ #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C) if( use_ret == -(POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT) ) - snprintf( buf, buflen, "PEM - No PEM header or footer found" ); + polarssl_snprintf( buf, buflen, "PEM - No PEM header or footer found" ); if( use_ret == -(POLARSSL_ERR_PEM_INVALID_DATA) ) - snprintf( buf, buflen, "PEM - PEM string is not as expected" ); + polarssl_snprintf( buf, buflen, "PEM - PEM string is not as expected" ); if( use_ret == -(POLARSSL_ERR_PEM_MALLOC_FAILED) ) - snprintf( buf, buflen, "PEM - Failed to allocate memory" ); + polarssl_snprintf( buf, buflen, "PEM - Failed to allocate memory" ); if( use_ret == -(POLARSSL_ERR_PEM_INVALID_ENC_IV) ) - snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" ); + polarssl_snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" ); if( use_ret == -(POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG) ) - snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" ); + polarssl_snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" ); if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_REQUIRED) ) - snprintf( buf, buflen, "PEM - Private key password can't be empty" ); + polarssl_snprintf( buf, buflen, "PEM - Private key password can't be empty" ); if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_MISMATCH) ) - snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" ); + polarssl_snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" ); if( use_ret == -(POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" ); + polarssl_snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" ); if( use_ret == -(POLARSSL_ERR_PEM_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "PEM - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "PEM - Bad input parameters to function" ); #endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */ #if defined(POLARSSL_PK_C) if( use_ret == -(POLARSSL_ERR_PK_MALLOC_FAILED) ) - snprintf( buf, buflen, "PK - Memory alloation failed" ); + polarssl_snprintf( buf, buflen, "PK - Memory alloation failed" ); if( use_ret == -(POLARSSL_ERR_PK_TYPE_MISMATCH) ) - snprintf( buf, buflen, "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" ); + polarssl_snprintf( buf, buflen, "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" ); if( use_ret == -(POLARSSL_ERR_PK_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "PK - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "PK - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_PK_FILE_IO_ERROR) ) - snprintf( buf, buflen, "PK - Read/write of file failed" ); + polarssl_snprintf( buf, buflen, "PK - Read/write of file failed" ); if( use_ret == -(POLARSSL_ERR_PK_KEY_INVALID_VERSION) ) - snprintf( buf, buflen, "PK - Unsupported key version" ); + polarssl_snprintf( buf, buflen, "PK - Unsupported key version" ); if( use_ret == -(POLARSSL_ERR_PK_KEY_INVALID_FORMAT) ) - snprintf( buf, buflen, "PK - Invalid key tag or value" ); + polarssl_snprintf( buf, buflen, "PK - Invalid key tag or value" ); if( use_ret == -(POLARSSL_ERR_PK_UNKNOWN_PK_ALG) ) - snprintf( buf, buflen, "PK - Key algorithm is unsupported (only RSA and EC are supported)" ); + polarssl_snprintf( buf, buflen, "PK - Key algorithm is unsupported (only RSA and EC are supported)" ); if( use_ret == -(POLARSSL_ERR_PK_PASSWORD_REQUIRED) ) - snprintf( buf, buflen, "PK - Private key password can't be empty" ); + polarssl_snprintf( buf, buflen, "PK - Private key password can't be empty" ); if( use_ret == -(POLARSSL_ERR_PK_PASSWORD_MISMATCH) ) - snprintf( buf, buflen, "PK - Given private key password does not allow for correct decryption" ); + polarssl_snprintf( buf, buflen, "PK - Given private key password does not allow for correct decryption" ); if( use_ret == -(POLARSSL_ERR_PK_INVALID_PUBKEY) ) - snprintf( buf, buflen, "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" ); + polarssl_snprintf( buf, buflen, "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" ); if( use_ret == -(POLARSSL_ERR_PK_INVALID_ALG) ) - snprintf( buf, buflen, "PK - The algorithm tag or value is invalid" ); + polarssl_snprintf( buf, buflen, "PK - The algorithm tag or value is invalid" ); if( use_ret == -(POLARSSL_ERR_PK_UNKNOWN_NAMED_CURVE) ) - snprintf( buf, buflen, "PK - Elliptic curve is unsupported (only NIST curves are supported)" ); + polarssl_snprintf( buf, buflen, "PK - Elliptic curve is unsupported (only NIST curves are supported)" ); if( use_ret == -(POLARSSL_ERR_PK_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "PK - Unavailable feature, e.g. RSA disabled for RSA key" ); + polarssl_snprintf( buf, buflen, "PK - Unavailable feature, e.g. RSA disabled for RSA key" ); if( use_ret == -(POLARSSL_ERR_PK_SIG_LEN_MISMATCH) ) - snprintf( buf, buflen, "PK - The signature is valid but its length is less than expected" ); + polarssl_snprintf( buf, buflen, "PK - The signature is valid but its length is less than expected" ); #endif /* POLARSSL_PK_C */ #if defined(POLARSSL_PKCS12_C) if( use_ret == -(POLARSSL_ERR_PKCS12_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "PKCS12 - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "PKCS12 - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "PKCS12 - Feature not available, e.g. unsupported encryption scheme" ); + polarssl_snprintf( buf, buflen, "PKCS12 - Feature not available, e.g. unsupported encryption scheme" ); if( use_ret == -(POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT) ) - snprintf( buf, buflen, "PKCS12 - PBE ASN.1 data not as expected" ); + polarssl_snprintf( buf, buflen, "PKCS12 - PBE ASN.1 data not as expected" ); if( use_ret == -(POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH) ) - snprintf( buf, buflen, "PKCS12 - Given private key password does not allow for correct decryption" ); + polarssl_snprintf( buf, buflen, "PKCS12 - Given private key password does not allow for correct decryption" ); #endif /* POLARSSL_PKCS12_C */ #if defined(POLARSSL_PKCS5_C) if( use_ret == -(POLARSSL_ERR_PKCS5_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "PKCS5 - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "PKCS5 - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_PKCS5_INVALID_FORMAT) ) - snprintf( buf, buflen, "PKCS5 - Unexpected ASN.1 data" ); + polarssl_snprintf( buf, buflen, "PKCS5 - Unexpected ASN.1 data" ); if( use_ret == -(POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "PKCS5 - Requested encryption or digest alg not available" ); + polarssl_snprintf( buf, buflen, "PKCS5 - Requested encryption or digest alg not available" ); if( use_ret == -(POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH) ) - snprintf( buf, buflen, "PKCS5 - Given private key password does not allow for correct decryption" ); + polarssl_snprintf( buf, buflen, "PKCS5 - Given private key password does not allow for correct decryption" ); #endif /* POLARSSL_PKCS5_C */ #if defined(POLARSSL_RSA_C) if( use_ret == -(POLARSSL_ERR_RSA_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "RSA - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "RSA - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_RSA_INVALID_PADDING) ) - snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" ); + polarssl_snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" ); if( use_ret == -(POLARSSL_ERR_RSA_KEY_GEN_FAILED) ) - snprintf( buf, buflen, "RSA - Something failed during generation of a key" ); + polarssl_snprintf( buf, buflen, "RSA - Something failed during generation of a key" ); if( use_ret == -(POLARSSL_ERR_RSA_KEY_CHECK_FAILED) ) - snprintf( buf, buflen, "RSA - Key failed to pass the libraries validity check" ); + polarssl_snprintf( buf, buflen, "RSA - Key failed to pass the library's validity check" ); if( use_ret == -(POLARSSL_ERR_RSA_PUBLIC_FAILED) ) - snprintf( buf, buflen, "RSA - The public key operation failed" ); + polarssl_snprintf( buf, buflen, "RSA - The public key operation failed" ); if( use_ret == -(POLARSSL_ERR_RSA_PRIVATE_FAILED) ) - snprintf( buf, buflen, "RSA - The private key operation failed" ); + polarssl_snprintf( buf, buflen, "RSA - The private key operation failed" ); if( use_ret == -(POLARSSL_ERR_RSA_VERIFY_FAILED) ) - snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" ); + polarssl_snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" ); if( use_ret == -(POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE) ) - snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" ); + polarssl_snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" ); if( use_ret == -(POLARSSL_ERR_RSA_RNG_FAILED) ) - snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" ); + polarssl_snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" ); #endif /* POLARSSL_RSA_C */ #if defined(POLARSSL_SSL_TLS_C) if( use_ret == -(POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "SSL - The requested feature is not available" ); + polarssl_snprintf( buf, buflen, "SSL - The requested feature is not available" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "SSL - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "SSL - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_SSL_INVALID_MAC) ) - snprintf( buf, buflen, "SSL - Verification of the message MAC failed" ); + polarssl_snprintf( buf, buflen, "SSL - Verification of the message MAC failed" ); if( use_ret == -(POLARSSL_ERR_SSL_INVALID_RECORD) ) - snprintf( buf, buflen, "SSL - An invalid SSL record was received" ); + polarssl_snprintf( buf, buflen, "SSL - An invalid SSL record was received" ); if( use_ret == -(POLARSSL_ERR_SSL_CONN_EOF) ) - snprintf( buf, buflen, "SSL - The connection indicated an EOF" ); + polarssl_snprintf( buf, buflen, "SSL - The connection indicated an EOF" ); if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_CIPHER) ) - snprintf( buf, buflen, "SSL - An unknown cipher was received" ); + polarssl_snprintf( buf, buflen, "SSL - An unknown cipher was received" ); if( use_ret == -(POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN) ) - snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" ); + polarssl_snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" ); if( use_ret == -(POLARSSL_ERR_SSL_NO_RNG) ) - snprintf( buf, buflen, "SSL - No RNG was provided to the SSL module" ); + polarssl_snprintf( buf, buflen, "SSL - No RNG was provided to the SSL module" ); if( use_ret == -(POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE) ) - snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" ); + polarssl_snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" ); if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE) ) - snprintf( buf, buflen, "SSL - DESCRIPTION MISSING" ); + polarssl_snprintf( buf, buflen, "SSL - Our own certificate(s) is/are too large to send in an SSL message" ); if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED) ) - snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" ); + polarssl_snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" ); if( use_ret == -(POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED) ) - snprintf( buf, buflen, "SSL - The own private key or pre-shared key is not set, but needed" ); + polarssl_snprintf( buf, buflen, "SSL - The own private key or pre-shared key is not set, but needed" ); if( use_ret == -(POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED) ) - snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" ); + polarssl_snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" ); if( use_ret == -(POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE) ) - snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" ); + polarssl_snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" ); if( use_ret == -(POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE) ) { - snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" ); + polarssl_snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" ); return; } if( use_ret == -(POLARSSL_ERR_SSL_PEER_VERIFY_FAILED) ) - snprintf( buf, buflen, "SSL - Verification of our peer failed" ); + polarssl_snprintf( buf, buflen, "SSL - Verification of our peer failed" ); if( use_ret == -(POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) ) - snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" ); + polarssl_snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO) ) - snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO) ) - snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE) ) - snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST) ) - snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE) ) - snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE) ) - snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE) ) - snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP) ) - snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS) ) - snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY) ) - snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC) ) - snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_FINISHED) ) - snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_MALLOC_FAILED) ) - snprintf( buf, buflen, "SSL - Memory allocation failed" ); + polarssl_snprintf( buf, buflen, "SSL - Memory allocation failed" ); if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FAILED) ) - snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" ); + polarssl_snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" ); if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH) ) - snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" ); + polarssl_snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" ); if( use_ret == -(POLARSSL_ERR_SSL_COMPRESSION_FAILED) ) - snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION) ) - snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" ); + polarssl_snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" ); if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET) ) - snprintf( buf, buflen, "SSL - Processing of the NewSessionTicket handshake message failed" ); + polarssl_snprintf( buf, buflen, "SSL - Processing of the NewSessionTicket handshake message failed" ); if( use_ret == -(POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED) ) - snprintf( buf, buflen, "SSL - Session ticket has expired" ); + polarssl_snprintf( buf, buflen, "SSL - Session ticket has expired" ); if( use_ret == -(POLARSSL_ERR_SSL_PK_TYPE_MISMATCH) ) - snprintf( buf, buflen, "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" ); + polarssl_snprintf( buf, buflen, "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" ); if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_IDENTITY) ) - snprintf( buf, buflen, "SSL - Unknown identity received (eg, PSK identity)" ); + polarssl_snprintf( buf, buflen, "SSL - Unknown identity received (eg, PSK identity)" ); if( use_ret == -(POLARSSL_ERR_SSL_INTERNAL_ERROR) ) - snprintf( buf, buflen, "SSL - Internal error (eg, unexpected failure in lower-level module)" ); + polarssl_snprintf( buf, buflen, "SSL - Internal error (eg, unexpected failure in lower-level module)" ); if( use_ret == -(POLARSSL_ERR_SSL_COUNTER_WRAPPING) ) - snprintf( buf, buflen, "SSL - A counter would wrap (eg, too many messages exchanged)" ); + polarssl_snprintf( buf, buflen, "SSL - A counter would wrap (eg, too many messages exchanged)" ); if( use_ret == -(POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO) ) - snprintf( buf, buflen, "SSL - Unexpected message at ServerHello in renegotiation" ); + polarssl_snprintf( buf, buflen, "SSL - Unexpected message at ServerHello in renegotiation" ); + if( use_ret == -(POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE) ) + polarssl_snprintf( buf, buflen, "SSL - None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages)" ); #endif /* POLARSSL_SSL_TLS_C */ #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C) if( use_ret == -(POLARSSL_ERR_X509_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" ); + polarssl_snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" ); if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_OID) ) - snprintf( buf, buflen, "X509 - Requested OID is unknown" ); + polarssl_snprintf( buf, buflen, "X509 - Requested OID is unknown" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_FORMAT) ) - snprintf( buf, buflen, "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" ); + polarssl_snprintf( buf, buflen, "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_VERSION) ) - snprintf( buf, buflen, "X509 - The CRT/CRL/CSR version element is invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The CRT/CRL/CSR version element is invalid" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_SERIAL) ) - snprintf( buf, buflen, "X509 - The serial tag or value is invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The serial tag or value is invalid" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_ALG) ) - snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_NAME) ) - snprintf( buf, buflen, "X509 - The name tag or value is invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The name tag or value is invalid" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_DATE) ) - snprintf( buf, buflen, "X509 - The date tag or value is invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The date tag or value is invalid" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_SIGNATURE) ) - snprintf( buf, buflen, "X509 - The signature tag or value invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The signature tag or value invalid" ); if( use_ret == -(POLARSSL_ERR_X509_INVALID_EXTENSIONS) ) - snprintf( buf, buflen, "X509 - The extension tag or value is invalid" ); + polarssl_snprintf( buf, buflen, "X509 - The extension tag or value is invalid" ); if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_VERSION) ) - snprintf( buf, buflen, "X509 - CRT/CRL/CSR has an unsupported version number" ); + polarssl_snprintf( buf, buflen, "X509 - CRT/CRL/CSR has an unsupported version number" ); if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_SIG_ALG) ) - snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" ); + polarssl_snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" ); if( use_ret == -(POLARSSL_ERR_X509_SIG_MISMATCH) ) - snprintf( buf, buflen, "X509 - Signature algorithms do not match. (see \\c ::x509_crt sig_oid)" ); + polarssl_snprintf( buf, buflen, "X509 - Signature algorithms do not match. (see \\c ::x509_crt sig_oid)" ); if( use_ret == -(POLARSSL_ERR_X509_CERT_VERIFY_FAILED) ) - snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" ); + polarssl_snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" ); if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT) ) - snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" ); + polarssl_snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" ); if( use_ret == -(POLARSSL_ERR_X509_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "X509 - Input invalid" ); + polarssl_snprintf( buf, buflen, "X509 - Input invalid" ); if( use_ret == -(POLARSSL_ERR_X509_MALLOC_FAILED) ) - snprintf( buf, buflen, "X509 - Allocation of memory failed" ); + polarssl_snprintf( buf, buflen, "X509 - Allocation of memory failed" ); if( use_ret == -(POLARSSL_ERR_X509_FILE_IO_ERROR) ) - snprintf( buf, buflen, "X509 - Read/write of file failed" ); + polarssl_snprintf( buf, buflen, "X509 - Read/write of file failed" ); #endif /* POLARSSL_X509_USE,X509_CREATE_C */ // END generated code if( strlen( buf ) == 0 ) - snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); + polarssl_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); } use_ret = ret & ~0xFF80; @@ -513,7 +518,7 @@ void polarssl_strerror( int ret, char *buf, size_t buflen ) if( buflen - len < 5 ) return; - snprintf( buf + len, buflen - len, " : " ); + polarssl_snprintf( buf + len, buflen - len, " : " ); buf += len + 3; buflen -= len + 3; @@ -524,216 +529,216 @@ void polarssl_strerror( int ret, char *buf, size_t buflen ) // BEGIN generated code #if defined(POLARSSL_AES_C) if( use_ret == -(POLARSSL_ERR_AES_INVALID_KEY_LENGTH) ) - snprintf( buf, buflen, "AES - Invalid key length" ); + polarssl_snprintf( buf, buflen, "AES - Invalid key length" ); if( use_ret == -(POLARSSL_ERR_AES_INVALID_INPUT_LENGTH) ) - snprintf( buf, buflen, "AES - Invalid data input length" ); + polarssl_snprintf( buf, buflen, "AES - Invalid data input length" ); #endif /* POLARSSL_AES_C */ #if defined(POLARSSL_ASN1_PARSE_C) if( use_ret == -(POLARSSL_ERR_ASN1_OUT_OF_DATA) ) - snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" ); + polarssl_snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" ); if( use_ret == -(POLARSSL_ERR_ASN1_UNEXPECTED_TAG) ) - snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" ); + polarssl_snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" ); if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_LENGTH) ) - snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" ); + polarssl_snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" ); if( use_ret == -(POLARSSL_ERR_ASN1_LENGTH_MISMATCH) ) - snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" ); + polarssl_snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" ); if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_DATA) ) - snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" ); + polarssl_snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" ); if( use_ret == -(POLARSSL_ERR_ASN1_MALLOC_FAILED) ) - snprintf( buf, buflen, "ASN1 - Memory allocation failed" ); + polarssl_snprintf( buf, buflen, "ASN1 - Memory allocation failed" ); if( use_ret == -(POLARSSL_ERR_ASN1_BUF_TOO_SMALL) ) - snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" ); + polarssl_snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" ); #endif /* POLARSSL_ASN1_PARSE_C */ #if defined(POLARSSL_BASE64_C) if( use_ret == -(POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) ) - snprintf( buf, buflen, "BASE64 - Output buffer too small" ); + polarssl_snprintf( buf, buflen, "BASE64 - Output buffer too small" ); if( use_ret == -(POLARSSL_ERR_BASE64_INVALID_CHARACTER) ) - snprintf( buf, buflen, "BASE64 - Invalid character in input" ); + polarssl_snprintf( buf, buflen, "BASE64 - Invalid character in input" ); #endif /* POLARSSL_BASE64_C */ #if defined(POLARSSL_BIGNUM_C) if( use_ret == -(POLARSSL_ERR_MPI_FILE_IO_ERROR) ) - snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" ); + polarssl_snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" ); if( use_ret == -(POLARSSL_ERR_MPI_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_MPI_INVALID_CHARACTER) ) - snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" ); + polarssl_snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" ); if( use_ret == -(POLARSSL_ERR_MPI_BUFFER_TOO_SMALL) ) - snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" ); + polarssl_snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" ); if( use_ret == -(POLARSSL_ERR_MPI_NEGATIVE_VALUE) ) - snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" ); + polarssl_snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" ); if( use_ret == -(POLARSSL_ERR_MPI_DIVISION_BY_ZERO) ) - snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" ); + polarssl_snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" ); if( use_ret == -(POLARSSL_ERR_MPI_NOT_ACCEPTABLE) ) - snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" ); + polarssl_snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" ); if( use_ret == -(POLARSSL_ERR_MPI_MALLOC_FAILED) ) - snprintf( buf, buflen, "BIGNUM - Memory allocation failed" ); + polarssl_snprintf( buf, buflen, "BIGNUM - Memory allocation failed" ); #endif /* POLARSSL_BIGNUM_C */ #if defined(POLARSSL_BLOWFISH_C) if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH) ) - snprintf( buf, buflen, "BLOWFISH - Invalid key length" ); + polarssl_snprintf( buf, buflen, "BLOWFISH - Invalid key length" ); if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH) ) - snprintf( buf, buflen, "BLOWFISH - Invalid data input length" ); + polarssl_snprintf( buf, buflen, "BLOWFISH - Invalid data input length" ); #endif /* POLARSSL_BLOWFISH_C */ #if defined(POLARSSL_CAMELLIA_C) if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH) ) - snprintf( buf, buflen, "CAMELLIA - Invalid key length" ); + polarssl_snprintf( buf, buflen, "CAMELLIA - Invalid key length" ); if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH) ) - snprintf( buf, buflen, "CAMELLIA - Invalid data input length" ); + polarssl_snprintf( buf, buflen, "CAMELLIA - Invalid data input length" ); #endif /* POLARSSL_CAMELLIA_C */ #if defined(POLARSSL_CCM_C) if( use_ret == -(POLARSSL_ERR_CCM_BAD_INPUT) ) - snprintf( buf, buflen, "CCM - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "CCM - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_CCM_AUTH_FAILED) ) - snprintf( buf, buflen, "CCM - Authenticated decryption failed" ); + polarssl_snprintf( buf, buflen, "CCM - Authenticated decryption failed" ); #endif /* POLARSSL_CCM_C */ #if defined(POLARSSL_CTR_DRBG_C) if( use_ret == -(POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) ) - snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" ); + polarssl_snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" ); if( use_ret == -(POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG) ) - snprintf( buf, buflen, "CTR_DRBG - Too many random requested in single call" ); + polarssl_snprintf( buf, buflen, "CTR_DRBG - Too many random requested in single call" ); if( use_ret == -(POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG) ) - snprintf( buf, buflen, "CTR_DRBG - Input too large (Entropy + additional)" ); + polarssl_snprintf( buf, buflen, "CTR_DRBG - Input too large (Entropy + additional)" ); if( use_ret == -(POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR) ) - snprintf( buf, buflen, "CTR_DRBG - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "CTR_DRBG - Read/write error in file" ); #endif /* POLARSSL_CTR_DRBG_C */ #if defined(POLARSSL_DES_C) if( use_ret == -(POLARSSL_ERR_DES_INVALID_INPUT_LENGTH) ) - snprintf( buf, buflen, "DES - The data input has an invalid length" ); + polarssl_snprintf( buf, buflen, "DES - The data input has an invalid length" ); #endif /* POLARSSL_DES_C */ #if defined(POLARSSL_ENTROPY_C) if( use_ret == -(POLARSSL_ERR_ENTROPY_SOURCE_FAILED) ) - snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" ); + polarssl_snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" ); if( use_ret == -(POLARSSL_ERR_ENTROPY_MAX_SOURCES) ) - snprintf( buf, buflen, "ENTROPY - No more sources can be added" ); + polarssl_snprintf( buf, buflen, "ENTROPY - No more sources can be added" ); if( use_ret == -(POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED) ) - snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" ); + polarssl_snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" ); if( use_ret == -(POLARSSL_ERR_ENTROPY_FILE_IO_ERROR) ) - snprintf( buf, buflen, "ENTROPY - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "ENTROPY - Read/write error in file" ); #endif /* POLARSSL_ENTROPY_C */ #if defined(POLARSSL_GCM_C) if( use_ret == -(POLARSSL_ERR_GCM_AUTH_FAILED) ) - snprintf( buf, buflen, "GCM - Authenticated decryption failed" ); + polarssl_snprintf( buf, buflen, "GCM - Authenticated decryption failed" ); if( use_ret == -(POLARSSL_ERR_GCM_BAD_INPUT) ) - snprintf( buf, buflen, "GCM - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "GCM - Bad input parameters to function" ); #endif /* POLARSSL_GCM_C */ #if defined(POLARSSL_HMAC_DRBG_C) if( use_ret == -(POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG) ) - snprintf( buf, buflen, "HMAC_DRBG - Too many random requested in single call" ); + polarssl_snprintf( buf, buflen, "HMAC_DRBG - Too many random requested in single call" ); if( use_ret == -(POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG) ) - snprintf( buf, buflen, "HMAC_DRBG - Input too large (Entropy + additional)" ); + polarssl_snprintf( buf, buflen, "HMAC_DRBG - Input too large (Entropy + additional)" ); if( use_ret == -(POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR) ) - snprintf( buf, buflen, "HMAC_DRBG - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "HMAC_DRBG - Read/write error in file" ); if( use_ret == -(POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED) ) - snprintf( buf, buflen, "HMAC_DRBG - The entropy source failed" ); + polarssl_snprintf( buf, buflen, "HMAC_DRBG - The entropy source failed" ); #endif /* POLARSSL_HMAC_DRBG_C */ #if defined(POLARSSL_MD2_C) if( use_ret == -(POLARSSL_ERR_MD2_FILE_IO_ERROR) ) - snprintf( buf, buflen, "MD2 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "MD2 - Read/write error in file" ); #endif /* POLARSSL_MD2_C */ #if defined(POLARSSL_MD4_C) if( use_ret == -(POLARSSL_ERR_MD4_FILE_IO_ERROR) ) - snprintf( buf, buflen, "MD4 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "MD4 - Read/write error in file" ); #endif /* POLARSSL_MD4_C */ #if defined(POLARSSL_MD5_C) if( use_ret == -(POLARSSL_ERR_MD5_FILE_IO_ERROR) ) - snprintf( buf, buflen, "MD5 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "MD5 - Read/write error in file" ); #endif /* POLARSSL_MD5_C */ #if defined(POLARSSL_NET_C) if( use_ret == -(POLARSSL_ERR_NET_UNKNOWN_HOST) ) - snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" ); + polarssl_snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" ); if( use_ret == -(POLARSSL_ERR_NET_SOCKET_FAILED) ) - snprintf( buf, buflen, "NET - Failed to open a socket" ); + polarssl_snprintf( buf, buflen, "NET - Failed to open a socket" ); if( use_ret == -(POLARSSL_ERR_NET_CONNECT_FAILED) ) - snprintf( buf, buflen, "NET - The connection to the given server / port failed" ); + polarssl_snprintf( buf, buflen, "NET - The connection to the given server / port failed" ); if( use_ret == -(POLARSSL_ERR_NET_BIND_FAILED) ) - snprintf( buf, buflen, "NET - Binding of the socket failed" ); + polarssl_snprintf( buf, buflen, "NET - Binding of the socket failed" ); if( use_ret == -(POLARSSL_ERR_NET_LISTEN_FAILED) ) - snprintf( buf, buflen, "NET - Could not listen on the socket" ); + polarssl_snprintf( buf, buflen, "NET - Could not listen on the socket" ); if( use_ret == -(POLARSSL_ERR_NET_ACCEPT_FAILED) ) - snprintf( buf, buflen, "NET - Could not accept the incoming connection" ); + polarssl_snprintf( buf, buflen, "NET - Could not accept the incoming connection" ); if( use_ret == -(POLARSSL_ERR_NET_RECV_FAILED) ) - snprintf( buf, buflen, "NET - Reading information from the socket failed" ); + polarssl_snprintf( buf, buflen, "NET - Reading information from the socket failed" ); if( use_ret == -(POLARSSL_ERR_NET_SEND_FAILED) ) - snprintf( buf, buflen, "NET - Sending information through the socket failed" ); + polarssl_snprintf( buf, buflen, "NET - Sending information through the socket failed" ); if( use_ret == -(POLARSSL_ERR_NET_CONN_RESET) ) - snprintf( buf, buflen, "NET - Connection was reset by peer" ); + polarssl_snprintf( buf, buflen, "NET - Connection was reset by peer" ); if( use_ret == -(POLARSSL_ERR_NET_WANT_READ) ) - snprintf( buf, buflen, "NET - Connection requires a read call" ); + polarssl_snprintf( buf, buflen, "NET - Connection requires a read call" ); if( use_ret == -(POLARSSL_ERR_NET_WANT_WRITE) ) - snprintf( buf, buflen, "NET - Connection requires a write call" ); + polarssl_snprintf( buf, buflen, "NET - Connection requires a write call" ); #endif /* POLARSSL_NET_C */ #if defined(POLARSSL_OID_C) if( use_ret == -(POLARSSL_ERR_OID_NOT_FOUND) ) - snprintf( buf, buflen, "OID - OID is not found" ); + polarssl_snprintf( buf, buflen, "OID - OID is not found" ); if( use_ret == -(POLARSSL_ERR_OID_BUF_TOO_SMALL) ) - snprintf( buf, buflen, "OID - output buffer is too small" ); + polarssl_snprintf( buf, buflen, "OID - output buffer is too small" ); #endif /* POLARSSL_OID_C */ #if defined(POLARSSL_PADLOCK_C) if( use_ret == -(POLARSSL_ERR_PADLOCK_DATA_MISALIGNED) ) - snprintf( buf, buflen, "PADLOCK - Input data should be aligned" ); + polarssl_snprintf( buf, buflen, "PADLOCK - Input data should be aligned" ); #endif /* POLARSSL_PADLOCK_C */ #if defined(POLARSSL_PBKDF2_C) if( use_ret == -(POLARSSL_ERR_PBKDF2_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" ); #endif /* POLARSSL_PBKDF2_C */ #if defined(POLARSSL_RIPEMD160_C) if( use_ret == -(POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR) ) - snprintf( buf, buflen, "RIPEMD160 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "RIPEMD160 - Read/write error in file" ); #endif /* POLARSSL_RIPEMD160_C */ #if defined(POLARSSL_SHA1_C) if( use_ret == -(POLARSSL_ERR_SHA1_FILE_IO_ERROR) ) - snprintf( buf, buflen, "SHA1 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "SHA1 - Read/write error in file" ); #endif /* POLARSSL_SHA1_C */ #if defined(POLARSSL_SHA256_C) if( use_ret == -(POLARSSL_ERR_SHA256_FILE_IO_ERROR) ) - snprintf( buf, buflen, "SHA256 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "SHA256 - Read/write error in file" ); #endif /* POLARSSL_SHA256_C */ #if defined(POLARSSL_SHA512_C) if( use_ret == -(POLARSSL_ERR_SHA512_FILE_IO_ERROR) ) - snprintf( buf, buflen, "SHA512 - Read/write error in file" ); + polarssl_snprintf( buf, buflen, "SHA512 - Read/write error in file" ); #endif /* POLARSSL_SHA512_C */ #if defined(POLARSSL_THREADING_C) if( use_ret == -(POLARSSL_ERR_THREADING_FEATURE_UNAVAILABLE) ) - snprintf( buf, buflen, "THREADING - The selected feature is not available" ); + polarssl_snprintf( buf, buflen, "THREADING - The selected feature is not available" ); if( use_ret == -(POLARSSL_ERR_THREADING_BAD_INPUT_DATA) ) - snprintf( buf, buflen, "THREADING - Bad input parameters to function" ); + polarssl_snprintf( buf, buflen, "THREADING - Bad input parameters to function" ); if( use_ret == -(POLARSSL_ERR_THREADING_MUTEX_ERROR) ) - snprintf( buf, buflen, "THREADING - Locking / unlocking / free failed with error code" ); + polarssl_snprintf( buf, buflen, "THREADING - Locking / unlocking / free failed with error code" ); #endif /* POLARSSL_THREADING_C */ #if defined(POLARSSL_XTEA_C) if( use_ret == -(POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH) ) - snprintf( buf, buflen, "XTEA - The data input has an invalid length" ); + polarssl_snprintf( buf, buflen, "XTEA - The data input has an invalid length" ); #endif /* POLARSSL_XTEA_C */ // END generated code if( strlen( buf ) != 0 ) return; - snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); + polarssl_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); } #if defined(POLARSSL_ERROR_STRERROR_BC) @@ -747,8 +752,6 @@ void error_strerror( int ret, char *buf, size_t buflen ) #if defined(POLARSSL_ERROR_STRERROR_DUMMY) -#include - /* * Provide an non-function in case POLARSSL_ERROR_C is not defined */ diff --git a/ext/polarssl/library/gcm.c b/ext/mbedtls/library/gcm.c similarity index 96% rename from ext/polarssl/library/gcm.c rename to ext/mbedtls/library/gcm.c index d48f318d3b..b537b02053 100644 --- a/ext/polarssl/library/gcm.c +++ b/ext/mbedtls/library/gcm.c @@ -1,12 +1,9 @@ /* * NIST SP800-38D compliant GCM implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -43,15 +40,20 @@ #include "polarssl/gcm.h" +#include + #if defined(POLARSSL_AESNI_C) #include "polarssl/aesni.h" #endif +#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */ /* * 32-bit integer manipulation macros (big endian) @@ -134,7 +136,7 @@ static int gcm_gen_table( gcm_context *ctx ) ctx->HH[i] = vh; } - for( i = 2; i < 16; i <<= 1 ) + for( i = 2; i <= 8; i *= 2 ) { uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i; vh = *HiH; @@ -166,6 +168,8 @@ int gcm_init( gcm_context *ctx, cipher_id_t cipher, const unsigned char *key, if( cipher_info->block_size != 16 ) return( POLARSSL_ERR_GCM_BAD_INPUT ); + cipher_free( &ctx->cipher_ctx ); + if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 ) return( ret ); @@ -499,9 +503,6 @@ void gcm_free( gcm_context *ctx ) } #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) - -#include - /* * AES-GCM test vectors from: * @@ -509,10 +510,10 @@ void gcm_free( gcm_context *ctx ) */ #define MAX_TESTS 6 -int key_index[MAX_TESTS] = +static const int key_index[MAX_TESTS] = { 0, 0, 1, 1, 1, 1 }; -unsigned char key[MAX_TESTS][32] = +static const unsigned char key[MAX_TESTS][32] = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -524,13 +525,13 @@ unsigned char key[MAX_TESTS][32] = 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 }, }; -size_t iv_len[MAX_TESTS] = +static const size_t iv_len[MAX_TESTS] = { 12, 12, 12, 12, 8, 60 }; -int iv_index[MAX_TESTS] = +static const int iv_index[MAX_TESTS] = { 0, 0, 1, 1, 1, 2 }; -unsigned char iv[MAX_TESTS][64] = +static const unsigned char iv[MAX_TESTS][64] = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, @@ -546,13 +547,13 @@ unsigned char iv[MAX_TESTS][64] = 0xa6, 0x37, 0xb3, 0x9b }, }; -size_t add_len[MAX_TESTS] = +static const size_t add_len[MAX_TESTS] = { 0, 0, 0, 20, 20, 20 }; -int add_index[MAX_TESTS] = +static const int add_index[MAX_TESTS] = { 0, 0, 0, 1, 1, 1 }; -unsigned char additional[MAX_TESTS][64] = +static const unsigned char additional[MAX_TESTS][64] = { { 0x00 }, { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, @@ -560,13 +561,13 @@ unsigned char additional[MAX_TESTS][64] = 0xab, 0xad, 0xda, 0xd2 }, }; -size_t pt_len[MAX_TESTS] = +static const size_t pt_len[MAX_TESTS] = { 0, 16, 64, 60, 60, 60 }; -int pt_index[MAX_TESTS] = +static const int pt_index[MAX_TESTS] = { 0, 0, 1, 1, 1, 1 }; -unsigned char pt[MAX_TESTS][64] = +static const unsigned char pt[MAX_TESTS][64] = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, @@ -580,7 +581,7 @@ unsigned char pt[MAX_TESTS][64] = 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 }, }; -unsigned char ct[MAX_TESTS * 3][64] = +static const unsigned char ct[MAX_TESTS * 3][64] = { { 0x00 }, { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, @@ -689,7 +690,7 @@ unsigned char ct[MAX_TESTS * 3][64] = 0x44, 0xae, 0x7e, 0x3f }, }; -unsigned char tag[MAX_TESTS * 3][16] = +static const unsigned char tag[MAX_TESTS * 3][16] = { { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61, 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a }, @@ -940,8 +941,6 @@ int gcm_self_test( int verbose ) return( 0 ); } - - #endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */ #endif /* POLARSSL_GCM_C */ diff --git a/ext/polarssl/library/havege.c b/ext/mbedtls/library/havege.c similarity index 97% rename from ext/polarssl/library/havege.c rename to ext/mbedtls/library/havege.c index 3acd5bca10..fe656991c2 100644 --- a/ext/polarssl/library/havege.c +++ b/ext/mbedtls/library/havege.c @@ -1,12 +1,9 @@ /** * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/library/hmac_drbg.c b/ext/mbedtls/library/hmac_drbg.c similarity index 96% rename from ext/polarssl/library/hmac_drbg.c rename to ext/mbedtls/library/hmac_drbg.c index d691be11f1..c7904d0696 100644 --- a/ext/polarssl/library/hmac_drbg.c +++ b/ext/mbedtls/library/hmac_drbg.c @@ -1,12 +1,9 @@ /* * HMAC_DRBG implementation (NIST SP 800-90) * - * Copyright (C) 2014, Brainspark B.V. + * Copyright (C) 2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,15 +36,20 @@ #include "polarssl/hmac_drbg.h" +#include + #if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_SELF_TEST */ +#endif /* POLARSSL_PLATFORM_C */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -312,7 +314,7 @@ void hmac_drbg_free( hmac_drbg_context *ctx ) if( ctx == NULL ) return; - md_free_ctx( &ctx->md_ctx ); + md_free( &ctx->md_ctx ); polarssl_zeroize( ctx, sizeof( hmac_drbg_context ) ); } @@ -379,8 +381,6 @@ int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path ) #if defined(POLARSSL_SELF_TEST) -#include - #if !defined(POLARSSL_SHA1_C) /* Dummy checkup routine */ int hmac_drbg_self_test( int verbose ) @@ -396,7 +396,7 @@ int hmac_drbg_self_test( int verbose ) #define OUTPUT_LEN 80 /* From a NIST PR=true test vector */ -static unsigned char entropy_pr[] = { +static const unsigned char entropy_pr[] = { 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f, 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11, 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42, @@ -412,7 +412,7 @@ static const unsigned char result_pr[OUTPUT_LEN] = { 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 }; /* From a NIST PR=false test vector */ -static unsigned char entropy_nopr[] = { +static const unsigned char entropy_nopr[] = { 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66, 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8, 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3, @@ -461,7 +461,7 @@ int hmac_drbg_self_test( int verbose ) test_offset = 0; CHK( hmac_drbg_init( &ctx, md_info, - hmac_drbg_self_test_entropy, entropy_pr, + hmac_drbg_self_test_entropy, (void *) entropy_pr, NULL, 0 ) ); hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON ); CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); @@ -480,7 +480,7 @@ int hmac_drbg_self_test( int verbose ) test_offset = 0; CHK( hmac_drbg_init( &ctx, md_info, - hmac_drbg_self_test_entropy, entropy_nopr, + hmac_drbg_self_test_entropy, (void *) entropy_nopr, NULL, 0 ) ); CHK( hmac_drbg_reseed( &ctx, NULL, 0 ) ); CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); diff --git a/ext/polarssl/library/md.c b/ext/mbedtls/library/md.c similarity index 96% rename from ext/polarssl/library/md.c rename to ext/mbedtls/library/md.c index 5f3f32db3f..cf4d7e3349 100644 --- a/ext/polarssl/library/md.c +++ b/ext/mbedtls/library/md.c @@ -1,16 +1,13 @@ /** * \file md.c * - * \brief Generic message digest wrapper for PolarSSL + * \brief Generic message digest wrapper for mbed TLS * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +36,7 @@ #include "polarssl/md_wrap.h" #include +#include #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ !defined(EFI32) @@ -205,12 +203,14 @@ int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ) return( 0 ); } +#if ! defined(POLARSSL_DEPRECATED_REMOVED) int md_free_ctx( md_context_t *ctx ) { md_free( ctx ); return( 0 ); } +#endif int md_starts( md_context_t *ctx ) { diff --git a/ext/polarssl/library/md2.c b/ext/mbedtls/library/md2.c similarity index 97% rename from ext/polarssl/library/md2.c rename to ext/mbedtls/library/md2.c index 45bce371c7..17569c0a80 100644 --- a/ext/polarssl/library/md2.c +++ b/ext/mbedtls/library/md2.c @@ -1,12 +1,9 @@ /* * RFC 1115/1319 compliant MD2 implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,15 +36,20 @@ #include "polarssl/md2.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#include + +#if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { diff --git a/ext/polarssl/library/md4.c b/ext/mbedtls/library/md4.c similarity index 94% rename from ext/polarssl/library/md4.c rename to ext/mbedtls/library/md4.c index f6b71d56e5..d33cc520a8 100644 --- a/ext/polarssl/library/md4.c +++ b/ext/mbedtls/library/md4.c @@ -1,12 +1,9 @@ /* * RFC 1186/1320 compliant MD4 implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,15 +36,20 @@ #include "polarssl/md4.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#include + +#if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -70,12 +72,12 @@ static void polarssl_zeroize( void *v, size_t n ) { #endif #ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ +#define PUT_UINT32_LE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ } #endif diff --git a/ext/polarssl/library/md5.c b/ext/mbedtls/library/md5.c similarity index 94% rename from ext/polarssl/library/md5.c rename to ext/mbedtls/library/md5.c index 89354bc7d3..49f0674fb1 100644 --- a/ext/polarssl/library/md5.c +++ b/ext/mbedtls/library/md5.c @@ -1,12 +1,9 @@ /* * RFC 1321 compliant MD5 implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,15 +35,20 @@ #include "polarssl/md5.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#include + +#if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -69,12 +71,12 @@ static void polarssl_zeroize( void *v, size_t n ) { #endif #ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ +#define PUT_UINT32_LE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ } #endif @@ -441,7 +443,7 @@ void md5_hmac( const unsigned char *key, size_t keylen, /* * RFC 1321 test vectors */ -static unsigned char md5_test_buf[7][81] = +static const unsigned char md5_test_buf[7][81] = { { "" }, { "a" }, @@ -479,7 +481,7 @@ static const unsigned char md5_test_sum[7][16] = /* * RFC 2202 test vectors */ -static unsigned char md5_hmac_test_key[7][26] = +static const unsigned char md5_hmac_test_key[7][26] = { { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" }, { "Jefe" }, @@ -496,7 +498,7 @@ static const int md5_hmac_test_keylen[7] = 16, 4, 16, 25, 16, 80, 80 }; -static unsigned char md5_hmac_test_buf[7][74] = +static const unsigned char md5_hmac_test_buf[7][74] = { { "Hi There" }, { "what do ya want for nothing?" }, @@ -578,7 +580,7 @@ int md5_self_test( int verbose ) if( i == 5 || i == 6 ) { - memset( buf, '\xAA', buflen = 80 ); + memset( buf, 0xAA, buflen = 80 ); md5_hmac_starts( &ctx, buf, buflen ); } else diff --git a/ext/polarssl/library/md_wrap.c b/ext/mbedtls/library/md_wrap.c similarity index 97% rename from ext/polarssl/library/md_wrap.c rename to ext/mbedtls/library/md_wrap.c index de701d319a..f554333e78 100644 --- a/ext/polarssl/library/md_wrap.c +++ b/ext/mbedtls/library/md_wrap.c @@ -1,16 +1,13 @@ /** * \file md_wrap.c - * \brief Generic message digest wrapper for PolarSSL + * \brief Generic message digest wrapper for mbed TLS * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -68,12 +65,11 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include - /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -399,7 +395,7 @@ static void ripemd160_hmac_reset_wrap( void *ctx ) static void * ripemd160_ctx_alloc( void ) { ripemd160_context *ctx; - ctx = (ripemd160_context *) polarssl_malloc( sizeof( ripemd160_context ) ); + ctx = polarssl_malloc( sizeof( ripemd160_context ) ); if( ctx == NULL ) return( NULL ); @@ -495,7 +491,7 @@ static void sha1_hmac_reset_wrap( void *ctx ) static void * sha1_ctx_alloc( void ) { sha1_context *ctx; - ctx = (sha1_context *) polarssl_malloc( sizeof( sha1_context ) ); + ctx = polarssl_malloc( sizeof( sha1_context ) ); if( ctx == NULL ) return( NULL ); @@ -704,7 +700,7 @@ static void sha256_hmac_wrap( const unsigned char *key, size_t keylen, static void * sha256_ctx_alloc( void ) { sha256_context *ctx; - ctx = (sha256_context *) polarssl_malloc( sizeof( sha256_context ) ); + ctx = polarssl_malloc( sizeof( sha256_context ) ); if( ctx == NULL ) return( NULL ); @@ -910,7 +906,7 @@ static void sha512_hmac_wrap( const unsigned char *key, size_t keylen, static void * sha512_ctx_alloc( void ) { sha512_context *ctx; - ctx = (sha512_context *) polarssl_malloc( sizeof( sha512_context ) ); + ctx = polarssl_malloc( sizeof( sha512_context ) ); if( ctx == NULL ) return( NULL ); diff --git a/ext/polarssl/library/memory_buffer_alloc.c b/ext/mbedtls/library/memory_buffer_alloc.c similarity index 77% rename from ext/polarssl/library/memory_buffer_alloc.c rename to ext/mbedtls/library/memory_buffer_alloc.c index 00ac3f1385..5eb8ab138c 100644 --- a/ext/polarssl/library/memory_buffer_alloc.c +++ b/ext/mbedtls/library/memory_buffer_alloc.c @@ -1,12 +1,9 @@ /* * Buffer-based memory allocator * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,14 +27,14 @@ #endif #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) - #include "polarssl/memory_buffer_alloc.h" +/* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C + is dependent upon POLARSSL_PLATFORM_C */ +#include "polarssl/platform.h" + #include -#if defined(POLARSSL_MEMORY_DEBUG) -#include -#endif #if defined(POLARSSL_MEMORY_BACKTRACE) #include #endif @@ -46,12 +43,6 @@ #include "polarssl/threading.h" #endif -#if defined(POLARSSL_PLATFORM_C) -#include "polarssl/platform.h" -#else -#define polarssl_fprintf fprintf -#endif - /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -84,7 +75,6 @@ typedef struct size_t len; memory_header *first; memory_header *first_free; - size_t current_alloc_size; int verify; #if defined(POLARSSL_MEMORY_DEBUG) size_t malloc_count; @@ -277,7 +267,7 @@ static void *buffer_alloc_malloc( size_t len ) polarssl_fprintf( stderr, "FATAL: block in free_list but allocated " "data\n" ); #endif - exit( 1 ); + polarssl_exit( 1 ); } #if defined(POLARSSL_MEMORY_DEBUG) @@ -316,7 +306,7 @@ static void *buffer_alloc_malloc( size_t len ) #endif if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) - exit( 1 ); + polarssl_exit( 1 ); return( ( (unsigned char *) cur ) + sizeof(memory_header) ); } @@ -371,7 +361,7 @@ static void *buffer_alloc_malloc( size_t len ) #endif if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) - exit( 1 ); + polarssl_exit( 1 ); return( ( (unsigned char *) cur ) + sizeof(memory_header) ); } @@ -390,14 +380,14 @@ static void buffer_alloc_free( void *ptr ) polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed " "space\n" ); #endif - exit( 1 ); + polarssl_exit( 1 ); } p -= sizeof(memory_header); hdr = (memory_header *) p; if( verify_header( hdr ) != 0 ) - exit( 1 ); + polarssl_exit( 1 ); if( hdr->alloc != 1 ) { @@ -405,7 +395,7 @@ static void buffer_alloc_free( void *ptr ) polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated " "data\n" ); #endif - exit( 1 ); + polarssl_exit( 1 ); } hdr->alloc = 0; @@ -484,7 +474,8 @@ static void buffer_alloc_free( void *ptr ) if( old == NULL ) { hdr->next_free = heap.first_free; - heap.first_free->prev_free = hdr; + if( heap.first_free != NULL ) + heap.first_free->prev_free = hdr; heap.first_free = hdr; } @@ -494,7 +485,7 @@ static void buffer_alloc_free( void *ptr ) #endif if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 ) - exit( 1 ); + polarssl_exit( 1 ); } void memory_buffer_set_verify( int verify ) @@ -527,6 +518,24 @@ void memory_buffer_alloc_status() debug_chain(); } } + +void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks ) +{ + *max_used = heap.maximum_used; + *max_blocks = heap.maximum_header_count; +} + +void memory_buffer_alloc_max_reset( void ) +{ + heap.maximum_used = 0; + heap.maximum_header_count = 0; +} + +void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks ) +{ + *cur_used = heap.total_used; + *cur_blocks = heap.header_count; +} #endif /* POLARSSL_MEMORY_DEBUG */ #if defined(POLARSSL_THREADING_C) @@ -562,9 +571,11 @@ int memory_buffer_alloc_init( unsigned char *buf, size_t len ) if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE ) { + /* Adjust len first since buf is used in the computation */ + len -= POLARSSL_MEMORY_ALIGN_MULTIPLE + - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE; buf += POLARSSL_MEMORY_ALIGN_MULTIPLE - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE; - len -= (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE; } heap.buf = buf; @@ -586,4 +597,138 @@ void memory_buffer_alloc_free() polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) ); } +#if defined(POLARSSL_SELF_TEST) +static int check_pointer( void *p ) +{ + if( p == NULL ) + return( -1 ); + + if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 ) + return( -1 ); + + return( 0 ); +} + +static int check_all_free( ) +{ + if( +#if defined(POLARSSL_MEMORY_DEBUG) + heap.total_used != 0 || +#endif + heap.first != heap.first_free || + (void *) heap.first != (void *) heap.buf ) + { + return( -1 ); + } + + return( 0 ); +} + +#define TEST_ASSERT( condition ) \ + if( ! (condition) ) \ + { \ + if( verbose != 0 ) \ + polarssl_printf( "failed\n" ); \ + \ + ret = 1; \ + goto cleanup; \ + } + +int memory_buffer_alloc_self_test( int verbose ) +{ + unsigned char buf[1024]; + unsigned char *p, *q, *r, *end; + int ret = 0; + + if( verbose != 0 ) + polarssl_printf( " MBA test #1 (basic alloc-free cycle): " ); + + memory_buffer_alloc_init( buf, sizeof( buf ) ); + + p = polarssl_malloc( 1 ); + q = polarssl_malloc( 128 ); + r = polarssl_malloc( 16 ); + + TEST_ASSERT( check_pointer( p ) == 0 && + check_pointer( q ) == 0 && + check_pointer( r ) == 0 ); + + polarssl_free( r ); + polarssl_free( q ); + polarssl_free( p ); + + TEST_ASSERT( check_all_free( ) == 0 ); + + /* Memorize end to compare with the next test */ + end = heap.buf + heap.len; + + memory_buffer_alloc_free( ); + + if( verbose != 0 ) + polarssl_printf( "passed\n" ); + + if( verbose != 0 ) + polarssl_printf( " MBA test #2 (buf not aligned): " ); + + memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 ); + + TEST_ASSERT( heap.buf + heap.len == end ); + + p = polarssl_malloc( 1 ); + q = polarssl_malloc( 128 ); + r = polarssl_malloc( 16 ); + + TEST_ASSERT( check_pointer( p ) == 0 && + check_pointer( q ) == 0 && + check_pointer( r ) == 0 ); + + polarssl_free( r ); + polarssl_free( q ); + polarssl_free( p ); + + TEST_ASSERT( check_all_free( ) == 0 ); + + memory_buffer_alloc_free( ); + + if( verbose != 0 ) + polarssl_printf( "passed\n" ); + + if( verbose != 0 ) + polarssl_printf( " MBA test #3 (full): " ); + + memory_buffer_alloc_init( buf, sizeof( buf ) ); + + p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) ); + + TEST_ASSERT( check_pointer( p ) == 0 ); + TEST_ASSERT( polarssl_malloc( 1 ) == NULL ); + + polarssl_free( p ); + + p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 ); + q = polarssl_malloc( 16 ); + + TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 ); + TEST_ASSERT( polarssl_malloc( 1 ) == NULL ); + + polarssl_free( q ); + + TEST_ASSERT( polarssl_malloc( 17 ) == NULL ); + + polarssl_free( p ); + + TEST_ASSERT( check_all_free( ) == 0 ); + + memory_buffer_alloc_free( ); + + if( verbose != 0 ) + polarssl_printf( "passed\n" ); + +cleanup: + memory_buffer_alloc_free( ); + + return( ret ); +} +#endif /* POLARSSL_SELF_TEST */ + #endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */ diff --git a/ext/polarssl/library/net.c b/ext/mbedtls/library/net.c similarity index 96% rename from ext/polarssl/library/net.c rename to ext/mbedtls/library/net.c index 3f0e448ba9..1fb68842e3 100644 --- a/ext/polarssl/library/net.c +++ b/ext/mbedtls/library/net.c @@ -1,12 +1,9 @@ /* * TCP networking functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +30,8 @@ #include "polarssl/net.h" +#include + #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ !defined(EFI32) @@ -130,6 +129,12 @@ typedef UINT32 uint32_t; (((unsigned long )(n) & 0xFF000000) >> 24)) #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + unsigned short net_htons( unsigned short n ); unsigned long net_htonl( unsigned long n ); #define net_htons(n) POLARSSL_HTONS(n) @@ -174,7 +179,7 @@ int net_connect( int *fd, const char *host, int port ) /* getaddrinfo expects port as a string */ memset( port_str, 0, sizeof( port_str ) ); - snprintf( port_str, sizeof( port_str ), "%d", port ); + polarssl_snprintf( port_str, sizeof( port_str ), "%d", port ); /* Do name resolution with both IPv6 and IPv4, but only TCP */ memset( &hints, 0, sizeof( hints ) ); @@ -260,7 +265,7 @@ int net_bind( int *fd, const char *bind_ip, int port ) /* getaddrinfo expects port as a string */ memset( port_str, 0, sizeof( port_str ) ); - snprintf( port_str, sizeof( port_str ), "%d", port ); + polarssl_snprintf( port_str, sizeof( port_str ), "%d", port ); /* Bind to IPv6 and/or IPv4, but only in TCP */ memset( &hints, 0, sizeof( hints ) ); @@ -496,12 +501,12 @@ int net_set_nonblock( int fd ) void net_usleep( unsigned long usec ) { struct timeval tv; - tv.tv_sec = 0; + tv.tv_sec = usec / 1000000; #if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \ ( defined(__APPLE__) && defined(__MACH__) ) ) - tv.tv_usec = (suseconds_t) usec; + tv.tv_usec = (suseconds_t) usec % 1000000; #else - tv.tv_usec = usec; + tv.tv_usec = usec % 1000000; #endif select( 0, NULL, NULL, NULL, &tv ); } diff --git a/ext/polarssl/library/oid.c b/ext/mbedtls/library/oid.c similarity index 96% rename from ext/polarssl/library/oid.c rename to ext/mbedtls/library/oid.c index 7b54054de2..b616d7ea91 100644 --- a/ext/polarssl/library/oid.c +++ b/ext/mbedtls/library/oid.c @@ -3,12 +3,9 @@ * * \brief Object Identifier (OID) database * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,12 +33,19 @@ #include "polarssl/oid.h" #include "polarssl/rsa.h" +#include +#include + +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C) #include "polarssl/x509.h" #endif -#include - /* * Macro to automatically add the size of #define'd OIDs */ @@ -231,6 +235,10 @@ static const oid_x520_attr_t oid_x520_attr_type[] = { ADD_LEN( OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" }, "DC", }, + { + { ADD_LEN( OID_AT_UNIQUE_IDENTIFIER ), "id-at-uniqueIdentifier", "Unique Identifier" }, + "uniqueIdentifier", + }, { { NULL, 0, NULL, NULL }, NULL, @@ -369,7 +377,7 @@ static const oid_sig_alg_t oid_sig_alg[] = }, { { NULL, 0, NULL, NULL }, - 0, 0, + POLARSSL_MD_NONE, POLARSSL_PK_NONE, }, }; @@ -403,7 +411,7 @@ static const oid_pk_alg_t oid_pk_alg[] = }, { { NULL, 0, NULL, NULL }, - 0, + POLARSSL_PK_NONE, }, }; @@ -468,7 +476,7 @@ static const oid_ecp_grp_t oid_ecp_grp[] = }, { { NULL, 0, NULL, NULL }, - 0, + POLARSSL_ECP_DP_NONE, }, }; @@ -498,7 +506,7 @@ static const oid_cipher_alg_t oid_cipher_alg[] = }, { { NULL, 0, NULL, NULL }, - 0, + POLARSSL_CIPHER_NONE, }, }; @@ -551,7 +559,7 @@ static const oid_md_alg_t oid_md_alg[] = }, { { NULL, 0, NULL, NULL }, - 0, + POLARSSL_MD_NONE, }, }; @@ -582,7 +590,7 @@ static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] = }, { { NULL, 0, NULL, NULL }, - 0, 0, + POLARSSL_MD_NONE, POLARSSL_CIPHER_NONE, }, }; @@ -655,7 +663,7 @@ int oid_get_numeric_string( char *buf, size_t size, /* First byte contains first two dots */ if( oid->len > 0 ) { - ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 ); + ret = polarssl_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 ); SAFE_SNPRINTF(); } @@ -672,7 +680,7 @@ int oid_get_numeric_string( char *buf, size_t size, if( !( oid->p[i] & 0x80 ) ) { /* Last byte */ - ret = snprintf( p, n, ".%d", value ); + ret = polarssl_snprintf( p, n, ".%d", value ); SAFE_SNPRINTF(); value = 0; } diff --git a/ext/polarssl/library/padlock.c b/ext/mbedtls/library/padlock.c similarity index 94% rename from ext/polarssl/library/padlock.c rename to ext/mbedtls/library/padlock.c index 5d06390cad..d83f76c4ec 100644 --- a/ext/polarssl/library/padlock.c +++ b/ext/mbedtls/library/padlock.c @@ -1,12 +1,9 @@ /* * VIA PadLock support functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +36,8 @@ #include "polarssl/padlock.h" +#include + #if defined(POLARSSL_HAVE_X86) /* @@ -105,7 +104,7 @@ int padlock_xcryptecb( aes_context *ctx, "movl %1, %%ebx \n\t" : "=m" (ebx) : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) - : "ecx", "edx", "esi", "edi" ); + : "memory", "ecx", "edx", "esi", "edi" ); memcpy( output, blk, 16 ); @@ -156,7 +155,7 @@ int padlock_xcryptcbc( aes_context *ctx, : "=m" (ebx) : "m" (ebx), "m" (count), "m" (ctrl), "m" (rk), "m" (input), "m" (output), "m" (iw) - : "eax", "ecx", "edx", "esi", "edi" ); + : "memory", "eax", "ecx", "edx", "esi", "edi" ); memcpy( iv, iw, 16 ); diff --git a/ext/polarssl/library/pbkdf2.c b/ext/mbedtls/library/pbkdf2.c similarity index 84% rename from ext/polarssl/library/pbkdf2.c rename to ext/mbedtls/library/pbkdf2.c index e76f066421..783e4a8bec 100644 --- a/ext/polarssl/library/pbkdf2.c +++ b/ext/mbedtls/library/pbkdf2.c @@ -1,17 +1,13 @@ /** * \file pbkdf2.c * - * \brief Password-Based Key Derivation Function 2 (from PKCS#5) - * DEPRECATED: Use pkcs5.c instead + * \brief Compatibility wrappers for pkcs5.c * * \author Mathias Olsson * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,6 +41,7 @@ #include "polarssl/pbkdf2.h" #include "polarssl/pkcs5.h" +#if ! defined(POLARSSL_DEPRECATED_REMOVED) int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen, const unsigned char *salt, size_t slen, unsigned int iteration_count, @@ -53,12 +50,15 @@ int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen, return pkcs5_pbkdf2_hmac( ctx, password, plen, salt, slen, iteration_count, key_length, output ); } +#endif #if defined(POLARSSL_SELF_TEST) +#if ! defined(POLARSSL_DEPRECATED_REMOVED) int pbkdf2_self_test( int verbose ) { return pkcs5_self_test( verbose ); } +#endif #endif /* POLARSSL_SELF_TEST */ #endif /* POLARSSL_PBKDF2_C */ diff --git a/ext/polarssl/library/pem.c b/ext/mbedtls/library/pem.c similarity index 97% rename from ext/polarssl/library/pem.c rename to ext/mbedtls/library/pem.c index 485d829c08..5060484cc0 100644 --- a/ext/polarssl/library/pem.c +++ b/ext/mbedtls/library/pem.c @@ -1,12 +1,9 @@ /* * Privacy Enhanced Mail (PEM) decoding * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,6 +27,7 @@ #endif #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C) + #include "polarssl/pem.h" #include "polarssl/base64.h" #include "polarssl/des.h" @@ -37,15 +35,16 @@ #include "polarssl/md5.h" #include "polarssl/cipher.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include - /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; @@ -237,12 +236,14 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); s1 += strlen( header ); + if( *s1 == ' ' ) s1++; if( *s1 == '\r' ) s1++; if( *s1 == '\n' ) s1++; else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); end = s2; end += strlen( footer ); + if( *end == ' ' ) end++; if( *end == '\r' ) end++; if( *end == '\n' ) end++; *use_len = end - data; @@ -322,7 +323,7 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); - if( ( buf = (unsigned char *) polarssl_malloc( len ) ) == NULL ) + if( ( buf = polarssl_malloc( len ) ) == NULL ) return( POLARSSL_ERR_PEM_MALLOC_FAILED ); if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) diff --git a/ext/polarssl/library/pk.c b/ext/mbedtls/library/pk.c similarity index 91% rename from ext/polarssl/library/pk.c rename to ext/mbedtls/library/pk.c index 4aba3aa6d8..4d78b57455 100644 --- a/ext/polarssl/library/pk.c +++ b/ext/mbedtls/library/pk.c @@ -1,12 +1,9 @@ /* * Public Key abstraction layer * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,7 +27,6 @@ #endif #if defined(POLARSSL_PK_C) - #include "polarssl/pk.h" #include "polarssl/pk_wrap.h" @@ -300,6 +296,32 @@ int pk_encrypt( pk_context *ctx, output, olen, osize, f_rng, p_rng ) ); } +/* + * Check public-private key pair + */ +int pk_check_pair( const pk_context *pub, const pk_context *prv ) +{ + if( pub == NULL || pub->pk_info == NULL || + prv == NULL || prv->pk_info == NULL || + prv->pk_info->check_pair_func == NULL ) + { + return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); + } + + if( prv->pk_info->type == POLARSSL_PK_RSA_ALT ) + { + if( pub->pk_info->type != POLARSSL_PK_RSA ) + return( POLARSSL_ERR_PK_TYPE_MISMATCH ); + } + else + { + if( pub->pk_info != prv->pk_info ) + return( POLARSSL_ERR_PK_TYPE_MISMATCH ); + } + + return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) ); +} + /* * Get key size in bits */ diff --git a/ext/polarssl/library/pk_wrap.c b/ext/mbedtls/library/pk_wrap.c similarity index 87% rename from ext/polarssl/library/pk_wrap.c rename to ext/mbedtls/library/pk_wrap.c index 5e9ff605ee..6068605bfd 100644 --- a/ext/polarssl/library/pk_wrap.c +++ b/ext/mbedtls/library/pk_wrap.c @@ -1,12 +1,9 @@ /* * Public Key abstraction layer: wrapper functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,12 +27,13 @@ #endif #if defined(POLARSSL_PK_C) - #include "polarssl/pk_wrap.h" /* Even if RSA not activated, for the sake of RSA-alt */ #include "polarssl/rsa.h" +#include + #if defined(POLARSSL_ECP_C) #include "polarssl/ecp.h" #endif @@ -117,14 +115,21 @@ static int rsa_encrypt_wrap( void *ctx, unsigned char *output, size_t *olen, size_t osize, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) { - ((void) osize); - *olen = ((rsa_context *) ctx)->len; + if( *olen > osize ) + return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE ); + return( rsa_pkcs1_encrypt( (rsa_context *) ctx, f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) ); } +static int rsa_check_pair_wrap( const void *pub, const void *prv ) +{ + return( rsa_check_pub_priv( (const rsa_context *) pub, + (const rsa_context *) prv ) ); +} + static void *rsa_alloc_wrap( void ) { void *ctx = polarssl_malloc( sizeof( rsa_context ) ); @@ -163,6 +168,7 @@ const pk_info_t rsa_info = { rsa_sign_wrap, rsa_decrypt_wrap, rsa_encrypt_wrap, + rsa_check_pair_wrap, rsa_alloc_wrap, rsa_free_wrap, rsa_debug, @@ -234,6 +240,12 @@ static int eckey_sign_wrap( void *ctx, md_type_t md_alg, #endif /* POLARSSL_ECDSA_C */ +static int eckey_check_pair( const void *pub, const void *prv ) +{ + return( ecp_check_pub_priv( (const ecp_keypair *) pub, + (const ecp_keypair *) prv ) ); +} + static void *eckey_alloc_wrap( void ) { void *ctx = polarssl_malloc( sizeof( ecp_keypair ) ); @@ -271,6 +283,7 @@ const pk_info_t eckey_info = { #endif NULL, NULL, + eckey_check_pair, eckey_alloc_wrap, eckey_free_wrap, eckey_debug, @@ -294,6 +307,7 @@ const pk_info_t eckeydh_info = { NULL, NULL, NULL, + eckey_check_pair, eckey_alloc_wrap, /* Same underlying key structure */ eckey_free_wrap, /* Same underlying key structure */ eckey_debug, /* Same underlying key structure */ @@ -367,6 +381,7 @@ const pk_info_t ecdsa_info = { ecdsa_sign_wrap, NULL, NULL, + eckey_check_pair, /* Compatible key structures */ ecdsa_alloc_wrap, ecdsa_free_wrap, eckey_debug, /* Compatible key structures */ @@ -419,6 +434,36 @@ static int rsa_alt_decrypt_wrap( void *ctx, RSA_PRIVATE, olen, input, output, osize ) ); } +#if defined(POLARSSL_RSA_C) +static int rsa_alt_check_pair( const void *pub, const void *prv ) +{ + unsigned char sig[POLARSSL_MPI_MAX_SIZE]; + unsigned char hash[32]; + size_t sig_len = 0; + int ret; + + if( rsa_alt_get_size( prv ) != rsa_get_size( pub ) ) + return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); + + memset( hash, 0x2a, sizeof( hash ) ); + + if( ( ret = rsa_alt_sign_wrap( (void *) prv, POLARSSL_MD_NONE, + hash, sizeof( hash ), + sig, &sig_len, NULL, NULL ) ) != 0 ) + { + return( ret ); + } + + if( rsa_verify_wrap( (void *) pub, POLARSSL_MD_NONE, + hash, sizeof( hash ), sig, sig_len ) != 0 ) + { + return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); + } + + return( 0 ); +} +#endif /* POLARSSL_RSA_C */ + static void *rsa_alt_alloc_wrap( void ) { void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) ); @@ -444,6 +489,11 @@ const pk_info_t rsa_alt_info = { rsa_alt_sign_wrap, rsa_alt_decrypt_wrap, NULL, +#if defined(POLARSSL_RSA_C) + rsa_alt_check_pair, +#else + NULL, +#endif rsa_alt_alloc_wrap, rsa_alt_free_wrap, NULL, diff --git a/ext/polarssl/library/pkcs11.c b/ext/mbedtls/library/pkcs11.c similarity index 96% rename from ext/polarssl/library/pkcs11.c rename to ext/mbedtls/library/pkcs11.c index 64e7ce3795..14cde315bb 100644 --- a/ext/polarssl/library/pkcs11.c +++ b/ext/mbedtls/library/pkcs11.c @@ -5,12 +5,9 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,6 +27,7 @@ #include "polarssl/pkcs11.h" #if defined(POLARSSL_PKCS11_C) + #include "polarssl/md.h" #include "polarssl/oid.h" #include "polarssl/x509_crt.h" diff --git a/ext/polarssl/library/pkcs12.c b/ext/mbedtls/library/pkcs12.c similarity index 97% rename from ext/polarssl/library/pkcs12.c rename to ext/mbedtls/library/pkcs12.c index 0cf2edf102..f84fd52cdf 100644 --- a/ext/polarssl/library/pkcs12.c +++ b/ext/mbedtls/library/pkcs12.c @@ -1,12 +1,9 @@ /* * PKCS#12 Personal Information Exchange Syntax * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,6 +38,8 @@ #include "polarssl/asn1.h" #include "polarssl/cipher.h" +#include + #if defined(POLARSSL_ARC4_C) #include "polarssl/arc4.h" #endif @@ -199,7 +198,7 @@ int pkcs12_pbe( asn1_buf *pbe_params, int mode, if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 ) goto exit; - if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 ) + if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, (operation_t) mode ) ) != 0 ) goto exit; if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 ) diff --git a/ext/polarssl/library/pkcs5.c b/ext/mbedtls/library/pkcs5.c similarity index 92% rename from ext/polarssl/library/pkcs5.c rename to ext/mbedtls/library/pkcs5.c index e2c4e48c79..c1bab70a38 100644 --- a/ext/polarssl/library/pkcs5.c +++ b/ext/mbedtls/library/pkcs5.c @@ -5,12 +5,9 @@ * * \author Mathias Olsson * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,9 +43,12 @@ #include "polarssl/cipher.h" #include "polarssl/oid.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf #endif @@ -201,7 +201,7 @@ int pkcs5_pbes2( asn1_buf *pbe_params, int mode, if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 ) goto exit; - if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 ) + if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, (operation_t) mode ) ) != 0 ) goto exit; if( ( ret = cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len, @@ -298,44 +298,39 @@ int pkcs5_self_test( int verbose ) } #else -#include - #define MAX_TESTS 6 -size_t plen[MAX_TESTS] = - { 8, 8, 8, 8, 24, 9 }; +static const size_t plen[MAX_TESTS] = + { 8, 8, 8, 24, 9 }; -unsigned char password[MAX_TESTS][32] = +static const unsigned char password[MAX_TESTS][32] = { "password", "password", "password", - "password", "passwordPASSWORDpassword", "pass\0word", }; -size_t slen[MAX_TESTS] = - { 4, 4, 4, 4, 36, 5 }; +static const size_t slen[MAX_TESTS] = + { 4, 4, 4, 36, 5 }; -unsigned char salt[MAX_TESTS][40] = +static const unsigned char salt[MAX_TESTS][40] = { "salt", "salt", "salt", - "salt", "saltSALTsaltSALTsaltSALTsaltSALTsalt", "sa\0lt", }; -uint32_t it_cnt[MAX_TESTS] = - { 1, 2, 4096, 16777216, 4096, 4096 }; - -uint32_t key_len[MAX_TESTS] = - { 20, 20, 20, 20, 25, 16 }; +static const uint32_t it_cnt[MAX_TESTS] = + { 1, 2, 4096, 4096, 4096 }; +static const uint32_t key_len[MAX_TESTS] = + { 20, 20, 20, 25, 16 }; -unsigned char result_key[MAX_TESTS][32] = +static const unsigned char result_key[MAX_TESTS][32] = { { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, @@ -346,9 +341,6 @@ unsigned char result_key[MAX_TESTS][32] = { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1 }, - { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, - 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, - 0x26, 0x34, 0xe9, 0x84 }, { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, @@ -379,9 +371,6 @@ int pkcs5_self_test( int verbose ) goto exit; } - if( verbose != 0 ) - polarssl_printf( " PBKDF2 note: test #3 may be slow!\n" ); - for( i = 0; i < MAX_TESTS; i++ ) { if( verbose != 0 ) diff --git a/ext/polarssl/library/pkparse.c b/ext/mbedtls/library/pkparse.c similarity index 92% rename from ext/polarssl/library/pkparse.c rename to ext/mbedtls/library/pkparse.c index 29217a28a2..39c51f6485 100644 --- a/ext/polarssl/library/pkparse.c +++ b/ext/mbedtls/library/pkparse.c @@ -1,12 +1,9 @@ /* * Public Key layer for parsing key files and structures * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,6 +32,8 @@ #include "polarssl/asn1.h" #include "polarssl/oid.h" +#include + #if defined(POLARSSL_RSA_C) #include "polarssl/rsa.h" #endif @@ -71,7 +70,7 @@ static void polarssl_zeroize( void *v, size_t n ) { /* * Load all data from a file into a given buffer. */ -static int load_file( const char *path, unsigned char **buf, size_t *n ) +int pk_load_file( const char *path, unsigned char **buf, size_t *n ) { FILE *f; long size; @@ -90,7 +89,7 @@ static int load_file( const char *path, unsigned char **buf, size_t *n ) *n = (size_t) size; if( *n + 1 == 0 || - ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) + ( *buf = polarssl_malloc( *n + 1 ) ) == NULL ) { fclose( f ); return( POLARSSL_ERR_PK_MALLOC_FAILED ); @@ -120,7 +119,7 @@ int pk_parse_keyfile( pk_context *ctx, size_t n; unsigned char *buf; - if( ( ret = load_file( path, &buf, &n ) ) != 0 ) + if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 ) return( ret ); if( pwd == NULL ) @@ -144,7 +143,7 @@ int pk_parse_public_keyfile( pk_context *ctx, const char *path ) size_t n; unsigned char *buf; - if( ( ret = load_file( path, &buf, &n ) ) != 0 ) + if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 ) return( ret ); ret = pk_parse_public_key( ctx, buf, n ); @@ -346,7 +345,7 @@ static int pk_group_from_specified( const asn1_buf *params, ecp_group *grp ) /* * order INTEGER */ - if( ( ret = asn1_get_mpi( &p, end, &grp->N ) ) ) + if( ( ret = asn1_get_mpi( &p, end, &grp->N ) ) != 0 ) return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + ret ); grp->nbits = mpi_msb( &grp->N ); @@ -762,58 +761,61 @@ static int pk_parse_key_sec1_der( ecp_keypair *eck, p += len; - /* - * Is 'parameters' present? - */ - if( ( ret = asn1_get_tag( &p, end, &len, - ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 ) + pubkey_done = 0; + if( p != end ) { - if( ( ret = pk_get_ecparams( &p, p + len, ¶ms) ) != 0 || - ( ret = pk_use_ecparams( ¶ms, &eck->grp ) ) != 0 ) + /* + * Is 'parameters' present? + */ + if( ( ret = asn1_get_tag( &p, end, &len, + ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 ) { - ecp_keypair_free( eck ); - return( ret ); + if( ( ret = pk_get_ecparams( &p, p + len, ¶ms) ) != 0 || + ( ret = pk_use_ecparams( ¶ms, &eck->grp ) ) != 0 ) + { + ecp_keypair_free( eck ); + return( ret ); + } } - } - else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) - { - ecp_keypair_free( eck ); - return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + ret ); - } - - /* - * Is 'publickey' present? If not, or if we can't read it (eg because it - * is compressed), create it from the private key. - */ - pubkey_done = 0; - if( ( ret = asn1_get_tag( &p, end, &len, - ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 ) - { - end2 = p + len; - - if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 ) + else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) + { + ecp_keypair_free( eck ); return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + ret ); + } - if( p + len != end2 ) - return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + - POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); - - if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 ) - pubkey_done = 1; - else + /* + * Is 'publickey' present? If not, or if we can't read it (eg because it + * is compressed), create it from the private key. + */ + if( ( ret = asn1_get_tag( &p, end, &len, + ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 ) { - /* - * The only acceptable failure mode of pk_get_ecpubkey() above - * is if the point format is not recognized. - */ - if( ret != POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ) - return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT ); + end2 = p + len; + + if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 ) + return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + ret ); + + if( p + len != end2 ) + return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + + POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); + + if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 ) + pubkey_done = 1; + else + { + /* + * The only acceptable failure mode of pk_get_ecpubkey() above + * is if the point format is not recognized. + */ + if( ret != POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ) + return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT ); + } + } + else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) + { + ecp_keypair_free( eck ); + return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + ret ); } - } - else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) - { - ecp_keypair_free( eck ); - return( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + ret ); } if( ! pubkey_done && @@ -925,6 +927,7 @@ static int pk_parse_key_pkcs8_unencrypted_der( /* * Parse an encrypted PKCS#8 encoded private key */ +#if defined(POLARSSL_PKCS12_C) || defined(POLARSSL_PKCS5_C) static int pk_parse_key_pkcs8_encrypted_der( pk_context *pk, const unsigned char *key, size_t keylen, @@ -1042,6 +1045,7 @@ static int pk_parse_key_pkcs8_encrypted_der( return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) ); } +#endif /* POLARSSL_PKCS12_C || POLARSSL_PKCS5_C */ /* * Parse a private key @@ -1133,6 +1137,7 @@ int pk_parse_key( pk_context *pk, else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) return( ret ); +#if defined(POLARSSL_PKCS12_C) || defined(POLARSSL_PKCS5_C) ret = pem_read_buffer( &pem, "-----BEGIN ENCRYPTED PRIVATE KEY-----", "-----END ENCRYPTED PRIVATE KEY-----", @@ -1151,6 +1156,7 @@ int pk_parse_key( pk_context *pk, } else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) return( ret ); +#endif /* POLARSSL_PKCS12_C || POLARSSL_PKCS5_C */ #else ((void) pwd); ((void) pwdlen); @@ -1163,6 +1169,7 @@ int pk_parse_key( pk_context *pk, * We try the different DER format parsers to see if one passes without * error */ +#if defined(POLARSSL_PKCS12_C) || defined(POLARSSL_PKCS5_C) if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, key, keylen, pwd, pwdlen ) ) == 0 ) { @@ -1175,6 +1182,7 @@ int pk_parse_key( pk_context *pk, { return( ret ); } +#endif /* POLARSSL_PKCS12_C || POLARSSL_PKCS5_C */ if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 ) return( 0 ); diff --git a/ext/polarssl/library/pkwrite.c b/ext/mbedtls/library/pkwrite.c similarity index 98% rename from ext/polarssl/library/pkwrite.c rename to ext/mbedtls/library/pkwrite.c index d6273807b2..35dbd0b755 100644 --- a/ext/polarssl/library/pkwrite.c +++ b/ext/mbedtls/library/pkwrite.c @@ -1,12 +1,9 @@ /* * Public Key layer for writing key files and structures * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,6 +32,8 @@ #include "polarssl/asn1write.h" #include "polarssl/oid.h" +#include + #if defined(POLARSSL_RSA_C) #include "polarssl/rsa.h" #endif diff --git a/ext/polarssl/library/platform.c b/ext/mbedtls/library/platform.c similarity index 66% rename from ext/polarssl/library/platform.c rename to ext/mbedtls/library/platform.c index d57cbc8286..a36dc62ab9 100644 --- a/ext/polarssl/library/platform.c +++ b/ext/mbedtls/library/platform.c @@ -1,12 +1,9 @@ /* * Platform abstraction layer * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -65,6 +62,36 @@ int platform_set_malloc_free( void * (*malloc_func)( size_t ), } #endif /* POLARSSL_PLATFORM_MEMORY */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_snprintf_uninit( char * s, size_t n, + const char * format, ... ) +{ + ((void) s); + ((void) n); + ((void) format); + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_SNPRINTF platform_snprintf_uninit +#endif /* !POLARSSL_PLATFORM_STD_SNPRINTF */ + +int (*polarssl_snprintf)( char * s, size_t n, + const char * format, + ... ) = POLARSSL_PLATFORM_STD_SNPRINTF; + +int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, + ... ) ) +{ + polarssl_snprintf = snprintf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + #if defined(POLARSSL_PLATFORM_PRINTF_ALT) #if !defined(POLARSSL_PLATFORM_STD_PRINTF) /* @@ -113,4 +140,26 @@ int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) } #endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_EXIT_ALT) +#if !defined(POLARSSL_PLATFORM_STD_EXIT) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static void platform_exit_uninit( int status ) +{ + ((void) status); +} + +#define POLARSSL_PLATFORM_STD_EXIT platform_exit_uninit +#endif /* !POLARSSL_PLATFORM_STD_EXIT */ + +void (*polarssl_exit)( int status ) = POLARSSL_PLATFORM_STD_EXIT; + +int platform_set_exit( void (*exit_func)( int status ) ) +{ + polarssl_exit = exit_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_EXIT_ALT */ + #endif /* POLARSSL_PLATFORM_C */ diff --git a/ext/polarssl/library/ripemd160.c b/ext/mbedtls/library/ripemd160.c similarity index 96% rename from ext/polarssl/library/ripemd160.c rename to ext/mbedtls/library/ripemd160.c index fcd77609fc..2c196f42b9 100644 --- a/ext/polarssl/library/ripemd160.c +++ b/ext/mbedtls/library/ripemd160.c @@ -1,12 +1,9 @@ /* * RIPE MD-160 implementation * - * Copyright (C) 2014-2014, Brainspark B.V. + * Copyright (C) 2014-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,19 +36,20 @@ #include "polarssl/ripemd160.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#include + +#if defined(POLARSSL_FS_IO) #include #endif #if defined(POLARSSL_SELF_TEST) -#include -#endif - #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* * 32-bit integer manipulation macros (little endian) @@ -67,12 +65,12 @@ #endif #ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ +#define PUT_UINT32_LE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ } #endif diff --git a/ext/polarssl/library/rsa.c b/ext/mbedtls/library/rsa.c similarity index 97% rename from ext/polarssl/library/rsa.c rename to ext/mbedtls/library/rsa.c index 958085c8ab..8ffb3411aa 100644 --- a/ext/polarssl/library/rsa.c +++ b/ext/mbedtls/library/rsa.c @@ -1,12 +1,9 @@ /* * The RSA public-key cryptosystem * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,16 +37,20 @@ #include "polarssl/rsa.h" #include "polarssl/oid.h" +#include + #if defined(POLARSSL_PKCS1_V21) #include "polarssl/md.h" #endif +#if defined(POLARSSL_PKCS1_V15) && !defined(__OpenBSD__) #include -#include +#endif #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf #endif @@ -240,6 +241,26 @@ cleanup: return( 0 ); } +/* + * Check if contexts holding a public and private key match + */ +int rsa_check_pub_priv( const rsa_context *pub, const rsa_context *prv ) +{ + if( rsa_check_pubkey( pub ) != 0 || + rsa_check_privkey( prv ) != 0 ) + { + return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); + } + + if( mpi_cmp_mpi( &pub->N, &prv->N ) != 0 || + mpi_cmp_mpi( &pub->E, &prv->E ) != 0 ) + { + return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); + } + + return( 0 ); +} + /* * Do an RSA public key operation */ @@ -261,11 +282,18 @@ int rsa_public( rsa_context *ctx, return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); } +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_lock( &ctx->mutex ); +#endif + olen = ctx->len; MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) ); MPI_CHK( mpi_write_binary( &T, output, olen ) ); cleanup: +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_unlock( &ctx->mutex ); +#endif mpi_free( &T ); @@ -275,7 +303,6 @@ cleanup: return( 0 ); } -#if !defined(POLARSSL_RSA_NO_CRT) /* * Generate or update blinding values, see section 10 of: * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA, @@ -329,7 +356,6 @@ cleanup: return( ret ); } -#endif /* !POLARSSL_RSA_NO_CRT */ /* * Do an RSA private key operation @@ -343,7 +369,6 @@ int rsa_private( rsa_context *ctx, int ret; size_t olen; mpi T, T1, T2; -#if !defined(POLARSSL_RSA_NO_CRT) mpi *Vi, *Vf; /* @@ -361,7 +386,6 @@ int rsa_private( rsa_context *ctx, Vi = &ctx->Vi; Vf = &ctx->Vf; #endif -#endif /* !POLARSSL_RSA_NO_CRT */ mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 ); @@ -372,11 +396,6 @@ int rsa_private( rsa_context *ctx, return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); } -#if defined(POLARSSL_RSA_NO_CRT) - ((void) f_rng); - ((void) p_rng); - MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) ); -#else if( f_rng != NULL ) { /* @@ -388,6 +407,13 @@ int rsa_private( rsa_context *ctx, MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) ); } +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_lock( &ctx->mutex ); +#endif + +#if defined(POLARSSL_RSA_NO_CRT) + MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) ); +#else /* * faster decryption using the CRT * @@ -409,6 +435,7 @@ int rsa_private( rsa_context *ctx, */ MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) ); MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) ); +#endif /* POLARSSL_RSA_NO_CRT */ if( f_rng != NULL ) { @@ -419,16 +446,16 @@ int rsa_private( rsa_context *ctx, MPI_CHK( mpi_mul_mpi( &T, &T, Vf ) ); MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) ); } -#endif /* POLARSSL_RSA_NO_CRT */ olen = ctx->len; MPI_CHK( mpi_write_binary( &T, output, olen ) ); cleanup: - mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 ); -#if !defined(POLARSSL_RSA_NO_CRT) && defined(POLARSSL_THREADING_C) +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_unlock( &ctx->mutex ); mpi_free( &Vi_copy ); mpi_free( &Vf_copy ); #endif + mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 ); if( ret != 0 ) return( POLARSSL_ERR_RSA_PRIVATE_FAILED + ret ); @@ -511,7 +538,7 @@ int rsa_rsaes_oaep_encrypt( rsa_context *ctx, if( f_rng == NULL ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); - md_info = md_info_from_type( ctx->hash_id ); + md_info = md_info_from_type( (md_type_t) ctx->hash_id ); if( md_info == NULL ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); @@ -690,7 +717,7 @@ int rsa_rsaes_oaep_decrypt( rsa_context *ctx, if( ilen < 16 || ilen > sizeof( buf ) ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); - md_info = md_info_from_type( ctx->hash_id ); + md_info = md_info_from_type( (md_type_t) ctx->hash_id ); if( md_info == NULL ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); @@ -746,7 +773,7 @@ int rsa_rsaes_oaep_decrypt( rsa_context *ctx, for( i = 0; i < ilen - 2 * hlen - 2; i++ ) { pad_done |= p[i]; - pad_len += ( pad_done == 0 ); + pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; } p += pad_len; @@ -820,8 +847,8 @@ int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx, * (minus one, for the 00 byte) */ for( i = 0; i < ilen - 3; i++ ) { - pad_done |= ( p[i] == 0 ); - pad_count += ( pad_done == 0 ); + pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1; + pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; } p += pad_count; @@ -928,7 +955,7 @@ int rsa_rsassa_pss_sign( rsa_context *ctx, hashlen = md_get_size( md_info ); } - md_info = md_info_from_type( ctx->hash_id ); + md_info = md_info_from_type( (md_type_t) ctx->hash_id ); if( md_info == NULL ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); @@ -1425,10 +1452,8 @@ int rsa_copy( rsa_context *dst, const rsa_context *src ) MPI_CHK( mpi_copy( &dst->RP, &src->RP ) ); MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) ); -#if !defined(POLARSSL_RSA_NO_CRT) MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) ); MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) ); -#endif dst->padding = src->padding; dst->hash_id = src->hash_id; @@ -1445,9 +1470,7 @@ cleanup: */ void rsa_free( rsa_context *ctx ) { -#if !defined(POLARSSL_RSA_NO_CRT) mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf ); -#endif mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN ); mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP ); mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D ); diff --git a/ext/polarssl/library/sha1.c b/ext/mbedtls/library/sha1.c similarity index 97% rename from ext/polarssl/library/sha1.c rename to ext/mbedtls/library/sha1.c index 20408c742f..fca6109bbc 100644 --- a/ext/polarssl/library/sha1.c +++ b/ext/mbedtls/library/sha1.c @@ -1,12 +1,9 @@ /* * FIPS-180-1 compliant SHA-1 implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,15 +35,20 @@ #include "polarssl/sha1.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#include + +#if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -474,7 +476,7 @@ void sha1_hmac( const unsigned char *key, size_t keylen, /* * FIPS-180-1 test vectors */ -static unsigned char sha1_test_buf[3][57] = +static const unsigned char sha1_test_buf[3][57] = { { "abc" }, { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, @@ -499,7 +501,7 @@ static const unsigned char sha1_test_sum[3][20] = /* * RFC 2202 test vectors */ -static unsigned char sha1_hmac_test_key[7][26] = +static const unsigned char sha1_hmac_test_key[7][26] = { { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" "\x0B\x0B\x0B\x0B" }, @@ -519,7 +521,7 @@ static const int sha1_hmac_test_keylen[7] = 20, 4, 20, 25, 20, 80, 80 }; -static unsigned char sha1_hmac_test_buf[7][74] = +static const unsigned char sha1_hmac_test_buf[7][74] = { { "Hi There" }, { "what do ya want for nothing?" }, @@ -620,7 +622,7 @@ int sha1_self_test( int verbose ) if( i == 5 || i == 6 ) { - memset( buf, '\xAA', buflen = 80 ); + memset( buf, 0xAA, buflen = 80 ); sha1_hmac_starts( &ctx, buf, buflen ); } else diff --git a/ext/polarssl/library/sha256.c b/ext/mbedtls/library/sha256.c similarity index 97% rename from ext/polarssl/library/sha256.c rename to ext/mbedtls/library/sha256.c index 4fc66982f2..2b4b7e1328 100644 --- a/ext/polarssl/library/sha256.c +++ b/ext/mbedtls/library/sha256.c @@ -1,12 +1,9 @@ /* * FIPS-180-2 compliant SHA-256 implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,15 +35,20 @@ #include "polarssl/sha256.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#include + +#if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -481,7 +483,7 @@ void sha256_hmac( const unsigned char *key, size_t keylen, /* * FIPS-180-2 test vectors */ -static unsigned char sha256_test_buf[3][57] = +static const unsigned char sha256_test_buf[3][57] = { { "abc" }, { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, @@ -531,7 +533,7 @@ static const unsigned char sha256_test_sum[6][32] = /* * RFC 4231 test vectors */ -static unsigned char sha256_hmac_test_key[7][26] = +static const unsigned char sha256_hmac_test_key[7][26] = { { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" "\x0B\x0B\x0B\x0B" }, @@ -551,7 +553,7 @@ static const int sha256_hmac_test_keylen[7] = 20, 4, 20, 25, 20, 131, 131 }; -static unsigned char sha256_hmac_test_buf[7][153] = +static const unsigned char sha256_hmac_test_buf[7][153] = { { "Hi There" }, { "what do ya want for nothing?" }, @@ -701,7 +703,7 @@ int sha256_self_test( int verbose ) if( j == 5 || j == 6 ) { - memset( buf, '\xAA', buflen = 131 ); + memset( buf, 0xAA, buflen = 131 ); sha256_hmac_starts( &ctx, buf, buflen, k ); } else diff --git a/ext/polarssl/library/sha512.c b/ext/mbedtls/library/sha512.c similarity index 97% rename from ext/polarssl/library/sha512.c rename to ext/mbedtls/library/sha512.c index f1d15256f8..4074d33d95 100644 --- a/ext/polarssl/library/sha512.c +++ b/ext/mbedtls/library/sha512.c @@ -1,12 +1,9 @@ /* * FIPS-180-2 compliant SHA-384/512 implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,15 +35,26 @@ #include "polarssl/sha512.h" -#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) +#if defined(_MSC_VER) || defined(__WATCOMC__) + #define UL64(x) x##ui64 +#else + #define UL64(x) x##ULL +#endif + +#include + +#if defined(POLARSSL_FS_IO) #include #endif +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { @@ -480,7 +488,7 @@ void sha512_hmac( const unsigned char *key, size_t keylen, /* * FIPS-180-2 test vectors */ -static unsigned char sha512_test_buf[3][113] = +static const unsigned char sha512_test_buf[3][113] = { { "abc" }, { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" @@ -549,7 +557,7 @@ static const unsigned char sha512_test_sum[6][64] = /* * RFC 4231 test vectors */ -static unsigned char sha512_hmac_test_key[7][26] = +static const unsigned char sha512_hmac_test_key[7][26] = { { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" "\x0B\x0B\x0B\x0B" }, @@ -569,7 +577,7 @@ static const int sha512_hmac_test_keylen[7] = 20, 4, 20, 25, 20, 131, 131 }; -static unsigned char sha512_hmac_test_buf[7][153] = +static const unsigned char sha512_hmac_test_buf[7][153] = { { "Hi There" }, { "what do ya want for nothing?" }, @@ -755,7 +763,7 @@ int sha512_self_test( int verbose ) if( j == 5 || j == 6 ) { - memset( buf, '\xAA', buflen = 131 ); + memset( buf, 0xAA, buflen = 131 ); sha512_hmac_starts( &ctx, buf, buflen, k ); } else diff --git a/ext/polarssl/library/ssl_cache.c b/ext/mbedtls/library/ssl_cache.c similarity index 92% rename from ext/polarssl/library/ssl_cache.c rename to ext/mbedtls/library/ssl_cache.c index 836b68511b..0c2df29bb1 100644 --- a/ext/polarssl/library/ssl_cache.c +++ b/ext/mbedtls/library/ssl_cache.c @@ -1,12 +1,9 @@ /* * SSL session cache implementation * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,15 +34,16 @@ #include "polarssl/ssl_cache.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include - void ssl_cache_init( ssl_cache_context *cache ) { memset( cache, 0, sizeof( ssl_cache_context ) ); @@ -105,10 +103,8 @@ int ssl_cache_get( void *data, ssl_session *session ) */ if( entry->peer_cert.p != NULL ) { - session->peer_cert = - (x509_crt *) polarssl_malloc( sizeof(x509_crt) ); - - if( session->peer_cert == NULL ) + if( ( session->peer_cert = polarssl_malloc( + sizeof(x509_crt) ) ) == NULL ) { ret = 1; goto exit; @@ -226,8 +222,7 @@ int ssl_cache_set( void *data, const ssl_session *session ) /* * max_entries not reached, create new entry */ - cur = (ssl_cache_entry *) - polarssl_malloc( sizeof(ssl_cache_entry) ); + cur = polarssl_malloc( sizeof(ssl_cache_entry) ); if( cur == NULL ) { ret = 1; @@ -264,8 +259,7 @@ int ssl_cache_set( void *data, const ssl_session *session ) */ if( session->peer_cert != NULL ) { - cur->peer_cert.p = (unsigned char *) - polarssl_malloc( session->peer_cert->raw.len ); + cur->peer_cert.p = polarssl_malloc( session->peer_cert->raw.len ); if( cur->peer_cert.p == NULL ) { ret = 1; diff --git a/ext/polarssl/library/ssl_ciphersuites.c b/ext/mbedtls/library/ssl_ciphersuites.c similarity index 99% rename from ext/polarssl/library/ssl_ciphersuites.c rename to ext/mbedtls/library/ssl_ciphersuites.c index 7907980cd4..0fee1e6de0 100644 --- a/ext/polarssl/library/ssl_ciphersuites.c +++ b/ext/mbedtls/library/ssl_ciphersuites.c @@ -1,14 +1,11 @@ /** * \file ssl_ciphersuites.c * - * \brief SSL ciphersuites for PolarSSL + * \brief SSL ciphersuites for mbed TLS * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,7 +33,8 @@ #include "polarssl/ssl_ciphersuites.h" #include "polarssl/ssl.h" -#include +// #include +#include #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ !defined(EFI32) @@ -1676,7 +1674,9 @@ static const ssl_ciphersuite_t ciphersuite_definitions[] = #endif /* POLARSSL_DES_C */ #endif /* POLARSSL_ENABLE_WEAK_CIPHERSUITES */ - { 0, "", 0, 0, 0, 0, 0, 0, 0, 0 } + { 0, "", + POLARSSL_CIPHER_NONE, POLARSSL_MD_NONE, POLARSSL_KEY_EXCHANGE_NONE, + 0, 0, 0, 0, 0 } }; #if defined(SSL_CIPHERSUITES) diff --git a/ext/polarssl/library/ssl_cli.c b/ext/mbedtls/library/ssl_cli.c similarity index 92% rename from ext/polarssl/library/ssl_cli.c rename to ext/mbedtls/library/ssl_cli.c index 27abb3efe4..0c62c3448b 100644 --- a/ext/polarssl/library/ssl_cli.c +++ b/ext/mbedtls/library/ssl_cli.c @@ -1,12 +1,9 @@ /* * SSLv3/TLSv1 client-side functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,16 +31,16 @@ #include "polarssl/debug.h" #include "polarssl/ssl.h" +#include + #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include -#include - #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) #include typedef UINT32 uint32_t; @@ -114,6 +111,7 @@ static void ssl_write_hostname_ext( ssl_context *ssl, } #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */ +#if defined(POLARSSL_SSL_RENEGOTIATION) static void ssl_write_renegotiation_ext( ssl_context *ssl, unsigned char *buf, size_t *olen ) @@ -141,8 +139,13 @@ static void ssl_write_renegotiation_ext( ssl_context *ssl, *olen = 5 + ssl->verify_data_len; } +#endif /* POLARSSL_SSL_RENEGOTIATION */ -#if defined(POLARSSL_SSL_PROTO_TLS1_2) +/* + * Only if we handle at least one key exchange that needs signatures. + */ +#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \ + defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED) static void ssl_write_signature_algorithms_ext( ssl_context *ssl, unsigned char *buf, size_t *olen ) @@ -236,7 +239,8 @@ static void ssl_write_signature_algorithms_ext( ssl_context *ssl, *olen = 6 + sig_alg_len; } -#endif /* POLARSSL_SSL_PROTO_TLS1_2 */ +#endif /* POLARSSL_SSL_PROTO_TLS1_2 && + POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */ #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl, @@ -359,6 +363,58 @@ static void ssl_write_truncated_hmac_ext( ssl_context *ssl, } #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) +static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl, + unsigned char *buf, size_t *olen ) +{ + unsigned char *p = buf; + + if( ssl->encrypt_then_mac == SSL_ETM_DISABLED || + ssl->max_minor_ver == SSL_MINOR_VERSION_0 ) + { + *olen = 0; + return; + } + + SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac " + "extension" ) ); + + *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); + + *p++ = 0x00; + *p++ = 0x00; + + *olen = 4; +} +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) +static void ssl_write_extended_ms_ext( ssl_context *ssl, + unsigned char *buf, size_t *olen ) +{ + unsigned char *p = buf; + + if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED || + ssl->max_minor_ver == SSL_MINOR_VERSION_0 ) + { + *olen = 0; + return; + } + + SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret " + "extension" ) ); + + *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); + + *p++ = 0x00; + *p++ = 0x00; + + *olen = 4; +} +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) static void ssl_write_session_ticket_ext( ssl_context *ssl, unsigned char *buf, size_t *olen ) @@ -464,7 +520,9 @@ static int ssl_write_client_hello( ssl_context *ssl ) return( POLARSSL_ERR_SSL_NO_RNG ); } +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#endif { ssl->major_ver = ssl->min_major_ver; ssl->minor_ver = ssl->min_minor_ver; @@ -528,7 +586,10 @@ static int ssl_write_client_hello( ssl_context *ssl ) */ n = ssl->session_negotiate->length; - if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 || + if( n < 16 || n > 32 || +#if defined(POLARSSL_SSL_RENEGOTIATION) + ssl->renegotiation != SSL_INITIAL_HANDSHAKE || +#endif ssl->handshake->resume == 0 ) { n = 0; @@ -539,16 +600,20 @@ static int ssl_write_client_hello( ssl_context *ssl ) * RFC 5077 section 3.4: "When presenting a ticket, the client MAY * generate and include a Session ID in the TLS ClientHello." */ - if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE && - ssl->session_negotiate->ticket != NULL && - ssl->session_negotiate->ticket_len != 0 ) +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#endif { - ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 ); + if( ssl->session_negotiate->ticket != NULL && + ssl->session_negotiate->ticket_len != 0 ) + { + ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 ); - if( ret != 0 ) - return( ret ); + if( ret != 0 ) + return( ret ); - ssl->session_negotiate->length = n = 32; + ssl->session_negotiate->length = n = 32; + } } #endif /* POLARSSL_SSL_SESSION_TICKETS */ @@ -567,16 +632,6 @@ static int ssl_write_client_hello( ssl_context *ssl ) // Skip writing ciphersuite length for now p += 2; - /* - * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV - */ - if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) - { - *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); - *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO ); - n++; - } - for( i = 0; ciphersuites[i] != 0; i++ ) { ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] ); @@ -588,6 +643,10 @@ static int ssl_write_client_hello( ssl_context *ssl ) ciphersuite_info->max_minor_ver < ssl->min_minor_ver ) continue; + if( ssl->arc4_disabled == SSL_ARC4_DISABLED && + ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) + continue; + SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d", ciphersuites[i] ) ); @@ -596,6 +655,29 @@ static int ssl_write_client_hello( ssl_context *ssl ) *p++ = (unsigned char)( ciphersuites[i] ); } + /* + * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV + */ +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#endif + { + *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); + *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO ); + n++; + } + + /* Some versions of OpenSSL don't handle it correctly if not at end */ +#if defined(POLARSSL_SSL_FALLBACK_SCSV) + if( ssl->fallback == SSL_IS_FALLBACK ) + { + SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) ); + *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 ); + *p++ = (unsigned char)( SSL_FALLBACK_SCSV ); + n++; + } +#endif + *q++ = (unsigned char)( n >> 7 ); *q++ = (unsigned char)( n << 1 ); @@ -625,10 +707,13 @@ static int ssl_write_client_hello( ssl_context *ssl ) ext_len += olen; #endif +#if defined(POLARSSL_SSL_RENEGOTIATION) ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); ext_len += olen; +#endif -#if defined(POLARSSL_SSL_PROTO_TLS1_2) +#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \ + defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED) ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen ); ext_len += olen; #endif @@ -651,6 +736,16 @@ static int ssl_write_client_hello( ssl_context *ssl ) ext_len += olen; #endif +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); + ext_len += olen; +#endif + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); + ext_len += olen; +#endif + #if defined(POLARSSL_SSL_SESSION_TICKETS) ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); ext_len += olen; @@ -661,6 +756,9 @@ static int ssl_write_client_hello( ssl_context *ssl ) ext_len += olen; #endif + /* olen unused if all extensions are disabled */ + ((void) olen); + SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d", ext_len ) ); @@ -694,37 +792,39 @@ static int ssl_parse_renegotiation_info( ssl_context *ssl, { int ret; - if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) { - if( len != 1 || buf[0] != 0x0 ) + /* Check verify-data in constant-time. The length OTOH is no secret */ + if( len != 1 + ssl->verify_data_len * 2 || + buf[0] != ssl->verify_data_len * 2 || + safer_memcmp( buf + 1, + ssl->own_verify_data, ssl->verify_data_len ) != 0 || + safer_memcmp( buf + 1 + ssl->verify_data_len, + ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) { - SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) ); + SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) return( ret ); return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); } - - ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; } else +#endif /* POLARSSL_SSL_RENEGOTIATION */ { - /* Check verify-data in constant-time. The length OTOH is no secret */ - if( len != 1 + ssl->verify_data_len * 2 || - buf[0] != ssl->verify_data_len * 2 || - safer_memcmp( buf + 1, - ssl->own_verify_data, ssl->verify_data_len ) != 0 || - safer_memcmp( buf + 1 + ssl->verify_data_len, - ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) + if( len != 1 || buf[0] != 0x00 ) { - SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) ); + SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) return( ret ); return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); } + + ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; } return( 0 ); @@ -769,6 +869,46 @@ static int ssl_parse_truncated_hmac_ext( ssl_context *ssl, } #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) +static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl, + const unsigned char *buf, + size_t len ) +{ + if( ssl->encrypt_then_mac == SSL_ETM_DISABLED || + ssl->minor_ver == SSL_MINOR_VERSION_0 || + len != 0 ) + { + return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); + } + + ((void) buf); + + ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED; + + return( 0 ); +} +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) +static int ssl_parse_extended_ms_ext( ssl_context *ssl, + const unsigned char *buf, + size_t len ) +{ + if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED || + ssl->minor_ver == SSL_MINOR_VERSION_0 || + len != 0 ) + { + return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); + } + + ((void) buf); + + ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED; + + return( 0 ); +} +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) static int ssl_parse_session_ticket_ext( ssl_context *ssl, const unsigned char *buf, @@ -877,8 +1017,11 @@ static int ssl_parse_server_hello( ssl_context *ssl ) size_t n; size_t ext_len; unsigned char *buf, *ext; +#if defined(POLARSSL_SSL_RENEGOTIATION) int renegotiation_info_seen = 0; +#endif int handshake_failure = 0; + const ssl_ciphersuite_t *suite_info; #if defined(POLARSSL_DEBUG_C) uint32_t t; #endif @@ -902,6 +1045,7 @@ static int ssl_parse_server_hello( ssl_context *ssl ) if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) { +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation == SSL_RENEGOTIATION ) { ssl->renego_records_seen++; @@ -917,6 +1061,7 @@ static int ssl_parse_server_hello( ssl_context *ssl ) SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) ); return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); } +#endif /* POLARSSL_SSL_RENEGOTIATION */ SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); @@ -1025,8 +1170,10 @@ static int ssl_parse_server_hello( ssl_context *ssl ) /* * Check if the session can be resumed */ - if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || - ssl->handshake->resume == 0 || n == 0 || + if( ssl->handshake->resume == 0 || n == 0 || +#if defined(POLARSSL_SSL_RENEGOTIATION) + ssl->renegotiation != SSL_INITIAL_HANDSHAKE || +#endif ssl->session_negotiate->ciphersuite != i || ssl->session_negotiate->compression != comp || ssl->session_negotiate->length != n || @@ -1059,6 +1206,16 @@ static int ssl_parse_server_hello( ssl_context *ssl ) SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) ); SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) ); + suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ); + if( suite_info == NULL || + ( ssl->arc4_disabled && + suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) ) + { + SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); + return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); + } + + i = 0; while( 1 ) { @@ -1107,7 +1264,9 @@ static int ssl_parse_server_hello( ssl_context *ssl ) { case TLS_EXT_RENEGOTIATION_INFO: SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) renegotiation_info_seen = 1; +#endif if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ) ) != 0 ) @@ -1141,6 +1300,32 @@ static int ssl_parse_server_hello( ssl_context *ssl ) break; #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + case TLS_EXT_ENCRYPT_THEN_MAC: + SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); + + if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, + ext + 4, ext_size ) ) != 0 ) + { + return( ret ); + } + + break; +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + case TLS_EXT_EXTENDED_MASTER_SECRET: + SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) ); + + if( ( ret = ssl_parse_extended_ms_ext( ssl, + ext + 4, ext_size ) ) != 0 ) + { + return( ret ); + } + + break; +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) case TLS_EXT_SESSION_TICKET: SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); @@ -1201,6 +1386,7 @@ static int ssl_parse_server_hello( ssl_context *ssl ) SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); handshake_failure = 1; } +#if defined(POLARSSL_SSL_RENEGOTIATION) else if( ssl->renegotiation == SSL_RENEGOTIATION && ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION && renegotiation_info_seen == 0 ) @@ -1222,6 +1408,7 @@ static int ssl_parse_server_hello( ssl_context *ssl ) SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); handshake_failure = 1; } +#endif /* POLARSSL_SSL_RENEGOTIATION */ if( handshake_failure == 1 ) { @@ -1258,7 +1445,7 @@ static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p, return( ret ); } - if( ssl->handshake->dhm_ctx.len < 64 || + if( ssl->handshake->dhm_ctx.len < SSL_MIN_DHM_BYTES || ssl->handshake->dhm_ctx.len > 512 ) { SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) ); @@ -1292,7 +1479,7 @@ static int ssl_check_server_ecdh_params( const ssl_context *ssl ) SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) ); -#if defined(POLARSSL_SSL_ECP_SET_CURVES) +#if defined(POLARSSL_SSL_SET_CURVES) if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) ) #else if( ssl->handshake->ecdh_ctx.grp.nbits < 163 || @@ -2253,12 +2440,6 @@ static int ssl_write_client_key_exchange( ssl_context *ssl ) return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); } - if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) - { - SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); - return( ret ); - } - ssl->out_msglen = i + n; ssl->out_msgtype = SSL_MSG_HANDSHAKE; ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE; @@ -2283,9 +2464,16 @@ static int ssl_write_client_key_exchange( ssl_context *ssl ) static int ssl_write_certificate_verify( ssl_context *ssl ) { const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; + int ret; SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); + if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); + return( ret ); + } + if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK || ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK || @@ -2312,6 +2500,12 @@ static int ssl_write_certificate_verify( ssl_context *ssl ) SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); + if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); + return( ret ); + } + if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK || ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK || diff --git a/ext/polarssl/library/ssl_srv.c b/ext/mbedtls/library/ssl_srv.c similarity index 87% rename from ext/polarssl/library/ssl_srv.c rename to ext/mbedtls/library/ssl_srv.c index 01b0aca207..fc217549da 100644 --- a/ext/polarssl/library/ssl_srv.c +++ b/ext/mbedtls/library/ssl_srv.c @@ -1,12 +1,9 @@ /* * SSLv3/TLSv1 server-side functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +30,9 @@ #include "polarssl/debug.h" #include "polarssl/ssl.h" + +#include + #if defined(POLARSSL_ECP_C) #include "polarssl/ecp.h" #endif @@ -40,13 +40,11 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include -#include - #if defined(POLARSSL_HAVE_TIME) #include #endif @@ -431,41 +429,44 @@ static int ssl_parse_renegotiation_info( ssl_context *ssl, { int ret; - if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) { - if( len != 1 || buf[0] != 0x0 ) + /* Check verify-data in constant-time. The length OTOH is no secret */ + if( len != 1 + ssl->verify_data_len || + buf[0] != ssl->verify_data_len || + safer_memcmp( buf + 1, ssl->peer_verify_data, + ssl->verify_data_len ) != 0 ) { - SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) ); + SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) return( ret ); return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); } - - ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; } else +#endif /* POLARSSL_SSL_RENEGOTIATION */ { - /* Check verify-data in constant-time. The length OTOH is no secret */ - if( len != 1 + ssl->verify_data_len || - buf[0] != ssl->verify_data_len || - safer_memcmp( buf + 1, ssl->peer_verify_data, - ssl->verify_data_len ) != 0 ) + if( len != 1 || buf[0] != 0x0 ) { - SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) ); + SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) return( ret ); return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); } + + ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; } return( 0 ); } -#if defined(POLARSSL_SSL_PROTO_TLS1_2) +#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \ + defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED) static int ssl_parse_signature_algorithms_ext( ssl_context *ssl, const unsigned char *buf, size_t len ) @@ -509,7 +510,8 @@ have_sig_alg: return( 0 ); } -#endif /* POLARSSL_SSL_PROTO_TLS1_2 */ +#endif /* POLARSSL_SSL_PROTO_TLS1_2 && + POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */ #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) static int ssl_parse_supported_elliptic_curves( ssl_context *ssl, @@ -629,12 +631,59 @@ static int ssl_parse_truncated_hmac_ext( ssl_context *ssl, ((void) buf); - ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED; + if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED ) + ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED; return( 0 ); } #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) +static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl, + const unsigned char *buf, + size_t len ) +{ + if( len != 0 ) + { + SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); + return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); + } + + ((void) buf); + + if( ssl->encrypt_then_mac == SSL_ETM_ENABLED && + ssl->minor_ver != SSL_MINOR_VERSION_0 ) + { + ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED; + } + + return( 0 ); +} +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) +static int ssl_parse_extended_ms_ext( ssl_context *ssl, + const unsigned char *buf, + size_t len ) +{ + if( len != 0 ) + { + SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); + return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); + } + + ((void) buf); + + if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED && + ssl->minor_ver != SSL_MINOR_VERSION_0 ) + { + ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED; + } + + return( 0 ); +} +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) static int ssl_parse_session_ticket_ext( ssl_context *ssl, unsigned char *buf, @@ -653,11 +702,13 @@ static int ssl_parse_session_ticket_ext( ssl_context *ssl, if( len == 0 ) return( 0 ); +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) { SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); return( 0 ); } +#endif /* POLARSSL_SSL_RENEGOTIATION */ /* * Failures are ok: just ignore the ticket and proceed. @@ -749,11 +800,11 @@ static int ssl_parse_alpn_ext( ssl_context *ssl, #if defined(POLARSSL_X509_CRT_PARSE_C) /* - * Return 1 if the given EC key uses the given curve, 0 otherwise + * Return 0 if the given key uses one of the acceptable curves, -1 otherwise */ #if defined(POLARSSL_ECDSA_C) -static int ssl_key_matches_curves( pk_context *pk, - const ecp_curve_info **curves ) +static int ssl_check_key_curve( pk_context *pk, + const ecp_curve_info **curves ) { const ecp_curve_info **crv = curves; ecp_group_id grp_id = pk_ec( *pk )->grp.id; @@ -761,11 +812,11 @@ static int ssl_key_matches_curves( pk_context *pk, while( *crv != NULL ) { if( (*crv)->grp_id == grp_id ) - return( 1 ); + return( 0 ); crv++; } - return( 0 ); + return( -1 ); } #endif /* POLARSSL_ECDSA_C */ @@ -776,8 +827,9 @@ static int ssl_key_matches_curves( pk_context *pk, static int ssl_pick_cert( ssl_context *ssl, const ssl_ciphersuite_t * ciphersuite_info ) { - ssl_key_cert *cur, *list; + ssl_key_cert *cur, *list, *fallback = NULL; pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); + int flags; #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) if( ssl->handshake->sni_key_cert != NULL ) @@ -789,10 +841,18 @@ static int ssl_pick_cert( ssl_context *ssl, if( pk_alg == POLARSSL_PK_NONE ) return( 0 ); + SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); + for( cur = list; cur != NULL; cur = cur->next ) { + SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", + cur->cert ); + if( ! pk_can_do( cur->key, pk_alg ) ) + { + SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); continue; + } /* * This avoids sending the client a cert it'll reject based on @@ -803,27 +863,57 @@ static int ssl_pick_cert( ssl_context *ssl, * and decrypting with the same RSA key. */ if( ssl_check_cert_usage( cur->cert, ciphersuite_info, - SSL_IS_SERVER ) != 0 ) + SSL_IS_SERVER, &flags ) != 0 ) { + SSL_DEBUG_MSG( 3, ( "certificate mismatch: " + "(extended) key usage extension" ) ); continue; } #if defined(POLARSSL_ECDSA_C) - if( pk_alg == POLARSSL_PK_ECDSA ) + if( pk_alg == POLARSSL_PK_ECDSA && + ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 ) { - if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) ) - break; + SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); + continue; } - else #endif - break; + + /* + * Try to select a SHA-1 certificate for pre-1.2 clients, but still + * present them a SHA-higher cert rather than failing if it's the only + * one we got that satisfies the other conditions. + */ + if( ssl->minor_ver < SSL_MINOR_VERSION_3 && + cur->cert->sig_md != POLARSSL_MD_SHA1 ) + { + if( fallback == NULL ) + fallback = cur; + { + SSL_DEBUG_MSG( 3, ( "certificate not preferred: " + "sha-2 with pre-TLS 1.2 client" ) ); + continue; + } + } + + /* If we get there, we got a winner */ + break; } if( cur == NULL ) - return( -1 ); + cur = fallback; - ssl->handshake->key_cert = cur; - return( 0 ); + + /* Do not update ssl->handshake->key_cert unless the is a match */ + if( cur != NULL ) + { + ssl->handshake->key_cert = cur; + SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", + ssl->handshake->key_cert->cert ); + return( 0 ); + } + + return( -1 ); } #endif /* POLARSSL_X509_CRT_PARSE_C */ @@ -839,19 +929,35 @@ static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id, suite_info = ssl_ciphersuite_from_id( suite_id ); if( suite_info == NULL ) { - SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) ); - return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); } + SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) ); + if( suite_info->min_minor_ver > ssl->minor_ver || suite_info->max_minor_ver < ssl->minor_ver ) + { + SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); + return( 0 ); + } + + if( ssl->arc4_disabled == SSL_ARC4_DISABLED && + suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) + { + SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); return( 0 ); + } #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) if( ssl_ciphersuite_uses_ec( suite_info ) && ( ssl->handshake->curves == NULL || ssl->handshake->curves[0] == NULL ) ) + { + SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " + "no common elliptic curve" ) ); return( 0 ); + } #endif #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) @@ -861,7 +967,10 @@ static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id, ssl->f_psk == NULL && ( ssl->psk == NULL || ssl->psk_identity == NULL || ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) ) + { + SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) ); return( 0 ); + } #endif #if defined(POLARSSL_X509_CRT_PARSE_C) @@ -873,7 +982,11 @@ static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id, * This must be done last since we modify the key_cert list. */ if( ssl_pick_cert( ssl, suite_info ) != 0 ) + { + SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " + "no suitable certificate" ) ); return( 0 ); + } #endif *ciphersuite_info = suite_info; @@ -883,7 +996,7 @@ static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id, #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) static int ssl_parse_client_hello_v2( ssl_context *ssl ) { - int ret; + int ret, got_common_suite; unsigned int i, j; size_t n; unsigned int ciph_len, sess_len, chal_len; @@ -893,6 +1006,7 @@ static int ssl_parse_client_hello_v2( ssl_context *ssl ) SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) { SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); @@ -902,6 +1016,7 @@ static int ssl_parse_client_hello_v2( ssl_context *ssl ) return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); } +#endif /* POLARSSL_SSL_RENEGOTIATION */ buf = ssl->in_hdr; @@ -1038,20 +1153,48 @@ static int ssl_parse_client_hello_v2( ssl_context *ssl ) if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO ) { SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation == SSL_RENEGOTIATION ) { - SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) ); + SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " + "during renegotiation" ) ); if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) return( ret ); return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); } +#endif /* POLARSSL_SSL_RENEGOTIATION */ ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; break; } } +#if defined(POLARSSL_SSL_FALLBACK_SCSV) + for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) + { + if( p[0] == 0 && + p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) && + p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) ) + { + SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); + + if( ssl->minor_ver < ssl->max_minor_ver ) + { + SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); + + ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, + SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); + + return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); + } + + break; + } + } +#endif /* POLARSSL_SSL_FALLBACK_SCSV */ + + got_common_suite = 0; ciphersuites = ssl->ciphersuite_list[ssl->minor_ver]; ciphersuite_info = NULL; #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE) @@ -1069,6 +1212,8 @@ static int ssl_parse_client_hello_v2( ssl_context *ssl ) p[2] != ( ( ciphersuites[i] ) & 0xFF ) ) continue; + got_common_suite = 1; + if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], &ciphersuite_info ) ) != 0 ) return( ret ); @@ -1078,11 +1223,21 @@ static int ssl_parse_client_hello_v2( ssl_context *ssl ) } } - SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); - - return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); + if( got_common_suite ) + { + SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " + "but none of them usable" ) ); + return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE ); + } + else + { + SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); + return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); + } have_ciphersuite_v2: + SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); + ssl->session_negotiate->ciphersuite = ciphersuites[i]; ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info ); @@ -1112,25 +1267,31 @@ have_ciphersuite_v2: static int ssl_parse_client_hello( ssl_context *ssl ) { - int ret; + int ret, got_common_suite; unsigned int i, j; size_t n; unsigned int ciph_len, sess_len; unsigned int comp_len; unsigned int ext_len = 0; unsigned char *buf, *p, *ext; +#if defined(POLARSSL_SSL_RENEGOTIATION) int renegotiation_info_seen = 0; +#endif int handshake_failure = 0; const int *ciphersuites; const ssl_ciphersuite_t *ciphersuite_info; SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); - if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE && - ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 ) +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#endif { - SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); - return( ret ); + if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); + return( ret ); + } } buf = ssl->in_hdr; @@ -1177,18 +1338,24 @@ static int ssl_parse_client_hello( ssl_context *ssl ) return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); } - if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE && - ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 ) +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) +#endif { - SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); - return( ret ); + if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); + return( ret ); + } } buf = ssl->in_msg; - if( !ssl->renegotiation ) - n = ssl->in_left - 5; - else +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) n = ssl->in_msglen; + else +#endif + n = ssl->in_left - 5; ssl->handshake->update_checksum( ssl, buf, n ); @@ -1351,6 +1518,7 @@ static int ssl_parse_client_hello( ssl_context *ssl ) if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO ) { SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation == SSL_RENEGOTIATION ) { SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) ); @@ -1360,11 +1528,36 @@ static int ssl_parse_client_hello( ssl_context *ssl ) return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); } + renegotiation_info_seen = 1; +#endif /* POLARSSL_SSL_RENEGOTIATION */ ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; break; } } +#if defined(POLARSSL_SSL_FALLBACK_SCSV) + for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 ) + { + if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) && + p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) ) + { + SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) ); + + if( ssl->minor_ver < ssl->max_minor_ver ) + { + SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) ); + + ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, + SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); + + return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); + } + + break; + } + } +#endif /* POLARSSL_SSL_FALLBACK_SCSV */ + ext = buf + 44 + sess_len + ciph_len + comp_len; while( ext_len ) @@ -1395,24 +1588,30 @@ static int ssl_parse_client_hello( ssl_context *ssl ) case TLS_EXT_RENEGOTIATION_INFO: SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) renegotiation_info_seen = 1; +#endif ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); if( ret != 0 ) return( ret ); break; -#if defined(POLARSSL_SSL_PROTO_TLS1_2) +#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \ + defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED) case TLS_EXT_SIG_ALG: SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation == SSL_RENEGOTIATION ) break; +#endif ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); if( ret != 0 ) return( ret ); break; -#endif /* POLARSSL_SSL_PROTO_TLS1_2 */ +#endif /* POLARSSL_SSL_PROTO_TLS1_2 && + POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */ #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: @@ -1453,6 +1652,26 @@ static int ssl_parse_client_hello( ssl_context *ssl ) break; #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + case TLS_EXT_ENCRYPT_THEN_MAC: + SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); + + ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); + if( ret != 0 ) + return( ret ); + break; +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + case TLS_EXT_EXTENDED_MASTER_SECRET: + SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); + + ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); + if( ret != 0 ) + return( ret ); + break; +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) case TLS_EXT_SESSION_TICKET: SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); @@ -1491,12 +1710,13 @@ static int ssl_parse_client_hello( ssl_context *ssl ) /* * Renegotiation security checks */ - if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION && + if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION && ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE ) { SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); handshake_failure = 1; } +#if defined(POLARSSL_SSL_RENEGOTIATION) else if( ssl->renegotiation == SSL_RENEGOTIATION && ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION && renegotiation_info_seen == 0 ) @@ -1518,6 +1738,7 @@ static int ssl_parse_client_hello( ssl_context *ssl ) SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); handshake_failure = 1; } +#endif /* POLARSSL_SSL_RENEGOTIATION */ if( handshake_failure == 1 ) { @@ -1532,6 +1753,7 @@ static int ssl_parse_client_hello( ssl_context *ssl ) * (At the end because we need information from the EC-based extensions * and certificate from the SNI callback triggered by the SNI extension.) */ + got_common_suite = 0; ciphersuites = ssl->ciphersuite_list[ssl->minor_ver]; ciphersuite_info = NULL; #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE) @@ -1548,6 +1770,8 @@ static int ssl_parse_client_hello( ssl_context *ssl ) p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) continue; + got_common_suite = 1; + if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], &ciphersuite_info ) ) != 0 ) return( ret ); @@ -1557,14 +1781,23 @@ static int ssl_parse_client_hello( ssl_context *ssl ) } } - SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); - - if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) - return( ret ); - - return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); + if( got_common_suite ) + { + SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " + "but none of them usable" ) ); + ssl_send_fatal_handshake_failure( ssl ); + return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE ); + } + else + { + SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); + ssl_send_fatal_handshake_failure( ssl ); + return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); + } have_ciphersuite: + SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); + ssl->session_negotiate->ciphersuite = ciphersuites[i]; ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info ); @@ -1602,6 +1835,76 @@ static void ssl_write_truncated_hmac_ext( ssl_context *ssl, } #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) +static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl, + unsigned char *buf, + size_t *olen ) +{ + unsigned char *p = buf; + const ssl_ciphersuite_t *suite = NULL; + const cipher_info_t *cipher = NULL; + + if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED || + ssl->minor_ver == SSL_MINOR_VERSION_0 ) + { + *olen = 0; + return; + } + + /* + * RFC 7366: "If a server receives an encrypt-then-MAC request extension + * from a client and then selects a stream or Authenticated Encryption + * with Associated Data (AEAD) ciphersuite, it MUST NOT send an + * encrypt-then-MAC response extension back to the client." + */ + if( ( suite = ssl_ciphersuite_from_id( + ssl->session_negotiate->ciphersuite ) ) == NULL || + ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL || + cipher->mode != POLARSSL_MODE_CBC ) + { + *olen = 0; + return; + } + + SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) ); + + *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); + + *p++ = 0x00; + *p++ = 0x00; + + *olen = 4; +} +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) +static void ssl_write_extended_ms_ext( ssl_context *ssl, + unsigned char *buf, + size_t *olen ) +{ + unsigned char *p = buf; + + if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED || + ssl->minor_ver == SSL_MINOR_VERSION_0 ) + { + *olen = 0; + return; + } + + SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret " + "extension" ) ); + + *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); + + *p++ = 0x00; + *p++ = 0x00; + + *olen = 4; +} +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) static void ssl_write_session_ticket_ext( ssl_context *ssl, unsigned char *buf, @@ -1644,16 +1947,29 @@ static void ssl_write_renegotiation_ext( ssl_context *ssl, *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF ); - *p++ = 0x00; - *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; - *p++ = ssl->verify_data_len * 2 & 0xFF; +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) + { + *p++ = 0x00; + *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; + *p++ = ssl->verify_data_len * 2 & 0xFF; + + memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); + p += ssl->verify_data_len; + memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); + p += ssl->verify_data_len; - memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); - p += ssl->verify_data_len; - memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); - p += ssl->verify_data_len; + *olen = 5 + ssl->verify_data_len * 2; + } + else +#endif /* POLARSSL_SSL_RENEGOTIATION */ + { + *p++ = 0x00; + *p++ = 0x01; + *p++ = 0x00; - *olen = 5 + ssl->verify_data_len * 2; + *olen = 5; + } } #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) @@ -1812,7 +2128,9 @@ static int ssl_write_server_hello( ssl_context *ssl ) * If not, try looking up session ID in our cache. */ if( ssl->handshake->resume == 0 && +#if defined(POLARSSL_SSL_RENEGOTIATION) ssl->renegotiation == SSL_INITIAL_HANDSHAKE && +#endif ssl->session_negotiate->length != 0 && ssl->f_get_cache != NULL && ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 ) @@ -1905,6 +2223,16 @@ static int ssl_write_server_hello( ssl_context *ssl ) ext_len += olen; #endif +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); + ext_len += olen; +#endif + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); + ext_len += olen; +#endif + #if defined(POLARSSL_SSL_SESSION_TICKETS) ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); ext_len += olen; @@ -2267,7 +2595,7 @@ curve_matching_done: curve = ssl->handshake->curves; #endif - if( *curve == NULL ) + if( curve == NULL || *curve == NULL ) { SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); @@ -2558,6 +2886,10 @@ static int ssl_parse_encrypted_pms( ssl_context *ssl, int ret; size_t len = pk_get_len( ssl_own_key( ssl ) ); unsigned char *pms = ssl->handshake->premaster + pms_offset; + unsigned char fake_pms[48], peer_pms[48]; + unsigned char mask; + size_t i, peer_pmslen; + unsigned int diff; if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) ) { @@ -2587,31 +2919,46 @@ static int ssl_parse_encrypted_pms( ssl_context *ssl, return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); } + /* + * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding + * must not cause the connection to end immediately; instead, send a + * bad_record_mac later in the handshake. + * Also, avoid data-dependant branches here to protect against + * timing-based variants. + */ + ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) ); + if( ret != 0 ) + return( ret ); + ret = pk_decrypt( ssl_own_key( ssl ), p, len, - pms, &ssl->handshake->pmslen, - sizeof( ssl->handshake->premaster ) - pms_offset, + peer_pms, &peer_pmslen, + sizeof( peer_pms ), ssl->f_rng, ssl->p_rng ); - if( ret != 0 || ssl->handshake->pmslen != 48 || - pms[0] != ssl->handshake->max_major_ver || - pms[1] != ssl->handshake->max_minor_ver ) - { - SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); + diff = (unsigned int) ret; + diff |= peer_pmslen ^ 48; + diff |= peer_pms[0] ^ ssl->handshake->max_major_ver; + diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver; - /* - * Protection against Bleichenbacher's attack: - * invalid PKCS#1 v1.5 padding must not cause - * the connection to end immediately; instead, - * send a bad_record_mac later in the handshake. - */ - ssl->handshake->pmslen = 48; +#if defined(POLARSSL_SSL_DEBUG_ALL) + if( diff != 0 ) + SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); +#endif - ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen ); - if( ret != 0 ) - return( ret ); + if( sizeof( ssl->handshake->premaster ) < pms_offset || + sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) + { + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); } + ssl->handshake->pmslen = 48; - return( ret ); + /* mask = diff ? 0xff : 0x00 */ + mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ); + for( i = 0; i < ssl->handshake->pmslen; i++ ) + pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); + + return( 0 ); } #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED || POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */ diff --git a/ext/polarssl/library/ssl_tls.c b/ext/mbedtls/library/ssl_tls.c similarity index 89% rename from ext/polarssl/library/ssl_tls.c rename to ext/mbedtls/library/ssl_tls.c index 5f080defea..b8fb507153 100644 --- a/ext/polarssl/library/ssl_tls.c +++ b/ext/mbedtls/library/ssl_tls.c @@ -1,12 +1,9 @@ /* * SSLv3/TLSv1 shared functions * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,6 +39,8 @@ #include "polarssl/debug.h" #include "polarssl/ssl.h" +#include + #if defined(POLARSSL_X509_CRT_PARSE_C) && \ defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) #include "polarssl/oid.h" @@ -50,12 +49,11 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include - #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ !defined(EFI32) #define strcasecmp _stricmp @@ -95,7 +93,7 @@ static int ssl_session_copy( ssl_session *dst, const ssl_session *src ) { int ret; - dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) ); + dst->peer_cert = polarssl_malloc( sizeof(x509_crt) ); if( dst->peer_cert == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -114,7 +112,7 @@ static int ssl_session_copy( ssl_session *dst, const ssl_session *src ) #if defined(POLARSSL_SSL_SESSION_TICKETS) if( src->ticket != NULL ) { - dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len ); + dst->ticket = polarssl_malloc( src->ticket_len ); if( dst->ticket == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -470,10 +468,47 @@ int ssl_derive_keys( ssl_context *ssl ) SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, handshake->pmslen ); +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED ) + { + unsigned char session_hash[48]; + size_t hash_len; + + SSL_DEBUG_MSG( 3, ( "using extended master secret" ) ); + + ssl->handshake->calc_verify( ssl, session_hash ); + +#if defined(POLARSSL_SSL_PROTO_TLS1_2) + if( ssl->minor_ver == SSL_MINOR_VERSION_3 ) + { +#if defined(POLARSSL_SHA512_C) + if( ssl->transform_negotiate->ciphersuite_info->mac == + POLARSSL_MD_SHA384 ) + { + hash_len = 48; + } + else +#endif + hash_len = 32; + } + else +#endif /* POLARSSL_SSL_PROTO_TLS1_2 */ + hash_len = 36; + + SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len ); + + handshake->tls_prf( handshake->premaster, handshake->pmslen, + "extended master secret", + session_hash, hash_len, session->master, 48 ); + + } + else +#endif handshake->tls_prf( handshake->premaster, handshake->pmslen, "master secret", handshake->randbytes, 64, session->master, 48 ); + polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) ); } else @@ -564,12 +599,23 @@ int ssl_derive_keys( ssl_context *ssl ) { /* * GenericBlockCipher: - * first multiple of blocklen greater than maclen - * + IV except for SSL3 and TLS 1.0 + * 1. if EtM is in use: one block plus MAC + * otherwise: * first multiple of blocklen greater than maclen + * 2. IV except for SSL3 and TLS 1.0 */ - transform->minlen = transform->maclen - + cipher_info->block_size - - transform->maclen % cipher_info->block_size; +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + if( session->encrypt_then_mac == SSL_ETM_ENABLED ) + { + transform->minlen = transform->maclen + + cipher_info->block_size; + } + else +#endif + { + transform->minlen = transform->maclen + + cipher_info->block_size + - transform->maclen % cipher_info->block_size; + } #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) if( ssl->minor_ver == SSL_MINOR_VERSION_0 || @@ -599,6 +645,7 @@ int ssl_derive_keys( ssl_context *ssl ) /* * Finally setup the cipher contexts, IVs and MAC secrets. */ +#if defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT ) { key1 = keyblk + transform->maclen * 2; @@ -617,6 +664,9 @@ int ssl_derive_keys( ssl_context *ssl ) iv_copy_len ); } else +#endif /* POLARSSL_SSL_CLI_C */ +#if defined(POLARSSL_SSL_SRV_C) + if( ssl->endpoint == SSL_IS_SERVER ) { key1 = keyblk + transform->maclen * 2 + transform->keylen; key2 = keyblk + transform->maclen * 2; @@ -633,6 +683,12 @@ int ssl_derive_keys( ssl_context *ssl ) memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len, iv_copy_len ); } + else +#endif /* POLARSSL_SSL_SRV_C */ + { + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); + } #if defined(POLARSSL_SSL_PROTO_SSL3) if( ssl->minor_ver == SSL_MINOR_VERSION_0 ) @@ -1023,25 +1079,44 @@ static void ssl_mac( md_context_t *md_ctx, unsigned char *secret, } #endif /* POLARSSL_SSL_PROTO_SSL3 */ +#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ + ( defined(POLARSSL_CIPHER_MODE_CBC) && \ + ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) +#define POLARSSL_SOME_MODES_USE_MAC +#endif + /* * Encryption/decryption functions */ static int ssl_encrypt_buf( ssl_context *ssl ) { size_t i; - const cipher_mode_t mode = cipher_get_cipher_mode( - &ssl->transform_out->cipher_ctx_enc ); + cipher_mode_t mode; + int auth_done = 0; SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); + if( ssl->session_out == NULL || ssl->transform_out == NULL ) + { + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); + } + + mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ); + + SSL_DEBUG_BUF( 4, "before encrypt: output payload", + ssl->out_msg, ssl->out_msglen ); + /* - * Add MAC before encrypt, except for AEAD modes + * Add MAC before if needed */ -#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ - ( defined(POLARSSL_CIPHER_MODE_CBC) && \ - ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) - if( mode != POLARSSL_MODE_GCM && - mode != POLARSSL_MODE_CCM ) +#if defined(POLARSSL_SOME_MODES_USE_MAC) + if( mode == POLARSSL_MODE_STREAM || + ( mode == POLARSSL_MODE_CBC +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED +#endif + ) ) { #if defined(POLARSSL_SSL_PROTO_SSL3) if( ssl->minor_ver == SSL_MINOR_VERSION_0 ) @@ -1076,6 +1151,7 @@ static int ssl_encrypt_buf( ssl_context *ssl ) ssl->transform_out->maclen ); ssl->out_msglen += ssl->transform_out->maclen; + auth_done++; } #endif /* AEAD not the only option */ @@ -1092,9 +1168,6 @@ static int ssl_encrypt_buf( ssl_context *ssl ) "including %d bytes of padding", ssl->out_msglen, 0 ) ); - SSL_DEBUG_BUF( 4, "before encrypt: output payload", - ssl->out_msg, ssl->out_msglen ); - if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc, ssl->transform_out->iv_enc, ssl->transform_out->ivlen, @@ -1137,6 +1210,7 @@ static int ssl_encrypt_buf( ssl_context *ssl ) /* * Generate IV */ +#if defined(POLARSSL_SSL_AEAD_RANDOM_IV) ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen, ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen ); @@ -1146,6 +1220,18 @@ static int ssl_encrypt_buf( ssl_context *ssl ) memcpy( ssl->out_iv, ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen, ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen ); +#else + if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 ) + { + /* Reminder if we ever add an AEAD mode with a different size */ + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); + } + + memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen, + ssl->out_ctr, 8 ); + memcpy( ssl->out_iv, ssl->out_ctr, 8 ); +#endif SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv, ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen ); @@ -1162,9 +1248,6 @@ static int ssl_encrypt_buf( ssl_context *ssl ) "including %d bytes of padding", ssl->out_msglen, 0 ) ); - SSL_DEBUG_BUF( 4, "before encrypt: output payload", - ssl->out_msg, ssl->out_msglen ); - /* * Encrypt and authenticate */ @@ -1187,6 +1270,7 @@ static int ssl_encrypt_buf( ssl_context *ssl ) } ssl->out_msglen += taglen; + auth_done++; SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen ); } @@ -1245,9 +1329,6 @@ static int ssl_encrypt_buf( ssl_context *ssl ) ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) ); - SSL_DEBUG_BUF( 4, "before encrypt: output payload", - ssl->out_iv, ssl->out_msglen ); - if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc, ssl->transform_out->iv_enc, ssl->transform_out->ivlen, @@ -1275,6 +1356,40 @@ static int ssl_encrypt_buf( ssl_context *ssl ) ssl->transform_out->ivlen ); } #endif + +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + if( auth_done == 0 ) + { + /* + * MAC(MAC_write_key, seq_num + + * TLSCipherText.type + + * TLSCipherText.version + + * length_of( (IV +) ENC(...) ) + + * IV + // except for TLS 1.0 + * ENC(content + padding + padding_length)); + */ + unsigned char pseudo_hdr[13]; + + SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); + + memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 ); + memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 ); + pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF ); + pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF ); + + SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 ); + + md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 ); + md_hmac_update( &ssl->transform_out->md_ctx_enc, + ssl->out_iv, ssl->out_msglen ); + md_hmac_finish( &ssl->transform_out->md_ctx_enc, + ssl->out_iv + ssl->out_msglen ); + md_hmac_reset( &ssl->transform_out->md_ctx_enc ); + + ssl->out_msglen += ssl->transform_out->maclen; + auth_done++; + } +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ } else #endif /* POLARSSL_CIPHER_MODE_CBC && @@ -1284,6 +1399,13 @@ static int ssl_encrypt_buf( ssl_context *ssl ) return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); } + /* Make extra sure authentication was performed, exactly once */ + if( auth_done != 1 ) + { + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); + } + for( i = 8; i > 0; i-- ) if( ++ssl->out_ctr[i - 1] != 0 ) break; @@ -1305,16 +1427,22 @@ static int ssl_encrypt_buf( ssl_context *ssl ) static int ssl_decrypt_buf( ssl_context *ssl ) { size_t i; - const cipher_mode_t mode = cipher_get_cipher_mode( - &ssl->transform_in->cipher_ctx_dec ); -#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ - ( defined(POLARSSL_CIPHER_MODE_CBC) && \ - ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) + cipher_mode_t mode; + int auth_done = 0; +#if defined(POLARSSL_SOME_MODES_USE_MAC) size_t padlen = 0, correct = 1; #endif SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); + if( ssl->session_in == NULL || ssl->transform_in == NULL ) + { + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); + } + + mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec ); + if( ssl->in_msglen < ssl->transform_in->minlen ) { SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)", @@ -1362,7 +1490,7 @@ static int ssl_decrypt_buf( ssl_context *ssl ) unsigned char explicit_iv_len = ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen; - if( ssl->in_msglen < explicit_iv_len + taglen ) + if( ssl->in_msglen < (size_t) explicit_iv_len + taglen ) { SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " "+ taglen (%d)", ssl->in_msglen, @@ -1411,6 +1539,7 @@ static int ssl_decrypt_buf( ssl_context *ssl ) return( ret ); } + auth_done++; if( olen != dec_msglen ) { @@ -1437,13 +1566,6 @@ static int ssl_decrypt_buf( ssl_context *ssl ) /* * Check immediate ciphertext sanity */ - if( ssl->in_msglen % ssl->transform_in->ivlen != 0 ) - { - SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", - ssl->in_msglen, ssl->transform_in->ivlen ) ); - return( POLARSSL_ERR_SSL_INVALID_MAC ); - } - #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2) if( ssl->minor_ver >= SSL_MINOR_VERSION_2 ) minlen += ssl->transform_in->ivlen; @@ -1463,6 +1585,59 @@ static int ssl_decrypt_buf( ssl_context *ssl ) dec_msg = ssl->in_msg; dec_msg_result = ssl->in_msg; + /* + * Authenticate before decrypt if enabled + */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED ) + { + unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE]; + unsigned char pseudo_hdr[13]; + + SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); + + dec_msglen -= ssl->transform_in->maclen; + ssl->in_msglen -= ssl->transform_in->maclen; + + memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 ); + memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 ); + pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF ); + pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF ); + + SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 ); + + md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 ); + md_hmac_update( &ssl->transform_in->md_ctx_dec, + ssl->in_iv, ssl->in_msglen ); + md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac ); + md_hmac_reset( &ssl->transform_in->md_ctx_dec ); + + SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen, + ssl->transform_in->maclen ); + SSL_DEBUG_BUF( 4, "computed mac", computed_mac, + ssl->transform_in->maclen ); + + if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac, + ssl->transform_in->maclen ) != 0 ) + { + SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); + + return( POLARSSL_ERR_SSL_INVALID_MAC ); + } + auth_done++; + } +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ + + /* + * Check length sanity + */ + if( ssl->in_msglen % ssl->transform_in->ivlen != 0 ) + { + SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", + ssl->in_msglen, ssl->transform_in->ivlen ) ); + return( POLARSSL_ERR_SSL_INVALID_MAC ); + } + #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2) /* * Initialize for prepended IV for block cipher in TLS v1.1 and up @@ -1507,7 +1682,8 @@ static int ssl_decrypt_buf( ssl_context *ssl ) padlen = 1 + ssl->in_msg[ssl->in_msglen - 1]; - if( ssl->in_msglen < ssl->transform_in->maclen + padlen ) + if( ssl->in_msglen < ssl->transform_in->maclen + padlen && + auth_done == 0 ) { #if defined(POLARSSL_SSL_DEBUG_ALL) SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", @@ -1581,6 +1757,8 @@ static int ssl_decrypt_buf( ssl_context *ssl ) SSL_DEBUG_MSG( 1, ( "should never happen" ) ); return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); } + + ssl->in_msglen -= padlen; } else #endif /* POLARSSL_CIPHER_MODE_CBC && @@ -1594,17 +1772,15 @@ static int ssl_decrypt_buf( ssl_context *ssl ) ssl->in_msg, ssl->in_msglen ); /* - * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course + * Authenticate if not done yet. + * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). */ -#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ - ( defined(POLARSSL_CIPHER_MODE_CBC) && \ - ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) - if( mode != POLARSSL_MODE_GCM && - mode != POLARSSL_MODE_CCM ) +#if defined(POLARSSL_SOME_MODES_USE_MAC) + if( auth_done == 0 ) { unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE]; - ssl->in_msglen -= ( ssl->transform_in->maclen + padlen ); + ssl->in_msglen -= ssl->transform_in->maclen; ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 ); ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen ); @@ -1649,7 +1825,8 @@ static int ssl_decrypt_buf( ssl_context *ssl ) ssl->in_msglen ); md_hmac_finish( &ssl->transform_in->md_ctx_dec, ssl->in_msg + ssl->in_msglen ); - for( j = 0; j < extra_run; j++ ) + /* Call md_process at least once due to cache attacks */ + for( j = 0; j < extra_run + 1; j++ ) md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg ); md_hmac_reset( &ssl->transform_in->md_ctx_dec ); @@ -1674,6 +1851,7 @@ static int ssl_decrypt_buf( ssl_context *ssl ) #endif correct = 0; } + auth_done++; /* * Finally check the correct flag @@ -1681,7 +1859,14 @@ static int ssl_decrypt_buf( ssl_context *ssl ) if( correct == 0 ) return( POLARSSL_ERR_SSL_INVALID_MAC ); } -#endif /* AEAD not the only option */ +#endif /* POLARSSL_SOME_MODES_USE_MAC */ + + /* Make extra sure authentication was performed, exactly once */ + if( auth_done != 1 ) + { + SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); + } if( ssl->in_msglen == 0 ) { @@ -1717,6 +1902,10 @@ static int ssl_decrypt_buf( ssl_context *ssl ) return( 0 ); } +#undef MAC_NONE +#undef MAC_PLAINTEXT +#undef MAC_CIPHERTEXT + #if defined(POLARSSL_ZLIB_SUPPORT) /* * Compression/decompression functions @@ -2349,6 +2538,7 @@ int ssl_write_certificate( ssl_context *ssl ) return( 0 ); } +#if defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT ) { if( ssl->client_auth == 0 ) @@ -2376,7 +2566,9 @@ int ssl_write_certificate( ssl_context *ssl ) } #endif /* POLARSSL_SSL_PROTO_SSL3 */ } - else /* SSL_IS_SERVER */ +#endif /* POLARSSL_SSL_CLI_C */ +#if defined(POLARSSL_SSL_SRV_C) + if( ssl->endpoint == SSL_IS_SERVER ) { if( ssl_own_cert( ssl ) == NULL ) { @@ -2384,6 +2576,7 @@ int ssl_write_certificate( ssl_context *ssl ) return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED ); } } +#endif SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) ); @@ -2459,6 +2652,7 @@ int ssl_parse_certificate( ssl_context *ssl ) return( 0 ); } +#if defined(POLARSSL_SSL_SRV_C) if( ssl->endpoint == SSL_IS_SERVER && ( ssl->authmode == SSL_VERIFY_NONE || ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) ) @@ -2468,6 +2662,7 @@ int ssl_parse_certificate( ssl_context *ssl ) ssl->state++; return( 0 ); } +#endif if( ( ret = ssl_read_record( ssl ) ) != 0 ) { @@ -2477,6 +2672,7 @@ int ssl_parse_certificate( ssl_context *ssl ) ssl->state++; +#if defined(POLARSSL_SSL_SRV_C) #if defined(POLARSSL_SSL_PROTO_SSL3) /* * Check if the client sent an empty certificate @@ -2521,6 +2717,7 @@ int ssl_parse_certificate( ssl_context *ssl ) } #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \ POLARSSL_SSL_PROTO_TLS1_2 */ +#endif /* POLARSSL_SSL_SRV_C */ if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) { @@ -2552,7 +2749,7 @@ int ssl_parse_certificate( ssl_context *ssl ) polarssl_free( ssl->session_negotiate->peer_cert ); } - if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc( + if( ( ssl->session_negotiate->peer_cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ) { SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", @@ -2599,6 +2796,7 @@ int ssl_parse_certificate( ssl_context *ssl ) * On client, make sure the server cert doesn't change during renego to * avoid "triple handshake" attack: https://secure-resumption.com/ */ +#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT && ssl->renegotiation == SSL_RENEGOTIATION ) { @@ -2618,6 +2816,7 @@ int ssl_parse_certificate( ssl_context *ssl ) return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE ); } } +#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */ if( ssl->authmode != SSL_VERIFY_NONE ) { @@ -2661,7 +2860,8 @@ int ssl_parse_certificate( ssl_context *ssl ) if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert, ciphersuite_info, - ! ssl->endpoint ) != 0 ) + ! ssl->endpoint, + &ssl->session_negotiate->verify_result ) != 0 ) { SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); if( ret == 0 ) @@ -3060,11 +3260,13 @@ void ssl_handshake_wrapup( ssl_context *ssl ) polarssl_free( ssl->handshake ); ssl->handshake = NULL; +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->renegotiation == SSL_RENEGOTIATION ) { ssl->renegotiation = SSL_RENEGOTIATION_DONE; ssl->renego_records_seen = 0; } +#endif /* * Switch in our now active transform context @@ -3079,6 +3281,12 @@ void ssl_handshake_wrapup( ssl_context *ssl ) if( ssl->session ) { +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + /* RFC 7366 3.1: keep the EtM state */ + ssl->session_negotiate->encrypt_then_mac = + ssl->session->encrypt_then_mac; +#endif + ssl_session_free( ssl->session ); polarssl_free( ssl->session ); } @@ -3123,8 +3331,10 @@ int ssl_write_finished( ssl_context *ssl ) // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63) hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12; +#if defined(POLARSSL_SSL_RENEGOTIATION) ssl->verify_data_len = hash_len; memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); +#endif ssl->out_msglen = 4 + hash_len; ssl->out_msgtype = SSL_MSG_HANDSHAKE; @@ -3136,10 +3346,14 @@ int ssl_write_finished( ssl_context *ssl ) */ if( ssl->handshake->resume != 0 ) { +#if defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT ) ssl->state = SSL_HANDSHAKE_WRAPUP; - else +#endif +#if defined(POLARSSL_SSL_SRV_C) + if( ssl->endpoint == SSL_IS_SERVER ) ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC; +#endif } else ssl->state++; @@ -3244,16 +3458,21 @@ int ssl_parse_finished( ssl_context *ssl ) return( POLARSSL_ERR_SSL_BAD_HS_FINISHED ); } +#if defined(POLARSSL_SSL_RENEGOTIATION) ssl->verify_data_len = hash_len; memcpy( ssl->peer_verify_data, buf, hash_len ); +#endif if( ssl->handshake->resume != 0 ) { +#if defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT ) ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC; - +#endif +#if defined(POLARSSL_SSL_SRV_C) if( ssl->endpoint == SSL_IS_SERVER ) ssl->state = SSL_HANDSHAKE_WRAPUP; +#endif } else ssl->state++; @@ -3328,20 +3547,17 @@ static int ssl_handshake_init( ssl_context *ssl ) */ if( ssl->transform_negotiate == NULL ) { - ssl->transform_negotiate = - (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) ); + ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) ); } if( ssl->session_negotiate == NULL ) { - ssl->session_negotiate = - (ssl_session *) polarssl_malloc( sizeof(ssl_session) ); + ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) ); } if( ssl->handshake == NULL ) { - ssl->handshake = (ssl_handshake_params *) - polarssl_malloc( sizeof(ssl_handshake_params) ); + ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) ); } /* All pointers should exist and can be directly freed without issue */ @@ -3394,13 +3610,17 @@ int ssl_init( ssl_context *ssl ) ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() ); +#if defined(POLARSSL_SSL_RENEGOTIATION) ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT; + memset( ssl->renego_period, 0xFF, 7 ); + ssl->renego_period[7] = 0x00; +#endif #if defined(POLARSSL_DHM_C) if( ( ret = mpi_read_string( &ssl->dhm_P, 16, - POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 || + POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 || ( ret = mpi_read_string( &ssl->dhm_G, 16, - POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 ) + POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 ) { SSL_DEBUG_RET( 1, "mpi_read_string", ret ); return( ret ); @@ -3410,32 +3630,33 @@ int ssl_init( ssl_context *ssl ) /* * Prepare base structures */ - ssl->in_ctr = (unsigned char *) polarssl_malloc( len ); - ssl->in_hdr = ssl->in_ctr + 8; - ssl->in_iv = ssl->in_ctr + 13; - ssl->in_msg = ssl->in_ctr + 13; - - if( ssl->in_ctr == NULL ) + if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL || + ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL ) { SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) ); + polarssl_free( ssl->in_ctr ); + ssl->in_ctr = NULL; return( POLARSSL_ERR_SSL_MALLOC_FAILED ); } - ssl->out_ctr = (unsigned char *) polarssl_malloc( len ); + memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN ); + memset( ssl->out_ctr, 0, SSL_BUFFER_LEN ); + + ssl->in_hdr = ssl->in_ctr + 8; + ssl->in_iv = ssl->in_ctr + 13; + ssl->in_msg = ssl->in_ctr + 13; + ssl->out_hdr = ssl->out_ctr + 8; ssl->out_iv = ssl->out_ctr + 13; ssl->out_msg = ssl->out_ctr + 13; - if( ssl->out_ctr == NULL ) - { - SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) ); - polarssl_free( ssl->in_ctr ); - ssl->in_ctr = NULL; - return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - } +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + ssl->encrypt_then_mac = SSL_ETM_ENABLED; +#endif - memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN ); - memset( ssl->out_ctr, 0, SSL_BUFFER_LEN ); +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + ssl->extended_ms = SSL_EXTENDED_MS_ENABLED; +#endif #if defined(POLARSSL_SSL_SESSION_TICKETS) ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME; @@ -3460,12 +3681,16 @@ int ssl_session_reset( ssl_context *ssl ) int ret; ssl->state = SSL_HELLO_REQUEST; + +#if defined(POLARSSL_SSL_RENEGOTIATION) ssl->renegotiation = SSL_INITIAL_HANDSHAKE; - ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION; + ssl->renego_records_seen = 0; ssl->verify_data_len = 0; - memset( ssl->own_verify_data, 0, 36 ); - memset( ssl->peer_verify_data, 0, 36 ); + memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN ); + memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN ); +#endif + ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION; ssl->in_offt = NULL; @@ -3482,12 +3707,14 @@ int ssl_session_reset( ssl_context *ssl ) ssl->out_msgtype = 0; ssl->out_msglen = 0; ssl->out_left = 0; +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) + if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED ) + ssl->split_done = 0; +#endif ssl->transform_in = NULL; ssl->transform_out = NULL; - ssl->renego_records_seen = 0; - memset( ssl->out_ctr, 0, SSL_BUFFER_LEN ); memset( ssl->in_ctr, 0, SSL_BUFFER_LEN ); @@ -3548,7 +3775,7 @@ static int ssl_ticket_keys_init( ssl_context *ssl ) if( ssl->ticket_keys != NULL ) return( 0 ); - tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) ); + tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) ); if( tkeys == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -3591,10 +3818,16 @@ void ssl_set_endpoint( ssl_context *ssl, int endpoint ) { ssl->endpoint = endpoint; -#if defined(POLARSSL_SSL_SESSION_TICKETS) +#if defined(POLARSSL_SSL_SESSION_TICKETS) && \ + defined(POLARSSL_SSL_CLI_C) if( endpoint == SSL_IS_CLIENT ) ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED; #endif + +#if defined(POLARSSL_SSL_TRUNCATED_HMAC) + if( endpoint == SSL_IS_SERVER ) + ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED; +#endif } void ssl_set_authmode( ssl_context *ssl, int authmode ) @@ -3638,6 +3871,7 @@ void ssl_set_bio( ssl_context *ssl, ssl->p_send = p_send; } +#if defined(POLARSSL_SSL_SRV_C) void ssl_set_session_cache( ssl_context *ssl, int (*f_get_cache)(void *, ssl_session *), void *p_get_cache, int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache ) @@ -3647,7 +3881,9 @@ void ssl_set_session_cache( ssl_context *ssl, ssl->f_set_cache = f_set_cache; ssl->p_set_cache = p_set_cache; } +#endif /* POLARSSL_SSL_SRV_C */ +#if defined(POLARSSL_SSL_CLI_C) int ssl_set_session( ssl_context *ssl, const ssl_session *session ) { int ret; @@ -3667,6 +3903,7 @@ int ssl_set_session( ssl_context *ssl, const ssl_session *session ) return( 0 ); } +#endif /* POLARSSL_SSL_CLI_C */ void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites ) { @@ -3695,7 +3932,7 @@ static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl ) { ssl_key_cert *key_cert, *last; - key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) ); + key_cert = polarssl_malloc( sizeof(ssl_key_cert) ); if( key_cert == NULL ) return( NULL ); @@ -3741,6 +3978,7 @@ int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert, return( 0 ); } +#if ! defined(POLARSSL_DEPRECATED_REMOVED) #if defined(POLARSSL_RSA_C) int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert, rsa_context *rsa_key ) @@ -3751,7 +3989,7 @@ int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert, if( key_cert == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) ); + key_cert->key = polarssl_malloc( sizeof(pk_context) ); if( key_cert->key == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -3783,7 +4021,7 @@ int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert, if( key_cert == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) ); + key_cert->key = polarssl_malloc( sizeof(pk_context) ); if( key_cert->key == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -3798,6 +4036,7 @@ int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert, return( 0 ); } +#endif /* POLARSSL_DEPRECATED_REMOVED */ #endif /* POLARSSL_X509_CRT_PARSE_C */ #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) @@ -3810,22 +4049,23 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len, if( psk_len > POLARSSL_PSK_MAX_LEN ) return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); - if( ssl->psk != NULL ) + if( ssl->psk != NULL || ssl->psk_identity != NULL ) { polarssl_free( ssl->psk ); polarssl_free( ssl->psk_identity ); } + if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL || + ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL ) + { + polarssl_free( ssl->psk ); + ssl->psk = NULL; + return( POLARSSL_ERR_SSL_MALLOC_FAILED ); + } + ssl->psk_len = psk_len; ssl->psk_identity_len = psk_identity_len; - ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len ); - ssl->psk_identity = (unsigned char *) - polarssl_malloc( ssl->psk_identity_len ); - - if( ssl->psk == NULL || ssl->psk_identity == NULL ) - return( POLARSSL_ERR_SSL_MALLOC_FAILED ); - memcpy( ssl->psk, psk, ssl->psk_len ); memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len ); @@ -3903,7 +4143,7 @@ int ssl_set_hostname( ssl_context *ssl, const char *hostname ) if( ssl->hostname_len + 1 == 0 ) return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); - ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 ); + ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 ); if( ssl->hostname == NULL ) return( POLARSSL_ERR_SSL_MALLOC_FAILED ); @@ -3977,6 +4217,32 @@ void ssl_set_min_version( ssl_context *ssl, int major, int minor ) } } +#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C) +void ssl_set_fallback( ssl_context *ssl, char fallback ) +{ + ssl->fallback = fallback; +} +#endif + +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) +void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm ) +{ + ssl->encrypt_then_mac = etm; +} +#endif + +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) +void ssl_set_extended_master_secret( ssl_context *ssl, char ems ) +{ + ssl->extended_ms = ems; +} +#endif + +void ssl_set_arc4_support( ssl_context *ssl, char arc4 ) +{ + ssl->arc4_disabled = arc4; +} + #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code ) { @@ -3995,37 +4261,54 @@ int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code ) #if defined(POLARSSL_SSL_TRUNCATED_HMAC) int ssl_set_truncated_hmac( ssl_context *ssl, int truncate ) { - if( ssl->endpoint != SSL_IS_CLIENT ) - return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); - ssl->trunc_hmac = truncate; return( 0 ); } #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ -void ssl_set_renegotiation( ssl_context *ssl, int renegotiation ) +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) +void ssl_set_cbc_record_splitting( ssl_context *ssl, char split ) { - ssl->disable_renegotiation = renegotiation; + ssl->split_done = split; } +#endif void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy ) { ssl->allow_legacy_renegotiation = allow_legacy; } +#if defined(POLARSSL_SSL_RENEGOTIATION) +void ssl_set_renegotiation( ssl_context *ssl, int renegotiation ) +{ + ssl->disable_renegotiation = renegotiation; +} + void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records ) { ssl->renego_max_records = max_records; } +void ssl_set_renegotiation_period( ssl_context *ssl, + const unsigned char period[8] ) +{ + memcpy( ssl->renego_period, period, 8 ); +} +#endif /* POLARSSL_SSL_RENEGOTIATION */ + #if defined(POLARSSL_SSL_SESSION_TICKETS) int ssl_set_session_tickets( ssl_context *ssl, int use_tickets ) { ssl->session_tickets = use_tickets; +#if defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT ) return( 0 ); +#endif + + if( use_tickets == SSL_SESSION_TICKETS_DISABLED ) + return( 0 ); if( ssl->f_rng == NULL ) return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); @@ -4049,7 +4332,13 @@ size_t ssl_get_bytes_avail( const ssl_context *ssl ) int ssl_get_verify_result( const ssl_context *ssl ) { - return( ssl->session->verify_result ); + if( ssl->session != NULL ) + return( ssl->session->verify_result ); + + if( ssl->session_negotiate != NULL ) + return( ssl->session_negotiate->verify_result ); + + return( -1 ); } const char *ssl_get_ciphersuite( const ssl_context *ssl ) @@ -4092,6 +4381,7 @@ const x509_crt *ssl_get_peer_cert( const ssl_context *ssl ) } #endif /* POLARSSL_X509_CRT_PARSE_C */ +#if defined(POLARSSL_SSL_CLI_C) int ssl_get_session( const ssl_context *ssl, ssl_session *dst ) { if( ssl == NULL || @@ -4104,6 +4394,7 @@ int ssl_get_session( const ssl_context *ssl, ssl_session *dst ) return( ssl_session_copy( dst, ssl->session ) ); } +#endif /* POLARSSL_SSL_CLI_C */ /* * Perform a single step of the SSL handshake @@ -4116,7 +4407,6 @@ int ssl_handshake_step( ssl_context *ssl ) if( ssl->endpoint == SSL_IS_CLIENT ) ret = ssl_handshake_client_step( ssl ); #endif - #if defined(POLARSSL_SSL_SRV_C) if( ssl->endpoint == SSL_IS_SERVER ) ret = ssl_handshake_server_step( ssl ); @@ -4147,6 +4437,7 @@ int ssl_handshake( ssl_context *ssl ) return( ret ); } +#if defined(POLARSSL_SSL_RENEGOTIATION) #if defined(POLARSSL_SSL_SRV_C) /* * Write HelloRequest to request renegotiation on server @@ -4259,6 +4550,30 @@ int ssl_renegotiate( ssl_context *ssl ) return( ret ); } +/* + * Check record counters and renegotiate if they're above the limit. + */ +static int ssl_check_ctr_renegotiate( ssl_context *ssl ) +{ + if( ssl->state != SSL_HANDSHAKE_OVER || + ssl->renegotiation == SSL_RENEGOTIATION_PENDING || + ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ) + { + return( 0 ); + } + + // TODO: adapt for DTLS + if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 && + memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 ) + { + return( 0 ); + } + + SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) ); + return( ssl_renegotiate( ssl ) ); +} +#endif /* POLARSSL_SSL_RENEGOTIATION */ + /* * Receive application data decrypted from the SSL layer */ @@ -4269,6 +4584,14 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ) SSL_DEBUG_MSG( 2, ( "=> read" ) ); +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); + return( ret ); + } +#endif + if( ssl->state != SSL_HANDSHAKE_OVER ) { ret = ssl_handshake( ssl ); @@ -4313,10 +4636,12 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ) } } +#if defined(POLARSSL_SSL_RENEGOTIATION) if( ssl->in_msgtype == SSL_MSG_HANDSHAKE ) { SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); +#if defined(POLARSSL_SSL_CLI_C) if( ssl->endpoint == SSL_IS_CLIENT && ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST || ssl->in_hslen != 4 ) ) @@ -4324,6 +4649,7 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ) SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); } +#endif if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED || ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION && @@ -4393,6 +4719,7 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ) return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); } } +#endif /* POLARSSL_SSL_RENEGOTIATION */ /* Fatal and closure alerts handled by ssl_read_record() */ if( ssl->in_msgtype == SSL_MSG_ALERT ) @@ -4429,25 +4756,16 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ) } /* - * Send application data to be encrypted by the SSL layer + * Send application data to be encrypted by the SSL layer, + * taking care of max fragment length and buffer size */ -int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len ) +static int ssl_write_real( ssl_context *ssl, + const unsigned char *buf, size_t len ) { int ret; size_t n; unsigned int max_len = SSL_MAX_CONTENT_LEN; - SSL_DEBUG_MSG( 2, ( "=> write" ) ); - - if( ssl->state != SSL_HANDSHAKE_OVER ) - { - if( ( ret = ssl_handshake( ssl ) ) != 0 ) - { - SSL_DEBUG_RET( 1, "ssl_handshake", ret ); - return( ret ); - } - } - #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) /* * Assume mfl_code is correct since it was checked when set @@ -4487,9 +4805,81 @@ int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len ) } } + return( (int) n ); +} + +/* + * Write application data, doing 1/n-1 splitting if necessary. + * + * With non-blocking I/O, ssl_write_real() may return WANT_WRITE, + * then the caller will call us again with the same arguments, so + * remember wether we already did the split or not. + */ +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) +static int ssl_write_split( ssl_context *ssl, + const unsigned char *buf, size_t len ) +{ + int ret; + + if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED || + len <= 1 || + ssl->minor_ver > SSL_MINOR_VERSION_1 || + cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) + != POLARSSL_MODE_CBC ) + { + return( ssl_write_real( ssl, buf, len ) ); + } + + if( ssl->split_done == 0 ) + { + if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) + return( ret ); + ssl->split_done = 1; + } + + if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) + return( ret ); + ssl->split_done = 0; + + return( ret + 1 ); +} +#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */ + +/* + * Write application data (public-facing wrapper) + */ +int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len ) +{ + int ret; + + SSL_DEBUG_MSG( 2, ( "=> write" ) ); + +#if defined(POLARSSL_SSL_RENEGOTIATION) + if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); + return( ret ); + } +#endif + + if( ssl->state != SSL_HANDSHAKE_OVER ) + { + if( ( ret = ssl_handshake( ssl ) ) != 0 ) + { + SSL_DEBUG_RET( 1, "ssl_handshake", ret ); + return( ret ); + } + } + +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) + ret = ssl_write_split( ssl, buf, len ); +#else + ret = ssl_write_real( ssl, buf, len ); +#endif + SSL_DEBUG_MSG( 2, ( "<= write" ) ); - return( (int) n ); + return( ret ); } /* @@ -4811,8 +5201,10 @@ int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id ) #if defined(POLARSSL_X509_CRT_PARSE_C) int ssl_check_cert_usage( const x509_crt *cert, const ssl_ciphersuite_t *ciphersuite, - int cert_endpoint ) + int cert_endpoint, + int *flags ) { + int ret = 0; #if defined(POLARSSL_X509_CHECK_KEY_USAGE) int usage = 0; #endif @@ -4825,6 +5217,7 @@ int ssl_check_cert_usage( const x509_crt *cert, !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) ((void) cert); ((void) cert_endpoint); + ((void) flags); #endif #if defined(POLARSSL_X509_CHECK_KEY_USAGE) @@ -4864,7 +5257,10 @@ int ssl_check_cert_usage( const x509_crt *cert, } if( x509_crt_check_key_usage( cert, usage ) != 0 ) - return( -1 ); + { + *flags |= BADCERT_KEY_USAGE; + ret = -1; + } #else ((void) ciphersuite); #endif /* POLARSSL_X509_CHECK_KEY_USAGE */ @@ -4882,10 +5278,13 @@ int ssl_check_cert_usage( const x509_crt *cert, } if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 ) - return( -1 ); + { + *flags |= BADCERT_EXT_KEY_USAGE; + ret = -1; + } #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */ - return( 0 ); + return( ret ); } #endif /* POLARSSL_X509_CRT_PARSE_C */ diff --git a/ext/polarssl/library/threading.c b/ext/mbedtls/library/threading.c similarity index 94% rename from ext/polarssl/library/threading.c rename to ext/mbedtls/library/threading.c index 522c70fef7..fa745afdb3 100644 --- a/ext/polarssl/library/threading.c +++ b/ext/mbedtls/library/threading.c @@ -1,12 +1,9 @@ /* * Threading abstraction layer * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/ext/polarssl/library/timing.c b/ext/mbedtls/library/timing.c similarity index 93% rename from ext/polarssl/library/timing.c rename to ext/mbedtls/library/timing.c index b387bd87fe..c5d8d42a13 100644 --- a/ext/polarssl/library/timing.c +++ b/ext/mbedtls/library/timing.c @@ -1,12 +1,9 @@ /* * Portable interface to the CPU cycle counter * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -80,8 +77,10 @@ unsigned long hardclock( void ) #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM && ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */ +/* some versions of mingw-64 have 32-bit longs even on x84_64 */ #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \ - defined(__GNUC__) && defined(__i386__) + defined(__GNUC__) && ( defined(__i386__) || ( \ + ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) ) #define POLARSSL_HAVE_HARDCLOCK @@ -252,9 +251,13 @@ unsigned long get_timer( struct hr_time *val, int reset ) return( delta ); } -DWORD WINAPI TimerProc( LPVOID uElapse ) +/* It's OK to use a global because alarm() is supposed to be global anyway */ +static DWORD alarmMs; + +static DWORD WINAPI TimerProc( LPVOID TimerContext ) { - Sleep( (DWORD) uElapse ); + ((void) TimerContext); + Sleep( alarmMs ); alarmed = 1; return( TRUE ); } @@ -264,8 +267,8 @@ void set_alarm( int seconds ) DWORD ThreadId; alarmed = 0; - CloseHandle( CreateThread( NULL, 0, TimerProc, - (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) ); + alarmMs = seconds * 1000; + CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) ); } void m_sleep( int milliseconds ) @@ -434,9 +437,9 @@ hard_test: if( hardfail > 1 ) { if( verbose != 0 ) - polarssl_printf( "failed\n" ); + polarssl_printf( "failed (ignored)\n" ); - return( 1 ); + goto hard_test_done; } /* Get a reference ratio cycles/ms */ @@ -465,6 +468,8 @@ hard_test: if( verbose != 0 ) polarssl_printf( "passed\n" ); +hard_test_done: + #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME) if( verbose != 0 ) polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " ); diff --git a/ext/polarssl/library/version.c b/ext/mbedtls/library/version.c similarity index 84% rename from ext/polarssl/library/version.c rename to ext/mbedtls/library/version.c index c3c708a2de..2856d6ccd4 100644 --- a/ext/polarssl/library/version.c +++ b/ext/mbedtls/library/version.c @@ -1,12 +1,9 @@ /* * Version information * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,8 +31,6 @@ #include "polarssl/version.h" #include -const char version[] = POLARSSL_VERSION_STRING; - unsigned int version_get_number() { return( POLARSSL_VERSION_NUMBER ); diff --git a/ext/polarssl/library/version_features.c b/ext/mbedtls/library/version_features.c similarity index 92% rename from ext/polarssl/library/version_features.c rename to ext/mbedtls/library/version_features.c index 10231987fa..b8b3a4a35a 100644 --- a/ext/polarssl/library/version_features.c +++ b/ext/mbedtls/library/version_features.c @@ -1,12 +1,9 @@ /* * Version feature information * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +37,7 @@ #define strcasecmp _stricmp #endif -const char *features[] = { +static const char *features[] = { #if defined(POLARSSL_VERSION_FEATURES) #if defined(POLARSSL_HAVE_INT8) "POLARSSL_HAVE_INT8", @@ -69,12 +66,24 @@ const char *features[] = { #if defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS) "POLARSSL_PLATFORM_NO_STD_FUNCTIONS", #endif /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */ -#if defined(POLARSSL_PLATFORM_PRINTF_ALT) - "POLARSSL_PLATFORM_PRINTF_ALT", -#endif /* POLARSSL_PLATFORM_PRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_EXIT_ALT) + "POLARSSL_PLATFORM_EXIT_ALT", +#endif /* POLARSSL_PLATFORM_EXIT_ALT */ #if defined(POLARSSL_PLATFORM_FPRINTF_ALT) "POLARSSL_PLATFORM_FPRINTF_ALT", #endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) + "POLARSSL_PLATFORM_PRINTF_ALT", +#endif /* POLARSSL_PLATFORM_PRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) + "POLARSSL_PLATFORM_SNPRINTF_ALT", +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ +#if defined(POLARSSL_DEPRECATED_WARNING) + "POLARSSL_DEPRECATED_WARNING", +#endif /* POLARSSL_DEPRECATED_WARNING */ +#if defined(POLARSSL_DEPRECATED_REMOVED) + "POLARSSL_DEPRECATED_REMOVED", +#endif /* POLARSSL_DEPRECATED_REMOVED */ #if defined(POLARSSL_TIMING_ALT) "POLARSSL_TIMING_ALT", #endif /* POLARSSL_TIMING_ALT */ @@ -120,6 +129,9 @@ const char *features[] = { #if defined(POLARSSL_AES_ROM_TABLES) "POLARSSL_AES_ROM_TABLES", #endif /* POLARSSL_AES_ROM_TABLES */ +#if defined(POLARSSL_CAMELLIA_SMALL_MEMORY) + "POLARSSL_CAMELLIA_SMALL_MEMORY", +#endif /* POLARSSL_CAMELLIA_SMALL_MEMORY */ #if defined(POLARSSL_CIPHER_MODE_CBC) "POLARSSL_CIPHER_MODE_CBC", #endif /* POLARSSL_CIPHER_MODE_CBC */ @@ -273,15 +285,33 @@ const char *features[] = { #if defined(POLARSSL_SELF_TEST) "POLARSSL_SELF_TEST", #endif /* POLARSSL_SELF_TEST */ +#if defined(POLARSSL_SSL_AEAD_RANDOM_IV) + "POLARSSL_SSL_AEAD_RANDOM_IV", +#endif /* POLARSSL_SSL_AEAD_RANDOM_IV */ #if defined(POLARSSL_SSL_ALERT_MESSAGES) "POLARSSL_SSL_ALERT_MESSAGES", #endif /* POLARSSL_SSL_ALERT_MESSAGES */ #if defined(POLARSSL_SSL_DEBUG_ALL) "POLARSSL_SSL_DEBUG_ALL", #endif /* POLARSSL_SSL_DEBUG_ALL */ +#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) + "POLARSSL_SSL_ENCRYPT_THEN_MAC", +#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ +#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) + "POLARSSL_SSL_EXTENDED_MASTER_SECRET", +#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ +#if defined(POLARSSL_SSL_FALLBACK_SCSV) + "POLARSSL_SSL_FALLBACK_SCSV", +#endif /* POLARSSL_SSL_FALLBACK_SCSV */ #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) "POLARSSL_SSL_HW_RECORD_ACCEL", #endif /* POLARSSL_SSL_HW_RECORD_ACCEL */ +#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) + "POLARSSL_SSL_CBC_RECORD_SPLITTING", +#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */ +#if defined(POLARSSL_SSL_DISABLE_RENEGOTIATION) + "POLARSSL_SSL_DISABLE_RENEGOTIATION", +#endif /* POLARSSL_SSL_DISABLE_RENEGOTIATION */ #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) "POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO", #endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ diff --git a/ext/polarssl/library/x509.c b/ext/mbedtls/library/x509.c similarity index 86% rename from ext/polarssl/library/x509.c rename to ext/mbedtls/library/x509.c index 941472c9de..ab105d8fa1 100644 --- a/ext/polarssl/library/x509.c +++ b/ext/mbedtls/library/x509.c @@ -1,12 +1,9 @@ /* * X.509 common functions for parsing and verification * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -44,6 +41,10 @@ #include "polarssl/x509.h" #include "polarssl/asn1.h" #include "polarssl/oid.h" + +#include +#include + #if defined(POLARSSL_PEM_PARSE_C) #include "polarssl/pem.h" #endif @@ -51,23 +52,20 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else -#define polarssl_printf printf -#define polarssl_malloc malloc +#include +#include #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_printf printf +#define polarssl_snprintf snprintf #endif -#include -#include #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) #include #else #include #endif -#if defined(EFIX64) || defined(EFI32) -#include -#endif - #if defined(POLARSSL_FS_IO) #include #if !defined(_WIN32) @@ -77,6 +75,8 @@ #endif #endif +#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); } + /* * CertificateSerialNumber ::= INTEGER */ @@ -380,7 +380,8 @@ static int x509_get_attr_type_value( unsigned char **p, if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING && **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING && - **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING ) + **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING && + **p != ASN1_BIT_STRING ) return( POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); @@ -399,6 +400,11 @@ static int x509_get_attr_type_value( unsigned char **p, } /* + * Name ::= CHOICE { -- only one possibility for now -- + * rdnSequence RDNSequence } + * + * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName + * * RelativeDistinguishedName ::= * SET OF AttributeTypeAndValue * @@ -410,9 +416,11 @@ static int x509_get_attr_type_value( unsigned char **p, * * AttributeValue ::= ANY DEFINED BY AttributeType * - * We restrict RelativeDistinguishedName to be a set of 1 element. This is - * the most common case, and our x509_name structure currently can't handle - * more than that. + * The data structure is optimized for the common case where each RDN has only + * one element, which is represented as a list of AttributeTypeAndValue. + * For the general case we still use a flat list, but we mark elements of the + * same set so that they are "merged" together in the functions that consume + * this list, eg x509_dn_gets(). */ int x509_get_name( unsigned char **p, const unsigned char *end, x509_name *cur ) @@ -421,35 +429,64 @@ int x509_get_name( unsigned char **p, const unsigned char *end, size_t set_len; const unsigned char *end_set; - /* - * parse first SET, restricted to 1 element - */ - if( ( ret = asn1_get_tag( p, end, &set_len, - ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 ) - return( POLARSSL_ERR_X509_INVALID_NAME + ret ); + /* don't use recursion, we'd risk stack overflow if not optimized */ + while( 1 ) + { + /* + * parse SET + */ + if( ( ret = asn1_get_tag( p, end, &set_len, + ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 ) + return( POLARSSL_ERR_X509_INVALID_NAME + ret ); - end_set = *p + set_len; + end_set = *p + set_len; - if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 ) - return( ret ); + while( 1 ) + { + if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 ) + return( ret ); - if( *p != end_set ) - return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE ); + if( *p == end_set ) + break; - /* - * recurse until end of SEQUENCE is reached - */ - if( *p == end ) - return( 0 ); + /* Mark this item as being not the only one in a set */ + cur->next_merged = 1; + + cur->next = polarssl_malloc( sizeof( x509_name ) ); - cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) ); + if( cur->next == NULL ) + return( POLARSSL_ERR_X509_MALLOC_FAILED ); - if( cur->next == NULL ) - return( POLARSSL_ERR_X509_MALLOC_FAILED ); + memset( cur->next, 0, sizeof( x509_name ) ); - memset( cur->next, 0, sizeof( x509_name ) ); + cur = cur->next; + } + + /* + * continue until end of SEQUENCE is reached + */ + if( *p == end ) + return( 0 ); - return( x509_get_name( p, end, cur->next ) ); + cur->next = polarssl_malloc( sizeof( x509_name ) ); + + if( cur->next == NULL ) + return( POLARSSL_ERR_X509_MALLOC_FAILED ); + + memset( cur->next, 0, sizeof( x509_name ) ); + + cur = cur->next; + } +} + +static int x509_parse_int(unsigned char **p, unsigned n, int *res){ + *res = 0; + for( ; n > 0; --n ){ + if( ( **p < '0') || ( **p > '9' ) ) return POLARSSL_ERR_X509_INVALID_DATE; + *res *= 10; + *res += (*(*p)++ - '0'); + } + return 0; } /* @@ -462,7 +499,6 @@ int x509_get_time( unsigned char **p, const unsigned char *end, { int ret; size_t len; - char date[64]; unsigned char tag; if( ( end - *p ) < 1 ) @@ -479,20 +515,19 @@ int x509_get_time( unsigned char **p, const unsigned char *end, if( ret != 0 ) return( POLARSSL_ERR_X509_INVALID_DATE + ret ); - memset( date, 0, sizeof( date ) ); - memcpy( date, *p, ( len < sizeof( date ) - 1 ) ? - len : sizeof( date ) - 1 ); - - if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ", - &time->year, &time->mon, &time->day, - &time->hour, &time->min, &time->sec ) < 5 ) + CHECK( x509_parse_int( p, 2, &time->year ) ); + CHECK( x509_parse_int( p, 2, &time->mon ) ); + CHECK( x509_parse_int( p, 2, &time->day ) ); + CHECK( x509_parse_int( p, 2, &time->hour ) ); + CHECK( x509_parse_int( p, 2, &time->min ) ); + if( len > 10 ) + CHECK( x509_parse_int( p, 2, &time->sec ) ); + if( len > 12 && *(*p)++ != 'Z' ) return( POLARSSL_ERR_X509_INVALID_DATE ); time->year += 100 * ( time->year < 50 ); time->year += 1900; - *p += len; - return( 0 ); } else if( tag == ASN1_GENERALIZED_TIME ) @@ -503,17 +538,16 @@ int x509_get_time( unsigned char **p, const unsigned char *end, if( ret != 0 ) return( POLARSSL_ERR_X509_INVALID_DATE + ret ); - memset( date, 0, sizeof( date ) ); - memcpy( date, *p, ( len < sizeof( date ) - 1 ) ? - len : sizeof( date ) - 1 ); - - if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ", - &time->year, &time->mon, &time->day, - &time->hour, &time->min, &time->sec ) < 5 ) + CHECK( x509_parse_int( p, 4, &time->year ) ); + CHECK( x509_parse_int( p, 2, &time->mon ) ); + CHECK( x509_parse_int( p, 2, &time->day ) ); + CHECK( x509_parse_int( p, 2, &time->hour ) ); + CHECK( x509_parse_int( p, 2, &time->min ) ); + if( len > 12 ) + CHECK( x509_parse_int( p, 2, &time->sec ) ); + if( len > 14 && *(*p)++ != 'Z' ) return( POLARSSL_ERR_X509_INVALID_DATE ); - *p += len; - return( 0 ); } else @@ -632,50 +666,6 @@ int x509_get_ext( unsigned char **p, const unsigned char *end, return( 0 ); } -#if defined(POLARSSL_FS_IO) -/* - * Load all data from a file into a given buffer. - */ -int x509_load_file( const char *path, unsigned char **buf, size_t *n ) -{ - FILE *f; - long size; - - if( ( f = fopen( path, "rb" ) ) == NULL ) - return( POLARSSL_ERR_X509_FILE_IO_ERROR ); - - fseek( f, 0, SEEK_END ); - if( ( size = ftell( f ) ) == -1 ) - { - fclose( f ); - return( POLARSSL_ERR_X509_FILE_IO_ERROR ); - } - fseek( f, 0, SEEK_SET ); - - *n = (size_t) size; - - if( *n + 1 == 0 || - ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) - { - fclose( f ); - return( POLARSSL_ERR_X509_MALLOC_FAILED ); - } - - if( fread( *buf, 1, *n, f ) != *n ) - { - fclose( f ); - polarssl_free( *buf ); - return( POLARSSL_ERR_X509_FILE_IO_ERROR ); - } - - fclose( f ); - - (*buf)[*n] = '\0'; - - return( 0 ); -} -#endif /* POLARSSL_FS_IO */ - #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \ !defined(EFI32) #include @@ -736,7 +726,7 @@ int x509_dn_gets( char *buf, size_t size, const x509_name *dn ) { int ret; size_t i, n; - unsigned char c; + unsigned char c, merge = 0; const x509_name *name; const char *short_name = NULL; char s[X509_MAX_DN_NAME_SIZE], *p; @@ -757,16 +747,16 @@ int x509_dn_gets( char *buf, size_t size, const x509_name *dn ) if( name != dn ) { - ret = snprintf( p, n, ", " ); + ret = polarssl_snprintf( p, n, merge ? " + " : ", " ); SAFE_SNPRINTF(); } ret = oid_get_attr_short_name( &name->oid, &short_name ); if( ret == 0 ) - ret = snprintf( p, n, "%s=", short_name ); + ret = polarssl_snprintf( p, n, "%s=", short_name ); else - ret = snprintf( p, n, "\?\?=" ); + ret = polarssl_snprintf( p, n, "\?\?=" ); SAFE_SNPRINTF(); for( i = 0; i < name->val.len; i++ ) @@ -780,8 +770,10 @@ int x509_dn_gets( char *buf, size_t size, const x509_name *dn ) else s[i] = c; } s[i] = '\0'; - ret = snprintf( p, n, "%s", s ); + ret = polarssl_snprintf( p, n, "%s", s ); SAFE_SNPRINTF(); + + merge = name->next_merged; name = name->next; } @@ -809,14 +801,14 @@ int x509_serial_gets( char *buf, size_t size, const x509_buf *serial ) if( i == 0 && nr > 1 && serial->p[i] == 0x0 ) continue; - ret = snprintf( p, n, "%02X%s", + ret = polarssl_snprintf( p, n, "%02X%s", serial->p[i], ( i < nr - 1 ) ? ":" : "" ); SAFE_SNPRINTF(); } if( nr != serial->len ) { - ret = snprintf( p, n, "...." ); + ret = polarssl_snprintf( p, n, "...." ); SAFE_SNPRINTF(); } @@ -837,9 +829,9 @@ int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, ret = oid_get_sig_alg_desc( sig_oid, &desc ); if( ret != 0 ) - ret = snprintf( p, n, "???" ); + ret = polarssl_snprintf( p, n, "???" ); else - ret = snprintf( p, n, "%s", desc ); + ret = polarssl_snprintf( p, n, "%s", desc ); SAFE_SNPRINTF(); #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT) @@ -853,7 +845,7 @@ int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, md_info = md_info_from_type( md_alg ); mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id ); - ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", + ret = polarssl_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", md_info ? md_info->name : "???", mgf_md_info ? mgf_md_info->name : "???", pss_opts->expected_salt_len ); @@ -880,7 +872,7 @@ int x509_key_size_helper( char *buf, size_t size, const char *name ) if( strlen( name ) + sizeof( " key size" ) > size ) return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); - ret = snprintf( p, n, "%s key size", name ); + ret = polarssl_snprintf( p, n, "%s key size", name ); SAFE_SNPRINTF(); return( 0 ); @@ -889,6 +881,7 @@ int x509_key_size_helper( char *buf, size_t size, const char *name ) /* * Return an informational string describing the given OID */ +#if ! defined(POLARSSL_DEPRECATED_REMOVED) const char *x509_oid_get_description( x509_buf *oid ) { const char *desc = NULL; @@ -901,12 +894,15 @@ const char *x509_oid_get_description( x509_buf *oid ) return( desc ); } +#endif /* Return the x.y.z.... style numeric string for the given OID */ +#if ! defined(POLARSSL_DEPRECATED_REMOVED) int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid ) { return oid_get_numeric_string( buf, size, oid ); } +#endif /* * Return 0 if the x509_time is still valid, or 1 otherwise. diff --git a/ext/polarssl/library/x509_create.c b/ext/mbedtls/library/x509_create.c similarity index 98% rename from ext/polarssl/library/x509_create.c rename to ext/mbedtls/library/x509_create.c index 747dc82800..f505bab80b 100644 --- a/ext/polarssl/library/x509_create.c +++ b/ext/mbedtls/library/x509_create.c @@ -1,12 +1,9 @@ /* * X.509 base functions for creating certificates / CSRs * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,6 +32,8 @@ #include "polarssl/asn1write.h" #include "polarssl/oid.h" +#include + #if defined(_MSC_VER) && !defined strncasecmp && !defined(EFIX64) && \ !defined(EFI32) #define strncasecmp _strnicmp diff --git a/ext/polarssl/library/x509_crl.c b/ext/mbedtls/library/x509_crl.c similarity index 87% rename from ext/polarssl/library/x509_crl.c rename to ext/mbedtls/library/x509_crl.c index 7dd53c2f60..de2079fc7b 100644 --- a/ext/polarssl/library/x509_crl.c +++ b/ext/mbedtls/library/x509_crl.c @@ -1,12 +1,9 @@ /* * X.509 Certidicate Revocation List (CRL) parsing * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -43,6 +40,9 @@ #include "polarssl/x509_crl.h" #include "polarssl/oid.h" + +#include + #if defined(POLARSSL_PEM_PARSE_C) #include "polarssl/pem.h" #endif @@ -50,14 +50,14 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else -#define polarssl_malloc malloc +#include +#include #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_snprintf snprintf #endif -#include -#include #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) - #include #else #include @@ -243,8 +243,8 @@ static int x509_get_entries( unsigned char **p, if( cur_entry->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); + memset( cur_entry->next, 0, sizeof( x509_crl_entry ) ); cur_entry = cur_entry->next; - memset( cur_entry, 0, sizeof( x509_crl_entry ) ); } } @@ -252,25 +252,16 @@ static int x509_get_entries( unsigned char **p, } /* - * Parse one or more CRLs and add them to the chained list + * Parse one CRLs in DER format and append it to the chained list */ -int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) +int x509_crl_parse_der( x509_crl *chain, + const unsigned char *buf, size_t buflen ) { int ret; size_t len; unsigned char *p, *end; - x509_crl *crl; x509_buf sig_params1, sig_params2; - -#if defined(POLARSSL_PEM_PARSE_C) - size_t use_len; - pem_context pem; -#endif - - memset( &sig_params1, 0, sizeof( x509_buf ) ); - memset( &sig_params2, 0, sizeof( x509_buf ) ); - - crl = chain; + x509_crl *crl = chain; /* * Check for valid input @@ -278,15 +269,18 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) if( crl == NULL || buf == NULL ) return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); - while( crl->version != 0 && crl->next != NULL ) - crl = crl->next; + memset( &sig_params1, 0, sizeof( x509_buf ) ); + memset( &sig_params2, 0, sizeof( x509_buf ) ); /* * Add new CRL on the end of the chain if needed. */ + while( crl->version != 0 && crl->next != NULL ) + crl = crl->next; + if( crl->version != 0 && crl->next == NULL ) { - crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) ); + crl->next = polarssl_malloc( sizeof( x509_crl ) ); if( crl->next == NULL ) { @@ -294,57 +288,22 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); } + x509_crl_init( crl->next ); crl = crl->next; - x509_crl_init( crl ); } -#if defined(POLARSSL_PEM_PARSE_C) - pem_init( &pem ); - ret = pem_read_buffer( &pem, - "-----BEGIN X509 CRL-----", - "-----END X509 CRL-----", - buf, NULL, 0, &use_len ); - - if( ret == 0 ) - { - /* - * Was PEM encoded - */ - buflen -= use_len; - buf += use_len; - - /* - * Steal PEM buffer - */ - p = pem.buf; - pem.buf = NULL; - len = pem.buflen; - pem_free( &pem ); - } - else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) - { - pem_free( &pem ); - return( ret ); - } - else -#endif /* POLARSSL_PEM_PARSE_C */ - { - /* - * nope, copy the raw DER data - */ - p = (unsigned char *) polarssl_malloc( len = buflen ); - - if( p == NULL ) - return( POLARSSL_ERR_X509_MALLOC_FAILED ); - - memcpy( p, buf, buflen ); + /* + * Copy raw DER-encoded CRL + */ + if( ( p = polarssl_malloc( buflen ) ) == NULL ) + return( POLARSSL_ERR_X509_MALLOC_FAILED ); - buflen = 0; - } + memcpy( p, buf, buflen ); crl->raw.p = p; - crl->raw.len = len; - end = p + len; + crl->raw.len = buflen; + + end = p + buflen; /* * CertificateList ::= SEQUENCE { @@ -503,7 +462,8 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) if( crl->sig_oid1.len != crl->sig_oid2.len || memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 || sig_params1.len != sig_params2.len || - memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) + ( sig_params1.len != 0 && + memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) ) { x509_crl_free( crl ); return( POLARSSL_ERR_X509_SIG_MISMATCH ); @@ -522,23 +482,62 @@ int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); } - if( buflen > 0 ) + return( 0 ); +} + +/* + * Parse one or more CRLs and add them to the chained list + */ +int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen ) +{ +#if defined(POLARSSL_PEM_PARSE_C) + int ret; + size_t use_len; + pem_context pem; + int is_pem = 0; + + if( chain == NULL || buf == NULL ) + return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); + + do { - crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) ); + pem_init( &pem ); + ret = pem_read_buffer( &pem, + "-----BEGIN X509 CRL-----", + "-----END X509 CRL-----", + buf, NULL, 0, &use_len ); - if( crl->next == NULL ) + if( ret == 0 ) { - x509_crl_free( crl ); - return( POLARSSL_ERR_X509_MALLOC_FAILED ); - } + /* + * Was PEM encoded + */ + is_pem = 1; - crl = crl->next; - x509_crl_init( crl ); + buflen -= use_len; + buf += use_len; - return( x509_crl_parse( crl, buf, buflen ) ); + if( ( ret = x509_crl_parse_der( chain, + pem.buf, pem.buflen ) ) != 0 ) + { + return( ret ); + } + + pem_free( &pem ); + } + else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) + { + pem_free( &pem ); + return( ret ); + } } + while( is_pem && buflen > 0 ); - return( 0 ); + if( is_pem ) + return( 0 ); + else +#endif /* POLARSSL_PEM_PARSE_C */ + return( x509_crl_parse_der( chain, buf, buflen ) ); } #if defined(POLARSSL_FS_IO) @@ -551,7 +550,7 @@ int x509_crl_parse_file( x509_crl *chain, const char *path ) size_t n; unsigned char *buf; - if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 ) + if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 ) return( ret ); ret = x509_crl_parse( chain, buf, n ); @@ -634,23 +633,23 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, p = buf; n = size; - ret = snprintf( p, n, "%sCRL version : %d", + ret = polarssl_snprintf( p, n, "%sCRL version : %d", prefix, crl->version ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sissuer name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &crl->issuer ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sthis update : " \ + ret = polarssl_snprintf( p, n, "\n%sthis update : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crl->this_update.year, crl->this_update.mon, crl->this_update.day, crl->this_update.hour, crl->this_update.min, crl->this_update.sec ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%snext update : " \ + ret = polarssl_snprintf( p, n, "\n%snext update : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crl->next_update.year, crl->next_update.mon, crl->next_update.day, crl->next_update.hour, @@ -659,20 +658,20 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, entry = &crl->entry; - ret = snprintf( p, n, "\n%sRevoked certificates:", + ret = polarssl_snprintf( p, n, "\n%sRevoked certificates:", prefix ); SAFE_SNPRINTF(); while( entry != NULL && entry->raw.len != 0 ) { - ret = snprintf( p, n, "\n%sserial number: ", + ret = polarssl_snprintf( p, n, "\n%sserial number: ", prefix ); SAFE_SNPRINTF(); ret = x509_serial_gets( p, n, &entry->serial ); SAFE_SNPRINTF(); - ret = snprintf( p, n, " revocation date: " \ + ret = polarssl_snprintf( p, n, " revocation date: " \ "%04d-%02d-%02d %02d:%02d:%02d", entry->revocation_date.year, entry->revocation_date.mon, entry->revocation_date.day, entry->revocation_date.hour, @@ -682,14 +681,14 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, entry = entry->next; } - ret = snprintf( p, n, "\n%ssigned using : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, crl->sig_md, crl->sig_opts ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n" ); + ret = polarssl_snprintf( p, n, "\n" ); SAFE_SNPRINTF(); return( (int) ( size - n ) ); diff --git a/ext/polarssl/library/x509_crt.c b/ext/mbedtls/library/x509_crt.c similarity index 90% rename from ext/polarssl/library/x509_crt.c rename to ext/mbedtls/library/x509_crt.c index 88d7f04c7a..b94f213222 100644 --- a/ext/polarssl/library/x509_crt.c +++ b/ext/mbedtls/library/x509_crt.c @@ -1,12 +1,9 @@ /* * X.509 certificate parsing and verification * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -43,6 +40,10 @@ #include "polarssl/x509_crt.h" #include "polarssl/oid.h" + +#include +#include + #if defined(POLARSSL_PEM_PARSE_C) #include "polarssl/pem.h" #endif @@ -50,33 +51,29 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else -#define polarssl_malloc malloc +#include #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_snprintf snprintf #endif #if defined(POLARSSL_THREADING_C) #include "polarssl/threading.h" #endif -#include -#include #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) #include #else #include #endif -#if defined(EFIX64) || defined(EFI32) -#include -#endif - #if defined(POLARSSL_FS_IO) #include #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32) #include #include #include -#endif +#endif /* !_WIN32 || EFIX64 || EFI32 */ #endif /* Implementation that should never be optimized out by the compiler */ @@ -313,7 +310,7 @@ static int x509_get_ext_key_usage( unsigned char **p, * nameAssigner [0] DirectoryString OPTIONAL, * partyName [1] DirectoryString } * - * NOTE: PolarSSL only parses and uses dNSName at this point. + * NOTE: we only parse and use dNSName at this point. */ static int x509_get_subject_alt_name( unsigned char **p, const unsigned char *end, @@ -359,8 +356,10 @@ static int x509_get_subject_alt_name( unsigned char **p, /* Allocate and assign next pointer */ if( cur->buf.p != NULL ) { - cur->next = (asn1_sequence *) polarssl_malloc( - sizeof( asn1_sequence ) ); + if( cur->next != NULL ) + return( POLARSSL_ERR_X509_INVALID_EXTENSIONS ); + + cur->next = polarssl_malloc( sizeof( asn1_sequence ) ); if( cur->next == NULL ) return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + @@ -478,6 +477,10 @@ static int x509_get_crt_ext( unsigned char **p, continue; } + /* Forbid repeated extensions */ + if( ( crt->ext_types & ext_type ) != 0 ) + return( POLARSSL_ERR_X509_INVALID_EXTENSIONS ); + crt->ext_types |= ext_type; switch( ext_type ) @@ -549,7 +552,7 @@ static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf, if( crt == NULL || buf == NULL ) return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); - p = (unsigned char *) polarssl_malloc( len = buflen ); + p = polarssl_malloc( len = buflen ); if( p == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); @@ -757,7 +760,8 @@ static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf, if( crt->sig_oid1.len != crt->sig_oid2.len || memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 || sig_params1.len != sig_params2.len || - memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) + ( sig_params1.len != 0 && + memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) ) { x509_crt_free( crt ); return( POLARSSL_ERR_X509_SIG_MISMATCH ); @@ -806,14 +810,14 @@ int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf, */ if( crt->version != 0 && crt->next == NULL ) { - crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) ); + crt->next = polarssl_malloc( sizeof( x509_crt ) ); if( crt->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); prev = crt; + x509_crt_init( crt->next ); crt = crt->next; - x509_crt_init( crt ); } if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 ) @@ -946,7 +950,7 @@ int x509_crt_parse_file( x509_crt *chain, const char *path ) size_t n; unsigned char *buf; - if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 ) + if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 ) return( ret ); ret = x509_crt_parse( chain, buf, n ); @@ -986,6 +990,8 @@ int x509_crt_parse_path( x509_crt *chain, const char *path ) w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 ); + if( w_ret == 0 ) + return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); hFind = FindFirstFileW( szDir, &file_data ); if( hFind == INVALID_HANDLE_VALUE ) @@ -1003,6 +1009,8 @@ int x509_crt_parse_path( x509_crt *chain, const char *path ) lstrlenW( file_data.cFileName ), p, len - 1, NULL, NULL ); + if( w_ret == 0 ) + return( POLARSSL_ERR_X509_FILE_IO_ERROR ); w_ret = x509_crt_parse_file( chain, filename ); if( w_ret < 0 ) @@ -1033,7 +1041,7 @@ int x509_crt_parse_path( x509_crt *chain, const char *path ) while( ( entry = readdir( dir ) ) != NULL ) { - snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name ); + polarssl_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name ); if( stat( entry_name, &sb ) == -1 ) { @@ -1159,7 +1167,7 @@ static int x509_info_subject_alt_name( char **buf, size_t *size, #define PRINT_ITEM(i) \ { \ - ret = snprintf( p, n, "%s" i, sep ); \ + ret = polarssl_snprintf( p, n, "%s" i, sep ); \ SAFE_SNPRINTF(); \ sep = ", "; \ } @@ -1232,7 +1240,7 @@ static int x509_info_ext_key_usage( char **buf, size_t *size, if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 ) desc = "???"; - ret = snprintf( p, n, "%s%s", sep, desc ); + ret = polarssl_snprintf( p, n, "%s%s", sep, desc ); SAFE_SNPRINTF(); sep = ", "; @@ -1262,41 +1270,41 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, p = buf; n = size; - ret = snprintf( p, n, "%scert. version : %d\n", + ret = polarssl_snprintf( p, n, "%scert. version : %d\n", prefix, crt->version ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "%sserial number : ", + ret = polarssl_snprintf( p, n, "%sserial number : ", prefix ); SAFE_SNPRINTF(); ret = x509_serial_gets( p, n, &crt->serial ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sissuer name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &crt->issuer ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssubject name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &crt->subject ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sissued on : " \ + ret = polarssl_snprintf( p, n, "\n%sissued on : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crt->valid_from.year, crt->valid_from.mon, crt->valid_from.day, crt->valid_from.hour, crt->valid_from.min, crt->valid_from.sec ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sexpires on : " \ + ret = polarssl_snprintf( p, n, "\n%sexpires on : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crt->valid_to.year, crt->valid_to.mon, crt->valid_to.day, crt->valid_to.hour, crt->valid_to.min, crt->valid_to.sec ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssigned using : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk, @@ -1310,7 +1318,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, return( ret ); } - ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, + ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, (int) pk_get_size( &crt->pk ) ); SAFE_SNPRINTF(); @@ -1320,20 +1328,20 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_BASIC_CONSTRAINTS ) { - ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, + ret = polarssl_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, crt->ca_istrue ? "true" : "false" ); SAFE_SNPRINTF(); if( crt->max_pathlen > 0 ) { - ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); + ret = polarssl_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); SAFE_SNPRINTF(); } } if( crt->ext_types & EXT_SUBJECT_ALT_NAME ) { - ret = snprintf( p, n, "\n%ssubject alt name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssubject alt name : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_subject_alt_name( &p, &n, @@ -1343,7 +1351,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_NS_CERT_TYPE ) { - ret = snprintf( p, n, "\n%scert. type : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%scert. type : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 ) @@ -1352,7 +1360,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_KEY_USAGE ) { - ret = snprintf( p, n, "\n%skey usage : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%skey usage : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 ) @@ -1361,7 +1369,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) { - ret = snprintf( p, n, "\n%sext key usage : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%sext key usage : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_ext_key_usage( &p, &n, @@ -1369,12 +1377,63 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, return( ret ); } - ret = snprintf( p, n, "\n" ); + ret = polarssl_snprintf( p, n, "\n" ); SAFE_SNPRINTF(); return( (int) ( size - n ) ); } +struct x509_crt_verify_string { + int code; + const char *string; +}; + +static const struct x509_crt_verify_string x509_crt_verify_strings[] = { + { BADCERT_EXPIRED, "The certificate validity has expired" }, + { BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" }, + { BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" }, + { BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" }, + { BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" }, + { BADCRL_EXPIRED, "The CRL is expired" }, + { BADCERT_MISSING, "Certificate was missing" }, + { BADCERT_SKIP_VERIFY, "Certificate verification was skipped" }, + { BADCERT_OTHER, "Other reason (can be used by verify callback)" }, + { BADCERT_FUTURE, "The certificate validity starts in the future" }, + { BADCRL_FUTURE, "The CRL is from the future" }, + { BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" }, + { BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" }, + { BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" }, + { 0, NULL } +}; + +int x509_crt_verify_info( char *buf, size_t size, const char *prefix, + int flags ) +{ + int ret; + const struct x509_crt_verify_string *cur; + char *p = buf; + size_t n = size; + + for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ ) + { + if( ( flags & cur->code ) == 0 ) + continue; + + ret = polarssl_snprintf( p, n, "%s%s\n", prefix, cur->string ); + SAFE_SNPRINTF(); + flags ^= cur->code; + } + + if( flags != 0 ) + { + ret = polarssl_snprintf( p, n, "%sUnknown reason " + "(this should not happen)\n", prefix ); + SAFE_SNPRINTF(); + } + + return( (int) ( size - n ) ); +} + #if defined(POLARSSL_X509_CHECK_KEY_USAGE) int x509_crt_check_key_usage( const x509_crt *crt, int usage ) { @@ -1629,25 +1688,34 @@ static int x509_string_cmp( const x509_buf *a, const x509_buf *b ) */ static int x509_name_cmp( const x509_name *a, const x509_name *b ) { - if( a == NULL && b == NULL ) - return( 0 ); + /* Avoid recursion, it might not be optimised by the compiler */ + while( a != NULL || b != NULL ) + { + if( a == NULL || b == NULL ) + return( -1 ); - if( a == NULL || b == NULL ) - return( -1 ); + /* type */ + if( a->oid.tag != b->oid.tag || + a->oid.len != b->oid.len || + memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 ) + { + return( -1 ); + } - /* type */ - if( a->oid.tag != b->oid.tag || - a->oid.len != b->oid.len || - memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 ) - { - return( -1 ); - } + /* value */ + if( x509_string_cmp( &a->val, &b->val ) != 0 ) + return( -1 ); - /* value */ - if( x509_string_cmp( &a->val, &b->val ) != 0 ) - return( -1 ); + /* structure of the list of sets */ + if( a->next_merged != b->next_merged ) + return( -1 ); - return( x509_name_cmp( a->next, b->next ) ); + a = a->next; + b = b->next; + } + + /* a == NULL == b */ + return( 0 ); } /* @@ -1703,7 +1771,7 @@ static int x509_crt_verify_top( void *p_vrfy ) { int ret; - int ca_flags = 0, check_path_cnt = path_cnt + 1; + int ca_flags = 0, check_path_cnt; unsigned char hash[POLARSSL_MD_MAX_SIZE]; const md_info_t *md_info; @@ -1734,8 +1802,10 @@ static int x509_crt_verify_top( if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 ) continue; + check_path_cnt = path_cnt + 1; + /* - * Reduce path_len to check against if top of the chain is + * Reduce check_path_cnt to check against if top of the chain is * the same as the trusted CA */ if( child->subject_raw.len == trust_ca->subject_raw.len && @@ -1822,6 +1892,13 @@ static int x509_crt_verify_child( x509_crt *grandparent; const md_info_t *md_info; + /* path_cnt is 0 for the first intermediate CA */ + if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA ) + { + *flags |= BADCERT_NOT_TRUSTED; + return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED ); + } + if( x509_time_expired( &child->valid_to ) ) *flags |= BADCERT_EXPIRED; diff --git a/ext/polarssl/library/x509_csr.c b/ext/mbedtls/library/x509_csr.c similarity index 95% rename from ext/polarssl/library/x509_csr.c rename to ext/mbedtls/library/x509_csr.c index 0b4f771f92..558b078b73 100644 --- a/ext/polarssl/library/x509_csr.c +++ b/ext/mbedtls/library/x509_csr.c @@ -1,12 +1,9 @@ /* * X.509 Certificate Signing Request (CSR) parsing * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -43,6 +40,9 @@ #include "polarssl/x509_csr.h" #include "polarssl/oid.h" + +#include + #if defined(POLARSSL_PEM_PARSE_C) #include "polarssl/pem.h" #endif @@ -50,13 +50,13 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else -#define polarssl_malloc malloc +#include +#include #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_snprintf snprintf #endif -#include -#include - #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32) #include #endif @@ -113,7 +113,7 @@ int x509_csr_parse_der( x509_csr *csr, /* * first copy the raw DER data */ - p = (unsigned char *) polarssl_malloc( len = buflen ); + p = polarssl_malloc( len = buflen ); if( p == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); @@ -310,7 +310,7 @@ int x509_csr_parse_file( x509_csr *csr, const char *path ) size_t n; unsigned char *buf; - if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 ) + if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 ) return( ret ); ret = x509_csr_parse( csr, buf, n ); @@ -390,16 +390,16 @@ int x509_csr_info( char *buf, size_t size, const char *prefix, p = buf; n = size; - ret = snprintf( p, n, "%sCSR version : %d", + ret = polarssl_snprintf( p, n, "%sCSR version : %d", prefix, csr->version ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssubject name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &csr->subject ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssigned using : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); ret = x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, @@ -412,7 +412,7 @@ int x509_csr_info( char *buf, size_t size, const char *prefix, return( ret ); } - ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, + ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, (int) pk_get_size( &csr->pk ) ); SAFE_SNPRINTF(); diff --git a/ext/polarssl/library/x509write_crt.c b/ext/mbedtls/library/x509write_crt.c similarity index 98% rename from ext/polarssl/library/x509write_crt.c rename to ext/mbedtls/library/x509write_crt.c index e298c24ecf..80913ec191 100644 --- a/ext/polarssl/library/x509write_crt.c +++ b/ext/mbedtls/library/x509write_crt.c @@ -1,12 +1,9 @@ /* * X.509 certificate writing * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,6 +39,8 @@ #include "polarssl/asn1write.h" #include "polarssl/sha1.h" +#include + #if defined(POLARSSL_PEM_WRITE_C) #include "polarssl/pem.h" #endif /* POLARSSL_PEM_WRITE_C */ diff --git a/ext/polarssl/library/x509write_csr.c b/ext/mbedtls/library/x509write_csr.c similarity index 97% rename from ext/polarssl/library/x509write_csr.c rename to ext/mbedtls/library/x509write_csr.c index 53ae9c63b7..c5a5875401 100644 --- a/ext/polarssl/library/x509write_csr.c +++ b/ext/mbedtls/library/x509write_csr.c @@ -1,12 +1,9 @@ /* * X.509 Certificate Signing Request writing * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,13 +37,13 @@ #include "polarssl/oid.h" #include "polarssl/asn1write.h" +#include +#include + #if defined(POLARSSL_PEM_WRITE_C) #include "polarssl/pem.h" #endif -#include -#include - /* Implementation that should never be optimized out by the compiler */ static void polarssl_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; diff --git a/ext/polarssl/library/xtea.c b/ext/mbedtls/library/xtea.c similarity index 97% rename from ext/polarssl/library/xtea.c rename to ext/mbedtls/library/xtea.c index 75215c50af..0558b93c89 100644 --- a/ext/polarssl/library/xtea.c +++ b/ext/mbedtls/library/xtea.c @@ -1,12 +1,9 @@ /* * An 32-bit implementation of the XTEA algorithm * - * Copyright (C) 2006-2014, Brainspark B.V. + * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. + * This file is part of mbed TLS (https://tls.mbed.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,11 +30,16 @@ #include "polarssl/xtea.h" +#include + +#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#include #define polarssl_printf printf -#endif +#endif /* POLARSSL_PLATFORM_C */ +#endif /* POLARSSL_SELF_TEST */ #if !defined(POLARSSL_XTEA_ALT) @@ -193,9 +195,6 @@ int xtea_crypt_cbc( xtea_context *ctx, int mode, size_t length, #if defined(POLARSSL_SELF_TEST) -#include -#include - /* * XTEA tests vectors (non-official) */ diff --git a/ext/polarssl/CMakeLists.txt b/ext/polarssl/CMakeLists.txt deleted file mode 100644 index 9e1158c777..0000000000 --- a/ext/polarssl/CMakeLists.txt +++ /dev/null @@ -1,111 +0,0 @@ -cmake_minimum_required(VERSION 2.6) -project(POLARSSL C) - -string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") - -if(CMAKE_COMPILER_IS_GNUCC) - set(CMAKE_C_FLAGS "-Wall -Wextra -W -Wdeclaration-after-statement -Wlogical-op -Wwrite-strings") - set(CMAKE_C_FLAGS_RELEASE "-O2") - set(CMAKE_C_FLAGS_DEBUG "-g3 -O0") - set(CMAKE_C_FLAGS_COVERAGE "-g3 -O0 --coverage") - set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-omit-frame-pointer -g3 -O1 -Werror") - set(CMAKE_C_FLAGS_CHECK "-O1 -Werror") - set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") -endif(CMAKE_COMPILER_IS_GNUCC) - -if(CMAKE_COMPILER_IS_CLANG) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wpointer-arith -Wwrite-strings -Wdocumentation -Wunreachable-code") - set(CMAKE_C_FLAGS_RELEASE "-O2") - set(CMAKE_C_FLAGS_DEBUG "-g3 -O0") - set(CMAKE_C_FLAGS_COVERAGE "-g3 -O0 --coverage") - set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-omit-frame-pointer -g3 -O1 -Werror") - set(CMAKE_C_FLAGS_CHECK "-O1 -Werror") -endif(CMAKE_COMPILER_IS_CLANG) - -set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} - CACHE STRING "Choose the type of build: None Debug Release Coverage ASan Check CheckFull" - FORCE) - -if(CMAKE_BUILD_TYPE STREQUAL "Coverage") - if(CMAKE_COMPILER_IS_GNUCC) - set(CMAKE_SHARED_LINKER_FLAGS "--coverage") - endif(CMAKE_COMPILER_IS_GNUCC) - if(CMAKE_COMPILER_IS_CLANG) - set(CMAKE_SHARED_LINKER_FLAGS "--coverage") - endif(CMAKE_COMPILER_IS_CLANG) -endif(CMAKE_BUILD_TYPE STREQUAL "Coverage") - -option(USE_PKCS11_HELPER_LIBRARY "Build PolarSSL with the pkcs11-helper library." OFF) - -option(ENABLE_ZLIB_SUPPORT "Build PolarSSL with zlib library." OFF) -option(ENABLE_PROGRAMS "Build PolarSSL programs." ON) -option(ENABLE_TESTING "Build PolarSSL tests." ON) - -if(ENABLE_TESTING) - enable_testing() -endif() - -if(LIB_INSTALL_DIR) -else() -set(LIB_INSTALL_DIR lib) -endif() - -include_directories(include/) - -if(ENABLE_ZLIB_SUPPORT) - find_package(ZLIB) - - if(ZLIB_FOUND) - include_directories(${ZLIB_INCLUDE_DIR}) - endif(ZLIB_FOUND) -endif(ENABLE_ZLIB_SUPPORT) - -add_subdirectory(library) -add_subdirectory(include) - -if(ENABLE_TESTING) - if(CMAKE_COMPILER_IS_GNUCC) - add_subdirectory(tests) - endif(CMAKE_COMPILER_IS_GNUCC) - if(CMAKE_COMPILER_IS_CLANG) - add_subdirectory(tests) - endif(CMAKE_COMPILER_IS_CLANG) -endif() - -if(ENABLE_PROGRAMS) - add_subdirectory(programs) -endif() - -ADD_CUSTOM_TARGET(apidoc - COMMAND doxygen doxygen/polarssl.doxyfile - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -if(ENABLE_TESTING) - ADD_CUSTOM_TARGET(test-ref-config - COMMAND tests/scripts/test-ref-configs.pl - ) - - ADD_CUSTOM_TARGET(covtest - COMMAND make test - COMMAND programs/test/selftest - COMMAND cd tests && ./compat.sh - COMMAND cd tests && ./ssl-opt.sh - ) - - ADD_CUSTOM_TARGET(lcov - COMMAND rm -rf Coverage - COMMAND lcov --capture --initial --directory library/CMakeFiles/polarssl.dir -o files.info - COMMAND lcov --capture --directory library/CMakeFiles/polarssl.dir -o tests.info - COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info - COMMAND lcov --remove all.info -o final.info '*.h' - COMMAND gendesc tests/Descriptions.txt -o descriptions - COMMAND genhtml --title PolarSSL --description-file descriptions --keep-descriptions --legend --no-branch-coverage -o Coverage final.info - COMMAND rm -f files.info tests.info all.info final.info descriptions - ) - - ADD_CUSTOM_TARGET(memcheck - COMMAND ctest -O memcheck.log -D ExperimentalMemCheck - COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null - COMMAND rm -f memcheck.log - ) -endif() diff --git a/ext/polarssl/include/.gitignore b/ext/polarssl/include/.gitignore deleted file mode 100644 index f3c7a7c5da..0000000000 --- a/ext/polarssl/include/.gitignore +++ /dev/null @@ -1 +0,0 @@ -Makefile diff --git a/ext/polarssl/library/.gitignore b/ext/polarssl/library/.gitignore deleted file mode 100644 index 9d80fa47a8..0000000000 --- a/ext/polarssl/library/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.o -libpolarssl* diff --git a/ext/polarssl/library/CMakeLists.txt b/ext/polarssl/library/CMakeLists.txt deleted file mode 100644 index 33d96b47f9..0000000000 --- a/ext/polarssl/library/CMakeLists.txt +++ /dev/null @@ -1,136 +0,0 @@ -option(USE_STATIC_POLARSSL_LIBRARY "Build PolarSSL static library." ON) -option(USE_SHARED_POLARSSL_LIBRARY "Build PolarSSL shared library." OFF) -option(LINK_WITH_PTHREAD "Explicitly link PolarSSL library to pthread." OFF) - -set(src - aes.c - aesni.c - arc4.c - asn1parse.c - asn1write.c - base64.c - bignum.c - blowfish.c - camellia.c - ccm.c - certs.c - cipher.c - cipher_wrap.c - ctr_drbg.c - debug.c - des.c - dhm.c - ecp.c - ecp_curves.c - ecdh.c - ecdsa.c - entropy.c - entropy_poll.c - error.c - gcm.c - havege.c - hmac_drbg.c - md.c - md_wrap.c - md2.c - md4.c - md5.c - memory_buffer_alloc.c - net.c - oid.c - padlock.c - pbkdf2.c - pem.c - pkcs5.c - pkcs11.c - pkcs12.c - pk.c - pk_wrap.c - pkparse.c - pkwrite.c - platform.c - ripemd160.c - rsa.c - sha1.c - sha256.c - sha512.c - ssl_cache.c - ssl_ciphersuites.c - ssl_cli.c - ssl_srv.c - ssl_tls.c - threading.c - timing.c - version.c - version_features.c - x509.c - x509_crt.c - x509_crl.c - x509_csr.c - x509_create.c - x509write_crt.c - x509write_csr.c - xtea.c -) - -if(WIN32) -set(libs ws2_32) -endif(WIN32) - -if(CMAKE_COMPILER_IS_GNUCC) - set(CMAKE_C_FLAGS_CHECK "${CMAKE_C_FLAGS_CHECK} -Wmissing-declarations -Wmissing-prototypes") - set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") -endif(CMAKE_COMPILER_IS_GNUCC) - -if(CMAKE_COMPILER_IS_CLANG) - set(CMAKE_C_FLAGS_CHECK "${CMAKE_C_FLAGS_CHECK} -Wmissing-declarations -Wmissing-prototypes") -endif(CMAKE_COMPILER_IS_CLANG) - -if (NOT USE_STATIC_POLARSSL_LIBRARY AND NOT USE_SHARED_POLARSSL_LIBRARY) - message(FATAL_ERROR "Need to choose static or shared polarssl build!") -endif(NOT USE_STATIC_POLARSSL_LIBRARY AND NOT USE_SHARED_POLARSSL_LIBRARY) - -if(USE_STATIC_POLARSSL_LIBRARY AND USE_SHARED_POLARSSL_LIBRARY) - # if we build both static an shared, then let - # tests and programs link to the shared lib target - set(polarssl_static_target "polarssl_static") -elseif(USE_STATIC_POLARSSL_LIBRARY) - set(polarssl_static_target "polarssl") -endif() - -if(USE_STATIC_POLARSSL_LIBRARY) - add_library(${polarssl_static_target} STATIC ${src}) - set_target_properties(${polarssl_static_target} PROPERTIES OUTPUT_NAME polarssl) - target_link_libraries(${polarssl_static_target} ${libs}) - - if(ZLIB_FOUND) - target_link_libraries(${polarssl_static_target} ${ZLIB_LIBRARIES}) - endif(ZLIB_FOUND) - - if(LINK_WITH_PTHREAD) - target_link_libraries(${polarssl_static_target} pthread) - endif() - - install(TARGETS ${polarssl_static_target} - DESTINATION ${LIB_INSTALL_DIR} - PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) -endif() - -if(USE_SHARED_POLARSSL_LIBRARY) - add_library(polarssl SHARED ${src}) - set_target_properties(polarssl PROPERTIES VERSION 1.3.9 SOVERSION 7) - - target_link_libraries(polarssl ${libs}) - - if(ZLIB_FOUND) - target_link_libraries(polarssl ${ZLIB_LIBRARIES}) - endif(ZLIB_FOUND) - - if(LINK_WITH_PTHREAD) - target_link_libraries(polarssl pthread) - endif() - - install(TARGETS polarssl - DESTINATION ${LIB_INSTALL_DIR} - PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) -endif(USE_SHARED_POLARSSL_LIBRARY) diff --git a/m4/pdns_with_system_polarssl.m4 b/m4/pdns_with_system_polarssl.m4 index 942b81b630..6322c56b37 100644 --- a/m4/pdns_with_system_polarssl.m4 +++ b/m4/pdns_with_system_polarssl.m4 @@ -5,7 +5,7 @@ AC_DEFUN([PDNS_WITH_SYSTEM_POLARSSL],[ [with_system_polarssl=no], ) - POLARSSL_SUBDIR=polarssl + POLARSSL_SUBDIR=mbedtls POLARSSL_CFLAGS=-I\$\(top_srcdir\)/ext/$POLARSSL_SUBDIR/include/ POLARSSL_LIBS="-L\$(top_builddir)/ext/$POLARSSL_SUBDIR/library/ -lpolarssl" diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index 234d384ed5..8954fafca6 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -4,8 +4,8 @@ BINDIR=/usr/bin/ SYSCONFDIR=/etc/powerdns/ LOCALSTATEDIR=/var/run/ OPTFLAGS?=-O3 -CXXFLAGS:= $(CXXFLAGS) -Iext/rapidjson/include -I$(CURDIR)/ext/polarssl/include -Wall @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(OPTFLAGS) $(PROFILEFLAGS) $(ARCHFLAGS) -pthread -Iext/yahttp -DHAVE_CONFIG_H -CFLAGS:=$(CFLAGS) -Wall $(OPTFLAGS) @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(PROFILEFLAGS) $(ARCHFLAGS) -I$(CURDIR)/ext/polarssl/include -pthread -DHAVE_CONFIG_H +CXXFLAGS:= $(CXXFLAGS) -Iext/rapidjson/include -I$(CURDIR)/ext/mbedtls/include -Wall @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(OPTFLAGS) $(PROFILEFLAGS) $(ARCHFLAGS) -pthread -Iext/yahttp -DHAVE_CONFIG_H +CFLAGS:=$(CFLAGS) -Wall $(OPTFLAGS) @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(PROFILEFLAGS) $(ARCHFLAGS) -I$(CURDIR)/ext/mbedtls/include -pthread -DHAVE_CONFIG_H LDFLAGS:=$(LDFLAGS) $(ARCHFLAGS) -pthread @LD_RELRO@ @CF_STACK@ @LD_PIE@ STRIP_BINARIES?=1 @@ -20,7 +20,7 @@ PDNS_RECURSOR_OBJECTS=syncres.o misc.o unix_utility.o qtype.o logger.o \ arguments.o lwres.o pdns_recursor.o recursor_cache.o dnsparser.o \ dnswriter.o dnsrecords.o rcpgenerator.o base64.o zoneparser-tng.o \ rec_channel.o rec_channel_rec.o selectmplexer.o sillyrecords.o \ -dns_random.o pubsuffix.o ext/polarssl/library/aes.o ext/polarssl/library/base64.o dnslabeltext.o \ +dns_random.o pubsuffix.o ext/mbedtls/library/aes.o ext/mbedtls/library/base64.o dnslabeltext.o \ lua-pdns.o lua-recursor.o randomhelper.o recpacketcache.o dns.o \ reczones.o base32.o nsecrecords.o json.o ws-recursor.o ws-api.o \ version.o responsestats.o webserver.o ext/yahttp/yahttp/reqresp.o ext/yahttp/yahttp/router.o \