Skip to content

Commit c72e96a

Browse files
WIP
1 parent 0e8f04f commit c72e96a

File tree

5 files changed

+215
-15
lines changed

5 files changed

+215
-15
lines changed

src/core/radio/ble_secure.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ Error BleSecure::Start(ConnectCallback aConnectHandler, ReceiveCallback aReceive
8989
Error BleSecure::TcatStart(const MeshCoP::TcatAgent::VendorInfo &aVendorInfo,
9090
MeshCoP::TcatAgent::JoinCallback aJoinHandler)
9191
{
92-
return mTcatAgent.Start(aVendorInfo, mReceiveCallback.GetHandler(), aJoinHandler, mReceiveCallback.GetContext());
92+
Error error;
93+
VerifyOrExit(mBleState != kStopped, error = kErrorInvalidState);
94+
error = mTcatAgent.Start(aVendorInfo, mReceiveCallback.GetHandler(), aJoinHandler, mReceiveCallback.GetContext());
95+
96+
exit:
97+
return error;
9398
}
9499

95100
void BleSecure::Stop(void)
@@ -124,8 +129,13 @@ void BleSecure::Stop(void)
124129
Error BleSecure::Connect(void)
125130
{
126131
Ip6::SockAddr sockaddr;
132+
Error error;
127133

128-
return mTls.Connect(sockaddr);
134+
VerifyOrExit(mBleState == kConnected, error = kErrorInvalidState);
135+
136+
error = mTls.Connect(sockaddr);
137+
exit:
138+
return error;
129139
}
130140

131141
void BleSecure::Disconnect(void)
@@ -137,8 +147,11 @@ void BleSecure::Disconnect(void)
137147

138148
if (mBleState == kConnected)
139149
{
150+
mBleState = kAdvertising;
140151
IgnoreReturnValue(otPlatBleGapDisconnect(&GetInstance()));
141152
}
153+
154+
mConnectCallback.InvokeIfSet(&GetInstance(), false, false);
142155
}
143156

144157
void BleSecure::SetPsk(const MeshCoP::JoinerPskd &aPskd)
@@ -278,12 +291,7 @@ void BleSecure::HandleBleDisconnected(uint16_t aConnectionId)
278291
mBleState = kAdvertising;
279292
mMtuSize = kInitialMtuSize;
280293

281-
if (IsConnected())
282-
{
283-
Disconnect(); // Stop TLS connection
284-
}
285-
286-
mConnectCallback.InvokeIfSet(&GetInstance(), false, false);
294+
Disconnect(); // Stop TLS connection
287295
}
288296

289297
Error BleSecure::HandleBleMtuUpdate(uint16_t aMtu)

tests/toranj/openthread-core-toranj-config.h

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363

6464
#define OPENTHREAD_CONFIG_MESH_DIAG_ENABLE 1
6565

66+
#define OPENTHREAD_CONFIG_BLE_TCAT_ENABLE 1
67+
6668
#define OPENTHREAD_CONFIG_COMMISSIONER_ENABLE 1
6769

6870
#define OPENTHREAD_CONFIG_COMMISSIONER_MAX_JOINER_ENTRIES 4

tests/unit/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,27 @@ target_link_libraries(ot-test-string
11801180

11811181
add_test(NAME ot-test-string COMMAND ot-test-string)
11821182

1183+
add_executable(ot-test-tcat
1184+
test_tcat.cpp
1185+
)
1186+
1187+
target_include_directories(ot-test-tcat
1188+
PRIVATE
1189+
${COMMON_INCLUDES}
1190+
)
1191+
1192+
target_compile_options(ot-test-tcat
1193+
PRIVATE
1194+
${COMMON_COMPILE_OPTIONS}
1195+
)
1196+
1197+
target_link_libraries(ot-test-tcat
1198+
PRIVATE
1199+
${COMMON_LIBS}
1200+
)
1201+
1202+
add_test(NAME ot-test-tcat COMMAND ot-test-tcat)
1203+
11831204
add_executable(ot-test-timer
11841205
test_timer.cpp
11851206
)

tests/unit/test_platform.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -694,47 +694,47 @@ OT_TOOL_WEAK otError otPlatSetMcuPowerState(otInstance *aInstance, otPlatMcuPowe
694694
otError otPlatBleEnable(otInstance *aInstance)
695695
{
696696
OT_UNUSED_VARIABLE(aInstance);
697-
return OT_ERROR_NOT_IMPLEMENTED;
697+
return OT_ERROR_NONE;
698698
}
699699

700700
otError otPlatBleDisable(otInstance *aInstance)
701701
{
702702
OT_UNUSED_VARIABLE(aInstance);
703-
return OT_ERROR_NOT_IMPLEMENTED;
703+
return OT_ERROR_NONE;
704704
}
705705

706706
otError otPlatBleGapAdvStart(otInstance *aInstance, uint16_t aInterval)
707707
{
708708
OT_UNUSED_VARIABLE(aInstance);
709709
OT_UNUSED_VARIABLE(aInterval);
710-
return OT_ERROR_NOT_IMPLEMENTED;
710+
return OT_ERROR_NONE;
711711
}
712712

713713
otError otPlatBleGapAdvStop(otInstance *aInstance)
714714
{
715715
OT_UNUSED_VARIABLE(aInstance);
716-
return OT_ERROR_NOT_IMPLEMENTED;
716+
return OT_ERROR_NONE;
717717
}
718718

719719
otError otPlatBleGapDisconnect(otInstance *aInstance)
720720
{
721721
OT_UNUSED_VARIABLE(aInstance);
722-
return OT_ERROR_NOT_IMPLEMENTED;
722+
return OT_ERROR_NONE;
723723
}
724724

725725
otError otPlatBleGattMtuGet(otInstance *aInstance, uint16_t *aMtu)
726726
{
727727
OT_UNUSED_VARIABLE(aInstance);
728728
OT_UNUSED_VARIABLE(aMtu);
729-
return OT_ERROR_NOT_IMPLEMENTED;
729+
return OT_ERROR_NONE;
730730
}
731731

732732
otError otPlatBleGattServerIndicate(otInstance *aInstance, uint16_t aHandle, const otBleRadioPacket *aPacket)
733733
{
734734
OT_UNUSED_VARIABLE(aInstance);
735735
OT_UNUSED_VARIABLE(aHandle);
736736
OT_UNUSED_VARIABLE(aPacket);
737-
return OT_ERROR_NOT_IMPLEMENTED;
737+
return OT_ERROR_NONE;
738738
}
739739
#endif // OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
740740

tests/unit/test_tcat.cpp

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 2024, The OpenThread Authors.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include "openthread-core-config.h"
30+
31+
#include "test_platform.h"
32+
#include "test_util.h"
33+
34+
#if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
35+
36+
#include <openthread/ble_secure.h>
37+
38+
#define OT_TCAT_X509_CERT \
39+
"-----BEGIN CERTIFICATE-----\r\n" \
40+
"MIIBmDCCAT+gAwIBAgIEAQIDBDAKBggqhkjOPQQDAjBvMQswCQYDVQQGEwJYWDEQ\r\n" \
41+
"MA4GA1UECBMHTXlTdGF0ZTEPMA0GA1UEBxMGTXlDaXR5MQ8wDQYDVQQLEwZNeVVu\r\n" \
42+
"aXQxETAPBgNVBAoTCE15VmVuZG9yMRkwFwYDVQQDExB3d3cubXl2ZW5kb3IuY29t\r\n" \
43+
"MB4XDTIzMTAxNjEwMzk1NFoXDTI0MTAxNjEwMzk1NFowIjEgMB4GA1UEAxMXbXl2\r\n" \
44+
"ZW5kb3IuY29tL3RjYXQvbXlkZXYwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQB\r\n" \
45+
"aWwFDNj1bpQIdN+Kp2cHWw55U/+fa+OmZnoy1B4BOT+822jdwPBuyXWAQoBdYdQJ\r\n" \
46+
"ff4RgmhczyV4PhArPIuAoxYwFDASBgkrBgEEAYLfKgMEBQABAQEBMAoGCCqGSM49\r\n" \
47+
"BAMCA0cAMEQCIBEHxiEDij26y6V77Q311Gj4CZAuZuPGXZpnzL2BLk7bAiAlFk6G\r\n" \
48+
"mYGzkcrYyssFI9HlPgrisWoMmgummaTtCuvrEw==\r\n" \
49+
"-----END CERTIFICATE-----\r\n"
50+
51+
#define OT_TCAT_PRIV_KEY \
52+
"-----BEGIN EC PRIVATE KEY-----\r\n" \
53+
"MHcCAQEEIDeJ6lVQKiOIBxKwTZp6TkU5QVHt9pvXOR9CGpPBI3DhoAoGCCqGSM49\r\n" \
54+
"AwEHoUQDQgAEAWlsBQzY9W6UCHTfiqdnB1sOeVP/n2vjpmZ6MtQeATk/vNto3cDw\r\n" \
55+
"bsl1gEKAXWHUCX3+EYJoXM8leD4QKzyLgA==\r\n" \
56+
"-----END EC PRIVATE KEY-----\r\n"
57+
58+
#define OT_TCAT_TRUSTED_ROOT_CERTIFICATE \
59+
"-----BEGIN CERTIFICATE-----\r\n" \
60+
"MIICCDCCAa2gAwIBAgIJAIKxygBXoH+5MAoGCCqGSM49BAMCMG8xCzAJBgNVBAYT\r\n" \
61+
"AlhYMRAwDgYDVQQIEwdNeVN0YXRlMQ8wDQYDVQQHEwZNeUNpdHkxDzANBgNVBAsT\r\n" \
62+
"Bk15VW5pdDERMA8GA1UEChMITXlWZW5kb3IxGTAXBgNVBAMTEHd3dy5teXZlbmRv\r\n" \
63+
"ci5jb20wHhcNMjMxMDE2MTAzMzE1WhcNMjYxMDE2MTAzMzE1WjBvMQswCQYDVQQG\r\n" \
64+
"EwJYWDEQMA4GA1UECBMHTXlTdGF0ZTEPMA0GA1UEBxMGTXlDaXR5MQ8wDQYDVQQL\r\n" \
65+
"EwZNeVVuaXQxETAPBgNVBAoTCE15VmVuZG9yMRkwFwYDVQQDExB3d3cubXl2ZW5k\r\n" \
66+
"b3IuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWdyzPAXGKeZY94OhHAWX\r\n" \
67+
"HzJfQIjGSyaOzlgL9OEFw2SoUDncLKPGwfPAUSfuMyEkzszNDM0HHkBsDLqu4n25\r\n" \
68+
"/6MyMDAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQU4EynoSw9eDKZEVPkums2\r\n" \
69+
"IWLAJCowCgYIKoZIzj0EAwIDSQAwRgIhAMYGGL9xShyE6P9wEU+MAYF6W3CzdrwV\r\n" \
70+
"kuerX1encIH2AiEA5rq490NUobM1Au43roxJq1T6Z43LscPVbGZfULD1Jq0=\r\n" \
71+
"-----END CERTIFICATE-----\r\n"
72+
73+
namespace ot {
74+
75+
class TestBleSecure
76+
{
77+
public:
78+
TestBleSecure(void)
79+
: mIsConnected(false)
80+
, mIsBleConnectionOpen(false)
81+
{
82+
}
83+
void HandleBleSecureConnect(bool aConnected, bool aBleConnectionOpen)
84+
{
85+
mIsConnected = aConnected;
86+
mIsBleConnectionOpen = aBleConnectionOpen;
87+
}
88+
89+
bool IsConnected(void) const { return mIsConnected; }
90+
bool IsBleConnectionOpen(void) const { return mIsBleConnectionOpen; }
91+
92+
private:
93+
bool mIsConnected;
94+
bool mIsBleConnectionOpen;
95+
};
96+
97+
static void HandleBleSecureConnect(otInstance *aInstance, bool aConnected, bool aBleConnectionOpen, void *aContext)
98+
{
99+
OT_UNUSED_VARIABLE(aInstance);
100+
101+
static_cast<TestBleSecure *>(aContext)->HandleBleSecureConnect(aConnected, aBleConnectionOpen);
102+
}
103+
104+
void TestTcat(void)
105+
{
106+
const char kPskdVendor[] = "J01NM3";
107+
const char kUrl[] = "dummy_url";
108+
constexpr uint16_t kConnectionId = 0;
109+
110+
TestBleSecure ble;
111+
Instance *instance = testInitInstance();
112+
113+
otTcatVendorInfo vendorInfo = {.mProvisioningUrl = kUrl, .mPskdString = kPskdVendor};
114+
115+
otBleSecureSetCertificate(instance, reinterpret_cast<const uint8_t *>(OT_TCAT_X509_CERT), sizeof(OT_TCAT_X509_CERT),
116+
reinterpret_cast<const uint8_t *>(OT_TCAT_PRIV_KEY), sizeof(OT_TCAT_PRIV_KEY));
117+
118+
otBleSecureSetCaCertificateChain(instance, reinterpret_cast<const uint8_t *>(OT_TCAT_TRUSTED_ROOT_CERTIFICATE),
119+
sizeof(OT_TCAT_TRUSTED_ROOT_CERTIFICATE));
120+
121+
otBleSecureSetSslAuthMode(instance, true);
122+
123+
// Validate BLE secure and Tcat start APIs
124+
VerifyOrQuit(otBleSecureTcatStart(instance, &vendorInfo, nullptr) == kErrorInvalidState);
125+
SuccessOrQuit(otBleSecureStart(instance, HandleBleSecureConnect, nullptr, true, &ble));
126+
VerifyOrQuit(otBleSecureStart(instance, HandleBleSecureConnect, nullptr, true, nullptr) == kErrorAlready);
127+
SuccessOrQuit(otBleSecureTcatStart(instance, &vendorInfo, nullptr));
128+
129+
// Validate connection callbacks when platform informs that peer has connected/disconnected
130+
otPlatBleGapOnConnected(instance, kConnectionId);
131+
VerifyOrQuit(!ble.IsConnected() && ble.IsBleConnectionOpen());
132+
otPlatBleGapOnDisconnected(instance, kConnectionId);
133+
VerifyOrQuit(!ble.IsConnected() && !ble.IsBleConnectionOpen());
134+
135+
// Validate connection callbacks when calling `otBleSecureDisconnect()`
136+
otPlatBleGapOnConnected(instance, kConnectionId);
137+
VerifyOrQuit(!ble.IsConnected() && ble.IsBleConnectionOpen());
138+
otBleSecureDisconnect(instance);
139+
VerifyOrQuit(!ble.IsConnected() && !ble.IsBleConnectionOpen());
140+
141+
// Validate TLS connection can be started only when peer is connected
142+
otPlatBleGapOnConnected(instance, kConnectionId);
143+
SuccessOrQuit(otBleSecureConnect(instance));
144+
otBleSecureDisconnect(instance);
145+
VerifyOrQuit(otBleSecureConnect(instance) == kErrorInvalidState);
146+
147+
// Validate Tcat state changes after stopping BLE secure
148+
VerifyOrQuit(otBleSecureIsTcatEnabled(instance));
149+
otBleSecureStop(instance);
150+
VerifyOrQuit(!otBleSecureIsTcatEnabled(instance));
151+
152+
testFreeInstance(instance);
153+
}
154+
155+
} // namespace ot
156+
157+
#endif
158+
159+
int main(void)
160+
{
161+
#if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
162+
ot::TestTcat();
163+
printf("All tests passed\n");
164+
#else
165+
printf("Tcat is not enabled\n");
166+
return -1;
167+
#endif
168+
return 0;
169+
}

0 commit comments

Comments
 (0)