|
23 | 23 | #include "CallChainOfFunctionPointersWithContext.h"
|
24 | 24 | #include "ble/BLETypes.h"
|
25 | 25 |
|
| 26 | +/** |
| 27 | + * Overview |
| 28 | + * |
| 29 | + * Security Manager is used to provide link security through encryption, signing, and authentication |
| 30 | + * which are made possible by pairing and optionally bonding. Pairing is the process of establishing |
| 31 | + * and/or exchanging keys used for the current connection. Bonding means saving this information so that |
| 32 | + * it can later be used after reconnecting without having to pair again. This saves time and power. |
| 33 | + * |
| 34 | + * There are many ways to provide these at different levels of security depending on your requirements |
| 35 | + * and the facilities provided by the application. The process starts with initialising the SecurityManager |
| 36 | + * with default options for new connections. Some settings can later be changed per link or globally. |
| 37 | + * |
| 38 | + * The important settings in the init() function are the MITM requirement and IO capabilities. Man in the |
| 39 | + * Middle (MITM) protection prevents an attack where one device can impersonate another device by |
| 40 | + * pairing with both devices at the same time. This protection is achieved sharing some information |
| 41 | + * between the devices through some independent channel. The IO capabilities of both devices dictate |
| 42 | + * what algorithm is used. For details @see BLUETOOTH SPECIFICATION Version 5.0 | Vol 3, Part H - 2.3.5.1. |
| 43 | + * You can change the IO capabilities after initialisation with setIoCapability(). This will take effect |
| 44 | + * for all subsequent pairings. |
| 45 | + * |
| 46 | + * Sharing this information through IO capabilities means user interaction which limits the degree of |
| 47 | + * protection due to the limit of the amount of data that we can expect the user to transfer. Another |
| 48 | + * solution is using OOB (out of band) communication to transfer this data instead which can send much |
| 49 | + * more data making MITM attack even less likely to succeed. OOB data has to be exchanged by the application |
| 50 | + * and provided to the Security Manager. Use setOOBDataUsage() to indicate you want to use it. The same call also |
| 51 | + * allows you to set whether or not the communication channel you are using to transmit the OOB data is |
| 52 | + * itself secure against MITM protection - this will set the level of the link security achieved using pairing |
| 53 | + * using this data. |
| 54 | + * |
| 55 | + * The most secure pairing is provided by Secure Connections which relies on public key cryptography. |
| 56 | + * Support for Secure Connections is dependent on both the stack and controller on both sides supporting |
| 57 | + * it. If either side doesn't support it Legacy Pairing will be used. This is an older standard of pairing. |
| 58 | + * If higher security is required legacy pairing can be disabled by calling allowLegacyPairing(false); |
| 59 | + * |
| 60 | + * How to use |
| 61 | + * |
| 62 | + * First thing you need to do is to initialise the manager by calling init() with your chosen settings. |
| 63 | + * |
| 64 | + * The SecurityManager communicates with your application through events. These will trigger calls in |
| 65 | + * the EventHandler you must provide by calling the setSecurityManagerEventHandler() function. |
| 66 | + * |
| 67 | + * The most important process is pairing. This may be triggered manually by calling requestPairing() or |
| 68 | + * may be called as a result of the application requiring encryption or encryption through |
| 69 | + * requestAuthentication() or setLinkEncryption(). |
| 70 | + * |
| 71 | + * All these can be implicitly called by useing setLinkSecurity() to conveniently set the required |
| 72 | + * security for the link. The SecurityManager will trigger all the process required to achieve the set |
| 73 | + * security level. |
| 74 | + * |
| 75 | + * Depending on the IO capabilities and OOB usage settings different pairing algorithms will be chosen. |
| 76 | + * They will produce appropriate events which must be handled by your EventHandler. |
| 77 | + * |
| 78 | + * The simplest example would be a pairing of a device with no IO capabilities and no OOB data available. |
| 79 | + * With such limited pairing capabilities the "just works" method will be employed. This does not provide |
| 80 | + * any MITM protection. The pairing (triggered implicitly or called explicitly) will result in an event |
| 81 | + * being generated on the peer calling pairingRequest(). The event handler must make a decision (either in |
| 82 | + * the application itself or based on user interaction) whether to accept the pairing and call |
| 83 | + * accetPairing() or cancelPairing(). The result will be communicated on both peers through an event calling |
| 84 | + * pairingResult() in the EventHandler. |
| 85 | + * |
| 86 | + * |
| 87 | + * Sequence diagram "Just Works" pairing |
| 88 | + * |
| 89 | + * /----------- Device 1 --------------\ *------ BLE link ------* /-------------- Device 2 -------------\ |
| 90 | + * |
| 91 | + * App EventHandler SecurityManager SecurityManager EventHandler App |
| 92 | + * | | | | | | |
| 93 | + * |---------------------------> requestPairing() | | | |
| 94 | + * | | |------[pairing start]------>| | | |
| 95 | + * | | | |----------------> pairingRequest() ->| |
| 96 | + * | | | acceptPairing() <------------------------- | |
| 97 | + * | | |<---[pairing complete]----->| | | |
| 98 | + * |<- pairingResult() <---------------| |----------------> pairingResult() -->| |
| 99 | + * | | | | | | |
| 100 | + * |
| 101 | + * @note the requestPairing() call isn't required to trigger pairing. Pairing will also be triggered |
| 102 | + * if you request encryption and authentication and no bonding information is available. The sequence will |
| 103 | + * be the same save for the lack of explicit requestPairing() call. |
| 104 | + * |
| 105 | + * |
| 106 | + * Sequence diagram Encryption request when bonding information is available |
| 107 | + * |
| 108 | + * /----------- Device 1 --------------\ *------ BLE link ------* /-------------- Device 2 -------------\ |
| 109 | + * |
| 110 | + * App EventHandler SecurityManager SecurityManager EventHandler App |
| 111 | + * | | | | | | |
| 112 | + * |---------------------------> setLinkEncryption() | | | |
| 113 | + * | | |<-[encryption established]->| | | |
| 114 | + * |<- linkEncryptionResult() <--------| |---------> linkEncryptionResult() -->| |
| 115 | + * | | | | | | |
| 116 | + * |
| 117 | + * @note if bonding information is not available, pairing will be triggered |
| 118 | + */ |
| 119 | + |
26 | 120 | class SecurityManager {
|
27 | 121 | public:
|
28 | 122 | /** events sent and received when passkey is being entered */
|
|
0 commit comments