2015-05-06 12:16:45 +03:00
|
|
|
/**
|
|
|
|
* @file smp.c
|
|
|
|
* Security Manager Protocol implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2) Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3) Neither the name of Intel Corporation nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <nanokernel.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
|
|
|
|
#include "hci_core.h"
|
|
|
|
#include "conn.h"
|
|
|
|
#include "l2cap.h"
|
|
|
|
#include "smp.h"
|
|
|
|
|
2015-05-06 14:59:42 +03:00
|
|
|
struct bt_buf *bt_smp_create_pdu(struct bt_conn *conn, uint8_t op, size_t len)
|
|
|
|
{
|
|
|
|
struct bt_smp_hdr *hdr;
|
|
|
|
struct bt_buf *buf;
|
|
|
|
|
|
|
|
buf = bt_l2cap_create_pdu(conn, BT_L2CAP_CID_SMP, sizeof(*hdr) + len);
|
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hdr = (void *)bt_buf_add(buf, sizeof(*hdr));
|
|
|
|
hdr->code = op;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-05-06 12:16:45 +03:00
|
|
|
static void send_err_rsp(struct bt_conn *conn, uint8_t reason)
|
|
|
|
{
|
2015-05-06 14:59:42 +03:00
|
|
|
struct bt_smp_pairing_fail *rsp;
|
|
|
|
struct bt_buf *buf;
|
|
|
|
|
|
|
|
buf = bt_smp_create_pdu(conn, BT_SMP_CMD_PAIRING_FAIL, sizeof(*rsp));
|
|
|
|
if (!buf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rsp = (void *)bt_buf_add(buf, sizeof(*rsp));
|
|
|
|
rsp->reason = reason;
|
2015-05-06 12:16:45 +03:00
|
|
|
|
2015-05-06 14:59:42 +03:00
|
|
|
bt_conn_send(conn, buf);
|
2015-05-06 12:16:45 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 14:53:02 +03:00
|
|
|
static int smp_pairing_req(struct bt_conn *conn, struct bt_buf *buf)
|
|
|
|
{
|
|
|
|
struct bt_smp_pairing *req = (void *)buf->data;
|
|
|
|
struct bt_smp_pairing *rsp;
|
|
|
|
struct bt_buf *rsp_buf;
|
|
|
|
|
|
|
|
BT_DBG("\n");
|
|
|
|
|
|
|
|
if (buf->len != sizeof(*req))
|
|
|
|
return BT_SMP_ERR_INVALID_PARAMS;
|
|
|
|
|
|
|
|
rsp_buf = bt_smp_create_pdu(conn, BT_SMP_CMD_PAIRING_RSP, sizeof(*rsp));
|
|
|
|
if (!rsp_buf)
|
|
|
|
return BT_SMP_ERR_UNSPECIFIED;
|
|
|
|
|
|
|
|
rsp = (void *)bt_buf_add(rsp_buf, sizeof(*rsp));
|
|
|
|
|
|
|
|
/* For JustWorks pairing simplify rsp parameters.
|
|
|
|
* TODO: needs to be reworked later on
|
|
|
|
*/
|
|
|
|
rsp->auth_req = req->auth_req;
|
|
|
|
rsp->io_capability = BT_SMP_IO_NO_INPUT_OUTPUT;
|
|
|
|
rsp->oob_flag = BT_SMP_OOB_NOT_PRESENT;
|
|
|
|
rsp->max_key_size = BT_SMP_MAX_ENC_KEY_SIZE;
|
|
|
|
rsp->init_key_dist = 0;
|
|
|
|
rsp->resp_key_dist = 0;
|
|
|
|
|
|
|
|
bt_conn_send(conn, rsp_buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-07 16:37:50 +03:00
|
|
|
static int smp_pairing_confirm(struct bt_conn *conn, struct bt_buf *buf)
|
|
|
|
{
|
|
|
|
struct bt_smp_pairing_confirm *req = (void *)buf->data;
|
|
|
|
|
|
|
|
BT_DBG("\n");
|
|
|
|
|
|
|
|
if (buf->len != sizeof(*req))
|
|
|
|
return BT_SMP_ERR_INVALID_PARAMS;
|
|
|
|
|
|
|
|
/* TODO: Send pairing_confirm(Sconfirm) back */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-06 12:16:45 +03:00
|
|
|
void bt_smp_recv(struct bt_conn *conn, struct bt_buf *buf)
|
|
|
|
{
|
|
|
|
struct bt_smp_hdr *hdr = (void *)buf->data;
|
2015-05-07 14:53:02 +03:00
|
|
|
int err;
|
2015-05-06 12:16:45 +03:00
|
|
|
|
|
|
|
if (buf->len < sizeof(*hdr)) {
|
|
|
|
BT_ERR("Too small SMP PDU received\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("Received SMP code %u len %u\n", hdr->code, buf->len);
|
|
|
|
|
|
|
|
bt_buf_pull(buf, sizeof(*hdr));
|
|
|
|
|
|
|
|
switch (hdr->code) {
|
2015-05-07 14:53:02 +03:00
|
|
|
case BT_SMP_CMD_PAIRING_REQ:
|
|
|
|
err = smp_pairing_req(conn, buf);
|
|
|
|
break;
|
2015-05-07 16:37:50 +03:00
|
|
|
case BT_SMP_CMD_PAIRING_CONFIRM:
|
|
|
|
err = smp_pairing_confirm(conn, buf);
|
|
|
|
break;
|
2015-05-06 12:16:45 +03:00
|
|
|
default:
|
|
|
|
BT_DBG("Unhandled SMP code %u\n", hdr->code);
|
2015-05-07 14:53:02 +03:00
|
|
|
err = BT_SMP_ERR_CMD_NOTSUPP;
|
2015-05-06 12:16:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-07 14:53:02 +03:00
|
|
|
if (err) {
|
|
|
|
send_err_rsp(conn, err);
|
|
|
|
}
|
|
|
|
|
2015-05-06 12:16:45 +03:00
|
|
|
done:
|
|
|
|
bt_buf_put(buf);
|
|
|
|
}
|