xref: /btstack/test/security_manager/mock.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "ble/att_db.h"
7 #include "hci.h"
8 #include "hci_dump.h"
9 #include "l2cap.h"
10 #include "rijndael.h"
11 
12 
13 static btstack_packet_handler_t le_data_handler;
14 static btstack_packet_handler_t event_packet_handler;
15 
16 static uint8_t packet_buffer[256];
17 static uint16_t packet_buffer_len = 0;
18 
19 static uint8_t aes128_cyphertext[16];
20 
21 static hci_connection_t  the_connection;
22 static btstack_linked_list_t     connections;
23 
24 void mock_init(void){
25 	the_connection.item.next = NULL;
26 	connections = (btstack_linked_item*) &the_connection;
27 }
28 
29 uint8_t * mock_packet_buffer(void){
30 	return packet_buffer;
31 }
32 
33 void mock_clear_packet_buffer(void){
34 	packet_buffer_len = 0;
35 	memset(packet_buffer, 0, sizeof(packet_buffer));
36 }
37 
38 static void dump_packet(int packet_type, uint8_t * buffer, uint16_t size){
39 #if 0
40 	static int packet_counter = 1;
41 	char var_name[80];
42 	sprintf(var_name, "test_%s_packet_%02u", packet_type == HCI_COMMAND_DATA_PACKET ? "command" : "acl", packet_counter);
43 	printf("uint8_t %s[] = { ", var_name);
44 	for (int i = 0; i < size ; i++){
45 		if ((i % 16) == 0) printf("\n    ");
46 		printf ("0x%02x, ", buffer[i]);
47 	}
48 	printf("};\n");
49 	packet_counter++;
50 #endif
51 }
52 
53 void aes128_calc_cyphertext(uint8_t key[16], uint8_t plaintext[16], uint8_t cyphertext[16]){
54 	uint32_t rk[RKLENGTH(KEYBITS)];
55 	int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS);
56 	rijndaelEncrypt(rk, nrounds, plaintext, cyphertext);
57 }
58 
59 void mock_simulate_hci_event(uint8_t * packet, uint16_t size){
60 	hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
61 	if (event_packet_handler){
62 		event_packet_handler(HCI_EVENT_PACKET, 0, packet, size);
63 	}
64 	if (le_data_handler){
65 		le_data_handler(HCI_EVENT_PACKET, 0, packet, size);
66 	}
67 }
68 
69 void aes128_report_result(void){
70 	uint8_t le_enc_result[22];
71 	uint8_t enc1_data[] = { 0x0e, 0x14, 0x01, 0x17, 0x20, 0x00 };
72 	memcpy (le_enc_result, enc1_data, 6);
73 	reverse_128(aes128_cyphertext, &le_enc_result[6]);
74 	mock_simulate_hci_event(&le_enc_result[0], sizeof(le_enc_result));
75 }
76 
77 void mock_simulate_sm_data_packet(uint8_t * packet, uint16_t len){
78 
79 	uint16_t handle = 0x40;
80 	uint16_t cid = 0x06;
81 
82 	uint8_t acl_buffer[len + 8];
83 
84 	// 0 - Connection handle : PB=10 : BC=00
85     little_endian_store_16(acl_buffer, 0, handle | (0 << 12) | (0 << 14));
86     // 2 - ACL length
87     little_endian_store_16(acl_buffer, 2,  len + 4);
88     // 4 - L2CAP packet length
89     little_endian_store_16(acl_buffer, 4,  len + 0);
90     // 6 - L2CAP channel DEST
91     little_endian_store_16(acl_buffer, 6, cid);
92 
93 	memcpy(&acl_buffer[8], packet, len);
94 	hci_dump_packet(HCI_ACL_DATA_PACKET, 1, &acl_buffer[0], len + 8);
95 
96 	le_data_handler(SM_DATA_PACKET, handle, packet, len);
97 }
98 
99 void mock_simulate_command_complete(const hci_cmd_t *cmd){
100 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) cmd->opcode & 0xff, (uint8_t) cmd->opcode >> 8, 0};
101 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
102 }
103 
104 void mock_simulate_hci_state_working(void){
105 	uint8_t packet[] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
106 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
107 }
108 
109 void mock_simulate_connected(void){
110     uint8_t packet[] = { 0x3e, 0x13, 0x01, 0x00, 0x40, 0x00, 0x01, 0x01, 0x18, 0x12, 0x5e, 0x68, 0xc9, 0x73, 0x18, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05};
111 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
112 }
113 
114 void att_init_connection(att_connection_t * att_connection){
115     att_connection->mtu = 23;
116     att_connection->encryption_key_size = 0;
117     att_connection->authenticated = 0;
118 	att_connection->authorized = 0;
119 }
120 
121 void gap_local_bd_addr(bd_addr_t address_buffer){
122 	int i;
123 	for (i=0;i<6;i++) {
124 		address_buffer[i] = 0x11 * (i+1);
125 	}
126 }
127 int hci_can_send_command_packet_now(void){
128 	return 1;
129 }
130 int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
131 	return 1;
132 }
133 
134 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
135 	return &the_connection;
136 }
137 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
138 	return &the_connection;
139 }
140 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
141     btstack_linked_list_iterator_init(it, &connections);
142 }
143 
144 // get addr type and address used in advertisement packets
145 void gap_advertisements_get_address(uint8_t * addr_type, bd_addr_t addr){
146     *addr_type = 0;
147     uint8_t dummy[] = { 0x00, 0x1b, 0xdc, 0x07, 0x32, 0xef };
148     memcpy(addr, dummy, 6);
149 }
150 
151  void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
152     uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address,
153     uint8_t channel_map, uint8_t filter_policy) {
154  }
155 
156 extern "C" void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t cid){
157 	if (packet_buffer_len) return;
158     uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 0, 0};
159     little_endian_store_16(event, 2, cid);
160     le_data_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
161 }
162 
163 int  l2cap_can_send_connectionless_packet_now(void){
164 	return packet_buffer_len == 0;
165 }
166 
167 int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
168 	return packet_buffer_len == 0;
169 }
170 
171 int hci_send_cmd(const hci_cmd_t *cmd, ...){
172     va_list argptr;
173     va_start(argptr, cmd);
174     uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
175     va_end(argptr);
176 	hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
177 	dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
178 	packet_buffer_len = len;
179 
180 	// track le encrypt and le rand
181 	if (cmd->opcode ==  hci_le_encrypt.opcode){
182 	    uint8_t * key_flipped = &packet_buffer[3];
183 	    uint8_t key[16];
184 		reverse_128(key_flipped, key);
185 	    // printf("le_encrypt key ");
186 	    // hexdump(key, 16);
187 	    uint8_t * plaintext_flipped = &packet_buffer[19];
188 	    uint8_t plaintext[16];
189  		reverse_128(plaintext_flipped, plaintext);
190 	    // printf("le_encrypt txt ");
191 	    // hexdump(plaintext, 16);
192 	    aes128_calc_cyphertext(key, plaintext, aes128_cyphertext);
193 	    // printf("le_encrypt res ");
194 	    // hexdump(aes128_cyphertext, 16);
195 	}
196 	return 0;
197 }
198 
199 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
200 	le_data_handler = packet_handler;
201 }
202 
203 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
204 	event_packet_handler = callback_handler->callback;
205 }
206 
207 int l2cap_reserve_packet_buffer(void){
208 	printf("l2cap_reserve_packet_buffer\n");
209 	return 1;
210 }
211 
212 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
213 	printf("l2cap_send_prepared_connectionless\n");
214 	return 0;
215 }
216 
217 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t * buffer, uint16_t len){
218 	// printf("l2cap_send_connectionless\n");
219 
220     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
221 
222 	// 0 - Connection handle : PB=pb : BC=00
223     little_endian_store_16(packet_buffer, 0, handle | (pb << 12) | (0 << 14));
224     // 2 - ACL length
225     little_endian_store_16(packet_buffer, 2,  len + 4);
226     // 4 - L2CAP packet length
227     little_endian_store_16(packet_buffer, 4,  len + 0);
228     // 6 - L2CAP channel DEST
229     little_endian_store_16(packet_buffer, 6, cid);
230 
231 	memcpy(&packet_buffer[8], buffer, len);
232 	hci_dump_packet(HCI_ACL_DATA_PACKET, 0, &packet_buffer[0], len + 8);
233 
234 	dump_packet(HCI_ACL_DATA_PACKET, packet_buffer, len + 8);
235 	packet_buffer_len = len + 8;
236 
237 	return 0;
238 }
239 
240 void hci_disconnect_security_block(hci_con_handle_t con_handle){
241 	printf("hci_disconnect_security_block \n");
242 }
243 
244 int hci_non_flushable_packet_boundary_flag_supported(void){
245 	return 1;
246 }
247 
248 void l2cap_run(void){
249 }
250