-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cipher suite selection to test applications.
Signed-off-by: Achim Kraus <[email protected]>
- Loading branch information
Showing
5 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
#include "global.h" | ||
#include "dtls_debug.h" | ||
#include "dtls_ciphers_util.h" | ||
#include "dtls.h" | ||
|
||
#define DEFAULT_PORT 20220 | ||
|
@@ -214,6 +215,16 @@ send_to_peer(struct dtls_context_t *ctx, | |
&session->addr.sa, session->size); | ||
} | ||
|
||
static const dtls_cipher_t* ciphers = NULL; | ||
|
||
static void | ||
get_cipher_suites(struct dtls_context_t *ctx, | ||
session_t *session, const dtls_cipher_t **cipher_suites) { | ||
(void) ctx; | ||
(void) session; | ||
*cipher_suites = ciphers; | ||
} | ||
|
||
static int | ||
dtls_handle_read(struct dtls_context_t *ctx) { | ||
int fd; | ||
|
@@ -308,9 +319,9 @@ usage( const char *program, const char *version) { | |
fprintf(stderr, "%s v%s -- DTLS client implementation\n" | ||
"(c) 2011-2014 Olaf Bergmann <[email protected]>\n\n" | ||
#ifdef DTLS_PSK | ||
"usage: %s [-i file] [-k file] [-o file] [-p port] [-v num] addr [port]\n" | ||
"usage: %s [-i file] [-k file] [-o file] [-p port] [-v num] [-c cipher-suites] addr [port]\n" | ||
#else /* DTLS_PSK */ | ||
"usage: %s [-o file] [-p port] [-v num] addr [port]\n" | ||
"usage: %s [-o file] [-p port] [-v num] [-c cipher-suites] addr [port]\n" | ||
#endif /* DTLS_PSK */ | ||
#ifdef DTLS_PSK | ||
"\t-i file\t\tread PSK identity from file\n" | ||
|
@@ -320,11 +331,13 @@ usage( const char *program, const char *version) { | |
"\t-p port\t\tlisten on specified port (default is %d)\n" | ||
"\t-v num\t\tverbosity level (default: 3)\n", | ||
program, version, program, DEFAULT_PORT); | ||
cipher_suites_usage(stderr, "\t"); | ||
} | ||
|
||
static dtls_handler_t cb = { | ||
.write = send_to_peer, | ||
.read = read_from_peer, | ||
.get_cipher_suites = get_cipher_suites, | ||
.event = NULL, | ||
#ifdef DTLS_PSK | ||
.get_psk_info = get_psk_info, | ||
|
@@ -371,7 +384,7 @@ main(int argc, char **argv) { | |
memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length); | ||
#endif /* DTLS_PSK */ | ||
|
||
while ((opt = getopt(argc, argv, "p:o:v:" PSK_OPTIONS)) != -1) { | ||
while ((opt = getopt(argc, argv, "p:o:v:c:" PSK_OPTIONS)) != -1) { | ||
switch (opt) { | ||
#ifdef DTLS_PSK | ||
case 'i' : | ||
|
@@ -410,6 +423,9 @@ main(int argc, char **argv) { | |
case 'v' : | ||
log_level = strtol(optarg, NULL, 10); | ||
break; | ||
case 'c' : | ||
ciphers = init_cipher_suites(optarg); | ||
break; | ||
default: | ||
usage(argv[0], dtls_package_version()); | ||
exit(1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,9 @@ | |
#include <signal.h> | ||
|
||
#include "tinydtls.h" | ||
#include "dtls.h" | ||
#include "dtls_debug.h" | ||
#include "dtls_ciphers_util.h" | ||
#include "dtls.h" | ||
|
||
#ifdef IS_WINDOWS | ||
#include <winsock2.h> | ||
|
@@ -167,6 +168,16 @@ send_to_peer(struct dtls_context_t *ctx, | |
&session->addr.sa, session->size); | ||
} | ||
|
||
static const dtls_cipher_t* ciphers = NULL; | ||
|
||
static void | ||
get_cipher_suites(struct dtls_context_t *ctx, | ||
session_t *session, const dtls_cipher_t **cipher_suites) { | ||
(void) ctx; | ||
(void) session; | ||
*cipher_suites = ciphers; | ||
} | ||
|
||
static int | ||
dtls_handle_read(struct dtls_context_t *ctx) { | ||
int *fd; | ||
|
@@ -256,16 +267,18 @@ usage(const char *program, const char *version) { | |
|
||
fprintf(stderr, "%s v%s -- DTLS server implementation\n" | ||
"(c) 2011-2014 Olaf Bergmann <[email protected]>\n\n" | ||
"usage: %s [-A address] [-p port] [-v num]\n" | ||
"usage: %s [-A address] [-p port] [-v num] [-c cipher-suites]\n" | ||
"\t-A address\t\tlisten on specified address (default is ::)\n" | ||
"\t-p port\t\tlisten on specified port (default is %d)\n" | ||
"\t-v num\t\tverbosity level (default: 3)\n", | ||
program, version, program, DEFAULT_PORT); | ||
cipher_suites_usage(stderr, "\t"); | ||
} | ||
|
||
static dtls_handler_t cb = { | ||
.write = send_to_peer, | ||
.read = read_from_peer, | ||
.get_cipher_suites = get_cipher_suites, | ||
.event = NULL, | ||
#ifdef DTLS_PSK | ||
.get_psk_info = get_psk_info, | ||
|
@@ -300,7 +313,7 @@ main(int argc, char **argv) { | |
listen_addr.sin6_family = AF_INET6; | ||
listen_addr.sin6_addr = in6addr_any; | ||
|
||
while ((opt = getopt(argc, argv, "A:p:v:")) != -1) { | ||
while ((opt = getopt(argc, argv, "A:p:v:c:")) != -1) { | ||
switch (opt) { | ||
case 'A' : | ||
if (resolve_address(optarg, (struct sockaddr *)&listen_addr) < 0) { | ||
|
@@ -314,6 +327,9 @@ main(int argc, char **argv) { | |
case 'v' : | ||
log_level = strtol(optarg, NULL, 10); | ||
break; | ||
case 'c' : | ||
ciphers = init_cipher_suites(optarg); | ||
break; | ||
default: | ||
usage(argv[0], dtls_package_version()); | ||
exit(1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v. 1.0 which accompanies this distribution. | ||
* | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
*******************************************************************************/ | ||
|
||
#include <string.h> | ||
|
||
#include "dtls_ciphers_util.h" | ||
|
||
|
||
struct cipher_entry { | ||
const char* name; | ||
const dtls_cipher_t cipher; | ||
}; | ||
|
||
#define CIPHER_ENTRY(X) { .name = #X, .cipher = X } | ||
#define ARRAY_LENGTH (sizeof(map)/sizeof(struct cipher_entry)) | ||
#define SEP ':' | ||
|
||
static const struct cipher_entry map[] = { | ||
CIPHER_ENTRY(TLS_PSK_WITH_AES_128_CCM), | ||
CIPHER_ENTRY(TLS_PSK_WITH_AES_128_CCM_8), | ||
CIPHER_ENTRY(TLS_ECDHE_ECDSA_WITH_AES_128_CCM), | ||
CIPHER_ENTRY(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8), | ||
{ .name = NULL, .cipher = TLS_NULL_WITH_NULL_NULL} | ||
}; | ||
|
||
static dtls_cipher_t ciphers_table[ARRAY_LENGTH] = { TLS_NULL_WITH_NULL_NULL }; | ||
|
||
static dtls_cipher_t find_cipher_suite(const char *arg) { | ||
for (int index=0; index < ARRAY_LENGTH - 1; ++index) { | ||
size_t len = strlen(map[index].name); | ||
if (strncmp(arg, map[index].name, len) == 0 && (arg[len] == 0 || arg[len] == SEP)) { | ||
return map[index].cipher; | ||
} | ||
} | ||
return TLS_NULL_WITH_NULL_NULL; | ||
} | ||
|
||
static void add_cipher_suite(dtls_cipher_t cipher) { | ||
for (int index=0; index < ARRAY_LENGTH - 1; ++index) { | ||
if (ciphers_table[index] == cipher) { | ||
return; | ||
} | ||
if (ciphers_table[index] == TLS_NULL_WITH_NULL_NULL) { | ||
ciphers_table[index] = cipher; | ||
ciphers_table[index + 1] = TLS_NULL_WITH_NULL_NULL; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
const dtls_cipher_t* | ||
init_cipher_suites(const char* arg) { | ||
while (arg) { | ||
dtls_cipher_t cipher = find_cipher_suite(arg); | ||
if (cipher != TLS_NULL_WITH_NULL_NULL) { | ||
add_cipher_suite(cipher); | ||
} | ||
arg = strchr(arg, SEP); | ||
if (arg) { | ||
++arg; | ||
} | ||
} | ||
return ciphers_table; | ||
} | ||
|
||
void | ||
cipher_suites_usage(FILE* file, const char* head) { | ||
fprintf(file, "%s-c ciphers\tlist of cipher-suites separated by ':'\n", head); | ||
fprintf(file, "%s\t\t(default: %s:%s\n", head, map[0].name, map[1].name); | ||
fprintf(file, "%s\t\t :%s:%s)\n", head, map[2].name, map[3].name); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v. 1.0 which accompanies this distribution. | ||
* | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
*******************************************************************************/ | ||
|
||
#ifndef _DTLS_CIPHERS_UTIL_H_ | ||
#define _DTLS_CIPHERS_UTIL_H_ | ||
|
||
#include <stdio.h> | ||
|
||
#include "global.h" | ||
|
||
const dtls_cipher_t* init_cipher_suites(const char* arg); | ||
|
||
void cipher_suites_usage(FILE* file, const char* head); | ||
|
||
#endif /* _DTLS_CIPHERS_UTIL_H_ */ |