From f26447c54ed5517215a4936ba10996bbe7862231 Mon Sep 17 00:00:00 2001 From: pokgak Date: Mon, 8 Apr 2019 21:20:35 +0200 Subject: [PATCH] examples/gcoap: use sock_dtls --- examples/gcoap/Makefile | 14 ++++++++ examples/gcoap/credentials.h | 70 ++++++++++++++++++++++++++++++++++++ examples/gcoap/gcoap_cli.c | 24 +++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 examples/gcoap/credentials.h diff --git a/examples/gcoap/Makefile b/examples/gcoap/Makefile index 8bc265939378..bf1c66e10c30 100644 --- a/examples/gcoap/Makefile +++ b/examples/gcoap/Makefile @@ -34,6 +34,20 @@ USEMODULE += gcoap # Additional networking modules that can be dropped if not needed USEMODULE += gnrc_icmpv6_echo +# Use tinydtls's sock_dtls +USEMODULE += tinydtls_sock_dtls +USEMODULE += sock_dtls # FIXME: why it doesnt see this? +ifneq (,$(filter sock_dtls,$(USEMODULE))) + GCOAP_PORT = 5684 + CFLAGS += -DGCOAP_PORT=$(GCOAP_PORT) + INCLUDES += -I$(CURDIR) + ifneq (,$(filter tinydtls,$(USEMODULE))) + # Log level for the tinydtls package + # Values: 0:EMERG (Default), 1:ALERT 2:CRIT 3:WARN 4:NOTICE 5:INFO 6:DEBUG + TINYDTLS_LOG ?= 6 + endif +endif + # Required by gcoap example USEMODULE += od USEMODULE += fmt diff --git a/examples/gcoap/credentials.h b/examples/gcoap/credentials.h new file mode 100644 index 000000000000..415940cf7e7c --- /dev/null +++ b/examples/gcoap/credentials.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2018 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief tlsman test application (PSK and ECC keys) + * + * Small test for TLSMAN. Many definitions defined here are also available at + * sock_secure (and are intended to be used in standard applications) + * + * @author Raul Fuentes + * + * @} + */ + +#ifndef DTLS_CREDENTIALS_H +#define DTLS_CREDENTIALS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef MODULE_SOCK_DTLS +#ifdef DTLS_PSK +/** + * @brief Application specific PSK identity and key parameters + */ +static psk_params_t tdsec_psk_params[] = { + { .id = "RIOTer", .key = "LGPLisyourfriend", }, +}; +#endif /* DTLS_PSK */ + +#ifdef DTLS_ECC +static const unsigned char ecdsa_priv_key[] = { + 0x41, 0xC1, 0xCB, 0x6B, 0x51, 0x24, 0x7A, 0x14, + 0x43, 0x21, 0x43, 0x5B, 0x7A, 0x80, 0xE7, 0x14, + 0x89, 0x6A, 0x33, 0xBB, 0xAD, 0x72, 0x94, 0xCA, + 0x40, 0x14, 0x55, 0xA1, 0x94, 0xA9, 0x49, 0xFA +}; + +static const unsigned char ecdsa_pub_key_x[] = { + 0x36, 0xDF, 0xE2, 0xC6, 0xF9, 0xF2, 0xED, 0x29, + 0xDA, 0x0A, 0x9A, 0x8F, 0x62, 0x68, 0x4E, 0x91, + 0x63, 0x75, 0xBA, 0x10, 0x30, 0x0C, 0x28, 0xC5, + 0xE4, 0x7C, 0xFB, 0xF2, 0x5F, 0xA5, 0x8F, 0x52 +}; + +static const unsigned char ecdsa_pub_key_y[] = { + 0x71, 0xA0, 0xD4, 0xFC, 0xDE, 0x1A, 0xB8, 0x78, + 0x5A, 0x3C, 0x78, 0x69, 0x35, 0xA7, 0xCF, 0xAB, + 0xE9, 0x3F, 0x98, 0x72, 0x09, 0xDA, 0xED, 0x0B, + 0x4F, 0xAB, 0xC3, 0x6F, 0xC7, 0x72, 0xF8, 0x29 +}; +#endif /* DTLS_ECC */ +#endif /* MODULE_SOCK_DTLS */ + +#ifdef __cplusplus +} +#endif + +#endif /* DTLS_CREDENTIALS_H */ + diff --git a/examples/gcoap/gcoap_cli.c b/examples/gcoap/gcoap_cli.c index 2e0caeb702d8..9a6a85425fd4 100644 --- a/examples/gcoap/gcoap_cli.c +++ b/examples/gcoap/gcoap_cli.c @@ -25,6 +25,11 @@ #include "net/gcoap.h" #include "od.h" #include "fmt.h" +#ifdef MODULE_SOCK_DTLS +#include "net/tlsman.h" +#include "credentials.h" +#include "net/sock/dtls.h" +#endif #define ENABLE_DEBUG (0) #include "debug.h" @@ -33,6 +38,7 @@ static void _resp_handler(unsigned req_state, coap_pkt_t* pdu, sock_udp_ep_t *remote); static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx); static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx); +static int _get_psk_params(psk_params_t *psk); /* CoAP resources. Must be sorted by path (ASCII order). */ static const coap_resource_t _resources[] = { @@ -49,6 +55,23 @@ static gcoap_listener_t _listener = { /* Counts requests sent by CLI. */ static uint16_t req_count = 0; +static tlsman_handler_t credentials_handler = { + .get_psk_params = _get_psk_params, + .get_ecdsa_params = NULL, +}; + +static int _get_psk_params(psk_params_t *psk) +{ + psk->key = tdsec_psk_params[0].key; + psk->key_len = strlen(tdsec_psk_params[0].key); + + psk->id = tdsec_psk_params[0].id; + psk->hint = NULL; + psk->id_len = strlen(tdsec_psk_params[0].id); + psk->hint_len = 0; + return 0; +} + /* * Response callback. */ @@ -312,5 +335,6 @@ int gcoap_cli_cmd(int argc, char **argv) void gcoap_cli_init(void) { + tlsman_set_credentials_handler(&credentials_handler); gcoap_register_listener(&_listener); }