20#ifdef COAP_WITH_LIBOPENSSL
71#include <openssl/ssl.h>
72#include <openssl/engine.h>
73#include <openssl/err.h>
74#include <openssl/rand.h>
75#include <openssl/hmac.h>
76#include <openssl/x509v3.h>
78#if OPENSSL_VERSION_NUMBER >= 0x30000000L
81#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
84#if !defined(__MINGW32__)
85#pragma warning(disable : 4996)
90#ifdef COAP_EPOLL_SUPPORT
91# include <sys/epoll.h>
94#if OPENSSL_VERSION_NUMBER < 0x10100000L
95#error Must be compiled against OpenSSL 1.1.0 or later
99#define strcasecmp _stricmp
100#define strncasecmp _strnicmp
104#ifndef TLSEXT_TYPE_client_certificate_type
105#define TLSEXT_TYPE_client_certificate_type 19
107#ifndef TLSEXT_TYPE_server_certificate_type
108#define TLSEXT_TYPE_server_certificate_type 20
111#ifndef COAP_OPENSSL_CIPHERS
112#if OPENSSL_VERSION_NUMBER >= 0x10101000L
113#define COAP_OPENSSL_CIPHERS "TLSv1.3:TLSv1.2:!NULL"
115#define COAP_OPENSSL_CIPHERS "TLSv1.2:!NULL"
119#ifndef COAP_OPENSSL_PSK_CIPHERS
120#define COAP_OPENSSL_PSK_CIPHERS "PSK:!NULL"
123#ifndef COAP_OPENSSL_PKCS11_ENGINE_ID
124#define COAP_OPENSSL_PKCS11_ENGINE_ID "pkcs11"
128typedef struct coap_dtls_context_t {
131 HMAC_CTX *cookie_hmac;
134} coap_dtls_context_t;
136typedef struct coap_tls_context_t {
144typedef struct sni_entry {
146#if OPENSSL_VERSION_NUMBER < 0x10101000L
153typedef struct psk_sni_entry {
155#if OPENSSL_VERSION_NUMBER < 0x10101000L
161typedef struct coap_openssl_context_t {
162 coap_dtls_context_t dtls;
164 coap_tls_context_t tls;
169 sni_entry *sni_entry_list;
170#if OPENSSL_VERSION_NUMBER < 0x10101000L
171 size_t psk_sni_count;
172 psk_sni_entry *psk_sni_entry_list;
174} coap_openssl_context_t;
176#if COAP_SERVER_SUPPORT
177#if OPENSSL_VERSION_NUMBER < 0x10101000L
178static int psk_tls_server_name_call_back(SSL *ssl,
int *sd,
void *arg);
180static int psk_tls_client_hello_call_back(SSL *ssl,
int *al,
void *arg);
186 if (SSLeay() < 0x10100000L) {
187 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
190#if OPENSSL_VERSION_NUMBER >= 0x10101000L
198 if (SSLeay() < 0x10101000L) {
199 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
209 if (SSLeay() < 0x10100000L) {
210 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
213#if OPENSSL_VERSION_NUMBER >= 0x10101000L
214 if (SSLeay() < 0x10101000L) {
215 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
270#if COAP_CLIENT_SUPPORT
288static ENGINE *pkcs11_engine = NULL;
289static ENGINE *defined_engine = NULL;
293 SSL_load_error_strings();
295 ENGINE_load_dynamic();
300#if OPENSSL_VERSION_NUMBER < 0x30000000L
303 ENGINE_finish(pkcs11_engine);
304 pkcs11_engine = NULL;
306 if (defined_engine) {
308 ENGINE_finish(defined_engine);
309 defined_engine = NULL;
312 pkcs11_engine = NULL;
313 defined_engine = NULL;
329 return c_session->
tls;
335get_split_conf_entry(
const uint8_t **start,
size_t size,
const char *get_keyword,
337 const uint8_t *begin = *start;
340 const uint8_t *split;
346 kend = end = memchr(begin,
'\n', size);
352 if (end > begin && end[-1] ==
'\r')
355 if (begin[0] ==
'#' || (end - begin) == 0) {
357 size -= kend - begin + 1;
363 split = memchr(begin,
':', end - begin);
367 if ((
size_t)(split - begin) != strlen(get_keyword)) {
368 size -= kend - begin + 1;
372 if (memcmp(begin, get_keyword, split - begin)) {
373 size -= kend - begin + 1;
381 if ((end - begin) == 0)
384 split = memchr(begin,
':', end - begin);
396 if ((end - split) > 0) {
431 const uint8_t *start;
436 unsigned int defaults = 0;
437 int done_engine_id = 0;
438 int done_engine_init = 0;
444 end = start + conf_mem->
length;
446 if (defined_engine) {
447 coap_log_warn(
"coap_tls_engine_configure: Freeing off previous engine definition\n");
448 ENGINE_finish(defined_engine);
449 defined_engine = NULL;
453 if (!get_split_conf_entry(&start, end - start,
"engine", &engine_id, &p2)) {
454 coap_log_warn(
"coap_tls_engine_configure: engine not defined\n");
457 defined_engine = ENGINE_by_id((
const char *)engine_id->
s);
458 if (!defined_engine) {
459 coap_log_warn(
"coap_tls_engine_configure: engine '%s' not known\n", engine_id->
s);
469 while (get_split_conf_entry(&start, end - start,
"pre-cmd", &p1, &p2)) {
470 if (!ENGINE_ctrl_cmd_string(defined_engine, (
const char *)p1->
s, p2 ? (
const char *)p2->
s : NULL,
472 coap_log_warn(
"coap_tls_engine_configure: engine %s pre-cmd '%s:%s' failed\n",
473 (
const char *)engine_id->
s,
474 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
478 engine_id->
s, p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
487 if (!ENGINE_init(defined_engine)) {
488 coap_log_warn(
"coap_tls_engine_configure: %s failed initialization\n", (
const char *)engine_id->
s);
491 done_engine_init = 1;
493 (
const char *)engine_id->
s);
498 while (get_split_conf_entry(&start, end - start,
"post-cmd", &p1, &p2)) {
499 if (!ENGINE_ctrl_cmd_string(defined_engine, (
const char *)p1->
s, p2 ? (
const char *)p2->
s : NULL,
501 coap_log_warn(
"coap_tls_engine_configure: %s post-cmd '%s:%s' failed\n", (
const char *)engine_id->
s,
502 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
506 (
const char *)engine_id->
s,
507 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
515 if (!get_split_conf_entry(&start, end - start,
"enable-methods", &p1, &p2)) {
516 coap_log_warn(
"coap_tls_engine_configure: enable-methods not found\n");
519 defaults = strtoul((
const char *)p1->
s, NULL, 0);
520 if (!ENGINE_set_default(defined_engine, defaults)) {
521 coap_log_warn(
"coap_tls_engine_configure: enable-methods 0x%x invalid\n", defaults);
536 ENGINE_free(defined_engine);
537 if (done_engine_init)
538 ENGINE_finish(defined_engine);
539 defined_engine = NULL;
548 if (defined_engine) {
549 ENGINE_finish(defined_engine);
550 defined_engine = NULL;
571typedef struct coap_ssl_data {
580coap_dgram_create(BIO *a) {
581 coap_ssl_data *data = NULL;
582 data = malloc(
sizeof(coap_ssl_data));
586 BIO_set_data(a, data);
587 memset(data, 0x00,
sizeof(coap_ssl_data));
592coap_dgram_destroy(BIO *a) {
596 data = (coap_ssl_data *)BIO_get_data(a);
597 BIO_set_data(a, NULL);
604coap_dgram_read(BIO *a,
char *out,
int outl) {
606 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
609 if (data != NULL && data->pdu_len > 0) {
610 if (outl < (
int)data->pdu_len) {
611 memcpy(out, data->pdu, outl);
614 memcpy(out, data->pdu, data->pdu_len);
615 ret = (int)data->pdu_len;
617 if (!data->peekmode) {
624 BIO_clear_retry_flags(a);
626 BIO_set_retry_read(a);
632coap_dgram_write(BIO *a,
const char *in,
int inl) {
634 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
636 if (data && data->session) {
639 && data->session->endpoint == NULL
643 BIO_clear_retry_flags(a);
647 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
650 BIO_clear_retry_flags(a);
652 if (ret < 0 && (errno == ENOTCONN || errno == ECONNREFUSED))
654 BIO_set_retry_write(a);
657 BIO_clear_retry_flags(a);
664coap_dgram_puts(BIO *a,
const char *pstr) {
665 return coap_dgram_write(a, pstr, (
int)strlen(pstr));
669coap_dgram_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
671 coap_ssl_data *data = BIO_get_data(a);
676 case BIO_CTRL_GET_CLOSE:
677 ret = BIO_get_shutdown(a);
679 case BIO_CTRL_SET_CLOSE:
680 BIO_set_shutdown(a, (
int)num);
682 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
684 data->peekmode = (unsigned)num;
688 case BIO_CTRL_DGRAM_CONNECT:
691 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
692 case BIO_CTRL_DGRAM_GET_MTU:
693 case BIO_CTRL_DGRAM_SET_MTU:
694 case BIO_CTRL_DGRAM_QUERY_MTU:
695 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
700 case BIO_CTRL_DGRAM_MTU_DISCOVER:
701 case BIO_CTRL_DGRAM_SET_CONNECTED:
703 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
706 ((
struct timeval *)ptr)->tv_usec);
711 case BIO_C_FILE_SEEK:
712 case BIO_C_FILE_TELL:
714 case BIO_CTRL_PENDING:
715 case BIO_CTRL_WPENDING:
716 case BIO_CTRL_DGRAM_GET_PEER:
717 case BIO_CTRL_DGRAM_SET_PEER:
718 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
719 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
720 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
721 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
722 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
723 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
724 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
725 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
734coap_dtls_generate_cookie(SSL *ssl,
735 unsigned char *cookie,
736 unsigned int *cookie_len) {
737 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
738 coap_dtls_context_t *dtls = ctx ? (coap_dtls_context_t *)SSL_CTX_get_app_data(ctx) : NULL;
739 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
742 int r = HMAC_Init_ex(dtls->cookie_hmac, NULL, 0, NULL, NULL);
743 r &= HMAC_Update(dtls->cookie_hmac,
744 (
const uint8_t *)&data->session->addr_info.local.addr,
745 (
size_t)data->session->addr_info.local.size);
746 r &= HMAC_Update(dtls->cookie_hmac,
747 (
const uint8_t *)&data->session->addr_info.remote.addr,
748 (
size_t)data->session->addr_info.remote.size);
749 r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
756coap_dtls_verify_cookie(SSL *ssl,
757 const uint8_t *cookie,
758 unsigned int cookie_len) {
761 if (coap_dtls_generate_cookie(ssl, hmac, &len) &&
762 cookie_len == len && memcmp(cookie, hmac, len) == 0)
768#if COAP_CLIENT_SUPPORT
770coap_dtls_psk_client_callback(SSL *ssl,
773 unsigned int max_identity_len,
775 unsigned int max_psk_len) {
777 coap_openssl_context_t *o_context;
785 if (c_session == NULL || c_session->
context == NULL)
788 if (o_context == NULL)
792 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
793 temp.
length = strlen((
const char *)temp.
s);
797 (
const char *)temp.
s);
809 if (cpsk_info == NULL)
814 psk_identity = &cpsk_info->
identity;
815 psk_key = &cpsk_info->
key;
821 if (psk_identity == NULL || psk_key == NULL) {
827 if (!max_identity_len)
830 if (psk_identity->
length > max_identity_len) {
831 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
835 max_identity_len = (
unsigned int)psk_identity->
length;
837 memcpy(identity, psk_identity->
s, max_identity_len);
838 identity[max_identity_len] =
'\000';
840 if (psk_key->
length > max_psk_len) {
845 max_psk_len = (
unsigned int)psk_key->
length;
847 memcpy(psk, psk_key->
s, max_psk_len);
852#if COAP_SERVER_SUPPORT
854coap_dtls_psk_server_callback(
856 const char *identity,
858 unsigned int max_psk_len
866 if (c_session == NULL || c_session->
context == NULL)
872 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
873 lidentity.
length = strlen((
const char *)lidentity.
s);
877 (
int)lidentity.
length, (
const char *)lidentity.
s);
892 if (psk_key->
length > max_psk_len) {
897 max_psk_len = (
unsigned int)psk_key->
length;
899 memcpy(psk, psk_key->
s, max_psk_len);
905ssl_function_definition(
unsigned long e) {
906#if OPENSSL_VERSION_NUMBER >= 0x30000000L
910 static char buff[80];
912 snprintf(buff,
sizeof(buff),
" at %s:%s",
913 ERR_lib_error_string(e), ERR_func_error_string(e));
919coap_dtls_info_callback(
const SSL *ssl,
int where,
int ret) {
922 int w = where &~SSL_ST_MASK;
926 "coap_dtls_info_callback: session not determined, where 0x%0x and ret 0x%0x\n", where, ret);
929 if (w & SSL_ST_CONNECT)
930 pstr =
"SSL_connect";
931 else if (w & SSL_ST_ACCEPT)
936 if (where & SSL_CB_LOOP) {
939 }
else if (where & SSL_CB_ALERT) {
941 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
942 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) {
944 if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY)
948 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
951 SSL_alert_type_string_long(ret),
952 SSL_alert_desc_string_long(ret));
953 }
else if (where & SSL_CB_EXIT) {
959 while ((e = ERR_get_error()))
962 ssl_function_definition(e));
964 }
else if (ret < 0) {
966 int err = SSL_get_error(ssl, ret);
967 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE &&
968 err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT &&
969 err != SSL_ERROR_WANT_X509_LOOKUP) {
973 while ((e = ERR_get_error()))
976 ssl_function_definition(e));
982 if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
988coap_sock_create(BIO *a) {
994coap_sock_destroy(BIO *a) {
1006coap_sock_read(BIO *a,
char *out,
int outl) {
1010 if (session && out != NULL) {
1015 BIO_set_retry_read(a);
1018 BIO_clear_retry_flags(a);
1031coap_sock_write(BIO *a,
const char *in,
int inl) {
1040 (
const uint8_t *)in,
1044 BIO_clear_retry_flags(a);
1046 BIO_set_retry_read(a);
1049 BIO_clear_retry_flags(a);
1053 (errno == EPIPE || errno == ECONNRESET)) {
1073coap_sock_puts(BIO *a,
const char *pstr) {
1074 return coap_sock_write(a, pstr, (
int)strlen(pstr));
1078coap_sock_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
1089 case BIO_CTRL_SET_CLOSE:
1091 case BIO_CTRL_FLUSH:
1095 case BIO_CTRL_GET_CLOSE:
1104coap_set_user_prefs(SSL_CTX *ctx) {
1105 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
1107#ifdef COAP_OPENSSL_SIGALGS
1108 SSL_CTX_set1_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
1109 SSL_CTX_set1_client_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
1112#if OPENSSL_VERSION_NUMBER >= 0x10101000L && defined(COAP_OPENSSL_GROUPS)
1113 SSL_CTX_set1_groups_list(ctx, COAP_OPENSSL_GROUPS);
1117#if COAP_DTLS_RETRANSMIT_MS != 1000
1118#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1120timer_cb(SSL *s,
unsigned int timer_us) {
1123 return COAP_DTLS_RETRANSMIT_MS * 1000;
1125 return 2 * timer_us;
1132 coap_openssl_context_t *context;
1137 uint8_t cookie_secret[32];
1139 memset(context, 0,
sizeof(coap_openssl_context_t));
1142 context->dtls.ctx = SSL_CTX_new(DTLS_method());
1143 if (!context->dtls.ctx)
1145 SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
1146 SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
1147 SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
1148 coap_set_user_prefs(context->dtls.ctx);
1149 memset(cookie_secret, 0,
sizeof(cookie_secret));
1150 if (!RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
1152 "Insufficient entropy for random cookie generation");
1155 context->dtls.cookie_hmac = HMAC_CTX_new();
1156 if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
1157 EVP_sha256(), NULL))
1159 SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
1160 SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
1161 SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
1162 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
1163#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1164 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_LEGACY_SERVER_CONNECT);
1166 context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM,
"coapdgram");
1167 if (!context->dtls.meth)
1169 context->dtls.bio_addr = BIO_ADDR_new();
1170 if (!context->dtls.bio_addr)
1172 BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
1173 BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
1174 BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
1175 BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
1176 BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
1177 BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
1179#if !COAP_DISABLE_TCP
1181 context->tls.ctx = SSL_CTX_new(TLS_method());
1182 if (!context->tls.ctx)
1184 SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
1185 SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
1186 coap_set_user_prefs(context->tls.ctx);
1187 SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
1188 context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET,
"coapsock");
1189 if (!context->tls.meth)
1191 BIO_meth_set_write(context->tls.meth, coap_sock_write);
1192 BIO_meth_set_read(context->tls.meth, coap_sock_read);
1193 BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
1194 BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
1195 BIO_meth_set_create(context->tls.meth, coap_sock_create);
1196 BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
1207#if COAP_SERVER_SUPPORT
1212 coap_openssl_context_t *o_context =
1216 if (!setup_data || !o_context)
1219 SSL_CTX_set_psk_server_callback(o_context->dtls.ctx,
1220 coap_dtls_psk_server_callback);
1221#if !COAP_DISABLE_TCP
1222 SSL_CTX_set_psk_server_callback(o_context->tls.ctx,
1223 coap_dtls_psk_server_callback);
1229 SSL_CTX_use_psk_identity_hint(o_context->dtls.ctx, hint);
1230#if !COAP_DISABLE_TCP
1231 SSL_CTX_use_psk_identity_hint(o_context->tls.ctx, hint);
1235#if OPENSSL_VERSION_NUMBER < 0x10101000L
1236 SSL_CTX_set_tlsext_servername_arg(o_context->dtls.ctx,
1238 SSL_CTX_set_tlsext_servername_callback(o_context->dtls.ctx,
1239 psk_tls_server_name_call_back);
1240#if !COAP_DISABLE_TCP
1241 SSL_CTX_set_tlsext_servername_arg(o_context->tls.ctx,
1243 SSL_CTX_set_tlsext_servername_callback(o_context->tls.ctx,
1244 psk_tls_server_name_call_back);
1247 SSL_CTX_set_client_hello_cb(o_context->dtls.ctx,
1248 psk_tls_client_hello_call_back,
1250#if !COAP_DISABLE_TCP
1251 SSL_CTX_set_client_hello_cb(o_context->tls.ctx,
1252 psk_tls_client_hello_call_back,
1258 if (!o_context->dtls.ssl) {
1260 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
1261 if (!o_context->dtls.ssl)
1263 bio = BIO_new(o_context->dtls.meth);
1265 SSL_free(o_context->dtls.ssl);
1266 o_context->dtls.ssl = NULL;
1269 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1270 SSL_set_app_data(o_context->dtls.ssl, NULL);
1271 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1277 o_context->psk_pki_enabled |= IS_PSK;
1282#if COAP_CLIENT_SUPPORT
1287 coap_openssl_context_t *o_context =
1291 if (!setup_data || !o_context)
1294 if (!o_context->dtls.ssl) {
1296 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
1297 if (!o_context->dtls.ssl)
1299 bio = BIO_new(o_context->dtls.meth);
1301 SSL_free(o_context->dtls.ssl);
1302 o_context->dtls.ssl = NULL;
1305 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1306 SSL_set_app_data(o_context->dtls.ssl, NULL);
1307 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1316 o_context->psk_pki_enabled |= IS_PSK;
1322map_key_type(
int asn1_private_key_type
1324 switch (asn1_private_key_type) {
1326 return EVP_PKEY_NONE;
1328 return EVP_PKEY_RSA;
1330 return EVP_PKEY_RSA2;
1332 return EVP_PKEY_DSA;
1334 return EVP_PKEY_DSA1;
1336 return EVP_PKEY_DSA2;
1338 return EVP_PKEY_DSA3;
1340 return EVP_PKEY_DSA4;
1344 return EVP_PKEY_DHX;
1348 return EVP_PKEY_HMAC;
1350 return EVP_PKEY_CMAC;
1352 return EVP_PKEY_TLS1_PRF;
1354 return EVP_PKEY_HKDF;
1356 coap_log_warn(
"*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
1357 asn1_private_key_type);
1362#if !COAP_DISABLE_TCP
1363static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1365#if COAP_SERVER_SUPPORT
1368 const unsigned char **out,
1369 unsigned char *outlen,
1370 const unsigned char *in,
1374 unsigned char *tout = NULL;
1377 return SSL_TLSEXT_ERR_NOACK;
1378 ret = SSL_select_next_proto(&tout,
1385 return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
1391add_ca_to_cert_store(X509_STORE *st, X509 *x509) {
1395 while (ERR_get_error() != 0) {
1398 if (!X509_STORE_add_cert(st, x509)) {
1399 while ((e = ERR_get_error()) != 0) {
1400 int r = ERR_GET_REASON(e);
1401 if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1404 ERR_reason_error_string(e),
1405 ssl_function_definition(e));
1412missing_ENGINE_load_cert(ENGINE *engine,
const char *cert_id) {
1414 const char *cert_id;
1418 params.cert_id = cert_id;
1422 if (!ENGINE_ctrl_cmd(engine,
"LOAD_CERT_CTRL", 0, ¶ms, NULL, 1)) {
1429check_pkcs11_engine(
void) {
1430 static int already_tried = 0;
1435 if (!pkcs11_engine) {
1436 pkcs11_engine = ENGINE_by_id(COAP_OPENSSL_PKCS11_ENGINE_ID);
1437 if (!pkcs11_engine) {
1438 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support - need OpenSSL %s engine\n",
1439 COAP_OPENSSL_PKCS11_ENGINE_ID);
1443 if (!ENGINE_init(pkcs11_engine)) {
1445 ENGINE_free(pkcs11_engine);
1446 pkcs11_engine = NULL;
1447 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1455 ENGINE_free(pkcs11_engine);
1460#if OPENSSL_VERSION_NUMBER < 0x10101000L && COAP_SERVER_SUPPORT
1463install_engine_public_cert_ctx(ENGINE *engine, SSL_CTX *ctx,
1464 const char *public_cert) {
1467 x509 = missing_ENGINE_load_cert(engine, public_cert);
1475 if (!SSL_CTX_use_certificate(ctx, x509)) {
1476 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1488install_engine_private_key_ctx(ENGINE *engine, SSL_CTX *ctx,
1489 const char *private_key) {
1490 EVP_PKEY *pkey = ENGINE_load_private_key(engine,
1501 if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
1502 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1506 EVP_PKEY_free(pkey);
1509 EVP_PKEY_free(pkey);
1514install_engine_ca_ctx(ENGINE *engine, SSL_CTX *ctx,
const char *ca) {
1518 x509 = missing_ENGINE_load_cert(engine,
1522 "%s CA Certificate\n",
1527 if (!SSL_CTX_add_client_CA(ctx, x509)) {
1528 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1529 "%s CA Certificate\n",
1535 st = SSL_CTX_get_cert_store(ctx);
1536 add_ca_to_cert_store(st, x509);
1542load_in_cas_ctx(SSL_CTX *ctx,
1543 const char *ca_file) {
1544 STACK_OF(X509_NAME) *cert_names;
1548 char *rw_var = NULL;
1549 cert_names = SSL_load_client_CA_file(ca_file);
1550 if (cert_names != NULL)
1551 SSL_CTX_set_client_CA_list(ctx, cert_names);
1553 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1560 st = SSL_CTX_get_cert_store(ctx);
1561 in = BIO_new(BIO_s_file());
1563 memcpy(&rw_var, &ca_file,
sizeof(rw_var));
1564 if (!BIO_read_filename(in, rw_var)) {
1571 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
1573 add_ca_to_cert_store(st, x);
1581setup_pki_server(SSL_CTX *ctx,
1597 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1599 SSL_FILETYPE_PEM))) {
1609 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1611 if (!pkey || !SSL_CTX_use_PrivateKey(ctx, pkey)) {
1615 EVP_PKEY_free(pkey);
1623 EVP_PKEY_free(pkey);
1635 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1637 SSL_FILETYPE_ASN1))) {
1655 if (!check_pkcs11_engine()) {
1660 if (ENGINE_ctrl_cmd_string(pkcs11_engine,
1663 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1668 if (!install_engine_private_key_ctx(pkcs11_engine, ctx,
1676 if (!defined_engine ||
1677 !install_engine_private_key_ctx(defined_engine, ctx,
1706 if (!(SSL_CTX_use_certificate_file(ctx,
1708 SSL_FILETYPE_PEM))) {
1714 if (!SSL_CTX_use_certificate_chain_file(ctx,
1726 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1728 if (!cert || !SSL_CTX_use_certificate(ctx, cert)) {
1752 if (!(SSL_CTX_use_certificate_file(ctx,
1754 SSL_FILETYPE_ASN1))) {
1762 !(SSL_CTX_use_certificate_ASN1(ctx,
1771 if (!check_pkcs11_engine()) {
1774 if (!install_engine_public_cert_ctx(pkcs11_engine, ctx,
1782 if (!defined_engine ||
1783 !install_engine_public_cert_ctx(defined_engine, ctx,
1821 X509_STORE *st = SSL_CTX_get_cert_store(ctx);
1825 if ((x = PEM_read_bio_X509(bp, NULL, NULL, NULL)) == NULL)
1827 add_ca_to_cert_store(st, x);
1828 SSL_CTX_add_client_CA(ctx, x);
1844 if (!(SSL_CTX_use_certificate_file(ctx,
1846 SSL_FILETYPE_ASN1))) {
1859 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1867 st = SSL_CTX_get_cert_store(ctx);
1868 add_ca_to_cert_store(st, x509);
1873 if (!check_pkcs11_engine()) {
1876 if (!install_engine_ca_ctx(pkcs11_engine, ctx,
1884 if (!defined_engine ||
1885 !install_engine_ca_ctx(defined_engine, ctx,
1904#if OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT
1907install_engine_public_cert(ENGINE *engine, SSL *ssl,
const char *public_cert,
1911 x509 = missing_ENGINE_load_cert(engine, public_cert);
1919 if (!SSL_use_certificate(ssl, x509)) {
1920 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1932install_engine_private_key(ENGINE *engine, SSL *ssl,
const char *private_key,
1934 EVP_PKEY *pkey = ENGINE_load_private_key(engine,
1945 if (!SSL_use_PrivateKey(ssl, pkey)) {
1946 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1950 EVP_PKEY_free(pkey);
1953 EVP_PKEY_free(pkey);
1958install_engine_ca(ENGINE *engine, SSL *ssl,
const char *ca,
1961 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1966 "%s CA Certificate (no ctx)\n",
1971 x509 = missing_ENGINE_load_cert(engine,
1975 "%s CA Certificate\n",
1980 if (!SSL_add_client_CA(ssl, x509)) {
1981 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1982 "%s CA Certificate\n",
1988 st = SSL_CTX_get_cert_store(ctx);
1989 add_ca_to_cert_store(st, x509);
1995load_in_cas(SSL *ssl,
2000 char *rw_var = NULL;
2001 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2004 STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(ca_file);
2006 if (cert_names != NULL)
2007 SSL_set_client_CA_list(ssl, cert_names);
2017 in = BIO_new(BIO_s_file());
2019 memcpy(&rw_var, &ca_file,
sizeof(rw_var));
2020 if (!BIO_read_filename(in, rw_var)) {
2024 st = SSL_CTX_get_cert_store(ctx);
2026 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
2028 add_ca_to_cert_store(st, x);
2036setup_pki_ssl(SSL *ssl,
2052 if (!(SSL_use_PrivateKey_file(ssl,
2054 SSL_FILETYPE_PEM))) {
2064 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
2066 if (!pkey || !SSL_use_PrivateKey(ssl, pkey)) {
2070 EVP_PKEY_free(pkey);
2078 EVP_PKEY_free(pkey);
2090 if (!(SSL_use_PrivateKey_file(ssl,
2092 SSL_FILETYPE_ASN1))) {
2110 if (!check_pkcs11_engine()) {
2115 if (ENGINE_ctrl_cmd_string(pkcs11_engine,
2118 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
2123 if (!install_engine_private_key(pkcs11_engine, ssl,
2132 if (!defined_engine ||
2133 !install_engine_private_key(defined_engine, ssl,
2165 if (!(SSL_use_certificate_file(ssl,
2167 SSL_FILETYPE_PEM))) {
2173 if (!SSL_use_certificate_chain_file(ssl,
2185 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
2187 if (!cert || !SSL_use_certificate(ssl, cert)) {
2211 if (!(SSL_use_certificate_file(ssl,
2213 SSL_FILETYPE_ASN1))) {
2221 !(SSL_use_certificate_ASN1(ssl,
2230 if (!check_pkcs11_engine()) {
2233 if (!install_engine_public_cert(pkcs11_engine, ssl,
2242 if (!defined_engine ||
2243 !install_engine_public_cert(defined_engine, ssl,
2282 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2284 X509_STORE *st = ctx? SSL_CTX_get_cert_store(ctx) : NULL;
2288 if ((x = PEM_read_bio_X509(bp, NULL, 0, NULL)) == NULL)
2291 add_ca_to_cert_store(st, x);
2292 SSL_add_client_CA(ssl, x);
2308 if (!(SSL_use_certificate_file(ssl,
2310 SSL_FILETYPE_ASN1))) {
2322 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2325 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
2334 st = ctx ? SSL_CTX_get_cert_store(ctx) : NULL;
2336 add_ca_to_cert_store(st, x509);
2341 if (!check_pkcs11_engine()) {
2344 if (!install_engine_ca(pkcs11_engine, ssl,
2353 if (!defined_engine ||
2354 !install_engine_ca(defined_engine, ssl,
2375get_san_or_cn_from_cert(X509 *x509) {
2379 STACK_OF(GENERAL_NAME) *san_list;
2382 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
2384 int san_count = sk_GENERAL_NAME_num(san_list);
2386 for (n = 0; n < san_count; n++) {
2387 const GENERAL_NAME *name = sk_GENERAL_NAME_value(san_list, n);
2389 if (name && name->type == GEN_DNS) {
2390 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
2393 if (ASN1_STRING_length(name->d.dNSName) != (
int)strlen(dns_name))
2395 cn = OPENSSL_strdup(dns_name);
2396 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
2400 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
2403 X509_NAME_oneline(X509_get_subject_name(x509), buffer,
sizeof(buffer));
2406 n = (int)strlen(buffer) - 3;
2409 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
2410 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
2419 char *ecn = strchr(cn,
'/');
2421 return OPENSSL_strndup(cn, ecn-cn);
2423 return OPENSSL_strdup(cn);
2431tls_verify_call_back(
int preverify_ok, X509_STORE_CTX *ctx) {
2432 int index = SSL_get_ex_data_X509_STORE_CTX_idx();
2433 SSL *ssl = index >= 0 ? X509_STORE_CTX_get_ex_data(ctx, index) : NULL;
2435 coap_openssl_context_t *context = (session && session->
context) ?
2438 int depth = X509_STORE_CTX_get_error_depth(ctx);
2439 int err = X509_STORE_CTX_get_error(ctx);
2440 X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
2441 char *cn = x509 ? get_san_or_cn_from_cert(x509) : NULL;
2442 int keep_preverify_ok = preverify_ok;
2445 depth, err, preverify_ok, cn);
2447 X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
2451 if (!preverify_ok) {
2453 case X509_V_ERR_CERT_NOT_YET_VALID:
2454 case X509_V_ERR_CERT_HAS_EXPIRED:
2458 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2462 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2466 case X509_V_ERR_UNABLE_TO_GET_CRL:
2470 case X509_V_ERR_CRL_NOT_YET_VALID:
2471 case X509_V_ERR_CRL_HAS_EXPIRED:
2475 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2476 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2477 case X509_V_ERR_AKID_SKID_MISMATCH:
2487 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
2488 X509_STORE_CTX_set_error(ctx, err);
2490 if (!preverify_ok) {
2491 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
2494 "Unknown CA", cn ? cn :
"?", depth);
2498 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
2503 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
2508 int length = i2d_X509(x509, NULL);
2510 uint8_t *base_buf2 = base_buf = length > 0 ? OPENSSL_malloc(length) : NULL;
2515 assert(i2d_X509(x509, &base_buf2) > 0);
2519 depth, preverify_ok,
2523 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
2525 X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
2529 OPENSSL_free(base_buf);
2531 X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
2536 return preverify_ok;
2539#if COAP_SERVER_SUPPORT
2540#if OPENSSL_VERSION_NUMBER < 0x10101000L
2550tls_secret_call_back(SSL *ssl,
2553 STACK_OF(SSL_CIPHER) *peer_ciphers,
2557 int psk_requested = 0;
2562 assert(session != NULL);
2563 assert(session->
context != NULL);
2564 if (session == NULL ||
2572 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2573 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2576 SSL_CIPHER_get_name(peer_cipher));
2577 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2583 if (!psk_requested) {
2590 SSL_VERIFY_CLIENT_ONCE |
2591 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2592 tls_verify_call_back);
2594 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2603 X509_VERIFY_PARAM *param;
2605 param = X509_VERIFY_PARAM_new();
2606 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2607 SSL_set1_param(ssl, param);
2608 X509_VERIFY_PARAM_free(param);
2630 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2631 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2646tls_server_name_call_back(SSL *ssl,
2652 return SSL_TLSEXT_ERR_NOACK;
2658 coap_openssl_context_t *context = (session && session->
context) ?
2660 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2664 return SSL_TLSEXT_ERR_NOACK;
2666 if (!sni || !sni[0]) {
2669 for (i = 0; i < context->sni_count; i++) {
2670 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2674 if (i == context->sni_count) {
2683 return SSL_TLSEXT_ERR_ALERT_FATAL;
2688 ctx = SSL_CTX_new(DTLS_method());
2691 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2692 SSL_CTX_set_app_data(ctx, &context->dtls);
2693 SSL_CTX_set_read_ahead(ctx, 1);
2694 coap_set_user_prefs(ctx);
2695 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2696 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2697 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2698 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2700#if !COAP_DISABLE_TCP
2703 ctx = SSL_CTX_new(TLS_method());
2706 SSL_CTX_set_app_data(ctx, &context->tls);
2707 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2708 coap_set_user_prefs(ctx);
2709 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2710 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2713 sni_setup_data = *setup_data;
2714 sni_setup_data.
pki_key = *new_entry;
2715 setup_pki_server(ctx, &sni_setup_data);
2717 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2718 (context->sni_count+1)*
sizeof(sni_entry));
2719 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2720 context->sni_entry_list[context->sni_count].ctx = ctx;
2721 context->sni_count++;
2723 SSL_set_SSL_CTX(ssl, context->sni_entry_list[i].ctx);
2724 SSL_clear_options(ssl, 0xFFFFFFFFL);
2725 SSL_set_options(ssl, SSL_CTX_get_options(context->sni_entry_list[i].ctx));
2732 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2733 return SSL_TLSEXT_ERR_OK;
2736 return SSL_TLSEXT_ERR_ALERT_WARNING;
2748psk_tls_server_name_call_back(SSL *ssl,
2755 return SSL_TLSEXT_ERR_NOACK;
2761 coap_openssl_context_t *o_context = (c_session && c_session->
context) ?
2763 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2768 return SSL_TLSEXT_ERR_ALERT_FATAL;
2770 if (!sni || !sni[0]) {
2773 for (i = 0; i < o_context->psk_sni_count; i++) {
2774 if (!strcasecmp(sni, (
char *)o_context->psk_sni_entry_list[i].sni)) {
2778 if (i == o_context->psk_sni_count) {
2787 return SSL_TLSEXT_ERR_ALERT_FATAL;
2792 ctx = SSL_CTX_new(DTLS_method());
2795 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2796 SSL_CTX_set_app_data(ctx, &o_context->dtls);
2797 SSL_CTX_set_read_ahead(ctx, 1);
2798 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2799 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2800 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2801 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2802 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2804#if !COAP_DISABLE_TCP
2807 ctx = SSL_CTX_new(TLS_method());
2810 SSL_CTX_set_app_data(ctx, &o_context->tls);
2811 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2812 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2813 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2814 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2818 o_context->psk_sni_entry_list =
2819 OPENSSL_realloc(o_context->psk_sni_entry_list,
2820 (o_context->psk_sni_count+1)*
sizeof(psk_sni_entry));
2821 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2822 OPENSSL_strdup(sni);
2823 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2825 o_context->psk_sni_entry_list[o_context->psk_sni_count].ctx =
2827 o_context->psk_sni_count++;
2829 SSL_set_SSL_CTX(ssl, o_context->psk_sni_entry_list[i].ctx);
2830 SSL_clear_options(ssl, 0xFFFFFFFFL);
2831 SSL_set_options(ssl,
2832 SSL_CTX_get_options(o_context->psk_sni_entry_list[i].ctx));
2834 &o_context->psk_sni_entry_list[i].psk_info.key);
2835 snprintf(lhint,
sizeof(lhint),
"%.*s",
2836 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2837 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2838 SSL_use_psk_identity_hint(ssl, lhint);
2845 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2846 return SSL_TLSEXT_ERR_OK;
2849 return SSL_TLSEXT_ERR_ALERT_WARNING;
2862tls_client_hello_call_back(SSL *ssl,
2867 coap_openssl_context_t *dtls_context;
2869 int psk_requested = 0;
2870 const unsigned char *out;
2874 *al = SSL_AD_INTERNAL_ERROR;
2875 return SSL_CLIENT_HELLO_ERROR;
2878 assert(session != NULL);
2879 assert(session->
context != NULL);
2881 if (session == NULL ||
2884 *al = SSL_AD_INTERNAL_ERROR;
2885 return SSL_CLIENT_HELLO_ERROR;
2888 setup_data = &dtls_context->setup_data;
2896 size_t len = SSL_client_hello_get0_ciphers(ssl, &out);
2897 STACK_OF(SSL_CIPHER) *peer_ciphers = NULL;
2898 STACK_OF(SSL_CIPHER) *scsvc = NULL;
2900 if (len && SSL_bytes_to_cipher_list(ssl, out, len,
2901 SSL_client_hello_isv2(ssl),
2902 &peer_ciphers, &scsvc)) {
2904 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2905 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2908 "Client cipher: %s (%04x)\n",
2909 SSL_CIPHER_get_name(peer_cipher),
2910 SSL_CIPHER_get_protocol_id(peer_cipher));
2911 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2917 sk_SSL_CIPHER_free(peer_ciphers);
2918 sk_SSL_CIPHER_free(scsvc);
2921 if (psk_requested) {
2927 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2933 return SSL_CLIENT_HELLO_SUCCESS;
2943 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_client_certificate_type,
2946 for (ii = 0; ii < outlen; ii++) {
2962 *al = SSL_AD_UNSUPPORTED_EXTENSION;
2963 return SSL_CLIENT_HELLO_ERROR;
2972 coap_openssl_context_t *context =
2974 const char *sni =
"";
2975 char *sni_tmp = NULL;
2978 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2980 (((out[0]<<8) + out[1] +2) == (
int)outlen) &&
2981 out[2] == TLSEXT_NAMETYPE_host_name &&
2982 (((out[3]<<8) + out[4] +2 +3) == (
int)outlen)) {
2986 sni_tmp = OPENSSL_malloc(outlen+1);
2987 sni_tmp[outlen] =
'\000';
2988 memcpy(sni_tmp, out, outlen);
2992 for (i = 0; i < context->sni_count; i++) {
2993 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2997 if (i == context->sni_count) {
3007 *al = SSL_AD_UNRECOGNIZED_NAME;
3008 return SSL_CLIENT_HELLO_ERROR;
3012 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
3013 (context->sni_count+1)*
sizeof(sni_entry));
3014 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
3015 context->sni_entry_list[context->sni_count].pki_key = *new_entry;
3016 context->sni_count++;
3019 OPENSSL_free(sni_tmp);
3021 sni_setup_data = *setup_data;
3022 sni_setup_data.
pki_key = context->sni_entry_list[i].pki_key;
3034 SSL_VERIFY_CLIENT_ONCE |
3035 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
3036 tls_verify_call_back);
3038 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
3047 X509_VERIFY_PARAM *param;
3049 param = X509_VERIFY_PARAM_new();
3050 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
3051 SSL_set1_param(ssl, param);
3052 X509_VERIFY_PARAM_free(param);
3059 return SSL_CLIENT_HELLO_SUCCESS;
3071psk_tls_client_hello_call_back(SSL *ssl,
3076 coap_openssl_context_t *o_context;
3078 const unsigned char *out;
3084 if (!c_session || !c_session->
context) {
3097 const char *sni =
"";
3098 char *sni_tmp = NULL;
3101 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
3103 (((out[0]<<8) + out[1] +2) == (
int)outlen) &&
3104 out[2] == TLSEXT_NAMETYPE_host_name &&
3105 (((out[3]<<8) + out[4] +2 +3) == (
int)outlen)) {
3109 sni_tmp = OPENSSL_malloc(outlen+1);
3111 sni_tmp[outlen] =
'\000';
3112 memcpy(sni_tmp, out, outlen);
3117#if OPENSSL_VERSION_NUMBER < 0x10101000L
3120 for (i = 0; i < o_context->psk_sni_count; i++) {
3121 if (strcasecmp(sni, o_context->psk_sni_entry_list[i].sni) == 0) {
3125 if (i == o_context->psk_sni_count) {
3138 *al = SSL_AD_UNRECOGNIZED_NAME;
3139 return SSL_CLIENT_HELLO_ERROR;
3142#if OPENSSL_VERSION_NUMBER < 0x10101000L
3143 psk_sni_entry *tmp_entry;
3145 OPENSSL_realloc(o_context->psk_sni_entry_list,
3146 (o_context->psk_sni_count+1)*
sizeof(sni_entry));
3148 o_context->psk_sni_entry_list = tmp_entry;
3149 o_context->psk_sni_entry_list[o_context->psk_sni_count]
3151 OPENSSL_strdup(sni);
3152 if (o_context->psk_sni_entry_list[o_context->psk_sni_count].sni) {
3153 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
3155 o_context->psk_sni_count++;
3159 new_entry = &o_context->psk_sni_entry_list[i].psk_info;
3164 OPENSSL_free(sni_tmp);
3176 if (new_entry->
hint.
s) {
3177 snprintf(lhint,
sizeof(lhint),
"%.*s",
3180 SSL_use_psk_identity_hint(ssl, lhint);
3183 return SSL_CLIENT_HELLO_SUCCESS;
3186 *al = SSL_AD_INTERNAL_ERROR;
3187 return SSL_CLIENT_HELLO_ERROR;
3196 coap_openssl_context_t *context =
3201 context->setup_data = *setup_data;
3207 if (!defined_engine) {
3208 coap_log_warn(
"setup_pki: OpenSSL Engine not configured, PKI not set up\n");
3214 if (!context->setup_data.verify_peer_cert) {
3216 context->setup_data.check_common_ca = 0;
3218 context->setup_data.allow_self_signed = 1;
3219 context->setup_data.allow_expired_certs = 1;
3220 context->setup_data.cert_chain_validation = 1;
3221 context->setup_data.cert_chain_verify_depth = 10;
3222 context->setup_data.check_cert_revocation = 1;
3223 context->setup_data.allow_no_crl = 1;
3224 context->setup_data.allow_expired_crl = 1;
3225 context->setup_data.allow_bad_md_hash = 1;
3226 context->setup_data.allow_short_rsa_length = 1;
3228#if COAP_SERVER_SUPPORT
3230 if (context->dtls.ctx) {
3232#if OPENSSL_VERSION_NUMBER < 0x10101000L
3233 if (!setup_pki_server(context->dtls.ctx, setup_data))
3242#if OPENSSL_VERSION_NUMBER < 0x10101000L
3243 if (SSLeay() >= 0x10101000L) {
3244 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
3245 "no certificate checking\n",
3246 OPENSSL_VERSION_NUMBER, SSLeay());
3248 SSL_CTX_set_tlsext_servername_arg(context->dtls.ctx, &context->setup_data);
3249 SSL_CTX_set_tlsext_servername_callback(context->dtls.ctx,
3250 tls_server_name_call_back);
3252 SSL_CTX_set_client_hello_cb(context->dtls.ctx,
3253 tls_client_hello_call_back,
3257#if !COAP_DISABLE_TCP
3258 if (context->tls.ctx) {
3260#if OPENSSL_VERSION_NUMBER < 0x10101000L
3261 if (!setup_pki_server(context->tls.ctx, setup_data))
3270#if OPENSSL_VERSION_NUMBER < 0x10101000L
3271 if (SSLeay() >= 0x10101000L) {
3272 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
3273 "no certificate checking\n",
3274 OPENSSL_VERSION_NUMBER, SSLeay());
3276 SSL_CTX_set_tlsext_servername_arg(context->tls.ctx, &context->setup_data);
3277 SSL_CTX_set_tlsext_servername_callback(context->tls.ctx,
3278 tls_server_name_call_back);
3280 SSL_CTX_set_client_hello_cb(context->tls.ctx,
3281 tls_client_hello_call_back,
3285 SSL_CTX_set_alpn_select_cb(context->tls.ctx, server_alpn_callback, NULL);
3293 if (!context->dtls.ssl) {
3295 context->dtls.ssl = SSL_new(context->dtls.ctx);
3296 if (!context->dtls.ssl)
3298 bio = BIO_new(context->dtls.meth);
3300 SSL_free(context->dtls.ssl);
3301 context->dtls.ssl = NULL;
3304 SSL_set_bio(context->dtls.ssl, bio, bio);
3305 SSL_set_app_data(context->dtls.ssl, NULL);
3306 SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
3309 context->psk_pki_enabled |= IS_PKI;
3318 const char *ca_file,
3321 coap_openssl_context_t *context =
3323 if (context->dtls.ctx) {
3324 if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) {
3326 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
3330#if !COAP_DISABLE_TCP
3331 if (context->tls.ctx) {
3332 if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) {
3334 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
3344 coap_openssl_context_t *context =
3346 return context->psk_pki_enabled ? 1 : 0;
3353 coap_openssl_context_t *context = (coap_openssl_context_t *)handle;
3355 if (context->dtls.ssl)
3356 SSL_free(context->dtls.ssl);
3357 if (context->dtls.ctx)
3358 SSL_CTX_free(context->dtls.ctx);
3359 if (context->dtls.cookie_hmac)
3360 HMAC_CTX_free(context->dtls.cookie_hmac);
3361 if (context->dtls.meth)
3362 BIO_meth_free(context->dtls.meth);
3363 if (context->dtls.bio_addr)
3364 BIO_ADDR_free(context->dtls.bio_addr);
3365#if !COAP_DISABLE_TCP
3366 if (context->tls.ctx)
3367 SSL_CTX_free(context->tls.ctx);
3368 if (context->tls.meth)
3369 BIO_meth_free(context->tls.meth);
3371 for (i = 0; i < context->sni_count; i++) {
3372 OPENSSL_free(context->sni_entry_list[i].sni);
3373#if OPENSSL_VERSION_NUMBER < 0x10101000L
3374 SSL_CTX_free(context->sni_entry_list[i].ctx);
3377 if (context->sni_count)
3378 OPENSSL_free(context->sni_entry_list);
3379#if OPENSSL_VERSION_NUMBER < 0x10101000L
3380 for (i = 0; i < context->psk_sni_count; i++) {
3381 OPENSSL_free((
char *)context->psk_sni_entry_list[i].sni);
3382 SSL_CTX_free(context->psk_sni_entry_list[i].ctx);
3384 if (context->psk_sni_count)
3385 OPENSSL_free(context->psk_sni_entry_list);
3390#if COAP_SERVER_SUPPORT
3394 SSL *nssl = NULL, *ssl = NULL;
3395 coap_ssl_data *data;
3396 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3401 nssl = SSL_new(dtls->ctx);
3404 nbio = BIO_new(dtls->meth);
3407 SSL_set_bio(nssl, nbio, nbio);
3408 SSL_set_app_data(nssl, NULL);
3409 SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE);
3410 SSL_set_mtu(nssl, (
long)session->
mtu);
3414 SSL_set_app_data(ssl, session);
3416 rbio = SSL_get_rbio(ssl);
3417 data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3420 data->session = session;
3424 if (psk_hint != NULL && psk_hint->
length) {
3425 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
3428 memcpy(hint, psk_hint->
s, psk_hint->
length);
3429 hint[psk_hint->
length] =
'\000';
3430 SSL_use_psk_identity_hint(ssl, hint);
3437 r = SSL_accept(ssl);
3439 int err = SSL_get_error(ssl, r);
3440 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3458#if COAP_CLIENT_SUPPORT
3462 coap_openssl_context_t *context =
3465 if (context->psk_pki_enabled & IS_PSK) {
3470 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
3474 SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
3475#if COAP_SERVER_SUPPORT
3476 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
3478 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
3481 SSL_set_max_proto_version(ssl, DTLS1_2_VERSION);
3483#if !COAP_DISABLE_TCP
3485 SSL_set_max_proto_version(ssl, TLS1_2_VERSION);
3488 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
3491 if ((context->psk_pki_enabled & IS_PKI) ||
3492 (context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
3499 if (!(context->psk_pki_enabled & IS_PKI)) {
3516#if !COAP_DISABLE_TCP
3518 SSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
3523 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
3529 X509_VERIFY_PARAM *param;
3531 param = X509_VERIFY_PARAM_new();
3532 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
3533 SSL_set1_param(ssl, param);
3534 X509_VERIFY_PARAM_free(param);
3541 SSL_VERIFY_CLIENT_ONCE |
3542 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
3543 tls_verify_call_back);
3545 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
3552#if COAP_DTLS_RETRANSMIT_MS != 1000
3553#if OPENSSL_VERSION_NUMBER >= 0x10101000L
3555 DTLS_set_timer_cb(ssl, timer_cb);
3566 coap_ssl_data *data;
3568 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3569 coap_dtls_context_t *dtls = &context->dtls;
3571 ssl = SSL_new(dtls->ctx);
3574 bio = BIO_new(dtls->meth);
3577 data = (coap_ssl_data *)BIO_get_data(bio);
3580 data->session = session;
3581 SSL_set_bio(ssl, bio, bio);
3582 SSL_set_app_data(ssl, session);
3583 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
3584 SSL_set_mtu(ssl, (
long)session->
mtu);
3586 if (!setup_client_ssl_session(session, ssl))
3591 r = SSL_connect(ssl);
3593 int ret = SSL_get_error(ssl, r);
3594 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3612 SSL *ssl = (SSL *)session->
tls;
3614 SSL_set_mtu(ssl, (
long)session->
mtu);
3620 SSL *ssl = (SSL *)session->
tls;
3622 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3623 int r = SSL_shutdown(ssl);
3628 session->
tls = NULL;
3636 const uint8_t *data,
size_t data_len) {
3638 SSL *ssl = (SSL *)session->
tls;
3649 r = SSL_write(ssl, data, (
int)data_len);
3652 int err = SSL_get_error(ssl, r);
3653 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3656 if (err == SSL_ERROR_ZERO_RETURN)
3658 else if (err == SSL_ERROR_SSL) {
3659 unsigned long e = ERR_get_error();
3661 coap_log_info(
"***%s: coap_dtls_send: cannot send PDU: %d: %s\n",
3663 ERR_GET_REASON(e), ERR_reason_error_string(e));
3666 coap_log_info(
"***%s: coap_dtls_send: cannot send PDU: %d\n",
3697 SSL *ssl = (SSL *)session->
tls;
3698 coap_ssl_data *ssl_data;
3702 rbio = ssl ? SSL_get_rbio(ssl) : NULL;
3703 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3704 return ssl_data ? ssl_data->timeout : 1000;
3713 SSL *ssl = (SSL *)session->
tls;
3717 (DTLSv1_handle_timeout(ssl) < 0)) {
3725#if COAP_SERVER_SUPPORT
3728 const uint8_t *data,
size_t data_len) {
3729 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3730 coap_ssl_data *ssl_data;
3734 SSL_set_mtu(dtls->ssl, (
long)session->
mtu);
3735 rbio = dtls->ssl ? SSL_get_rbio(dtls->ssl) : NULL;
3736 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3737 assert(ssl_data != NULL);
3742 if (ssl_data->pdu_len) {
3743 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3746 ssl_data->session = session;
3747 ssl_data->pdu = data;
3748 ssl_data->pdu_len = (unsigned)data_len;
3749 r = DTLSv1_listen(dtls->ssl, dtls->bio_addr);
3751 int err = SSL_get_error(dtls->ssl, r);
3752 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3772 coap_ssl_data *ssl_data;
3773 SSL *ssl = (SSL *)session->
tls;
3776#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3780 assert(ssl != NULL);
3782 int in_init = SSL_in_init(ssl);
3784 rbio = ssl ? SSL_get_rbio(ssl) : NULL;
3785 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3791 if (ssl_data->pdu_len) {
3792 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3795 ssl_data->pdu = data;
3796 ssl_data->pdu_len = (unsigned)data_len;
3799#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3803 r = SSL_read(ssl, pdu, (
int)
sizeof(pdu));
3809 ssl_data = (coap_ssl_data *)BIO_get_data(rbio);
3812 int err = SSL_get_error(ssl, r);
3813 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3814 if (in_init && SSL_is_init_finished(ssl)) {
3822 if (err == SSL_ERROR_ZERO_RETURN)
3824 else if (err == SSL_ERROR_SSL) {
3825 unsigned long e = ERR_get_error();
3827#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3828#include <openssl/proverr.h>
3829 if (ERR_GET_REASON(e) == PROV_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES && !retry) {
3835 coap_log_info(
"***%s: coap_dtls_receive: cannot recv PDU: %d: %s\n",
3837 ERR_GET_REASON(e), ERR_reason_error_string(e));
3840 coap_log_info(
"***%s: coap_dtls_receive: cannot send PDU %d\n",
3858 if (ssl_data && ssl_data->pdu_len) {
3860 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
3861 ssl_data->pdu_len = 0;
3862 ssl_data->pdu = NULL;
3869 unsigned int overhead = 37;
3870 const SSL_CIPHER *s_ciph = NULL;
3871 if (session->
tls != NULL)
3872 s_ciph = SSL_get_current_cipher(session->
tls);
3874 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
3876 const EVP_CIPHER *e_ciph;
3880 e_ciph = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(s_ciph));
3882 switch (EVP_CIPHER_mode(e_ciph)) {
3883 case EVP_CIPH_GCM_MODE:
3884 ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
3885 maclen = EVP_GCM_TLS_TAG_LEN;
3888 case EVP_CIPH_CCM_MODE:
3889 ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
3890 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3891 if (strstr(cipher,
"CCM8"))
3897 case EVP_CIPH_CBC_MODE:
3898 e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
3899 blocksize = EVP_CIPHER_block_size(e_ciph);
3900 ivlen = EVP_CIPHER_iv_length(e_ciph);
3902 maclen = EVP_MD_size(e_md);
3905 case EVP_CIPH_STREAM_CIPHER:
3912 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3919 overhead = DTLS1_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 + pad;
3924#if !COAP_DISABLE_TCP
3925#if COAP_CLIENT_SUPPORT
3931 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3932 coap_tls_context_t *tls = &context->tls;
3934 ssl = SSL_new(tls->ctx);
3937 bio = BIO_new(tls->meth);
3940 BIO_set_data(bio, session);
3941 SSL_set_bio(ssl, bio, bio);
3942 SSL_set_app_data(ssl, session);
3944 if (!setup_client_ssl_session(session, ssl))
3947 r = SSL_connect(ssl);
3949 int ret = SSL_get_error(ssl, r);
3950 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3952 if (ret == SSL_ERROR_WANT_READ)
3954 if (ret == SSL_ERROR_WANT_WRITE) {
3956#ifdef COAP_EPOLL_SUPPORT
3970 if (SSL_is_init_finished(ssl)) {
3984#if COAP_SERVER_SUPPORT
3989 coap_tls_context_t *tls = &((coap_openssl_context_t *)session->
context->
dtls_context)->tls;
3993 ssl = SSL_new(tls->ctx);
3996 bio = BIO_new(tls->meth);
3999 BIO_set_data(bio, session);
4000 SSL_set_bio(ssl, bio, bio);
4001 SSL_set_app_data(ssl, session);
4004 if (psk_hint != NULL && psk_hint->
length) {
4005 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
4008 memcpy(hint, psk_hint->
s, psk_hint->
length);
4009 hint[psk_hint->
length] =
'\000';
4010 SSL_use_psk_identity_hint(ssl, hint);
4017 r = SSL_accept(ssl);
4019 int err = SSL_get_error(ssl, r);
4020 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
4022 if (err == SSL_ERROR_WANT_READ)
4024 if (err == SSL_ERROR_WANT_WRITE) {
4026#ifdef COAP_EPOLL_SUPPORT
4040 if (SSL_is_init_finished(ssl)) {
4045#if COAP_DTLS_RETRANSMIT_MS != 1000
4046#if OPENSSL_VERSION_NUMBER >= 0x10101000L
4048 DTLS_set_timer_cb(ssl, timer_cb);
4064 SSL *ssl = (SSL *)session->
tls;
4066 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
4067 int r = SSL_shutdown(ssl);
4072 session->
tls = NULL;
4085 SSL *ssl = (SSL *)session->
tls;
4091 in_init = !SSL_is_init_finished(ssl);
4094 r = SSL_write(ssl, data, (
int)data_len);
4097 int err = SSL_get_error(ssl, r);
4098 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
4099 if (in_init && SSL_is_init_finished(ssl)) {
4105 if (err == SSL_ERROR_WANT_READ)
4107 else if (err == SSL_ERROR_WANT_WRITE) {
4109#ifdef COAP_EPOLL_SUPPORT
4119 if (err == SSL_ERROR_ZERO_RETURN)
4121 else if (err == SSL_ERROR_SSL) {
4122 unsigned long e = ERR_get_error();
4124 coap_log_info(
"***%s: coap_tls_write: cannot send PDU: %d: %s\n",
4126 ERR_GET_REASON(e), ERR_reason_error_string(e));
4129 coap_log_info(
"***%s: coap_tls_send: cannot send PDU: %d\n",
4134 }
else if (in_init && SSL_is_init_finished(ssl)) {
4150 if (r == (ssize_t)data_len)
4167 SSL *ssl = (SSL *)session->
tls;
4175 in_init = !SSL_is_init_finished(ssl);
4178 r = SSL_read(ssl, data, (
int)data_len);
4180 int err = SSL_get_error(ssl, r);
4181 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
4182 if (in_init && SSL_is_init_finished(ssl)) {
4188 if (err == SSL_ERROR_WANT_READ)
4190 if (err == SSL_ERROR_WANT_WRITE) {
4192#ifdef COAP_EPOLL_SUPPORT
4202 if (err == SSL_ERROR_ZERO_RETURN)
4204 else if (err == SSL_ERROR_SSL) {
4205 unsigned long e = ERR_get_error();
4207 coap_log_info(
"***%s: coap_tls_read: cannot recv PDU: %d: %s\n",
4209 ERR_GET_REASON(e), ERR_reason_error_string(e));
4217 }
else if (in_init && SSL_is_init_finished(ssl)) {
4242#if COAP_SERVER_SUPPORT
4245 EVP_MD_CTX *digest_ctx = EVP_MD_CTX_new();
4248 EVP_DigestInit_ex(digest_ctx, EVP_sha256(), NULL);
4256 EVP_MD_CTX_free(digest_ctx);
4261 const uint8_t *data,
4263 return EVP_DigestUpdate(digest_ctx, data, data_len);
4270 int ret = EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
4277#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
4279coap_crypto_output_errors(
const char *prefix) {
4280#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
4285 while ((e = ERR_get_error()))
4288 ERR_reason_error_string(e),
4289 ssl_function_definition(e));
4299static struct hash_algs {
4301 const EVP_MD *(*get_hash)(void);
4310static const EVP_MD *
4311get_hash_alg(
cose_alg_t alg,
size_t *length) {
4314 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
4315 if (hashs[idx].alg == alg) {
4316 *length = hashs[idx].length;
4317 return hashs[idx].get_hash();
4320 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
4328 unsigned int length;
4329 const EVP_MD *evp_md;
4330 EVP_MD_CTX *evp_ctx = NULL;
4334 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
4335 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
4338 evp_ctx = EVP_MD_CTX_new();
4339 if (evp_ctx == NULL)
4341 if (EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
4344 if (EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
4350 if (EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
4352 dummy->length = length;
4353 if (hash_length < dummy->length)
4354 dummy->length = hash_length;
4356 EVP_MD_CTX_free(evp_ctx);
4360 coap_crypto_output_errors(
"coap_crypto_hash");
4363 EVP_MD_CTX_free(evp_ctx);
4368#if COAP_OSCORE_SUPPORT
4374#include <openssl/evp.h>
4375#include <openssl/hmac.h>
4382static struct cipher_algs {
4384 const EVP_CIPHER *(*get_cipher)(void);
4389static const EVP_CIPHER *
4393 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
4394 if (ciphers[idx].alg == alg)
4395 return ciphers[idx].get_cipher();
4397 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
4406static struct hmac_algs {
4408 const EVP_MD *(*get_hmac)(void);
4415static const EVP_MD *
4419 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
4420 if (hmacs[idx].hmac_alg == hmac_alg)
4421 return hmacs[idx].get_hmac();
4423 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
4429 return get_cipher_alg(alg) != NULL;
4438 return get_hmac_alg(hmac_alg) != NULL;
4442 if (1 != (Func)) { \
4451 size_t *max_result_len) {
4452 const EVP_CIPHER *cipher;
4455 int result_len = (int)(*max_result_len & INT_MAX);
4460 assert(params != NULL);
4461 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
4468 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
4471 C(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL));
4472 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
4473 C(EVP_CIPHER_CTX_ctrl(ctx,
4474 EVP_CTRL_AEAD_SET_IVLEN,
4477 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, NULL));
4478 C(EVP_EncryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
4481 C(EVP_EncryptUpdate(ctx, NULL, &result_len, NULL, (
int)data->
length));
4482 if (aad && aad->
s && (aad->
length > 0)) {
4483 C(EVP_EncryptUpdate(ctx, NULL, &result_len, aad->
s, (
int)aad->
length));
4485 C(EVP_EncryptUpdate(ctx, result, &result_len, data->
s, (
int)data->
length));
4488 C(EVP_EncryptFinal_ex(ctx, result + result_len, &tmp));
4492 C(EVP_CIPHER_CTX_ctrl(ctx,
4493 EVP_CTRL_CCM_GET_TAG,
4495 result + result_len));
4497 *max_result_len = result_len + ccm->
tag_len;
4498 EVP_CIPHER_CTX_free(ctx);
4502 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
4511 size_t *max_result_len) {
4512 const EVP_CIPHER *cipher;
4522 assert(params != NULL);
4523 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
4535 memcpy(&rwtag, &tag,
sizeof(rwtag));
4538 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
4540 C(EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL));
4541 C(EVP_CIPHER_CTX_ctrl(ctx,
4542 EVP_CTRL_AEAD_SET_IVLEN,
4545 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, rwtag));
4546 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
4548 C(EVP_DecryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
4550 C(EVP_DecryptUpdate(ctx, NULL, &len, NULL, (
int)data->
length));
4551 if (aad && aad->
s && (aad->
length > 0)) {
4552 C(EVP_DecryptUpdate(ctx, NULL, &len, aad->
s, (
int)aad->
length));
4554 tmp = EVP_DecryptUpdate(ctx, result, &len, data->
s, (
int)data->
length);
4555 EVP_CIPHER_CTX_free(ctx);
4557 *max_result_len = 0;
4560 *max_result_len = len;
4564 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
4573 unsigned int result_len;
4574 const EVP_MD *evp_md;
4581 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
4582 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
4588 result_len = (
unsigned int)
dummy->length;
4596 dummy->length = result_len;
4601 coap_crypto_output_errors(
"coap_crypto_hmac");
4613#pragma GCC diagnostic ignored "-Wunused-function"
#define COAP_SERVER_SUPPORT
struct coap_session_t coap_session_t
#define COAP_RXBUFFER_SIZE
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to modify the state of events that epoll is tracking on the appropriate file ...
Library specific build wrapper for coap_internal.h.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
static coap_log_t dtls_log_level
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
coap_tick_t coap_ticks_from_rt_us(uint64_t t)
Helper function that converts POSIX wallclock time in us to coap ticks.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
const coap_bin_const_t * coap_get_session_client_psk_identity(const coap_session_t *coap_session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
int coap_dtls_define_issue(coap_define_issue_key_t type, coap_define_issue_fail_t fail, coap_dtls_key_t *key, const coap_dtls_role_t role, int ret)
Report PKI DEFINE type issue.
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void coap_dtls_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
int coap_dtls_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
const coap_bin_const_t * coap_get_session_client_psk_key(const coap_session_t *coap_session)
Get the current client's PSK key.
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
void coap_dtls_map_key_type_to_define(const coap_dtls_pki_t *setup_data, coap_dtls_key_t *key)
Map the PKI key definitions to the new DEFINE format.
const coap_bin_const_t * coap_get_session_server_psk_key(const coap_session_t *coap_session)
Get the current server's PSK key.
const coap_bin_const_t * coap_get_session_server_psk_hint(const coap_session_t *coap_session)
Get the current server's PSK identity hint.
@ COAP_DEFINE_KEY_PRIVATE
@ COAP_DEFINE_FAIL_NOT_SUPPORTED
#define COAP_DTLS_HINT_LENGTH
int coap_tls_engine_configure(coap_str_const_t *conf_mem)
Configure an ENGINE for a TLS library.
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
struct coap_dtls_key_t coap_dtls_key_t
The structure that holds the PKI key information.
int coap_tls_engine_remove(void)
Remove a previously configured ENGINE from a TLS library.
struct coap_dtls_spsk_info_t coap_dtls_spsk_info_t
The structure that holds the Server Pre-Shared Key and Identity Hint information.
struct coap_dtls_pki_t coap_dtls_pki_t
@ COAP_PKI_KEY_DEF_PKCS11
The PKI key type is PKCS11 (pkcs11:...).
@ COAP_PKI_KEY_DEF_DER_BUF
The PKI key type is DER buffer (ASN.1).
@ COAP_PKI_KEY_DEF_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_DEF_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_DEF_ENGINE
The PKI key type is to be passed to ENGINE.
@ COAP_PKI_KEY_DEF_RPK_BUF
The PKI key type is RPK in buffer.
@ COAP_PKI_KEY_DEF_DER
The PKI key type is DER file.
@ COAP_PKI_KEY_DEF_PKCS11_RPK
The PKI key type is PKCS11 w/ RPK (pkcs11:...).
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_DEFINE
The individual PKI key types are Definable.
@ COAP_ASN1_PKEY_DH
DH type.
@ COAP_ASN1_PKEY_NONE
NONE.
@ COAP_ASN1_PKEY_TLS1_PRF
TLS1_PRF type.
@ COAP_ASN1_PKEY_RSA2
RSA2 type.
@ COAP_ASN1_PKEY_DSA
DSA type.
@ COAP_ASN1_PKEY_DHX
DHX type.
@ COAP_ASN1_PKEY_DSA4
DSA4 type.
@ COAP_ASN1_PKEY_DSA2
DSA2 type.
@ COAP_ASN1_PKEY_RSA
RSA type.
@ COAP_ASN1_PKEY_DSA1
DSA1 type.
@ COAP_ASN1_PKEY_HKDF
HKDF type.
@ COAP_ASN1_PKEY_EC
EC type.
@ COAP_ASN1_PKEY_DSA3
DSA3 type.
@ COAP_ASN1_PKEY_HMAC
HMAC type.
@ COAP_ASN1_PKEY_CMAC
CMAC type.
@ COAP_TLS_LIBRARY_OPENSSL
Using OpenSSL library.
@ COAP_EVENT_DTLS_CLOSED
Triggerred when (D)TLS session closed.
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
@ COAP_EVENT_DTLS_RENEGOTIATE
Triggered when (D)TLS session renegotiated.
@ COAP_EVENT_DTLS_ERROR
Triggered when (D)TLS error occurs.
#define coap_lock_callback_ret(r, c, func)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_dtls_log(level,...)
Logging function.
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
#define coap_log(level,...)
Logging function.
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
@ COSE_HMAC_ALG_HMAC384_384
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_ALGORITHM_SHA_256_64
@ COSE_ALGORITHM_SHA_256_256
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_STATE_HANDSHAKE
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
int coap_dtls_cid_is_supported(void)
Check whether (D)TLS CID is available.
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
The CoAP stack's global state is stored in a coap_context_t object.
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
The structure that holds the AES Crypto information.
size_t l
The number of bytes in the length field.
const uint8_t * nonce
must be exactly 15 - l bytes
coap_crypto_key_t key
The Key to use.
size_t tag_len
The size of the Tag.
The common structure that holds the Crypto information.
union coap_crypto_param_t::@242136240037311335022001367112102231100333222137 params
coap_crypto_aes_ccm_t aes
Used if AES type encryption.
cose_alg_t alg
The COSE algorith to use.
The structure that holds the Client PSK information.
coap_bin_const_t identity
The structure used for defining the Client PSK setup data to be used.
uint8_t use_cid
Set to 1 if DTLS Connection ID is to be used.
void * ih_call_back_arg
Passed in to the Identity Hint callback function.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_ih_callback_t validate_ih_call_back
Identity Hint check callback function.
uint8_t ec_jpake
Set to COAP_DTLS_CPSK_SETUP_VERSION to support this version of the struct.
The structure that holds the PKI key information.
union coap_dtls_key_t::@067104111220140054257241314117327310146344167156 key
coap_pki_key_define_t define
for definable type keys
coap_pki_key_t key_type
key format type
The structure used for defining the PKI setup data to be used.
uint8_t allow_no_crl
1 ignore if CRL not there
void * cn_call_back_arg
Passed in to the CN callback function.
uint8_t cert_chain_validation
1 if to check cert_chain_verify_depth
uint8_t use_cid
1 if DTLS Connection ID is to be used (Client only, server always enabled) if supported
uint8_t check_cert_revocation
1 if revocation checks wanted
coap_dtls_pki_sni_callback_t validate_sni_call_back
SNI check callback function.
uint8_t cert_chain_verify_depth
recommended depth is 3
coap_dtls_security_setup_t additional_tls_setup_call_back
Additional Security callback handler that is invoked when libcoap has done the standard,...
uint8_t allow_expired_certs
1 if expired certs are allowed
uint8_t verify_peer_cert
Set to COAP_DTLS_PKI_SETUP_VERSION to support this version of the struct.
char * client_sni
If not NULL, SNI to use in client TLS setup.
uint8_t allow_self_signed
1 if self-signed certs are allowed.
void * sni_call_back_arg
Passed in to the sni callback function.
coap_dtls_cn_callback_t validate_cn_call_back
CN check callback function.
uint8_t allow_expired_crl
1 if expired crl is allowed
uint8_t is_rpk_not_cert
1 is RPK instead of Public Certificate.
uint8_t check_common_ca
1 if peer cert is to be signed by the same CA as the local cert
coap_dtls_key_t pki_key
PKI key definition.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_psk_sni_callback_t validate_sni_call_back
SNI check callback function.
coap_dtls_id_callback_t validate_id_call_back
Identity check callback function.
void * id_call_back_arg
Passed in to the Identity callback function.
uint8_t ec_jpake
Set to COAP_DTLS_SPSK_SETUP_VERSION to support this version of the struct.
void * sni_call_back_arg
Passed in to the SNI callback function.
coap_dtls_spsk_info_t psk_info
Server PSK definition.
coap_layer_write_t l_write
coap_layer_establish_t l_establish
coap_const_char_ptr_t public_cert
define: Public Cert
coap_asn1_privatekey_type_t private_key_type
define: ASN1 Private Key Type (if needed)
const char * user_pin
define: User pin to access type PKCS11.
coap_const_char_ptr_t private_key
define: Private Key
coap_const_char_ptr_t ca
define: Common CA Certificate
size_t public_cert_len
define Public Cert length (if needed)
size_t ca_len
define CA Cert length (if needed)
coap_pki_define_t private_key_def
define: Private Key type definition
size_t private_key_len
define Private Key length (if needed)
coap_pki_define_t ca_def
define: Common CA type definition
coap_pki_define_t public_cert_def
define: Public Cert type definition
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned int dtls_timeout_count
dtls setup retry counter
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
coap_proto_t proto
protocol used
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
int dtls_event
Tracking any (D)TLS events on this session.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_context_t * context
session's context
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
The structure used for returning the underlying (D)TLS library information.
uint64_t built_version
(D)TLS Built against Library Version
coap_tls_library_t type
Library type.
uint64_t version
(D)TLS runtime Library Version
const char * s_byte
signed char ptr
const uint8_t * u_byte
unsigned char ptr