Skip to content

Commit

Permalink
tests/nanocoap_cli: add DTLS support
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Oct 11, 2022
1 parent ec88b80 commit 6cc934a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
11 changes: 7 additions & 4 deletions tests/nanocoap_cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ USEMODULE += gnrc_ipv6_default
USEMODULE += nanocoap_sock

# boards where basic nanocoap functionality fits, but no VFS
LOW_MEMORY_BOARDS := atmega1284p atxmega-a3bu-xplained derfmega128 \
LOW_MEMORY_BOARDS := atmega1284p atxmega-a3bu-xplained derfmega128 im880b \
saml11-xpro bluepill saml10-xpro blackpill nucleo-f302r8 \
stm32mp157c-dk2 stm32f7508-dk

# Don't enable VFS functions on small boards
# Don't enable VFS, DTLS functions on small boards
ifeq (,$(filter $(BOARD),$(LOW_MEMORY_BOARDS)))
USEMODULE += nanocoap_dtls
USEMODULE += prng_sha256prng

USEMODULE += nanocoap_vfs
USEMODULE += vfs_default
# USEMODULE += vfs_auto_format
USEMODULE += shell_cmds_default

# VFS operations require more stack on the main thread
CFLAGS += -DTHREAD_STACKSIZE_MAIN=THREAD_STACKSIZE_LARGE
# DTLS and VFS operations require more stack on the main thread
CFLAGS += -DTHREAD_STACKSIZE_MAIN=\(3*THREAD_STACKSIZE_DEFAULT\)

# always enable auto-format for native
ifeq ($(BOARD),native)
Expand Down
28 changes: 28 additions & 0 deletions tests/nanocoap_cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,33 @@
#include <stdio.h>
#include "msg.h"

#include "net/nanocoap_sock.h"
#include "net/gnrc/netif.h"
#include "net/ipv6/addr.h"
#include "shell.h"

#define MAIN_QUEUE_SIZE (4)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];

#if IS_USED(MODULE_NANOCOAP_DTLS)
#include "net/credman.h"
#include "net/dsm.h"
#include "tinydtls_keys.h"

static const uint8_t psk_id_0[] = PSK_DEFAULT_IDENTITY;
static const uint8_t psk_key_0[] = PSK_DEFAULT_KEY;
static const credman_credential_t credential = {
.type = CREDMAN_TYPE_PSK,
.tag = CONFIG_NANOCOAP_SOCK_DTLS_TAG,
.params = {
.psk = {
.key = { .s = psk_key_0, .len = sizeof(psk_key_0) - 1, },
.id = { .s = psk_id_0, .len = sizeof(psk_id_0) - 1, },
}
},
};
#endif

extern int nanotest_client_cmd(int argc, char **argv);
extern int nanotest_client_url_cmd(int argc, char **argv);
extern int nanotest_server_cmd(int argc, char **argv);
Expand Down Expand Up @@ -119,6 +139,14 @@ int main(void)
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
puts("nanocoap test app");

#if IS_USED(MODULE_NANOCOAP_DTLS)
int res = credman_add(&credential);
if (res < 0 && res != CREDMAN_EXIST) {
printf("nanocoap: cannot add credential to system: %d\n", res);
return res;
}
#endif

/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
Expand Down
63 changes: 63 additions & 0 deletions tests/nanocoap_cli/tinydtls_keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 examples
* @{
*
* @file
* @brief PSK and RPK keys for the dtls-sock example.
*
* @author Raul Fuentes <raul.fuentes-samaniego@inria.fr>
*
* @}
*/

#ifndef TINYDTLS_KEYS_H
#define TINYDTLS_KEYS_H

#ifdef __cplusplus
extern "C" {
#endif

/*
* Default keys examples for tinyDTLS (for RIOT, Linux and Contiki)
*/
#define PSK_DEFAULT_IDENTITY "Client_identity"
#define PSK_DEFAULT_KEY "secretPSK"
#define PSK_OPTIONS "i:k:"
#define PSK_ID_MAXLEN 32
#define PSK_MAXLEN 32

#ifdef CONFIG_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 /* CONFIG_DTLS_ECC */
#ifdef __cplusplus
}
#endif

#endif /* TINYDTLS_KEYS_H */

0 comments on commit 6cc934a

Please sign in to comment.