Skip to content

Commit 7311c3e

Browse files
Support MBEDTLS 3 (#1374)
* Support MBEDTLS 3 * mbedTLS 2 & 3 support (incl. System libs) Co-authored-by: Markus <16664240+Paulchen-Panther@users.noreply.github.com>
1 parent f156f52 commit 7311c3e

File tree

7 files changed

+147
-74
lines changed

7 files changed

+147
-74
lines changed

cmake/Findmbedtls.cmake

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ find_library(MBEDTLS_CRYPTO_LIBRARY mbedcrypto)
77
set(MBEDTLS_LIBRARIES ${MBEDTLS_SSL_LIBRARY} ${MBEDTLS_X509_LIBRARY} ${MBEDTLS_CRYPTO_LIBRARY})
88
set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARIES} PARENT_SCOPE)
99

10-
if (MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
11-
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" _MBEDTLS_VERSION_STRING REGEX "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
12-
string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+.[0-9]+.[0-9]+).*" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
10+
if (MBEDTLS_INCLUDE_DIR)
11+
if (EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
12+
file(STRINGS ${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h _MBEDTLS_VERSION_LINE REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
13+
string(REGEX REPLACE ".*MBEDTLS_VERSION_STRING[\t ]+\"(.*)\"" "\\1" MBEDTLS_VERSION ${_MBEDTLS_VERSION_LINE})
14+
set (MBEDTLS_VERSION ${MBEDTLS_VERSION} PARENT_SCOPE)
15+
elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
16+
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" _MBEDTLS_VERSION_STRING REGEX "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
17+
string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+.[0-9]+.[0-9]+).*" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
18+
set (MBEDTLS_VERSION ${MBEDTLS_VERSION} PARENT_SCOPE)
19+
endif()
1320
endif ()
1421

1522
if (MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES AND MBEDTLS_VERSION)
@@ -20,10 +27,11 @@ if (MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES AND MBEDTLS_VERSION)
2027
REQUIRED_VARS
2128
MBEDTLS_INCLUDE_DIR
2229
MBEDTLS_LIBRARIES
30+
2331
VERSION_VAR
2432
MBEDTLS_VERSION
2533
)
2634

27-
mark_as_advanced (MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES MBEDTLS_SSL_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_CRYPTO_LIBRARY)
35+
mark_as_advanced (MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES MBEDTLS_SSL_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_CRYPTO_LIBRARY MBEDTLS_VERSION)
2836

2937
endif (MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES AND MBEDTLS_VERSION)

dependencies/CMakeLists-mbedtls.txt.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ include(ExternalProject)
1414
ExternalProject_Add(
1515
mbedtls
1616
GIT_REPOSITORY "https://github.com/ARMmbed/mbedtls.git"
17-
GIT_TAG "v2.27.0" # Latest 2.x Version
17+
GIT_TAG origin/master
1818
BUILD_ALWAYS OFF
1919
DOWNLOAD_DIR "${DOWNLOAD_DIR}"
2020
SOURCE_DIR "${SOURCE_DIR}"

dependencies/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ if (NOT USE_SYSTEM_MBEDTLS_LIBS)
241241
FetchContent_Declare(
242242
mbedtls
243243
GIT_REPOSITORY https://github.com/ARMmbed/mbedtls.git
244-
GIT_TAG "v2.27.0" # Latest 2.x Version
244+
GIT_TAG origin/master
245245
BUILD_ALWAYS OFF
246246
GIT_PROGRESS 1
247247
DOWNLOAD_DIR "${MBEDTLS_DOWNLOAD_DIR}"
@@ -286,10 +286,18 @@ if (NOT USE_SYSTEM_MBEDTLS_LIBS)
286286

287287
set (MBEDTLS_INCLUDE_DIR "${MBEDTLS_SOURCE_DIR}/include")
288288
set (MBEDTLS_INCLUDE_DIR ${MBEDTLS_INCLUDE_DIR} PARENT_SCOPE)
289-
if (MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
290-
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" _MBEDTLS_VERSION_STRING REGEX "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
291-
string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+.[0-9]+.[0-9]+).*" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
292-
message(STATUS "Using static mbedtls libraries (build version \"${MBEDTLS_VERSION}\")")
289+
if (MBEDTLS_INCLUDE_DIR)
290+
if (EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
291+
file(STRINGS ${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h _MBEDTLS_VERSION_LINE REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
292+
string(REGEX REPLACE ".*MBEDTLS_VERSION_STRING[\t ]+\"(.*)\"" "\\1" MBEDTLS_VERSION ${_MBEDTLS_VERSION_LINE})
293+
set (MBEDTLS_VERSION ${MBEDTLS_VERSION} PARENT_SCOPE)
294+
message(STATUS "Using static mbedtls libraries (build version \"${MBEDTLS_VERSION}\")")
295+
elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
296+
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" _MBEDTLS_VERSION_STRING REGEX "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
297+
string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+.[0-9]+.[0-9]+).*" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
298+
set (MBEDTLS_VERSION ${MBEDTLS_VERSION} PARENT_SCOPE)
299+
message(STATUS "Using static mbedtls libraries (build version \"${MBEDTLS_VERSION}\")")
300+
endif()
293301
endif ()
294302

295303
include_directories(${MBEDTLS_INCLUDE_DIR})

include/grabber/EncoderThread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class EncoderThreadManager : public QObject
135135
for (int i = 0; i < _threadCount; i++)
136136
{
137137
_threads[i] = new Thread<EncoderThread>(new EncoderThread, this);
138-
_threads[i]->setObjectName("Encoder " + i);
138+
_threads[i]->setObjectName("Encoder " + QString::number(i));
139139
}
140140
}
141141

libsrc/leddevice/CMakeLists.txt

100755100644
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ SET( Leddevice_SOURCES
7070
FILE ( WRITE "${CMAKE_BINARY_DIR}/LedDevice_headers.h" "#pragma once\n\n//this file is autogenerated, don't touch it\n\n" )
7171
FILE ( WRITE "${CMAKE_BINARY_DIR}/LedDevice_register.cpp" "//this file is autogenerated, don't touch it\n\n" )
7272
FOREACH( f ${Leddevice_SOURCES} )
73+
# MESSAGE (STATUS "Add led device: ${f}")
7374
if ( "${f}" MATCHES "dev_.*/Led.evice.+h$" )
7475
GET_FILENAME_COMPONENT(fname ${f} NAME)
7576
FILE ( APPEND "${CMAKE_BINARY_DIR}/LedDevice_headers.h" "#include \"${fname}\"\n" )
@@ -86,7 +87,7 @@ target_link_libraries(leddevice
8687
hyperion-utils
8788
${CMAKE_THREAD_LIBS_INIT}
8889
Qt${QT_VERSION_MAJOR}::Network
89-
Qt${QT_VERSION_MAJOR}::SerialPort
90+
Qt${QT_VERSION_MAJOR}::SerialPort
9091
ssdp
9192
)
9293

@@ -117,3 +118,8 @@ if (NOT DEFAULT_USE_SYSTEM_MBEDTLS_LIBS)
117118
target_include_directories(leddevice PRIVATE ${MBEDTLS_INCLUDE_DIR})
118119
endif (MBEDTLS_LIBRARIES)
119120
endif ()
121+
122+
string(REGEX MATCH "[0-9]+|-([A-Za-z0-9_.]+)" MBEDTLS_MAJOR ${MBEDTLS_VERSION})
123+
if (MBEDTLS_MAJOR EQUAL "3")
124+
target_compile_definitions(leddevice PRIVATE USE_MBEDTLS3)
125+
endif()

libsrc/leddevice/dev_net/ProviderUdpSSL.cpp

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// STL includes
33
#include <cstdio>
44
#include <exception>
5+
#include <algorithm>
56

67
// Linux includes
78
#include <fcntl.h>
@@ -11,7 +12,6 @@
1112

1213
// Local Hyperion includes
1314
#include "ProviderUdpSSL.h"
14-
#include <utils/QStringUtils.h>
1515

1616
const int MAX_RETRY = 5;
1717
const ushort MAX_PORT_SSL = 65535;
@@ -22,6 +22,7 @@ ProviderUdpSSL::ProviderUdpSSL(const QJsonObject &deviceConfig)
2222
, entropy()
2323
, ssl()
2424
, conf()
25+
, cacert()
2526
, ctr_drbg()
2627
, timer()
2728
, _transport_type("DTLS")
@@ -246,42 +247,42 @@ bool ProviderUdpSSL::initConnection()
246247

247248
bool ProviderUdpSSL::seedingRNG()
248249
{
249-
sslLog("Seeding the random number generator...");
250+
sslLog( "Seeding the random number generator..." );
250251

251252
mbedtls_entropy_init(&entropy);
252253

253-
sslLog("Set mbedtls_ctr_drbg_seed...");
254+
sslLog( "Set mbedtls_ctr_drbg_seed..." );
254255

255256
QByteArray customDataArray = _custom.toLocal8Bit();
256257
const char* customData = customDataArray.constData();
257258

258259
int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
259-
&entropy, reinterpret_cast<const unsigned char*>(customData),
260-
std::min(strlen(customData), (size_t)MBEDTLS_CTR_DRBG_MAX_SEED_INPUT));
260+
&entropy, reinterpret_cast<const unsigned char*>(customData),
261+
std::min(strlen(customData), (size_t)MBEDTLS_CTR_DRBG_MAX_SEED_INPUT));
261262

262263
if (ret != 0)
263264
{
264-
sslLog(QString("mbedtls_ctr_drbg_seed FAILED %1").arg(errorMsg(ret)), "error");
265+
sslLog( QString("mbedtls_ctr_drbg_seed FAILED %1").arg( errorMsg( ret ) ), "error" );
265266
return false;
266267
}
267268

268-
sslLog("Seeding the random number generator...ok");
269+
sslLog( "Seeding the random number generator...ok" );
269270

270271
return true;
271272
}
272273

273274
bool ProviderUdpSSL::setupStructure()
274275
{
275-
int ret = 0;
276-
277276
sslLog( QString( "Setting up the %1 structure").arg( _transport_type ) );
278277

279278
//TLS MBEDTLS_SSL_TRANSPORT_STREAM
280279
//DTLS MBEDTLS_SSL_TRANSPORT_DATAGRAM
281280

282281
int transport = ( _transport_type == "DTLS" ) ? MBEDTLS_SSL_TRANSPORT_DATAGRAM : MBEDTLS_SSL_TRANSPORT_STREAM;
283282

284-
if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, transport, MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
283+
int ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, transport, MBEDTLS_SSL_PRESET_DEFAULT);
284+
285+
if (ret != 0)
285286
{
286287
sslLog( QString("mbedtls_ssl_config_defaults FAILED %1").arg( errorMsg( ret ) ), "error" );
287288
return false;
@@ -291,21 +292,18 @@ bool ProviderUdpSSL::setupStructure()
291292

292293
if( _debugStreamer )
293294
{
294-
int s = ( sizeof( ciphersuites ) ) / sizeof( int );
295-
296295
QString cipher_values;
297-
for(int i=0; i<s; i++)
296+
for(int i=0; ciphersuites != nullptr && ciphersuites[i] != 0; i++)
298297
{
299-
if(i > 0) cipher_values.append(", ");
298+
if (i > 0)
299+
cipher_values.append(", ");
300300
cipher_values.append(QString::number(ciphersuites[i]));
301301
}
302302

303303
sslLog( ( QString("used ciphersuites value: %1").arg( cipher_values ) ) );
304304
}
305305

306306
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
307-
//mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
308-
//mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
309307
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
310308

311309
mbedtls_ssl_conf_ciphersuites(&conf, ciphersuites);
@@ -343,15 +341,15 @@ bool ProviderUdpSSL::startUPDConnection()
343341
{
344342
sslLog( "init SSL Network -> startUPDConnection" );
345343

346-
int ret = 0;
347-
348344
mbedtls_ssl_session_reset(&ssl);
349345

350346
if(!setupPSK()) return false;
351347

352348
sslLog( QString("Connecting to udp %1:%2").arg( _address.toString() ).arg( _ssl_port ) );
353349

354-
if ((ret = mbedtls_net_connect( &client_fd, _address.toString().toUtf8(), std::to_string(_ssl_port).c_str(), MBEDTLS_NET_PROTO_UDP)) != 0)
350+
int ret = mbedtls_net_connect(&client_fd, _address.toString().toUtf8(), std::to_string(_ssl_port).c_str(), MBEDTLS_NET_PROTO_UDP);
351+
352+
if (ret != 0)
355353
{
356354
sslLog( QString("mbedtls_net_connect FAILED %1").arg( errorMsg( ret ) ), "error" );
357355
return false;
@@ -367,15 +365,19 @@ bool ProviderUdpSSL::startUPDConnection()
367365

368366
bool ProviderUdpSSL::setupPSK()
369367
{
370-
int ret;
371-
372368
QByteArray pskArray = _psk.toUtf8();
373369
QByteArray pskRawArray = QByteArray::fromHex(pskArray);
374370

375371
QByteArray pskIdArray = _psk_identity.toUtf8();
376372
QByteArray pskIdRawArray = pskIdArray;
377373

378-
if (0 != (ret = mbedtls_ssl_conf_psk( &conf, ( const unsigned char* ) pskRawArray.data(), pskRawArray.length() * sizeof(char), reinterpret_cast<const unsigned char *> ( pskIdRawArray.data() ), pskIdRawArray.length() * sizeof(char) ) ) )
374+
int ret = mbedtls_ssl_conf_psk( &conf,
375+
reinterpret_cast<const unsigned char*> (pskRawArray.constData()),
376+
pskRawArray.length() * sizeof(char),
377+
reinterpret_cast<const unsigned char*> (pskIdRawArray.constData()),
378+
pskIdRawArray.length() * sizeof(char));
379+
380+
if (ret != 0)
379381
{
380382
sslLog( QString("mbedtls_ssl_conf_psk FAILED %1").arg( errorMsg( ret ) ), "error" );
381383
return false;
@@ -460,9 +462,12 @@ void ProviderUdpSSL::freeSSLConnection()
460462
}
461463
}
462464

463-
void ProviderUdpSSL::writeBytes(unsigned size, const unsigned char * data)
465+
void ProviderUdpSSL::writeBytes(unsigned int size, const uint8_t* data)
464466
{
465-
if( _stopConnection ) return;
467+
if ( _stopConnection )
468+
{
469+
return;
470+
}
466471

467472
QMutexLocker locker(&_hueMutex);
468473

@@ -526,6 +531,46 @@ QString ProviderUdpSSL::errorMsg(int ret) {
526531
#else
527532
switch (ret)
528533
{
534+
#if defined(MBEDTLS_ERR_SSL_DECODE_ERROR)
535+
case MBEDTLS_ERR_SSL_DECODE_ERROR:
536+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_DECODE_ERROR -0x7300";
537+
break;
538+
#endif
539+
#if defined(MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER)
540+
case MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER:
541+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER -0x6600";
542+
break;
543+
#endif
544+
#if defined(MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE)
545+
case MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE:
546+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE -0x6E00";
547+
break;
548+
#endif
549+
#if defined(MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION)
550+
case MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION:
551+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION -0x6E80";
552+
break;
553+
#endif
554+
#if defined(MBEDTLS_ERR_SSL_BAD_CERTIFICATE)
555+
case MBEDTLS_ERR_SSL_BAD_CERTIFICATE:
556+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_BAD_CERTIFICATE -0x7A00";
557+
break;
558+
#endif
559+
#if defined(MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME)
560+
case MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME:
561+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME -0x7800";
562+
break;
563+
#endif
564+
#if defined(MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION)
565+
case MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION:
566+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION -0x7500";
567+
break;
568+
#endif
569+
#if defined(MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL)
570+
case MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL:
571+
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL -0x7580";
572+
break;
573+
#endif
529574
#if defined(MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE)
530575
case MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE:
531576
msg = "The requested feature is not available. - MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE -0x7080";
@@ -822,3 +867,40 @@ void ProviderUdpSSL::closeSSLNotify()
822867

823868
sslLog( "SSL Connection successful closed" );
824869
}
870+
871+
void ProviderUdpSSL::ProviderUdpSSLDebug(void* ctx, int level, const char* file, int line, const char* str)
872+
{
873+
const char* p, * basename;
874+
(void)ctx;
875+
/* Extract basename from file */
876+
for (p = basename = file; *p != '\0'; p++)
877+
{
878+
if (*p == '/' || *p == '\\')
879+
{
880+
basename = p + 1;
881+
}
882+
}
883+
mbedtls_printf("%s:%04d: |%d| %s", basename, line, level, str);
884+
}
885+
886+
int ProviderUdpSSL::ProviderUdpSSLVerify(void* data, mbedtls_x509_crt* crt, int depth, uint32_t* flags)
887+
{
888+
const uint32_t buf_size = 1024;
889+
char* buf = new char[buf_size];
890+
(void)data;
891+
892+
mbedtls_printf("\nVerifying certificate at depth %d:\n", depth);
893+
mbedtls_x509_crt_info(buf, buf_size - 1, " ", crt);
894+
mbedtls_printf("%s", buf);
895+
896+
if (*flags == 0)
897+
mbedtls_printf("No verification issue for this certificate\n");
898+
else
899+
{
900+
mbedtls_x509_crt_verify_info(buf, buf_size, " ! ", *flags);
901+
mbedtls_printf("%s\n", buf);
902+
}
903+
904+
delete[] buf;
905+
return 0;
906+
}

0 commit comments

Comments
 (0)