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"
79#if !defined(__MINGW32__)
80#pragma warning(disable : 4996)
85#ifdef COAP_EPOLL_SUPPORT
86# include <sys/epoll.h>
89#if OPENSSL_VERSION_NUMBER < 0x10100000L
90#error Must be compiled against OpenSSL 1.1.0 or later
94#define strcasecmp _stricmp
95#define strncasecmp _strnicmp
99#ifndef TLSEXT_TYPE_client_certificate_type
100#define TLSEXT_TYPE_client_certificate_type 19
102#ifndef TLSEXT_TYPE_server_certificate_type
103#define TLSEXT_TYPE_server_certificate_type 20
106#ifndef COAP_OPENSSL_CIPHERS
107#if OPENSSL_VERSION_NUMBER >= 0x10101000L
108#define COAP_OPENSSL_CIPHERS "TLSv1.3:TLSv1.2:!NULL"
110#define COAP_OPENSSL_CIPHERS "TLSv1.2:!NULL"
114#ifndef COAP_OPENSSL_PSK_CIPHERS
115#define COAP_OPENSSL_PSK_CIPHERS "PSK:!NULL"
119typedef struct coap_dtls_context_t {
122 HMAC_CTX *cookie_hmac;
125} coap_dtls_context_t;
127typedef struct coap_tls_context_t {
135typedef struct sni_entry {
137#if OPENSSL_VERSION_NUMBER < 0x10101000L
144typedef struct psk_sni_entry {
146#if OPENSSL_VERSION_NUMBER < 0x10101000L
152typedef struct coap_openssl_context_t {
153 coap_dtls_context_t dtls;
155 coap_tls_context_t tls;
160 sni_entry *sni_entry_list;
161 size_t psk_sni_count;
162 psk_sni_entry *psk_sni_entry_list;
163} coap_openssl_context_t;
165#if COAP_SERVER_SUPPORT
166#if OPENSSL_VERSION_NUMBER < 0x10101000L
167static int psk_tls_server_name_call_back(SSL *ssl,
int *sd,
void *arg);
169static int psk_tls_client_hello_call_back(SSL *ssl,
int *al,
void *arg);
175 if (SSLeay() < 0x10100000L) {
176 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
179#if OPENSSL_VERSION_NUMBER >= 0x10101000L
187 if (SSLeay() < 0x10101000L) {
188 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
198 if (SSLeay() < 0x10100000L) {
199 coap_log_warn(
"OpenSSL version 1.1.0 or later is required\n");
202#if OPENSSL_VERSION_NUMBER >= 0x10101000L
203 if (SSLeay() < 0x10101000L) {
204 coap_log_warn(
"OpenSSL version 1.1.1 or later is required\n");
259static ENGINE *ssl_engine = NULL;
263 SSL_load_error_strings();
265 ENGINE_load_dynamic();
272 ENGINE_finish(ssl_engine);
274 ENGINE_free(ssl_engine);
287 return c_session->
tls;
307typedef struct coap_ssl_st {
316coap_dgram_create(BIO *a) {
317 coap_ssl_data *data = NULL;
318 data = malloc(
sizeof(coap_ssl_data));
322 BIO_set_data(a, data);
323 memset(data, 0x00,
sizeof(coap_ssl_data));
328coap_dgram_destroy(BIO *a) {
332 data = (coap_ssl_data *)BIO_get_data(a);
339coap_dgram_read(BIO *a,
char *out,
int outl) {
341 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
344 if (data != NULL && data->pdu_len > 0) {
345 if (outl < (
int)data->pdu_len) {
346 memcpy(out, data->pdu, outl);
349 memcpy(out, data->pdu, data->pdu_len);
350 ret = (int)data->pdu_len;
352 if (!data->peekmode) {
359 BIO_clear_retry_flags(a);
361 BIO_set_retry_read(a);
367coap_dgram_write(BIO *a,
const char *in,
int inl) {
369 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
373#if COAP_SERVER_SUPPORT
374 && data->session->endpoint == NULL
378 BIO_clear_retry_flags(a);
382 ret = (int)data->session->sock.lfunc[
COAP_LAYER_TLS].l_write(data->session,
385 BIO_clear_retry_flags(a);
387 BIO_set_retry_write(a);
389 BIO_clear_retry_flags(a);
396coap_dgram_puts(BIO *a,
const char *pstr) {
397 return coap_dgram_write(a, pstr, (
int)strlen(pstr));
401coap_dgram_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
403 coap_ssl_data *data = BIO_get_data(a);
408 case BIO_CTRL_GET_CLOSE:
409 ret = BIO_get_shutdown(a);
411 case BIO_CTRL_SET_CLOSE:
412 BIO_set_shutdown(a, (
int)num);
415 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
416 data->peekmode = (unsigned)num;
418 case BIO_CTRL_DGRAM_CONNECT:
421 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
422 case BIO_CTRL_DGRAM_GET_MTU:
423 case BIO_CTRL_DGRAM_SET_MTU:
424 case BIO_CTRL_DGRAM_QUERY_MTU:
425 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
430 case BIO_CTRL_DGRAM_MTU_DISCOVER:
431 case BIO_CTRL_DGRAM_SET_CONNECTED:
434 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
436 struct timeval *)ptr)->tv_usec);
440 case BIO_C_FILE_SEEK:
441 case BIO_C_FILE_TELL:
443 case BIO_CTRL_PENDING:
444 case BIO_CTRL_WPENDING:
445 case BIO_CTRL_DGRAM_GET_PEER:
446 case BIO_CTRL_DGRAM_SET_PEER:
447 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
448 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
449 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
450 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
451 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
452 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
453 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
454 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
463coap_dtls_generate_cookie(SSL *ssl,
464 unsigned char *cookie,
465 unsigned int *cookie_len) {
466 coap_dtls_context_t *dtls =
467 (coap_dtls_context_t *)SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl));
468 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
469 int r = HMAC_Init_ex(dtls->cookie_hmac, NULL, 0, NULL, NULL);
470 r &= HMAC_Update(dtls->cookie_hmac,
471 (
const uint8_t *)&data->session->addr_info.local.addr,
472 (
size_t)data->session->addr_info.local.size);
473 r &= HMAC_Update(dtls->cookie_hmac,
474 (
const uint8_t *)&data->session->addr_info.remote.addr,
475 (
size_t)data->session->addr_info.remote.size);
476 r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
481coap_dtls_verify_cookie(SSL *ssl,
482 const uint8_t *cookie,
483 unsigned int cookie_len) {
486 if (coap_dtls_generate_cookie(ssl, hmac, &len) &&
487 cookie_len == len && memcmp(cookie, hmac, len) == 0)
493#if COAP_CLIENT_SUPPORT
495coap_dtls_psk_client_callback(SSL *ssl,
498 unsigned int max_identity_len,
500 unsigned int max_psk_len) {
502 coap_openssl_context_t *o_context;
510 if (c_session == NULL)
513 if (o_context == NULL)
517 temp.
s = hint ? (
const uint8_t *)hint : (
const uint8_t *)
"";
518 temp.
length = strlen((
const char *)temp.
s);
522 (
const char *)temp.
s);
534 if (cpsk_info == NULL)
539 psk_identity = &cpsk_info->
identity;
540 psk_key = &cpsk_info->
key;
546 if (psk_identity == NULL || psk_key == NULL) {
552 if (!max_identity_len)
555 if (psk_identity->
length > max_identity_len) {
556 coap_log_warn(
"psk_identity too large, truncated to %d bytes\n",
560 max_identity_len = (
unsigned int)psk_identity->
length;
562 memcpy(identity, psk_identity->
s, max_identity_len);
563 identity[max_identity_len] =
'\000';
565 if (psk_key->
length > max_psk_len) {
570 max_psk_len = (
unsigned int)psk_key->
length;
572 memcpy(psk, psk_key->
s, max_psk_len);
577#if COAP_SERVER_SUPPORT
579coap_dtls_psk_server_callback(
581 const char *identity,
583 unsigned int max_psk_len
591 if (c_session == NULL)
597 lidentity.
s = identity ? (
const uint8_t *)identity : (
const uint8_t *)
"";
598 lidentity.
length = strlen((
const char *)lidentity.
s);
602 (
int)lidentity.
length, (
const char *)lidentity.
s);
617 if (psk_key->
length > max_psk_len) {
622 max_psk_len = (
unsigned int)psk_key->
length;
624 memcpy(psk, psk_key->
s, max_psk_len);
630ssl_function_definition(
unsigned long e) {
631#if OPENSSL_VERSION_NUMBER >= 0x30000000L
635 static char buff[80];
637 snprintf(buff,
sizeof(buff),
" at %s:%s",
638 ERR_lib_error_string(e), ERR_func_error_string(e));
644coap_dtls_info_callback(
const SSL *ssl,
int where,
int ret) {
647 int w = where &~SSL_ST_MASK;
649 if (w & SSL_ST_CONNECT)
650 pstr =
"SSL_connect";
651 else if (w & SSL_ST_ACCEPT)
656 if (where & SSL_CB_LOOP) {
659 }
else if (where & SSL_CB_ALERT) {
661 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
662 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) {
664 if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY)
668 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
671 SSL_alert_type_string_long(ret),
672 SSL_alert_desc_string_long(ret));
673 }
else if (where & SSL_CB_EXIT) {
679 while ((e = ERR_get_error()))
682 ssl_function_definition(e));
684 }
else if (ret < 0) {
686 int err = SSL_get_error(ssl, ret);
687 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE &&
688 err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT &&
689 err != SSL_ERROR_WANT_X509_LOOKUP) {
693 while ((e = ERR_get_error()))
696 ssl_function_definition(e));
702 if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
708coap_sock_create(BIO *a) {
714coap_sock_destroy(BIO *a) {
726coap_sock_read(BIO *a,
char *out,
int outl) {
735 BIO_set_retry_read(a);
738 BIO_clear_retry_flags(a);
751coap_sock_write(BIO *a,
const char *in,
int inl) {
759 BIO_clear_retry_flags(a);
761 BIO_set_retry_read(a);
764 BIO_clear_retry_flags(a);
768 (errno == EPIPE || errno == ECONNRESET)) {
788coap_sock_puts(BIO *a,
const char *pstr) {
789 return coap_sock_write(a, pstr, (
int)strlen(pstr));
793coap_sock_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
804 case BIO_CTRL_SET_CLOSE:
810 case BIO_CTRL_GET_CLOSE:
819coap_set_user_prefs(SSL_CTX *ctx) {
820 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
822#ifdef COAP_OPENSSL_SIGALGS
823 SSL_CTX_set1_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
824 SSL_CTX_set1_client_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
827#if OPENSSL_VERSION_NUMBER >= 0x10101000L && defined(COAP_OPENSSL_GROUPS)
828 SSL_CTX_set1_groups_list(ctx, COAP_OPENSSL_GROUPS);
834 coap_openssl_context_t *context;
839 uint8_t cookie_secret[32];
841 memset(context, 0,
sizeof(coap_openssl_context_t));
844 context->dtls.ctx = SSL_CTX_new(DTLS_method());
845 if (!context->dtls.ctx)
847 SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
848 SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
849 SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
850 coap_set_user_prefs(context->dtls.ctx);
851 memset(cookie_secret, 0,
sizeof(cookie_secret));
852 if (!RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
854 "Insufficient entropy for random cookie generation");
855 coap_prng(cookie_secret,
sizeof(cookie_secret));
857 context->dtls.cookie_hmac = HMAC_CTX_new();
858 if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret),
861 SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
862 SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
863 SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
864 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
865#if OPENSSL_VERSION_NUMBER >= 0x30000000L
866 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_LEGACY_SERVER_CONNECT);
868 context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM,
"coapdgram");
869 if (!context->dtls.meth)
871 context->dtls.bio_addr = BIO_ADDR_new();
872 if (!context->dtls.bio_addr)
874 BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
875 BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
876 BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
877 BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
878 BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
879 BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
883 context->tls.ctx = SSL_CTX_new(TLS_method());
884 if (!context->tls.ctx)
886 SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
887 SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
888 coap_set_user_prefs(context->tls.ctx);
889 SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
890 context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET,
"coapsock");
891 if (!context->tls.meth)
893 BIO_meth_set_write(context->tls.meth, coap_sock_write);
894 BIO_meth_set_read(context->tls.meth, coap_sock_read);
895 BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
896 BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
897 BIO_meth_set_create(context->tls.meth, coap_sock_create);
898 BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
909#if COAP_SERVER_SUPPORT
914 coap_openssl_context_t *o_context =
918 if (!setup_data || !o_context)
921 SSL_CTX_set_psk_server_callback(o_context->dtls.ctx,
922 coap_dtls_psk_server_callback);
924 SSL_CTX_set_psk_server_callback(o_context->tls.ctx,
925 coap_dtls_psk_server_callback);
931 SSL_CTX_use_psk_identity_hint(o_context->dtls.ctx, hint);
933 SSL_CTX_use_psk_identity_hint(o_context->tls.ctx, hint);
937#if OPENSSL_VERSION_NUMBER < 0x10101000L
938 SSL_CTX_set_tlsext_servername_arg(o_context->dtls.ctx,
940 SSL_CTX_set_tlsext_servername_callback(o_context->dtls.ctx,
941 psk_tls_server_name_call_back);
943 SSL_CTX_set_tlsext_servername_arg(o_context->tls.ctx,
945 SSL_CTX_set_tlsext_servername_callback(o_context->tls.ctx,
946 psk_tls_server_name_call_back);
949 SSL_CTX_set_client_hello_cb(o_context->dtls.ctx,
950 psk_tls_client_hello_call_back,
953 SSL_CTX_set_client_hello_cb(o_context->tls.ctx,
954 psk_tls_client_hello_call_back,
960 if (!o_context->dtls.ssl) {
962 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
963 if (!o_context->dtls.ssl)
965 bio = BIO_new(o_context->dtls.meth);
967 SSL_free(o_context->dtls.ssl);
968 o_context->dtls.ssl = NULL;
971 SSL_set_bio(o_context->dtls.ssl, bio, bio);
972 SSL_set_app_data(o_context->dtls.ssl, NULL);
973 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
976 o_context->psk_pki_enabled |= IS_PSK;
981#if COAP_CLIENT_SUPPORT
986 coap_openssl_context_t *o_context =
990 if (!setup_data || !o_context)
993 if (!o_context->dtls.ssl) {
995 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
996 if (!o_context->dtls.ssl)
998 bio = BIO_new(o_context->dtls.meth);
1000 SSL_free(o_context->dtls.ssl);
1001 o_context->dtls.ssl = NULL;
1004 SSL_set_bio(o_context->dtls.ssl, bio, bio);
1005 SSL_set_app_data(o_context->dtls.ssl, NULL);
1006 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
1009 o_context->psk_pki_enabled |= IS_PSK;
1015map_key_type(
int asn1_private_key_type
1017 switch (asn1_private_key_type) {
1019 return EVP_PKEY_NONE;
1021 return EVP_PKEY_RSA;
1023 return EVP_PKEY_RSA2;
1025 return EVP_PKEY_DSA;
1027 return EVP_PKEY_DSA1;
1029 return EVP_PKEY_DSA2;
1031 return EVP_PKEY_DSA3;
1033 return EVP_PKEY_DSA4;
1037 return EVP_PKEY_DHX;
1041 return EVP_PKEY_HMAC;
1043 return EVP_PKEY_CMAC;
1045 return EVP_PKEY_TLS1_PRF;
1047 return EVP_PKEY_HKDF;
1049 coap_log_warn(
"*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
1050 asn1_private_key_type);
1055#if !COAP_DISABLE_TCP
1056static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
1058#if COAP_SERVER_SUPPORT
1061 const unsigned char **out,
1062 unsigned char *outlen,
1063 const unsigned char *in,
1067 unsigned char *tout = NULL;
1070 return SSL_TLSEXT_ERR_NOACK;
1071 ret = SSL_select_next_proto(&tout,
1078 return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
1084add_ca_to_cert_store(X509_STORE *st, X509 *x509) {
1088 while ((e = ERR_get_error()) != 0) {
1091 if (!X509_STORE_add_cert(st, x509)) {
1092 while ((e = ERR_get_error()) != 0) {
1093 int r = ERR_GET_REASON(e);
1094 if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1097 ERR_reason_error_string(e),
1098 ssl_function_definition(e));
1105missing_ENGINE_load_cert(
const char *cert_id) {
1107 const char *cert_id;
1111 params.cert_id = cert_id;
1115 if (!ENGINE_ctrl_cmd(ssl_engine,
"LOAD_CERT_CTRL", 0, ¶ms, NULL, 1)) {
1121#if OPENSSL_VERSION_NUMBER < 0x10101000L && COAP_SERVER_SUPPORT
1123setup_pki_server(SSL_CTX *ctx,
1130 if (!(SSL_CTX_use_certificate_file(ctx,
1132 SSL_FILETYPE_PEM))) {
1133 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1134 "Server Certificate\n",
1139 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1145 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1147 SSL_FILETYPE_PEM))) {
1148 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1149 "Server Private Key\n",
1154 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1160 STACK_OF(X509_NAME) *cert_names;
1164 char *rw_var = NULL;
1166 if (cert_names != NULL)
1167 SSL_CTX_set_client_CA_list(ctx, cert_names);
1169 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1176 st = SSL_CTX_get_cert_store(ctx);
1177 in = BIO_new(BIO_s_file());
1180 if (!BIO_read_filename(in, rw_var)) {
1187 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
1189 add_ca_to_cert_store(st, x);
1201 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1203 if (!cert || !SSL_CTX_use_certificate(ctx, cert)) {
1205 "Server PEM Certificate\n");
1217 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1225 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1227 if (!pkey || !SSL_CTX_use_PrivateKey(ctx, pkey)) {
1229 "Server PEM Private Key\n");
1233 EVP_PKEY_free(pkey);
1239 EVP_PKEY_free(pkey);
1241 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1252 st = SSL_CTX_get_cert_store(ctx);
1255 if ((x = PEM_read_bio_X509(bp, NULL, NULL, NULL)) == NULL)
1257 add_ca_to_cert_store(st, x);
1258 SSL_CTX_add_client_CA(ctx, x);
1269 if (!(SSL_CTX_use_certificate_ASN1(ctx,
1272 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1273 "Server Certificate\n",
1278 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1285 if (!(SSL_CTX_use_PrivateKey_ASN1(pkey_type, ctx,
1288 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1289 "Server Private Key\n",
1294 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1304 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1305 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1312 st = SSL_CTX_get_cert_store(ctx);
1313 add_ca_to_cert_store(st, x509);
1320 ssl_engine = ENGINE_by_id(
"pkcs11");
1322 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support\nn");
1325 if (!ENGINE_init(ssl_engine)) {
1327 ENGINE_free(ssl_engine);
1329 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1336 if (ENGINE_ctrl_cmd_string(ssl_engine,
"PIN",
1338 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1347 "pkcs11:", 7) == 0) {
1348 EVP_PKEY *pkey = ENGINE_load_private_key(ssl_engine,
1354 "Server Private Key\n",
1358 if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
1359 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1360 "Server Private Key\n",
1362 EVP_PKEY_free(pkey);
1365 EVP_PKEY_free(pkey);
1367 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1369 SSL_FILETYPE_ASN1))) {
1370 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1371 "Server Private Key\n",
1377 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1384 "pkcs11:", 7) == 0) {
1387 x509 = missing_ENGINE_load_cert(
1391 "Server Certificate\n",
1395 if (!SSL_CTX_use_certificate(ctx, x509)) {
1396 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1397 "Server Certificate\n",
1404 if (!(SSL_CTX_use_certificate_file(ctx,
1406 SSL_FILETYPE_ASN1))) {
1407 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1408 "Server Certificate\n",
1414 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1425 x509 = missing_ENGINE_load_cert(
1429 "Server CA Certificate\n",
1433 if (!SSL_CTX_add_client_CA(ctx, x509)) {
1434 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1440 st = SSL_CTX_get_cert_store(ctx);
1441 add_ca_to_cert_store(st, x509);
1445 X509 *x509 = fp ? d2i_X509_fp(fp, NULL) : NULL;
1447 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1448 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1455 st = SSL_CTX_get_cert_store(ctx);
1456 add_ca_to_cert_store(st, x509);
1463 coap_log_err(
"*** setup_pki: (D)TLS: Unknown key type %d\n",
1472#if OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT
1474setup_pki_ssl(SSL *ssl,
1478 coap_log_err(
"RPK Support not available in OpenSSL\n");
1485 if (!(SSL_use_certificate_file(ssl,
1487 SSL_FILETYPE_PEM))) {
1488 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1497 coap_log_err(
"*** setup_pki: (D)TLS: No %s Certificate defined\n",
1503 if (!(SSL_use_PrivateKey_file(ssl,
1505 SSL_FILETYPE_PEM))) {
1506 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1507 "Client Private Key\n",
1514 coap_log_err(
"*** setup_pki: (D)TLS: No %s Private Key defined\n",
1523 char *rw_var = NULL;
1524 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1527 STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(setup_data->
pki_key.
key.
pem.
ca_file);
1529 if (cert_names != NULL)
1530 SSL_set_client_CA_list(ssl, cert_names);
1532 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1541 in = BIO_new(BIO_s_file());
1544 if (!BIO_read_filename(in, rw_var)) {
1548 st = SSL_CTX_get_cert_store(ctx);
1550 if ((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL)
1552 add_ca_to_cert_store(st, x);
1564 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1566 if (!cert || !SSL_use_certificate(ssl, cert)) {
1568 "Server PEM Certificate\n");
1580 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1588 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1590 if (!pkey || !SSL_use_PrivateKey(ssl, pkey)) {
1592 "Server PEM Private Key\n");
1596 EVP_PKEY_free(pkey);
1602 EVP_PKEY_free(pkey);
1604 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1612 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1614 X509_STORE *st = SSL_CTX_get_cert_store(ctx);
1618 if ((x = PEM_read_bio_X509(bp, NULL, 0, NULL)) == NULL)
1620 add_ca_to_cert_store(st, x);
1621 SSL_add_client_CA(ssl, x);
1632 if (!(SSL_use_certificate_ASN1(ssl,
1635 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1644 coap_log_err(
"*** setup_pki: (D)TLS: No %s Certificate defined\n",
1651 if (!(SSL_use_PrivateKey_ASN1(pkey_type, ssl,
1654 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1663 coap_log_err(
"*** setup_pki: (D)TLS: No %s Private Key defined",
1673 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1676 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
1677 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1686 st = SSL_CTX_get_cert_store(ctx);
1687 add_ca_to_cert_store(st, x509);
1694 ssl_engine = ENGINE_by_id(
"pkcs11");
1696 coap_log_err(
"*** setup_pki: (D)TLS: No PKCS11 support - need OpenSSL pkcs11 engine\n");
1699 if (!ENGINE_init(ssl_engine)) {
1701 ENGINE_free(ssl_engine);
1703 coap_log_err(
"*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1710 if (ENGINE_ctrl_cmd_string(ssl_engine,
1713 coap_log_warn(
"*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1722 "pkcs11:", 7) == 0) {
1723 EVP_PKEY *pkey = ENGINE_load_private_key(ssl_engine,
1734 if (!SSL_use_PrivateKey(ssl, pkey)) {
1735 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1739 EVP_PKEY_free(pkey);
1742 EVP_PKEY_free(pkey);
1744 if (!(SSL_use_PrivateKey_file(ssl,
1746 SSL_FILETYPE_ASN1))) {
1747 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1755 coap_log_err(
"*** setup_pki: (D)TLS: No Server Private Key defined\n");
1762 "pkcs11:", 7) == 0) {
1765 x509 = missing_ENGINE_load_cert(
1774 if (!SSL_use_certificate(ssl, x509)) {
1775 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1784 if (!(SSL_use_certificate_file(ssl,
1786 SSL_FILETYPE_ASN1))) {
1787 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1795 coap_log_err(
"*** setup_pki: (D)TLS: No Server Certificate defined\n");
1805 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1807 x509 = missing_ENGINE_load_cert(
1811 "%s CA Certificate\n",
1816 if (!SSL_add_client_CA(ssl, x509)) {
1817 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1818 "%s CA Certificate\n",
1824 st = SSL_CTX_get_cert_store(ctx);
1825 add_ca_to_cert_store(st, x509);
1829 X509 *x509 = fp ? d2i_X509_fp(fp, NULL) : NULL;
1830 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1832 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
1833 coap_log_warn(
"*** setup_pki: (D)TLS: %s: Unable to configure "
1841 st = SSL_CTX_get_cert_store(ctx);
1842 add_ca_to_cert_store(st, x509);
1849 coap_log_err(
"*** setup_pki: (D)TLS: Unknown key type %d\n",
1858get_san_or_cn_from_cert(X509 *x509) {
1862 STACK_OF(GENERAL_NAME) *san_list;
1865 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
1867 int san_count = sk_GENERAL_NAME_num(san_list);
1869 for (n = 0; n < san_count; n++) {
1870 const GENERAL_NAME *name = sk_GENERAL_NAME_value(san_list, n);
1872 if (name->type == GEN_DNS) {
1873 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
1876 if (ASN1_STRING_length(name->d.dNSName) != (int)strlen(dns_name))
1878 cn = OPENSSL_strdup(dns_name);
1879 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1883 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1886 X509_NAME_oneline(X509_get_subject_name(x509), buffer,
sizeof(buffer));
1889 n = (int)strlen(buffer) - 3;
1892 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
1893 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
1902 char *ecn = strchr(cn,
'/');
1904 return OPENSSL_strndup(cn, ecn-cn);
1906 return OPENSSL_strdup(cn);
1914tls_verify_call_back(
int preverify_ok, X509_STORE_CTX *ctx) {
1915 SSL *ssl = X509_STORE_CTX_get_ex_data(ctx,
1916 SSL_get_ex_data_X509_STORE_CTX_idx());
1918 coap_openssl_context_t *context =
1921 int depth = X509_STORE_CTX_get_error_depth(ctx);
1922 int err = X509_STORE_CTX_get_error(ctx);
1923 X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
1924 char *cn = get_san_or_cn_from_cert(x509);
1925 int keep_preverify_ok = preverify_ok;
1927 if (!preverify_ok) {
1929 case X509_V_ERR_CERT_NOT_YET_VALID:
1930 case X509_V_ERR_CERT_HAS_EXPIRED:
1934 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1938 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1942 case X509_V_ERR_UNABLE_TO_GET_CRL:
1946 case X509_V_ERR_CRL_NOT_YET_VALID:
1947 case X509_V_ERR_CRL_HAS_EXPIRED:
1951 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1952 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1953 case X509_V_ERR_AKID_SKID_MISMATCH:
1963 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1964 X509_STORE_CTX_set_error(ctx, err);
1966 if (!preverify_ok) {
1967 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1970 "Unknown CA", cn ? cn :
"?", depth);
1974 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1979 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1984 int length = i2d_X509(x509, NULL);
1986 uint8_t *base_buf2 = base_buf = OPENSSL_malloc(length);
1989 i2d_X509(x509, &base_buf2);
1991 depth, preverify_ok,
1994 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1996 X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
2000 OPENSSL_free(base_buf);
2003 return preverify_ok;
2006#if COAP_SERVER_SUPPORT
2007#if OPENSSL_VERSION_NUMBER < 0x10101000L
2016tls_secret_call_back(SSL *ssl,
2019 STACK_OF(SSL_CIPHER) *peer_ciphers,
2024 int psk_requested = 0;
2029 assert(session != NULL);
2030 assert(session->
context != NULL);
2031 if (session == NULL ||
2039 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2040 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2043 SSL_CIPHER_get_name(peer_cipher));
2044 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2050 if (!psk_requested) {
2057 SSL_VERIFY_CLIENT_ONCE |
2058 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2059 tls_verify_call_back);
2061 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2070 X509_VERIFY_PARAM *param;
2072 param = X509_VERIFY_PARAM_new();
2073 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2074 SSL_set1_param(ssl, param);
2075 X509_VERIFY_PARAM_free(param);
2097 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2098 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2112tls_server_name_call_back(SSL *ssl,
2119 return SSL_TLSEXT_ERR_NOACK;
2125 coap_openssl_context_t *context =
2127 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2130 if (!sni || !sni[0]) {
2133 for (i = 0; i < context->sni_count; i++) {
2134 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2138 if (i == context->sni_count) {
2144 return SSL_TLSEXT_ERR_ALERT_FATAL;
2149 ctx = SSL_CTX_new(DTLS_method());
2152 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2153 SSL_CTX_set_app_data(ctx, &context->dtls);
2154 SSL_CTX_set_read_ahead(ctx, 1);
2155 coap_set_user_prefs(ctx);
2156 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2157 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2158 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2159 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2161#if !COAP_DISABLE_TCP
2164 ctx = SSL_CTX_new(TLS_method());
2167 SSL_CTX_set_app_data(ctx, &context->tls);
2168 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2169 coap_set_user_prefs(ctx);
2170 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2171 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2174 sni_setup_data = *setup_data;
2175 sni_setup_data.
pki_key = *new_entry;
2176 setup_pki_server(ctx, &sni_setup_data);
2178 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2179 (context->sni_count+1)*
sizeof(sni_entry));
2180 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2181 context->sni_entry_list[context->sni_count].ctx = ctx;
2182 context->sni_count++;
2184 SSL_set_SSL_CTX(ssl, context->sni_entry_list[i].ctx);
2185 SSL_clear_options(ssl, 0xFFFFFFFFL);
2186 SSL_set_options(ssl, SSL_CTX_get_options(context->sni_entry_list[i].ctx));
2193 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2194 return SSL_TLSEXT_ERR_OK;
2197 return SSL_TLSEXT_ERR_ALERT_WARNING;
2208psk_tls_server_name_call_back(SSL *ssl,
2215 return SSL_TLSEXT_ERR_NOACK;
2221 coap_openssl_context_t *o_context =
2223 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2227 if (!sni || !sni[0]) {
2230 for (i = 0; i < o_context->psk_sni_count; i++) {
2231 if (!strcasecmp(sni, (
char *)o_context->psk_sni_entry_list[i].sni)) {
2235 if (i == o_context->psk_sni_count) {
2242 return SSL_TLSEXT_ERR_ALERT_FATAL;
2247 ctx = SSL_CTX_new(DTLS_method());
2250 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2251 SSL_CTX_set_app_data(ctx, &o_context->dtls);
2252 SSL_CTX_set_read_ahead(ctx, 1);
2253 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2254 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2255 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2256 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2257 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2259#if !COAP_DISABLE_TCP
2262 ctx = SSL_CTX_new(TLS_method());
2265 SSL_CTX_set_app_data(ctx, &o_context->tls);
2266 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2267 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2268 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2269 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2273 o_context->psk_sni_entry_list =
2274 OPENSSL_realloc(o_context->psk_sni_entry_list,
2275 (o_context->psk_sni_count+1)*
sizeof(psk_sni_entry));
2276 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2277 OPENSSL_strdup(sni);
2278 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2280 o_context->psk_sni_entry_list[o_context->psk_sni_count].ctx =
2282 o_context->psk_sni_count++;
2284 SSL_set_SSL_CTX(ssl, o_context->psk_sni_entry_list[i].ctx);
2285 SSL_clear_options(ssl, 0xFFFFFFFFL);
2286 SSL_set_options(ssl,
2287 SSL_CTX_get_options(o_context->psk_sni_entry_list[i].ctx));
2289 &o_context->psk_sni_entry_list[i].psk_info.key);
2290 snprintf(lhint,
sizeof(lhint),
"%.*s",
2291 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2292 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2293 SSL_use_psk_identity_hint(ssl, lhint);
2300 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2301 return SSL_TLSEXT_ERR_OK;
2304 return SSL_TLSEXT_ERR_ALERT_WARNING;
2316tls_client_hello_call_back(SSL *ssl,
2321 coap_openssl_context_t *dtls_context;
2323 int psk_requested = 0;
2324 const unsigned char *out;
2328 *al = SSL_AD_INTERNAL_ERROR;
2329 return SSL_CLIENT_HELLO_ERROR;
2332 assert(session != NULL);
2333 assert(session->
context != NULL);
2335 if (session == NULL ||
2338 *al = SSL_AD_INTERNAL_ERROR;
2339 return SSL_CLIENT_HELLO_ERROR;
2342 setup_data = &dtls_context->setup_data;
2350 size_t len = SSL_client_hello_get0_ciphers(ssl, &out);
2351 STACK_OF(SSL_CIPHER) *peer_ciphers = NULL;
2352 STACK_OF(SSL_CIPHER) *scsvc = NULL;
2354 if (len && SSL_bytes_to_cipher_list(ssl, out, len,
2355 SSL_client_hello_isv2(ssl),
2356 &peer_ciphers, &scsvc)) {
2358 for (ii = 0; ii < sk_SSL_CIPHER_num(peer_ciphers); ii++) {
2359 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2362 "Client cipher: %s (%04x)\n",
2363 SSL_CIPHER_get_name(peer_cipher),
2364 SSL_CIPHER_get_protocol_id(peer_cipher));
2365 if (strstr(SSL_CIPHER_get_name(peer_cipher),
"PSK")) {
2371 sk_SSL_CIPHER_free(peer_ciphers);
2372 sk_SSL_CIPHER_free(scsvc);
2375 if (psk_requested) {
2381 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2387 return SSL_CLIENT_HELLO_SUCCESS;
2397 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_client_certificate_type,
2400 for (ii = 0; ii < outlen; ii++) {
2416 *al = SSL_AD_UNSUPPORTED_EXTENSION;
2417 return SSL_CLIENT_HELLO_ERROR;
2426 coap_openssl_context_t *context =
2428 const char *sni =
"";
2429 char *sni_tmp = NULL;
2432 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2434 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2435 out[2] == TLSEXT_NAMETYPE_host_name &&
2436 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
2440 sni_tmp = OPENSSL_malloc(outlen+1);
2441 sni_tmp[outlen] =
'\000';
2442 memcpy(sni_tmp, out, outlen);
2446 for (i = 0; i < context->sni_count; i++) {
2447 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2451 if (i == context->sni_count) {
2458 *al = SSL_AD_UNRECOGNIZED_NAME;
2459 return SSL_CLIENT_HELLO_ERROR;
2463 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2464 (context->sni_count+1)*
sizeof(sni_entry));
2465 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2466 context->sni_entry_list[context->sni_count].pki_key = *new_entry;
2467 context->sni_count++;
2470 OPENSSL_free(sni_tmp);
2472 sni_setup_data = *setup_data;
2473 sni_setup_data.
pki_key = context->sni_entry_list[i].pki_key;
2485 SSL_VERIFY_CLIENT_ONCE |
2486 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2487 tls_verify_call_back);
2489 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2498 X509_VERIFY_PARAM *param;
2500 param = X509_VERIFY_PARAM_new();
2501 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2502 SSL_set1_param(ssl, param);
2503 X509_VERIFY_PARAM_free(param);
2510 return SSL_CLIENT_HELLO_SUCCESS;
2521psk_tls_client_hello_call_back(SSL *ssl,
2526 coap_openssl_context_t *o_context;
2528 const unsigned char *out;
2534 if (!c_session || !c_session->
context) {
2547 const char *sni =
"";
2548 char *sni_tmp = NULL;
2552 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2554 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2555 out[2] == TLSEXT_NAMETYPE_host_name &&
2556 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
2560 sni_tmp = OPENSSL_malloc(outlen+1);
2562 sni_tmp[outlen] =
'\000';
2563 memcpy(sni_tmp, out, outlen);
2569 for (i = 0; i < o_context->psk_sni_count; i++) {
2570 if (strcasecmp(sni, o_context->psk_sni_entry_list[i].sni) == 0) {
2574 if (i == o_context->psk_sni_count) {
2578 psk_sni_entry *tmp_entry;
2584 *al = SSL_AD_UNRECOGNIZED_NAME;
2585 return SSL_CLIENT_HELLO_ERROR;
2589 OPENSSL_realloc(o_context->psk_sni_entry_list,
2590 (o_context->psk_sni_count+1)*
sizeof(sni_entry));
2592 o_context->psk_sni_entry_list = tmp_entry;
2593 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2594 OPENSSL_strdup(sni);
2595 if (o_context->psk_sni_entry_list[o_context->psk_sni_count].sni) {
2596 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2598 o_context->psk_sni_count++;
2603 OPENSSL_free(sni_tmp);
2606 &o_context->psk_sni_entry_list[i].psk_info.hint)
2611 &o_context->psk_sni_entry_list[i].psk_info.key)
2615 if (o_context->psk_sni_entry_list[i].psk_info.hint.s) {
2616 snprintf(lhint,
sizeof(lhint),
"%.*s",
2617 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2618 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2619 SSL_use_psk_identity_hint(ssl, lhint);
2622 return SSL_CLIENT_HELLO_SUCCESS;
2625 *al = SSL_AD_INTERNAL_ERROR;
2626 return SSL_CLIENT_HELLO_ERROR;
2636 coap_openssl_context_t *context =
2641 context->setup_data = *setup_data;
2642 if (!context->setup_data.verify_peer_cert) {
2646 context->setup_data.allow_self_signed = 1;
2647 context->setup_data.allow_expired_certs = 1;
2648 context->setup_data.cert_chain_validation = 1;
2649 context->setup_data.cert_chain_verify_depth = 10;
2650 context->setup_data.check_cert_revocation = 1;
2651 context->setup_data.allow_no_crl = 1;
2652 context->setup_data.allow_expired_crl = 1;
2653 context->setup_data.allow_bad_md_hash = 1;
2654 context->setup_data.allow_short_rsa_length = 1;
2656#if COAP_SERVER_SUPPORT
2658 if (context->dtls.ctx) {
2660#if OPENSSL_VERSION_NUMBER < 0x10101000L
2661 if (!setup_pki_server(context->dtls.ctx, setup_data))
2670#if OPENSSL_VERSION_NUMBER < 0x10101000L
2671 if (SSLeay() >= 0x10101000L) {
2672 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
2673 "no certificate checking\n",
2674 OPENSSL_VERSION_NUMBER, SSLeay());
2676 SSL_CTX_set_tlsext_servername_arg(context->dtls.ctx, &context->setup_data);
2677 SSL_CTX_set_tlsext_servername_callback(context->dtls.ctx,
2678 tls_server_name_call_back);
2680 SSL_CTX_set_client_hello_cb(context->dtls.ctx,
2681 tls_client_hello_call_back,
2685#if !COAP_DISABLE_TCP
2686 if (context->tls.ctx) {
2688#if OPENSSL_VERSION_NUMBER < 0x10101000L
2689 if (!setup_pki_server(context->tls.ctx, setup_data))
2698#if OPENSSL_VERSION_NUMBER < 0x10101000L
2699 if (SSLeay() >= 0x10101000L) {
2700 coap_log_warn(
"OpenSSL compiled with %lux, linked with %lux, so "
2701 "no certificate checking\n",
2702 OPENSSL_VERSION_NUMBER, SSLeay());
2704 SSL_CTX_set_tlsext_servername_arg(context->tls.ctx, &context->setup_data);
2705 SSL_CTX_set_tlsext_servername_callback(context->tls.ctx,
2706 tls_server_name_call_back);
2708 SSL_CTX_set_client_hello_cb(context->tls.ctx,
2709 tls_client_hello_call_back,
2713 SSL_CTX_set_alpn_select_cb(context->tls.ctx, server_alpn_callback, NULL);
2721 if (!context->dtls.ssl) {
2723 context->dtls.ssl = SSL_new(context->dtls.ctx);
2724 if (!context->dtls.ssl)
2726 bio = BIO_new(context->dtls.meth);
2728 SSL_free(context->dtls.ssl);
2729 context->dtls.ssl = NULL;
2732 SSL_set_bio(context->dtls.ssl, bio, bio);
2733 SSL_set_app_data(context->dtls.ssl, NULL);
2734 SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
2737 context->psk_pki_enabled |= IS_PKI;
2743 const char *ca_file,
2746 coap_openssl_context_t *context =
2748 if (context->dtls.ctx) {
2749 if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) {
2751 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
2755#if !COAP_DISABLE_TCP
2756 if (context->tls.ctx) {
2757 if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) {
2759 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
2769 coap_openssl_context_t *context =
2771 return context->psk_pki_enabled ? 1 : 0;
2778 coap_openssl_context_t *context = (coap_openssl_context_t *)handle;
2780 if (context->dtls.ssl)
2781 SSL_free(context->dtls.ssl);
2782 if (context->dtls.ctx)
2783 SSL_CTX_free(context->dtls.ctx);
2784 if (context->dtls.cookie_hmac)
2785 HMAC_CTX_free(context->dtls.cookie_hmac);
2786 if (context->dtls.meth)
2787 BIO_meth_free(context->dtls.meth);
2788 if (context->dtls.bio_addr)
2789 BIO_ADDR_free(context->dtls.bio_addr);
2790#if !COAP_DISABLE_TCP
2791 if (context->tls.ctx)
2792 SSL_CTX_free(context->tls.ctx);
2793 if (context->tls.meth)
2794 BIO_meth_free(context->tls.meth);
2796 for (i = 0; i < context->sni_count; i++) {
2797 OPENSSL_free(context->sni_entry_list[i].sni);
2798#if OPENSSL_VERSION_NUMBER < 0x10101000L
2799 SSL_CTX_free(context->sni_entry_list[i].ctx);
2802 if (context->sni_count)
2803 OPENSSL_free(context->sni_entry_list);
2804 for (i = 0; i < context->psk_sni_count; i++) {
2805 OPENSSL_free((
char *)context->psk_sni_entry_list[i].sni);
2806#if OPENSSL_VERSION_NUMBER < 0x10101000L
2807 SSL_CTX_free(context->psk_sni_entry_list[i].ctx);
2810 if (context->psk_sni_count)
2811 OPENSSL_free(context->psk_sni_entry_list);
2815#if COAP_SERVER_SUPPORT
2819 SSL *nssl = NULL, *ssl = NULL;
2820 coap_ssl_data *data;
2821 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
2825 nssl = SSL_new(dtls->ctx);
2828 nbio = BIO_new(dtls->meth);
2831 SSL_set_bio(nssl, nbio, nbio);
2832 SSL_set_app_data(nssl, NULL);
2833 SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE);
2834 SSL_set_mtu(nssl, (
long)session->
mtu);
2838 SSL_set_app_data(ssl, session);
2840 data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
2841 data->session = session;
2845 if (psk_hint != NULL && psk_hint->
length) {
2846 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
2849 memcpy(hint, psk_hint->
s, psk_hint->
length);
2850 hint[psk_hint->
length] =
'\000';
2851 SSL_use_psk_identity_hint(ssl, hint);
2858 r = SSL_accept(ssl);
2860 int err = SSL_get_error(ssl, r);
2861 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
2879#if COAP_CLIENT_SUPPORT
2883 coap_openssl_context_t *context =
2886 if (context->psk_pki_enabled & IS_PSK) {
2891 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2895 SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2896#if COAP_SERVER_SUPPORT
2897 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2899 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2902 SSL_set_max_proto_version(ssl, DTLS1_2_VERSION);
2904#if !COAP_DISABLE_TCP
2906 SSL_set_max_proto_version(ssl, TLS1_2_VERSION);
2909 coap_log_debug(
"CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2912 if (context->psk_pki_enabled & IS_PKI) {
2917#if !COAP_DISABLE_TCP
2919 SSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
2924 SSL_set_tlsext_host_name(ssl, setup_data->
client_sni) != 1) {
2930 X509_VERIFY_PARAM *param;
2932 param = X509_VERIFY_PARAM_new();
2933 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2934 SSL_set1_param(ssl, param);
2935 X509_VERIFY_PARAM_free(param);
2942 SSL_VERIFY_CLIENT_ONCE |
2943 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2944 tls_verify_call_back);
2946 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2960 coap_ssl_data *data;
2962 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
2963 coap_dtls_context_t *dtls = &context->dtls;
2965 ssl = SSL_new(dtls->ctx);
2968 bio = BIO_new(dtls->meth);
2971 data = (coap_ssl_data *)BIO_get_data(bio);
2972 data->session = session;
2973 SSL_set_bio(ssl, bio, bio);
2974 SSL_set_app_data(ssl, session);
2975 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2976 SSL_set_mtu(ssl, (
long)session->
mtu);
2978 if (!setup_client_ssl_session(session, ssl))
2983 r = SSL_connect(ssl);
2985 int ret = SSL_get_error(ssl, r);
2986 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3004 SSL *ssl = (SSL *)session->
tls;
3006 SSL_set_mtu(ssl, (
long)session->
mtu);
3012 SSL *ssl = (SSL *)session->
tls;
3014 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3015 int r = SSL_shutdown(ssl);
3017 r = SSL_shutdown(ssl);
3020 session->
tls = NULL;
3028 const uint8_t *data,
size_t data_len) {
3030 SSL *ssl = (SSL *)session->
tls;
3032 assert(ssl != NULL);
3035 r = SSL_write(ssl, data, (
int)data_len);
3038 int err = SSL_get_error(ssl, r);
3039 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3043 if (err == SSL_ERROR_ZERO_RETURN)
3045 else if (err == SSL_ERROR_SSL)
3063 if (r == (ssize_t)data_len)
3086 SSL *ssl = (SSL *)session->
tls;
3087 coap_ssl_data *ssl_data;
3090 ssl_data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
3091 return ssl_data->timeout;
3100 SSL *ssl = (SSL *)session->
tls;
3104 (DTLSv1_handle_timeout(ssl) < 0)) {
3112#if COAP_SERVER_SUPPORT
3115 const uint8_t *data,
size_t data_len) {
3116 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3117 coap_ssl_data *ssl_data;
3120 SSL_set_mtu(dtls->ssl, (
long)session->
mtu);
3121 ssl_data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(dtls->ssl));
3122 assert(ssl_data != NULL);
3123 if (ssl_data->pdu_len) {
3124 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3127 ssl_data->session = session;
3128 ssl_data->pdu = data;
3129 ssl_data->pdu_len = (unsigned)data_len;
3130 r = DTLSv1_listen(dtls->ssl, dtls->bio_addr);
3132 int err = SSL_get_error(dtls->ssl, r);
3133 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3153 coap_ssl_data *ssl_data;
3154 SSL *ssl = (SSL *)session->
tls;
3157 assert(ssl != NULL);
3159 int in_init = SSL_in_init(ssl);
3161 ssl_data = (coap_ssl_data *)BIO_get_data(SSL_get_rbio(ssl));
3162 assert(ssl_data != NULL);
3164 if (ssl_data->pdu_len) {
3165 coap_log_err(
"** %s: Previous data not read %u bytes\n",
3168 ssl_data->pdu = data;
3169 ssl_data->pdu_len = (unsigned)data_len;
3172 r = SSL_read(ssl, pdu, (
int)
sizeof(pdu));
3177 int err = SSL_get_error(ssl, r);
3178 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3179 if (in_init && SSL_is_init_finished(ssl)) {
3187 if (err == SSL_ERROR_ZERO_RETURN)
3189 else if (err == SSL_ERROR_SSL)
3207 if (ssl_data && ssl_data->pdu_len) {
3209 coap_log_debug(
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
3210 ssl_data->pdu_len = 0;
3211 ssl_data->pdu = NULL;
3222 unsigned int overhead = 37;
3223 const SSL_CIPHER *s_ciph = NULL;
3224 if (session->
tls != NULL)
3225 s_ciph = SSL_get_current_cipher(session->
tls);
3227 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
3229 const EVP_CIPHER *e_ciph;
3233 e_ciph = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(s_ciph));
3235 switch (EVP_CIPHER_mode(e_ciph)) {
3236 case EVP_CIPH_GCM_MODE:
3237 ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
3238 maclen = EVP_GCM_TLS_TAG_LEN;
3241 case EVP_CIPH_CCM_MODE:
3242 ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
3243 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3244 if (strstr(cipher,
"CCM8"))
3250 case EVP_CIPH_CBC_MODE:
3251 e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
3252 blocksize = EVP_CIPHER_block_size(e_ciph);
3253 ivlen = EVP_CIPHER_iv_length(e_ciph);
3255 maclen = EVP_MD_size(e_md);
3258 case EVP_CIPH_STREAM_CIPHER:
3265 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3272 overhead = DTLS1_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 + pad;
3277#if !COAP_DISABLE_TCP
3278#if COAP_CLIENT_SUPPORT
3284 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3285 coap_tls_context_t *tls = &context->tls;
3287 ssl = SSL_new(tls->ctx);
3290 bio = BIO_new(tls->meth);
3293 BIO_set_data(bio, session);
3294 SSL_set_bio(ssl, bio, bio);
3295 SSL_set_app_data(ssl, session);
3297 if (!setup_client_ssl_session(session, ssl))
3300 r = SSL_connect(ssl);
3302 int ret = SSL_get_error(ssl, r);
3303 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3305 if (ret == SSL_ERROR_WANT_READ)
3307 if (ret == SSL_ERROR_WANT_WRITE) {
3309#ifdef COAP_EPOLL_SUPPORT
3323 if (SSL_is_init_finished(ssl)) {
3337#if COAP_SERVER_SUPPORT
3342 coap_tls_context_t *tls = &((coap_openssl_context_t *)session->
context->
dtls_context)->tls;
3346 ssl = SSL_new(tls->ctx);
3349 bio = BIO_new(tls->meth);
3352 BIO_set_data(bio, session);
3353 SSL_set_bio(ssl, bio, bio);
3354 SSL_set_app_data(ssl, session);
3357 if (psk_hint != NULL && psk_hint->
length) {
3358 char *hint = OPENSSL_malloc(psk_hint->
length + 1);
3361 memcpy(hint, psk_hint->
s, psk_hint->
length);
3362 hint[psk_hint->
length] =
'\000';
3363 SSL_use_psk_identity_hint(ssl, hint);
3370 r = SSL_accept(ssl);
3372 int err = SSL_get_error(ssl, r);
3373 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3375 if (err == SSL_ERROR_WANT_READ)
3377 if (err == SSL_ERROR_WANT_WRITE) {
3379#ifdef COAP_EPOLL_SUPPORT
3393 if (SSL_is_init_finished(ssl)) {
3409 SSL *ssl = (SSL *)session->
tls;
3411 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3412 int r = SSL_shutdown(ssl);
3414 r = SSL_shutdown(ssl);
3417 session->
tls = NULL;
3430 SSL *ssl = (SSL *)session->
tls;
3436 in_init = !SSL_is_init_finished(ssl);
3438 r = SSL_write(ssl, data, (
int)data_len);
3441 int err = SSL_get_error(ssl, r);
3442 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3443 if (in_init && SSL_is_init_finished(ssl)) {
3449 if (err == SSL_ERROR_WANT_READ)
3451 else if (err == SSL_ERROR_WANT_WRITE) {
3453#ifdef COAP_EPOLL_SUPPORT
3465 if (err == SSL_ERROR_ZERO_RETURN)
3467 else if (err == SSL_ERROR_SSL)
3471 }
else if (in_init && SSL_is_init_finished(ssl)) {
3490 if (r == (ssize_t)data_len)
3507 SSL *ssl = (SSL *)session->
tls;
3515 in_init = !SSL_is_init_finished(ssl);
3517 r = SSL_read(ssl, data, (
int)data_len);
3519 int err = SSL_get_error(ssl, r);
3520 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3521 if (in_init && SSL_is_init_finished(ssl)) {
3527 if (err == SSL_ERROR_WANT_READ)
3529 if (err == SSL_ERROR_WANT_WRITE) {
3531#ifdef COAP_EPOLL_SUPPORT
3541 if (err == SSL_ERROR_ZERO_RETURN)
3543 else if (err == SSL_ERROR_SSL)
3547 }
else if (in_init && SSL_is_init_finished(ssl)) {
3573#if COAP_SERVER_SUPPORT
3576 EVP_MD_CTX *digest_ctx = EVP_MD_CTX_new();
3579 EVP_DigestInit_ex(digest_ctx, EVP_sha256(), NULL);
3586 EVP_MD_CTX_free(digest_ctx);
3591 const uint8_t *data,
3593 return EVP_DigestUpdate(digest_ctx, data, data_len);
3600 int ret = EVP_DigestFinal_ex(digest_ctx, (uint8_t *)digest_buffer, &size);
3607#if COAP_WS_SUPPORT || COAP_OSCORE_SUPPORT
3609coap_crypto_output_errors(
const char *prefix) {
3610#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_WARN
3615 while ((e = ERR_get_error()))
3618 ERR_reason_error_string(e),
3619 ssl_function_definition(e));
3629static struct hash_algs {
3631 const EVP_MD *(*get_hash)(void);
3640static const EVP_MD *
3641get_hash_alg(
cose_alg_t alg,
size_t *length) {
3644 for (idx = 0; idx <
sizeof(hashs) /
sizeof(
struct hash_algs); idx++) {
3645 if (hashs[idx].alg == alg) {
3646 *length = hashs[idx].length;
3647 return hashs[idx].get_hash();
3650 coap_log_debug(
"get_hash_alg: COSE hash %d not supported\n", alg);
3658 unsigned int length;
3659 const EVP_MD *evp_md;
3660 EVP_MD_CTX *evp_ctx = NULL;
3664 if ((evp_md = get_hash_alg(alg, &hash_length)) == NULL) {
3665 coap_log_debug(
"coap_crypto_hash: algorithm %d not supported\n", alg);
3668 evp_ctx = EVP_MD_CTX_new();
3669 if (evp_ctx == NULL)
3671 if (EVP_DigestInit_ex(evp_ctx, evp_md, NULL) == 0)
3674 if (EVP_DigestUpdate(evp_ctx, data->
s, data->
length) == 0)
3680 if (EVP_DigestFinal_ex(evp_ctx,
dummy->s, &length) == 0)
3682 dummy->length = length;
3683 if (hash_length < dummy->length)
3684 dummy->length = hash_length;
3686 EVP_MD_CTX_free(evp_ctx);
3690 coap_crypto_output_errors(
"coap_crypto_hash");
3693 EVP_MD_CTX_free(evp_ctx);
3698#if COAP_OSCORE_SUPPORT
3704#include <openssl/evp.h>
3705#include <openssl/hmac.h>
3712static struct cipher_algs {
3714 const EVP_CIPHER *(*get_cipher)(void);
3719static const EVP_CIPHER *
3723 for (idx = 0; idx <
sizeof(ciphers) /
sizeof(
struct cipher_algs); idx++) {
3724 if (ciphers[idx].alg == alg)
3725 return ciphers[idx].get_cipher();
3727 coap_log_debug(
"get_cipher_alg: COSE cipher %d not supported\n", alg);
3736static struct hmac_algs {
3738 const EVP_MD *(*get_hmac)(void);
3745static const EVP_MD *
3749 for (idx = 0; idx <
sizeof(hmacs) /
sizeof(
struct hmac_algs); idx++) {
3750 if (hmacs[idx].hmac_alg == hmac_alg)
3751 return hmacs[idx].get_hmac();
3753 coap_log_debug(
"get_hmac_alg: COSE HMAC %d not supported\n", hmac_alg);
3759 return get_cipher_alg(alg) != NULL;
3768 return get_hmac_alg(hmac_alg) != NULL;
3772 if (1 != (Func)) { \
3781 size_t *max_result_len) {
3782 const EVP_CIPHER *cipher;
3785 int result_len = (int)(*max_result_len & INT_MAX);
3790 assert(params != NULL);
3791 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
3798 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
3801 C(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL));
3802 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
3803 C(EVP_CIPHER_CTX_ctrl(ctx,
3804 EVP_CTRL_AEAD_SET_IVLEN,
3807 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, NULL));
3808 C(EVP_EncryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
3811 C(EVP_EncryptUpdate(ctx, NULL, &result_len, NULL, (
int)data->
length));
3812 if (aad && aad->
s && (aad->
length > 0)) {
3813 C(EVP_EncryptUpdate(ctx, NULL, &result_len, aad->
s, (
int)aad->
length));
3815 C(EVP_EncryptUpdate(ctx, result, &result_len, data->
s, (
int)data->
length));
3818 C(EVP_EncryptFinal_ex(ctx, result + result_len, &tmp));
3822 C(EVP_CIPHER_CTX_ctrl(ctx,
3823 EVP_CTRL_CCM_GET_TAG,
3825 result + result_len));
3827 *max_result_len = result_len + ccm->
tag_len;
3828 EVP_CIPHER_CTX_free(ctx);
3832 coap_crypto_output_errors(
"coap_crypto_aead_encrypt");
3841 size_t *max_result_len) {
3842 const EVP_CIPHER *cipher;
3852 assert(params != NULL);
3853 if (!params || ((cipher = get_cipher_alg(params->
alg)) == NULL)) {
3865 memcpy(&rwtag, &tag,
sizeof(rwtag));
3868 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
3870 C(EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL));
3871 C(EVP_CIPHER_CTX_ctrl(ctx,
3872 EVP_CTRL_AEAD_SET_IVLEN,
3875 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (
int)ccm->
tag_len, rwtag));
3876 C(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, (
int)ccm->
l, NULL));
3878 C(EVP_DecryptInit_ex(ctx, NULL, NULL, ccm->
key.
s, ccm->
nonce));
3880 C(EVP_DecryptUpdate(ctx, NULL, &len, NULL, (
int)data->
length));
3881 if (aad && aad->
s && (aad->
length > 0)) {
3882 C(EVP_DecryptUpdate(ctx, NULL, &len, aad->
s, (
int)aad->
length));
3884 tmp = EVP_DecryptUpdate(ctx, result, &len, data->
s, (
int)data->
length);
3885 EVP_CIPHER_CTX_free(ctx);
3887 *max_result_len = 0;
3890 *max_result_len = len;
3894 coap_crypto_output_errors(
"coap_crypto_aead_decrypt");
3903 unsigned int result_len;
3904 const EVP_MD *evp_md;
3911 if ((evp_md = get_hmac_alg(hmac_alg)) == 0) {
3912 coap_log_debug(
"coap_crypto_hmac: algorithm %d not supported\n", hmac_alg);
3918 result_len = (
unsigned int)
dummy->length;
3926 dummy->length = result_len;
3931 coap_crypto_output_errors(
"coap_crypto_hmac");
3943#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