Skip to content

Commit 3f7a7a4

Browse files
author
Donatien Garnier
committed
Merge
2 parents 7ef7ef5 + b43f27a commit 3f7a7a4

File tree

155 files changed

+6122
-22304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+6122
-22304
lines changed

features/FEATURE_BLE/ble/BLETypes.h

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ struct link_encryption_t : SafeEnum<link_encryption_t, uint8_t> {
129129
NOT_ENCRYPTED, /**< The link is not secured. */
130130
ENCRYPTION_IN_PROGRESS, /**< Link security is being established. */
131131
ENCRYPTED, /**< The link is secure. */
132-
ENCRYPTED_WITH_MITM /**< The link is secure and authenticated. */
132+
ENCRYPTED_WITH_MITM, /**< The link is secure and authenticated. */
133+
ENCRYPTED_WITH_SC_AND_MITM /**< The link is secure and authenticated with a secure connection key. */
133134
};
134135

135136
/**
@@ -460,6 +461,74 @@ struct random_address_type_t : SafeEnum<random_address_type_t, uint8_t> {
460461
SafeEnum<random_address_type_t, uint8_t>(value) { }
461462
};
462463

464+
/**
465+
* Security requirement that can be attached to an attribute operation.
466+
*/
467+
struct att_security_requirement_t : SafeEnum<att_security_requirement_t, uint8_t> {
468+
/**
469+
* Number of bits required to store the value.
470+
*
471+
* This value can be used to define a bitfield that host a value of this
472+
* enum.
473+
*/
474+
static const uint8_t size = 2;
475+
476+
/** struct scoped enum wrapped by the class */
477+
enum type {
478+
/**
479+
* The operation does not have security requirements.
480+
*
481+
* It is equivalent to: SecurityMode 1 level 1: No authentication, no
482+
* encryption and no signing required.
483+
*
484+
* @note This security mode is not applicable for signed operation.
485+
*
486+
* @note Equivalent to SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK.
487+
*/
488+
NONE,
489+
490+
/**
491+
* The operation requires security and there's no requirement towards
492+
* peer authentication.
493+
*
494+
* @note Security can be achieved either by signing messages or
495+
* encrypting the link.
496+
*
497+
* @note Signing is only applicable for signed write operations.
498+
*
499+
* @note Equivalent to SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM
500+
* or SecurityManager::SECURITY_MODE_SIGNED_NO_MITM.
501+
*/
502+
UNAUTHENTICATED,
503+
504+
/**
505+
* The operation requires security and the peer must be authenticated.
506+
*
507+
* @note Security can be achieved either by signing messages or
508+
* encrypting the link.
509+
*
510+
* @note Equivalent to SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM
511+
* or SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM.
512+
*/
513+
AUTHENTICATED,
514+
515+
/**
516+
* The operation require encryption with an authenticated peer that
517+
* paired using secure connection pairing.
518+
*
519+
* @note This security mode is not applicable for signed operation;
520+
* security is achieved with link encryption.
521+
*/
522+
SC_AUTHENTICATED
523+
};
524+
525+
/**
526+
* Construct a new instance of att_security_requirement_t.
527+
*/
528+
att_security_requirement_t(type value) :
529+
SafeEnum<att_security_requirement_t, uint8_t>(value) { }
530+
};
531+
463532
} // namespace ble
464533

465534
/**

features/FEATURE_BLE/ble/GattAttribute.h

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class GattAttribute {
7272
static const Handle_t INVALID_HANDLE = 0x0000;
7373

7474
public:
75+
76+
typedef ble::att_security_requirement_t Security_t;
77+
7578
/**
7679
* Construct an attribute.
7780
*
@@ -102,6 +105,9 @@ class GattAttribute {
102105
* true // variable length
103106
* );
104107
* @endcode
108+
*
109+
* @note By default, read and write operations are allowed and does not
110+
* require any security.
105111
*/
106112
GattAttribute(
107113
const UUID &uuid,
@@ -113,8 +119,12 @@ class GattAttribute {
113119
_valuePtr(valuePtr),
114120
_lenMax(maxLen),
115121
_len(len),
122+
_handle(),
116123
_hasVariableLen(hasVariableLen),
117-
_handle() {
124+
_read_allowed(true),
125+
_read_security(Security_t::NONE),
126+
_write_allowed(true),
127+
_write_security(Security_t::NONE) {
118128
}
119129

120130
public:
@@ -209,6 +219,78 @@ class GattAttribute {
209219
return _hasVariableLen;
210220
}
211221

222+
/**
223+
* Allow or disallow read operation from a client.
224+
* @param allow_read Read is allowed if true.
225+
*/
226+
void allowRead(bool allow_read)
227+
{
228+
_read_allowed = allow_read;
229+
}
230+
231+
/**
232+
* Indicate if a client is allowed to read the attribute.
233+
* @return true if a client is allowed to read the attribute.
234+
*/
235+
bool isReadAllowed(void) const
236+
{
237+
return _read_allowed;
238+
}
239+
240+
/**
241+
* Set the security requirements of the read operations.
242+
* @param requirement The security level required by the read operations.
243+
*/
244+
void setReadSecurityRequirement(Security_t requirement)
245+
{
246+
_read_security = requirement.value();
247+
}
248+
249+
/**
250+
* Return the security level required by read operations.
251+
* @return The security level of the read operations.
252+
*/
253+
Security_t getReadSecurityRequirement() const
254+
{
255+
return static_cast<Security_t::type>(_read_security);
256+
}
257+
258+
/**
259+
* Allow or disallow write operation from a client.
260+
* @param allow_write Write is allowed if true.
261+
*/
262+
void allowWrite(bool allow_write)
263+
{
264+
_write_allowed = allow_write;
265+
}
266+
267+
/**
268+
* Indicate if a client is allowed to write the attribute.
269+
* @return true if a client is allowed to write the attribute.
270+
*/
271+
bool isWriteAllowed(void) const
272+
{
273+
return _write_allowed;
274+
}
275+
276+
/**
277+
* Set the security requirements of the write operations.
278+
* @param requirement The security level required by the write operations.
279+
*/
280+
void setWriteSecurityRequirement(Security_t requirement)
281+
{
282+
_write_security = requirement.value();
283+
}
284+
285+
/**
286+
* Return the security level required by write operations.
287+
* @return The security level of the write operations.
288+
*/
289+
Security_t getWriteSecurityRequirement() const
290+
{
291+
return static_cast<Security_t::type>(_write_security);
292+
}
293+
212294
private:
213295
/**
214296
* Characteristic's UUID.
@@ -230,15 +312,35 @@ class GattAttribute {
230312
*/
231313
uint16_t _len;
232314

315+
/**
316+
* The attribute's handle in the ATT table.
317+
*/
318+
Handle_t _handle;
319+
233320
/**
234321
* Whether the length of the value can change throughout time.
235322
*/
236323
bool _hasVariableLen;
237324

238325
/**
239-
* The attribute's handle in the ATT table.
326+
* Whether read is allowed or not.
240327
*/
241-
Handle_t _handle;
328+
uint8_t _read_allowed:1;
329+
330+
/**
331+
* Security applied to the read operation.
332+
*/
333+
uint8_t _read_security: Security_t::size;
334+
335+
/**
336+
* Whether write is allowed or not.
337+
*/
338+
uint8_t _write_allowed:1;
339+
340+
/**
341+
* Security applied to the write operation.
342+
*/
343+
uint8_t _write_security: Security_t::size;
242344

243345
private:
244346
/* Disallow copy and assignment. */

0 commit comments

Comments
 (0)