xref: /btstack/src/ble/sm.h (revision 16ece13520cb8efcf94cb63eab58a3eda87f40a2)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #ifndef __SM_H
39 #define __SM_H
40 
41 #include <stdint.h>
42 #include "utils.h"
43 
44 #if defined __cplusplus
45 extern "C" {
46 #endif
47 
48 // Only for PTS testing
49 void sm_test_set_irk(sm_key_t irk);
50 
51 typedef struct {
52     btstack_linked_item_t  item;
53     bd_addr_t      address;
54     bd_addr_type_t address_type;
55 } sm_lookup_entry_t;
56 
57 /* API_START */
58 
59 /**
60  * @brief Initializes the Security Manager, connects to L2CAP
61  */
62 void sm_init(void);
63 
64 /**
65  * @brief Set secret ER key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2
66  * @param er
67  */
68 void sm_set_er(sm_key_t er);
69 
70 /**
71  * @brief Set secret IR key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2
72  */
73 void sm_set_ir(sm_key_t ir);
74 
75 /**
76  *
77  * @brief Registers OOB Data Callback. The callback should set the oob_data and return 1 if OOB data is availble
78  * @param get_oob_data_callback
79  */
80 void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t addres_type, bd_addr_t addr, uint8_t * oob_data));
81 
82 /**
83  *
84  * @brief Registers packet handler. Called by att_server.c
85  */
86 void sm_register_packet_handler(btstack_packet_handler_t handler);
87 
88 /**
89  * @brief Limit the STK generation methods. Bonding is stopped if the resulting one isn't in the list
90  * @param OR combination of SM_STK_GENERATION_METHOD_
91  */
92 void sm_set_accepted_stk_generation_methods(uint8_t accepted_stk_generation_methods);
93 
94 /**
95  * @brief Set the accepted encryption key size range. Bonding is stopped if the result isn't within the range
96  * @param min_size (default 7)
97  * @param max_size (default 16)
98  */
99 void sm_set_encryption_key_size_range(uint8_t min_size, uint8_t max_size);
100 
101 /**
102  * @brief Sets the requested authentication requirements, bonding yes/no, MITM yes/no
103  * @param OR combination of SM_AUTHREQ_ flags
104  */
105 void sm_set_authentication_requirements(uint8_t auth_req);
106 
107 /**
108  * @brief Sets the available IO Capabilities
109  * @param IO_CAPABILITY_
110  */
111 void sm_set_io_capabilities(io_capability_t io_capability);
112 
113 /**
114  * @brief Let Peripheral request an encrypted connection right after connecting
115  * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server
116  */
117 void sm_set_request_security(int enable);
118 
119 /**
120  * @brief Trigger Security Request
121  * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server
122  */
123 void sm_send_security_request(uint16_t handle);
124 
125 /**
126  * @brief Decline bonding triggered by event before
127  * @param addr_type and address
128  */
129 void sm_bonding_decline(uint16_t handle);
130 
131 /**
132  * @brief Confirm Just Works bonding
133  * @param addr_type and address
134  */
135 void sm_just_works_confirm(uint16_t handle);
136 
137 /**
138  * @brief Reports passkey input by user
139  * @param addr_type and address
140  * @param passkey in [0..999999]
141  */
142 void sm_passkey_input(uint16_t handle, uint32_t passkey);
143 
144 /**
145  *
146  * @brief Get encryption key size.
147  * @param addr_type and address
148  * @return 0 if not encrypted, 7-16 otherwise
149  */
150 int sm_encryption_key_size(uint16_t handle);
151 
152 /**
153  * @brief Get authentication property.
154  * @param addr_type and address
155  * @return 1 if bonded with OOB/Passkey (AND MITM protection)
156  */
157 int sm_authenticated(uint16_t handle);
158 
159 /**
160  * @brief Queries authorization state.
161  * @param addr_type and address
162  * @return authorization_state for the current session
163  */
164 authorization_state_t sm_authorization_state(uint16_t handle);
165 
166 /**
167  * @brief Used by att_server.c to request user authorization.
168  * @param addr_type and address
169  */
170 void sm_request_pairing(uint16_t handle);
171 
172 /**
173  * @brief Report user authorization decline.
174  * @param addr_type and address
175  */
176 void sm_authorization_decline(uint16_t handle);
177 
178 /**
179  * @brief Report user authorization grant.
180  * @param addr_type and address
181  */
182 void sm_authorization_grant(uint16_t handle);
183 
184 /**
185  * @brief Support for signed writes, used by att_server.
186  * @note Message and result are in little endian to allows passing in ATT PDU without flipping.
187  * @note calculated hash in done_callback is big endian
188  */
189 int  sm_cmac_ready(void);
190 void sm_cmac_start(sm_key_t k, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t hash[8]));
191 
192 /*
193  * @brief Match address against bonded devices
194  * @return 0 if successfully added to lookup queue
195  * @note Triggers SM_IDENTITY_RESOLVING_* events
196  */
197 int sm_address_resolution_lookup(uint8_t addr_type, bd_addr_t addr);
198 
199 /**
200  * @brief Identify device in LE Device DB.
201  * @param handle
202  * @return index from le_device_db or -1 if not found/identified
203  */
204 int sm_le_device_index(uint16_t handle );
205 /* API_END */
206 
207 // testing only
208 void sm_test_use_fixed_local_csrk(void);
209 
210 #if defined __cplusplus
211 }
212 #endif
213 
214 #endif // __SM_H
215