1 /*
2  * Copyright (c) 2017 Intel Corporation
3  * Copyright (c) 2020 Lingao Meng
4  * Copyright (c) 2021 Manulytica Limited
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/kernel.h>
10 #include <errno.h>
11 #include <zephyr/sys/atomic.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/sys/byteorder.h>
14 
15 #include <zephyr/net_buf.h>
16 #include <zephyr/bluetooth/bluetooth.h>
17 #include <zephyr/bluetooth/conn.h>
18 #include <zephyr/bluetooth/mesh.h>
19 #include <zephyr/bluetooth/uuid.h>
20 
21 #include "common/bt_str.h"
22 
23 #include "crypto.h"
24 #include "mesh.h"
25 #include "net.h"
26 #include "rpl.h"
27 #include "beacon.h"
28 #include "access.h"
29 #include "foundation.h"
30 #include "proxy.h"
31 #include "prov.h"
32 #include "settings.h"
33 #include "provisioner.h"
34 
35 /* Timeout for receiving the link open response */
36 #define LINK_ESTABLISHMENT_TIMEOUT 60
37 
38 #define LOG_LEVEL CONFIG_BT_MESH_PROVISIONER_LOG_LEVEL
39 #include <zephyr/logging/log.h>
40 LOG_MODULE_REGISTER(bt_mesh_provisioner);
41 
42 static struct {
43 	struct bt_mesh_cdb_node *node;
44 	uint16_t net_idx;
45 	uint8_t elem_count;
46 	uint8_t attention_duration;
47 	uint8_t uuid[16];
48 	uint8_t new_dev_key[16];
49 } provisionee;
50 
51 static void send_pub_key(void);
52 static void prov_dh_key_gen(void);
53 
reset_state(void)54 static int reset_state(void)
55 {
56 	if (!atomic_test_bit(bt_mesh_prov_link.flags, REPROVISION) &&
57 	    provisionee.node != NULL) {
58 		bt_mesh_cdb_node_del(provisionee.node, false);
59 	}
60 
61 	return bt_mesh_prov_reset_state();
62 }
63 
prov_link_close(enum prov_bearer_link_status status)64 static void prov_link_close(enum prov_bearer_link_status status)
65 {
66 	LOG_DBG("%u", status);
67 	bt_mesh_prov_link.expect = PROV_NO_PDU;
68 
69 	bt_mesh_prov_link.bearer->link_close(status);
70 }
71 
prov_fail(uint8_t reason)72 static void prov_fail(uint8_t reason)
73 {
74 	/* According to MshPRTv1.1: 5.4.4, the
75 	 * provisioner just closes the link when something fails, while the
76 	 * provisionee sends the fail message, and waits for the provisioner to
77 	 * close the link.
78 	 */
79 	prov_link_close(PROV_BEARER_LINK_STATUS_FAIL);
80 }
81 
send_invite(void)82 static void send_invite(void)
83 {
84 	PROV_BUF(inv, PDU_LEN_INVITE);
85 
86 	LOG_DBG("");
87 
88 	bt_mesh_prov_buf_init(&inv, PROV_INVITE);
89 	net_buf_simple_add_u8(&inv, provisionee.attention_duration);
90 
91 	memcpy(bt_mesh_prov_link.conf_inputs.invite, &provisionee.attention_duration,
92 	       PDU_LEN_INVITE);
93 
94 	if (bt_mesh_prov_send(&inv, NULL)) {
95 		LOG_ERR("Failed to send invite");
96 		return;
97 	}
98 
99 	bt_mesh_prov_link.expect = PROV_CAPABILITIES;
100 }
101 
start_sent(int err,void * cb_data)102 static void start_sent(int err, void *cb_data)
103 {
104 	send_pub_key();
105 }
106 
send_start(void)107 static void send_start(void)
108 {
109 	LOG_DBG("");
110 
111 	PROV_BUF(start, PDU_LEN_START);
112 
113 	bool oob_pub_key = bt_mesh_prov_link.conf_inputs.capabilities[3] == PUB_KEY_OOB;
114 
115 	bt_mesh_prov_buf_init(&start, PROV_START);
116 	net_buf_simple_add_u8(&start, bt_mesh_prov_link.algorithm);
117 
118 	if (atomic_test_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY) && oob_pub_key) {
119 		net_buf_simple_add_u8(&start, PUB_KEY_OOB);
120 		atomic_set_bit(bt_mesh_prov_link.flags, OOB_PUB_KEY);
121 	} else {
122 		net_buf_simple_add_u8(&start, PUB_KEY_NO_OOB);
123 	}
124 
125 	net_buf_simple_add_u8(&start, bt_mesh_prov_link.oob_method);
126 
127 	net_buf_simple_add_u8(&start, bt_mesh_prov_link.oob_action);
128 
129 	net_buf_simple_add_u8(&start, bt_mesh_prov_link.oob_size);
130 
131 	memcpy(bt_mesh_prov_link.conf_inputs.start, &start.data[1], PDU_LEN_START);
132 
133 	if (bt_mesh_prov_auth(true, bt_mesh_prov_link.oob_method,
134 		bt_mesh_prov_link.oob_action, bt_mesh_prov_link.oob_size) < 0) {
135 		LOG_ERR("Invalid authentication method: 0x%02x; "
136 		       "action: 0x%02x; size: 0x%02x", bt_mesh_prov_link.oob_method,
137 		       bt_mesh_prov_link.oob_action, bt_mesh_prov_link.oob_size);
138 		return;
139 	}
140 
141 	if (bt_mesh_prov_send(&start, start_sent)) {
142 		LOG_ERR("Failed to send Provisioning Start");
143 		return;
144 	}
145 }
146 
prov_check_method(struct bt_mesh_dev_capabilities * caps)147 static bool prov_check_method(struct bt_mesh_dev_capabilities *caps)
148 {
149 	if (bt_mesh_prov_link.oob_method == AUTH_METHOD_STATIC) {
150 		if (!caps->oob_type) {
151 			LOG_WRN("Device not support OOB static authentication provisioning");
152 			return false;
153 		}
154 	} else if (bt_mesh_prov_link.oob_method == AUTH_METHOD_INPUT) {
155 		if (bt_mesh_prov_link.oob_size > caps->input_size) {
156 			LOG_WRN("The required input length (0x%02x) "
157 				"exceeds the device capacity (0x%02x)",
158 				bt_mesh_prov_link.oob_size, caps->input_size);
159 			return false;
160 		}
161 
162 		if (!(BIT(bt_mesh_prov_link.oob_action) & caps->input_actions)) {
163 			LOG_WRN("The required input action (0x%04x) "
164 				"not supported by the device (0x%02x)",
165 				(uint16_t)BIT(bt_mesh_prov_link.oob_action), caps->input_actions);
166 			return false;
167 		}
168 
169 		if (bt_mesh_prov_link.oob_action == INPUT_OOB_STRING) {
170 			if (!bt_mesh_prov->output_string) {
171 				LOG_WRN("Not support output string");
172 				return false;
173 			}
174 		} else {
175 			if (!bt_mesh_prov->output_number) {
176 				LOG_WRN("Not support output number");
177 				return false;
178 			}
179 		}
180 	} else if (bt_mesh_prov_link.oob_method == AUTH_METHOD_OUTPUT) {
181 		if (bt_mesh_prov_link.oob_size > caps->output_size) {
182 			LOG_WRN("The required output length (0x%02x) "
183 				"exceeds the device capacity (0x%02x)",
184 				bt_mesh_prov_link.oob_size, caps->output_size);
185 			return false;
186 		}
187 
188 		if (!(BIT(bt_mesh_prov_link.oob_action) & caps->output_actions)) {
189 			LOG_WRN("The required output action (0x%04x) "
190 				"not supported by the device (0x%02x)",
191 				(uint16_t)BIT(bt_mesh_prov_link.oob_action), caps->output_actions);
192 			return false;
193 		}
194 
195 		if (!bt_mesh_prov->input) {
196 			LOG_WRN("Not support input");
197 			return false;
198 		}
199 	}
200 
201 	return true;
202 }
203 
prov_capabilities(const uint8_t * data)204 static void prov_capabilities(const uint8_t *data)
205 {
206 	struct bt_mesh_dev_capabilities caps;
207 
208 	caps.elem_count = data[0];
209 	LOG_DBG("Elements:          %u", caps.elem_count);
210 
211 	caps.algorithms = sys_get_be16(&data[1]);
212 	LOG_DBG("Algorithms:        0x%02x", caps.algorithms);
213 
214 	bool is_aes128 = false;
215 	bool is_sha256 = false;
216 
217 	if ((caps.algorithms & BIT(BT_MESH_PROV_AUTH_CMAC_AES128_AES_CCM)) &&
218 		IS_ENABLED(CONFIG_BT_MESH_ECDH_P256_CMAC_AES128_AES_CCM)) {
219 		is_aes128 = true;
220 	}
221 
222 	if ((caps.algorithms & BIT(BT_MESH_PROV_AUTH_HMAC_SHA256_AES_CCM)) &&
223 		IS_ENABLED(CONFIG_BT_MESH_ECDH_P256_HMAC_SHA256_AES_CCM)) {
224 		is_sha256 = true;
225 	}
226 
227 	if (!(is_sha256 || is_aes128)) {
228 		LOG_ERR("Invalid encryption algorithm");
229 		prov_fail(PROV_ERR_NVAL_FMT);
230 		return;
231 	}
232 
233 	caps.pub_key_type = data[3];
234 	caps.oob_type = data[4];
235 	caps.output_size = data[5];
236 	LOG_DBG("Public Key Type:   0x%02x", caps.pub_key_type);
237 	LOG_DBG("Static OOB Type:   0x%02x", caps.oob_type);
238 	LOG_DBG("Output OOB Size:   %u", caps.output_size);
239 
240 	caps.output_actions = (bt_mesh_output_action_t)
241 					(sys_get_be16(&data[6]));
242 	caps.input_size = data[8];
243 	caps.input_actions = (bt_mesh_input_action_t)
244 					(sys_get_be16(&data[9]));
245 	LOG_DBG("Output OOB Action: 0x%04x", caps.output_actions);
246 	LOG_DBG("Input OOB Size:    %u", caps.input_size);
247 	LOG_DBG("Input OOB Action:  0x%04x", caps.input_actions);
248 
249 	provisionee.elem_count = caps.elem_count;
250 	if (provisionee.elem_count == 0) {
251 		LOG_ERR("Invalid number of elements");
252 		prov_fail(PROV_ERR_NVAL_FMT);
253 		return;
254 	}
255 
256 	if (caps.oob_type & BT_MESH_OOB_AUTH_REQUIRED) {
257 
258 		bool oob_availability = caps.output_size > 0 || caps.input_size > 0 ||
259 			(caps.oob_type & BT_MESH_STATIC_OOB_AVAILABLE);
260 
261 		if (!oob_availability && !is_sha256) {
262 			LOG_ERR("Invalid capabilities for OOB authentication");
263 			prov_fail(PROV_ERR_NVAL_FMT);
264 			return;
265 		}
266 	}
267 
268 	bt_mesh_prov_link.algorithm = is_sha256 ? BT_MESH_PROV_AUTH_HMAC_SHA256_AES_CCM :
269 			BT_MESH_PROV_AUTH_CMAC_AES128_AES_CCM;
270 
271 	if (atomic_test_bit(bt_mesh_prov_link.flags, REPROVISION)) {
272 		if (!bt_mesh_prov_link.addr) {
273 			bt_mesh_prov_link.addr = bt_mesh_cdb_free_addr_get(
274 				provisionee.elem_count);
275 			if (!bt_mesh_prov_link.addr) {
276 				LOG_ERR("Failed allocating address for node");
277 				prov_fail(PROV_ERR_ADDR);
278 				return;
279 			}
280 		}
281 	} else {
282 		provisionee.node =
283 			bt_mesh_cdb_node_alloc(provisionee.uuid,
284 					       bt_mesh_prov_link.addr,
285 					       provisionee.elem_count,
286 					       provisionee.net_idx);
287 		if (provisionee.node == NULL) {
288 			LOG_ERR("Failed allocating node 0x%04x", bt_mesh_prov_link.addr);
289 			prov_fail(PROV_ERR_RESOURCES);
290 			return;
291 		}
292 
293 		/* Address might change in the alloc call */
294 		bt_mesh_prov_link.addr = provisionee.node->addr;
295 	}
296 
297 	memcpy(bt_mesh_prov_link.conf_inputs.capabilities, data, PDU_LEN_CAPABILITIES);
298 
299 	if (bt_mesh_prov->capabilities) {
300 		bt_mesh_prov->capabilities(&caps);
301 	}
302 
303 	if (!prov_check_method(&caps)) {
304 		prov_fail(PROV_ERR_UNEXP_ERR);
305 		return;
306 	}
307 
308 	send_start();
309 }
310 
send_confirm(void)311 static void send_confirm(void)
312 {
313 	PROV_BUF(cfm, PDU_LEN_CONFIRM);
314 	uint8_t auth_size = bt_mesh_prov_auth_size_get();
315 	uint8_t *inputs = (uint8_t *)&bt_mesh_prov_link.conf_inputs;
316 	uint8_t conf_key_input[64];
317 
318 	LOG_DBG("ConfInputs[0]   %s", bt_hex(inputs, 32));
319 	LOG_DBG("ConfInputs[32]  %s", bt_hex(&inputs[32], 32));
320 	LOG_DBG("ConfInputs[64]  %s", bt_hex(&inputs[64], 32));
321 	LOG_DBG("ConfInputs[96]  %s", bt_hex(&inputs[96], 32));
322 	LOG_DBG("ConfInputs[128] %s", bt_hex(&inputs[128], 17));
323 
324 	if (bt_mesh_prov_conf_salt(bt_mesh_prov_link.algorithm,
325 				   inputs,
326 				   bt_mesh_prov_link.conf_salt)) {
327 		LOG_ERR("Unable to generate confirmation salt");
328 		prov_fail(PROV_ERR_UNEXP_ERR);
329 		return;
330 	}
331 
332 	LOG_DBG("ConfirmationSalt: %s", bt_hex(bt_mesh_prov_link.conf_salt, auth_size));
333 
334 	memcpy(conf_key_input, bt_mesh_prov_link.dhkey, 32);
335 
336 	if (bt_mesh_prov_link.algorithm == BT_MESH_PROV_AUTH_HMAC_SHA256_AES_CCM &&
337 	    IS_ENABLED(CONFIG_BT_MESH_ECDH_P256_HMAC_SHA256_AES_CCM)) {
338 		memcpy(&conf_key_input[32], bt_mesh_prov_link.auth, 32);
339 		LOG_DBG("AuthValue  %s", bt_hex(bt_mesh_prov_link.auth, 32));
340 	}
341 
342 	if (bt_mesh_prov_conf_key(bt_mesh_prov_link.algorithm, conf_key_input,
343 				  bt_mesh_prov_link.conf_salt, bt_mesh_prov_link.conf_key)) {
344 		LOG_ERR("Unable to generate confirmation key");
345 		prov_fail(PROV_ERR_UNEXP_ERR);
346 		return;
347 	}
348 
349 	LOG_DBG("ConfirmationKey: %s", bt_hex(bt_mesh_prov_link.conf_key, auth_size));
350 
351 	if (bt_rand(bt_mesh_prov_link.rand, auth_size)) {
352 		LOG_ERR("Unable to generate random number");
353 		prov_fail(PROV_ERR_UNEXP_ERR);
354 		return;
355 	}
356 
357 	LOG_DBG("LocalRandom: %s", bt_hex(bt_mesh_prov_link.rand, auth_size));
358 
359 	bt_mesh_prov_buf_init(&cfm, PROV_CONFIRM);
360 
361 	if (bt_mesh_prov_conf(bt_mesh_prov_link.algorithm, bt_mesh_prov_link.conf_key,
362 			      bt_mesh_prov_link.rand, bt_mesh_prov_link.auth,
363 			      bt_mesh_prov_link.conf)) {
364 		LOG_ERR("Unable to generate confirmation value");
365 		prov_fail(PROV_ERR_UNEXP_ERR);
366 		return;
367 	}
368 
369 	net_buf_simple_add_mem(&cfm, bt_mesh_prov_link.conf, auth_size);
370 
371 	if (bt_mesh_prov_send(&cfm, NULL)) {
372 		LOG_ERR("Failed to send Provisioning Confirm");
373 		return;
374 	}
375 
376 	bt_mesh_prov_link.expect = PROV_CONFIRM;
377 }
378 
public_key_sent(int err,void * cb_data)379 static void public_key_sent(int err, void *cb_data)
380 {
381 	atomic_set_bit(bt_mesh_prov_link.flags, PUB_KEY_SENT);
382 
383 	if (atomic_test_bit(bt_mesh_prov_link.flags, OOB_PUB_KEY) &&
384 	    atomic_test_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY)) {
385 		prov_dh_key_gen();
386 		return;
387 	}
388 }
389 
send_pub_key(void)390 static void send_pub_key(void)
391 {
392 	PROV_BUF(buf, PDU_LEN_PUB_KEY);
393 	const uint8_t *key;
394 
395 	key = bt_mesh_pub_key_get();
396 	if (!key) {
397 		LOG_ERR("No public key available");
398 		prov_fail(PROV_ERR_UNEXP_ERR);
399 		return;
400 	}
401 
402 	bt_mesh_prov_buf_init(&buf, PROV_PUB_KEY);
403 	net_buf_simple_add_mem(&buf, key, PUB_KEY_SIZE);
404 	LOG_DBG("Local Public Key: %s", bt_hex(buf.data + 1, PUB_KEY_SIZE));
405 
406 	/* PublicKeyProvisioner */
407 	memcpy(bt_mesh_prov_link.conf_inputs.pub_key_provisioner, &buf.data[1], PDU_LEN_PUB_KEY);
408 
409 	if (bt_mesh_prov_send(&buf, public_key_sent)) {
410 		LOG_ERR("Failed to send Public Key");
411 		return;
412 	}
413 
414 	bt_mesh_prov_link.expect = PROV_PUB_KEY;
415 }
416 
prov_dh_key_gen(void)417 static void prov_dh_key_gen(void)
418 {
419 	const uint8_t *remote_pk;
420 	const uint8_t *local_pk;
421 
422 	local_pk = bt_mesh_prov_link.conf_inputs.pub_key_provisioner;
423 	remote_pk = bt_mesh_prov_link.conf_inputs.pub_key_device;
424 
425 	if (!memcmp(local_pk, remote_pk, PUB_KEY_SIZE)) {
426 		LOG_ERR("Public keys are identical");
427 		prov_fail(PROV_ERR_NVAL_FMT);
428 		return;
429 	}
430 
431 	if (bt_mesh_dhkey_gen(remote_pk, NULL, bt_mesh_prov_link.dhkey)) {
432 		LOG_ERR("Failed to generate DHKey");
433 		prov_fail(PROV_ERR_UNEXP_ERR);
434 	}
435 
436 	LOG_DBG("DHkey: %s", bt_hex(bt_mesh_prov_link.dhkey, DH_KEY_SIZE));
437 
438 	if (atomic_test_bit(bt_mesh_prov_link.flags, NOTIFY_INPUT_COMPLETE)) {
439 		bt_mesh_prov_link.expect = PROV_INPUT_COMPLETE;
440 	}
441 
442 	if (atomic_test_bit(bt_mesh_prov_link.flags, WAIT_STRING) ||
443 	    atomic_test_bit(bt_mesh_prov_link.flags, WAIT_NUMBER) ||
444 	    atomic_test_bit(bt_mesh_prov_link.flags, NOTIFY_INPUT_COMPLETE)) {
445 		atomic_set_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM);
446 		return;
447 	}
448 
449 	send_confirm();
450 }
451 
prov_dh_key_gen_handler(struct k_work * work)452 static void prov_dh_key_gen_handler(struct k_work *work)
453 {
454 	prov_dh_key_gen();
455 }
456 
457 static K_WORK_DEFINE(dh_gen_work, prov_dh_key_gen_handler);
458 
prov_pub_key(const uint8_t * data)459 static void prov_pub_key(const uint8_t *data)
460 {
461 	LOG_DBG("Remote Public Key: %s", bt_hex(data, PUB_KEY_SIZE));
462 
463 	atomic_set_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY);
464 
465 	/* PublicKeyDevice */
466 	memcpy(bt_mesh_prov_link.conf_inputs.pub_key_device, data, PUB_KEY_SIZE);
467 	bt_mesh_prov_link.bearer->clear_tx();
468 
469 	k_work_submit(&dh_gen_work);
470 }
471 
notify_input_complete(void)472 static void notify_input_complete(void)
473 {
474 	if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags,
475 				      NOTIFY_INPUT_COMPLETE) &&
476 	    bt_mesh_prov->input_complete) {
477 		bt_mesh_prov->input_complete();
478 	}
479 }
480 
prov_input_complete(const uint8_t * data)481 static void prov_input_complete(const uint8_t *data)
482 {
483 	LOG_DBG("");
484 
485 	notify_input_complete();
486 
487 	if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM)) {
488 		send_confirm();
489 	}
490 }
491 
send_prov_data(void)492 static void send_prov_data(void)
493 {
494 	PROV_BUF(pdu, PDU_LEN_DATA);
495 	struct bt_mesh_cdb_subnet *sub;
496 	uint8_t net_key[16];
497 	struct bt_mesh_key session_key;
498 	uint8_t nonce[13];
499 	int err;
500 
501 	err = bt_mesh_session_key(bt_mesh_prov_link.dhkey,
502 				  bt_mesh_prov_link.prov_salt, &session_key);
503 	if (err) {
504 		LOG_ERR("Unable to generate session key");
505 		prov_fail(PROV_ERR_UNEXP_ERR);
506 		return;
507 	}
508 
509 	err = bt_mesh_prov_nonce(bt_mesh_prov_link.dhkey,
510 				 bt_mesh_prov_link.prov_salt, nonce);
511 	if (err) {
512 		LOG_ERR("Unable to generate session nonce");
513 		prov_fail(PROV_ERR_UNEXP_ERR);
514 		goto session_key_destructor;
515 	}
516 
517 	LOG_DBG("Nonce: %s", bt_hex(nonce, 13));
518 
519 	err = bt_mesh_dev_key(bt_mesh_prov_link.dhkey,
520 			      bt_mesh_prov_link.prov_salt, provisionee.new_dev_key);
521 	if (err) {
522 		LOG_ERR("Unable to generate device key");
523 		prov_fail(PROV_ERR_UNEXP_ERR);
524 		goto session_key_destructor;
525 	}
526 
527 	sub = bt_mesh_cdb_subnet_get(provisionee.node->net_idx);
528 	if (sub == NULL) {
529 		LOG_ERR("No subnet with net_idx %u", provisionee.node->net_idx);
530 		prov_fail(PROV_ERR_UNEXP_ERR);
531 		goto session_key_destructor;
532 	}
533 
534 	err = bt_mesh_key_export(net_key, &sub->keys[SUBNET_KEY_TX_IDX(sub)].net_key);
535 	if (err) {
536 		LOG_ERR("Unable to export network key");
537 		prov_fail(PROV_ERR_UNEXP_ERR);
538 		goto session_key_destructor;
539 	}
540 
541 	bt_mesh_prov_buf_init(&pdu, PROV_DATA);
542 	net_buf_simple_add_mem(&pdu, net_key, sizeof(net_key));
543 	net_buf_simple_add_be16(&pdu, provisionee.node->net_idx);
544 	net_buf_simple_add_u8(&pdu, bt_mesh_cdb_subnet_flags(sub));
545 	net_buf_simple_add_be32(&pdu, bt_mesh_cdb.iv_index);
546 	net_buf_simple_add_be16(&pdu, bt_mesh_prov_link.addr);
547 	net_buf_simple_add(&pdu, 8); /* For MIC */
548 
549 	LOG_DBG("net_idx %u, iv_index 0x%08x, addr 0x%04x",
550 		provisionee.node->net_idx, bt_mesh.iv_index,
551 		bt_mesh_prov_link.addr);
552 
553 	err = bt_mesh_prov_encrypt(&session_key, nonce, &pdu.data[1],
554 				   &pdu.data[1]);
555 	if (err) {
556 		LOG_ERR("Unable to encrypt provisioning data");
557 		prov_fail(PROV_ERR_DECRYPT);
558 		goto session_key_destructor;
559 	}
560 
561 	if (bt_mesh_prov_send(&pdu, NULL)) {
562 		LOG_ERR("Failed to send Provisioning Data");
563 		goto session_key_destructor;
564 	}
565 
566 	bt_mesh_prov_link.expect = PROV_COMPLETE;
567 
568 session_key_destructor:
569 	bt_mesh_key_destroy(&session_key);
570 }
571 
prov_complete(const uint8_t * data)572 static void prov_complete(const uint8_t *data)
573 {
574 	struct bt_mesh_cdb_node *node = provisionee.node;
575 
576 	LOG_DBG("key %s, net_idx %u, num_elem %u, addr 0x%04x",
577 		bt_hex(&provisionee.new_dev_key, 16), node->net_idx,
578 		node->num_elem, node->addr);
579 
580 	bt_mesh_prov_link.expect = PROV_NO_PDU;
581 	atomic_set_bit(bt_mesh_prov_link.flags, COMPLETE);
582 
583 	bt_mesh_prov_link.bearer->link_close(PROV_BEARER_LINK_STATUS_SUCCESS);
584 }
585 
prov_node_add(void)586 static void prov_node_add(void)
587 {
588 	LOG_DBG("");
589 	struct bt_mesh_cdb_node *node = provisionee.node;
590 	int err;
591 
592 	err = bt_mesh_cdb_node_key_import(node, provisionee.new_dev_key);
593 	if (err) {
594 		LOG_ERR("Failed to import node device key");
595 		return;
596 	}
597 
598 	if (atomic_test_bit(bt_mesh_prov_link.flags, REPROVISION)) {
599 		bt_mesh_cdb_node_update(node, bt_mesh_prov_link.addr,
600 					provisionee.elem_count);
601 	} else if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
602 		bt_mesh_cdb_node_store(node);
603 	}
604 
605 	provisionee.node = NULL;
606 
607 	if (bt_mesh_prov->node_added) {
608 		bt_mesh_prov->node_added(node->net_idx, node->uuid, node->addr,
609 					 node->num_elem);
610 	}
611 }
612 
send_random(void)613 static void send_random(void)
614 {
615 	PROV_BUF(rnd, PDU_LEN_RANDOM);
616 	uint8_t rand_size = bt_mesh_prov_auth_size_get();
617 
618 	bt_mesh_prov_buf_init(&rnd, PROV_RANDOM);
619 	net_buf_simple_add_mem(&rnd, bt_mesh_prov_link.rand, rand_size);
620 
621 	if (bt_mesh_prov_send(&rnd, NULL)) {
622 		LOG_ERR("Failed to send Provisioning Random");
623 		return;
624 	}
625 
626 	bt_mesh_prov_link.expect = PROV_RANDOM;
627 }
628 
prov_random(const uint8_t * data)629 static void prov_random(const uint8_t *data)
630 {
631 	uint8_t rand_size = bt_mesh_prov_auth_size_get();
632 	uint8_t conf_verify[PROV_AUTH_MAX_LEN];
633 
634 	LOG_DBG("Remote Random: %s", bt_hex(data, rand_size));
635 	if (!memcmp(data, bt_mesh_prov_link.rand, rand_size)) {
636 		LOG_ERR("Random value is identical to ours, rejecting.");
637 		prov_fail(PROV_ERR_CFM_FAILED);
638 		return;
639 	}
640 
641 	if (bt_mesh_prov_conf(bt_mesh_prov_link.algorithm, bt_mesh_prov_link.conf_key,
642 		data, bt_mesh_prov_link.auth, conf_verify)) {
643 		LOG_ERR("Unable to calculate confirmation verification");
644 		prov_fail(PROV_ERR_UNEXP_ERR);
645 		return;
646 	}
647 
648 	if (memcmp(conf_verify, bt_mesh_prov_link.conf, rand_size)) {
649 		LOG_ERR("Invalid confirmation value");
650 		LOG_DBG("Received:   %s", bt_hex(bt_mesh_prov_link.conf, rand_size));
651 		LOG_DBG("Calculated: %s",  bt_hex(conf_verify, rand_size));
652 		prov_fail(PROV_ERR_CFM_FAILED);
653 		return;
654 	}
655 
656 	if (bt_mesh_prov_salt(bt_mesh_prov_link.algorithm, bt_mesh_prov_link.conf_salt,
657 		bt_mesh_prov_link.rand, data, bt_mesh_prov_link.prov_salt)) {
658 		LOG_ERR("Failed to generate provisioning salt");
659 		prov_fail(PROV_ERR_UNEXP_ERR);
660 		return;
661 	}
662 
663 	LOG_DBG("ProvisioningSalt: %s", bt_hex(bt_mesh_prov_link.prov_salt, 16));
664 
665 	send_prov_data();
666 }
667 
prov_confirm(const uint8_t * data)668 static void prov_confirm(const uint8_t *data)
669 {
670 	uint8_t conf_size = bt_mesh_prov_auth_size_get();
671 
672 	LOG_DBG("Remote Confirm: %s", bt_hex(data, conf_size));
673 
674 	if (!memcmp(data, bt_mesh_prov_link.conf, conf_size)) {
675 		LOG_ERR("Confirm value is identical to ours, rejecting.");
676 		prov_fail(PROV_ERR_CFM_FAILED);
677 		return;
678 	}
679 
680 	memcpy(bt_mesh_prov_link.conf, data, conf_size);
681 
682 	send_random();
683 }
684 
prov_failed(const uint8_t * data)685 static void prov_failed(const uint8_t *data)
686 {
687 	LOG_WRN("Error: 0x%02x", data[0]);
688 	reset_state();
689 }
690 
local_input_complete(void)691 static void local_input_complete(void)
692 {
693 	if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM)) {
694 		send_confirm();
695 	}
696 }
697 
prov_link_closed(enum prov_bearer_link_status status)698 static void prov_link_closed(enum prov_bearer_link_status status)
699 {
700 	LOG_DBG("");
701 	if (atomic_test_bit(bt_mesh_prov_link.flags, COMPLETE)) {
702 		prov_node_add();
703 	}
704 
705 	reset_state();
706 }
707 
prov_link_opened(void)708 static void prov_link_opened(void)
709 {
710 	send_invite();
711 }
712 
713 static const struct bt_mesh_prov_role role_provisioner = {
714 	.input_complete = local_input_complete,
715 	.link_opened = prov_link_opened,
716 	.link_closed = prov_link_closed,
717 	.error = prov_fail,
718 	.op = {
719 		[PROV_CAPABILITIES] = prov_capabilities,
720 		[PROV_PUB_KEY] = prov_pub_key,
721 		[PROV_INPUT_COMPLETE] = prov_input_complete,
722 		[PROV_CONFIRM] = prov_confirm,
723 		[PROV_RANDOM] = prov_random,
724 		[PROV_COMPLETE] = prov_complete,
725 		[PROV_FAILED] = prov_failed,
726 	},
727 };
728 
prov_set_method(uint8_t method,uint8_t action,uint8_t size)729 static void prov_set_method(uint8_t method, uint8_t action, uint8_t size)
730 {
731 	bt_mesh_prov_link.oob_method = method;
732 	bt_mesh_prov_link.oob_action = action;
733 	bt_mesh_prov_link.oob_size = size;
734 }
735 
bt_mesh_auth_method_set_input(bt_mesh_input_action_t action,uint8_t size)736 int bt_mesh_auth_method_set_input(bt_mesh_input_action_t action, uint8_t size)
737 {
738 	if (!action || !size || size > PROV_IO_OOB_SIZE_MAX) {
739 		return -EINVAL;
740 	}
741 
742 	prov_set_method(AUTH_METHOD_INPUT, find_msb_set(action) - 1, size);
743 	return 0;
744 }
745 
bt_mesh_auth_method_set_output(bt_mesh_output_action_t action,uint8_t size)746 int bt_mesh_auth_method_set_output(bt_mesh_output_action_t action, uint8_t size)
747 {
748 	if (!action || !size || size > PROV_IO_OOB_SIZE_MAX) {
749 		return -EINVAL;
750 	}
751 
752 	prov_set_method(AUTH_METHOD_OUTPUT, find_msb_set(action) - 1, size);
753 	return 0;
754 }
755 
bt_mesh_auth_method_set_static(const uint8_t * static_val,uint8_t size)756 int bt_mesh_auth_method_set_static(const uint8_t *static_val, uint8_t size)
757 {
758 	if (!size || !static_val) {
759 		return -EINVAL;
760 	}
761 
762 	prov_set_method(AUTH_METHOD_STATIC, 0, 0);
763 
764 	/* Trim the Auth if it is longer than required length */
765 	memcpy(bt_mesh_prov_link.auth, static_val,
766 	       size > PROV_AUTH_MAX_LEN ? PROV_AUTH_MAX_LEN : size);
767 
768 	/* Pad with zeros if the Auth is shorter the required length */
769 	if (size < PROV_AUTH_MAX_LEN) {
770 		memset(bt_mesh_prov_link.auth + size, 0, PROV_AUTH_MAX_LEN - size);
771 	}
772 
773 	return 0;
774 }
775 
bt_mesh_auth_method_set_none(void)776 int bt_mesh_auth_method_set_none(void)
777 {
778 	prov_set_method(AUTH_METHOD_NO_OOB, 0, 0);
779 	return 0;
780 }
781 
bt_mesh_prov_remote_pub_key_set(const uint8_t public_key[PUB_KEY_SIZE])782 int bt_mesh_prov_remote_pub_key_set(const uint8_t public_key[PUB_KEY_SIZE])
783 {
784 	if (public_key == NULL) {
785 		return -EINVAL;
786 	}
787 
788 	if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY)) {
789 		return -EALREADY;
790 	}
791 
792 	memcpy(bt_mesh_prov_link.conf_inputs.pub_key_device, public_key, PDU_LEN_PUB_KEY);
793 
794 	return 0;
795 }
796 
link_open(const uint8_t * uuid,const struct prov_bearer * bearer,uint16_t net_idx,uint16_t addr,uint8_t attention_duration,void * bearer_cb_data,uint8_t timeout)797 static int link_open(const uint8_t *uuid, const struct prov_bearer *bearer,
798 		     uint16_t net_idx, uint16_t addr,
799 		     uint8_t attention_duration, void *bearer_cb_data, uint8_t timeout)
800 {
801 	int err;
802 
803 	if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, LINK_ACTIVE)) {
804 		return -EBUSY;
805 	}
806 
807 	if (uuid) {
808 		memcpy(provisionee.uuid, uuid, 16);
809 
810 		struct bt_uuid_128 uuid_repr = { .uuid = { BT_UUID_TYPE_128 } };
811 
812 		memcpy(uuid_repr.val, uuid, 16);
813 		LOG_DBG("Provisioning %s", bt_uuid_str(&uuid_repr.uuid));
814 
815 	} else {
816 		atomic_set_bit(bt_mesh_prov_link.flags, REPROVISION);
817 		LOG_DBG("Reprovisioning");
818 	}
819 
820 	atomic_set_bit(bt_mesh_prov_link.flags, PROVISIONER);
821 	bt_mesh_prov_link.addr = addr;
822 	bt_mesh_prov_link.bearer = bearer;
823 	bt_mesh_prov_link.role = &role_provisioner;
824 	provisionee.net_idx = net_idx;
825 	provisionee.attention_duration = attention_duration;
826 
827 	err = bt_mesh_prov_link.bearer->link_open(
828 		uuid, timeout, bt_mesh_prov_bearer_cb_get(), bearer_cb_data);
829 	if (err) {
830 		atomic_clear_bit(bt_mesh_prov_link.flags, LINK_ACTIVE);
831 	}
832 
833 	return err;
834 }
835 
836 #if defined(CONFIG_BT_MESH_PB_ADV)
bt_mesh_pb_adv_open(const uint8_t uuid[16],uint16_t net_idx,uint16_t addr,uint8_t attention_duration)837 int bt_mesh_pb_adv_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
838 			uint8_t attention_duration)
839 {
840 	return link_open(uuid, &bt_mesh_pb_adv, net_idx, addr, attention_duration, NULL,
841 			 LINK_ESTABLISHMENT_TIMEOUT);
842 }
843 #endif
844 
845 #if defined(CONFIG_BT_MESH_PB_GATT_CLIENT)
bt_mesh_pb_gatt_open(const uint8_t uuid[16],uint16_t net_idx,uint16_t addr,uint8_t attention_duration)846 int bt_mesh_pb_gatt_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
847 			 uint8_t attention_duration)
848 {
849 	return link_open(uuid, &bt_mesh_pb_gatt, net_idx, addr, attention_duration, NULL,
850 			 LINK_ESTABLISHMENT_TIMEOUT);
851 }
852 #endif
853 
854 #if defined(CONFIG_BT_MESH_RPR_CLI)
bt_mesh_pb_remote_open(struct bt_mesh_rpr_cli * cli,const struct bt_mesh_rpr_node * srv,const uint8_t uuid[16],uint16_t net_idx,uint16_t addr)855 int bt_mesh_pb_remote_open(struct bt_mesh_rpr_cli *cli,
856 			   const struct bt_mesh_rpr_node *srv, const uint8_t uuid[16],
857 			   uint16_t net_idx, uint16_t addr)
858 {
859 	struct pb_remote_ctx ctx = { cli, srv };
860 
861 	return link_open(uuid, &pb_remote_cli, net_idx, addr, 0, &ctx, 0);
862 }
863 
864 /* Remote Provision done where client and server is on same node, skip open link
865  * and sending of reprovision message, just execute reprovisioning on it self.
866  */
reprovision_local_client_server(uint16_t addr)867 static int reprovision_local_client_server(uint16_t addr)
868 {
869 	int err;
870 	const uint8_t *pub_key;
871 	const uint8_t *priv_key = NULL;
872 
873 	if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, LINK_ACTIVE)) {
874 		return -EBUSY;
875 	}
876 
877 	LOG_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x",
878 		provisionee.node->net_idx, bt_mesh_cdb.iv_index, addr);
879 
880 	atomic_set_bit(bt_mesh_prov_link.flags, REPROVISION);
881 	atomic_set_bit(bt_mesh_prov_link.flags, PROVISIONER);
882 	bt_mesh_prov_link.addr = addr;
883 	bt_mesh_prov_link.bearer = &pb_remote_cli;
884 	bt_mesh_prov_link.role = &role_provisioner;
885 	provisionee.net_idx = provisionee.node->net_idx;
886 	provisionee.attention_duration = 0;
887 
888 	if (IS_ENABLED(CONFIG_BT_MESH_PROV_OOB_PUBLIC_KEY) &&
889 		       bt_mesh_prov->public_key_be && bt_mesh_prov->private_key_be) {
890 		LOG_DBG("Use OOB Public and Private key");
891 		pub_key = bt_mesh_prov->public_key_be;
892 		priv_key = bt_mesh_prov->private_key_be;
893 	} else {
894 		pub_key = bt_mesh_pub_key_get();
895 	}
896 
897 	if (!pub_key) {
898 		LOG_ERR("No public key available");
899 		return -ENOEXEC;
900 	}
901 
902 	if (bt_mesh_dhkey_gen(pub_key, priv_key, bt_mesh_prov_link.dhkey)) {
903 		LOG_ERR("Failed to generate DHKey");
904 		return -ENOEXEC;
905 	}
906 	LOG_DBG("DHkey: %s", bt_hex(bt_mesh_prov_link.dhkey, DH_KEY_SIZE));
907 
908 	err = bt_mesh_dev_key(bt_mesh_prov_link.dhkey,
909 			      bt_mesh_prov_link.prov_salt, provisionee.new_dev_key);
910 	if (err) {
911 		LOG_ERR("Unable to generate device key");
912 		return err;
913 	}
914 
915 	bt_mesh_dev_key_cand(provisionee.new_dev_key);
916 	/* Mark the link that was never opened as closed. */
917 	atomic_set_bit(bt_mesh_prov_link.flags, COMPLETE);
918 	bt_mesh_reprovision(addr);
919 	bt_mesh_dev_key_cand_activate();
920 
921 	if (bt_mesh_prov->reprovisioned) {
922 		LOG_DBG("Application reprovisioned callback 0x%04x", bt_mesh_primary_addr());
923 		bt_mesh_prov->reprovisioned(bt_mesh_primary_addr());
924 	}
925 
926 	prov_link_closed(PROV_BEARER_LINK_STATUS_SUCCESS);
927 	return 0;
928 }
929 
bt_mesh_pb_remote_open_node(struct bt_mesh_rpr_cli * cli,struct bt_mesh_rpr_node * srv,uint16_t addr,bool composition_change)930 int bt_mesh_pb_remote_open_node(struct bt_mesh_rpr_cli *cli,
931 				struct bt_mesh_rpr_node *srv, uint16_t addr,
932 				bool composition_change)
933 {
934 	struct pb_remote_ctx ctx = { cli, srv };
935 
936 	if (srv->addr != addr) {
937 		ctx.refresh = BT_MESH_RPR_NODE_REFRESH_ADDR;
938 	} else if (composition_change) {
939 		ctx.refresh = BT_MESH_RPR_NODE_REFRESH_COMPOSITION;
940 	} else {
941 		ctx.refresh = BT_MESH_RPR_NODE_REFRESH_DEVKEY;
942 	}
943 
944 	provisionee.node = bt_mesh_cdb_node_get(srv->addr);
945 	if (!provisionee.node) {
946 		LOG_ERR("No CDB node for 0x%04x", srv->addr);
947 		return -ENOENT;
948 	}
949 
950 	/* Check if server is on same device as client */
951 	if (IS_ENABLED(CONFIG_BT_MESH_RPR_SRV) && bt_mesh_has_addr(srv->addr)) {
952 		return reprovision_local_client_server(addr);
953 	}
954 
955 	return link_open(NULL, &pb_remote_cli, provisionee.node->net_idx, addr,
956 			 0, &ctx, 0);
957 }
958 #endif
959