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 if (atomic_test_bit(bt_mesh_prov_link.flags, REPROVISION)) {
593 bt_mesh_cdb_node_update(node, bt_mesh_prov_link.addr,
594 provisionee.elem_count);
595 }
596
597 err = bt_mesh_cdb_node_key_import(node, provisionee.new_dev_key);
598 if (err) {
599 LOG_ERR("Failed to import node device key");
600 return;
601 }
602
603 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
604 bt_mesh_cdb_node_store(node);
605 }
606
607 provisionee.node = NULL;
608
609 if (bt_mesh_prov->node_added) {
610 bt_mesh_prov->node_added(node->net_idx, node->uuid, node->addr,
611 node->num_elem);
612 }
613 }
614
send_random(void)615 static void send_random(void)
616 {
617 PROV_BUF(rnd, PDU_LEN_RANDOM);
618 uint8_t rand_size = bt_mesh_prov_auth_size_get();
619
620 bt_mesh_prov_buf_init(&rnd, PROV_RANDOM);
621 net_buf_simple_add_mem(&rnd, bt_mesh_prov_link.rand, rand_size);
622
623 if (bt_mesh_prov_send(&rnd, NULL)) {
624 LOG_ERR("Failed to send Provisioning Random");
625 return;
626 }
627
628 bt_mesh_prov_link.expect = PROV_RANDOM;
629 }
630
prov_random(const uint8_t * data)631 static void prov_random(const uint8_t *data)
632 {
633 uint8_t rand_size = bt_mesh_prov_auth_size_get();
634 uint8_t conf_verify[PROV_AUTH_MAX_LEN];
635
636 LOG_DBG("Remote Random: %s", bt_hex(data, rand_size));
637 if (!memcmp(data, bt_mesh_prov_link.rand, rand_size)) {
638 LOG_ERR("Random value is identical to ours, rejecting.");
639 prov_fail(PROV_ERR_CFM_FAILED);
640 return;
641 }
642
643 if (bt_mesh_prov_conf(bt_mesh_prov_link.algorithm, bt_mesh_prov_link.conf_key,
644 data, bt_mesh_prov_link.auth, conf_verify)) {
645 LOG_ERR("Unable to calculate confirmation verification");
646 prov_fail(PROV_ERR_UNEXP_ERR);
647 return;
648 }
649
650 if (memcmp(conf_verify, bt_mesh_prov_link.conf, rand_size)) {
651 LOG_ERR("Invalid confirmation value");
652 LOG_DBG("Received: %s", bt_hex(bt_mesh_prov_link.conf, rand_size));
653 LOG_DBG("Calculated: %s", bt_hex(conf_verify, rand_size));
654 prov_fail(PROV_ERR_CFM_FAILED);
655 return;
656 }
657
658 if (bt_mesh_prov_salt(bt_mesh_prov_link.algorithm, bt_mesh_prov_link.conf_salt,
659 bt_mesh_prov_link.rand, data, bt_mesh_prov_link.prov_salt)) {
660 LOG_ERR("Failed to generate provisioning salt");
661 prov_fail(PROV_ERR_UNEXP_ERR);
662 return;
663 }
664
665 LOG_DBG("ProvisioningSalt: %s", bt_hex(bt_mesh_prov_link.prov_salt, 16));
666
667 send_prov_data();
668 }
669
prov_confirm(const uint8_t * data)670 static void prov_confirm(const uint8_t *data)
671 {
672 uint8_t conf_size = bt_mesh_prov_auth_size_get();
673
674 LOG_DBG("Remote Confirm: %s", bt_hex(data, conf_size));
675
676 if (!memcmp(data, bt_mesh_prov_link.conf, conf_size)) {
677 LOG_ERR("Confirm value is identical to ours, rejecting.");
678 prov_fail(PROV_ERR_CFM_FAILED);
679 return;
680 }
681
682 memcpy(bt_mesh_prov_link.conf, data, conf_size);
683
684 send_random();
685 }
686
prov_failed(const uint8_t * data)687 static void prov_failed(const uint8_t *data)
688 {
689 LOG_WRN("Error: 0x%02x", data[0]);
690 reset_state();
691 }
692
local_input_complete(void)693 static void local_input_complete(void)
694 {
695 if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM)) {
696 send_confirm();
697 }
698 }
699
prov_link_closed(enum prov_bearer_link_status status)700 static void prov_link_closed(enum prov_bearer_link_status status)
701 {
702 LOG_DBG("");
703 if (atomic_test_bit(bt_mesh_prov_link.flags, COMPLETE)) {
704 prov_node_add();
705 }
706
707 reset_state();
708 }
709
prov_link_opened(void)710 static void prov_link_opened(void)
711 {
712 send_invite();
713 }
714
715 static const struct bt_mesh_prov_role role_provisioner = {
716 .input_complete = local_input_complete,
717 .link_opened = prov_link_opened,
718 .link_closed = prov_link_closed,
719 .error = prov_fail,
720 .op = {
721 [PROV_CAPABILITIES] = prov_capabilities,
722 [PROV_PUB_KEY] = prov_pub_key,
723 [PROV_INPUT_COMPLETE] = prov_input_complete,
724 [PROV_CONFIRM] = prov_confirm,
725 [PROV_RANDOM] = prov_random,
726 [PROV_COMPLETE] = prov_complete,
727 [PROV_FAILED] = prov_failed,
728 },
729 };
730
prov_set_method(uint8_t method,uint8_t action,uint8_t size)731 static void prov_set_method(uint8_t method, uint8_t action, uint8_t size)
732 {
733 bt_mesh_prov_link.oob_method = method;
734 bt_mesh_prov_link.oob_action = action;
735 bt_mesh_prov_link.oob_size = size;
736 }
737
bt_mesh_auth_method_set_input(bt_mesh_input_action_t action,uint8_t size)738 int bt_mesh_auth_method_set_input(bt_mesh_input_action_t action, uint8_t size)
739 {
740 if (!action || !size || size > PROV_IO_OOB_SIZE_MAX) {
741 return -EINVAL;
742 }
743
744 prov_set_method(AUTH_METHOD_INPUT, find_msb_set(action) - 1, size);
745 return 0;
746 }
747
bt_mesh_auth_method_set_output(bt_mesh_output_action_t action,uint8_t size)748 int bt_mesh_auth_method_set_output(bt_mesh_output_action_t action, uint8_t size)
749 {
750 if (!action || !size || size > PROV_IO_OOB_SIZE_MAX) {
751 return -EINVAL;
752 }
753
754 prov_set_method(AUTH_METHOD_OUTPUT, find_msb_set(action) - 1, size);
755 return 0;
756 }
757
bt_mesh_auth_method_set_static(const uint8_t * static_val,uint8_t size)758 int bt_mesh_auth_method_set_static(const uint8_t *static_val, uint8_t size)
759 {
760 if (!size || !static_val) {
761 return -EINVAL;
762 }
763
764 prov_set_method(AUTH_METHOD_STATIC, 0, 0);
765
766 /* Trim the Auth if it is longer than required length */
767 memcpy(bt_mesh_prov_link.auth, static_val,
768 size > PROV_AUTH_MAX_LEN ? PROV_AUTH_MAX_LEN : size);
769
770 /* Pad with zeros if the Auth is shorter the required length */
771 if (size < PROV_AUTH_MAX_LEN) {
772 memset(bt_mesh_prov_link.auth + size, 0, PROV_AUTH_MAX_LEN - size);
773 }
774
775 return 0;
776 }
777
bt_mesh_auth_method_set_none(void)778 int bt_mesh_auth_method_set_none(void)
779 {
780 prov_set_method(AUTH_METHOD_NO_OOB, 0, 0);
781 return 0;
782 }
783
bt_mesh_prov_remote_pub_key_set(const uint8_t public_key[PUB_KEY_SIZE])784 int bt_mesh_prov_remote_pub_key_set(const uint8_t public_key[PUB_KEY_SIZE])
785 {
786 if (public_key == NULL) {
787 return -EINVAL;
788 }
789
790 if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY)) {
791 return -EALREADY;
792 }
793
794 memcpy(bt_mesh_prov_link.conf_inputs.pub_key_device, public_key, PDU_LEN_PUB_KEY);
795
796 return 0;
797 }
798
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)799 static int link_open(const uint8_t *uuid, const struct prov_bearer *bearer,
800 uint16_t net_idx, uint16_t addr,
801 uint8_t attention_duration, void *bearer_cb_data, uint8_t timeout)
802 {
803 int err;
804
805 if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, LINK_ACTIVE)) {
806 return -EBUSY;
807 }
808
809 if (uuid) {
810 memcpy(provisionee.uuid, uuid, 16);
811
812 struct bt_uuid_128 uuid_repr = { .uuid = { BT_UUID_TYPE_128 } };
813
814 memcpy(uuid_repr.val, uuid, 16);
815 LOG_DBG("Provisioning %s", bt_uuid_str(&uuid_repr.uuid));
816
817 } else {
818 atomic_set_bit(bt_mesh_prov_link.flags, REPROVISION);
819 LOG_DBG("Reprovisioning");
820 }
821
822 atomic_set_bit(bt_mesh_prov_link.flags, PROVISIONER);
823 bt_mesh_prov_link.addr = addr;
824 bt_mesh_prov_link.bearer = bearer;
825 bt_mesh_prov_link.role = &role_provisioner;
826 provisionee.net_idx = net_idx;
827 provisionee.attention_duration = attention_duration;
828
829 err = bt_mesh_prov_link.bearer->link_open(
830 uuid, timeout, bt_mesh_prov_bearer_cb_get(), bearer_cb_data);
831 if (err) {
832 atomic_clear_bit(bt_mesh_prov_link.flags, LINK_ACTIVE);
833 }
834
835 return err;
836 }
837
838 #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)839 int bt_mesh_pb_adv_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
840 uint8_t attention_duration)
841 {
842 return link_open(uuid, &bt_mesh_pb_adv, net_idx, addr, attention_duration, NULL,
843 LINK_ESTABLISHMENT_TIMEOUT);
844 }
845 #endif
846
847 #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)848 int bt_mesh_pb_gatt_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
849 uint8_t attention_duration)
850 {
851 return link_open(uuid, &bt_mesh_pb_gatt, net_idx, addr, attention_duration, NULL,
852 LINK_ESTABLISHMENT_TIMEOUT);
853 }
854 #endif
855
856 #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)857 int bt_mesh_pb_remote_open(struct bt_mesh_rpr_cli *cli,
858 const struct bt_mesh_rpr_node *srv, const uint8_t uuid[16],
859 uint16_t net_idx, uint16_t addr)
860 {
861 struct pb_remote_ctx ctx = { cli, srv };
862
863 return link_open(uuid, &pb_remote_cli, net_idx, addr, 0, &ctx, 0);
864 }
865
866 /* Remote Provision done where client and server is on same node, skip open link
867 * and sending of reprovision message, just execute reprovisioning on it self.
868 */
reprovision_local_client_server(uint16_t addr)869 static int reprovision_local_client_server(uint16_t addr)
870 {
871 int err;
872 const uint8_t *pub_key;
873 const uint8_t *priv_key = NULL;
874
875 if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, LINK_ACTIVE)) {
876 return -EBUSY;
877 }
878
879 LOG_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x",
880 provisionee.node->net_idx, bt_mesh_cdb.iv_index, addr);
881
882 atomic_set_bit(bt_mesh_prov_link.flags, REPROVISION);
883 atomic_set_bit(bt_mesh_prov_link.flags, PROVISIONER);
884 bt_mesh_prov_link.addr = addr;
885 bt_mesh_prov_link.bearer = &pb_remote_cli;
886 bt_mesh_prov_link.role = &role_provisioner;
887 provisionee.net_idx = provisionee.node->net_idx;
888 provisionee.attention_duration = 0;
889
890 if (IS_ENABLED(CONFIG_BT_MESH_PROV_OOB_PUBLIC_KEY) &&
891 bt_mesh_prov->public_key_be && bt_mesh_prov->private_key_be) {
892 LOG_DBG("Use OOB Public and Private key");
893 pub_key = bt_mesh_prov->public_key_be;
894 priv_key = bt_mesh_prov->private_key_be;
895 } else {
896 pub_key = bt_mesh_pub_key_get();
897 }
898
899 if (!pub_key) {
900 LOG_ERR("No public key available");
901 return -ENOEXEC;
902 }
903
904 if (bt_mesh_dhkey_gen(pub_key, priv_key, bt_mesh_prov_link.dhkey)) {
905 LOG_ERR("Failed to generate DHKey");
906 return -ENOEXEC;
907 }
908 LOG_DBG("DHkey: %s", bt_hex(bt_mesh_prov_link.dhkey, DH_KEY_SIZE));
909
910 err = bt_mesh_dev_key(bt_mesh_prov_link.dhkey,
911 bt_mesh_prov_link.prov_salt, provisionee.new_dev_key);
912 if (err) {
913 LOG_ERR("Unable to generate device key");
914 return err;
915 }
916
917 bt_mesh_dev_key_cand(provisionee.new_dev_key);
918 /* Mark the link that was never opened as closed. */
919 atomic_set_bit(bt_mesh_prov_link.flags, COMPLETE);
920 bt_mesh_reprovision(addr);
921 bt_mesh_dev_key_cand_activate();
922
923 if (bt_mesh_prov->reprovisioned) {
924 LOG_DBG("Application reprovisioned callback 0x%04x", bt_mesh_primary_addr());
925 bt_mesh_prov->reprovisioned(bt_mesh_primary_addr());
926 }
927
928 prov_link_closed(PROV_BEARER_LINK_STATUS_SUCCESS);
929 return 0;
930 }
931
bt_mesh_pb_remote_open_node(struct bt_mesh_rpr_cli * cli,struct bt_mesh_rpr_node * srv,uint16_t addr,bool composition_change)932 int bt_mesh_pb_remote_open_node(struct bt_mesh_rpr_cli *cli,
933 struct bt_mesh_rpr_node *srv, uint16_t addr,
934 bool composition_change)
935 {
936 struct pb_remote_ctx ctx = { cli, srv };
937
938 if (srv->addr != addr) {
939 ctx.refresh = BT_MESH_RPR_NODE_REFRESH_ADDR;
940 } else if (composition_change) {
941 ctx.refresh = BT_MESH_RPR_NODE_REFRESH_COMPOSITION;
942 } else {
943 ctx.refresh = BT_MESH_RPR_NODE_REFRESH_DEVKEY;
944 }
945
946 provisionee.node = bt_mesh_cdb_node_get(srv->addr);
947 if (!provisionee.node) {
948 LOG_ERR("No CDB node for 0x%04x", srv->addr);
949 return -ENOENT;
950 }
951
952 /* Check if server is on same device as client */
953 if (IS_ENABLED(CONFIG_BT_MESH_RPR_SRV) && bt_mesh_has_addr(srv->addr)) {
954 return reprovision_local_client_server(addr);
955 }
956
957 return link_open(NULL, &pb_remote_cli, provisionee.node->net_idx, addr,
958 0, &ctx, 0);
959 }
960 #endif
961