20#ifdef COAP_WITH_LIBOPENSSL
66#include <openssl/ssl.h>
67#include <openssl/engine.h>
68#include <openssl/err.h>
69#include <openssl/rand.h>
70#include <openssl/hmac.h>
71#include <openssl/x509v3.h>
73#if OPENSSL_VERSION_NUMBER >= 0x30000000L
76#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
80#ifdef COAP_EPOLL_SUPPORT
81# include <sys/epoll.h>
84#if OPENSSL_VERSION_NUMBER < 0x10100000L
85#error Must be compiled against OpenSSL 1.1.0 or later
89#define strcasecmp _stricmp
90#define strncasecmp _strnicmp
94#ifndef TLSEXT_TYPE_client_certificate_type
95#define TLSEXT_TYPE_client_certificate_type 19
97#ifndef TLSEXT_TYPE_server_certificate_type
98#define TLSEXT_TYPE_server_certificate_type 20
101#ifndef COAP_OPENSSL_CIPHERS
102#if OPENSSL_VERSION_NUMBER >= 0x10101000L
103#define COAP_OPENSSL_CIPHERS "TLSv1.3:TLSv1.2:!NULL"
105#define COAP_OPENSSL_CIPHERS "TLSv1.2:!NULL"
109#ifndef COAP_OPENSSL_PSK_CIPHERS
110#define COAP_OPENSSL_PSK_CIPHERS "PSK:!NULL"
114typedef struct coap_dtls_context_t {
117 HMAC_CTX *cookie_hmac;
120} coap_dtls_context_t;
122typedef struct coap_tls_context_t {
130typedef struct sni_entry {
132#if OPENSSL_VERSION_NUMBER < 0x10101000L
139typedef struct psk_sni_entry {
141#if OPENSSL_VERSION_NUMBER < 0x10101000L
147typedef struct coap_openssl_context_t {
148 coap_dtls_context_t dtls;
150 coap_tls_context_t tls;
155 sni_entry *sni_entry_list;
156 size_t psk_sni_count;
157 psk_sni_entry *psk_sni_entry_list;
158} coap_openssl_context_t;
160#if COAP_SERVER_SUPPORT
161#if OPENSSL_VERSION_NUMBER < 0x10101000L
162static int psk_tls_server_name_call_back(SSL *ssl,
int *sd,
void *arg);
164static int psk_tls_client_hello_call_back(SSL *ssl,
int *al,
void *arg);
170 if (SSLeay() < 0x10100000L) {
171 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
174#if OPENSSL_VERSION_NUMBER >= 0x10101000L
182 if (SSLeay() < 0x10101000L) {
183 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
193 if (SSLeay() < 0x10100000L) {
194 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
197#if OPENSSL_VERSION_NUMBER >= 0x10101000L
198 if (SSLeay() < 0x10101000L) {
199 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
254static ENGINE *ssl_engine = NULL;
258 SSL_load_error_strings();
260 ENGINE_load_dynamic();
267 ENGINE_finish(ssl_engine);
269 ENGINE_free(ssl_engine);
281 return c_session->
tls;
301typedef struct coap_ssl_st {
310coap_dgram_create(BIO *a) {
311 coap_ssl_data *data = NULL;
312 data = malloc(
sizeof(coap_ssl_data));
316 BIO_set_data(a, data);
317 memset(data, 0x00,
sizeof(coap_ssl_data));
322coap_dgram_destroy(BIO *a) {
326 data = (coap_ssl_data *)BIO_get_data(a);
333coap_dgram_read(BIO *a,
char *out,
int outl) {
335 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
338 if (data != NULL && data->pdu_len > 0) {
339 if (outl < (
int)data->pdu_len) {
340 memcpy(out, data->pdu, outl);
343 memcpy(out, data->pdu, data->pdu_len);
344 ret = (int)data->pdu_len;
346 if (!data->peekmode) {
353 BIO_clear_retry_flags(a);
355 BIO_set_retry_read(a);
361coap_dgram_write(BIO *a,
const char *in,
int inl) {
363 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
367#if COAP_SERVER_SUPPORT
368 && data->session->endpoint == NULL
372 BIO_clear_retry_flags(a);
376 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
379 BIO_clear_retry_flags(a);
381 BIO_set_retry_write(a);
383 BIO_clear_retry_flags(a);
390coap_dgram_puts(BIO *a,
const char *pstr) {
391 return coap_dgram_write(a, pstr, (
int)strlen(pstr));
395coap_dgram_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
397 coap_ssl_data *data = BIO_get_data(a);
402 case BIO_CTRL_GET_CLOSE:
403 ret = BIO_get_shutdown(a);
405 case BIO_CTRL_SET_CLOSE:
406 BIO_set_shutdown(a, (
int)num);
409 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
410 data->peekmode = (unsigned)num;
412 case BIO_CTRL_DGRAM_CONNECT:
415 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
416 case BIO_CTRL_DGRAM_GET_MTU:
417 case BIO_CTRL_DGRAM_SET_MTU:
418 case BIO_CTRL_DGRAM_QUERY_MTU:
419 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
424 case BIO_CTRL_DGRAM_MTU_DISCOVER:
425 case BIO_CTRL_DGRAM_SET_CONNECTED:
428 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
430 struct timeval *)ptr)->tv_usec);
434 case BIO_C_FILE_SEEK:
435 case BIO_C_FILE_TELL:
437 case BIO_CTRL_PENDING:
438 case BIO_CTRL_WPENDING:
439 case BIO_CTRL_DGRAM_GET_PEER:
440 case BIO_CTRL_DGRAM_SET_PEER:
441 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
442 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
443 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
444 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
445 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
446 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
447 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
448 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
457coap_dtls_generate_cookie(SSL *ssl,
458 unsigned char *cookie,
459 unsigned int *cookie_len) {
460 coap_dtls_context_t *dtls =
461 (coap_dtls_context_t *)SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl));
462 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
463 int r = HMAC_Init_ex(dtls->cookie_hmac, NULL, 0, NULL, NULL);
464 r &= HMAC_Update(dtls->cookie_hmac,
465 (
const uint8_t *)&data->session->addr_info.local.addr,
466 (
size_t)data->session->addr_info.local.size);
467 r &= HMAC_Update(dtls->cookie_hmac,
468 (
const uint8_t *)&data->session->addr_info.remote.addr,
469 (
size_t)data->session->addr_info.remote.size);
470 r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
475coap_dtls_verify_cookie(SSL *ssl,
476 const uint8_t *cookie,
477 unsigned int cookie_len) {
480 if (coap_dtls_generate_cookie(ssl, hmac, &len) &&
481 cookie_len == len && memcmp(cookie, hmac, len) == 0)
487#if COAP_CLIENT_SUPPORT
489coap_dtls_psk_client_callback(SSL *ssl,
492 unsigned int max_identity_len,
494 unsigned int max_psk_len) {
496 coap_openssl_context_t *o_context;
504 if (c_session == NULL)
507 if (o_context == NULL)
511 temp.
s = hint ? (
const uint8_t *)hint : (
const uint8_t *)
"";
512 temp.
length = strlen((
const char *)temp.
s);
516 (
const char *)temp.
s);
528 if (cpsk_info == NULL)
533 psk_identity = &cpsk_info->
identity;
534 psk_key = &cpsk_info->
key;
540 if (psk_identity == NULL || psk_key == NULL) {
546 if (!max_identity_len)
549 if (psk_identity->
length > max_identity_len) {
550 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
554 max_identity_len = (
unsigned int)psk_identity->
length;
556 memcpy(identity, psk_identity->
s, max_identity_len);
557 identity[max_identity_len] =
'\000';
559 if (psk_key->
length > max_psk_len) {
564 max_psk_len = (
unsigned int)psk_key->
length;
566 memcpy(psk, psk_key->
s, max_psk_len);
571#if COAP_SERVER_SUPPORT
573coap_dtls_psk_server_callback(
575 const char *identity,
577 unsigned int max_psk_len
585 if (c_session == NULL)
591 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
592 lidentity.
length = strlen((
const char *)lidentity.
s);
596 (
int)lidentity.
length, (
const char *)lidentity.
s);
611 if (psk_key->
length > max_psk_len) {
616 max_psk_len = (
unsigned int)psk_key->
length;
618 memcpy(psk, psk_key->
s, max_psk_len);
624ssl_function_definition(
unsigned long e) {
625#if OPENSSL_VERSION_NUMBER >= 0x30000000L
629 static char buff[80];
631 snprintf(buff,
sizeof(buff),
" at %s:%s",
632 ERR_lib_error_string(e), ERR_func_error_string(e));
638coap_dtls_info_callback(
const SSL *ssl,
int where,
int ret) {
641 int w = where &~SSL_ST_MASK;
643 if (w & SSL_ST_CONNECT)
644 pstr =
"SSL_connect";
645 else if (w & SSL_ST_ACCEPT)
650 if (where & SSL_CB_LOOP) {
653 }
else if (where & SSL_CB_ALERT) {
655 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
656 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) {
658 if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY)
662 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
665 SSL_alert_type_string_long(ret),
666 SSL_alert_desc_string_long(ret));
667 }
else if (where & SSL_CB_EXIT) {
673 while ((e = ERR_get_error()))
676 ssl_function_definition(e));
678 }
else if (ret < 0) {
680 int err = SSL_get_error(ssl, ret);
681 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE &&
682 err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT &&
683 err != SSL_ERROR_WANT_X509_LOOKUP) {
687 while ((e = ERR_get_error()))
690 ssl_function_definition(e));
696 if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
702coap_sock_create(BIO *a) {
708coap_sock_destroy(BIO *a) {
720coap_sock_read(BIO *a,
char *out,
int outl) {
729 BIO_set_retry_read(a);
732 BIO_clear_retry_flags(a);
745coap_sock_write(BIO *a,
const char *in,
int inl) {
753 BIO_clear_retry_flags(a);
755 BIO_set_retry_read(a);
758 BIO_clear_retry_flags(a);
762 (errno == EPIPE || errno == ECONNRESET)) {
782coap_sock_puts(BIO *a,
const char *pstr) {
783 return coap_sock_write(a, pstr, (
int)strlen(pstr));
787coap_sock_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
798 case BIO_CTRL_SET_CLOSE:
804 case BIO_CTRL_GET_CLOSE:
813coap_set_user_prefs(SSL_CTX *ctx) {
814 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
816#ifdef COAP_OPENSSL_SIGALGS
817 SSL_CTX_set1_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
818 SSL_CTX_set1_client_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
821#if OPENSSL_VERSION_NUMBER >= 0x10101000L && defined(COAP_OPENSSL_GROUPS)
822 SSL_CTX_set1_groups_list(ctx, COAP_OPENSSL_GROUPS);
828 coap_openssl_context_t *context;
833 uint8_t cookie_secret[32];
835 memset(context, 0,
sizeof(coap_openssl_context_t));
838 context->dtls.ctx = SSL_CTX_new(DTLS_method());
839 if (!context->dtls.ctx)
841 SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
842 SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
843 SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
844 coap_set_user_prefs(context->dtls.ctx);
845 memset(cookie_secret, 0,
sizeof(cookie_secret));
846 if (!RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
848 "Insufficient entropy for random cookie generation");
849 coap_prng(cookie_secret,
sizeof(cookie_secret));
851 context->dtls.cookie_hmac = HMAC_CTX_new();
852 if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
855 SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
856 SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
857 SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
858 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
859#if OPENSSL_VERSION_NUMBER >= 0x30000000L
860 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_LEGACY_SERVER_CONNECT);
862 context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM,
"coapdgram");
863 if (!context->dtls.meth)
865 context->dtls.bio_addr = BIO_ADDR_new();
866 if (!context->dtls.bio_addr)
868 BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
869 BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
870 BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
871 BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
872 BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
873 BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
877 context->tls.ctx = SSL_CTX_new(TLS_method());
878 if (!context->tls.ctx)
880 SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
881 SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
882 coap_set_user_prefs(context->tls.ctx);
883 SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
884 context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET,
"coapsock");
885 if (!context->tls.meth)
887 BIO_meth_set_write(context->tls.meth, coap_sock_write);
888 BIO_meth_set_read(context->tls.meth, coap_sock_read);
889 BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
890 BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
891 BIO_meth_set_create(context->tls.meth, coap_sock_create);
892 BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
903#if COAP_SERVER_SUPPORT
908 coap_openssl_context_t *o_context =
912 if (!setup_data || !o_context)
915 SSL_CTX_set_psk_server_callback(o_context->dtls.ctx,
916 coap_dtls_psk_server_callback);
918 SSL_CTX_set_psk_server_callback(o_context->tls.ctx,
919 coap_dtls_psk_server_callback);
925 SSL_CTX_use_psk_identity_hint(o_context->dtls.ctx, hint);
927 SSL_CTX_use_psk_identity_hint(o_context->tls.ctx, hint);
931#if OPENSSL_VERSION_NUMBER < 0x10101000L
932 SSL_CTX_set_tlsext_servername_arg(o_context->dtls.ctx,
934 SSL_CTX_set_tlsext_servername_callback(o_context->dtls.ctx,
935 psk_tls_server_name_call_back);
937 SSL_CTX_set_tlsext_servername_arg(o_context->tls.ctx,
939 SSL_CTX_set_tlsext_servername_callback(o_context->tls.ctx,
940 psk_tls_server_name_call_back);
943 SSL_CTX_set_client_hello_cb(o_context->dtls.ctx,
944 psk_tls_client_hello_call_back,
947 SSL_CTX_set_client_hello_cb(o_context->tls.ctx,
948 psk_tls_client_hello_call_back,
954 if (!o_context->dtls.ssl) {
956 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
957 if (!o_context->dtls.ssl)
959 bio = BIO_new(o_context->dtls.meth);
961 SSL_free(o_context->dtls.ssl);
962 o_context->dtls.ssl = NULL;
965 SSL_set_bio(o_context->dtls.ssl, bio, bio);
966 SSL_set_app_data(o_context->dtls.ssl, NULL);
967 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
970 o_context->psk_pki_enabled |= IS_PSK;
975#if COAP_CLIENT_SUPPORT
980 coap_openssl_context_t *o_context =
984 if (!setup_data || !o_context)
987 if (!o_context->dtls.ssl) {
989 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
990 if (!o_context->dtls.ssl)
992 bio = BIO_new(o_context->dtls.meth);
994 SSL_free(o_context->dtls.ssl);
995 o_context->dtls.ssl = NULL;
998 SSL_set_bio(o_context->dtls.ssl, bio, bio);
999 SSL_set_app_data(o_context->dtls.ssl, NULL);
1000 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1003 o_context->psk_pki_enabled |= IS_PSK;
1009map_key_type(
int asn1_private_key_type
1011 switch (asn1_private_key_type) {
1013 return EVP_PKEY_NONE;
1015 return EVP_PKEY_RSA;
1017 return EVP_PKEY_RSA2;
1019 return EVP_PKEY_DSA;
1021 return EVP_PKEY_DSA1;
1023 return EVP_PKEY_DSA2;
1025 return EVP_PKEY_DSA3;
1027 return EVP_PKEY_DSA4;
1031 return EVP_PKEY_DHX;
1035 return EVP_PKEY_HMAC;
1037 return EVP_PKEY_CMAC;
1039 return EVP_PKEY_TLS1_PRF;
1041 return EVP_PKEY_HKDF;
1043 coap_log_warn(
"*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
1044 asn1_private_key_type);
1049#if !COAP_DISABLE_TCP
1050static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1052#if COAP_SERVER_SUPPORT
1055 const unsigned char **out,
1056 unsigned char *outlen,
1057 const unsigned char *in,
1061 unsigned char *tout = NULL;
1064 return SSL_TLSEXT_ERR_NOACK;
1065 ret = SSL_select_next_proto(&tout,
1072 return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
1078add_ca_to_cert_store(X509_STORE *st, X509 *x509) {
1082 while ((e = ERR_get_error()) != 0) {
1085 if (!X509_STORE_add_cert(st, x509)) {
1086 while ((e = ERR_get_error()) != 0) {
1087 int r = ERR_GET_REASON(e);
1088 if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1091 ERR_reason_error_string(e),
1092 ssl_function_definition(e));
1099missing_ENGINE_load_cert(
const char *cert_id) {
1101 const char *cert_id;
1105 params.cert_id = cert_id;
1109 if (!ENGINE_ctrl_cmd(ssl_engine,
"LOAD_CERT_CTRL", 0, ¶ms, NULL, 1)) {
1115#if OPENSSL_VERSION_NUMBER < 0x10101000L && COAP_SERVER_SUPPORT
1117setup_pki_server(SSL_CTX *ctx,
1124 if (!(SSL_CTX_use_certificate_file(ctx,
1126 SSL_FILETYPE_PEM))) {
1127 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1128 "Server Certificate\n",
1133 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1139 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1141 SSL_FILETYPE_PEM))) {
1142 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1143 "Server Private Key\n",
1148 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1154 STACK_OF(X509_NAME) *cert_names;
1158 char *rw_var = NULL;
1160 if (cert_names != NULL)
1161 SSL_CTX_set_client_CA_list(ctx, cert_names);
1163 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1170 st = SSL_CTX_get_cert_store(ctx);
1171 in = BIO_new(BIO_s_file());
1174 if (!BIO_read_filename(in, rw_var)) {
1181 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
1183 add_ca_to_cert_store(st, x);
1195 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1197 if (!cert || !SSL_CTX_use_certificate(ctx, cert)) {
1199 "Server PEM Certificate\n");
1211 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1219 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1221 if (!pkey || !SSL_CTX_use_PrivateKey(ctx, pkey)) {
1223 "Server PEM Private Key\n");
1227 EVP_PKEY_free(pkey);
1233 EVP_PKEY_free(pkey);
1235 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1246 st = SSL_CTX_get_cert_store(ctx);
1249 if ((x = PEM_read_bio_X509(bp, NULL, NULL, NULL)) == NULL)
1251 add_ca_to_cert_store(st, x);
1252 SSL_CTX_add_client_CA(ctx, x);
1263 if (!(SSL_CTX_use_certificate_ASN1(ctx,
1266 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1267 "Server Certificate\n",
1272 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1279 if (!(SSL_CTX_use_PrivateKey_ASN1(pkey_type, ctx,
1282 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1283 "Server Private Key\n",
1288 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1298 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1299 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1306 st = SSL_CTX_get_cert_store(ctx);
1307 add_ca_to_cert_store(st, x509);
1314 ssl_engine = ENGINE_by_id(
"pkcs11");
1316 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support\nn");
1319 if (!ENGINE_init(ssl_engine)) {
1321 ENGINE_free(ssl_engine);
1323 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1330 if (ENGINE_ctrl_cmd_string(ssl_engine,
"PIN",
1332 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1341 "pkcs11:", 7) == 0) {
1342 EVP_PKEY *pkey = ENGINE_load_private_key(ssl_engine,
1348 "Server Private Key\n",
1352 if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
1353 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1354 "Server Private Key\n",
1356 EVP_PKEY_free(pkey);
1359 EVP_PKEY_free(pkey);
1361 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1363 SSL_FILETYPE_ASN1))) {
1364 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1365 "Server Private Key\n",
1371 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1378 "pkcs11:", 7) == 0) {
1381 x509 = missing_ENGINE_load_cert(
1385 "Server Certificate\n",
1389 if (!SSL_CTX_use_certificate(ctx, x509)) {
1390 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1391 "Server Certificate\n",
1398 if (!(SSL_CTX_use_certificate_file(ctx,
1400 SSL_FILETYPE_ASN1))) {
1401 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1402 "Server Certificate\n",
1408 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1419 x509 = missing_ENGINE_load_cert(
1423 "Server CA Certificate\n",
1427 if (!SSL_CTX_add_client_CA(ctx, x509)) {
1428 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1434 st = SSL_CTX_get_cert_store(ctx);
1435 add_ca_to_cert_store(st, x509);
1439 X509 *x509 = fp ? d2i_X509_fp(fp, NULL) : NULL;
1441 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1442 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1449 st = SSL_CTX_get_cert_store(ctx);
1450 add_ca_to_cert_store(st, x509);
1457 coap_log_err(
"*** setup_pki: (D)TLS: Unknown key type %d\n",
1466#if OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT
1468setup_pki_ssl(SSL *ssl,
1472 coap_log_err(
"RPK Support not available in OpenSSL\n");
1479 if (!(SSL_use_certificate_file(ssl,
1481 SSL_FILETYPE_PEM))) {
1482 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1491 coap_log_err(
"*** setup_pki: (D)TLS: No %s Certificate defined\n",
1497 if (!(SSL_use_PrivateKey_file(ssl,
1499 SSL_FILETYPE_PEM))) {
1500 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1501 "Client Private Key\n",
1508 coap_log_err(
"*** setup_pki: (D)TLS: No %s Private Key defined\n",
1517 char *rw_var = NULL;
1518 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1521 STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(setup_data->
pki_key.
key.
pem.
ca_file);
1523 if (cert_names != NULL)
1524 SSL_set_client_CA_list(ssl, cert_names);
1526 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1535 in = BIO_new(BIO_s_file());
1538 if (!BIO_read_filename(in, rw_var)) {
1542 st = SSL_CTX_get_cert_store(ctx);
1544 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
1546 add_ca_to_cert_store(st, x);
1558 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1560 if (!cert || !SSL_use_certificate(ssl, cert)) {
1562 "Server PEM Certificate\n");
1574 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1582 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1584 if (!pkey || !SSL_use_PrivateKey(ssl, pkey)) {
1586 "Server PEM Private Key\n");
1590 EVP_PKEY_free(pkey);
1596 EVP_PKEY_free(pkey);
1598 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1606 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1608 X509_STORE *st = SSL_CTX_get_cert_store(ctx);
1612 if ((x = PEM_read_bio_X509(bp, NULL, 0, NULL)) == NULL)
1614 add_ca_to_cert_store(st, x);
1615 SSL_add_client_CA(ssl, x);
1626 if (!(SSL_use_certificate_ASN1(ssl,
1629 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1638 coap_log_err(
"*** setup_pki: (D)TLS: No %s Certificate defined\n",
1645 if (!(SSL_use_PrivateKey_ASN1(pkey_type, ssl,
1648 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1657 coap_log_err(
"*** setup_pki: (D)TLS: No %s Private Key defined",
1667 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1670 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
1671 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1680 st = SSL_CTX_get_cert_store(ctx);
1681 add_ca_to_cert_store(st, x509);
1688 ssl_engine = ENGINE_by_id(
"pkcs11");
1690 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support - need OpenSSL pkcs11 engine\n");
1693 if (!ENGINE_init(ssl_engine)) {
1695 ENGINE_free(ssl_engine);
1697 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1704 if (ENGINE_ctrl_cmd_string(ssl_engine,
1707 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1716 "pkcs11:", 7) == 0) {
1717 EVP_PKEY *pkey = ENGINE_load_private_key(ssl_engine,
1728 if (!SSL_use_PrivateKey(ssl, pkey)) {
1729 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1733 EVP_PKEY_free(pkey);
1736 EVP_PKEY_free(pkey);
1738 if (!(SSL_use_PrivateKey_file(ssl,
1740 SSL_FILETYPE_ASN1))) {
1741 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1749 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1756 "pkcs11:", 7) == 0) {
1759 x509 = missing_ENGINE_load_cert(
1768 if (!SSL_use_certificate(ssl, x509)) {
1769 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1778 if (!(SSL_use_certificate_file(ssl,
1780 SSL_FILETYPE_ASN1))) {
1781 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1789 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1799 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1801 x509 = missing_ENGINE_load_cert(
1805 "%s CA Certificate\n",
1810 if (!SSL_add_client_CA(ssl, x509)) {
1811 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1812 "%s CA Certificate\n",
1818 st = SSL_CTX_get_cert_store(ctx);
1819 add_ca_to_cert_store(st, x509);
1823 X509 *x509 = fp ? d2i_X509_fp(fp, NULL) : NULL;
1824 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1826 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
1827 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1835 st = SSL_CTX_get_cert_store(ctx);
1836 add_ca_to_cert_store(st, x509);
1843 coap_log_err(
"*** setup_pki: (D)TLS: Unknown key type %d\n",
1852get_san_or_cn_from_cert(X509 *x509) {
1856 STACK_OF(GENERAL_NAME) *san_list;
1859 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
1861 int san_count = sk_GENERAL_NAME_num(san_list);
1863 for (n = 0; n < san_count; n++) {
1864 const GENERAL_NAME *name = sk_GENERAL_NAME_value(san_list, n);
1866 if (name->type == GEN_DNS) {
1867 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
1870 if (ASN1_STRING_length(name->d.dNSName) != (int)strlen(dns_name))
1872 cn = OPENSSL_strdup(dns_name);
1873 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1877 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1880 X509_NAME_oneline(X509_get_subject_name(x509), buffer,
sizeof(buffer));
1883 n = (int)strlen(buffer) - 3;
1886 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
1887 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
1896 char *ecn = strchr(cn,
'/');
1898 return OPENSSL_strndup(cn, ecn-cn);
1900 return OPENSSL_strdup(cn);
1908tls_verify_call_back(
int preverify_ok, X509_STORE_CTX *ctx) {
1909 SSL *ssl = X509_STORE_CTX_get_ex_data(ctx,
1910 SSL_get_ex_data_X509_STORE_CTX_idx());
1912 coap_openssl_context_t *context =
1915 int depth = X509_STORE_CTX_get_error_depth(ctx);
1916 int err = X509_STORE_CTX_get_error(ctx);
1917 X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
1918 char *cn = get_san_or_cn_from_cert(x509);
1919 int keep_preverify_ok = preverify_ok;
1921 if (!preverify_ok) {
1923 case X509_V_ERR_CERT_NOT_YET_VALID:
1924 case X509_V_ERR_CERT_HAS_EXPIRED:
1928 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1932 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1936 case X509_V_ERR_UNABLE_TO_GET_CRL:
1940 case X509_V_ERR_CRL_NOT_YET_VALID:
1941 case X509_V_ERR_CRL_HAS_EXPIRED:
1945 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1946 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1947 case X509_V_ERR_AKID_SKID_MISMATCH:
1957 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1958 X509_STORE_CTX_set_error(ctx, err);
1960 if (!preverify_ok) {
1961 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1964 "Unknown CA", cn ? cn :
"?", depth);
1968 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1973 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1978 int length = i2d_X509(x509, NULL);
1980 uint8_t *base_buf2 = base_buf = OPENSSL_malloc(length);
1983 i2d_X509(x509, &base_buf2);
1985 depth, preverify_ok,
1988 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1990 X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1994 OPENSSL_free(base_buf);
1997 return preverify_ok;
2000#if COAP_SERVER_SUPPORT
2001#if OPENSSL_VERSION_NUMBER < 0x10101000L
2010tls_secret_call_back(SSL *ssl,
2013 STACK_OF(SSL_CIPHER) *peer_ciphers,
2018 int psk_requested = 0;
2023 assert(session != NULL);
2024 assert(session->
context != NULL);
2025 if (session == NULL ||
2033 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2034 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2037 SSL_CIPHER_get_name(peer_cipher));
2038 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2044 if (!psk_requested) {
2051 SSL_VERIFY_CLIENT_ONCE |
2052 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2053 tls_verify_call_back);
2055 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2064 X509_VERIFY_PARAM *param;
2066 param = X509_VERIFY_PARAM_new();
2067 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2068 SSL_set1_param(ssl, param);
2069 X509_VERIFY_PARAM_free(param);
2091 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2092 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2106tls_server_name_call_back(SSL *ssl,
2113 return SSL_TLSEXT_ERR_NOACK;
2119 coap_openssl_context_t *context =
2121 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2124 if (!sni || !sni[0]) {
2127 for (i = 0; i < context->sni_count; i++) {
2128 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2132 if (i == context->sni_count) {
2138 return SSL_TLSEXT_ERR_ALERT_FATAL;
2143 ctx = SSL_CTX_new(DTLS_method());
2146 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2147 SSL_CTX_set_app_data(ctx, &context->dtls);
2148 SSL_CTX_set_read_ahead(ctx, 1);
2149 coap_set_user_prefs(ctx);
2150 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2151 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2152 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2153 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2155#if !COAP_DISABLE_TCP
2158 ctx = SSL_CTX_new(TLS_method());
2161 SSL_CTX_set_app_data(ctx, &context->tls);
2162 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2163 coap_set_user_prefs(ctx);
2164 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2165 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2168 sni_setup_data = *setup_data;
2169 sni_setup_data.
pki_key = *new_entry;
2170 setup_pki_server(ctx, &sni_setup_data);
2172 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2173 (context->sni_count+1)*
sizeof(sni_entry));
2174 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2175 context->sni_entry_list[context->sni_count].ctx = ctx;
2176 context->sni_count++;
2178 SSL_set_SSL_CTX(ssl, context->sni_entry_list[i].ctx);
2179 SSL_clear_options(ssl, 0xFFFFFFFFL);
2180 SSL_set_options(ssl, SSL_CTX_get_options(context->sni_entry_list[i].ctx));
2187 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2188 return SSL_TLSEXT_ERR_OK;
2191 return SSL_TLSEXT_ERR_ALERT_WARNING;
2202psk_tls_server_name_call_back(SSL *ssl,
2209 return SSL_TLSEXT_ERR_NOACK;
2215 coap_openssl_context_t *o_context =
2217 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2221 if (!sni || !sni[0]) {
2224 for (i = 0; i < o_context->psk_sni_count; i++) {
2225 if (!strcasecmp(sni, (
char *)o_context->psk_sni_entry_list[i].sni)) {
2229 if (i == o_context->psk_sni_count) {
2236 return SSL_TLSEXT_ERR_ALERT_FATAL;
2241 ctx = SSL_CTX_new(DTLS_method());
2244 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2245 SSL_CTX_set_app_data(ctx, &o_context->dtls);
2246 SSL_CTX_set_read_ahead(ctx, 1);
2247 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2248 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2249 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2250 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2251 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2253#if !COAP_DISABLE_TCP
2256 ctx = SSL_CTX_new(TLS_method());
2259 SSL_CTX_set_app_data(ctx, &o_context->tls);
2260 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2261 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2262 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2263 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2267 o_context->psk_sni_entry_list =
2268 OPENSSL_realloc(o_context->psk_sni_entry_list,
2269 (o_context->psk_sni_count+1)*
sizeof(psk_sni_entry));
2270 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2271 OPENSSL_strdup(sni);
2272 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2274 o_context->psk_sni_entry_list[o_context->psk_sni_count].ctx =
2276 o_context->psk_sni_count++;
2278 SSL_set_SSL_CTX(ssl, o_context->psk_sni_entry_list[i].ctx);
2279 SSL_clear_options(ssl, 0xFFFFFFFFL);
2280 SSL_set_options(ssl,
2281 SSL_CTX_get_options(o_context->psk_sni_entry_list[i].ctx));
2283 &o_context->psk_sni_entry_list[i].psk_info.key);
2284 snprintf(lhint,
sizeof(lhint),
"%.*s",
2285 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2286 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2287 SSL_use_psk_identity_hint(ssl, lhint);
2294 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2295 return SSL_TLSEXT_ERR_OK;
2298 return SSL_TLSEXT_ERR_ALERT_WARNING;
2310tls_client_hello_call_back(SSL *ssl,
2315 coap_openssl_context_t *dtls_context;
2317 int psk_requested = 0;
2318 const unsigned char *out;
2322 *al = SSL_AD_INTERNAL_ERROR;
2323 return SSL_CLIENT_HELLO_ERROR;
2326 assert(session != NULL);
2327 assert(session->
context != NULL);
2329 if (session == NULL ||
2332 *al = SSL_AD_INTERNAL_ERROR;
2333 return SSL_CLIENT_HELLO_ERROR;
2336 setup_data = &dtls_context->setup_data;
2344 size_t len = SSL_client_hello_get0_ciphers(ssl, &out);
2345 STACK_OF(SSL_CIPHER) *peer_ciphers = NULL;
2346 STACK_OF(SSL_CIPHER) *scsvc = NULL;
2348 if (len && SSL_bytes_to_cipher_list(ssl, out, len,
2349 SSL_client_hello_isv2(ssl),
2350 &peer_ciphers, &scsvc)) {
2352 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2353 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2356 "Client cipher: %s (%04x)\n",
2357 SSL_CIPHER_get_name(peer_cipher),
2358 SSL_CIPHER_get_protocol_id(peer_cipher));
2359 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2365 sk_SSL_CIPHER_free(peer_ciphers);
2366 sk_SSL_CIPHER_free(scsvc);
2369 if (psk_requested) {
2375 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2381 return SSL_CLIENT_HELLO_SUCCESS;
2391 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_client_certificate_type,
2394 for (ii = 0; ii < outlen; ii++) {
2410 *al = SSL_AD_UNSUPPORTED_EXTENSION;
2411 return SSL_CLIENT_HELLO_ERROR;
2420 coap_openssl_context_t *context =
2422 const char *sni =
"";
2423 char *sni_tmp = NULL;
2426 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2428 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2429 out[2] == TLSEXT_NAMETYPE_host_name &&
2430 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
2434 sni_tmp = OPENSSL_malloc(outlen+1);
2435 sni_tmp[outlen] =
'\000';
2436 memcpy(sni_tmp, out, outlen);
2440 for (i = 0; i < context->sni_count; i++) {
2441 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2445 if (i == context->sni_count) {
2452 *al = SSL_AD_UNRECOGNIZED_NAME;
2453 return SSL_CLIENT_HELLO_ERROR;
2457 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2458 (context->sni_count+1)*
sizeof(sni_entry));
2459 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2460 context->sni_entry_list[context->sni_count].pki_key = *new_entry;
2461 context->sni_count++;
2464 OPENSSL_free(sni_tmp);
2466 sni_setup_data = *setup_data;
2467 sni_setup_data.
pki_key = context->sni_entry_list[i].pki_key;
2479 SSL_VERIFY_CLIENT_ONCE |
2480 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2481 tls_verify_call_back);
2483 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2492 X509_VERIFY_PARAM *param;
2494 param = X509_VERIFY_PARAM_new();
2495 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2496 SSL_set1_param(ssl, param);
2497 X509_VERIFY_PARAM_free(param);
2504 return SSL_CLIENT_HELLO_SUCCESS;
2515psk_tls_client_hello_call_back(SSL *ssl,
2520 coap_openssl_context_t *o_context;
2522 const unsigned char *out;
2528 if (!c_session || !c_session->
context) {
2541 const char *sni =
"";
2542 char *sni_tmp = NULL;
2546 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2548 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2549 out[2] == TLSEXT_NAMETYPE_host_name &&
2550 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
2554 sni_tmp = OPENSSL_malloc(outlen+1);
2556 sni_tmp[outlen] =
'\000';
2557 memcpy(sni_tmp, out, outlen);
2563 for (i = 0; i < o_context->psk_sni_count; i++) {
2564 if (strcasecmp(sni, o_context->psk_sni_entry_list[i].sni) == 0) {
2568 if (i == o_context->psk_sni_count) {
2572 psk_sni_entry *tmp_entry;
2578 *al = SSL_AD_UNRECOGNIZED_NAME;
2579 return SSL_CLIENT_HELLO_ERROR;
2583 OPENSSL_realloc(o_context->psk_sni_entry_list,
2584 (o_context->psk_sni_count+1)*
sizeof(sni_entry));
2586 o_context->psk_sni_entry_list = tmp_entry;
2587 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2588 OPENSSL_strdup(sni);
2589 if (o_context->psk_sni_entry_list[o_context->psk_sni_count].sni) {
2590 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2592 o_context->psk_sni_count++;
2597 OPENSSL_free(sni_tmp);
2600 &o_context->psk_sni_entry_list[i].psk_info.hint)
2605 &o_context->psk_sni_entry_list[i].psk_info.key)
2609 if (o_context->psk_sni_entry_list[i].psk_info.hint.s) {
2610 snprintf(lhint,
sizeof(lhint),
"%.*s",
2611 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2612 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2613 SSL_use_psk_identity_hint(ssl, lhint);
2616 return SSL_CLIENT_HELLO_SUCCESS;
2619 *al = SSL_AD_INTERNAL_ERROR;
2620 return SSL_CLIENT_HELLO_ERROR;
2630 coap_openssl_context_t *context =
2635 context->setup_data = *setup_data;
2636 if (!context->setup_data.verify_peer_cert) {
2640 context->setup_data.allow_self_signed = 1;
2641 context->setup_data.allow_expired_certs = 1;
2642 context->setup_data.cert_chain_validation = 1;
2643 context->setup_data.cert_chain_verify_depth = 10;
2644 context->setup_data.check_cert_revocation = 1;
2645 context->setup_data.allow_no_crl = 1;
2646 context->setup_data.allow_expired_crl = 1;
2647 context->setup_data.allow_bad_md_hash = 1;
2648 context->setup_data.allow_short_rsa_length = 1;
2650#if COAP_SERVER_SUPPORT
2652 if (context->dtls.ctx) {
2654#if OPENSSL_VERSION_NUMBER < 0x10101000L
2655 if (!setup_pki_server(context->dtls.ctx, setup_data))
2664#if OPENSSL_VERSION_NUMBER < 0x10101000L
2665 if (SSLeay() >= 0x10101000L) {
2666 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
2667 "no certificate checking\n",
2668 OPENSSL_VERSION_NUMBER, SSLeay());
2670 SSL_CTX_set_tlsext_servername_arg(context->dtls.ctx, &context->setup_data);
2671 SSL_CTX_set_tlsext_servername_callback(context->dtls.ctx,
2672 tls_server_name_call_back);
2674 SSL_CTX_set_client_hello_cb(context->dtls.ctx,
2675 tls_client_hello_call_back,
2679#if !COAP_DISABLE_TCP
2680 if (context->tls.ctx) {
2682#if OPENSSL_VERSION_NUMBER < 0x10101000L
2683 if (!setup_pki_server(context->tls.ctx, setup_data))
2692#if OPENSSL_VERSION_NUMBER < 0x10101000L
2693 if (SSLeay() >= 0x10101000L) {
2694 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
2695 "no certificate checking\n",
2696 OPENSSL_VERSION_NUMBER, SSLeay());
2698 SSL_CTX_set_tlsext_servername_arg(context->tls.ctx, &context->setup_data);
2699 SSL_CTX_set_tlsext_servername_callback(context->tls.ctx,
2700 tls_server_name_call_back);
2702 SSL_CTX_set_client_hello_cb(context->tls.ctx,
2703 tls_client_hello_call_back,
2707 SSL_CTX_set_alpn_select_cb(context->tls.ctx, server_alpn_callback, NULL);
2715 if (!context->dtls.ssl) {
2717 context->dtls.ssl = SSL_new(context->dtls.ctx);
2718 if (!context->dtls.ssl)
2720 bio = BIO_new(context->dtls.meth);
2722 SSL_free(context->dtls.ssl);
2723 context->dtls.ssl = NULL;
2726 SSL_set_bio(context->dtls.ssl, bio, bio);
2727 SSL_set_app_data(context->dtls.ssl, NULL);
2728 SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
2731 context->psk_pki_enabled |= IS_PKI;
2737 const char *ca_file,
2740 coap_openssl_context_t *context =
2742 if (context->dtls.ctx) {
2743 if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) {
2745 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
2749#if !COAP_DISABLE_TCP
2750 if (context->tls.ctx) {
2751 if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) {
2753 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
2763 coap_openssl_context_t *context =
2765 return context->psk_pki_enabled ? 1 : 0;
2772 coap_openssl_context_t *context = (coap_openssl_context_t *)handle;
2774 if (context->dtls.ssl)
2775 SSL_free(context->dtls.ssl);
2776 if (context->dtls.ctx)
2777 SSL_CTX_free(context->dtls.ctx);
2778 if (context->dtls.cookie_hmac)
2779 HMAC_CTX_free(context->dtls.cookie_hmac);
2780 if (context->dtls.meth)
2781 BIO_meth_free(context->dtls.meth);
2782 if (context->dtls.bio_addr)
2783 BIO_ADDR_free(context->dtls.bio_addr);
2784#if !COAP_DISABLE_TCP
2785 if (context->tls.ctx)
2786 SSL_CTX_free(context->tls.ctx);
2787 if (context->tls.meth)
2788 BIO_meth_free(context->tls.meth);
2790 for (i = 0; i < context->sni_count; i++) {
2791 OPENSSL_free(context->sni_entry_list[i].sni);
2792#if OPENSSL_VERSION_NUMBER < 0x10101000L
2793 SSL_CTX_free(context->sni_entry_list[i].ctx);
2796 if (context->sni_count)
2797 OPENSSL_free(context->sni_entry_list);
2798 for (i = 0; i < context->psk_sni_count; i++) {
2799 OPENSSL_free((
char *)context->psk_sni_entry_list[i].sni);
2800#if OPENSSL_VERSION_NUMBER < 0x10101000L
2801 SSL_CTX_free(context->psk_sni_entry_list[i].ctx);
2804 if (context->psk_sni_count)
2805 OPENSSL_free(context->psk_sni_entry_list);
2809#if COAP_SERVER_SUPPORT
2813 SSL *nssl = NULL, *ssl = NULL;
2814 coap_ssl_data *data;
2815 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
2819 nssl = SSL_new(dtls->ctx);
2822 nbio = BIO_new(dtls->meth);
2825 SSL_set_bio(nssl, nbio, nbio);
2826 SSL_set_app_data(nssl, NULL);
2827 SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE);
2828 SSL_set_mtu(nssl, (
long)session->
mtu);
2832 SSL_set_app_data(ssl, session);
2834 data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
2835 data->session = session;
2839 if (psk_hint != NULL && psk_hint->
length) {
2840 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
2843 memcpy(hint, psk_hint->
s, psk_hint->
length);
2844 hint[psk_hint->
length] =
'\000';
2845 SSL_use_psk_identity_hint(ssl, hint);
2852 r = SSL_accept(ssl);
2854 int err = SSL_get_error(ssl, r);
2855 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
2873#if COAP_CLIENT_SUPPORT
2877 coap_openssl_context_t *context =
2880 if (context->psk_pki_enabled & IS_PSK) {
2885 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2889 SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2890#if COAP_SERVER_SUPPORT
2891 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2893 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2896 SSL_set_max_proto_version(ssl, DTLS1_2_VERSION);
2898#if !COAP_DISABLE_TCP
2900 SSL_set_max_proto_version(ssl, TLS1_2_VERSION);
2903 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2906 if (context->psk_pki_enabled & IS_PKI) {
2911#if !COAP_DISABLE_TCP
2913 SSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
2918 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2924 X509_VERIFY_PARAM *param;
2926 param = X509_VERIFY_PARAM_new();
2927 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2928 SSL_set1_param(ssl, param);
2929 X509_VERIFY_PARAM_free(param);
2936 SSL_VERIFY_CLIENT_ONCE |
2937 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2938 tls_verify_call_back);
2940 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2954 coap_ssl_data *data;
2956 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
2957 coap_dtls_context_t *dtls = &context->dtls;
2959 ssl = SSL_new(dtls->ctx);
2962 bio = BIO_new(dtls->meth);
2965 data = (coap_ssl_data *)BIO_get_data(bio);
2966 data->session = session;
2967 SSL_set_bio(ssl, bio, bio);
2968 SSL_set_app_data(ssl, session);
2969 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2970 SSL_set_mtu(ssl, (
long)session->
mtu);
2972 if (!setup_client_ssl_session(session, ssl))
2977 r = SSL_connect(ssl);
2979 int ret = SSL_get_error(ssl, r);
2980 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
2998 SSL *ssl = (SSL *)session->
tls;
3000 SSL_set_mtu(ssl, (
long)session->
mtu);
3006 SSL *ssl = (SSL *)session->
tls;
3008 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3009 int r = SSL_shutdown(ssl);
3011 r = SSL_shutdown(ssl);
3014 session->
tls = NULL;
3022 const uint8_t *data,
size_t data_len) {
3024 SSL *ssl = (SSL *)session->
tls;
3026 assert(ssl != NULL);
3029 r = SSL_write(ssl, data, (
int)data_len);
3032 int err = SSL_get_error(ssl, r);
3033 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3037 if (err == SSL_ERROR_ZERO_RETURN)
3039 else if (err == SSL_ERROR_SSL)
3057 if (r == (ssize_t)data_len)
3080 SSL *ssl = (SSL *)session->
tls;
3081 coap_ssl_data *ssl_data;
3084 ssl_data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
3085 return ssl_data->timeout;
3094 SSL *ssl = (SSL *)session->
tls;
3098 (DTLSv1_handle_timeout(ssl) < 0)) {
3106#if COAP_SERVER_SUPPORT
3109 const uint8_t *data,
size_t data_len) {
3110 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3111 coap_ssl_data *ssl_data;
3114 SSL_set_mtu(dtls->ssl, (
long)session->
mtu);
3115 ssl_data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(dtls->ssl));
3116 assert(ssl_data != NULL);
3117 if (ssl_data->pdu_len) {
3118 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3121 ssl_data->session = session;
3122 ssl_data->pdu = data;
3123 ssl_data->pdu_len = (unsigned)data_len;
3124 r = DTLSv1_listen(dtls->ssl, dtls->bio_addr);
3126 int err = SSL_get_error(dtls->ssl, r);
3127 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3147 coap_ssl_data *ssl_data;
3148 SSL *ssl = (SSL *)session->
tls;
3151 assert(ssl != NULL);
3153 int in_init = SSL_in_init(ssl);
3155 ssl_data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
3156 assert(ssl_data != NULL);
3158 if (ssl_data->pdu_len) {
3159 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3162 ssl_data->pdu = data;
3163 ssl_data->pdu_len = (unsigned)data_len;
3166 r = SSL_read(ssl, pdu, (
int)
sizeof(pdu));
3171 int err = SSL_get_error(ssl, r);
3172 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3173 if (in_init && SSL_is_init_finished(ssl)) {
3181 if (err == SSL_ERROR_ZERO_RETURN)
3183 else if (err == SSL_ERROR_SSL)
3201 if (ssl_data && ssl_data->pdu_len) {
3203 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
3204 ssl_data->pdu_len = 0;
3205 ssl_data->pdu = NULL;
3216 unsigned int overhead = 37;
3217 const SSL_CIPHER *s_ciph = NULL;
3218 if (session->
tls != NULL)
3219 s_ciph = SSL_get_current_cipher(session->
tls);
3221 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
3223 const EVP_CIPHER *e_ciph;
3227 e_ciph = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(s_ciph));
3229 switch (EVP_CIPHER_mode(e_ciph)) {
3230 case EVP_CIPH_GCM_MODE:
3231 ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
3232 maclen = EVP_GCM_TLS_TAG_LEN;
3235 case EVP_CIPH_CCM_MODE:
3236 ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
3237 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3238 if (strstr(cipher,
"CCM8"))
3244 case EVP_CIPH_CBC_MODE:
3245 e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
3246 blocksize = EVP_CIPHER_block_size(e_ciph);
3247 ivlen = EVP_CIPHER_iv_length(e_ciph);
3249 maclen = EVP_MD_size(e_md);
3252 case EVP_CIPH_STREAM_CIPHER:
3259 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3266 overhead = DTLS1_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 + pad;
3271#if !COAP_DISABLE_TCP
3272#if COAP_CLIENT_SUPPORT
3278 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3279 coap_tls_context_t *tls = &context->tls;
3281 ssl = SSL_new(tls->ctx);
3284 bio = BIO_new(tls->meth);
3287 BIO_set_data(bio, session);
3288 SSL_set_bio(ssl, bio, bio);
3289 SSL_set_app_data(ssl, session);
3291 if (!setup_client_ssl_session(session, ssl))
3294 r = SSL_connect(ssl);
3296 int ret = SSL_get_error(ssl, r);
3297 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3299 if (ret == SSL_ERROR_WANT_READ)
3301 if (ret == SSL_ERROR_WANT_WRITE) {
3303#ifdef COAP_EPOLL_SUPPORT
3317 if (SSL_is_init_finished(ssl)) {
3331#if COAP_SERVER_SUPPORT
3336 coap_tls_context_t *tls = &((coap_openssl_context_t *)session->
context->
dtls_context)->tls;
3340 ssl = SSL_new(tls->ctx);
3343 bio = BIO_new(tls->meth);
3346 BIO_set_data(bio, session);
3347 SSL_set_bio(ssl, bio, bio);
3348 SSL_set_app_data(ssl, session);
3351 if (psk_hint != NULL && psk_hint->
length) {
3352 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
3355 memcpy(hint, psk_hint->
s, psk_hint->
length);
3356 hint[psk_hint->
length] =
'\000';
3357 SSL_use_psk_identity_hint(ssl, hint);
3364 r = SSL_accept(ssl);
3366 int err = SSL_get_error(ssl, r);
3367 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3369 if (err == SSL_ERROR_WANT_READ)
3371 if (err == SSL_ERROR_WANT_WRITE) {
3373#ifdef COAP_EPOLL_SUPPORT
3387 if (SSL_is_init_finished(ssl)) {
3403 SSL *ssl = (SSL *)session->
tls;
3405 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3406 int r = SSL_shutdown(ssl);
3408 r = SSL_shutdown(ssl);
3411 session->
tls = NULL;
3424 SSL *ssl = (SSL *)session->
tls;
3430 in_init = !SSL_is_init_finished(ssl);
3432 r = SSL_write(ssl, data, (
int)data_len);
3435 int err = SSL_get_error(ssl, r);
3436 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3437 if (in_init && SSL_is_init_finished(ssl)) {
3443 if (err == SSL_ERROR_WANT_READ)
3445 else if (err == SSL_ERROR_WANT_WRITE) {
3447#ifdef COAP_EPOLL_SUPPORT
3459 if (err == SSL_ERROR_ZERO_RETURN)
3461 else if (err == SSL_ERROR_SSL)
3465 }
else if (in_init && SSL_is_init_finished(ssl)) {
3484 if (r == (ssize_t)data_len)
3501 SSL *ssl = (SSL *)session->
tls;
3509 in_init = !SSL_is_init_finished(ssl);
3511 r = SSL_read(ssl, data, (
int)data_len);
3513 int err = SSL_get_error(ssl, r);
3514 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3515 if (in_init && SSL_is_init_finished(ssl)) {
3521 if (err == SSL_ERROR_WANT_READ)
3523 if (err == SSL_ERROR_WANT_WRITE) {
3525#ifdef COAP_EPOLL_SUPPORT
3535 if (err == SSL_ERROR_ZERO_RETURN)
3537 else if (err == SSL_ERROR_SSL)
3541 }
else if (in_init && SSL_is_init_finished(ssl)) {
3567#if COAP_SERVER_SUPPORT
3570 EVP_MD_CTX *digest_ctx = EVP_MD_CTX_new();
3573 EVP_DigestInit_ex(digest_ctx, EVP_sha256(), NULL);
3580 EVP_MD_CTX_free(digest_ctx);
3585 const uint8_t *data,
3587 return EVP_DigestUpdate(digest_ctx, data, data_len);
3594 int ret = EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
3601#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
3603coap_crypto_output_errors(
const char *prefix) {
3604#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
3609 while ((e = ERR_get_error()))
3612 ERR_reason_error_string(e),
3613 ssl_function_definition(e));
3623static struct hash_algs {
3625 const EVP_MD *(*get_hash)(void);
3634static const EVP_MD *
3635get_hash_alg(
cose_alg_t alg,
size_t *length) {
3638 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3639 if (hashs[idx].alg == alg) {
3640 *length = hashs[idx].length;
3641 return hashs[idx].get_hash();
3644 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3652 unsigned int length;
3653 const EVP_MD *evp_md;
3654 EVP_MD_CTX *evp_ctx = NULL;
3658 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
3659 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3662 evp_ctx = EVP_MD_CTX_new();
3663 if (evp_ctx == NULL)
3665 if (EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
3668 if (EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
3674 if (EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
3676 dummy->length = length;
3677 if (hash_length < dummy->length)
3678 dummy->length = hash_length;
3680 EVP_MD_CTX_free(evp_ctx);
3684 coap_crypto_output_errors(
"coap_crypto_hash");
3687 EVP_MD_CTX_free(evp_ctx);
3692#if COAP_OSCORE_SUPPORT
3698#include <openssl/evp.h>
3699#include <openssl/hmac.h>
3706static struct cipher_algs {
3708 const EVP_CIPHER *(*get_cipher)(void);
3713static const EVP_CIPHER *
3717 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3718 if (ciphers[idx].alg == alg)
3719 return ciphers[idx].get_cipher();
3721 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3730static struct hmac_algs {
3732 const EVP_MD *(*get_hmac)(void);
3739static const EVP_MD *
3743 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3744 if (hmacs[idx].hmac_alg == hmac_alg)
3745 return hmacs[idx].get_hmac();
3747 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3753 return get_cipher_alg(alg) != NULL;
3762 return get_hmac_alg(hmac_alg) != NULL;
3766 if (1 != (Func)) { \
3775 size_t *max_result_len) {
3776 const EVP_CIPHER *cipher;
3779 int result_len = (int)(*max_result_len & INT_MAX);
3784 assert(params != NULL);
3785 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
3792 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
3795 C(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL));
3796 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
3797 C(EVP_CIPHER_CTX_ctrl(ctx,
3798 EVP_CTRL_AEAD_SET_IVLEN,
3801 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, NULL));
3802 C(EVP_EncryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
3805 C(EVP_EncryptUpdate(ctx, NULL, &result_len, NULL, (
int)data->
length));
3806 if (aad && aad->
s && (aad->
length > 0)) {
3807 C(EVP_EncryptUpdate(ctx, NULL, &result_len, aad->
s, (
int)aad->
length));
3809 C(EVP_EncryptUpdate(ctx, result, &result_len, data->
s, (
int)data->
length));
3812 C(EVP_EncryptFinal_ex(ctx, result + result_len, &tmp));
3816 C(EVP_CIPHER_CTX_ctrl(ctx,
3817 EVP_CTRL_CCM_GET_TAG,
3819 result + result_len));
3821 *max_result_len = result_len + ccm->
tag_len;
3822 EVP_CIPHER_CTX_free(ctx);
3826 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
3835 size_t *max_result_len) {
3836 const EVP_CIPHER *cipher;
3846 assert(params != NULL);
3847 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
3859 memcpy(&rwtag, &tag,
sizeof(rwtag));
3862 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
3864 C(EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL));
3865 C(EVP_CIPHER_CTX_ctrl(ctx,
3866 EVP_CTRL_AEAD_SET_IVLEN,
3869 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, rwtag));
3870 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
3872 C(EVP_DecryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
3874 C(EVP_DecryptUpdate(ctx, NULL, &len, NULL, (
int)data->
length));
3875 if (aad && aad->
s && (aad->
length > 0)) {
3876 C(EVP_DecryptUpdate(ctx, NULL, &len, aad->
s, (
int)aad->
length));
3878 tmp = EVP_DecryptUpdate(ctx, result, &len, data->
s, (
int)data->
length);
3879 EVP_CIPHER_CTX_free(ctx);
3881 *max_result_len = 0;
3884 *max_result_len = len;
3888 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
3897 unsigned int result_len;
3898 const EVP_MD *evp_md;
3905 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
3906 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3912 result_len = (
unsigned int)
dummy->length;
3920 dummy->length = result_len;
3925 coap_crypto_output_errors(
"coap_crypto_hmac");
3937#pragma GCC diagnostic ignored "-Wunused-function"
Pulls together all the internal only header files.
#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 ...
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.
struct coap_digest_t coap_digest_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(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
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_handle_event(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_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 *session)
Get the current client's PSK identity.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
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_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.
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.
#define COAP_DTLS_HINT_LENGTH
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
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_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_DTLS_ROLE_SERVER
Internal function invoked for server.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
@ COAP_PKI_KEY_PKCS11
The PKI key type is PKCS11 (DER)
@ COAP_PKI_KEY_PEM_BUF
The PKI key type is PEM buffer.
@ COAP_PKI_KEY_PEM
The PKI key type is PEM file.
@ COAP_PKI_KEY_ASN1
The PKI key type is ASN.1 (DER) buffer.
@ 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_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_oscore_is_supported(void)
Check whether OSCORE is available.
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(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_STATE_HANDSHAKE
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 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::@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.
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.
The structure that holds the PKI key information.
coap_pki_key_pem_t pem
for PEM file keys
coap_pki_key_pkcs11_t pkcs11
for PKCS11 keys
union coap_dtls_key_t::@3 key
coap_pki_key_pem_buf_t pem_buf
for PEM memory keys
coap_pki_key_t key_type
key format type
coap_pki_key_asn1_t asn1
for ASN.1 (DER) memory keys
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 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.
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
const uint8_t * private_key
ASN1 (DER) Private Key.
coap_asn1_privatekey_type_t private_key_type
Private Key Type.
size_t public_cert_len
ASN1 Public Cert length.
size_t private_key_len
ASN1 Private Key length.
const uint8_t * ca_cert
ASN1 (DER) Common CA Cert.
size_t ca_cert_len
ASN1 CA Cert length.
const uint8_t * public_cert
ASN1 (DER) Public Cert, or Public Key if RPK.
size_t ca_cert_len
PEM buffer CA Cert length.
const uint8_t * ca_cert
PEM buffer Common CA Cert.
size_t private_key_len
PEM buffer Private Key length.
const uint8_t * private_key
PEM buffer Private Key If RPK and 'EC PRIVATE KEY' this can be used for both the public_cert and priv...
size_t public_cert_len
PEM buffer Public Cert length.
const uint8_t * public_cert
PEM buffer Public Cert, or Public Key if RPK.
const char * ca_file
File location of Common CA in PEM format.
const char * public_cert
File location of Public Cert.
const char * private_key
File location of Private Key in PEM format.
const char * private_key
pkcs11: URI for Private Key
const char * ca
pkcs11: URI for Common CA Certificate
const char * user_pin
User pin to access PKCS11.
const char * public_cert
pkcs11: URI for Public Cert
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