diff options
author | Sven Gothel <[email protected]> | 2021-02-02 07:19:15 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-02-02 07:19:15 +0100 |
commit | 32a8e1a7622f4cede05c7038d3e7a4213b546a35 (patch) | |
tree | 0669e80d6bf2ecb220319ad0be45e2bc1fb1cb90 /src/direct_bt | |
parent | 26091fd771661bb8dcf4364f9a088590d98454fc (diff) |
Promote persistent SMP Key Storage to API: SMPKeyBinv2.2.3
SMPKeyBin stores device's BDAddressAndType, its security connection setup BTSecurityLevel + SMPIOCapability
and optionally the initiator and responder LTK and CSRK within one file.
Since the LTK and CSRK can be optionally set due to their availability per initiator and responder,
implementation supports mixed mode for certain devices,
e.g. LTK responder key only etc.
This was the final motivation to promote the storage demo code to the API.
Diffstat (limited to 'src/direct_bt')
-rw-r--r-- | src/direct_bt/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/direct_bt/SMPKeyBin.cpp | 200 |
2 files changed, 201 insertions, 0 deletions
diff --git a/src/direct_bt/CMakeLists.txt b/src/direct_bt/CMakeLists.txt index 89ff0588..dea50003 100644 --- a/src/direct_bt/CMakeLists.txt +++ b/src/direct_bt/CMakeLists.txt @@ -34,6 +34,7 @@ set (direct_bt_LIB_SRCS ${PROJECT_SOURCE_DIR}/src/direct_bt/MgmtTypes.cpp ${PROJECT_SOURCE_DIR}/src/direct_bt/SMPHandler.cpp ${PROJECT_SOURCE_DIR}/src/direct_bt/SMPTypes.cpp + ${PROJECT_SOURCE_DIR}/src/direct_bt/SMPKeyBin.cpp ${PROJECT_SOURCE_DIR}/src/direct_bt/UUID.cpp # autogenerated files ${CMAKE_CURRENT_BINARY_DIR}/../version.c diff --git a/src/direct_bt/SMPKeyBin.cpp b/src/direct_bt/SMPKeyBin.cpp new file mode 100644 index 00000000..a726e118 --- /dev/null +++ b/src/direct_bt/SMPKeyBin.cpp @@ -0,0 +1,200 @@ +/* + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2021 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <cstring> +#include <string> +#include <memory> +#include <cstdint> +#include <cstdio> + +#include "SMPKeyBin.hpp" + +using namespace direct_bt; + +std::string SMPKeyBin::toString() const noexcept { + std::string res = "SMPKeyBin["+addrAndType.toString()+", sec "+getBTSecurityLevelString(sec_level)+ + ", io "+getSMPIOCapabilityString(io_cap)+ + ", "; + if( isVersionValid() ) { + res += "Init["; + if( hasLTKInit() ) { + res += ltk_init.toString(); + } + if( hasCSRKInit() ) { + if( hasLTKInit() ) { + res += ", "; + } + res += csrk_init.toString(); + } + res += "], Resp["; + if( hasLTKResp() ) { + res += ltk_resp.toString(); + } + if( hasCSRKResp() ) { + if( hasLTKResp() ) { + res += ", "; + } + res += csrk_resp.toString(); + } + res += "], "; + } + res += "ver["+jau::uint16HexString(version)+", ok "+std::to_string( isVersionValid() )+ + "], size["+std::to_string(size); + if( verbose ) { + res += ", calc "+std::to_string( calcSize() ); + } + res += ", valid "+std::to_string( isSizeValid() )+ + "], valid "+std::to_string( isValid() )+"]"; + return res; +} + +bool SMPKeyBin::write(const std::string path, const std::string basename) const noexcept { + if( !isValid() ) { + if( verbose ) { + fprintf(stderr, "****** WRITE SMPKeyBin: Invalid (skipped) %s\n", toString().c_str()); + } + return false; + } + const std::string fname = path+"/"+basename; + std::ofstream file(fname, std::ios::binary | std::ios::trunc); + uint8_t buffer[2]; + + jau::put_uint16(buffer, 0, version, true /* littleEndian */); + file.write((char*)buffer, sizeof(version)); + + jau::put_uint16(buffer, 0, size, true /* littleEndian */); + file.write((char*)buffer, sizeof(size)); + + file.write((char*)&addrAndType.address, sizeof(addrAndType.address)); + file.write((char*)&addrAndType.type, sizeof(addrAndType.type)); + file.write((char*)&sec_level, sizeof(sec_level)); + file.write((char*)&io_cap, sizeof(io_cap)); + + file.write((char*)&keys_init, sizeof(keys_init)); + file.write((char*)&keys_resp, sizeof(keys_resp)); + + if( hasLTKInit() ) { + file.write((char*)<k_init, sizeof(ltk_init)); + } + if( hasCSRKInit() ) { + file.write((char*)&csrk_init, sizeof(csrk_init)); + } + + if( hasLTKResp() ) { + file.write((char*)<k_resp, sizeof(ltk_resp)); + } + if( hasCSRKResp() ) { + file.write((char*)&csrk_resp, sizeof(csrk_resp)); + } + + file.close(); + if( verbose ) { + fprintf(stderr, "****** WRITE SMPKeyBin: %s: %s\n", fname.c_str(), toString().c_str()); + } + return true; +} + +bool SMPKeyBin::read(const std::string path, const std::string basename) { + const std::string fname = path+"/"+basename; + std::ifstream file(fname, std::ios::binary); + if (!file.is_open() ) { + if( verbose ) { + fprintf(stderr, "****** READ SMPKeyBin failed: %s\n", fname.c_str()); + } + return false; + } + bool err = false; + uint8_t buffer[2]; + + file.read((char*)buffer, sizeof(version)); + version = jau::get_uint16(buffer, 0, true /* littleEndian */); + err = file.fail(); + + if( !err ) { + file.read((char*)buffer, sizeof(size)); + size = jau::get_uint16(buffer, 0, true /* littleEndian */); + err = file.fail(); + } + uint16_t remaining = size - sizeof(version) - sizeof(size); + + if( !err && 11 <= remaining ) { + file.read((char*)&addrAndType.address, sizeof(addrAndType.address)); + file.read((char*)&addrAndType.type, sizeof(addrAndType.type)); + file.read((char*)&sec_level, sizeof(sec_level)); + file.read((char*)&io_cap, sizeof(io_cap)); + + file.read((char*)&keys_init, sizeof(keys_init)); + file.read((char*)&keys_resp, sizeof(keys_resp)); + remaining -= 11; + err = file.fail(); + } else { + err = true; + } + addrAndType.clearHash(); + + if( !err && hasLTKInit() ) { + if( sizeof(ltk_init) <= remaining ) { + file.read((char*)<k_init, sizeof(ltk_init)); + remaining -= sizeof(ltk_init); + err = file.fail(); + } else { + err = true; + } + } + if( !err && hasCSRKInit() ) { + if( sizeof(csrk_init) <= remaining ) { + file.read((char*)&csrk_init, sizeof(csrk_init)); + remaining -= sizeof(csrk_init); + err = file.fail(); + } else { + err = true; + } + } + + if( !err && hasLTKResp() ) { + if( sizeof(ltk_resp) <= remaining ) { + file.read((char*)<k_resp, sizeof(ltk_resp)); + remaining -= sizeof(ltk_resp); + err = file.fail(); + } else { + err = true; + } + } + if( !err && hasCSRKResp() ) { + if( sizeof(csrk_resp) <= remaining ) { + file.read((char*)&csrk_resp, sizeof(csrk_resp)); + remaining -= sizeof(csrk_resp); + err = file.fail(); + } else { + err = true; + } + } + + file.close(); + if( verbose ) { + fprintf(stderr, "****** READ SMPKeyBin: %s: %s, remaining %u\n", + fname.c_str(), toString().c_str(), remaining); + } + return isValid(); +} |