diff --git a/.travis.yml b/.travis.yml index 1b161a94..799a1b9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ services: env: - PLATFORM=posix TESTS=yes TLS=no - - PLATFORM=posix TLS=openssl + - PLATFORM=posix TESTS=yes TLS=openssl # - PLATFORM=contiki TLS=no # - PLATFORM=lwip TLS=no diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 8a745de3..95c8ec17 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -27,6 +27,21 @@ int coap_dtls_is_supported(void); /** Returns 1 if support for TLS is enabled, or 0 otherwise. */ int coap_tls_is_supported( void ); +#define COAP_TLS_LIBRARY_NOTLS 0 +#define COAP_TLS_LIBRARY_TINYDTLS 1 +#define COAP_TLS_LIBRARY_OPENSSL 2 +#define COAP_TLS_LIBRARY_GNUTLS 3 + +typedef struct coap_tls_version_t { + uint64_t version; /* Library Version */ + int type; /* One of COAP_TLS_LIBRARY_* */ +} coap_tls_version_t; + +/** + * Returns the version and type of library libcoap was compiled against + */ +coap_tls_version_t *coap_get_tls_library_version(void); + /** Sets the log level to the specified value. */ void coap_dtls_set_log_level(int level); diff --git a/libcoap-1.map b/libcoap-1.map index 2cbb8197..4256f6bf 100644 --- a/libcoap-1.map +++ b/libcoap-1.map @@ -77,6 +77,7 @@ global: coap_get_log_level; coap_get_query; coap_get_resource_from_key; + coap_get_tls_library_version; coap_handle_dgram; coap_handle_event; coap_handle_failed_notify; diff --git a/libcoap-1.sym b/libcoap-1.sym index a64b5928..a6b363c1 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -75,6 +75,7 @@ coap_get_data coap_get_log_level coap_get_query coap_get_resource_from_key +coap_get_tls_library_version coap_handle_dgram coap_handle_event coap_handle_failed_notify diff --git a/src/coap_notls.c b/src/coap_notls.c index 3a2681e8..eb4f4902 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -29,6 +29,14 @@ coap_tls_is_supported(void) { return 0; } +coap_tls_version_t * +coap_get_tls_library_version(void) { + static coap_tls_version_t version; + version.version = 0; + version.type = COAP_TLS_LIBRARY_NOTLS; + return &version; +} + int coap_dtls_context_set_pki( coap_context_t *ctx UNUSED, coap_dtls_security_setup_t setup_callback UNUSED, coap_dtls_pki_t* setup_data UNUSED diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 7aea91c3..77a30edf 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -41,13 +41,29 @@ typedef struct coap_openssl_context_t { } coap_openssl_context_t; int coap_dtls_is_supported(void) { + if (SSLeay() < 0x10100000L) { + coap_log(LOG_WARNING, "OpenSSL version 1.1.0 or later is required\n"); + return 0; + } return 1; } int coap_tls_is_supported(void) { + if (SSLeay() < 0x10100000L) { + coap_log(LOG_WARNING, "OpenSSL version 1.1.0 or later is required\n"); + return 0; + } return 1; } +coap_tls_version_t * +coap_get_tls_library_version(void) { + static coap_tls_version_t version; + version.version = SSLeay(); + version.type = COAP_TLS_LIBRARY_OPENSSL; + return &version; +} + void coap_dtls_startup(void) { SSL_load_error_strings(); SSL_library_init(); diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 8352cdfe..38bfa5b0 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -444,6 +444,14 @@ int coap_tls_is_supported(void) { return 0; } +coap_tls_version_t * +coap_get_tls_library_version(void) { + static coap_tls_version_t version; + version.version = DTLS_VERSION; + version.type = COAP_TLS_LIBRARY_TINYDTLS; + return &version; +} + int coap_dtls_context_set_pki( coap_context_t *ctx UNUSED, coap_dtls_security_setup_t setup_callback UNUSED, coap_dtls_pki_t* setup_data UNUSED diff --git a/tests/Makefile.am b/tests/Makefile.am index e70125b1..7e76335a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -21,7 +21,8 @@ testdriver_SOURCES = \ test_pdu.c \ test_sendqueue.c \ test_uri.c \ - test_wellknown.c + test_wellknown.c \ + test_tls.c testdriver_LDADD = $(CUNIT_LIBS) $(DTLS_LIBS) $(top_builddir)/.libs/libcoap-$(LIBCOAP_API_VERSION).la diff --git a/tests/test_sendqueue.c b/tests/test_sendqueue.c index 37630676..ea572815 100644 --- a/tests/test_sendqueue.c +++ b/tests/test_sendqueue.c @@ -277,14 +277,14 @@ t_sendqueue_tests_create(void) { coap_address_t addr; coap_address_init(&addr); - addr.size = sizeof(struct sockaddr_in6); - addr.addr.sin6.sin6_family = AF_INET6; - addr.addr.sin6.sin6_addr = in6addr_any; - addr.addr.sin6.sin6_port = htons(COAP_DEFAULT_PORT); + addr.size = sizeof(struct sockaddr_in); + addr.addr.sin.sin_family = AF_INET; + addr.addr.sin.sin_addr.s_addr = htonl(INADDR_ANY); + addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT); ctx = coap_new_context(&addr); - addr.addr.sin6.sin6_addr = in6addr_loopback; + addr.addr.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); session = coap_new_client_session(ctx, NULL, &addr, COAP_PROTO_UDP); coap_ticks(×tamp[0]); diff --git a/tests/test_tls.c b/tests/test_tls.c new file mode 100644 index 00000000..3595de7f --- /dev/null +++ b/tests/test_tls.c @@ -0,0 +1,98 @@ +/* libcoap unit tests + * + * Copyright (C) 2018 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. + */ + +#include "coap_config.h" +#include "test_tls.h" + +#include + +#undef HAVE_DTLS + +#ifdef HAVE_LIBTINYDTLS +#define HAVE_DTLS 1 + +#include +#include +#endif /* HAVE_LIBTINYDTLS */ + +#ifdef HAVE_OPENSSL +#define HAVE_DTLS 1 +#include +#endif /* HAVE_OPENSSL */ + +#ifdef HAVE_GNUTLS +#define HAVE_DTLS 1 +#include +#endif /* HAVE_GNUTLS */ + +static void +t_tls1(void) { + int need_dtls = 0; +#ifdef HAVE_DTLS + need_dtls = 1; +#endif /* HAVE_DTLS */ + + CU_ASSERT(coap_dtls_is_supported() == need_dtls); +} + +static void +t_tls2(void) { + coap_tls_version_t *v = coap_get_tls_library_version(); + coap_tls_version_t version; + + memset(&version, 0, sizeof(coap_tls_version_t)); + +#if defined(HAVE_OPENSSL) + version.version = SSLeay(); + version.type = COAP_TLS_LIBRARY_OPENSSL; +#elif defined(HAVE_LIBTINYDTLS) + version.version = DTLS_VERSION; + version.type = COAP_TLS_LIBRARY_TINYDTLS; +#elif defined(HAVE_GNUTLS) + version.version = GNUTLS_VERSION_NUMBER; + version.type = COAP_TLS_LIBRARY_GNUTLS; +#else /* no DTLS */ + version.version = 0; + version.type = COAP_TLS_LIBRARY_NOTLS; +#endif /* HAVE_OPENSSL || HAVE_LIBTINYDTLS */ + + CU_ASSERT_PTR_NOT_NULL_FATAL(v); + CU_ASSERT(version.version == v->version); + CU_ASSERT(version.type == v->type); +} + +static int +t_tls_tests_create(void) { + coap_dtls_startup(); + return 0; +} + +CU_pSuite +t_init_tls_tests(void) { + CU_pSuite suite; + + suite = CU_add_suite("TLS", t_tls_tests_create, NULL); + if (!suite) { /* signal error */ + fprintf(stderr, "W: cannot add TLS test suite (%s)\n", + CU_get_error_msg()); + + return NULL; + } + +#define TLS_TEST(s,t) \ + if (!CU_ADD_TEST(s,t)) { \ + fprintf(stderr, "W: cannot add TLS test (%s)\n", \ + CU_get_error_msg()); \ + } + + TLS_TEST(suite, t_tls1); + TLS_TEST(suite, t_tls2); + + return suite; +} + diff --git a/tests/test_tls.h b/tests/test_tls.h new file mode 100644 index 00000000..f68558f1 --- /dev/null +++ b/tests/test_tls.h @@ -0,0 +1,11 @@ +/* libcoap unit tests + * + * Copyright (C) 2018 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. + */ + +#include + +CU_pSuite t_init_tls_tests(void); diff --git a/tests/test_wellknown.c b/tests/test_wellknown.c index 9afc3f72..7d3f52bc 100644 --- a/tests/test_wellknown.c +++ b/tests/test_wellknown.c @@ -254,14 +254,14 @@ t_wkc_tests_create(void) { coap_address_init(&addr); - addr.size = sizeof(struct sockaddr_in6); - addr.addr.sin6.sin6_family = AF_INET6; - addr.addr.sin6.sin6_addr = in6addr_any; - addr.addr.sin6.sin6_port = htons(COAP_DEFAULT_PORT); + addr.size = sizeof(struct sockaddr_in); + addr.addr.sin.sin_family = AF_INET; + addr.addr.sin.sin_addr.s_addr = htonl(INADDR_ANY); + addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT); ctx = coap_new_context(&addr); - addr.addr.sin6.sin6_addr = in6addr_loopback; + addr.addr.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); session = coap_new_client_session(ctx, NULL, &addr, COAP_PROTO_UDP); pdu = coap_pdu_init(0, 0, 0, TEST_PDU_SIZE); diff --git a/tests/testdriver.c b/tests/testdriver.c index 39480199..edfee183 100644 --- a/tests/testdriver.c +++ b/tests/testdriver.c @@ -11,6 +11,7 @@ #include "test_error_response.h" #include "test_sendqueue.h" #include "test_wellknown.h" +#include "test_tls.h" #include "libcoap.h" #ifdef __GNUC__ @@ -36,6 +37,7 @@ main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) { t_init_error_response_tests(); t_init_sendqueue_tests(); t_init_wellknown_tests(); + t_init_tls_tests(); CU_basic_set_mode(run_mode); result = CU_basic_run_tests();