Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTSecurityRegistry.hpp
Go to the documentation of this file.
1 /*
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  * Copyright (c) 2020 ZAFENA AB
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef DBT_SEC_SETTINGS_HPP_
27 #define DBT_SEC_SETTINGS_HPP_
28 
29 #include <string>
30 #include <cstdio>
31 
32 #include <direct_bt/DirectBT.hpp>
33 
34 namespace direct_bt {
35 
36  /**
37  * Application toolkit providing BT security setup and its device association
38  * on a pattern matching basis, i.e. EUI48Sub or name-sub.
39  */
40  namespace BTSecurityRegistry {
41 
42  struct Entry {
43  static constexpr int NO_PASSKEY = -1;
44 
46  std::string nameSub;
47 
52 
53  Entry(const EUI48Sub& addrSub_)
54  : addrSub(addrSub_), nameSub() {}
55 
56  Entry(const std::string& nameSub_)
57  : addrSub(EUI48Sub::ALL_DEVICE), nameSub(nameSub_) {}
58 
59  constexpr bool isSecLevelOrIOCapSet() const noexcept {
61  }
62  constexpr const BTSecurityLevel& getSecLevel() const noexcept { return sec_level; }
63  constexpr const SMPIOCapability& getIOCap() const noexcept { return io_cap; }
64 
65  constexpr bool isSecurityAutoEnabled() const noexcept {
67  }
68  constexpr const SMPIOCapability& getSecurityAutoIOCap() const noexcept { return io_cap_auto; }
69 
70  constexpr int getPairingPasskey() const noexcept { return passkey; }
71 
72  constexpr bool getPairingNumericComparison() const noexcept { return true; }
73 
74  std::string toString() const noexcept {
75  const std::string id = addrSub == EUI48Sub::ALL_DEVICE ? "'"+nameSub+"'" : addrSub.toString();
76  return "BTSecurityDetail["+id+", lvl "+
78  ", io "+to_string(io_cap)+
79  ", auto-io "+to_string(io_cap_auto)+
80  ", passkey "+std::to_string(passkey)+"]";
81  }
82  };
83 
84  /**
85  * Function for user defined EUI48 address and name BTSecurityRegistry::Entry matching criteria and algorithm.
86  * <p>
87  * Return {@code true} if the given {@code address} or {@code name} matches
88  * with the BTSecurityRegistry::Entry.
89  * </p>
90  *
91  * @param address EUI48 address
92  * @param name optional name, maybe empty
93  * @param e Entry entry
94  */
95  typedef bool (*AddressNameEntryMatchFunc)(const EUI48& address, const std::string& name, const Entry& e);
96 
97  /**
98  * Function for user defined EUI48Sub addressSub and name BTSecurityRegistry::Entry matching criteria and algorithm.
99  * <p>
100  * Return {@code true} if the given {@code addressSub} or {@code name} matches
101  * with the BTSecurityRegistry::Entry.
102  * </p>
103  *
104  * @param addressSub EUI48Sub address
105  * @param name optional name, maybe empty
106  * @param e Entry entry
107  */
108  typedef bool (*AddressSubNameEntryMatchFunc)(const EUI48Sub& addressSub, const std::string& name, const Entry& e);
109 
110  /**
111  * Function for user defined std::string name BTSecurityRegistry::Entry matching criteria and algorithm.
112  * <p>
113  * Return {@code true} if the given {@code name} matches
114  * with the BTSecurityRegistry::Entry.
115  * </p>
116  *
117  * @param name
118  * @param e Entry entry
119  */
120  typedef bool (*NameEntryMatchFunc)(const std::string& name, const Entry& e);
121 
122  /**
123  * Returns a matching BTSecurityRegistry::Entry with the given {@code addr} and/or {@code name}.
124  * <p>
125  * Matching criteria and algorithm is defined by the given AddressNameEntryMatchFunc.
126  * </p>
127  */
128  Entry* get(const EUI48& addr, const std::string& name, AddressNameEntryMatchFunc m) noexcept;
129 
130  /**
131  * Returns a matching BTSecurityRegistry::Entry with the given {@code addrSub} and/or {@code name}.
132  * <p>
133  * Matching criteria and algorithm is defined by the given AddressSubNameEntryMatchFunc.
134  * </p>
135  */
136  Entry* get(const EUI48Sub& addrSub, const std::string& name, AddressSubNameEntryMatchFunc m) noexcept;
137 
138  /**
139  * Returns a matching BTSecurityRegistry::Entry with the given {@code name}.
140  * <p>
141  * Matching criteria and algorithm is defined by the given NameEntryMatchFunc.
142  * </p>
143  */
144  Entry* get(const std::string& name, NameEntryMatchFunc m) noexcept;
145 
146  /**
147  * Returns a matching Entry,
148  * - which Entry::addrSub is set and the given {@code addr} starts with Entry::addrSub, or
149  * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
150  *
151  * Otherwise {@code null} is returned.
152  */
153  inline Entry* getStartOf(const EUI48& addr, const std::string& name) noexcept {
154  return get(addr, name, [](const EUI48& a, const std::string& n, const Entry& e)->bool {
155  return ( e.addrSub.length > 0 && 0 == a.indexOf(e.addrSub) ) ||
156  ( e.nameSub.length() > 0 && 0 == n.find(e.nameSub) );
157  });
158  }
159  /**
160  * Returns a matching Entry,
161  * - which Entry::addrSub is set and the given {@code addrSub} starts with Entry::addrSub, or
162  * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
163  *
164  * Otherwise {@code null} is returned.
165  */
166  inline Entry* getStartOf(const EUI48Sub& addrSub, const std::string& name) noexcept {
167  return get(addrSub, name, [](const EUI48Sub& as, const std::string& n, const Entry& e)->bool {
168  return ( e.addrSub.length > 0 && 0 == as.indexOf(e.addrSub) ) ||
169  ( e.nameSub.length() > 0 && 0 == n.find(e.nameSub) );
170  });
171  }
172  /**
173  * Returns a matching Entry,
174  * which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
175  *
176  * Otherwise {@code null} is returned.
177  */
178  inline Entry* getStartOf(const std::string& name) noexcept {
179  return get(name, [](const std::string& n, const Entry& e)->bool {
180  return e.nameSub.length() > 0 && 0 == n.find(e.nameSub);
181  });
182  }
183 
184  /**
185  * Returns a matching Entry,
186  * - which Entry::addrSub is set and the given {@code addrSub} starts with Entry::addrSub, or
187  * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
188  *
189  * Otherwise {@code null} is returned.
190  */
191  inline Entry* getEqual(const EUI48Sub& addrSub, const std::string& name) noexcept {
192  return get(addrSub, name, [](const EUI48Sub& as, const std::string& n, const Entry& e)->bool {
193  return ( e.addrSub.length > 0 && as == e.addrSub ) ||
194  ( e.nameSub.length() > 0 && n == e.nameSub );
195  });
196  }
197  /**
198  * Returns a matching Entry,
199  * which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
200  *
201  * Otherwise {@code null} is returned.
202  */
203  inline Entry* getEqual(const std::string& name) noexcept {
204  return get(name, [](const std::string& n, const Entry& e)->bool {
205  return e.nameSub.length() > 0 && n == e.nameSub;
206  });
207  }
208 
209  /**
210  * Returns the reference of the current list of Entry, not a copy.
211  */
212  jau::darray<Entry>& getEntries() noexcept;
213 
214  /**
215  * Determines whether the given {@code addrOrNameSub} is a EUI48Sub or just a {@code name}
216  * and retrieves an entry. If no entry exists, creates a new entry.
217  * <p>
218  * Implementation uses getEqual() to find a pre-existing entry.
219  * </p>
220  * @param addrOrNameSub either a EUI48Sub or just a name
221  * @return new or existing instance
222  */
223  Entry* getOrCreate(const std::string& addrOrNameSub) noexcept;
224 
225  /**
226  * Clears internal list
227  */
228  void clear() noexcept;
229 
230  std::string allToString() noexcept;
231 
232  } // namespace BTSecurityRegistry
233 
234 } // namespace direct_bt
235 
236 #endif /* DBT_SEC_SETTINGS_HPP_ */
direct_bt::SMPIOCapability::UNSET
@ UNSET
Denoting unset value, i.e.
DirectBT.hpp
direct_bt::BTSecurityRegistry::getOrCreate
Entry * getOrCreate(const std::string &addrOrNameSub) noexcept
Determines whether the given.
Definition: BTSecurityRegistry.cpp:71
direct_bt::EUI48::indexOf
jau::snsize_t indexOf(const EUI48Sub &needle) const noexcept
Definition: BTAddress.hpp:388
direct_bt::BTSecurityRegistry::Entry::isSecLevelOrIOCapSet
constexpr bool isSecLevelOrIOCapSet() const noexcept
Definition: BTSecurityRegistry.hpp:59
direct_bt::BTSecurityRegistry::allToString
std::string allToString() noexcept
Definition: BTSecurityRegistry.cpp:93
direct_bt::BTSecurityRegistry::NameEntryMatchFunc
bool(* NameEntryMatchFunc)(const std::string &name, const Entry &e)
Function for user defined std::string name BTSecurityRegistry::Entry matching criteria and algorithm.
Definition: BTSecurityRegistry.hpp:120
direct_bt::BTSecurityRegistry::getStartOf
Entry * getStartOf(const EUI48 &addr, const std::string &name) noexcept
Returns a matching Entry,.
Definition: BTSecurityRegistry.hpp:153
direct_bt::BTSecurityRegistry::Entry::io_cap_auto
SMPIOCapability io_cap_auto
Definition: BTSecurityRegistry.hpp:50
direct_bt::EUI48Sub::toString
std::string toString() const noexcept
Returns the EUI48 sub-string representation, less or equal 17 characters representing less or equal 6...
Definition: BTTypes0.cpp:166
direct_bt::BTSecurityRegistry::Entry::getPairingPasskey
constexpr int getPairingPasskey() const noexcept
Definition: BTSecurityRegistry.hpp:70
direct_bt::BTSecurityRegistry::Entry
Definition: BTSecurityRegistry.hpp:42
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::BTSecurityRegistry::Entry::toString
std::string toString() const noexcept
Definition: BTSecurityRegistry.hpp:74
jau::to_string
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
Definition: byte_util.hpp:198
direct_bt::EUI48Sub
A 48 bit EUI-48 sub-identifier, see EUI48.
Definition: BTAddress.hpp:154
direct_bt::BTSecurityLevel
BTSecurityLevel
Bluetooth Security Level.
Definition: BTTypes0.hpp:211
direct_bt::BTSecurityRegistry::Entry::getIOCap
constexpr const SMPIOCapability & getIOCap() const noexcept
Definition: BTSecurityRegistry.hpp:63
direct_bt::EUI48Sub::indexOf
static jau::snsize_t indexOf(const uint8_t haystack_b[], const jau::nsize_t haystack_length, const uint8_t needle_b[], const jau::nsize_t needle_length) noexcept
Find index of needle within haystack.
Definition: BTTypes0.cpp:242
direct_bt::BTSecurityRegistry::AddressNameEntryMatchFunc
bool(* AddressNameEntryMatchFunc)(const EUI48 &address, const std::string &name, const Entry &e)
Function for user defined EUI48 address and name BTSecurityRegistry::Entry matching criteria and algo...
Definition: BTSecurityRegistry.hpp:95
direct_bt::EUI48Sub::ALL_DEVICE
static const EUI48Sub ALL_DEVICE
EUI48 MAC address matching all device, i.e.
Definition: BTAddress.hpp:158
jau::darray
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:102
direct_bt::BTSecurityRegistry::Entry::getPairingNumericComparison
constexpr bool getPairingNumericComparison() const noexcept
Definition: BTSecurityRegistry.hpp:72
direct_bt::BTSecurityRegistry::Entry::Entry
Entry(const std::string &nameSub_)
Definition: BTSecurityRegistry.hpp:56
direct_bt::EUI48Sub::length
jau::nsize_t length
The actual length in bytes of the EUI48 sub-address, less or equal 6 bytes.
Definition: BTAddress.hpp:170
direct_bt::BTSecurityRegistry::getEntries
jau::darray< Entry > & getEntries() noexcept
Returns the reference of the current list of Entry, not a copy.
Definition: BTSecurityRegistry.cpp:67
direct_bt::BTSecurityRegistry::Entry::io_cap
SMPIOCapability io_cap
Definition: BTSecurityRegistry.hpp:49
direct_bt::BTSecurityRegistry::getEqual
Entry * getEqual(const EUI48Sub &addrSub, const std::string &name) noexcept
Returns a matching Entry,.
Definition: BTSecurityRegistry.hpp:191
direct_bt::BTSecurityRegistry::Entry::addrSub
EUI48Sub addrSub
Definition: BTSecurityRegistry.hpp:45
direct_bt::BTSecurityRegistry::get
Entry * get(const EUI48 &addr, const std::string &name, AddressNameEntryMatchFunc m) noexcept
Returns a matching BTSecurityRegistry::Entry with the given.
Definition: BTSecurityRegistry.cpp:36
direct_bt::BTSecurityLevel::UNSET
@ UNSET
Security Level not set, value 0.
direct_bt::BTSecurityRegistry::AddressSubNameEntryMatchFunc
bool(* AddressSubNameEntryMatchFunc)(const EUI48Sub &addressSub, const std::string &name, const Entry &e)
Function for user defined EUI48Sub addressSub and name BTSecurityRegistry::Entry matching criteria an...
Definition: BTSecurityRegistry.hpp:108
direct_bt::BTSecurityRegistry::Entry::nameSub
std::string nameSub
Definition: BTSecurityRegistry.hpp:46
direct_bt::to_string
std::string to_string(const BDAddressType type) noexcept
Definition: BTTypes0.cpp:129
direct_bt::BTSecurityRegistry::Entry::getSecurityAutoIOCap
constexpr const SMPIOCapability & getSecurityAutoIOCap() const noexcept
Definition: BTSecurityRegistry.hpp:68
direct_bt::EUI48
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: BTAddress.hpp:388
direct_bt::SMPIOCapability
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
Definition: SMPTypes.hpp:185
direct_bt::BTSecurityRegistry::Entry::sec_level
BTSecurityLevel sec_level
Definition: BTSecurityRegistry.hpp:48
direct_bt::BTSecurityRegistry::Entry::NO_PASSKEY
static constexpr int NO_PASSKEY
Definition: BTSecurityRegistry.hpp:43
direct_bt::BTSecurityRegistry::Entry::Entry
Entry(const EUI48Sub &addrSub_)
Definition: BTSecurityRegistry.hpp:53
direct_bt::BTSecurityRegistry::Entry::passkey
int passkey
Definition: BTSecurityRegistry.hpp:51
direct_bt::BTSecurityRegistry::Entry::getSecLevel
constexpr const BTSecurityLevel & getSecLevel() const noexcept
Definition: BTSecurityRegistry.hpp:62
direct_bt::BTSecurityRegistry::clear
void clear() noexcept
Clears internal list.
Definition: BTSecurityRegistry.cpp:90
direct_bt::BTSecurityRegistry::Entry::isSecurityAutoEnabled
constexpr bool isSecurityAutoEnabled() const noexcept
Definition: BTSecurityRegistry.hpp:65