20#if COAP_WITH_LIBOPENSSL
74#include <openssl/ssl.h>
75#include <openssl/engine.h>
76#include <openssl/err.h>
77#include <openssl/rand.h>
78#include <openssl/hmac.h>
79#include <openssl/x509v3.h>
81#if OPENSSL_VERSION_NUMBER >= 0x30000000L
84#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
87#if !defined(__MINGW32__)
88#pragma warning(disable : 4996)
93#ifdef COAP_EPOLL_SUPPORT
94# include <sys/epoll.h>
97#if OPENSSL_VERSION_NUMBER < 0x10100000L
98#error Must be compiled against OpenSSL 1.1.0 or later
102#define strcasecmp _stricmp
103#define strncasecmp _strnicmp
107#ifndef TLSEXT_TYPE_client_certificate_type
108#define TLSEXT_TYPE_client_certificate_type 19
110#ifndef TLSEXT_TYPE_server_certificate_type
111#define TLSEXT_TYPE_server_certificate_type 20
114#ifndef COAP_OPENSSL_CIPHERS
115#if OPENSSL_VERSION_NUMBER >= 0x10101000L
116#define COAP_OPENSSL_CIPHERS "TLSv1.3:TLSv1.2:!NULL"
118#define COAP_OPENSSL_CIPHERS "TLSv1.2:!NULL"
122#ifndef COAP_OPENSSL_PSK_CIPHERS
123#define COAP_OPENSSL_PSK_CIPHERS "PSK:!NULL"
126#ifndef COAP_OPENSSL_PKCS11_ENGINE_ID
127#define COAP_OPENSSL_PKCS11_ENGINE_ID "pkcs11"
131typedef struct coap_dtls_context_t {
134 HMAC_CTX *cookie_hmac;
137} coap_dtls_context_t;
139typedef struct coap_tls_context_t {
147typedef struct sni_entry {
149#if OPENSSL_VERSION_NUMBER < 0x10101000L
156typedef struct psk_sni_entry {
158#if OPENSSL_VERSION_NUMBER < 0x10101000L
164typedef struct coap_openssl_context_t {
165 coap_dtls_context_t dtls;
167 coap_tls_context_t tls;
172 sni_entry *sni_entry_list;
173#if OPENSSL_VERSION_NUMBER < 0x10101000L
174 size_t psk_sni_count;
175 psk_sni_entry *psk_sni_entry_list;
177} coap_openssl_context_t;
179#if COAP_SERVER_SUPPORT
180#if OPENSSL_VERSION_NUMBER < 0x10101000L
181static int psk_tls_server_name_call_back(SSL *ssl,
int *sd,
void *arg);
183static int psk_tls_client_hello_call_back(SSL *ssl,
int *al,
void *arg);
189 if (SSLeay() < 0x10100000L) {
190 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
193#if OPENSSL_VERSION_NUMBER >= 0x10101000L
201 if (SSLeay() < 0x10101000L) {
202 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
212 if (SSLeay() < 0x10100000L) {
213 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
216#if OPENSSL_VERSION_NUMBER >= 0x10101000L
217 if (SSLeay() < 0x10101000L) {
218 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
273#if COAP_CLIENT_SUPPORT
291static ENGINE *pkcs11_engine =
NULL;
292static ENGINE *defined_engine =
NULL;
296 SSL_load_error_strings();
298 ENGINE_load_dynamic();
303#if OPENSSL_VERSION_NUMBER < 0x30000000L
306 ENGINE_finish(pkcs11_engine);
307 pkcs11_engine =
NULL;
309 if (defined_engine) {
311 ENGINE_finish(defined_engine);
312 defined_engine =
NULL;
315 pkcs11_engine =
NULL;
316 defined_engine =
NULL;
332 return c_session->
tls;
338get_split_conf_entry(
const uint8_t **start,
size_t size,
const char *get_keyword,
340 const uint8_t *begin = *start;
343 const uint8_t *split;
349 kend = end = memchr(begin,
'\n', size);
355 if (end > begin && end[-1] ==
'\r')
358 if (begin[0] ==
'#' || (end - begin) == 0) {
360 size -= kend - begin + 1;
366 split = memchr(begin,
':', end - begin);
370 if ((
size_t)(split - begin) != strlen(get_keyword)) {
371 size -= kend - begin + 1;
375 if (memcmp(begin, get_keyword, split - begin)) {
376 size -= kend - begin + 1;
384 if ((end - begin) == 0)
387 split = memchr(begin,
':', end - begin);
399 if ((end - split) > 0) {
434 const uint8_t *start;
439 unsigned int defaults = 0;
440 int done_engine_id = 0;
441 int done_engine_init = 0;
447 end = start + conf_mem->
length;
449 if (defined_engine) {
450 coap_log_warn(
"coap_tls_engine_configure: Freeing off previous engine definition\n");
451 ENGINE_finish(defined_engine);
452 defined_engine =
NULL;
456 if (!get_split_conf_entry(&start, end - start,
"engine", &engine_id, &p2)) {
457 coap_log_warn(
"coap_tls_engine_configure: engine not defined\n");
460 defined_engine = ENGINE_by_id((
const char *)engine_id->
s);
461 if (!defined_engine) {
462 coap_log_warn(
"coap_tls_engine_configure: engine '%s' not known\n", engine_id->
s);
472 while (get_split_conf_entry(&start, end - start,
"pre-cmd", &p1, &p2)) {
473 if (!ENGINE_ctrl_cmd_string(defined_engine, (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
NULL,
475 coap_log_warn(
"coap_tls_engine_configure: engine %s pre-cmd '%s:%s' failed\n",
476 (
const char *)engine_id->
s,
477 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
481 engine_id->
s, p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
490 if (!ENGINE_init(defined_engine)) {
491 coap_log_warn(
"coap_tls_engine_configure: %s failed initialization\n", (
const char *)engine_id->
s);
494 done_engine_init = 1;
496 (
const char *)engine_id->
s);
501 while (get_split_conf_entry(&start, end - start,
"post-cmd", &p1, &p2)) {
502 if (!ENGINE_ctrl_cmd_string(defined_engine, (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
NULL,
504 coap_log_warn(
"coap_tls_engine_configure: %s post-cmd '%s:%s' failed\n", (
const char *)engine_id->
s,
505 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
509 (
const char *)engine_id->
s,
510 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
518 if (!get_split_conf_entry(&start, end - start,
"enable-methods", &p1, &p2)) {
519 coap_log_warn(
"coap_tls_engine_configure: enable-methods not found\n");
522 defaults = strtoul((
const char *)p1->
s,
NULL, 0);
523 if (!ENGINE_set_default(defined_engine, defaults)) {
524 coap_log_warn(
"coap_tls_engine_configure: enable-methods 0x%x invalid\n", defaults);
539 ENGINE_free(defined_engine);
540 if (done_engine_init)
541 ENGINE_finish(defined_engine);
542 defined_engine =
NULL;
551 if (defined_engine) {
552 ENGINE_finish(defined_engine);
553 defined_engine =
NULL;
574typedef struct coap_ssl_data {
583coap_dgram_create(BIO *a) {
584 coap_ssl_data *data =
NULL;
585 data = malloc(
sizeof(coap_ssl_data));
589 BIO_set_data(a, data);
590 memset(data, 0x00,
sizeof(coap_ssl_data));
595coap_dgram_destroy(BIO *a) {
599 data = (coap_ssl_data *)BIO_get_data(a);
602 BIO_set_data(a,
NULL);
607coap_dgram_read(BIO *a,
char *out,
int outl) {
609 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
612 if (data !=
NULL && data->pdu_len > 0) {
613 if (outl < (
int)data->pdu_len) {
614 memcpy(out, data->pdu, outl);
617 memcpy(out, data->pdu, data->pdu_len);
618 ret = (int)data->pdu_len;
620 if (!data->peekmode) {
627 BIO_clear_retry_flags(a);
629 BIO_set_retry_read(a);
635coap_dgram_write(BIO *a,
const char *in,
int inl) {
637 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
639 if (data && data->session) {
641#if COAP_SERVER_SUPPORT
642 && data->session->endpoint ==
NULL
646 BIO_clear_retry_flags(a);
650 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
653 BIO_clear_retry_flags(a);
655 if (ret < 0 && (errno == ENOTCONN || errno == ECONNREFUSED))
657 BIO_set_retry_write(a);
660 BIO_clear_retry_flags(a);
667coap_dgram_puts(BIO *a,
const char *pstr) {
668 return coap_dgram_write(a, pstr, (
int)strlen(pstr));
672coap_dgram_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
674 coap_ssl_data *data = BIO_get_data(a);
679 case BIO_CTRL_GET_CLOSE:
680 ret = BIO_get_shutdown(a);
682 case BIO_CTRL_SET_CLOSE:
683 BIO_set_shutdown(a, (
int)num);
685 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
687 data->peekmode = (unsigned)num;
691 case BIO_CTRL_DGRAM_CONNECT:
694 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
695 case BIO_CTRL_DGRAM_GET_MTU:
696 case BIO_CTRL_DGRAM_SET_MTU:
697 case BIO_CTRL_DGRAM_QUERY_MTU:
698 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
703 case BIO_CTRL_DGRAM_MTU_DISCOVER:
704 case BIO_CTRL_DGRAM_SET_CONNECTED:
706 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
709 ((
struct timeval *)ptr)->tv_usec);
714 case BIO_C_FILE_SEEK:
715 case BIO_C_FILE_TELL:
717 case BIO_CTRL_PENDING:
718 case BIO_CTRL_WPENDING:
719 case BIO_CTRL_DGRAM_GET_PEER:
720 case BIO_CTRL_DGRAM_SET_PEER:
721 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
722 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
723 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
724 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
725 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
726 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
727 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
728 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
737coap_dtls_generate_cookie(SSL *ssl,
738 unsigned char *cookie,
739 unsigned int *cookie_len) {
740 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
741 coap_dtls_context_t *dtls = ctx ? (coap_dtls_context_t *)SSL_CTX_get_app_data(ctx) :
NULL;
742 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
745 int r = HMAC_Init_ex(dtls->cookie_hmac,
NULL, 0,
NULL,
NULL);
746 r &= HMAC_Update(dtls->cookie_hmac,
747 (
const uint8_t *)&data->session->addr_info.local.addr,
748 (
size_t)data->session->addr_info.local.size);
749 r &= HMAC_Update(dtls->cookie_hmac,
750 (
const uint8_t *)&data->session->addr_info.remote.addr,
751 (
size_t)data->session->addr_info.remote.size);
752 r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
759coap_dtls_verify_cookie(SSL *ssl,
760 const uint8_t *cookie,
761 unsigned int cookie_len) {
764 if (coap_dtls_generate_cookie(ssl, hmac, &len) &&
765 cookie_len == len && memcmp(cookie, hmac, len) == 0)
771#if COAP_CLIENT_SUPPORT
773coap_dtls_psk_client_callback(SSL *ssl,
776 unsigned int max_identity_len,
778 unsigned int max_psk_len) {
780 coap_openssl_context_t *o_context;
791 if (o_context ==
NULL)
795 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
796 temp.
length = strlen((
const char *)temp.
s);
800 (
const char *)temp.
s);
812 if (cpsk_info ==
NULL)
817 psk_identity = &cpsk_info->
identity;
818 psk_key = &cpsk_info->
key;
824 if (psk_identity ==
NULL || psk_key ==
NULL) {
830 if (!max_identity_len)
833 if (psk_identity->
length > max_identity_len) {
834 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
838 max_identity_len = (
unsigned int)psk_identity->
length;
840 memcpy(identity, psk_identity->
s, max_identity_len);
841 identity[max_identity_len] =
'\000';
843 if (psk_key->
length > max_psk_len) {
848 max_psk_len = (
unsigned int)psk_key->
length;
850 memcpy(psk, psk_key->
s, max_psk_len);
855#if COAP_SERVER_SUPPORT
857coap_dtls_psk_server_callback(
859 const char *identity,
861 unsigned int max_psk_len
872 setup_data = &c_session->
context->spsk_setup_data;
875 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
876 lidentity.
length = strlen((
const char *)lidentity.
s);
880 (
int)lidentity.
length, (
const char *)lidentity.
s);
895 if (psk_key->
length > max_psk_len) {
900 max_psk_len = (
unsigned int)psk_key->
length;
902 memcpy(psk, psk_key->
s, max_psk_len);
908ssl_function_definition(
unsigned long e) {
909#if OPENSSL_VERSION_NUMBER >= 0x30000000L
913 static char buff[80];
915 snprintf(buff,
sizeof(buff),
" at %s:%s",
916 ERR_lib_error_string(e), ERR_func_error_string(e));
922coap_dtls_info_callback(
const SSL *ssl,
int where,
int ret) {
925 int w = where &~SSL_ST_MASK;
929 "coap_dtls_info_callback: session not determined, where 0x%0x and ret 0x%0x\n", where, ret);
932 if (w & SSL_ST_CONNECT)
933 pstr =
"SSL_connect";
934 else if (w & SSL_ST_ACCEPT)
939 if (where & SSL_CB_LOOP) {
942 }
else if (where & SSL_CB_ALERT) {
944 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
945 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) {
947 if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY)
951 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
954 SSL_alert_type_string_long(ret),
955 SSL_alert_desc_string_long(ret));
956 }
else if (where & SSL_CB_EXIT) {
962 while ((e = ERR_get_error()))
965 ssl_function_definition(e));
967 }
else if (ret < 0) {
969 int err = SSL_get_error(ssl, ret);
970 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE &&
971 err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT &&
972 err != SSL_ERROR_WANT_X509_LOOKUP) {
976 while ((e = ERR_get_error()))
979 ssl_function_definition(e));
985 if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
991coap_sock_create(BIO *a) {
997coap_sock_destroy(BIO *a) {
1009coap_sock_read(BIO *a,
char *out,
int outl) {
1013 if (session && out !=
NULL) {
1018 BIO_set_retry_read(a);
1021 BIO_clear_retry_flags(a);
1034coap_sock_write(BIO *a,
const char *in,
int inl) {
1043 (
const uint8_t *)in,
1047 BIO_clear_retry_flags(a);
1049 BIO_set_retry_read(a);
1052 BIO_clear_retry_flags(a);
1056 (errno == EPIPE || errno == ECONNRESET)) {
1076coap_sock_puts(BIO *a,
const char *pstr) {
1077 return coap_sock_write(a, pstr, (
int)strlen(pstr));
1081coap_sock_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
1092 case BIO_CTRL_SET_CLOSE:
1094 case BIO_CTRL_FLUSH:
1098 case BIO_CTRL_GET_CLOSE:
1107coap_set_user_prefs(SSL_CTX *ctx) {
1108 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
1110#ifdef COAP_OPENSSL_SIGALGS
1111 SSL_CTX_set1_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
1112 SSL_CTX_set1_client_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
1115#if OPENSSL_VERSION_NUMBER >= 0x10101000L && defined(COAP_OPENSSL_GROUPS)
1116 SSL_CTX_set1_groups_list(ctx, COAP_OPENSSL_GROUPS);
1120#if COAP_DTLS_RETRANSMIT_MS != 1000
1121#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1123timer_cb(SSL *s,
unsigned int timer_us) {
1128 return 2 * timer_us;
1135 coap_openssl_context_t *context;
1140 uint8_t cookie_secret[32];
1142 memset(context, 0,
sizeof(coap_openssl_context_t));
1145 context->dtls.ctx = SSL_CTX_new(DTLS_method());
1146 if (!context->dtls.ctx)
1148 SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
1149 SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
1150 SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
1151 coap_set_user_prefs(context->dtls.ctx);
1152 memset(cookie_secret, 0,
sizeof(cookie_secret));
1153 if (!RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
1155 "Insufficient entropy for random cookie generation");
1158 context->dtls.cookie_hmac = HMAC_CTX_new();
1159 if (!context->dtls.cookie_hmac)
1161 if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
1162 EVP_sha256(),
NULL))
1164 SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
1165 SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
1166 SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
1167 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
1168#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1169 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_LEGACY_SERVER_CONNECT);
1171 context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM,
"coapdgram");
1172 if (!context->dtls.meth)
1174 context->dtls.bio_addr = BIO_ADDR_new();
1175 if (!context->dtls.bio_addr)
1177 BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
1178 BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
1179 BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
1180 BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
1181 BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
1182 BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
1184#if !COAP_DISABLE_TCP
1186 context->tls.ctx = SSL_CTX_new(TLS_method());
1187 if (!context->tls.ctx)
1189 SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
1190 SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
1191 coap_set_user_prefs(context->tls.ctx);
1192 SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
1193 context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET,
"coapsock");
1194 if (!context->tls.meth)
1196 BIO_meth_set_write(context->tls.meth, coap_sock_write);
1197 BIO_meth_set_read(context->tls.meth, coap_sock_read);
1198 BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
1199 BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
1200 BIO_meth_set_create(context->tls.meth, coap_sock_create);
1201 BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
1212#if COAP_SERVER_SUPPORT
1217 coap_openssl_context_t *o_context =
1221 if (!setup_data || !o_context)
1224 SSL_CTX_set_psk_server_callback(o_context->dtls.ctx,
1225 coap_dtls_psk_server_callback);
1226#if !COAP_DISABLE_TCP
1227 SSL_CTX_set_psk_server_callback(o_context->tls.ctx,
1228 coap_dtls_psk_server_callback);
1234 SSL_CTX_use_psk_identity_hint(o_context->dtls.ctx, hint);
1235#if !COAP_DISABLE_TCP
1236 SSL_CTX_use_psk_identity_hint(o_context->tls.ctx, hint);
1240#if OPENSSL_VERSION_NUMBER < 0x10101000L
1241 SSL_CTX_set_tlsext_servername_arg(o_context->dtls.ctx,
1242 &c_context->spsk_setup_data);
1243 SSL_CTX_set_tlsext_servername_callback(o_context->dtls.ctx,
1244 psk_tls_server_name_call_back);
1245#if !COAP_DISABLE_TCP
1246 SSL_CTX_set_tlsext_servername_arg(o_context->tls.ctx,
1247 &c_context->spsk_setup_data);
1248 SSL_CTX_set_tlsext_servername_callback(o_context->tls.ctx,
1249 psk_tls_server_name_call_back);
1252 SSL_CTX_set_client_hello_cb(o_context->dtls.ctx,
1253 psk_tls_client_hello_call_back,
1255#if !COAP_DISABLE_TCP
1256 SSL_CTX_set_client_hello_cb(o_context->tls.ctx,
1257 psk_tls_client_hello_call_back,
1263 if (!o_context->dtls.ssl) {
1265 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
1266 if (!o_context->dtls.ssl)
1268 bio = BIO_new(o_context->dtls.meth);
1270 SSL_free(o_context->dtls.ssl);
1271 o_context->dtls.ssl =
NULL;
1274 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1275 SSL_set_app_data(o_context->dtls.ssl,
NULL);
1276 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1282 o_context->psk_pki_enabled |= IS_PSK;
1287#if COAP_CLIENT_SUPPORT
1292 coap_openssl_context_t *o_context =
1296 if (!setup_data || !o_context)
1299 if (!o_context->dtls.ssl) {
1301 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
1302 if (!o_context->dtls.ssl)
1304 bio = BIO_new(o_context->dtls.meth);
1306 SSL_free(o_context->dtls.ssl);
1307 o_context->dtls.ssl =
NULL;
1310 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1311 SSL_set_app_data(o_context->dtls.ssl,
NULL);
1312 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1321 o_context->psk_pki_enabled |= IS_PSK;
1327map_key_type(
int asn1_private_key_type
1329 switch (asn1_private_key_type) {
1331 return EVP_PKEY_NONE;
1333 return EVP_PKEY_RSA;
1335 return EVP_PKEY_RSA2;
1337 return EVP_PKEY_DSA;
1339 return EVP_PKEY_DSA1;
1341 return EVP_PKEY_DSA2;
1343 return EVP_PKEY_DSA3;
1345 return EVP_PKEY_DSA4;
1349 return EVP_PKEY_DHX;
1353 return EVP_PKEY_HMAC;
1355 return EVP_PKEY_CMAC;
1357 return EVP_PKEY_TLS1_PRF;
1359 return EVP_PKEY_HKDF;
1361 coap_log_warn(
"*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
1362 asn1_private_key_type);
1367#if !COAP_DISABLE_TCP
1368static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1370#if COAP_SERVER_SUPPORT
1373 const unsigned char **out,
1374 unsigned char *outlen,
1375 const unsigned char *in,
1379 unsigned char *tout =
NULL;
1382 return SSL_TLSEXT_ERR_NOACK;
1383 ret = SSL_select_next_proto(&tout,
1390 return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
1396add_ca_to_cert_store(X509_STORE *st, X509 *x509) {
1400 while (ERR_get_error() != 0) {
1403 if (!X509_STORE_add_cert(st, x509)) {
1404 while ((e = ERR_get_error()) != 0) {
1405 int r = ERR_GET_REASON(e);
1406 if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1409 ERR_reason_error_string(e),
1410 ssl_function_definition(e));
1417missing_ENGINE_load_cert(ENGINE *engine,
const char *cert_id) {
1419 const char *cert_id;
1423 params.cert_id = cert_id;
1427 if (!ENGINE_ctrl_cmd(engine,
"LOAD_CERT_CTRL", 0, ¶ms,
NULL, 1)) {
1434check_pkcs11_engine(
void) {
1435 static int already_tried = 0;
1440 if (!pkcs11_engine) {
1441 pkcs11_engine = ENGINE_by_id(COAP_OPENSSL_PKCS11_ENGINE_ID);
1442 if (!pkcs11_engine) {
1443 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support - need OpenSSL %s engine\n",
1444 COAP_OPENSSL_PKCS11_ENGINE_ID);
1448 if (!ENGINE_init(pkcs11_engine)) {
1450 ENGINE_free(pkcs11_engine);
1451 pkcs11_engine =
NULL;
1452 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1460 ENGINE_free(pkcs11_engine);
1465#if OPENSSL_VERSION_NUMBER < 0x10101000L && COAP_SERVER_SUPPORT
1468install_engine_public_cert_ctx(ENGINE *engine, SSL_CTX *ctx,
1469 const char *public_cert) {
1472 x509 = missing_ENGINE_load_cert(engine, public_cert);
1480 if (!SSL_CTX_use_certificate(ctx, x509)) {
1481 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1493install_engine_private_key_ctx(ENGINE *engine, SSL_CTX *ctx,
1494 const char *private_key) {
1495 EVP_PKEY *pkey = ENGINE_load_private_key(engine,
1506 if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
1507 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1511 EVP_PKEY_free(pkey);
1514 EVP_PKEY_free(pkey);
1519install_engine_ca_ctx(ENGINE *engine, SSL_CTX *ctx,
const char *ca) {
1523 x509 = missing_ENGINE_load_cert(engine,
1527 "%s CA Certificate\n",
1532 if (!SSL_CTX_add_client_CA(ctx, x509)) {
1533 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1534 "%s CA Certificate\n",
1540 st = SSL_CTX_get_cert_store(ctx);
1541 add_ca_to_cert_store(st, x509);
1547load_in_cas_ctx(SSL_CTX *ctx,
1548 const char *ca_file) {
1549 STACK_OF(X509_NAME) *cert_names;
1553 char *rw_var =
NULL;
1554 cert_names = SSL_load_client_CA_file(ca_file);
1555 if (cert_names !=
NULL)
1556 SSL_CTX_set_client_CA_list(ctx, cert_names);
1558 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1565 st = SSL_CTX_get_cert_store(ctx);
1566 in = BIO_new(BIO_s_file());
1570 memcpy(&rw_var, &ca_file,
sizeof(rw_var));
1571 if (!BIO_read_filename(in, rw_var)) {
1579 add_ca_to_cert_store(st, x);
1587setup_pki_server(SSL_CTX *ctx,
1603 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1605 SSL_FILETYPE_PEM))) {
1615 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp,
NULL, 0,
NULL) :
NULL;
1617 if (!pkey || !SSL_CTX_use_PrivateKey(ctx, pkey)) {
1621 EVP_PKEY_free(pkey);
1629 EVP_PKEY_free(pkey);
1641 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1643 SSL_FILETYPE_ASN1))) {
1661 if (!check_pkcs11_engine()) {
1666 if (ENGINE_ctrl_cmd_string(pkcs11_engine,
1669 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1674 if (!install_engine_private_key_ctx(pkcs11_engine, ctx,
1682 if (!defined_engine ||
1683 !install_engine_private_key_ctx(defined_engine, ctx,
1712 if (!(SSL_CTX_use_certificate_file(ctx,
1714 SSL_FILETYPE_PEM))) {
1720 if (!SSL_CTX_use_certificate_chain_file(ctx,
1732 X509 *cert = bp ? PEM_read_bio_X509(bp,
NULL, 0,
NULL) :
NULL;
1734 if (!cert || !SSL_CTX_use_certificate(ctx, cert)) {
1758 if (!(SSL_CTX_use_certificate_file(ctx,
1760 SSL_FILETYPE_ASN1))) {
1768 !(SSL_CTX_use_certificate_ASN1(ctx,
1777 if (!check_pkcs11_engine()) {
1780 if (!install_engine_public_cert_ctx(pkcs11_engine, ctx,
1788 if (!defined_engine ||
1789 !install_engine_public_cert_ctx(defined_engine, ctx,
1827 X509_STORE *st = SSL_CTX_get_cert_store(ctx);
1833 add_ca_to_cert_store(st, x);
1834 SSL_CTX_add_client_CA(ctx, x);
1850 if (!(SSL_CTX_use_certificate_file(ctx,
1852 SSL_FILETYPE_ASN1))) {
1865 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1873 st = SSL_CTX_get_cert_store(ctx);
1874 add_ca_to_cert_store(st, x509);
1879 if (!check_pkcs11_engine()) {
1882 if (!install_engine_ca_ctx(pkcs11_engine, ctx,
1890 if (!defined_engine ||
1891 !install_engine_ca_ctx(defined_engine, ctx,
1910#if OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT
1913install_engine_public_cert(ENGINE *engine, SSL *ssl,
const char *public_cert,
1917 x509 = missing_ENGINE_load_cert(engine, public_cert);
1925 if (!SSL_use_certificate(ssl, x509)) {
1926 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1938install_engine_private_key(ENGINE *engine, SSL *ssl,
const char *private_key,
1940 EVP_PKEY *pkey = ENGINE_load_private_key(engine,
1951 if (!SSL_use_PrivateKey(ssl, pkey)) {
1952 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1956 EVP_PKEY_free(pkey);
1959 EVP_PKEY_free(pkey);
1964install_engine_ca(ENGINE *engine, SSL *ssl,
const char *ca,
1967 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1972 "%s CA Certificate (no ctx)\n",
1977 x509 = missing_ENGINE_load_cert(engine,
1981 "%s CA Certificate\n",
1986 if (!SSL_add_client_CA(ssl, x509)) {
1987 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1988 "%s CA Certificate\n",
1994 st = SSL_CTX_get_cert_store(ctx);
1995 add_ca_to_cert_store(st, x509);
2001load_in_cas(SSL *ssl,
2006 char *rw_var =
NULL;
2007 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2010 STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(ca_file);
2012 if (cert_names !=
NULL)
2013 SSL_set_client_CA_list(ssl, cert_names);
2023 in = BIO_new(BIO_s_file());
2027 memcpy(&rw_var, &ca_file,
sizeof(rw_var));
2028 if (!BIO_read_filename(in, rw_var)) {
2032 st = SSL_CTX_get_cert_store(ctx);
2036 add_ca_to_cert_store(st, x);
2044setup_pki_ssl(SSL *ssl,
2060 if (!(SSL_use_PrivateKey_file(ssl,
2062 SSL_FILETYPE_PEM))) {
2072 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp,
NULL, 0,
NULL) :
NULL;
2074 if (!pkey || !SSL_use_PrivateKey(ssl, pkey)) {
2078 EVP_PKEY_free(pkey);
2086 EVP_PKEY_free(pkey);
2098 if (!(SSL_use_PrivateKey_file(ssl,
2100 SSL_FILETYPE_ASN1))) {
2118 if (!check_pkcs11_engine()) {
2123 if (ENGINE_ctrl_cmd_string(pkcs11_engine,
2126 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
2131 if (!install_engine_private_key(pkcs11_engine, ssl,
2140 if (!defined_engine ||
2141 !install_engine_private_key(defined_engine, ssl,
2173 if (!(SSL_use_certificate_file(ssl,
2175 SSL_FILETYPE_PEM))) {
2181 if (!SSL_use_certificate_chain_file(ssl,
2193 X509 *cert = bp ? PEM_read_bio_X509(bp,
NULL, 0,
NULL) :
NULL;
2195 if (!cert || !SSL_use_certificate(ssl, cert)) {
2219 if (!(SSL_use_certificate_file(ssl,
2221 SSL_FILETYPE_ASN1))) {
2229 !(SSL_use_certificate_ASN1(ssl,
2238 if (!check_pkcs11_engine()) {
2241 if (!install_engine_public_cert(pkcs11_engine, ssl,
2250 if (!defined_engine ||
2251 !install_engine_public_cert(defined_engine, ssl,
2290 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2292 X509_STORE *st = ctx? SSL_CTX_get_cert_store(ctx) :
NULL;
2296 if ((x = PEM_read_bio_X509(bp,
NULL, 0,
NULL)) ==
NULL)
2299 add_ca_to_cert_store(st, x);
2300 SSL_add_client_CA(ssl, x);
2316 if (!(SSL_use_certificate_file(ssl,
2318 SSL_FILETYPE_ASN1))) {
2330 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2333 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
2342 st = ctx ? SSL_CTX_get_cert_store(ctx) :
NULL;
2344 add_ca_to_cert_store(st, x509);
2349 if (!check_pkcs11_engine()) {
2352 if (!install_engine_ca(pkcs11_engine, ssl,
2361 if (!defined_engine ||
2362 !install_engine_ca(defined_engine, ssl,
2383get_san_or_cn_from_cert(X509 *x509) {
2387 STACK_OF(GENERAL_NAME) *san_list;
2390 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name,
NULL,
NULL);
2392 int san_count = sk_GENERAL_NAME_num(san_list);
2394 for (n = 0; n < san_count; n++) {
2395 const GENERAL_NAME *name = sk_GENERAL_NAME_value(san_list, n);
2397 if (name && name->type == GEN_DNS) {
2398 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
2401 if (ASN1_STRING_length(name->d.dNSName) != (int)strlen(dns_name))
2403 cn = OPENSSL_strdup(dns_name);
2404 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
2408 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
2411 X509_NAME_oneline(X509_get_subject_name(x509), buffer,
sizeof(buffer));
2414 n = (int)strlen(buffer) - 3;
2417 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
2418 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
2427 char *ecn = strchr(cn,
'/');
2429 return OPENSSL_strndup(cn, ecn-cn);
2431 return OPENSSL_strdup(cn);
2439tls_verify_call_back(
int preverify_ok, X509_STORE_CTX *ctx) {
2440 int index = SSL_get_ex_data_X509_STORE_CTX_idx();
2441 SSL *ssl = index >= 0 ? X509_STORE_CTX_get_ex_data(ctx, index) :
NULL;
2443 coap_openssl_context_t *context = (session && session->
context) ?
2446 int depth = X509_STORE_CTX_get_error_depth(ctx);
2447 int err = X509_STORE_CTX_get_error(ctx);
2448 X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
2449 char *cn = x509 ? get_san_or_cn_from_cert(x509) :
NULL;
2450 int keep_preverify_ok = preverify_ok;
2453 depth, err, preverify_ok, cn);
2455 X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
2459 if (!preverify_ok) {
2461 case X509_V_ERR_CERT_NOT_YET_VALID:
2462 case X509_V_ERR_CERT_HAS_EXPIRED:
2466 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2470 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2474 case X509_V_ERR_UNABLE_TO_GET_CRL:
2478 case X509_V_ERR_CRL_NOT_YET_VALID:
2479 case X509_V_ERR_CRL_HAS_EXPIRED:
2483 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2484 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2485 case X509_V_ERR_AKID_SKID_MISMATCH:
2495 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
2496 X509_STORE_CTX_set_error(ctx, err);
2498 if (!preverify_ok) {
2499 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
2502 "Unknown CA", cn ? cn :
"?", depth);
2506 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
2511 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
2516 int length = i2d_X509(x509,
NULL);
2518 uint8_t *base_buf2 = base_buf = length > 0 ? OPENSSL_malloc(length) :
NULL;
2523 assert(i2d_X509(x509, &base_buf2) > 0);
2527 depth, preverify_ok,
2531 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
2533 X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
2537 OPENSSL_free(base_buf);
2539 X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
2544 return preverify_ok;
2547#if COAP_SERVER_SUPPORT
2548#if OPENSSL_VERSION_NUMBER < 0x10101000L
2558tls_secret_call_back(SSL *ssl,
2561 STACK_OF(SSL_CIPHER) *peer_ciphers,
2565 int psk_requested = 0;
2570 assert(session !=
NULL);
2572 if (session ==
NULL ||
2577 (session->
context->spsk_setup_data.psk_info.key.s &&
2578 session->
context->spsk_setup_data.psk_info.key.length)) {
2580 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2581 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2584 SSL_CIPHER_get_name(peer_cipher));
2585 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2591 if (!psk_requested) {
2598 SSL_VERIFY_CLIENT_ONCE |
2599 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2600 tls_verify_call_back);
2602 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2611 X509_VERIFY_PARAM *param;
2613 param = X509_VERIFY_PARAM_new();
2615 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2616 SSL_set1_param(ssl, param);
2617 X509_VERIFY_PARAM_free(param);
2629 }
else if (session->
context->spsk_setup_data.psk_info.key.s &&
2630 session->
context->spsk_setup_data.psk_info.key.length) {
2631 memcpy(secret, session->
context->spsk_setup_data.psk_info.key.s,
2632 session->
context->spsk_setup_data.psk_info.key.length);
2633 *secretlen = session->
context->spsk_setup_data.psk_info.key.length;
2640 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2641#ifdef COAP_OPENSSL_PSK_SECURITY_LEVEL
2647 SSL_set_security_level(ssl, COAP_OPENSSL_PSK_SECURITY_LEVEL);
2649 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2664tls_server_name_call_back(SSL *ssl,
2670 return SSL_TLSEXT_ERR_NOACK;
2676 coap_openssl_context_t *context = (session && session->
context) ?
2678 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2682 return SSL_TLSEXT_ERR_NOACK;
2684 if (!sni || !sni[0]) {
2687 for (i = 0; i < context->sni_count; i++) {
2688 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2692 if (i == context->sni_count) {
2701 return SSL_TLSEXT_ERR_ALERT_FATAL;
2706 ctx = SSL_CTX_new(DTLS_method());
2709 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2710 SSL_CTX_set_app_data(ctx, &context->dtls);
2711 SSL_CTX_set_read_ahead(ctx, 1);
2712 coap_set_user_prefs(ctx);
2713 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2714 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2715 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2716 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2718#if !COAP_DISABLE_TCP
2721 ctx = SSL_CTX_new(TLS_method());
2724 SSL_CTX_set_app_data(ctx, &context->tls);
2725 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2726 coap_set_user_prefs(ctx);
2727 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2728 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback,
NULL);
2731 sni_setup_data = *setup_data;
2732 sni_setup_data.
pki_key = *new_entry;
2733 setup_pki_server(ctx, &sni_setup_data);
2735 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2736 (context->sni_count+1)*
sizeof(sni_entry));
2737 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2738 context->sni_entry_list[context->sni_count].ctx = ctx;
2739 context->sni_count++;
2741 SSL_set_SSL_CTX(ssl, context->sni_entry_list[i].ctx);
2742 SSL_clear_options(ssl, 0xFFFFFFFFL);
2743 SSL_set_options(ssl, SSL_CTX_get_options(context->sni_entry_list[i].ctx));
2750 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2751 return SSL_TLSEXT_ERR_OK;
2754 return SSL_TLSEXT_ERR_ALERT_WARNING;
2766psk_tls_server_name_call_back(SSL *ssl,
2773 return SSL_TLSEXT_ERR_NOACK;
2779 coap_openssl_context_t *o_context = (c_session && c_session->
context) ?
2781 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2786 return SSL_TLSEXT_ERR_ALERT_FATAL;
2788 if (!sni || !sni[0]) {
2791 for (i = 0; i < o_context->psk_sni_count; i++) {
2792 if (!strcasecmp(sni, (
char *)o_context->psk_sni_entry_list[i].sni)) {
2796 if (i == o_context->psk_sni_count) {
2805 return SSL_TLSEXT_ERR_ALERT_FATAL;
2810 ctx = SSL_CTX_new(DTLS_method());
2813 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2814 SSL_CTX_set_app_data(ctx, &o_context->dtls);
2815 SSL_CTX_set_read_ahead(ctx, 1);
2816 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2817 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2818 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2819 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2820 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2822#if !COAP_DISABLE_TCP
2825 ctx = SSL_CTX_new(TLS_method());
2828 SSL_CTX_set_app_data(ctx, &o_context->tls);
2829 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2830 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2831 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2832 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback,
NULL);
2836 o_context->psk_sni_entry_list =
2837 OPENSSL_realloc(o_context->psk_sni_entry_list,
2838 (o_context->psk_sni_count+1)*
sizeof(psk_sni_entry));
2839 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2840 OPENSSL_strdup(sni);
2841 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2843 o_context->psk_sni_entry_list[o_context->psk_sni_count].ctx =
2845 o_context->psk_sni_count++;
2847 SSL_set_SSL_CTX(ssl, o_context->psk_sni_entry_list[i].ctx);
2848 SSL_clear_options(ssl, 0xFFFFFFFFL);
2849 SSL_set_options(ssl,
2850 SSL_CTX_get_options(o_context->psk_sni_entry_list[i].ctx));
2852 &o_context->psk_sni_entry_list[i].psk_info.key);
2853 snprintf(lhint,
sizeof(lhint),
"%.*s",
2854 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2855 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2856 SSL_use_psk_identity_hint(ssl, lhint);
2863 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2864 return SSL_TLSEXT_ERR_OK;
2867 return SSL_TLSEXT_ERR_ALERT_WARNING;
2880tls_client_hello_call_back(SSL *ssl,
2885 coap_openssl_context_t *dtls_context;
2887 int psk_requested = 0;
2888 const unsigned char *out;
2892 *al = SSL_AD_INTERNAL_ERROR;
2893 return SSL_CLIENT_HELLO_ERROR;
2896 assert(session !=
NULL);
2899 if (session ==
NULL ||
2902 *al = SSL_AD_INTERNAL_ERROR;
2903 return SSL_CLIENT_HELLO_ERROR;
2906 setup_data = &dtls_context->setup_data;
2912 (session->
context->spsk_setup_data.psk_info.key.s &&
2913 session->
context->spsk_setup_data.psk_info.key.length)) {
2914 size_t len = SSL_client_hello_get0_ciphers(ssl, &out);
2915 STACK_OF(SSL_CIPHER) *peer_ciphers =
NULL;
2916 STACK_OF(SSL_CIPHER) *scsvc =
NULL;
2918 if (len && SSL_bytes_to_cipher_list(ssl, out, len,
2919 SSL_client_hello_isv2(ssl),
2920 &peer_ciphers, &scsvc)) {
2922 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2923 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2926 "Client cipher: %s (%04x)\n",
2927 SSL_CIPHER_get_name(peer_cipher),
2928 SSL_CIPHER_get_protocol_id(peer_cipher));
2929 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2935 sk_SSL_CIPHER_free(peer_ciphers);
2936 sk_SSL_CIPHER_free(scsvc);
2939 if (psk_requested) {
2945 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2951 return SSL_CLIENT_HELLO_SUCCESS;
2961 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_client_certificate_type,
2964 for (ii = 0; ii < outlen; ii++) {
2980 *al = SSL_AD_UNSUPPORTED_EXTENSION;
2981 return SSL_CLIENT_HELLO_ERROR;
2990 coap_openssl_context_t *context =
2992 const char *sni =
"";
2993 char *sni_tmp =
NULL;
2996 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2998 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2999 out[2] == TLSEXT_NAMETYPE_host_name &&
3000 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
3004 sni_tmp = OPENSSL_malloc(outlen+1);
3005 sni_tmp[outlen] =
'\000';
3006 memcpy(sni_tmp, out, outlen);
3010 for (i = 0; i < context->sni_count; i++) {
3011 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
3015 if (i == context->sni_count) {
3025 *al = SSL_AD_UNRECOGNIZED_NAME;
3026 return SSL_CLIENT_HELLO_ERROR;
3030 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
3031 (context->sni_count+1)*
sizeof(sni_entry));
3032 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
3033 context->sni_entry_list[context->sni_count].pki_key = *new_entry;
3034 context->sni_count++;
3037 OPENSSL_free(sni_tmp);
3039 sni_setup_data = *setup_data;
3040 sni_setup_data.
pki_key = context->sni_entry_list[i].pki_key;
3052 SSL_VERIFY_CLIENT_ONCE |
3053 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
3054 tls_verify_call_back);
3056 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
3065 X509_VERIFY_PARAM *param;
3067 param = X509_VERIFY_PARAM_new();
3069 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
3070 SSL_set1_param(ssl, param);
3071 X509_VERIFY_PARAM_free(param);
3079 return SSL_CLIENT_HELLO_SUCCESS;
3091psk_tls_client_hello_call_back(SSL *ssl,
3096 coap_openssl_context_t *o_context;
3098 const unsigned char *out;
3104 if (!c_session || !c_session->
context) {
3111 setup_data = &c_session->
context->spsk_setup_data;
3117 const char *sni =
"";
3118 char *sni_tmp =
NULL;
3121 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
3123 (((out[0]<<8) + out[1] +2) == (
int)outlen) &&
3124 out[2] == TLSEXT_NAMETYPE_host_name &&
3125 (((out[3]<<8) + out[4] +2 +3) == (
int)outlen)) {
3129 sni_tmp = OPENSSL_malloc(outlen+1);
3131 sni_tmp[outlen] =
'\000';
3132 memcpy(sni_tmp, out, outlen);
3137#if OPENSSL_VERSION_NUMBER < 0x10101000L
3140 for (i = 0; i < o_context->psk_sni_count; i++) {
3141 if (strcasecmp(sni, o_context->psk_sni_entry_list[i].sni) == 0) {
3145 if (i == o_context->psk_sni_count) {
3158 *al = SSL_AD_UNRECOGNIZED_NAME;
3159 return SSL_CLIENT_HELLO_ERROR;
3162#if OPENSSL_VERSION_NUMBER < 0x10101000L
3163 psk_sni_entry *tmp_entry;
3165 OPENSSL_realloc(o_context->psk_sni_entry_list,
3166 (o_context->psk_sni_count+1)*
sizeof(sni_entry));
3168 o_context->psk_sni_entry_list = tmp_entry;
3169 o_context->psk_sni_entry_list[o_context->psk_sni_count]
3171 OPENSSL_strdup(sni);
3172 if (o_context->psk_sni_entry_list[o_context->psk_sni_count].sni) {
3173 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
3175 o_context->psk_sni_count++;
3179 new_entry = &o_context->psk_sni_entry_list[i].psk_info;
3184 OPENSSL_free(sni_tmp);
3196 if (new_entry->
hint.
s) {
3197 snprintf(lhint,
sizeof(lhint),
"%.*s",
3200 SSL_use_psk_identity_hint(ssl, lhint);
3203 return SSL_CLIENT_HELLO_SUCCESS;
3206 *al = SSL_AD_INTERNAL_ERROR;
3207 return SSL_CLIENT_HELLO_ERROR;
3216 coap_openssl_context_t *context =
3221 context->setup_data = *setup_data;
3227 if (!defined_engine) {
3228 coap_log_warn(
"setup_pki: OpenSSL Engine not configured, PKI not set up\n");
3234 if (!context->setup_data.verify_peer_cert) {
3236 context->setup_data.check_common_ca = 0;
3238 context->setup_data.allow_self_signed = 1;
3239 context->setup_data.allow_expired_certs = 1;
3240 context->setup_data.cert_chain_validation = 1;
3241 context->setup_data.cert_chain_verify_depth = 10;
3242 context->setup_data.check_cert_revocation = 1;
3243 context->setup_data.allow_no_crl = 1;
3244 context->setup_data.allow_expired_crl = 1;
3245 context->setup_data.allow_bad_md_hash = 1;
3246 context->setup_data.allow_short_rsa_length = 1;
3248#if COAP_SERVER_SUPPORT
3250 if (context->dtls.ctx) {
3252#if OPENSSL_VERSION_NUMBER < 0x10101000L
3253 if (!setup_pki_server(context->dtls.ctx, setup_data))
3262#if OPENSSL_VERSION_NUMBER < 0x10101000L
3263 if (SSLeay() >= 0x10101000L) {
3264 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
3265 "no certificate checking\n",
3266 OPENSSL_VERSION_NUMBER, SSLeay());
3268 SSL_CTX_set_tlsext_servername_arg(context->dtls.ctx, &context->setup_data);
3269 SSL_CTX_set_tlsext_servername_callback(context->dtls.ctx,
3270 tls_server_name_call_back);
3272 SSL_CTX_set_client_hello_cb(context->dtls.ctx,
3273 tls_client_hello_call_back,
3277#if !COAP_DISABLE_TCP
3278 if (context->tls.ctx) {
3280#if OPENSSL_VERSION_NUMBER < 0x10101000L
3281 if (!setup_pki_server(context->tls.ctx, setup_data))
3290#if OPENSSL_VERSION_NUMBER < 0x10101000L
3291 if (SSLeay() >= 0x10101000L) {
3292 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
3293 "no certificate checking\n",
3294 OPENSSL_VERSION_NUMBER, SSLeay());
3296 SSL_CTX_set_tlsext_servername_arg(context->tls.ctx, &context->setup_data);
3297 SSL_CTX_set_tlsext_servername_callback(context->tls.ctx,
3298 tls_server_name_call_back);
3300 SSL_CTX_set_client_hello_cb(context->tls.ctx,
3301 tls_client_hello_call_back,
3305 SSL_CTX_set_alpn_select_cb(context->tls.ctx, server_alpn_callback,
NULL);
3313 if (!context->dtls.ssl) {
3315 context->dtls.ssl = SSL_new(context->dtls.ctx);
3316 if (!context->dtls.ssl)
3318 bio = BIO_new(context->dtls.meth);
3320 SSL_free(context->dtls.ssl);
3321 context->dtls.ssl =
NULL;
3324 SSL_set_bio(context->dtls.ssl, bio, bio);
3325 SSL_set_app_data(context->dtls.ssl,
NULL);
3326 SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
3329 context->psk_pki_enabled |= IS_PKI;
3338 const char *ca_file,
3341 coap_openssl_context_t *context =
3343 if (context->dtls.ctx) {
3344 if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) {
3346 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
3350#if !COAP_DISABLE_TCP
3351 if (context->tls.ctx) {
3352 if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) {
3354 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
3364#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3365 coap_openssl_context_t *context =
3367 if (context->dtls.ctx) {
3368 if (!SSL_CTX_set_default_verify_store(context->dtls.ctx)) {
3373#if !COAP_DISABLE_TCP
3374 if (context->tls.ctx) {
3375 if (!SSL_CTX_set_default_verify_store(context->tls.ctx)) {
3384 coap_log_warn(
"coap_context_set_pki_trust_store: (D)TLS environment "
3385 "not supported for OpenSSL < v3.0.0\n");
3392 coap_openssl_context_t *context =
3394 return context->psk_pki_enabled ? 1 : 0;
3401 coap_openssl_context_t *context = (coap_openssl_context_t *)handle;
3403 if (context->dtls.ssl)
3404 SSL_free(context->dtls.ssl);
3405 if (context->dtls.ctx)
3406 SSL_CTX_free(context->dtls.ctx);
3407 if (context->dtls.cookie_hmac)
3408 HMAC_CTX_free(context->dtls.cookie_hmac);
3409 if (context->dtls.meth)
3410 BIO_meth_free(context->dtls.meth);
3411 if (context->dtls.bio_addr)
3412 BIO_ADDR_free(context->dtls.bio_addr);
3413#if !COAP_DISABLE_TCP
3414 if (context->tls.ctx)
3415 SSL_CTX_free(context->tls.ctx);
3416 if (context->tls.meth)
3417 BIO_meth_free(context->tls.meth);
3419 for (i = 0; i < context->sni_count; i++) {
3420 OPENSSL_free(context->sni_entry_list[i].sni);
3421#if OPENSSL_VERSION_NUMBER < 0x10101000L
3422 SSL_CTX_free(context->sni_entry_list[i].ctx);
3425 if (context->sni_count)
3426 OPENSSL_free(context->sni_entry_list);
3427#if OPENSSL_VERSION_NUMBER < 0x10101000L
3428 for (i = 0; i < context->psk_sni_count; i++) {
3429 OPENSSL_free((
char *)context->psk_sni_entry_list[i].sni);
3430 SSL_CTX_free(context->psk_sni_entry_list[i].ctx);
3432 if (context->psk_sni_count)
3433 OPENSSL_free(context->psk_sni_entry_list);
3438#if COAP_SERVER_SUPPORT
3443 coap_ssl_data *data;
3444 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3449 nssl = SSL_new(dtls->ctx);
3452 nbio = BIO_new(dtls->meth);
3455 SSL_set_bio(nssl, nbio, nbio);
3456 SSL_set_app_data(nssl,
NULL);
3457 SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE);
3458 SSL_set_mtu(nssl, (
long)session->
mtu);
3462 SSL_set_app_data(ssl, session);
3464 rbio = SSL_get_rbio(ssl);
3465 data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) :
NULL;
3468 data->session = session;
3473 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
3476 memcpy(hint, psk_hint->
s, psk_hint->
length);
3477 hint[psk_hint->
length] =
'\000';
3478 SSL_use_psk_identity_hint(ssl, hint);
3485 r = SSL_accept(ssl);
3487 int err = SSL_get_error(ssl, r);
3488 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3506#if COAP_CLIENT_SUPPORT
3510 coap_openssl_context_t *context =
3513 if (context->psk_pki_enabled & IS_PSK) {
3518 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
3522 SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
3523#if COAP_SERVER_SUPPORT
3524 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
3526 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
3527#ifdef COAP_OPENSSL_PSK_SECURITY_LEVEL
3533 SSL_set_security_level(ssl, COAP_OPENSSL_PSK_SECURITY_LEVEL);
3537 SSL_set_max_proto_version(ssl, DTLS1_2_VERSION);
3539#if !COAP_DISABLE_TCP
3541 SSL_set_max_proto_version(ssl, TLS1_2_VERSION);
3544 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
3547 if ((context->psk_pki_enabled & IS_PKI) ||
3548 (context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) {
3555 if (!(context->psk_pki_enabled & IS_PKI)) {
3572#if !COAP_DISABLE_TCP
3574 SSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
3579 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
3585 X509_VERIFY_PARAM *param;
3587 param = X509_VERIFY_PARAM_new();
3589 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
3590 SSL_set1_param(ssl, param);
3591 X509_VERIFY_PARAM_free(param);
3599 SSL_VERIFY_CLIENT_ONCE |
3600 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
3601 tls_verify_call_back);
3603 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
3610#if COAP_DTLS_RETRANSMIT_MS != 1000
3611#if OPENSSL_VERSION_NUMBER >= 0x10101000L
3613 DTLS_set_timer_cb(ssl, timer_cb);
3624 coap_ssl_data *data;
3626 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3627 coap_dtls_context_t *dtls = &context->dtls;
3629 ssl = SSL_new(dtls->ctx);
3632 bio = BIO_new(dtls->meth);
3635 data = (coap_ssl_data *)BIO_get_data(bio);
3638 data->session = session;
3639 SSL_set_bio(ssl, bio, bio);
3640 SSL_set_app_data(ssl, session);
3641 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
3642 SSL_set_mtu(ssl, (
long)session->
mtu);
3644 if (!setup_client_ssl_session(session, ssl))
3649 r = SSL_connect(ssl);
3651 int ret = SSL_get_error(ssl, r);
3652 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3670 SSL *ssl = (SSL *)session->
tls;
3672 SSL_set_mtu(ssl, (
long)session->
mtu);
3678 SSL *ssl = (SSL *)session->
tls;
3680 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3681 int r = SSL_shutdown(ssl);
3694 const uint8_t *data,
size_t data_len) {
3696 SSL *ssl = (SSL *)session->
tls;
3705 r = SSL_write(ssl, data, (
int)data_len);
3708 int err = SSL_get_error(ssl, r);
3709 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3712 if (err == SSL_ERROR_ZERO_RETURN)
3714 else if (err == SSL_ERROR_SSL) {
3715 unsigned long e = ERR_get_error();
3717 coap_log_info(
"***%s: coap_dtls_send: cannot send PDU: %d: %s\n",
3719 ERR_GET_REASON(e), ERR_reason_error_string(e));
3722 coap_log_info(
"***%s: coap_dtls_send: cannot send PDU: %d\n",
3741 if (r == (ssize_t)data_len)
3764 SSL *ssl = (SSL *)session->
tls;
3765 coap_ssl_data *ssl_data;
3769 rbio = ssl ? SSL_get_rbio(ssl) :
NULL;
3770 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) :
NULL;
3771 return ssl_data ? ssl_data->timeout : 1000;
3780 SSL *ssl = (SSL *)session->
tls;
3784 (DTLSv1_handle_timeout(ssl) < 0)) {
3794#if COAP_SERVER_SUPPORT
3797 const uint8_t *data,
size_t data_len) {
3798 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3799 coap_ssl_data *ssl_data;
3803 SSL_set_mtu(dtls->ssl, (
long)session->
mtu);
3804 rbio = dtls->ssl ? SSL_get_rbio(dtls->ssl) :
NULL;
3805 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) :
NULL;
3806 assert(ssl_data !=
NULL);
3811 if (ssl_data->pdu_len) {
3812 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3815 ssl_data->session = session;
3816 ssl_data->pdu = data;
3817 ssl_data->pdu_len = (unsigned)data_len;
3818 r = DTLSv1_listen(dtls->ssl, dtls->bio_addr);
3820 int err = SSL_get_error(dtls->ssl, r);
3821 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3841 coap_ssl_data *ssl_data;
3842 SSL *ssl = (SSL *)session->
tls;
3845#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3849 assert(ssl !=
NULL);
3851 int in_init = SSL_in_init(ssl);
3853 rbio = ssl ? SSL_get_rbio(ssl) :
NULL;
3854 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) :
NULL;
3860 if (ssl_data->pdu_len) {
3861 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3864 ssl_data->pdu = data;
3865 ssl_data->pdu_len = (unsigned)data_len;
3868#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3872 r = SSL_read(ssl, pdu, (
int)
sizeof(pdu));
3879 int err = SSL_get_error(ssl, r);
3880 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3881 if (in_init && SSL_is_init_finished(ssl)) {
3889 if (err == SSL_ERROR_ZERO_RETURN)
3891 else if (err == SSL_ERROR_SSL) {
3892 unsigned long e = ERR_get_error();
3894#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3895#include <openssl/proverr.h>
3896 if (ERR_GET_REASON(e) == PROV_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES && !retry) {
3902 coap_log_info(
"***%s: coap_dtls_receive: cannot recv PDU: %d: %s\n",
3904 ERR_GET_REASON(e), ERR_reason_error_string(e));
3907 coap_log_info(
"***%s: coap_dtls_receive: cannot send PDU %d\n",
3926 if (ssl_data && ssl_data->pdu_len) {
3928 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
3929 ssl_data->pdu_len = 0;
3930 ssl_data->pdu =
NULL;
3937 unsigned int overhead = 37;
3938 const SSL_CIPHER *s_ciph =
NULL;
3940 s_ciph = SSL_get_current_cipher(session->
tls);
3942 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
3944 const EVP_CIPHER *e_ciph;
3948 e_ciph = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(s_ciph));
3950 switch (EVP_CIPHER_mode(e_ciph)) {
3951 case EVP_CIPH_GCM_MODE:
3952 ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
3953 maclen = EVP_GCM_TLS_TAG_LEN;
3956 case EVP_CIPH_CCM_MODE:
3957 ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
3958 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3959 if (strstr(cipher,
"CCM8"))
3965 case EVP_CIPH_CBC_MODE:
3966 e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
3967 blocksize = EVP_CIPHER_block_size(e_ciph);
3968 ivlen = EVP_CIPHER_iv_length(e_ciph);
3970 maclen = EVP_MD_size(e_md);
3973 case EVP_CIPH_STREAM_CIPHER:
3980 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3987 overhead = DTLS1_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 + pad;
3992#if !COAP_DISABLE_TCP
3993#if COAP_CLIENT_SUPPORT
3999 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
4000 coap_tls_context_t *tls = &context->tls;
4002 ssl = SSL_new(tls->ctx);
4005 bio = BIO_new(tls->meth);
4008 BIO_set_data(bio, session);
4009 SSL_set_bio(ssl, bio, bio);
4010 SSL_set_app_data(ssl, session);
4012 if (!setup_client_ssl_session(session, ssl))
4015 r = SSL_connect(ssl);
4017 int ret = SSL_get_error(ssl, r);
4018 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
4020 if (ret == SSL_ERROR_WANT_READ)
4022 if (ret == SSL_ERROR_WANT_WRITE) {
4024#ifdef COAP_EPOLL_SUPPORT
4038 if (SSL_is_init_finished(ssl)) {
4052#if COAP_SERVER_SUPPORT
4057 coap_tls_context_t *tls = &((coap_openssl_context_t *)session->
context->
dtls_context)->tls;
4061 ssl = SSL_new(tls->ctx);
4064 bio = BIO_new(tls->meth);
4067 BIO_set_data(bio, session);
4068 SSL_set_bio(ssl, bio, bio);
4069 SSL_set_app_data(ssl, session);
4073 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
4076 memcpy(hint, psk_hint->
s, psk_hint->
length);
4077 hint[psk_hint->
length] =
'\000';
4078 SSL_use_psk_identity_hint(ssl, hint);
4085 r = SSL_accept(ssl);
4087 int err = SSL_get_error(ssl, r);
4088 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
4090 if (err == SSL_ERROR_WANT_READ)
4092 if (err == SSL_ERROR_WANT_WRITE) {
4094#ifdef COAP_EPOLL_SUPPORT
4108 if (SSL_is_init_finished(ssl)) {
4113#if COAP_DTLS_RETRANSMIT_MS != 1000
4114#if OPENSSL_VERSION_NUMBER >= 0x10101000L
4116 DTLS_set_timer_cb(ssl, timer_cb);
4132 SSL *ssl = (SSL *)session->
tls;
4134 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
4135 int r = SSL_shutdown(ssl);
4153 SSL *ssl = (SSL *)session->
tls;
4159 in_init = !SSL_is_init_finished(ssl);
4162 r = SSL_write(ssl, data, (
int)data_len);
4165 int err = SSL_get_error(ssl, r);
4166 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
4167 if (in_init && SSL_is_init_finished(ssl)) {
4173 if (err == SSL_ERROR_WANT_READ)
4175 else if (err == SSL_ERROR_WANT_WRITE) {
4177#ifdef COAP_EPOLL_SUPPORT
4187 if (err == SSL_ERROR_ZERO_RETURN)
4189 else if (err == SSL_ERROR_SSL) {
4190 unsigned long e = ERR_get_error();
4192 coap_log_info(
"***%s: coap_tls_write: cannot send PDU: %d: %s\n",
4194 ERR_GET_REASON(e), ERR_reason_error_string(e));
4197 coap_log_info(
"***%s: coap_tls_send: cannot send PDU: %d\n",
4202 }
else if (in_init && SSL_is_init_finished(ssl)) {
4221 if (r == (ssize_t)data_len)
4238 SSL *ssl = (SSL *)session->
tls;
4246 in_init = !SSL_is_init_finished(ssl);
4249 r = SSL_read(ssl, data, (
int)data_len);
4251 int err = SSL_get_error(ssl, r);
4252 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
4253 if (in_init && SSL_is_init_finished(ssl)) {
4259 if (err == SSL_ERROR_WANT_READ)
4261 if (err == SSL_ERROR_WANT_WRITE) {
4263#ifdef COAP_EPOLL_SUPPORT
4273 if (err == SSL_ERROR_ZERO_RETURN)
4275 else if (err == SSL_ERROR_SSL) {
4276 unsigned long e = ERR_get_error();
4278 coap_log_info(
"***%s: coap_tls_read: cannot recv PDU: %d: %s\n",
4280 ERR_GET_REASON(e), ERR_reason_error_string(e));
4288 }
else if (in_init && SSL_is_init_finished(ssl)) {
4314#if COAP_SERVER_SUPPORT
4316coap_digest_setup(
void) {
4317 EVP_MD_CTX *digest_ctx = EVP_MD_CTX_new();
4320 EVP_DigestInit_ex(digest_ctx, EVP_sha256(),
NULL);
4326coap_digest_free(coap_digest_ctx_t *digest_ctx) {
4328 EVP_MD_CTX_free(digest_ctx);
4332coap_digest_update(coap_digest_ctx_t *digest_ctx,
4333 const uint8_t *data,
4335 return EVP_DigestUpdate(digest_ctx, data, data_len);
4339coap_digest_final(coap_digest_ctx_t *digest_ctx,
4340 coap_digest_t *digest_buffer) {
4341 unsigned int size =
sizeof(coap_digest_t);
4342 int ret = EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
4344 coap_digest_free(digest_ctx);
4349#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
4351coap_crypto_output_errors(
const char *prefix) {
4352#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
4357 while ((e = ERR_get_error()))
4360 ERR_reason_error_string(e),
4361 ssl_function_definition(e));
4371static struct hash_algs {
4373 const EVP_MD *(*get_hash)(void);
4382static const EVP_MD *
4383get_hash_alg(
cose_alg_t alg,
size_t *length) {
4386 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
4387 if (hashs[idx].alg == alg) {
4388 *length = hashs[idx].length;
4389 return hashs[idx].get_hash();
4392 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
4400 unsigned int length;
4401 const EVP_MD *evp_md;
4402 EVP_MD_CTX *evp_ctx =
NULL;
4406 if ((evp_md = get_hash_alg(alg, &hash_length)) ==
NULL) {
4407 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
4410 evp_ctx = EVP_MD_CTX_new();
4411 if (evp_ctx ==
NULL)
4413 if (EVP_DigestInit_ex(evp_ctx, evp_md,
NULL) == 0)
4416 if (EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
4422 if (EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
4424 dummy->length = length;
4425 if (hash_length < dummy->length)
4426 dummy->length = hash_length;
4428 EVP_MD_CTX_free(evp_ctx);
4432 coap_crypto_output_errors(
"coap_crypto_hash");
4435 EVP_MD_CTX_free(evp_ctx);
4440#if COAP_OSCORE_SUPPORT
4446#include <openssl/evp.h>
4447#include <openssl/hmac.h>
4454static struct cipher_algs {
4456 const EVP_CIPHER *(*get_cipher)(void);
4461static const EVP_CIPHER *
4465 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
4466 if (ciphers[idx].alg == alg)
4467 return ciphers[idx].get_cipher();
4469 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
4478static struct hmac_algs {
4480 const EVP_MD *(*get_hmac)(void);
4487static const EVP_MD *
4491 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
4492 if (hmacs[idx].hmac_alg == hmac_alg)
4493 return hmacs[idx].get_hmac();
4495 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
4501 return get_cipher_alg(alg) !=
NULL;
4510 return get_hmac_alg(hmac_alg) !=
NULL;
4514 if (1 != (Func)) { \
4523 size_t *max_result_len) {
4524 const EVP_CIPHER *cipher;
4527 int result_len = (int)(*max_result_len & INT_MAX);
4528 EVP_CIPHER_CTX *ctx;
4533 assert(params !=
NULL);
4534 if (!params || ((cipher = get_cipher_alg(params->
alg)) ==
NULL)) {
4541 ctx = EVP_CIPHER_CTX_new();
4547 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l,
NULL));
4548 C(EVP_CIPHER_CTX_ctrl(ctx,
4549 EVP_CTRL_AEAD_SET_IVLEN,
4552 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len,
NULL));
4556 C(EVP_EncryptUpdate(ctx,
NULL, &result_len,
NULL, (
int)data->
length));
4557 if (aad && aad->
s && (aad->
length > 0)) {
4558 C(EVP_EncryptUpdate(ctx,
NULL, &result_len, aad->
s, (
int)aad->
length));
4560 C(EVP_EncryptUpdate(ctx, result, &result_len, data->
s, (
int)data->
length));
4563 C(EVP_EncryptFinal_ex(ctx, result + result_len, &tmp));
4567 C(EVP_CIPHER_CTX_ctrl(ctx,
4568 EVP_CTRL_CCM_GET_TAG,
4570 result + result_len));
4572 *max_result_len = result_len + ccm->
tag_len;
4573 EVP_CIPHER_CTX_free(ctx);
4577 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
4586 size_t *max_result_len) {
4587 const EVP_CIPHER *cipher;
4593 EVP_CIPHER_CTX *ctx;
4598 assert(params !=
NULL);
4599 if (!params || ((cipher = get_cipher_alg(params->
alg)) ==
NULL)) {
4611 memcpy(&rwtag, &tag,
sizeof(rwtag));
4614 ctx = EVP_CIPHER_CTX_new();
4619 C(EVP_CIPHER_CTX_ctrl(ctx,
4620 EVP_CTRL_AEAD_SET_IVLEN,
4623 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, rwtag));
4624 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l,
NULL));
4629 if (aad && aad->
s && (aad->
length > 0)) {
4630 C(EVP_DecryptUpdate(ctx,
NULL, &len, aad->
s, (
int)aad->
length));
4632 tmp = EVP_DecryptUpdate(ctx, result, &len, data->
s, (
int)data->
length);
4633 EVP_CIPHER_CTX_free(ctx);
4635 *max_result_len = 0;
4638 *max_result_len = len;
4642 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
4651 unsigned int result_len;
4652 const EVP_MD *evp_md;
4659 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
4660 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
4666 result_len = (
unsigned int)
dummy->length;
4674 dummy->length = result_len;
4680 coap_crypto_output_errors(
"coap_crypto_hmac");
4692#pragma GCC diagnostic ignored "-Wunused-function"
#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)
int coap_dtls_context_load_pki_trust_store(coap_context_t *ctx 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)
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.
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.
#define COAP_DTLS_RETRANSMIT_MS
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_thread_shutdown(void)
Close down the underlying (D)TLS Library layer.
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.
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_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.
int coap_tls_engine_remove(void)
Remove a previously configured ENGINE from a TLS library.
@ 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, 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.
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::@2 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.
coap_pki_key_define_t define
for definable type keys
union coap_dtls_key_t::@3 key
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