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 size_t psk_sni_count;
171 psk_sni_entry *psk_sni_entry_list;
172} coap_openssl_context_t;
174#if COAP_SERVER_SUPPORT
175#if OPENSSL_VERSION_NUMBER < 0x10101000L
176static int psk_tls_server_name_call_back(SSL *ssl,
int *sd,
void *arg);
178static int psk_tls_client_hello_call_back(SSL *ssl,
int *al,
void *arg);
184 if (SSLeay() < 0x10100000L) {
185 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
188#if OPENSSL_VERSION_NUMBER >= 0x10101000L
196 if (SSLeay() < 0x10101000L) {
197 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
207 if (SSLeay() < 0x10100000L) {
208 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
211#if OPENSSL_VERSION_NUMBER >= 0x10101000L
212 if (SSLeay() < 0x10101000L) {
213 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
268#if COAP_CLIENT_SUPPORT
286static ENGINE *pkcs11_engine = NULL;
287static ENGINE *defined_engine = NULL;
291 SSL_load_error_strings();
293 ENGINE_load_dynamic();
300 ENGINE_finish(pkcs11_engine);
301 pkcs11_engine = NULL;
303 if (defined_engine) {
305 ENGINE_finish(defined_engine);
306 defined_engine = NULL;
318 return c_session->
tls;
324get_split_conf_entry(
const uint8_t **start,
size_t size,
const char *get_keyword,
326 const uint8_t *begin = *start;
329 const uint8_t *split;
335 kend = end = memchr(begin,
'\n', size);
341 if (end > begin && end[-1] ==
'\r')
344 if (begin[0] ==
'#' || (end - begin) == 0) {
346 size -= kend - begin + 1;
352 split = memchr(begin,
':', end - begin);
356 if ((
size_t)(split - begin) != strlen(get_keyword)) {
357 size -= kend - begin + 1;
361 if (memcmp(begin, get_keyword, split - begin)) {
362 size -= kend - begin + 1;
370 if ((end - begin) == 0)
373 split = memchr(begin,
':', end - begin);
385 if ((end - split) > 0) {
420 const uint8_t *start;
425 unsigned int defaults = 0;
426 int done_engine_id = 0;
427 int done_engine_init = 0;
433 end = start + conf_mem->
length;
435 if (defined_engine) {
436 coap_log_warn(
"coap_tls_engine_configure: Freeing off previous engine definition\n");
437 ENGINE_finish(defined_engine);
438 defined_engine = NULL;
442 if (!get_split_conf_entry(&start, end - start,
"engine", &engine_id, &p2)) {
443 coap_log_warn(
"coap_tls_engine_configure: engine not defined\n");
446 defined_engine = ENGINE_by_id((
const char *)engine_id->
s);
447 if (!defined_engine) {
448 coap_log_warn(
"coap_tls_engine_configure: engine '%s' not known\n", engine_id->
s);
458 while (get_split_conf_entry(&start, end - start,
"pre-cmd", &p1, &p2)) {
459 if (!ENGINE_ctrl_cmd_string(defined_engine, (
const char *)p1->
s, p2 ? (
const char *)p2->
s : NULL,
461 coap_log_warn(
"coap_tls_engine_configure: engine %s pre-cmd '%s:%s' failed\n",
462 (
const char *)engine_id->
s,
463 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
467 engine_id->
s, p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
476 if (!ENGINE_init(defined_engine)) {
477 coap_log_warn(
"coap_tls_engine_configure: %s failed initialization\n", (
const char *)engine_id->
s);
480 done_engine_init = 1;
482 (
const char *)engine_id->
s);
487 while (get_split_conf_entry(&start, end - start,
"post-cmd", &p1, &p2)) {
488 if (!ENGINE_ctrl_cmd_string(defined_engine, (
const char *)p1->
s, p2 ? (
const char *)p2->
s : NULL,
490 coap_log_warn(
"coap_tls_engine_configure: %s post-cmd '%s:%s' failed\n", (
const char *)engine_id->
s,
491 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
495 (
const char *)engine_id->
s,
496 (
const char *)p1->
s, p2 ? (
const char *)p2->
s :
"(NULL)");
504 if (!get_split_conf_entry(&start, end - start,
"enable-methods", &p1, &p2)) {
505 coap_log_warn(
"coap_tls_engine_configure: enable-methods not found\n");
508 defaults = strtoul((
const char *)p1->
s, NULL, 0);
509 if (!ENGINE_set_default(defined_engine, defaults)) {
510 coap_log_warn(
"coap_tls_engine_configure: enable-methods 0x%x invalid\n", defaults);
525 ENGINE_free(defined_engine);
526 if (done_engine_init)
527 ENGINE_finish(defined_engine);
528 defined_engine = NULL;
537 if (defined_engine) {
538 ENGINE_finish(defined_engine);
539 defined_engine = NULL;
560typedef struct coap_ssl_data {
569coap_dgram_create(BIO *a) {
570 coap_ssl_data *data = NULL;
571 data = malloc(
sizeof(coap_ssl_data));
575 BIO_set_data(a, data);
576 memset(data, 0x00,
sizeof(coap_ssl_data));
581coap_dgram_destroy(BIO *a) {
585 data = (coap_ssl_data *)BIO_get_data(a);
592coap_dgram_read(BIO *a,
char *out,
int outl) {
594 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
597 if (data != NULL && data->pdu_len > 0) {
598 if (outl < (
int)data->pdu_len) {
599 memcpy(out, data->pdu, outl);
602 memcpy(out, data->pdu, data->pdu_len);
603 ret = (int)data->pdu_len;
605 if (!data->peekmode) {
612 BIO_clear_retry_flags(a);
614 BIO_set_retry_read(a);
620coap_dgram_write(BIO *a,
const char *in,
int inl) {
622 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
624 if (data && data->session) {
627 && data->session->endpoint == NULL
631 BIO_clear_retry_flags(a);
635 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
638 BIO_clear_retry_flags(a);
640 BIO_set_retry_write(a);
642 BIO_clear_retry_flags(a);
649coap_dgram_puts(BIO *a,
const char *pstr) {
650 return coap_dgram_write(a, pstr, (
int)strlen(pstr));
654coap_dgram_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
656 coap_ssl_data *data = BIO_get_data(a);
661 case BIO_CTRL_GET_CLOSE:
662 ret = BIO_get_shutdown(a);
664 case BIO_CTRL_SET_CLOSE:
665 BIO_set_shutdown(a, (
int)num);
667 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
669 data->peekmode = (unsigned)num;
673 case BIO_CTRL_DGRAM_CONNECT:
676 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
677 case BIO_CTRL_DGRAM_GET_MTU:
678 case BIO_CTRL_DGRAM_SET_MTU:
679 case BIO_CTRL_DGRAM_QUERY_MTU:
680 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
685 case BIO_CTRL_DGRAM_MTU_DISCOVER:
686 case BIO_CTRL_DGRAM_SET_CONNECTED:
688 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
691 ((
struct timeval *)ptr)->tv_usec);
696 case BIO_C_FILE_SEEK:
697 case BIO_C_FILE_TELL:
699 case BIO_CTRL_PENDING:
700 case BIO_CTRL_WPENDING:
701 case BIO_CTRL_DGRAM_GET_PEER:
702 case BIO_CTRL_DGRAM_SET_PEER:
703 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
704 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
705 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
706 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
707 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
708 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
709 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
710 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
719coap_dtls_generate_cookie(SSL *ssl,
720 unsigned char *cookie,
721 unsigned int *cookie_len) {
722 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
723 coap_dtls_context_t *dtls = ctx ? (coap_dtls_context_t *)SSL_CTX_get_app_data(ctx) : NULL;
724 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
727 int r = HMAC_Init_ex(dtls->cookie_hmac, NULL, 0, NULL, NULL);
728 r &= HMAC_Update(dtls->cookie_hmac,
729 (
const uint8_t *)&data->session->addr_info.local.addr,
730 (
size_t)data->session->addr_info.local.size);
731 r &= HMAC_Update(dtls->cookie_hmac,
732 (
const uint8_t *)&data->session->addr_info.remote.addr,
733 (
size_t)data->session->addr_info.remote.size);
734 r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
741coap_dtls_verify_cookie(SSL *ssl,
742 const uint8_t *cookie,
743 unsigned int cookie_len) {
746 if (coap_dtls_generate_cookie(ssl, hmac, &len) &&
747 cookie_len == len && memcmp(cookie, hmac, len) == 0)
753#if COAP_CLIENT_SUPPORT
755coap_dtls_psk_client_callback(SSL *ssl,
758 unsigned int max_identity_len,
760 unsigned int max_psk_len) {
762 coap_openssl_context_t *o_context;
770 if (c_session == NULL || c_session->
context == NULL)
773 if (o_context == NULL)
777 temp.
s = hint ? (
const uint8_t *)hint : (const uint8_t *)
"";
778 temp.
length = strlen((
const char *)temp.
s);
782 (
const char *)temp.
s);
794 if (cpsk_info == NULL)
799 psk_identity = &cpsk_info->
identity;
800 psk_key = &cpsk_info->
key;
806 if (psk_identity == NULL || psk_key == NULL) {
812 if (!max_identity_len)
815 if (psk_identity->
length > max_identity_len) {
816 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
820 max_identity_len = (
unsigned int)psk_identity->
length;
822 memcpy(identity, psk_identity->
s, max_identity_len);
823 identity[max_identity_len] =
'\000';
825 if (psk_key->
length > max_psk_len) {
830 max_psk_len = (
unsigned int)psk_key->
length;
832 memcpy(psk, psk_key->
s, max_psk_len);
837#if COAP_SERVER_SUPPORT
839coap_dtls_psk_server_callback(
841 const char *identity,
843 unsigned int max_psk_len
851 if (c_session == NULL || c_session->
context == NULL)
857 lidentity.
s = identity ? (
const uint8_t *)identity : (const uint8_t *)
"";
858 lidentity.
length = strlen((
const char *)lidentity.
s);
862 (
int)lidentity.
length, (
const char *)lidentity.
s);
877 if (psk_key->
length > max_psk_len) {
882 max_psk_len = (
unsigned int)psk_key->
length;
884 memcpy(psk, psk_key->
s, max_psk_len);
890ssl_function_definition(
unsigned long e) {
891#if OPENSSL_VERSION_NUMBER >= 0x30000000L
895 static char buff[80];
897 snprintf(buff,
sizeof(buff),
" at %s:%s",
898 ERR_lib_error_string(e), ERR_func_error_string(e));
904coap_dtls_info_callback(
const SSL *ssl,
int where,
int ret) {
907 int w = where &~SSL_ST_MASK;
911 "coap_dtls_info_callback: session not determined, where 0x%0x and ret 0x%0x\n", where, ret);
914 if (w & SSL_ST_CONNECT)
915 pstr =
"SSL_connect";
916 else if (w & SSL_ST_ACCEPT)
921 if (where & SSL_CB_LOOP) {
924 }
else if (where & SSL_CB_ALERT) {
926 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
927 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) {
929 if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY)
933 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
936 SSL_alert_type_string_long(ret),
937 SSL_alert_desc_string_long(ret));
938 }
else if (where & SSL_CB_EXIT) {
944 while ((e = ERR_get_error()))
947 ssl_function_definition(e));
949 }
else if (ret < 0) {
951 int err = SSL_get_error(ssl, ret);
952 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE &&
953 err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT &&
954 err != SSL_ERROR_WANT_X509_LOOKUP) {
958 while ((e = ERR_get_error()))
961 ssl_function_definition(e));
967 if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
973coap_sock_create(BIO *a) {
979coap_sock_destroy(BIO *a) {
991coap_sock_read(BIO *a,
char *out,
int outl) {
995 if (session && out != NULL) {
1000 BIO_set_retry_read(a);
1003 BIO_clear_retry_flags(a);
1016coap_sock_write(BIO *a,
const char *in,
int inl) {
1025 (
const uint8_t *)in,
1029 BIO_clear_retry_flags(a);
1031 BIO_set_retry_read(a);
1034 BIO_clear_retry_flags(a);
1038 (errno == EPIPE || errno == ECONNRESET)) {
1058coap_sock_puts(BIO *a,
const char *pstr) {
1059 return coap_sock_write(a, pstr, (
int)strlen(pstr));
1063coap_sock_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
1074 case BIO_CTRL_SET_CLOSE:
1076 case BIO_CTRL_FLUSH:
1080 case BIO_CTRL_GET_CLOSE:
1089coap_set_user_prefs(SSL_CTX *ctx) {
1090 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
1092#ifdef COAP_OPENSSL_SIGALGS
1093 SSL_CTX_set1_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
1094 SSL_CTX_set1_client_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
1097#if OPENSSL_VERSION_NUMBER >= 0x10101000L && defined(COAP_OPENSSL_GROUPS)
1098 SSL_CTX_set1_groups_list(ctx, COAP_OPENSSL_GROUPS);
1102#if COAP_DTLS_RETRANSMIT_MS != 1000
1103#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1105timer_cb(SSL *s,
unsigned int timer_us) {
1108 return COAP_DTLS_RETRANSMIT_MS * 1000;
1110 return 2 * timer_us;
1117 coap_openssl_context_t *context;
1122 uint8_t cookie_secret[32];
1124 memset(context, 0,
sizeof(coap_openssl_context_t));
1127 context->dtls.ctx = SSL_CTX_new(DTLS_method());
1128 if (!context->dtls.ctx)
1130 SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
1131 SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
1132 SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
1133 coap_set_user_prefs(context->dtls.ctx);
1134 memset(cookie_secret, 0,
sizeof(cookie_secret));
1135 if (!RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
1137 "Insufficient entropy for random cookie generation");
1140 context->dtls.cookie_hmac = HMAC_CTX_new();
1141 if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
1142 EVP_sha256(), NULL))
1144 SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
1145 SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
1146 SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
1147 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
1148#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1149 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_LEGACY_SERVER_CONNECT);
1151 context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM,
"coapdgram");
1152 if (!context->dtls.meth)
1154 context->dtls.bio_addr = BIO_ADDR_new();
1155 if (!context->dtls.bio_addr)
1157 BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
1158 BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
1159 BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
1160 BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
1161 BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
1162 BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
1164#if !COAP_DISABLE_TCP
1166 context->tls.ctx = SSL_CTX_new(TLS_method());
1167 if (!context->tls.ctx)
1169 SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
1170 SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
1171 coap_set_user_prefs(context->tls.ctx);
1172 SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
1173 context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET,
"coapsock");
1174 if (!context->tls.meth)
1176 BIO_meth_set_write(context->tls.meth, coap_sock_write);
1177 BIO_meth_set_read(context->tls.meth, coap_sock_read);
1178 BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
1179 BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
1180 BIO_meth_set_create(context->tls.meth, coap_sock_create);
1181 BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
1192#if COAP_SERVER_SUPPORT
1197 coap_openssl_context_t *o_context =
1201 if (!setup_data || !o_context)
1204 SSL_CTX_set_psk_server_callback(o_context->dtls.ctx,
1205 coap_dtls_psk_server_callback);
1206#if !COAP_DISABLE_TCP
1207 SSL_CTX_set_psk_server_callback(o_context->tls.ctx,
1208 coap_dtls_psk_server_callback);
1214 SSL_CTX_use_psk_identity_hint(o_context->dtls.ctx, hint);
1215#if !COAP_DISABLE_TCP
1216 SSL_CTX_use_psk_identity_hint(o_context->tls.ctx, hint);
1220#if OPENSSL_VERSION_NUMBER < 0x10101000L
1221 SSL_CTX_set_tlsext_servername_arg(o_context->dtls.ctx,
1223 SSL_CTX_set_tlsext_servername_callback(o_context->dtls.ctx,
1224 psk_tls_server_name_call_back);
1225#if !COAP_DISABLE_TCP
1226 SSL_CTX_set_tlsext_servername_arg(o_context->tls.ctx,
1228 SSL_CTX_set_tlsext_servername_callback(o_context->tls.ctx,
1229 psk_tls_server_name_call_back);
1232 SSL_CTX_set_client_hello_cb(o_context->dtls.ctx,
1233 psk_tls_client_hello_call_back,
1235#if !COAP_DISABLE_TCP
1236 SSL_CTX_set_client_hello_cb(o_context->tls.ctx,
1237 psk_tls_client_hello_call_back,
1243 if (!o_context->dtls.ssl) {
1245 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
1246 if (!o_context->dtls.ssl)
1248 bio = BIO_new(o_context->dtls.meth);
1250 SSL_free(o_context->dtls.ssl);
1251 o_context->dtls.ssl = NULL;
1254 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1255 SSL_set_app_data(o_context->dtls.ssl, NULL);
1256 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1262 o_context->psk_pki_enabled |= IS_PSK;
1267#if COAP_CLIENT_SUPPORT
1272 coap_openssl_context_t *o_context =
1276 if (!setup_data || !o_context)
1279 if (!o_context->dtls.ssl) {
1281 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
1282 if (!o_context->dtls.ssl)
1284 bio = BIO_new(o_context->dtls.meth);
1286 SSL_free(o_context->dtls.ssl);
1287 o_context->dtls.ssl = NULL;
1290 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1291 SSL_set_app_data(o_context->dtls.ssl, NULL);
1292 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1301 o_context->psk_pki_enabled |= IS_PSK;
1307map_key_type(
int asn1_private_key_type
1309 switch (asn1_private_key_type) {
1311 return EVP_PKEY_NONE;
1313 return EVP_PKEY_RSA;
1315 return EVP_PKEY_RSA2;
1317 return EVP_PKEY_DSA;
1319 return EVP_PKEY_DSA1;
1321 return EVP_PKEY_DSA2;
1323 return EVP_PKEY_DSA3;
1325 return EVP_PKEY_DSA4;
1329 return EVP_PKEY_DHX;
1333 return EVP_PKEY_HMAC;
1335 return EVP_PKEY_CMAC;
1337 return EVP_PKEY_TLS1_PRF;
1339 return EVP_PKEY_HKDF;
1341 coap_log_warn(
"*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
1342 asn1_private_key_type);
1347#if !COAP_DISABLE_TCP
1348static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1350#if COAP_SERVER_SUPPORT
1353 const unsigned char **out,
1354 unsigned char *outlen,
1355 const unsigned char *in,
1359 unsigned char *tout = NULL;
1362 return SSL_TLSEXT_ERR_NOACK;
1363 ret = SSL_select_next_proto(&tout,
1370 return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
1376add_ca_to_cert_store(X509_STORE *st, X509 *x509) {
1380 while (ERR_get_error() != 0) {
1383 if (!X509_STORE_add_cert(st, x509)) {
1384 while ((e = ERR_get_error()) != 0) {
1385 int r = ERR_GET_REASON(e);
1386 if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1389 ERR_reason_error_string(e),
1390 ssl_function_definition(e));
1397missing_ENGINE_load_cert(ENGINE *engine,
const char *cert_id) {
1399 const char *cert_id;
1403 params.cert_id = cert_id;
1407 if (!ENGINE_ctrl_cmd(engine,
"LOAD_CERT_CTRL", 0, ¶ms, NULL, 1)) {
1414check_pkcs11_engine(
void) {
1415 static int already_tried = 0;
1420 if (!pkcs11_engine) {
1421 pkcs11_engine = ENGINE_by_id(COAP_OPENSSL_PKCS11_ENGINE_ID);
1422 if (!pkcs11_engine) {
1423 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support - need OpenSSL %s engine\n",
1424 COAP_OPENSSL_PKCS11_ENGINE_ID);
1428 if (!ENGINE_init(pkcs11_engine)) {
1430 ENGINE_free(pkcs11_engine);
1431 pkcs11_engine = NULL;
1432 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1440 ENGINE_free(pkcs11_engine);
1445#if OPENSSL_VERSION_NUMBER < 0x10101000L && COAP_SERVER_SUPPORT
1448install_engine_public_cert_ctx(ENGINE *engine, SSL_CTX *ctx,
1449 const char *public_cert) {
1452 x509 = missing_ENGINE_load_cert(engine, public_cert);
1460 if (!SSL_CTX_use_certificate(ctx, x509)) {
1461 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1473install_engine_private_key_ctx(ENGINE *engine, SSL_CTX *ctx,
1474 const char *private_key) {
1475 EVP_PKEY *pkey = ENGINE_load_private_key(engine,
1486 if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
1487 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1491 EVP_PKEY_free(pkey);
1494 EVP_PKEY_free(pkey);
1499install_engine_ca_ctx(ENGINE *engine, SSL_CTX *ctx,
const char *ca) {
1503 x509 = missing_ENGINE_load_cert(engine,
1507 "%s CA Certificate\n",
1512 if (!SSL_CTX_add_client_CA(ctx, x509)) {
1513 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1514 "%s CA Certificate\n",
1520 st = SSL_CTX_get_cert_store(ctx);
1521 add_ca_to_cert_store(st, x509);
1527load_in_cas_ctx(SSL_CTX *ctx,
1528 const char *ca_file) {
1529 STACK_OF(X509_NAME) *cert_names;
1533 char *rw_var = NULL;
1534 cert_names = SSL_load_client_CA_file(ca_file);
1535 if (cert_names != NULL)
1536 SSL_CTX_set_client_CA_list(ctx, cert_names);
1538 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1545 st = SSL_CTX_get_cert_store(ctx);
1546 in = BIO_new(BIO_s_file());
1548 memcpy(&rw_var, &ca_file,
sizeof(rw_var));
1549 if (!BIO_read_filename(in, rw_var)) {
1556 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
1558 add_ca_to_cert_store(st, x);
1566setup_pki_server(SSL_CTX *ctx,
1582 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1584 SSL_FILETYPE_PEM))) {
1594 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1596 if (!pkey || !SSL_CTX_use_PrivateKey(ctx, pkey)) {
1600 EVP_PKEY_free(pkey);
1608 EVP_PKEY_free(pkey);
1620 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1622 SSL_FILETYPE_ASN1))) {
1640 if (!check_pkcs11_engine()) {
1645 if (ENGINE_ctrl_cmd_string(pkcs11_engine,
1648 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1653 if (!install_engine_private_key_ctx(pkcs11_engine, ctx,
1661 if (!defined_engine ||
1662 !install_engine_private_key_ctx(defined_engine, ctx,
1689 if (!(SSL_CTX_use_certificate_file(ctx,
1691 SSL_FILETYPE_PEM))) {
1701 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1703 if (!cert || !SSL_CTX_use_certificate(ctx, cert)) {
1727 if (!(SSL_CTX_use_certificate_file(ctx,
1729 SSL_FILETYPE_ASN1))) {
1737 !(SSL_CTX_use_certificate_ASN1(ctx,
1746 if (!check_pkcs11_engine()) {
1749 if (!install_engine_public_cert_ctx(pkcs11_engine, ctx,
1757 if (!defined_engine ||
1758 !install_engine_public_cert_ctx(defined_engine, ctx,
1796 X509_STORE *st = SSL_CTX_get_cert_store(ctx);
1800 if ((x = PEM_read_bio_X509(bp, NULL, NULL, NULL)) == NULL)
1802 add_ca_to_cert_store(st, x);
1803 SSL_CTX_add_client_CA(ctx, x);
1819 if (!(SSL_CTX_use_certificate_file(ctx,
1821 SSL_FILETYPE_ASN1))) {
1834 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1842 st = SSL_CTX_get_cert_store(ctx);
1843 add_ca_to_cert_store(st, x509);
1848 if (!check_pkcs11_engine()) {
1851 if (!install_engine_ca_ctx(pkcs11_engine, ctx,
1859 if (!defined_engine ||
1860 !install_engine_ca_ctx(defined_engine, ctx,
1879#if OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT
1882install_engine_public_cert(ENGINE *engine, SSL *ssl,
const char *public_cert,
1886 x509 = missing_ENGINE_load_cert(engine, public_cert);
1894 if (!SSL_use_certificate(ssl, x509)) {
1895 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1907install_engine_private_key(ENGINE *engine, SSL *ssl,
const char *private_key,
1909 EVP_PKEY *pkey = ENGINE_load_private_key(engine,
1920 if (!SSL_use_PrivateKey(ssl, pkey)) {
1921 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1925 EVP_PKEY_free(pkey);
1928 EVP_PKEY_free(pkey);
1933install_engine_ca(ENGINE *engine, SSL *ssl,
const char *ca,
1936 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1941 "%s CA Certificate (no ctx)\n",
1946 x509 = missing_ENGINE_load_cert(engine,
1950 "%s CA Certificate\n",
1955 if (!SSL_add_client_CA(ssl, x509)) {
1956 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1957 "%s CA Certificate\n",
1963 st = SSL_CTX_get_cert_store(ctx);
1964 add_ca_to_cert_store(st, x509);
1970load_in_cas(SSL *ssl,
1975 char *rw_var = NULL;
1976 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1979 STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(ca_file);
1981 if (cert_names != NULL)
1982 SSL_set_client_CA_list(ssl, cert_names);
1992 in = BIO_new(BIO_s_file());
1994 memcpy(&rw_var, &ca_file,
sizeof(rw_var));
1995 if (!BIO_read_filename(in, rw_var)) {
1999 st = SSL_CTX_get_cert_store(ctx);
2001 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
2003 add_ca_to_cert_store(st, x);
2011setup_pki_ssl(SSL *ssl,
2027 if (!(SSL_use_PrivateKey_file(ssl,
2029 SSL_FILETYPE_PEM))) {
2039 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
2041 if (!pkey || !SSL_use_PrivateKey(ssl, pkey)) {
2045 EVP_PKEY_free(pkey);
2053 EVP_PKEY_free(pkey);
2065 if (!(SSL_use_PrivateKey_file(ssl,
2067 SSL_FILETYPE_ASN1))) {
2085 if (!check_pkcs11_engine()) {
2090 if (ENGINE_ctrl_cmd_string(pkcs11_engine,
2093 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
2098 if (!install_engine_private_key(pkcs11_engine, ssl,
2107 if (!defined_engine ||
2108 !install_engine_private_key(defined_engine, ssl,
2137 if (!(SSL_use_certificate_file(ssl,
2139 SSL_FILETYPE_PEM))) {
2149 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
2151 if (!cert || !SSL_use_certificate(ssl, cert)) {
2175 if (!(SSL_use_certificate_file(ssl,
2177 SSL_FILETYPE_ASN1))) {
2185 !(SSL_use_certificate_ASN1(ssl,
2194 if (!check_pkcs11_engine()) {
2197 if (!install_engine_public_cert(pkcs11_engine, ssl,
2206 if (!defined_engine ||
2207 !install_engine_public_cert(defined_engine, ssl,
2246 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2248 X509_STORE *st = ctx? SSL_CTX_get_cert_store(ctx) : NULL;
2252 if ((x = PEM_read_bio_X509(bp, NULL, 0, NULL)) == NULL)
2255 add_ca_to_cert_store(st, x);
2256 SSL_add_client_CA(ssl, x);
2272 if (!(SSL_use_certificate_file(ssl,
2274 SSL_FILETYPE_ASN1))) {
2286 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
2289 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
2298 st = ctx ? SSL_CTX_get_cert_store(ctx) : NULL;
2300 add_ca_to_cert_store(st, x509);
2305 if (!check_pkcs11_engine()) {
2308 if (!install_engine_ca(pkcs11_engine, ssl,
2317 if (!defined_engine ||
2318 !install_engine_ca(defined_engine, ssl,
2339get_san_or_cn_from_cert(X509 *x509) {
2343 STACK_OF(GENERAL_NAME) *san_list;
2346 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
2348 int san_count = sk_GENERAL_NAME_num(san_list);
2350 for (n = 0; n < san_count; n++) {
2351 const GENERAL_NAME *name = sk_GENERAL_NAME_value(san_list, n);
2353 if (name && name->type == GEN_DNS) {
2354 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
2357 if (ASN1_STRING_length(name->d.dNSName) != (
int)strlen(dns_name))
2359 cn = OPENSSL_strdup(dns_name);
2360 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
2364 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
2367 X509_NAME_oneline(X509_get_subject_name(x509), buffer,
sizeof(buffer));
2370 n = (int)strlen(buffer) - 3;
2373 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
2374 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
2383 char *ecn = strchr(cn,
'/');
2385 return OPENSSL_strndup(cn, ecn-cn);
2387 return OPENSSL_strdup(cn);
2395tls_verify_call_back(
int preverify_ok, X509_STORE_CTX *ctx) {
2396 int index = SSL_get_ex_data_X509_STORE_CTX_idx();
2397 SSL *ssl = index >= 0 ? X509_STORE_CTX_get_ex_data(ctx, index) : NULL;
2399 coap_openssl_context_t *context = (session && session->
context) ?
2402 int depth = X509_STORE_CTX_get_error_depth(ctx);
2403 int err = X509_STORE_CTX_get_error(ctx);
2404 X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
2405 char *cn = x509 ? get_san_or_cn_from_cert(x509) : NULL;
2406 int keep_preverify_ok = preverify_ok;
2409 X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
2413 if (!preverify_ok) {
2415 case X509_V_ERR_CERT_NOT_YET_VALID:
2416 case X509_V_ERR_CERT_HAS_EXPIRED:
2420 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2424 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2428 case X509_V_ERR_UNABLE_TO_GET_CRL:
2432 case X509_V_ERR_CRL_NOT_YET_VALID:
2433 case X509_V_ERR_CRL_HAS_EXPIRED:
2437 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2438 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2439 case X509_V_ERR_AKID_SKID_MISMATCH:
2449 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
2450 X509_STORE_CTX_set_error(ctx, err);
2452 if (!preverify_ok) {
2453 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
2456 "Unknown CA", cn ? cn :
"?", depth);
2460 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
2465 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
2470 int length = i2d_X509(x509, NULL);
2472 uint8_t *base_buf2 = base_buf = length > 0 ? OPENSSL_malloc(length) : NULL;
2477 assert(i2d_X509(x509, &base_buf2) > 0);
2480 depth, preverify_ok,
2484 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
2486 X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
2490 OPENSSL_free(base_buf);
2492 X509_STORE_CTX_set_error(ctx, X509_V_ERR_UNSPECIFIED);
2497 return preverify_ok;
2500#if COAP_SERVER_SUPPORT
2501#if OPENSSL_VERSION_NUMBER < 0x10101000L
2511tls_secret_call_back(SSL *ssl,
2514 STACK_OF(SSL_CIPHER) *peer_ciphers,
2518 int psk_requested = 0;
2523 assert(session != NULL);
2524 assert(session->
context != NULL);
2525 if (session == NULL ||
2533 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2534 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2537 SSL_CIPHER_get_name(peer_cipher));
2538 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2544 if (!psk_requested) {
2551 SSL_VERIFY_CLIENT_ONCE |
2552 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2553 tls_verify_call_back);
2555 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2564 X509_VERIFY_PARAM *param;
2566 param = X509_VERIFY_PARAM_new();
2567 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2568 SSL_set1_param(ssl, param);
2569 X509_VERIFY_PARAM_free(param);
2591 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2592 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2607tls_server_name_call_back(SSL *ssl,
2613 return SSL_TLSEXT_ERR_NOACK;
2619 coap_openssl_context_t *context = (session && session->
context) ?
2621 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2625 return SSL_TLSEXT_ERR_NOACK;
2628 if (!sni || !sni[0]) {
2632 for (i = 0; i < context->sni_count; i++) {
2633 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2637 if (i == context->sni_count) {
2648 return SSL_TLSEXT_ERR_ALERT_FATAL;
2653 ctx = SSL_CTX_new(DTLS_method());
2656 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2657 SSL_CTX_set_app_data(ctx, &context->dtls);
2658 SSL_CTX_set_read_ahead(ctx, 1);
2659 coap_set_user_prefs(ctx);
2660 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2661 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2662 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2663 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2665#if !COAP_DISABLE_TCP
2668 ctx = SSL_CTX_new(TLS_method());
2671 SSL_CTX_set_app_data(ctx, &context->tls);
2672 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2673 coap_set_user_prefs(ctx);
2674 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2675 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2678 sni_setup_data = *setup_data;
2679 sni_setup_data.
pki_key = *new_entry;
2680 setup_pki_server(ctx, &sni_setup_data);
2682 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2683 (context->sni_count+1)*
sizeof(sni_entry));
2684 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2685 context->sni_entry_list[context->sni_count].ctx = ctx;
2686 context->sni_count++;
2688 SSL_set_SSL_CTX(ssl, context->sni_entry_list[i].ctx);
2689 SSL_clear_options(ssl, 0xFFFFFFFFL);
2690 SSL_set_options(ssl, SSL_CTX_get_options(context->sni_entry_list[i].ctx));
2697 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2698 return SSL_TLSEXT_ERR_OK;
2701 return SSL_TLSEXT_ERR_ALERT_WARNING;
2713psk_tls_server_name_call_back(SSL *ssl,
2720 return SSL_TLSEXT_ERR_NOACK;
2726 coap_openssl_context_t *o_context = (c_session && c_session->
context) ?
2728 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2733 return SSL_TLSEXT_ERR_ALERT_FATAL;
2735 if (!sni || !sni[0]) {
2738 for (i = 0; i < o_context->psk_sni_count; i++) {
2739 if (!strcasecmp(sni, (
char *)o_context->psk_sni_entry_list[i].sni)) {
2743 if (i == o_context->psk_sni_count) {
2752 return SSL_TLSEXT_ERR_ALERT_FATAL;
2757 ctx = SSL_CTX_new(DTLS_method());
2760 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2761 SSL_CTX_set_app_data(ctx, &o_context->dtls);
2762 SSL_CTX_set_read_ahead(ctx, 1);
2763 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2764 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2765 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2766 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2767 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2769#if !COAP_DISABLE_TCP
2772 ctx = SSL_CTX_new(TLS_method());
2775 SSL_CTX_set_app_data(ctx, &o_context->tls);
2776 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2777 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2778 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2779 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2783 o_context->psk_sni_entry_list =
2784 OPENSSL_realloc(o_context->psk_sni_entry_list,
2785 (o_context->psk_sni_count+1)*
sizeof(psk_sni_entry));
2786 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2787 OPENSSL_strdup(sni);
2788 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2790 o_context->psk_sni_entry_list[o_context->psk_sni_count].ctx =
2792 o_context->psk_sni_count++;
2794 SSL_set_SSL_CTX(ssl, o_context->psk_sni_entry_list[i].ctx);
2795 SSL_clear_options(ssl, 0xFFFFFFFFL);
2796 SSL_set_options(ssl,
2797 SSL_CTX_get_options(o_context->psk_sni_entry_list[i].ctx));
2799 &o_context->psk_sni_entry_list[i].psk_info.key);
2800 snprintf(lhint,
sizeof(lhint),
"%.*s",
2801 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2802 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2803 SSL_use_psk_identity_hint(ssl, lhint);
2810 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2811 return SSL_TLSEXT_ERR_OK;
2814 return SSL_TLSEXT_ERR_ALERT_WARNING;
2827tls_client_hello_call_back(SSL *ssl,
2832 coap_openssl_context_t *dtls_context;
2834 int psk_requested = 0;
2835 const unsigned char *out;
2839 *al = SSL_AD_INTERNAL_ERROR;
2840 return SSL_CLIENT_HELLO_ERROR;
2843 assert(session != NULL);
2844 assert(session->
context != NULL);
2846 if (session == NULL ||
2849 *al = SSL_AD_INTERNAL_ERROR;
2850 return SSL_CLIENT_HELLO_ERROR;
2853 setup_data = &dtls_context->setup_data;
2861 size_t len = SSL_client_hello_get0_ciphers(ssl, &out);
2862 STACK_OF(SSL_CIPHER) *peer_ciphers = NULL;
2863 STACK_OF(SSL_CIPHER) *scsvc = NULL;
2865 if (len && SSL_bytes_to_cipher_list(ssl, out, len,
2866 SSL_client_hello_isv2(ssl),
2867 &peer_ciphers, &scsvc)) {
2869 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2870 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2873 "Client cipher: %s (%04x)\n",
2874 SSL_CIPHER_get_name(peer_cipher),
2875 SSL_CIPHER_get_protocol_id(peer_cipher));
2876 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2882 sk_SSL_CIPHER_free(peer_ciphers);
2883 sk_SSL_CIPHER_free(scsvc);
2886 if (psk_requested) {
2892 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2898 return SSL_CLIENT_HELLO_SUCCESS;
2908 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_client_certificate_type,
2911 for (ii = 0; ii < outlen; ii++) {
2927 *al = SSL_AD_UNSUPPORTED_EXTENSION;
2928 return SSL_CLIENT_HELLO_ERROR;
2937 coap_openssl_context_t *context =
2939 const char *sni =
"";
2940 char *sni_tmp = NULL;
2943 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2945 (((out[0]<<8) + out[1] +2) == (
int)outlen) &&
2946 out[2] == TLSEXT_NAMETYPE_host_name &&
2947 (((out[3]<<8) + out[4] +2 +3) == (
int)outlen)) {
2951 sni_tmp = OPENSSL_malloc(outlen+1);
2952 sni_tmp[outlen] =
'\000';
2953 memcpy(sni_tmp, out, outlen);
2957 for (i = 0; i < context->sni_count; i++) {
2958 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2962 if (i == context->sni_count) {
2972 *al = SSL_AD_UNRECOGNIZED_NAME;
2973 return SSL_CLIENT_HELLO_ERROR;
2977 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2978 (context->sni_count+1)*
sizeof(sni_entry));
2979 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2980 context->sni_entry_list[context->sni_count].pki_key = *new_entry;
2981 context->sni_count++;
2984 OPENSSL_free(sni_tmp);
2986 sni_setup_data = *setup_data;
2987 sni_setup_data.
pki_key = context->sni_entry_list[i].pki_key;
2999 SSL_VERIFY_CLIENT_ONCE |
3000 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
3001 tls_verify_call_back);
3003 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
3012 X509_VERIFY_PARAM *param;
3014 param = X509_VERIFY_PARAM_new();
3015 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
3016 SSL_set1_param(ssl, param);
3017 X509_VERIFY_PARAM_free(param);
3024 return SSL_CLIENT_HELLO_SUCCESS;
3036psk_tls_client_hello_call_back(SSL *ssl,
3041 coap_openssl_context_t *o_context;
3043 const unsigned char *out;
3049 if (!c_session || !c_session->
context) {
3062 const char *sni =
"";
3063 char *sni_tmp = NULL;
3067 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
3069 (((out[0]<<8) + out[1] +2) == (
int)outlen) &&
3070 out[2] == TLSEXT_NAMETYPE_host_name &&
3071 (((out[3]<<8) + out[4] +2 +3) == (
int)outlen)) {
3075 sni_tmp = OPENSSL_malloc(outlen+1);
3077 sni_tmp[outlen] =
'\000';
3078 memcpy(sni_tmp, out, outlen);
3084 for (i = 0; i < o_context->psk_sni_count; i++) {
3085 if (strcasecmp(sni, o_context->psk_sni_entry_list[i].sni) == 0) {
3089 if (i == o_context->psk_sni_count) {
3093 psk_sni_entry *tmp_entry;
3102 *al = SSL_AD_UNRECOGNIZED_NAME;
3103 return SSL_CLIENT_HELLO_ERROR;
3107 OPENSSL_realloc(o_context->psk_sni_entry_list,
3108 (o_context->psk_sni_count+1)*
sizeof(sni_entry));
3110 o_context->psk_sni_entry_list = tmp_entry;
3111 o_context->psk_sni_entry_list[o_context->psk_sni_count]
3113 OPENSSL_strdup(sni);
3114 if (o_context->psk_sni_entry_list[o_context->psk_sni_count].sni) {
3115 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
3117 o_context->psk_sni_count++;
3122 OPENSSL_free(sni_tmp);
3125 &o_context->psk_sni_entry_list[i].psk_info.hint)
3130 &o_context->psk_sni_entry_list[i].psk_info.key)
3134 if (o_context->psk_sni_entry_list[i].psk_info.hint.s) {
3135 snprintf(lhint,
sizeof(lhint),
"%.*s",
3136 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
3137 o_context->psk_sni_entry_list[i].psk_info.hint.s);
3138 SSL_use_psk_identity_hint(ssl, lhint);
3141 return SSL_CLIENT_HELLO_SUCCESS;
3144 *al = SSL_AD_INTERNAL_ERROR;
3145 return SSL_CLIENT_HELLO_ERROR;
3154 coap_openssl_context_t *context =
3159 context->setup_data = *setup_data;
3165 if (!defined_engine) {
3166 coap_log_warn(
"setup_pki: OpenSSL Engine not configured, PKI not set up\n");
3172 if (!context->setup_data.verify_peer_cert) {
3174 context->setup_data.check_common_ca = 0;
3176 context->setup_data.allow_self_signed = 1;
3177 context->setup_data.allow_expired_certs = 1;
3178 context->setup_data.cert_chain_validation = 1;
3179 context->setup_data.cert_chain_verify_depth = 10;
3180 context->setup_data.check_cert_revocation = 1;
3181 context->setup_data.allow_no_crl = 1;
3182 context->setup_data.allow_expired_crl = 1;
3183 context->setup_data.allow_bad_md_hash = 1;
3184 context->setup_data.allow_short_rsa_length = 1;
3186#if COAP_SERVER_SUPPORT
3188 if (context->dtls.ctx) {
3190#if OPENSSL_VERSION_NUMBER < 0x10101000L
3191 if (!setup_pki_server(context->dtls.ctx, setup_data))
3200#if OPENSSL_VERSION_NUMBER < 0x10101000L
3201 if (SSLeay() >= 0x10101000L) {
3202 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
3203 "no certificate checking\n",
3204 OPENSSL_VERSION_NUMBER, SSLeay());
3206 SSL_CTX_set_tlsext_servername_arg(context->dtls.ctx, &context->setup_data);
3207 SSL_CTX_set_tlsext_servername_callback(context->dtls.ctx,
3208 tls_server_name_call_back);
3210 SSL_CTX_set_client_hello_cb(context->dtls.ctx,
3211 tls_client_hello_call_back,
3215#if !COAP_DISABLE_TCP
3216 if (context->tls.ctx) {
3218#if OPENSSL_VERSION_NUMBER < 0x10101000L
3219 if (!setup_pki_server(context->tls.ctx, setup_data))
3228#if OPENSSL_VERSION_NUMBER < 0x10101000L
3229 if (SSLeay() >= 0x10101000L) {
3230 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
3231 "no certificate checking\n",
3232 OPENSSL_VERSION_NUMBER, SSLeay());
3234 SSL_CTX_set_tlsext_servername_arg(context->tls.ctx, &context->setup_data);
3235 SSL_CTX_set_tlsext_servername_callback(context->tls.ctx,
3236 tls_server_name_call_back);
3238 SSL_CTX_set_client_hello_cb(context->tls.ctx,
3239 tls_client_hello_call_back,
3243 SSL_CTX_set_alpn_select_cb(context->tls.ctx, server_alpn_callback, NULL);
3251 if (!context->dtls.ssl) {
3253 context->dtls.ssl = SSL_new(context->dtls.ctx);
3254 if (!context->dtls.ssl)
3256 bio = BIO_new(context->dtls.meth);
3258 SSL_free(context->dtls.ssl);
3259 context->dtls.ssl = NULL;
3262 SSL_set_bio(context->dtls.ssl, bio, bio);
3263 SSL_set_app_data(context->dtls.ssl, NULL);
3264 SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
3267 context->psk_pki_enabled |= IS_PKI;
3276 const char *ca_file,
3279 coap_openssl_context_t *context =
3281 if (context->dtls.ctx) {
3282 if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) {
3284 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
3288#if !COAP_DISABLE_TCP
3289 if (context->tls.ctx) {
3290 if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) {
3292 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
3302 coap_openssl_context_t *context =
3304 return context->psk_pki_enabled ? 1 : 0;
3311 coap_openssl_context_t *context = (coap_openssl_context_t *)handle;
3313 if (context->dtls.ssl)
3314 SSL_free(context->dtls.ssl);
3315 if (context->dtls.ctx)
3316 SSL_CTX_free(context->dtls.ctx);
3317 if (context->dtls.cookie_hmac)
3318 HMAC_CTX_free(context->dtls.cookie_hmac);
3319 if (context->dtls.meth)
3320 BIO_meth_free(context->dtls.meth);
3321 if (context->dtls.bio_addr)
3322 BIO_ADDR_free(context->dtls.bio_addr);
3323#if !COAP_DISABLE_TCP
3324 if (context->tls.ctx)
3325 SSL_CTX_free(context->tls.ctx);
3326 if (context->tls.meth)
3327 BIO_meth_free(context->tls.meth);
3329 for (i = 0; i < context->sni_count; i++) {
3330 OPENSSL_free(context->sni_entry_list[i].sni);
3331#if OPENSSL_VERSION_NUMBER < 0x10101000L
3332 SSL_CTX_free(context->sni_entry_list[i].ctx);
3335 if (context->sni_count)
3336 OPENSSL_free(context->sni_entry_list);
3337 for (i = 0; i < context->psk_sni_count; i++) {
3338 OPENSSL_free((
char *)context->psk_sni_entry_list[i].sni);
3339#if OPENSSL_VERSION_NUMBER < 0x10101000L
3340 SSL_CTX_free(context->psk_sni_entry_list[i].ctx);
3343 if (context->psk_sni_count)
3344 OPENSSL_free(context->psk_sni_entry_list);
3348#if COAP_SERVER_SUPPORT
3352 SSL *nssl = NULL, *ssl = NULL;
3353 coap_ssl_data *data;
3354 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3359 nssl = SSL_new(dtls->ctx);
3362 nbio = BIO_new(dtls->meth);
3365 SSL_set_bio(nssl, nbio, nbio);
3366 SSL_set_app_data(nssl, NULL);
3367 SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE);
3368 SSL_set_mtu(nssl, (
long)session->
mtu);
3372 SSL_set_app_data(ssl, session);
3374 rbio = SSL_get_rbio(ssl);
3375 data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3378 data->session = session;
3382 if (psk_hint != NULL && psk_hint->
length) {
3383 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
3386 memcpy(hint, psk_hint->
s, psk_hint->
length);
3387 hint[psk_hint->
length] =
'\000';
3388 SSL_use_psk_identity_hint(ssl, hint);
3395 r = SSL_accept(ssl);
3397 int err = SSL_get_error(ssl, r);
3398 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3416#if COAP_CLIENT_SUPPORT
3420 coap_openssl_context_t *context =
3423 if (context->psk_pki_enabled & IS_PSK) {
3428 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
3432 SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
3433#if COAP_SERVER_SUPPORT
3434 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
3436 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
3439 SSL_set_max_proto_version(ssl, DTLS1_2_VERSION);
3441#if !COAP_DISABLE_TCP
3443 SSL_set_max_proto_version(ssl, TLS1_2_VERSION);
3446 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
3449 if (context->psk_pki_enabled & IS_PKI) {
3454#if !COAP_DISABLE_TCP
3456 SSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
3461 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
3467 X509_VERIFY_PARAM *param;
3469 param = X509_VERIFY_PARAM_new();
3470 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
3471 SSL_set1_param(ssl, param);
3472 X509_VERIFY_PARAM_free(param);
3479 SSL_VERIFY_CLIENT_ONCE |
3480 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
3481 tls_verify_call_back);
3483 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
3490#if COAP_DTLS_RETRANSMIT_MS != 1000
3491#if OPENSSL_VERSION_NUMBER >= 0x10101000L
3493 DTLS_set_timer_cb(ssl, timer_cb);
3504 coap_ssl_data *data;
3506 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3507 coap_dtls_context_t *dtls = &context->dtls;
3509 ssl = SSL_new(dtls->ctx);
3512 bio = BIO_new(dtls->meth);
3515 data = (coap_ssl_data *)BIO_get_data(bio);
3518 data->session = session;
3519 SSL_set_bio(ssl, bio, bio);
3520 SSL_set_app_data(ssl, session);
3521 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
3522 SSL_set_mtu(ssl, (
long)session->
mtu);
3524 if (!setup_client_ssl_session(session, ssl))
3529 r = SSL_connect(ssl);
3531 int ret = SSL_get_error(ssl, r);
3532 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3550 SSL *ssl = (SSL *)session->
tls;
3552 SSL_set_mtu(ssl, (
long)session->
mtu);
3558 SSL *ssl = (SSL *)session->
tls;
3560 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3561 int r = SSL_shutdown(ssl);
3566 session->
tls = NULL;
3574 const uint8_t *data,
size_t data_len) {
3576 SSL *ssl = (SSL *)session->
tls;
3578 assert(ssl != NULL);
3581 r = SSL_write(ssl, data, (
int)data_len);
3584 int err = SSL_get_error(ssl, r);
3585 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3589 if (err == SSL_ERROR_ZERO_RETURN)
3591 else if (err == SSL_ERROR_SSL)
3609 if (r == (ssize_t)data_len)
3632 SSL *ssl = (SSL *)session->
tls;
3633 coap_ssl_data *ssl_data;
3637 rbio = ssl ? SSL_get_rbio(ssl) : NULL;
3638 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3639 return ssl_data ? ssl_data->timeout : 1000;
3648 SSL *ssl = (SSL *)session->
tls;
3652 (DTLSv1_handle_timeout(ssl) < 0)) {
3660#if COAP_SERVER_SUPPORT
3663 const uint8_t *data,
size_t data_len) {
3664 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3665 coap_ssl_data *ssl_data;
3669 SSL_set_mtu(dtls->ssl, (
long)session->
mtu);
3670 rbio = dtls->ssl ? SSL_get_rbio(dtls->ssl) : NULL;
3671 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3672 assert(ssl_data != NULL);
3677 if (ssl_data->pdu_len) {
3678 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3681 ssl_data->session = session;
3682 ssl_data->pdu = data;
3683 ssl_data->pdu_len = (unsigned)data_len;
3684 r = DTLSv1_listen(dtls->ssl, dtls->bio_addr);
3686 int err = SSL_get_error(dtls->ssl, r);
3687 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3707 coap_ssl_data *ssl_data;
3708 SSL *ssl = (SSL *)session->
tls;
3712 assert(ssl != NULL);
3714 int in_init = SSL_in_init(ssl);
3716 rbio = ssl ? SSL_get_rbio(ssl) : NULL;
3717 ssl_data = rbio ? (coap_ssl_data *)BIO_get_data(rbio) : NULL;
3718 assert(ssl_data != NULL);
3724 if (ssl_data->pdu_len) {
3725 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3728 ssl_data->pdu = data;
3729 ssl_data->pdu_len = (unsigned)data_len;
3732 r = SSL_read(ssl, pdu, (
int)
sizeof(pdu));
3737 int err = SSL_get_error(ssl, r);
3738 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3739 if (in_init && SSL_is_init_finished(ssl)) {
3747 if (err == SSL_ERROR_ZERO_RETURN)
3749 else if (err == SSL_ERROR_SSL)
3767 if (ssl_data && ssl_data->pdu_len) {
3769 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
3770 ssl_data->pdu_len = 0;
3771 ssl_data->pdu = NULL;
3782 unsigned int overhead = 37;
3783 const SSL_CIPHER *s_ciph = NULL;
3784 if (session->
tls != NULL)
3785 s_ciph = SSL_get_current_cipher(session->
tls);
3787 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
3789 const EVP_CIPHER *e_ciph;
3793 e_ciph = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(s_ciph));
3795 switch (EVP_CIPHER_mode(e_ciph)) {
3796 case EVP_CIPH_GCM_MODE:
3797 ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
3798 maclen = EVP_GCM_TLS_TAG_LEN;
3801 case EVP_CIPH_CCM_MODE:
3802 ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
3803 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3804 if (strstr(cipher,
"CCM8"))
3810 case EVP_CIPH_CBC_MODE:
3811 e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
3812 blocksize = EVP_CIPHER_block_size(e_ciph);
3813 ivlen = EVP_CIPHER_iv_length(e_ciph);
3815 maclen = EVP_MD_size(e_md);
3818 case EVP_CIPH_STREAM_CIPHER:
3825 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3832 overhead = DTLS1_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 + pad;
3837#if !COAP_DISABLE_TCP
3838#if COAP_CLIENT_SUPPORT
3844 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3845 coap_tls_context_t *tls = &context->tls;
3847 ssl = SSL_new(tls->ctx);
3850 bio = BIO_new(tls->meth);
3853 BIO_set_data(bio, session);
3854 SSL_set_bio(ssl, bio, bio);
3855 SSL_set_app_data(ssl, session);
3857 if (!setup_client_ssl_session(session, ssl))
3860 r = SSL_connect(ssl);
3862 int ret = SSL_get_error(ssl, r);
3863 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3865 if (ret == SSL_ERROR_WANT_READ)
3867 if (ret == SSL_ERROR_WANT_WRITE) {
3869#ifdef COAP_EPOLL_SUPPORT
3883 if (SSL_is_init_finished(ssl)) {
3897#if COAP_SERVER_SUPPORT
3902 coap_tls_context_t *tls = &((coap_openssl_context_t *)session->
context->
dtls_context)->tls;
3906 ssl = SSL_new(tls->ctx);
3909 bio = BIO_new(tls->meth);
3912 BIO_set_data(bio, session);
3913 SSL_set_bio(ssl, bio, bio);
3914 SSL_set_app_data(ssl, session);
3917 if (psk_hint != NULL && psk_hint->
length) {
3918 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
3921 memcpy(hint, psk_hint->
s, psk_hint->
length);
3922 hint[psk_hint->
length] =
'\000';
3923 SSL_use_psk_identity_hint(ssl, hint);
3930 r = SSL_accept(ssl);
3932 int err = SSL_get_error(ssl, r);
3933 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3935 if (err == SSL_ERROR_WANT_READ)
3937 if (err == SSL_ERROR_WANT_WRITE) {
3939#ifdef COAP_EPOLL_SUPPORT
3953 if (SSL_is_init_finished(ssl)) {
3958#if COAP_DTLS_RETRANSMIT_MS != 1000
3959#if OPENSSL_VERSION_NUMBER >= 0x10101000L
3961 DTLS_set_timer_cb(ssl, timer_cb);
3977 SSL *ssl = (SSL *)session->
tls;
3979 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3980 int r = SSL_shutdown(ssl);
3985 session->
tls = NULL;
3998 SSL *ssl = (SSL *)session->
tls;
4004 in_init = !SSL_is_init_finished(ssl);
4006 r = SSL_write(ssl, data, (
int)data_len);
4009 int err = SSL_get_error(ssl, r);
4010 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
4011 if (in_init && SSL_is_init_finished(ssl)) {
4017 if (err == SSL_ERROR_WANT_READ)
4019 else if (err == SSL_ERROR_WANT_WRITE) {
4021#ifdef COAP_EPOLL_SUPPORT
4033 if (err == SSL_ERROR_ZERO_RETURN)
4035 else if (err == SSL_ERROR_SSL)
4039 }
else if (in_init && SSL_is_init_finished(ssl)) {
4058 if (r == (ssize_t)data_len)
4075 SSL *ssl = (SSL *)session->
tls;
4083 in_init = !SSL_is_init_finished(ssl);
4085 r = SSL_read(ssl, data, (
int)data_len);
4087 int err = SSL_get_error(ssl, r);
4088 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
4089 if (in_init && SSL_is_init_finished(ssl)) {
4095 if (err == SSL_ERROR_WANT_READ)
4097 if (err == SSL_ERROR_WANT_WRITE) {
4099#ifdef COAP_EPOLL_SUPPORT
4109 if (err == SSL_ERROR_ZERO_RETURN)
4111 else if (err == SSL_ERROR_SSL)
4115 }
else if (in_init && SSL_is_init_finished(ssl)) {
4141#if COAP_SERVER_SUPPORT
4144 EVP_MD_CTX *digest_ctx = EVP_MD_CTX_new();
4147 EVP_DigestInit_ex(digest_ctx, EVP_sha256(), NULL);
4154 EVP_MD_CTX_free(digest_ctx);
4159 const uint8_t *data,
4161 return EVP_DigestUpdate(digest_ctx, data, data_len);
4168 int ret = EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
4175#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
4177coap_crypto_output_errors(
const char *prefix) {
4178#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
4183 while ((e = ERR_get_error()))
4186 ERR_reason_error_string(e),
4187 ssl_function_definition(e));
4197static struct hash_algs {
4199 const EVP_MD *(*get_hash)(void);
4208static const EVP_MD *
4209get_hash_alg(
cose_alg_t alg,
size_t *length) {
4212 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
4213 if (hashs[idx].alg == alg) {
4214 *length = hashs[idx].length;
4215 return hashs[idx].get_hash();
4218 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
4226 unsigned int length;
4227 const EVP_MD *evp_md;
4228 EVP_MD_CTX *evp_ctx = NULL;
4232 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
4233 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
4236 evp_ctx = EVP_MD_CTX_new();
4237 if (evp_ctx == NULL)
4239 if (EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
4242 if (EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
4248 if (EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
4250 dummy->length = length;
4251 if (hash_length < dummy->length)
4252 dummy->length = hash_length;
4254 EVP_MD_CTX_free(evp_ctx);
4258 coap_crypto_output_errors(
"coap_crypto_hash");
4261 EVP_MD_CTX_free(evp_ctx);
4266#if COAP_OSCORE_SUPPORT
4272#include <openssl/evp.h>
4273#include <openssl/hmac.h>
4280static struct cipher_algs {
4282 const EVP_CIPHER *(*get_cipher)(void);
4287static const EVP_CIPHER *
4291 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
4292 if (ciphers[idx].alg == alg)
4293 return ciphers[idx].get_cipher();
4295 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
4304static struct hmac_algs {
4306 const EVP_MD *(*get_hmac)(void);
4313static const EVP_MD *
4317 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
4318 if (hmacs[idx].hmac_alg == hmac_alg)
4319 return hmacs[idx].get_hmac();
4321 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
4327 return get_cipher_alg(alg) != NULL;
4336 return get_hmac_alg(hmac_alg) != NULL;
4340 if (1 != (Func)) { \
4349 size_t *max_result_len) {
4350 const EVP_CIPHER *cipher;
4353 int result_len = (int)(*max_result_len & INT_MAX);
4358 assert(params != NULL);
4359 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
4366 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
4369 C(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL));
4370 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
4371 C(EVP_CIPHER_CTX_ctrl(ctx,
4372 EVP_CTRL_AEAD_SET_IVLEN,
4375 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, NULL));
4376 C(EVP_EncryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
4379 C(EVP_EncryptUpdate(ctx, NULL, &result_len, NULL, (
int)data->
length));
4380 if (aad && aad->
s && (aad->
length > 0)) {
4381 C(EVP_EncryptUpdate(ctx, NULL, &result_len, aad->
s, (
int)aad->
length));
4383 C(EVP_EncryptUpdate(ctx, result, &result_len, data->
s, (
int)data->
length));
4386 C(EVP_EncryptFinal_ex(ctx, result + result_len, &tmp));
4390 C(EVP_CIPHER_CTX_ctrl(ctx,
4391 EVP_CTRL_CCM_GET_TAG,
4393 result + result_len));
4395 *max_result_len = result_len + ccm->
tag_len;
4396 EVP_CIPHER_CTX_free(ctx);
4400 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
4409 size_t *max_result_len) {
4410 const EVP_CIPHER *cipher;
4420 assert(params != NULL);
4421 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
4433 memcpy(&rwtag, &tag,
sizeof(rwtag));
4436 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
4438 C(EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL));
4439 C(EVP_CIPHER_CTX_ctrl(ctx,
4440 EVP_CTRL_AEAD_SET_IVLEN,
4443 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, rwtag));
4444 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
4446 C(EVP_DecryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
4448 C(EVP_DecryptUpdate(ctx, NULL, &len, NULL, (
int)data->
length));
4449 if (aad && aad->
s && (aad->
length > 0)) {
4450 C(EVP_DecryptUpdate(ctx, NULL, &len, aad->
s, (
int)aad->
length));
4452 tmp = EVP_DecryptUpdate(ctx, result, &len, data->
s, (
int)data->
length);
4453 EVP_CIPHER_CTX_free(ctx);
4455 *max_result_len = 0;
4458 *max_result_len = len;
4462 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
4471 unsigned int result_len;
4472 const EVP_MD *evp_md;
4479 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
4480 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
4486 result_len = (
unsigned int)
dummy->length;
4494 dummy->length = result_len;
4499 coap_crypto_output_errors(
"coap_crypto_hmac");
4511#pragma GCC diagnostic ignored "-Wunused-function"
struct coap_session_t coap_session_t
#define COAP_SERVER_SUPPORT
#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_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
Triggered 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::@344247312334342161152274343216257004206261032141 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
coap_pki_key_t key_type
key format type
union coap_dtls_key_t::@056010174050062120053044334363130312151232015326 key
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 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