|
| 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