Skip to content

Commit e70dbe4

Browse files
WIP
1 parent 0e8f04f commit e70dbe4

File tree

4 files changed

+210
-15
lines changed

4 files changed

+210
-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/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

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
30+
#include "openthread-core-config.h"
31+
32+
#include "test_platform.h"
33+
#include "test_util.h"
34+
35+
36+
#if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
37+
38+
#include <openthread/ble_secure.h>
39+
40+
#define OT_CLI_TCAT_X509_CERT \
41+
"-----BEGIN CERTIFICATE-----\r\n" \
42+
"MIIBmDCCAT+gAwIBAgIEAQIDBDAKBggqhkjOPQQDAjBvMQswCQYDVQQGEwJYWDEQ\r\n" \
43+
"MA4GA1UECBMHTXlTdGF0ZTEPMA0GA1UEBxMGTXlDaXR5MQ8wDQYDVQQLEwZNeVVu\r\n" \
44+
"aXQxETAPBgNVBAoTCE15VmVuZG9yMRkwFwYDVQQDExB3d3cubXl2ZW5kb3IuY29t\r\n" \
45+
"MB4XDTIzMTAxNjEwMzk1NFoXDTI0MTAxNjEwMzk1NFowIjEgMB4GA1UEAxMXbXl2\r\n" \
46+
"ZW5kb3IuY29tL3RjYXQvbXlkZXYwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQB\r\n" \
47+
"aWwFDNj1bpQIdN+Kp2cHWw55U/+fa+OmZnoy1B4BOT+822jdwPBuyXWAQoBdYdQJ\r\n" \
48+
"ff4RgmhczyV4PhArPIuAoxYwFDASBgkrBgEEAYLfKgMEBQABAQEBMAoGCCqGSM49\r\n" \
49+
"BAMCA0cAMEQCIBEHxiEDij26y6V77Q311Gj4CZAuZuPGXZpnzL2BLk7bAiAlFk6G\r\n" \
50+
"mYGzkcrYyssFI9HlPgrisWoMmgummaTtCuvrEw==\r\n" \
51+
"-----END CERTIFICATE-----\r\n"
52+
53+
#define OT_CLI_TCAT_PRIV_KEY \
54+
"-----BEGIN EC PRIVATE KEY-----\r\n" \
55+
"MHcCAQEEIDeJ6lVQKiOIBxKwTZp6TkU5QVHt9pvXOR9CGpPBI3DhoAoGCCqGSM49\r\n" \
56+
"AwEHoUQDQgAEAWlsBQzY9W6UCHTfiqdnB1sOeVP/n2vjpmZ6MtQeATk/vNto3cDw\r\n" \
57+
"bsl1gEKAXWHUCX3+EYJoXM8leD4QKzyLgA==\r\n" \
58+
"-----END EC PRIVATE KEY-----\r\n"
59+
60+
#define OT_CLI_TCAT_TRUSTED_ROOT_CERTIFICATE \
61+
"-----BEGIN CERTIFICATE-----\r\n" \
62+
"MIICCDCCAa2gAwIBAgIJAIKxygBXoH+5MAoGCCqGSM49BAMCMG8xCzAJBgNVBAYT\r\n" \
63+
"AlhYMRAwDgYDVQQIEwdNeVN0YXRlMQ8wDQYDVQQHEwZNeUNpdHkxDzANBgNVBAsT\r\n" \
64+
"Bk15VW5pdDERMA8GA1UEChMITXlWZW5kb3IxGTAXBgNVBAMTEHd3dy5teXZlbmRv\r\n" \
65+
"ci5jb20wHhcNMjMxMDE2MTAzMzE1WhcNMjYxMDE2MTAzMzE1WjBvMQswCQYDVQQG\r\n" \
66+
"EwJYWDEQMA4GA1UECBMHTXlTdGF0ZTEPMA0GA1UEBxMGTXlDaXR5MQ8wDQYDVQQL\r\n" \
67+
"EwZNeVVuaXQxETAPBgNVBAoTCE15VmVuZG9yMRkwFwYDVQQDExB3d3cubXl2ZW5k\r\n" \
68+
"b3IuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWdyzPAXGKeZY94OhHAWX\r\n" \
69+
"HzJfQIjGSyaOzlgL9OEFw2SoUDncLKPGwfPAUSfuMyEkzszNDM0HHkBsDLqu4n25\r\n" \
70+
"/6MyMDAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQU4EynoSw9eDKZEVPkums2\r\n" \
71+
"IWLAJCowCgYIKoZIzj0EAwIDSQAwRgIhAMYGGL9xShyE6P9wEU+MAYF6W3CzdrwV\r\n" \
72+
"kuerX1encIH2AiEA5rq490NUobM1Au43roxJq1T6Z43LscPVbGZfULD1Jq0=\r\n" \
73+
"-----END CERTIFICATE-----\r\n"
74+
75+
namespace ot {
76+
77+
class TestBleSecure
78+
{
79+
public:
80+
static void HandleBleSecureConnect(otInstance *aInstance, bool aConnected, bool aBleConnectionOpen, void *aContext)
81+
{
82+
OT_UNUSED_VARIABLE(aInstance);
83+
84+
static_cast<TestBleSecure *>(aContext)->HandleBleSecureConnect(aConnected, aBleConnectionOpen);
85+
}
86+
87+
void HandleBleSecureConnect(bool aConnected, bool BleConnectionOpen)
88+
{
89+
mIsConnected = aConnected;
90+
mIsBleConnectionOpen = BleConnectionOpen;
91+
}
92+
93+
bool IsConnected(void) const { return mIsConnected; }
94+
bool IsBleConnectionOpen(void) const { return mIsBleConnectionOpen; }
95+
96+
private:
97+
bool mIsConnected = false;
98+
bool mIsBleConnectionOpen = false;
99+
};
100+
101+
void TestTcat(void)
102+
{
103+
Instance *instance = testInitInstance();
104+
uint16_t connectionId = 0;
105+
TestBleSecure ble;
106+
107+
static const char sPskdVendor[] = "J01NM3";
108+
static const char sUrl[] = "dummy_url";
109+
110+
otTcatVendorInfo mVendorInfo = {.mProvisioningUrl = sUrl, .mPskdString = sPskdVendor};
111+
112+
otBleSecureSetCertificate(instance, reinterpret_cast<const uint8_t *>(OT_CLI_TCAT_X509_CERT),
113+
sizeof(OT_CLI_TCAT_X509_CERT), reinterpret_cast<const uint8_t *>(OT_CLI_TCAT_PRIV_KEY),
114+
sizeof(OT_CLI_TCAT_PRIV_KEY));
115+
116+
otBleSecureSetCaCertificateChain(instance, reinterpret_cast<const uint8_t *>(OT_CLI_TCAT_TRUSTED_ROOT_CERTIFICATE),
117+
sizeof(OT_CLI_TCAT_TRUSTED_ROOT_CERTIFICATE));
118+
119+
otBleSecureSetSslAuthMode(instance, true);
120+
121+
// Validate BLE secure and Tcat start APIs
122+
VerifyOrQuit(otBleSecureTcatStart(instance, &mVendorInfo, nullptr) == kErrorInvalidState);
123+
SuccessOrQuit(otBleSecureStart(instance, ble.HandleBleSecureConnect, nullptr, true, &ble));
124+
VerifyOrQuit(otBleSecureStart(instance, ble.HandleBleSecureConnect, nullptr, true, nullptr) == kErrorAlready);
125+
SuccessOrQuit(otBleSecureTcatStart(instance, &mVendorInfo, nullptr));
126+
127+
// Validate connection callbacks when platform informs that peer has connected/disconnected
128+
otPlatBleGapOnConnected(instance, connectionId);
129+
VerifyOrQuit(!ble.IsConnected() && ble.IsBleConnectionOpen());
130+
otPlatBleGapOnDisconnected(instance, connectionId);
131+
VerifyOrQuit(!ble.IsConnected() && !ble.IsBleConnectionOpen());
132+
133+
// Validate connection callbacks when calling BLE secure disconnect
134+
otPlatBleGapOnConnected(instance, connectionId);
135+
VerifyOrQuit(!ble.IsConnected() && ble.IsBleConnectionOpen());
136+
otBleSecureDisconnect(instance);
137+
VerifyOrQuit(!ble.IsConnected() && !ble.IsBleConnectionOpen());
138+
139+
// Validate TLS connection can be started only when peer is connected
140+
otPlatBleGapOnConnected(instance, connectionId);
141+
SuccessOrQuit(otBleSecureConnect(instance));
142+
otBleSecureDisconnect(instance);
143+
VerifyOrQuit(otBleSecureConnect(instance) == kErrorInvalidState);
144+
145+
// Validate Tcat state changes after stopping BLE secure
146+
VerifyOrQuit(otBleSecureIsTcatEnabled(instance));
147+
otBleSecureStop(instance);
148+
VerifyOrQuit(!otBleSecureIsTcatEnabled(instance));
149+
150+
testFreeInstance(instance);
151+
}
152+
} // namespace ot
153+
154+
#endif
155+
156+
int main(void)
157+
{
158+
#if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
159+
ot::TestTcat();
160+
printf("All tests passed\n");
161+
#else
162+
printf("Tcat is not enabled\n");
163+
return -1;
164+
#endif
165+
return 0;
166+
}

0 commit comments

Comments
 (0)