43static std::vector<std::string>
get_file_list(
const std::string& dname) {
44 std::vector<std::string> res;
48 if( 0 == item.
basename().find(
"bd_") ) {
49 const jau::nsize_t suffix_pos = item.basename().size() - 4;
50 if( suffix_pos == item.basename().find(
".key", suffix_pos) ) {
51 receiver->push_back( item.path() );
60bool SMPKeyBin::remove_impl(
const std::string& fname) {
116 return smpKeyBin.
write( path, overwrite );
126 std::vector<SMPKeyBin> res;
128 for(
const std::string& fname : fnames) {
138 std::vector<SMPKeyBin> res;
139 std::vector<SMPKeyBin> all =
readAll(dname, verbose_);
141 if( localAddress == f.getLocalAddrAndType() ) {
149 std::string res =
"SMPKeyBin[local["+
to_string(localRole)+
", "+localAddress.
toString()+
"], remote "+remoteAddress.
toString()+
229 r.erase(it, r.end());
233 std::string r(
"bd_"+localAddress_.address.toString()+
"_"+remoteAddress_.address.toString()+
std::to_string(
number(remoteAddress_.type))+
".key");
235 r.erase(it, r.end());
239 return getFilename(path, remoteDevice.getAdapter().getAddressAndType(), remoteDevice.getAddressAndType());
249 jau::fprintf_td(stderr,
"Write SMPKeyBin: Invalid (skipped) %s\n", toString().c_str());
253 const std::string fname = getFilename(path);
255 if( fname_stat.
exists() ) {
256 if( fname_stat.
is_file() && overwrite ) {
257 if( !remove_impl(fname) ) {
258 jau::fprintf_td(stderr,
"Write SMPKeyBin: Failed deletion of existing file %s, %s\n", fname_stat.
to_string().c_str(), toString().c_str());
263 jau::fprintf_td(stderr,
"Write SMPKeyBin: Not overwriting existing %s, %s\n", fname_stat.
to_string().c_str(), toString().c_str());
268 std::ofstream file(fname, std::ios::out | std::ios::binary);
270 if ( !file.good() || !file.is_open() ) {
271 jau::fprintf_td(stderr,
"Write SMPKeyBin: Failed: File not open %s: %s\n", fname_stat.
to_string().c_str(), toString().c_str());
278 file.write((
char*)buffer,
sizeof(version));
281 file.write((
char*)buffer,
sizeof(size));
284 file.write((
char*)buffer,
sizeof(ts_creation_sec));
286 file.write((
char*)&localRole,
sizeof(localRole));
289 file.write((
char*)buffer,
sizeof(localAddress.address.b));
291 file.write((
char*)&localAddress.type,
sizeof(localAddress.type));
294 file.write((
char*)buffer,
sizeof(remoteAddress.address.b));
296 file.write((
char*)&remoteAddress.type,
sizeof(remoteAddress.type));
297 file.write((
char*)&sec_level,
sizeof(sec_level));
298 file.write((
char*)&io_cap,
sizeof(io_cap));
300 file.write((
char*)&keys_init,
sizeof(keys_init));
301 file.write((
char*)&keys_resp,
sizeof(keys_resp));
304 file.write((
char*)<k_init,
sizeof(ltk_init));
307 file.write((
char*)&irk_init,
sizeof(irk_init));
309 if( hasCSRKInit() ) {
310 file.write((
char*)&csrk_init,
sizeof(csrk_init));
313 file.write((
char*)&lk_init,
sizeof(lk_init));
317 file.write((
char*)<k_resp,
sizeof(ltk_resp));
320 file.write((
char*)&irk_resp,
sizeof(irk_resp));
322 if( hasCSRKResp() ) {
323 file.write((
char*)&csrk_resp,
sizeof(csrk_resp));
326 file.write((
char*)&lk_resp,
sizeof(lk_resp));
329 const bool res = file.good() && file.is_open();
332 jau::fprintf_td(stderr,
"Write SMPKeyBin: Success: %s: %s\n", fname.c_str(), toString().c_str());
335 jau::fprintf_td(stderr,
"Write SMPKeyBin: Failed: %s: %s\n", fname.c_str(), toString().c_str());
342 std::ifstream file(fname, std::ios::binary);
343 if ( !file.is_open() ) {
353 file.read((
char*)buffer,
sizeof(version));
358 file.read((
char*)buffer,
sizeof(size));
362 uint16_t remaining = size -
sizeof(version) -
sizeof(size);
364 if( !err && 8 <= remaining ) {
365 file.read((
char*)buffer,
sizeof(ts_creation_sec));
372 if( !err && 7+7+4 <= remaining ) {
373 file.read((
char*)&localRole,
sizeof(localRole));
375 file.read((
char*)buffer,
sizeof(localAddress.
address.
b));
378 file.read((
char*)&localAddress.
type,
sizeof(localAddress.
type));
380 file.read((
char*)buffer,
sizeof(remoteAddress.
address.
b));
383 file.read((
char*)&remoteAddress.
type,
sizeof(remoteAddress.
type));
384 file.read((
char*)&sec_level,
sizeof(sec_level));
385 file.read((
char*)&io_cap,
sizeof(io_cap));
387 file.read((
char*)&keys_init,
sizeof(keys_init));
388 file.read((
char*)&keys_resp,
sizeof(keys_resp));
397 if(
sizeof(ltk_init) <= remaining ) {
398 file.read((
char*)<k_init,
sizeof(ltk_init));
399 remaining -=
sizeof(ltk_init);
406 if(
sizeof(irk_init) <= remaining ) {
407 file.read((
char*)&irk_init,
sizeof(irk_init));
408 remaining -=
sizeof(irk_init);
415 if(
sizeof(csrk_init) <= remaining ) {
416 file.read((
char*)&csrk_init,
sizeof(csrk_init));
417 remaining -=
sizeof(csrk_init);
424 if(
sizeof(lk_init) <= remaining ) {
425 file.read((
char*)&lk_init,
sizeof(lk_init));
426 remaining -=
sizeof(lk_init);
434 if(
sizeof(ltk_resp) <= remaining ) {
435 file.read((
char*)<k_resp,
sizeof(ltk_resp));
436 remaining -=
sizeof(ltk_resp);
443 if(
sizeof(irk_resp) <= remaining ) {
444 file.read((
char*)&irk_resp,
sizeof(irk_resp));
445 remaining -=
sizeof(irk_resp);
452 if(
sizeof(csrk_resp) <= remaining ) {
453 file.read((
char*)&csrk_resp,
sizeof(csrk_resp));
454 remaining -=
sizeof(csrk_resp);
461 if(
sizeof(lk_resp) <= remaining ) {
462 file.read((
char*)&lk_resp,
sizeof(lk_resp));
463 remaining -=
sizeof(lk_resp);
476 remove_impl( fname );
478 jau::fprintf_td(stderr,
"Read SMPKeyBin: Failed %s (removed): %s, remaining %u\n",
479 fname.c_str(),
toString().c_str(), remaining);
485 fname.c_str(),
toString().c_str(), remaining);
static std::vector< std::string > get_file_list(const std::string &dname)
Unique Bluetooth EUI48 address and BDAddressType tuple.
void clearHash()
Method clears the cached hash value.
std::string toString() const noexcept
BTRole getRole() const noexcept
Return the current BTRole of this adapter.
BDAddressAndType const & getAddressAndType() const noexcept
Returns the adapter's public BDAddressAndType, i.e.
BTDevice represents one remote Bluetooth device.
SMPLongTermKey getLongTermKey(const bool responder) const noexcept
Returns a copy of the Long Term Key (LTK), valid after connection and SMP pairing has been completed.
SMPSignatureResolvingKey getSignatureResolvingKey(const bool responder) const noexcept
Returns a copy of the Signature Resolving Key (CSRK), valid after connection and SMP pairing has been...
constexpr BDAddressAndType const & getAddressAndType() const noexcept
Returns the devices' unique EUI48 address and type tuple, might be its initially reported (resolvable...
SMPPairingState getPairingState() const noexcept
Returns the current SMPPairingState.
SMPIdentityResolvingKey getIdentityResolvingKey(const bool responder) const noexcept
Returns a copy of the Identity Resolving Key (IRK), valid after connection and SMP pairing has been c...
PairingMode getPairingMode() const noexcept
Returns the current PairingMode used by the device.
BTSecurityLevel getConnSecurityLevel() const noexcept
Return the BTSecurityLevel, determined when the connection is established.
SMPKeyType getAvailableSMPKeys(const bool responder) const noexcept
Returns the available SMPKeyType mask for the responder (LL slave) or initiator (LL master).
BTAdapter & getAdapter() const
Returns the managing adapter.
SMPIOCapability getConnIOCapability() const noexcept
Return the set SMPIOCapability value, determined when the connection is established.
SMPLinkKey getLinkKey(const bool responder) const noexcept
Returns a copy of the Link Key (LK), valid after connection and SMP pairing has been completed.
std::string toString() const noexcept override
Storage for SMP keys including required connection parameter per local adapter and remote device.
constexpr bool hasLKInit() const noexcept
static std::string getFilename(const std::string &path, const BDAddressAndType &localAddress_, const BDAddressAndType &remoteAddress_) noexcept
void setLTKResp(const SMPLongTermKey &v) noexcept
static std::vector< SMPKeyBin > readAll(const std::string &dname, const bool verbose_)
constexpr bool hasLTKInit() const noexcept
void setLKResp(const SMPLinkKey &v) noexcept
static std::vector< SMPKeyBin > readAllForLocalAdapter(const BDAddressAndType &localAddress, const std::string &dname, const bool verbose_)
std::string toString() const noexcept
constexpr bool hasIRKInit() const noexcept
constexpr bool isValid() const noexcept
Returns true if.
void setVerbose(bool v) noexcept
static SMPKeyBin create(const BTDevice &device)
Create a new SMPKeyBin instance based upon given BTDevice's BTSecurityLevel, SMPPairingState,...
void setCSRKInit(const SMPSignatureResolvingKey &v) noexcept
static bool remove(const std::string &path, const BDAddressAndType &localAddress_, const BDAddressAndType &remoteAddress_)
void setIRKResp(const SMPIdentityResolvingKey &v) noexcept
void setIRKInit(const SMPIdentityResolvingKey &v) noexcept
constexpr bool uses_SC() const noexcept
Return whether Secure Connection (SC) is being used via LTK keys.
constexpr bool hasLKResp() const noexcept
constexpr bool hasLTKResp() const noexcept
void setCSRKResp(const SMPSignatureResolvingKey &v) noexcept
constexpr bool hasCSRKInit() const noexcept
constexpr bool isSizeValid() const noexcept
bool write(const std::string &path, const bool overwrite) const noexcept
void setLTKInit(const SMPLongTermKey &v) noexcept
std::string getFileBasename() const noexcept
Returns the base filename, see SMPKeyBin API doc for naming scheme.
static bool createAndWrite(const BTDevice &device, const std::string &path, const bool verbose_)
Create a new SMPKeyBin instance on the fly based upon given BTDevice's BTSecurityLevel,...
static SMPKeyBin read(const std::string &fname, const bool verbose_)
Create a new SMPKeyBin instance based upon stored file denoted by fname.
void setLKInit(const SMPLinkKey &v) noexcept
constexpr bool isVersionValid() const noexcept
constexpr bool hasIRKResp() const noexcept
constexpr bool hasCSRKResp() const noexcept
Representing a directory item split into dirname() and basename().
const std::string & basename() const noexcept
Return the basename, shall not be empty nor contain a dirname.
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
constexpr bool exists() const noexcept
Returns true if entity does not exist, exclusive bit.
constexpr bool is_file() const noexcept
Returns true if entity is a file, might be in combination with is_link().
std::string to_string() const noexcept
Returns a comprehensive string representation of this element.
Class template jau::function is a general-purpose static-polymorphic function wrapper.
constexpr uint16_t get_uint16(uint8_t const *buffer) noexcept
Returns a uint16_t value from the given byte address using packed_t to resolve a potential memory ali...
constexpr void put_uint64(uint8_t *buffer, const uint64_t &v) noexcept
See put_uint16() for reference.
constexpr void put_uint16(uint8_t *buffer, const uint16_t v) noexcept
Put the given uint16_t value into the given byte address using packed_t to resolve a potential memory...
constexpr uint64_t get_uint64(uint8_t const *buffer) noexcept
See get_uint16() for reference.
@ little
Identifier for little endian, equivalent to endian::little.
std::string to_string(const alphabet &v) noexcept
SMPPairingState
SMP Pairing Process state definition.
SMPKeyType
SMP Key Type for Distribution, indicates keys distributed in the Transport Specific Key Distribution ...
@ COMPLETED
Phase 3: Key & value distribution completed by responding (slave) device sending SMPIdentInfoMsg (#1)...
@ NONE
No pairing in process.
@ LINK_KEY
SMP on the LE transport: Indicate that the device would like to derive the Link Key from the LTK.
@ SIGN_KEY
Indicates that the device shall distribute CSRK using the Signing Information command.
@ ENC_KEY
LE legacy pairing: Indicates device shall distribute LTK using the Encryption Information command,...
@ ID_KEY
Indicates that the device shall distribute IRK using the Identity Information command followed by its...
std::string to_string(const DiscoveryPolicy v) noexcept
BTSecurityLevel
Bluetooth Security Level.
PairingMode
Bluetooth secure pairing mode.
constexpr bool is_set(const LE_Features mask, const LE_Features bit) noexcept
constexpr uint8_t number(const DiscoveryPolicy rhs) noexcept
@ NONE
No encryption and no authentication.
@ PRE_PAIRED
Reusing encryption keys from previous pairing.
@ NEGOTIATING
Pairing mode in negotiating, i.e.
@ NONE
No pairing mode, implying no secure connections, no encryption and no MITM protection.
bool get_dir_content(const std::string &path, const consume_dir_item &digest) noexcept
Returns a list of directory elements excluding .
bool remove(const std::string &path, const traverse_options topts=traverse_options::none) noexcept
Remove the given path.
jau::function< R(A...)> bind_capref(I *data_ptr, R(*func)(I *, A...)) noexcept
Bind given data by passing the captured reference (pointer) to the value and non-void function to an ...
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
int fprintf_td(const uint64_t elapsed_ms, FILE *stream, const char *format,...) noexcept
Convenient fprintf() invocation, prepending the given elapsed_ms timestamp.
std::string toString() const noexcept
std::string toString() const noexcept
std::string toString() const noexcept
std::string toString() const noexcept
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
std::string toString() const noexcept
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
std::string to_iso8601_string() const noexcept
Convenience string conversion interpreted since Unix Epoch in UTC to ISO 8601 YYYY-mm-ddTHH:MM:SS....