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