1 From 6b26519bea04167b9cf68069bf641af4651c87e0 Mon Sep 17 00:00:00 2001
2 From: Sachin Agrawal <sachin.agrawal@intel.com>
3 Date: Thu, 26 Feb 2015 09:28:06 -0800
4 Subject: [PATCH 1/1] Updated tinyDTLS test apps to use identity hint
6 dtls-server test is updated to pass 'psk hint' to tinyDTLS library so that
7 it can send ServerKeyExchange packet. Similarly, dtls-client test
8 is updated to retrieve PSK specific for the server whose identity was
9 received via 'psk hint' packet.
11 Change-Id: Ifba720f1505ed6afbf56dfc179d98790176ce0b0
12 Signed-off-by: Sachin Agrawal <sachin.agrawal@intel.com>
14 extlibs/tinydtls/tests/dtls-client.c | 45 ++++++++++++++++++++++------------
15 extlibs/tinydtls/tests/dtls-server.c | 43 +++++++++++++++++++++-----------
16 2 files changed, 59 insertions(+), 29 deletions(-)
18 diff --git a/extlibs/tinydtls/tests/dtls-client.c b/extlibs/tinydtls/tests/dtls-client.c
19 index 96ed0fa..65b0275 100644
20 --- a/extlibs/tinydtls/tests/dtls-client.c
21 +++ b/extlibs/tinydtls/tests/dtls-client.c
24 #define DEFAULT_PORT 20220
26 -#define PSK_DEFAULT_IDENTITY "Client_identity"
27 +#define PSK_CLIENT_IDENTITY "Client_identity"
28 +#define PSK_SERVER_IDENTITY "Server_identity"
29 #define PSK_DEFAULT_KEY "secretPSK"
30 -#define PSK_OPTIONS "i:k:"
31 +#define PSK_OPTIONS "i:s:k:"
34 #define UNUSED_PARAM __attribute__((unused))
35 @@ -93,8 +94,10 @@ read_from_file(char *arg, unsigned char *buf, size_t max_buf_len) {
36 /* The PSK information for DTLS */
37 #define PSK_ID_MAXLEN 256
38 #define PSK_MAXLEN 256
39 -static unsigned char psk_id[PSK_ID_MAXLEN];
40 -static size_t psk_id_length = 0;
41 +static unsigned char psk_client_id[PSK_ID_MAXLEN];
42 +static size_t psk_client_id_length = 0;
43 +static unsigned char psk_server_id[PSK_ID_MAXLEN];
44 +static size_t psk_server_id_length = 0;
45 static unsigned char psk_key[PSK_MAXLEN];
46 static size_t psk_key_length = 0;
48 @@ -114,15 +117,15 @@ get_psk_info(struct dtls_context_t *ctx UNUSED_PARAM,
49 dtls_debug("got psk_identity_hint: '%.*s'\n", id_len, id);
52 - if (result_length < psk_id_length) {
53 + if (result_length < psk_client_id_length) {
54 dtls_warn("cannot set psk_identity -- buffer too small\n");
55 return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
58 - memcpy(result, psk_id, psk_id_length);
59 - return psk_id_length;
60 + memcpy(result, psk_client_id, psk_client_id_length);
61 + return psk_client_id_length;
63 - if (id_len != psk_id_length || memcmp(psk_id, id, id_len) != 0) {
64 + if (id_len != psk_server_id_length || memcmp(psk_server_id, id, id_len) != 0) {
65 dtls_warn("PSK for unknown id requested, exiting\n");
66 return dtls_alert_fatal_create(DTLS_ALERT_ILLEGAL_PARAMETER);
67 } else if (result_length < psk_key_length) {
68 @@ -291,12 +294,13 @@ usage( const char *program, const char *version) {
69 fprintf(stderr, "%s v%s -- DTLS client implementation\n"
70 "(c) 2011-2014 Olaf Bergmann <bergmann@tzi.org>\n\n"
72 - "usage: %s [-i file] [-k file] [-o file] [-p port] [-v num] addr [port]\n"
73 + "usage: %s [-i file] [-s file] [-k file] [-o file] [-p port] [-v num] addr [port]\n"
75 "usage: %s [-o file] [-p port] [-v num] addr [port]\n"
78 - "\t-i file\t\tread PSK identity from file\n"
79 + "\t-i file\t\tread PSK Client identity from file\n"
80 + "\t-s file\t\tread PSK Server identity from file\n"
81 "\t-k file\t\tread pre-shared key from file\n"
83 "\t-o file\t\toutput received data to this file (use '-' for STDOUT)\n"
84 @@ -337,9 +341,11 @@ main(int argc, char **argv) {
85 snprintf(port_str, sizeof(port_str), "%d", port);
88 - psk_id_length = strlen(PSK_DEFAULT_IDENTITY);
89 + psk_client_id_length = strlen(PSK_CLIENT_IDENTITY);
90 + psk_server_id_length = strlen(PSK_SERVER_IDENTITY);
91 psk_key_length = strlen(PSK_DEFAULT_KEY);
92 - memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length);
93 + memcpy(psk_client_id, PSK_CLIENT_IDENTITY, psk_client_id_length);
94 + memcpy(psk_server_id, PSK_SERVER_IDENTITY, psk_server_id_length);
95 memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length);
98 @@ -347,11 +353,20 @@ main(int argc, char **argv) {
102 - ssize_t result = read_from_file(optarg, psk_id, PSK_ID_MAXLEN);
103 + ssize_t result = read_from_file(optarg, psk_client_id, PSK_ID_MAXLEN);
105 - dtls_warn("cannot read PSK identity\n");
106 + dtls_warn("cannot read Client PSK identity\n");
108 - psk_id_length = result;
109 + psk_client_id_length = result;
114 + ssize_t result = read_from_file(optarg, psk_server_id, PSK_ID_MAXLEN);
116 + dtls_warn("cannot read Server PSK identity\n");
118 + psk_server_id_length = result;
122 diff --git a/extlibs/tinydtls/tests/dtls-server.c b/extlibs/tinydtls/tests/dtls-server.c
123 index 3f030b1..ae1283e 100644
124 --- a/extlibs/tinydtls/tests/dtls-server.c
125 +++ b/extlibs/tinydtls/tests/dtls-server.c
126 @@ -47,6 +47,9 @@ handle_sigint(int signum) {
131 +#define PSK_SERVER_HINT "Server_identity"
133 /* This function is the "key store" for tinyDTLS. It is called to
134 * retrieve a key for the given identity within this particular
136 @@ -70,23 +73,35 @@ get_psk_info(struct dtls_context_t *ctx, const session_t *session,
137 (unsigned char *)"", 1 }
140 - if (type != DTLS_PSK_KEY) {
144 + case DTLS_PSK_HINT:
145 + if (result_length < strlen(PSK_SERVER_HINT)) {
146 + dtls_warn("cannot set psk_hint -- buffer too small\n");
147 + return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
152 - for (i = 0; i < sizeof(psk)/sizeof(struct keymap_t); i++) {
153 - if (id_len == psk[i].id_length && memcmp(id, psk[i].id, id_len) == 0) {
154 - if (result_length < psk[i].key_length) {
155 - dtls_warn("buffer too small for PSK");
156 - return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
159 - memcpy(result, psk[i].key, psk[i].key_length);
160 - return psk[i].key_length;
161 + memcpy(result, PSK_SERVER_HINT, strlen(PSK_SERVER_HINT));
162 + return strlen(PSK_SERVER_HINT);
167 + for (i = 0; i < sizeof(psk)/sizeof(struct keymap_t); i++) {
168 + if (id_len == psk[i].id_length && memcmp(id, psk[i].id, id_len) == 0) {
169 + if (result_length < psk[i].key_length) {
170 + dtls_warn("buffer too small for PSK");
171 + return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
174 + memcpy(result, psk[i].key, psk[i].key_length);
175 + return psk[i].key_length;
182 + dtls_warn("unsupported request type: %d\n", type);
185 return dtls_alert_fatal_create(DTLS_ALERT_DECRYPT_ERROR);