smp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013-2014 Intel Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <endian.h>
  20. #include <stdbool.h>
  21. #include <sys/socket.h>
  22. #include "lib/bluetooth.h"
  23. #include "lib/hci.h"
  24. #include "src/shared/util.h"
  25. #include "src/shared/crypto.h"
  26. #include "src/shared/ecc.h"
  27. #include "monitor/bt.h"
  28. #include "bthost.h"
  29. #define SMP_CID 0x0006
  30. #define SMP_BREDR_CID 0x0007
  31. #define L2CAP_FC_SMP_BREDR 0x80
  32. #define SMP_PASSKEY_ENTRY_FAILED 0x01
  33. #define SMP_OOB_NOT_AVAIL 0x02
  34. #define SMP_AUTH_REQUIREMENTS 0x03
  35. #define SMP_CONFIRM_FAILED 0x04
  36. #define SMP_PAIRING_NOTSUPP 0x05
  37. #define SMP_ENC_KEY_SIZE 0x06
  38. #define SMP_CMD_NOTSUPP 0x07
  39. #define SMP_UNSPECIFIED 0x08
  40. #define SMP_REPEATED_ATTEMPTS 0x09
  41. #define SMP_INVALID_PARAMS 0x0a
  42. #define SMP_DHKEY_CHECK_FAILED 0x0b
  43. #define SMP_NUMERIC_COMP_FAILED 0x0c
  44. #define SMP_BREDR_PAIRING_IN_PROGRESS 0x0d
  45. #define DIST_ENC_KEY 0x01
  46. #define DIST_ID_KEY 0x02
  47. #define DIST_SIGN 0x04
  48. #define DIST_LINK_KEY 0x08
  49. #define SC_NO_DIST (DIST_ENC_KEY | DIST_LINK_KEY)
  50. #define MAX_IO_CAP 0x04
  51. #define SMP_AUTH_NONE 0x00
  52. #define SMP_AUTH_BONDING 0x01
  53. #define SMP_AUTH_MITM 0x04
  54. #define SMP_AUTH_SC 0x08
  55. #define SMP_AUTH_KEYPRESS 0x10
  56. struct smp {
  57. struct bthost *bthost;
  58. struct smp_conn *conn;
  59. struct bt_crypto *crypto;
  60. };
  61. struct smp_conn {
  62. struct smp *smp;
  63. uint16_t handle;
  64. uint8_t addr_type;
  65. bool out;
  66. bool sc;
  67. bool initiator;
  68. uint8_t method;
  69. uint8_t local_key_dist;
  70. uint8_t remote_key_dist;
  71. uint8_t ia[6];
  72. uint8_t ia_type;
  73. uint8_t ra[6];
  74. uint8_t ra_type;
  75. uint8_t tk[16];
  76. uint8_t prnd[16];
  77. uint8_t rrnd[16];
  78. uint8_t pcnf[16];
  79. uint8_t preq[7];
  80. uint8_t prsp[7];
  81. uint8_t ltk[16];
  82. uint8_t local_sk[32];
  83. uint8_t local_pk[64];
  84. uint8_t remote_pk[64];
  85. uint8_t dhkey[32];
  86. uint8_t mackey[16];
  87. uint8_t passkey_notify;
  88. uint8_t passkey_round;
  89. };
  90. enum {
  91. JUST_WORKS,
  92. JUST_CFM,
  93. REQ_PASSKEY,
  94. CFM_PASSKEY,
  95. REQ_OOB,
  96. DSP_PASSKEY,
  97. OVERLAP,
  98. };
  99. static const uint8_t gen_method[5][5] = {
  100. { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
  101. { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
  102. { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
  103. { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
  104. { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
  105. };
  106. static const uint8_t sc_method[5][5] = {
  107. { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
  108. { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
  109. { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
  110. { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
  111. { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
  112. };
  113. static uint8_t get_auth_method(struct smp_conn *conn, uint8_t local_io,
  114. uint8_t remote_io)
  115. {
  116. /* If either side has unknown io_caps, use JUST_CFM (which gets
  117. * converted later to JUST_WORKS if we're initiators.
  118. */
  119. if (local_io > MAX_IO_CAP || remote_io > MAX_IO_CAP)
  120. return JUST_CFM;
  121. if (conn->sc)
  122. return sc_method[remote_io][local_io];
  123. return gen_method[remote_io][local_io];
  124. }
  125. static uint8_t sc_select_method(struct smp_conn *conn)
  126. {
  127. struct bt_l2cap_smp_pairing_request *local, *remote;
  128. uint8_t local_mitm, remote_mitm, local_io, remote_io, method;
  129. if (conn->out) {
  130. local = (void *) &conn->preq[1];
  131. remote = (void *) &conn->prsp[1];
  132. } else {
  133. local = (void *) &conn->prsp[1];
  134. remote = (void *) &conn->preq[1];
  135. }
  136. local_io = local->io_capa;
  137. remote_io = remote->io_capa;
  138. local_mitm = (local->auth_req & SMP_AUTH_MITM);
  139. remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
  140. /* If either side wants MITM, look up the method from the table,
  141. * otherwise use JUST WORKS.
  142. */
  143. if (local_mitm || remote_mitm)
  144. method = get_auth_method(conn, local_io, remote_io);
  145. else
  146. method = JUST_WORKS;
  147. /* Don't confirm locally initiated pairing attempts */
  148. if (method == JUST_CFM && conn->initiator)
  149. method = JUST_WORKS;
  150. return method;
  151. }
  152. static uint8_t key_dist(struct bthost *host)
  153. {
  154. if (!bthost_bredr_capable(host))
  155. return (DIST_ENC_KEY | DIST_ID_KEY | DIST_SIGN);
  156. return (DIST_ENC_KEY | DIST_ID_KEY | DIST_SIGN | DIST_LINK_KEY);
  157. }
  158. static void smp_send(struct smp_conn *conn, uint8_t smp_cmd, const void *data,
  159. uint8_t len)
  160. {
  161. struct iovec iov[2];
  162. uint16_t cid;
  163. iov[0].iov_base = &smp_cmd;
  164. iov[0].iov_len = 1;
  165. iov[1].iov_base = (void *) data;
  166. iov[1].iov_len = len;
  167. if (conn->addr_type == BDADDR_BREDR)
  168. cid = SMP_BREDR_CID;
  169. else
  170. cid = SMP_CID;
  171. bthost_send_cid_v(conn->smp->bthost, conn->handle, cid, iov, 2);
  172. }
  173. static bool send_public_key(struct smp_conn *conn)
  174. {
  175. if (!ecc_make_key(conn->local_pk, conn->local_sk))
  176. return false;
  177. smp_send(conn, BT_L2CAP_SMP_PUBLIC_KEY, conn->local_pk, 64);
  178. return true;
  179. }
  180. static void sc_dhkey_check(struct smp_conn *conn)
  181. {
  182. uint8_t io_cap[3], r[16], a[7], b[7], *local_addr, *remote_addr;
  183. struct bt_l2cap_smp_dhkey_check check;
  184. memcpy(a, conn->ia, 6);
  185. memcpy(b, conn->ra, 6);
  186. a[6] = conn->ia_type;
  187. b[6] = conn->ra_type;
  188. if (conn->out) {
  189. local_addr = a;
  190. remote_addr = b;
  191. memcpy(io_cap, &conn->preq[1], 3);
  192. } else {
  193. local_addr = b;
  194. remote_addr = a;
  195. memcpy(io_cap, &conn->prsp[1], 3);
  196. }
  197. memset(r, 0, sizeof(r));
  198. bt_crypto_f6(conn->smp->crypto, conn->mackey, conn->prnd, conn->rrnd,
  199. r, io_cap, local_addr, remote_addr, check.e);
  200. smp_send(conn, BT_L2CAP_SMP_DHKEY_CHECK, &check, sizeof(check));
  201. }
  202. static void sc_mackey_and_ltk(struct smp_conn *conn)
  203. {
  204. uint8_t *na, *nb, a[7], b[7];
  205. if (conn->out) {
  206. na = conn->prnd;
  207. nb = conn->rrnd;
  208. } else {
  209. na = conn->rrnd;
  210. nb = conn->prnd;
  211. }
  212. memcpy(a, conn->ia, 6);
  213. memcpy(b, conn->ra, 6);
  214. a[6] = conn->ia_type;
  215. b[6] = conn->ra_type;
  216. bt_crypto_f5(conn->smp->crypto, conn->dhkey, na, nb, a, b,
  217. conn->mackey, conn->ltk);
  218. }
  219. static uint8_t sc_passkey_send_confirm(struct smp_conn *conn)
  220. {
  221. struct bt_l2cap_smp_pairing_confirm cfm;
  222. uint8_t r;
  223. r = ((conn->passkey_notify >> conn->passkey_round) & 0x01);
  224. r |= 0x80;
  225. if (!bt_crypto_f4(conn->smp->crypto, conn->local_pk, conn->remote_pk,
  226. conn->prnd, r, cfm.value))
  227. return SMP_UNSPECIFIED;
  228. smp_send(conn, BT_L2CAP_SMP_PAIRING_CONFIRM, &cfm, sizeof(cfm));
  229. return 0;
  230. }
  231. static uint8_t sc_passkey_round(struct smp_conn *conn, uint8_t smp_op)
  232. {
  233. uint8_t cfm[16], r;
  234. /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
  235. if (conn->passkey_round >= 20)
  236. return 0;
  237. switch (smp_op) {
  238. case BT_L2CAP_SMP_PAIRING_RANDOM:
  239. r = ((conn->passkey_notify >> conn->passkey_round) & 0x01);
  240. r |= 0x80;
  241. if (!bt_crypto_f4(conn->smp->crypto, conn->remote_pk,
  242. conn->local_pk, conn->rrnd, r, cfm))
  243. return SMP_UNSPECIFIED;
  244. if (memcmp(conn->pcnf, cfm, 16))
  245. return SMP_CONFIRM_FAILED;
  246. conn->passkey_round++;
  247. if (conn->passkey_round == 20) {
  248. /* Generate MacKey and LTK */
  249. sc_mackey_and_ltk(conn);
  250. }
  251. /* The round is only complete when the initiator
  252. * receives pairing random.
  253. */
  254. if (!conn->out) {
  255. smp_send(conn, BT_L2CAP_SMP_PAIRING_RANDOM,
  256. conn->prnd, sizeof(conn->prnd));
  257. return 0;
  258. }
  259. /* Start the next round */
  260. if (conn->passkey_round != 20)
  261. return sc_passkey_round(conn, 0);
  262. /* Passkey rounds are complete - start DHKey Check */
  263. sc_dhkey_check(conn);
  264. break;
  265. case BT_L2CAP_SMP_PAIRING_CONFIRM:
  266. if (conn->out) {
  267. smp_send(conn, BT_L2CAP_SMP_PAIRING_RANDOM,
  268. conn->prnd, sizeof(conn->prnd));
  269. return 0;
  270. }
  271. return sc_passkey_send_confirm(conn);
  272. case BT_L2CAP_SMP_PUBLIC_KEY:
  273. default:
  274. /* Initiating device starts the round */
  275. if (!conn->out)
  276. return 0;
  277. return sc_passkey_send_confirm(conn);
  278. }
  279. return 0;
  280. }
  281. static bool verify_random(struct smp_conn *conn, const uint8_t rnd[16])
  282. {
  283. uint8_t confirm[16];
  284. if (!bt_crypto_c1(conn->smp->crypto, conn->tk, conn->rrnd, conn->prsp,
  285. conn->preq, conn->ia_type, conn->ia,
  286. conn->ra_type, conn->ra, confirm))
  287. return false;
  288. if (memcmp(conn->pcnf, confirm, sizeof(conn->pcnf)) != 0) {
  289. bthost_debug(conn->smp->bthost,
  290. "Confirmation values don't match");
  291. return false;
  292. }
  293. if (conn->out) {
  294. bt_crypto_s1(conn->smp->crypto, conn->tk, conn->rrnd,
  295. conn->prnd, conn->ltk);
  296. bthost_le_start_encrypt(conn->smp->bthost, conn->handle,
  297. conn->ltk);
  298. } else {
  299. bt_crypto_s1(conn->smp->crypto, conn->tk, conn->prnd,
  300. conn->rrnd, conn->ltk);
  301. }
  302. return true;
  303. }
  304. static void distribute_keys(struct smp_conn *conn)
  305. {
  306. uint8_t buf[16];
  307. if (conn->local_key_dist & DIST_ENC_KEY) {
  308. memset(buf, 0, sizeof(buf));
  309. smp_send(conn, BT_L2CAP_SMP_ENCRYPT_INFO, buf, sizeof(buf));
  310. smp_send(conn, BT_L2CAP_SMP_CENTRAL_IDENT, buf, 10);
  311. }
  312. if (conn->local_key_dist & DIST_ID_KEY) {
  313. memset(buf, 0, sizeof(buf));
  314. smp_send(conn, BT_L2CAP_SMP_IDENT_INFO, buf, sizeof(buf));
  315. memset(buf, 0, sizeof(buf));
  316. if (conn->out) {
  317. buf[0] = conn->ia_type;
  318. memcpy(&buf[1], conn->ia, 6);
  319. } else {
  320. buf[0] = conn->ra_type;
  321. memcpy(&buf[1], conn->ra, 6);
  322. }
  323. smp_send(conn, BT_L2CAP_SMP_IDENT_ADDR_INFO, buf, 7);
  324. }
  325. if (conn->local_key_dist & DIST_SIGN) {
  326. memset(buf, 0, sizeof(buf));
  327. smp_send(conn, BT_L2CAP_SMP_SIGNING_INFO, buf, sizeof(buf));
  328. }
  329. }
  330. static void pairing_req(struct smp_conn *conn, const void *data, uint16_t len)
  331. {
  332. struct bthost *bthost = conn->smp->bthost;
  333. struct bt_l2cap_smp_pairing_response rsp;
  334. memcpy(conn->preq, data, sizeof(conn->preq));
  335. if (conn->addr_type == BDADDR_BREDR) {
  336. rsp.io_capa = 0x00;
  337. rsp.oob_data = 0x00;
  338. rsp.auth_req = 0x00;
  339. } else {
  340. rsp.io_capa = bthost_get_io_capability(bthost);
  341. rsp.oob_data = 0x00;
  342. rsp.auth_req = bthost_get_auth_req(bthost);
  343. }
  344. rsp.max_key_size = 0x10;
  345. rsp.init_key_dist = conn->preq[5] & key_dist(bthost);
  346. rsp.resp_key_dist = conn->preq[6] & key_dist(bthost);
  347. conn->prsp[0] = BT_L2CAP_SMP_PAIRING_RESPONSE;
  348. memcpy(&conn->prsp[1], &rsp, sizeof(rsp));
  349. conn->local_key_dist = rsp.resp_key_dist;
  350. conn->remote_key_dist = rsp.init_key_dist;
  351. if (((conn->prsp[3] & 0x08) && (conn->preq[3] & 0x08)) ||
  352. conn->addr_type == BDADDR_BREDR) {
  353. conn->sc = true;
  354. conn->local_key_dist &= ~SC_NO_DIST;
  355. conn->remote_key_dist &= ~SC_NO_DIST;
  356. }
  357. smp_send(conn, BT_L2CAP_SMP_PAIRING_RESPONSE, &rsp, sizeof(rsp));
  358. if (conn->addr_type == BDADDR_BREDR)
  359. distribute_keys(conn);
  360. }
  361. static void pairing_rsp(struct smp_conn *conn, const void *data, uint16_t len)
  362. {
  363. struct smp *smp = conn->smp;
  364. uint8_t cfm[16];
  365. memcpy(conn->prsp, data, sizeof(conn->prsp));
  366. conn->local_key_dist = conn->prsp[5];
  367. conn->remote_key_dist = conn->prsp[6];
  368. if (conn->addr_type == BDADDR_BREDR) {
  369. conn->local_key_dist &= ~SC_NO_DIST;
  370. conn->remote_key_dist &= ~SC_NO_DIST;
  371. distribute_keys(conn);
  372. return;
  373. }
  374. if (((conn->prsp[3] & 0x08) && (conn->preq[3] & 0x08)) ||
  375. conn->addr_type == BDADDR_BREDR) {
  376. conn->sc = true;
  377. conn->local_key_dist &= ~SC_NO_DIST;
  378. conn->remote_key_dist &= ~SC_NO_DIST;
  379. if (conn->addr_type == BDADDR_BREDR)
  380. distribute_keys(conn);
  381. else
  382. send_public_key(conn);
  383. return;
  384. }
  385. bt_crypto_c1(smp->crypto, conn->tk, conn->prnd, conn->prsp,
  386. conn->preq, conn->ia_type, conn->ia,
  387. conn->ra_type, conn->ra, cfm);
  388. smp_send(conn, BT_L2CAP_SMP_PAIRING_CONFIRM, cfm, sizeof(cfm));
  389. }
  390. static void sc_check_confirm(struct smp_conn *conn)
  391. {
  392. if (conn->method == REQ_PASSKEY || conn->method == DSP_PASSKEY) {
  393. sc_passkey_round(conn, BT_L2CAP_SMP_PAIRING_CONFIRM);
  394. return;
  395. }
  396. if (conn->out)
  397. smp_send(conn, BT_L2CAP_SMP_PAIRING_RANDOM, conn->prnd,
  398. sizeof(conn->prnd));
  399. }
  400. static void pairing_cfm(struct smp_conn *conn, const void *data, uint16_t len)
  401. {
  402. uint8_t rsp[16];
  403. memcpy(conn->pcnf, data + 1, 16);
  404. if (conn->sc) {
  405. sc_check_confirm(conn);
  406. return;
  407. }
  408. if (conn->out) {
  409. memset(rsp, 0, sizeof(rsp));
  410. smp_send(conn, BT_L2CAP_SMP_PAIRING_RANDOM, conn->prnd,
  411. sizeof(conn->prnd));
  412. } else {
  413. bt_crypto_c1(conn->smp->crypto, conn->tk, conn->prnd,
  414. conn->prsp, conn->preq, conn->ia_type,
  415. conn->ia, conn->ra_type, conn->ra, rsp);
  416. smp_send(conn, BT_L2CAP_SMP_PAIRING_CONFIRM, rsp, sizeof(rsp));
  417. }
  418. }
  419. static uint8_t sc_random(struct smp_conn *conn)
  420. {
  421. /* Passkey entry has special treatment */
  422. if (conn->method == REQ_PASSKEY || conn->method == DSP_PASSKEY)
  423. return sc_passkey_round(conn, BT_L2CAP_SMP_PAIRING_RANDOM);
  424. if (conn->out) {
  425. uint8_t cfm[16];
  426. bt_crypto_f4(conn->smp->crypto, conn->remote_pk,
  427. conn->local_pk, conn->rrnd, 0, cfm);
  428. if (memcmp(conn->pcnf, cfm, 16))
  429. return 0x04; /* Confirm Value Failed */
  430. } else {
  431. smp_send(conn, BT_L2CAP_SMP_PAIRING_RANDOM, conn->prnd, 16);
  432. }
  433. sc_mackey_and_ltk(conn);
  434. if (conn->out)
  435. sc_dhkey_check(conn);
  436. return 0;
  437. }
  438. static void pairing_rnd(struct smp_conn *conn, const void *data, uint16_t len)
  439. {
  440. memcpy(conn->rrnd, data + 1, 16);
  441. if (conn->sc) {
  442. uint8_t reason = sc_random(conn);
  443. if (reason)
  444. smp_send(conn, BT_L2CAP_SMP_PAIRING_FAILED, &reason,
  445. sizeof(reason));
  446. return;
  447. }
  448. if (!verify_random(conn, data + 1))
  449. return;
  450. if (conn->out)
  451. return;
  452. smp_send(conn, BT_L2CAP_SMP_PAIRING_RANDOM, conn->prnd,
  453. sizeof(conn->prnd));
  454. }
  455. static void encrypt_info(struct smp_conn *conn, const void *data, uint16_t len)
  456. {
  457. }
  458. static void central_ident(struct smp_conn *conn, const void *data, uint16_t len)
  459. {
  460. conn->remote_key_dist &= ~DIST_ENC_KEY;
  461. if (conn->out && !conn->remote_key_dist)
  462. distribute_keys(conn);
  463. }
  464. static void ident_addr_info(struct smp_conn *conn, const void *data,
  465. uint16_t len)
  466. {
  467. }
  468. static void ident_info(struct smp_conn *conn, const void *data, uint16_t len)
  469. {
  470. conn->remote_key_dist &= ~DIST_ID_KEY;
  471. if (conn->out && !conn->remote_key_dist)
  472. distribute_keys(conn);
  473. }
  474. static void signing_info(struct smp_conn *conn, const void *data, uint16_t len)
  475. {
  476. conn->remote_key_dist &= ~DIST_SIGN;
  477. if (conn->out && !conn->remote_key_dist)
  478. distribute_keys(conn);
  479. }
  480. static void public_key(struct smp_conn *conn, const void *data, uint16_t len)
  481. {
  482. struct smp *smp = conn->smp;
  483. uint8_t buf[16];
  484. memcpy(conn->remote_pk, data + 1, 64);
  485. if (!conn->out) {
  486. if (!send_public_key(conn))
  487. return;
  488. }
  489. if (!ecdh_shared_secret(conn->remote_pk, conn->local_sk, conn->dhkey))
  490. return;
  491. conn->method = sc_select_method(conn);
  492. if (conn->method == DSP_PASSKEY || conn->method == REQ_PASSKEY) {
  493. sc_passkey_round(conn, BT_L2CAP_SMP_PUBLIC_KEY);
  494. return;
  495. }
  496. if (conn->out)
  497. return;
  498. if (!bt_crypto_f4(smp->crypto, conn->local_pk, conn->remote_pk,
  499. conn->prnd, 0, buf))
  500. return;
  501. smp_send(conn, BT_L2CAP_SMP_PAIRING_CONFIRM, buf, sizeof(buf));
  502. }
  503. static void dhkey_check(struct smp_conn *conn, const void *data, uint16_t len)
  504. {
  505. const struct bt_l2cap_smp_dhkey_check *cmd = data + 1;
  506. uint8_t a[7], b[7], *local_addr, *remote_addr;
  507. uint8_t io_cap[3], r[16], e[16];
  508. memcpy(a, &conn->ia, 6);
  509. memcpy(b, &conn->ra, 6);
  510. a[6] = conn->ia_type;
  511. b[6] = conn->ra_type;
  512. if (conn->out) {
  513. local_addr = a;
  514. remote_addr = b;
  515. memcpy(io_cap, &conn->prsp[1], 3);
  516. } else {
  517. local_addr = b;
  518. remote_addr = a;
  519. memcpy(io_cap, &conn->preq[1], 3);
  520. }
  521. memset(r, 0, sizeof(r));
  522. if (conn->method == REQ_PASSKEY || conn->method == DSP_PASSKEY)
  523. put_le32(conn->passkey_notify, r);
  524. if (!bt_crypto_f6(conn->smp->crypto, conn->mackey, conn->rrnd,
  525. conn->prnd, r, io_cap, remote_addr, local_addr, e))
  526. return;
  527. if (memcmp(cmd->e, e, 16)) {
  528. uint8_t reason = 0x0b; /* DHKey Check Failed */
  529. smp_send(conn, BT_L2CAP_SMP_PAIRING_FAILED, &reason,
  530. sizeof(reason));
  531. }
  532. if (conn->out)
  533. bthost_le_start_encrypt(conn->smp->bthost, conn->handle,
  534. conn->ltk);
  535. else
  536. sc_dhkey_check(conn);
  537. }
  538. void smp_pair(void *conn_data, uint8_t io_cap, uint8_t auth_req)
  539. {
  540. struct smp_conn *conn = conn_data;
  541. struct bt_l2cap_smp_pairing_request req;
  542. req.io_capa = io_cap;
  543. req.oob_data = 0x00;
  544. req.auth_req = auth_req;
  545. req.max_key_size = 0x10;
  546. req.init_key_dist = key_dist(conn->smp->bthost);
  547. req.resp_key_dist = key_dist(conn->smp->bthost);
  548. conn->preq[0] = BT_L2CAP_SMP_PAIRING_REQUEST;
  549. memcpy(&conn->preq[1], &req, sizeof(req));
  550. smp_send(conn, BT_L2CAP_SMP_PAIRING_REQUEST, &req, sizeof(req));
  551. }
  552. void smp_data(void *conn_data, const void *data, uint16_t len)
  553. {
  554. struct smp_conn *conn = conn_data;
  555. uint8_t opcode;
  556. if (len < 1) {
  557. bthost_debug(conn->smp->bthost, "Received too small SMP PDU");
  558. return;
  559. }
  560. if (conn->addr_type == BDADDR_BREDR) {
  561. bthost_debug(conn->smp->bthost,
  562. "Received BR/EDR SMP data on LE link");
  563. return;
  564. }
  565. opcode = *((const uint8_t *) data);
  566. switch (opcode) {
  567. case BT_L2CAP_SMP_PAIRING_REQUEST:
  568. pairing_req(conn, data, len);
  569. break;
  570. case BT_L2CAP_SMP_PAIRING_RESPONSE:
  571. pairing_rsp(conn, data, len);
  572. break;
  573. case BT_L2CAP_SMP_PAIRING_CONFIRM:
  574. pairing_cfm(conn, data, len);
  575. break;
  576. case BT_L2CAP_SMP_PAIRING_RANDOM:
  577. pairing_rnd(conn, data, len);
  578. break;
  579. case BT_L2CAP_SMP_ENCRYPT_INFO:
  580. encrypt_info(conn, data, len);
  581. break;
  582. case BT_L2CAP_SMP_CENTRAL_IDENT:
  583. central_ident(conn, data, len);
  584. break;
  585. case BT_L2CAP_SMP_IDENT_ADDR_INFO:
  586. ident_addr_info(conn, data, len);
  587. break;
  588. case BT_L2CAP_SMP_IDENT_INFO:
  589. ident_info(conn, data, len);
  590. break;
  591. case BT_L2CAP_SMP_SIGNING_INFO:
  592. signing_info(conn, data, len);
  593. break;
  594. case BT_L2CAP_SMP_PUBLIC_KEY:
  595. public_key(conn, data, len);
  596. break;
  597. case BT_L2CAP_SMP_DHKEY_CHECK:
  598. dhkey_check(conn, data, len);
  599. break;
  600. default:
  601. break;
  602. }
  603. }
  604. void smp_bredr_data(void *conn_data, const void *data, uint16_t len)
  605. {
  606. struct smp_conn *conn = conn_data;
  607. uint8_t opcode;
  608. if (len < 1) {
  609. bthost_debug(conn->smp->bthost, "Received too small SMP PDU");
  610. return;
  611. }
  612. if (conn->addr_type != BDADDR_BREDR) {
  613. bthost_debug(conn->smp->bthost,
  614. "Received LE SMP data on BR/EDR link");
  615. return;
  616. }
  617. opcode = *((const uint8_t *) data);
  618. switch (opcode) {
  619. case BT_L2CAP_SMP_PAIRING_REQUEST:
  620. pairing_req(conn, data, len);
  621. break;
  622. case BT_L2CAP_SMP_PAIRING_RESPONSE:
  623. pairing_rsp(conn, data, len);
  624. break;
  625. default:
  626. break;
  627. }
  628. }
  629. int smp_get_ltk(void *smp_data, uint64_t rand, uint16_t ediv, uint8_t *ltk)
  630. {
  631. struct smp_conn *conn = smp_data;
  632. static const uint8_t no_ltk[16] = { 0 };
  633. if (!memcmp(conn->ltk, no_ltk, 16))
  634. return -ENOENT;
  635. memcpy(ltk, conn->ltk, 16);
  636. return 0;
  637. }
  638. static void smp_conn_bredr(struct smp_conn *conn, uint8_t encrypt)
  639. {
  640. struct smp *smp = conn->smp;
  641. struct bt_l2cap_smp_pairing_request req;
  642. uint64_t fixed_chan;
  643. if (encrypt != 0x02)
  644. return;
  645. conn->sc = true;
  646. if (!conn->out)
  647. return;
  648. fixed_chan = bthost_conn_get_fixed_chan(smp->bthost, conn->handle);
  649. if (!(fixed_chan & L2CAP_FC_SMP_BREDR))
  650. return;
  651. memset(&req, 0, sizeof(req));
  652. req.max_key_size = 0x10;
  653. req.init_key_dist = key_dist(smp->bthost);
  654. req.resp_key_dist = key_dist(smp->bthost);
  655. smp_send(conn, BT_L2CAP_SMP_PAIRING_REQUEST, &req, sizeof(req));
  656. }
  657. void smp_conn_encrypted(void *conn_data, uint8_t encrypt)
  658. {
  659. struct smp_conn *conn = conn_data;
  660. if (!encrypt)
  661. return;
  662. if (conn->addr_type == BDADDR_BREDR) {
  663. smp_conn_bredr(conn, encrypt);
  664. return;
  665. }
  666. if (conn->out && conn->remote_key_dist)
  667. return;
  668. distribute_keys(conn);
  669. }
  670. static uint8_t type2hci(uint8_t addr_type)
  671. {
  672. switch (addr_type) {
  673. case BDADDR_BREDR:
  674. case BDADDR_LE_PUBLIC:
  675. return LE_PUBLIC_ADDRESS;
  676. case BDADDR_LE_RANDOM:
  677. return LE_RANDOM_ADDRESS;
  678. }
  679. return 0x00;
  680. }
  681. void *smp_conn_add(void *smp_data, uint16_t handle,
  682. const uint8_t *ia, uint8_t ia_type,
  683. const uint8_t *ra, uint8_t ra_type, bool conn_init)
  684. {
  685. struct smp *smp = smp_data;
  686. struct smp_conn *conn;
  687. char ia_str[18], ra_str[18];
  688. conn = malloc(sizeof(struct smp_conn));
  689. if (!conn)
  690. return NULL;
  691. memset(conn, 0, sizeof(*conn));
  692. conn->smp = smp;
  693. conn->handle = handle;
  694. conn->out = conn_init;
  695. conn->addr_type = conn_init ? ia_type : ra_type;
  696. conn->ia_type = type2hci(ia_type);
  697. conn->ra_type = type2hci(ra_type);
  698. memcpy(conn->ia, ia, 6);
  699. memcpy(conn->ra, ra, 6);
  700. ba2str((bdaddr_t *) ia, ia_str);
  701. ba2str((bdaddr_t *) ra, ra_str);
  702. bthost_debug(smp->bthost, "ia %s type 0x%02x ra %s type 0x%02x",
  703. ia_str, ia_type, ra_str, ra_type);
  704. bt_crypto_random_bytes(smp->crypto, conn->prnd, sizeof(conn->prnd));
  705. return conn;
  706. }
  707. void smp_conn_del(void *conn_data)
  708. {
  709. struct smp_conn *conn = conn_data;
  710. free(conn);
  711. }
  712. void *smp_start(struct bthost *bthost)
  713. {
  714. struct smp *smp;
  715. smp = malloc(sizeof(struct smp));
  716. if (!smp)
  717. return NULL;
  718. memset(smp, 0, sizeof(*smp));
  719. smp->crypto = bt_crypto_new();
  720. if (!smp->crypto) {
  721. free(smp);
  722. return NULL;
  723. }
  724. smp->bthost = bthost;
  725. return smp;
  726. }
  727. void smp_stop(void *smp_data)
  728. {
  729. struct smp *smp = smp_data;
  730. bt_crypto_unref(smp->crypto);
  731. free(smp);
  732. }