xref: /btstack/test/security_manager/mock.c (revision f12924e0a61c0582b548a1e70cea1bd59ba21f1d)
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_le_get_own_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 direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy) {
153  }
154 
155 uint16_t hci_get_manufacturer(void){
156  return 0xffff;
157 };
158 
159 void hci_le_set_own_address_type(uint8_t own_address){
160 }
161 
162 extern "C" void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t cid){
163 	if (packet_buffer_len) return;
164     uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 0, 0};
165     little_endian_store_16(event, 2, cid);
166     le_data_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
167 }
168 
169 int  l2cap_can_send_connectionless_packet_now(void){
170 	return packet_buffer_len == 0;
171 }
172 
173 int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
174 	return packet_buffer_len == 0;
175 }
176 
177 int hci_send_cmd(const hci_cmd_t *cmd, ...){
178     va_list argptr;
179     va_start(argptr, cmd);
180     uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
181     va_end(argptr);
182 	hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
183 	dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
184 	packet_buffer_len = len;
185 
186 	// track le encrypt and le rand
187 	if (cmd->opcode ==  hci_le_encrypt.opcode){
188 	    uint8_t * key_flipped = &packet_buffer[3];
189 	    uint8_t key[16];
190 		reverse_128(key_flipped, key);
191 	    // printf("le_encrypt key ");
192 	    // hexdump(key, 16);
193 	    uint8_t * plaintext_flipped = &packet_buffer[19];
194 	    uint8_t plaintext[16];
195  		reverse_128(plaintext_flipped, plaintext);
196 	    // printf("le_encrypt txt ");
197 	    // hexdump(plaintext, 16);
198 	    aes128_calc_cyphertext(key, plaintext, aes128_cyphertext);
199 	    // printf("le_encrypt res ");
200 	    // hexdump(aes128_cyphertext, 16);
201 	}
202 	return 0;
203 }
204 
205 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
206 	le_data_handler = packet_handler;
207 }
208 
209 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
210 	event_packet_handler = callback_handler->callback;
211 }
212 
213 int l2cap_reserve_packet_buffer(void){
214 	printf("l2cap_reserve_packet_buffer\n");
215 	return 1;
216 }
217 
218 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
219 	printf("l2cap_send_prepared_connectionless\n");
220 	return 0;
221 }
222 
223 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t * buffer, uint16_t len){
224 	// printf("l2cap_send_connectionless\n");
225 
226     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
227 
228 	// 0 - Connection handle : PB=pb : BC=00
229     little_endian_store_16(packet_buffer, 0, handle | (pb << 12) | (0 << 14));
230     // 2 - ACL length
231     little_endian_store_16(packet_buffer, 2,  len + 4);
232     // 4 - L2CAP packet length
233     little_endian_store_16(packet_buffer, 4,  len + 0);
234     // 6 - L2CAP channel DEST
235     little_endian_store_16(packet_buffer, 6, cid);
236 
237 	memcpy(&packet_buffer[8], buffer, len);
238 	hci_dump_packet(HCI_ACL_DATA_PACKET, 0, &packet_buffer[0], len + 8);
239 
240 	dump_packet(HCI_ACL_DATA_PACKET, packet_buffer, len + 8);
241 	packet_buffer_len = len + 8;
242 
243 	return 0;
244 }
245 
246 void hci_disconnect_security_block(hci_con_handle_t con_handle){
247 	printf("hci_disconnect_security_block \n");
248 }
249 
250 int hci_non_flushable_packet_boundary_flag_supported(void){
251 	return 1;
252 }
253 
254 void l2cap_run(void){
255 }
256