Skip to content

Commit 699437e

Browse files
author
Jamie Smith
authored
Allow Mbed CE to be built as an Arduino core (ARMmbed#206)
* Various changes for Arduino core support * Apply Arduino Mbed TLS patches * Apply linker script patch, add missing CXX standard * Fix SDBlockDevice compile error, fix "no rule to make mbed-target-config.h" * Fix missing source file for RPi Pico * Fix missing licenses
1 parent 8e38d02 commit 699437e

File tree

41 files changed

+3469
-126
lines changed

Some content is hidden

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

41 files changed

+3469
-126
lines changed

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,10 @@ endif()
228228

229229
# Generate target config header and include it in all files
230230
if(NOT MBED_IS_NATIVE_BUILD)
231-
mbed_write_target_config_header(${CMAKE_CURRENT_BINARY_DIR}/mbed-target-config.h MBED_TARGET_DEFINITIONS MBED_CONFIG_DEFINITIONS)
232-
target_compile_options(mbed-core-flags INTERFACE -include ${CMAKE_CURRENT_BINARY_DIR}/mbed-target-config.h)
231+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated-headers)
232+
mbed_write_target_config_header(${CMAKE_CURRENT_BINARY_DIR}/generated-headers/mbed-target-config.h MBED_TARGET_DEFINITIONS MBED_CONFIG_DEFINITIONS)
233+
target_compile_options(mbed-core-flags INTERFACE -include ${CMAKE_CURRENT_BINARY_DIR}/generated-headers/mbed-target-config.h)
234+
target_include_directories(mbed-core-flags INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/generated-headers)
233235
endif()
234236

235237
# Include mbed.h and config from generate folder
@@ -283,7 +285,7 @@ if(NOT MBED_IS_NATIVE_BUILD)
283285
mbed_create_distro(mbed-os ${MBED_TARGET_CMAKE_NAME} mbed-core-flags mbed-core-sources mbed-rtos-flags mbed-rtos-sources)
284286

285287
# Set up the linker script and hook it up to the top-level OS targets
286-
mbed_setup_linker_script(mbed-baremetal mbed-os ${CMAKE_CURRENT_BINARY_DIR}/mbed-target-config.h)
288+
mbed_setup_linker_script(mbed-baremetal mbed-os ${CMAKE_CURRENT_BINARY_DIR}/generated-headers/mbed-target-config.h)
287289

288290
# Make sure that things linking mbed-core-flags can also get the target-specific include dirs and flags.
289291
mbed_extract_flags(${MBED_TARGET_CMAKE_NAME}-flags ${MBED_TARGET_CMAKE_NAME})

connectivity/mbedtls/include/mbedtls/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@
12041204
*
12051205
* Enable functions that use the filesystem.
12061206
*/
1207-
//#define MBEDTLS_FS_IO
1207+
#define MBEDTLS_FS_IO
12081208

12091209
/**
12101210
* \def MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
@@ -3227,7 +3227,7 @@
32273227
* on it, and considering stronger message digests instead.
32283228
*
32293229
*/
3230-
//#define MBEDTLS_SHA1_C
3230+
#define MBEDTLS_SHA1_C
32313231

32323232
/**
32333233
* \def MBEDTLS_SHA256_C

connectivity/mbedtls/mbed_lib.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

connectivity/mbedtls/mbed_lib.json5

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "mbedtls",
3+
"config": {
4+
"entropy-nv-seed": {
5+
"macro_name": "MBEDTLS_ENTROPY_NV_SEED",
6+
"help": "Set to 1 to enable Mbed TLS's Non-Volatile Storage entropy source. This source allows usage of Mbed TLS on devices which do not have a cryptographic RNG.",
7+
"value": null,
8+
// Note: see here for details on how to implement the seed I/O: https://os.mbed.com/docs/mbed-os/v6.16/porting/entropy-sources.html
9+
}
10+
}
11+
}

connectivity/mbedtls/platform/inc/platform_mbed.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
* \ingroup public-crypto
3232
*/
3333

34+
#if CONFIG_MBEDTLS_ENTROPY_NV_SEED
35+
#define MBEDTLS_ENTROPY_NV_SEED
36+
#endif
37+
3438
#if defined(FEATURE_EXPERIMENTAL_API) && defined(FEATURE_PSA)
3539

3640
#if defined(MBEDTLS_ENTROPY_NV_SEED)
@@ -72,9 +76,6 @@
7276
#include "mbedtls_device.h"
7377
#endif
7478

75-
// Include SHA1 certificate support. Used for a lot of root CAs.
76-
#define MBEDTLS_SHA1_C 1
77-
7879
/*
7980
* MBEDTLS_ERR_PLATFORM_HW_FAILED is deprecated and should not be used.
8081
*/

connectivity/mbedtls/source/x509_crt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@
7474
#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
7575
#include <sys/types.h>
7676
#include <sys/stat.h>
77+
#if defined(__MBED__)
78+
#include <platform/mbed_retarget.h>
79+
#else
7780
#include <dirent.h>
81+
#endif /* __MBED__ */
7882
#endif /* !_WIN32 || EFIX64 || EFI32 */
7983
#endif
8084

connectivity/mbedtls/tools/importer/adjust-config.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22
#
33
# This file is part of mbed TLS (https://tls.mbed.org)
44
#
5-
# Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
5+
# Copyright (c) 2023, Arm Limited, All Rights Reserved
6+
#
7+
# SPDX-License-Identifier: Apache-2.0
8+
# Licensed under the Apache License, Version 2.0 (the License); you may
9+
# not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# * http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
16+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
619
#
720
# Purpose
821
#
@@ -53,7 +66,6 @@ conf unset MBEDTLS_TIMING_C
5366

5467
# not supported on all targets with mbed OS, nor used by mbed Client
5568
conf unset MBEDTLS_HAVE_TIME_DATE
56-
conf unset MBEDTLS_FS_IO
5769
conf unset MBEDTLS_PSA_ITS_FILE_C
5870
conf unset MBEDTLS_PSA_CRYPTO_STORAGE_C
5971
conf set MBEDTLS_NO_PLATFORM_ENTROPY
@@ -89,7 +101,6 @@ conf unset MBEDTLS_PEM_WRITE_C
89101
conf unset MBEDTLS_PKCS5_C
90102
conf unset MBEDTLS_PKCS12_C
91103
conf unset MBEDTLS_RIPEMD160_C
92-
conf unset MBEDTLS_SHA1_C
93104
conf unset MBEDTLS_XTEA_C
94105

95106
conf set MBEDTLS_CMAC_C

connectivity/mbedtls/tools/importer/adjust-no-entropy-config.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22
#
33
# This file is part of mbed TLS (https://tls.mbed.org)
44
#
5-
# Copyright (c) 2018, ARM Limited, All Rights Reserved
5+
# Copyright (c) 2018, Arm Limited, All Rights Reserved
6+
#
7+
# SPDX-License-Identifier: Apache-2.0
8+
# Licensed under the Apache License, Version 2.0 (the License); you may
9+
# not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# * http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
16+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
619
#
720
# Purpose
821
#

connectivity/nanostack/coap-service/test/coap-service/unittest/stub/mbedtls_stub.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *a, const unsigned char *b, size_t c
303303
return mbedtls_stub.expected_int;
304304
}
305305

306+
int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *a, const char *b)
307+
{
308+
// means 5 valid certificates found
309+
return 5;
310+
}
311+
306312
int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
307313
const mbedtls_x509_crt *crt)
308314
{

connectivity/netsocket/include/netsocket/TLSSocketWrapper.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,46 @@ class TLSSocketWrapper : public Socket {
116116
*/
117117
nsapi_error_t set_root_ca_cert(const char *root_ca_pem);
118118

119+
/**
120+
* @brief Sets the Root CA certificate to a collection of files on the filesystem.
121+
*
122+
* All files in the supplied directory will be scanned. Note that to set up a filesystem,
123+
* you must mount one or more block devices before calling this function.
124+
*
125+
* @note Must be called before calling connect()
126+
*
127+
* @param root_ca_path Path containing Root CA Certificate files in any Mbed TLS-supported format.
128+
* This can point to a directory on any mounted filesystem.
129+
* @retval NSAPI_ERROR_OK on success.
130+
* @retval NSAPI_ERROR_NO_MEMORY in case there is not enough memory to allocate certificate.
131+
* @retval NSAPI_ERROR_PARAMETER in case the provided root_ca parameter failed parsing.
132+
*
133+
*/
134+
nsapi_error_t set_root_ca_cert_path(const char *root_ca_path);
135+
136+
/** Appends the certificate to an existing CA chain.
137+
*
138+
* @note Must be called before calling connect()
139+
*
140+
* @param root_ca Root CA Certificate in any Mbed TLS-supported format.
141+
* @param len Length of certificate (including terminating 0 for PEM).
142+
* @retval NSAPI_ERROR_OK on success.
143+
* @retval NSAPI_ERROR_NO_MEMORY in case there is not enough memory to allocate certificate.
144+
* @retval NSAPI_ERROR_PARAMETER in case the provided root_ca parameter failed parsing.
145+
*/
146+
nsapi_error_t append_root_ca_cert(const void *root_ca, size_t len);
147+
148+
/** Appends the certificate to an existing CA chain.
149+
*
150+
* @note Must be called before calling connect()
151+
*
152+
* @param root_ca_pem Root CA Certificate in PEM format.
153+
* @retval NSAPI_ERROR_OK on success.
154+
* @retval NSAPI_ERROR_NO_MEMORY in case there is not enough memory to allocate certificate.
155+
* @retval NSAPI_ERROR_PARAMETER in case the provided root_ca parameter failed parsing.
156+
*/
157+
nsapi_error_t append_root_ca_cert(const char *root_ca_pem);
158+
119159
/** Sets client certificate, and client private key.
120160
*
121161
* @param client_cert Client certification in PEM or DER format.

connectivity/netsocket/source/TLSSocketWrapper.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,62 @@ nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const char *root_ca_pem)
136136
return set_root_ca_cert(root_ca_pem, strlen(root_ca_pem) + 1);
137137
}
138138

139+
nsapi_error_t TLSSocketWrapper::set_root_ca_cert_path(const char *root_ca_path)
140+
{
141+
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO)
142+
return NSAPI_ERROR_UNSUPPORTED;
143+
#else
144+
mbedtls_x509_crt *crt;
145+
146+
crt = new (std::nothrow) mbedtls_x509_crt;
147+
if (!crt) {
148+
return NSAPI_ERROR_NO_MEMORY;
149+
}
150+
151+
mbedtls_x509_crt_init(crt);
152+
153+
/* Parse CA certification */
154+
int ret = mbedtls_x509_crt_parse_path(crt, root_ca_path);
155+
if (ret < 0) {
156+
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
157+
mbedtls_x509_crt_free(crt);
158+
delete crt;
159+
return NSAPI_ERROR_PARAMETER;
160+
}
161+
set_ca_chain(crt);
162+
_cacert_allocated = true;
163+
return NSAPI_ERROR_OK;
164+
#endif
165+
}
166+
167+
nsapi_error_t TLSSocketWrapper::append_root_ca_cert(const void *root_ca, size_t len)
168+
{
169+
#if !defined(MBEDTLS_X509_CRT_PARSE_C)
170+
return NSAPI_ERROR_UNSUPPORTED;
171+
#else
172+
mbedtls_x509_crt *crt;
173+
174+
crt = get_ca_chain();
175+
if (!crt) {
176+
return NSAPI_ERROR_NO_MEMORY;
177+
}
178+
179+
/* Parse CA certification */
180+
int ret;
181+
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(root_ca),
182+
len)) != 0) {
183+
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
184+
return NSAPI_ERROR_PARAMETER;
185+
}
186+
return NSAPI_ERROR_OK;
187+
#endif
188+
}
189+
190+
nsapi_error_t TLSSocketWrapper::append_root_ca_cert(const char *root_ca_pem)
191+
{
192+
return append_root_ca_cert(root_ca_pem, strlen(root_ca_pem) + 1);
193+
}
194+
139195
nsapi_error_t TLSSocketWrapper::set_client_cert_key(const char *client_cert_pem, const char *client_private_key_pem)
140196
{
141197
return set_client_cert_key(client_cert_pem, strlen(client_cert_pem) + 1, client_private_key_pem, strlen(client_private_key_pem) + 1);

connectivity/netsocket/tests/TESTS/netsocket/tls/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ list(
1919
tlssocket_endpoint_close.cpp
2020
tlssocket_echotest.cpp
2121
tlssocket_echotest_burst.cpp
22-
tlssocket_connect_invalid.cpp
22+
tlssocket_connect_invalid.cpp
23+
tlssocket_cert_in_filesystem.cpp
2324
)
2425

2526
if(MBED_GREENTEA_TEST_BAREMETAL)
@@ -33,6 +34,8 @@ mbed_greentea_add_test(
3334
${TEST_SOURCE_LIST}
3435
TEST_REQUIRED_LIBS
3536
mbed-netsocket
37+
mbed-storage-blockdevice
38+
mbed-storage-littlefs
3639
TEST_SKIPPED
3740
${TEST_SKIPPED}
3841
)

connectivity/netsocket/tests/TESTS/netsocket/tls/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ static void test_failure_handler(const failure_t failure)
218218

219219

220220
Case cases[] = {
221-
// Disable tests temporarily till echo server is back on
222221
Case("TLSSOCKET_ECHOTEST", TLSSOCKET_ECHOTEST),
223222
Case("TLSSOCKET_ECHOTEST_NONBLOCK", TLSSOCKET_ECHOTEST_NONBLOCK),
224223
Case("TLSSOCKET_CONNECT_INVALID", TLSSOCKET_CONNECT_INVALID),
@@ -235,6 +234,9 @@ Case cases[] = {
235234
Case("TLSSOCKET_SEND_REPEAT", TLSSOCKET_SEND_REPEAT),
236235
Case("TLSSOCKET_SEND_TIMEOUT", TLSSOCKET_SEND_TIMEOUT),
237236
Case("TLSSOCKET_NO_CERT", TLSSOCKET_NO_CERT),
237+
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_FS_IO)
238+
Case("TLSSOCKET_CERT_IN_FILESYSTEM", TLSSOCKET_CERT_IN_FILESYSTEM),
239+
#endif
238240
// Temporarily removing this test, as TLS library consumes too much memory
239241
// and we see frequent memory allocation failures on architectures with less
240242
// RAM such as DISCO_L475VG_IOT1A and NUCLEO_F207ZG (both have 128 kB RAM)

connectivity/netsocket/tests/TESTS/netsocket/tls/tls_tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void TLSSOCKET_SEND_REPEAT();
9191
void TLSSOCKET_NO_CERT();
9292
void TLSSOCKET_SIMULTANEOUS();
9393
void TLSSOCKET_SEND_TIMEOUT();
94+
void TLSSOCKET_CERT_IN_FILESYSTEM();
9495

9596
#endif // defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
9697

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2020, Arduino SA, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "TLSSocket.h"
20+
#include "greentea-client/test_env.h"
21+
#include "unity/unity.h"
22+
#include "utest.h"
23+
#include "tls_tests.h"
24+
#include "HeapBlockDevice.h"
25+
#include "LittleFileSystem.h"
26+
27+
using namespace utest::v1;
28+
29+
void TLSSOCKET_CERT_IN_FILESYSTEM()
30+
{
31+
SKIP_IF_TCP_UNSUPPORTED();
32+
33+
HeapBlockDevice bd(1024 * 10);
34+
LittleFileSystem fs("fs");
35+
TEST_ASSERT_EQUAL(0, fs.format(&bd));
36+
TEST_ASSERT_EQUAL(0, fs.mount(&bd));
37+
38+
FILE *fp = fopen("/fs/certs.pem", "wb");
39+
int ret = fwrite(tls_global::cert, strlen(tls_global::cert), 1, fp);
40+
fclose(fp);
41+
42+
TLSSocket sock;
43+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance()));
44+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert_path("/fs"));
45+
46+
SocketAddress a;
47+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, NetworkInterface::get_default_instance()->gethostbyname(ECHO_SERVER_ADDR, &a));
48+
a.set_port(ECHO_SERVER_PORT_TLS);
49+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(a));
50+
}

connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ TEST_F(TestTLSSocketWrapper, set_root_ca_cert_invalid)
399399
EXPECT_EQ(wrapper->set_root_ca_cert(cert, strlen(cert)), NSAPI_ERROR_PARAMETER);
400400
}
401401

402+
TEST_F(TestTLSSocketWrapper, set_root_ca_cert_path)
403+
{
404+
EXPECT_EQ(transport->open(&stack), NSAPI_ERROR_OK);
405+
EXPECT_EQ(wrapper->set_root_ca_cert_path("/"), NSAPI_ERROR_OK);
406+
}
407+
402408
TEST_F(TestTLSSocketWrapper, set_client_cert_key)
403409
{
404410
EXPECT_EQ(wrapper->get_own_cert(), static_cast<mbedtls_x509_crt *>(NULL));

connectivity/netsocket/tests/UNITTESTS/netsocket/TLSSocketWrapper/tls_test_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
#define UNITTESTS_FEATURES_NETSOCKET_TLSSOCKET_TLS_TEST_CONFIG_H_
2020

2121
#define MBEDTLS_SSL_CLI_C
22-
22+
#define MBEDTLS_FS_IO
2323

2424
#endif /* UNITTESTS_FEATURES_NETSOCKET_TLSSOCKET_TLS_TEST_CONFIG_H_ */

0 commit comments

Comments
 (0)