61 #include <openssl/ssl.h>
62 #include <openssl/engine.h>
63 #include <openssl/err.h>
64 #include <openssl/rand.h>
65 #include <openssl/hmac.h>
66 #include <openssl/x509v3.h>
68 #ifdef COAP_EPOLL_SUPPORT
69 # include <sys/epoll.h>
72 #if OPENSSL_VERSION_NUMBER < 0x10100000L
73 #error Must be compiled against OpenSSL 1.1.0 or later
77 #define strcasecmp _stricmp
78 #define strncasecmp _strnicmp
82 #ifndef TLSEXT_TYPE_client_certificate_type
83 #define TLSEXT_TYPE_client_certificate_type 19
85 #ifndef TLSEXT_TYPE_server_certificate_type
86 #define TLSEXT_TYPE_server_certificate_type 20
89 #ifndef COAP_OPENSSL_CIPHERS
90 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
91 #define COAP_OPENSSL_CIPHERS "TLSv1.3:TLSv1.2:!NULL"
93 #define COAP_OPENSSL_CIPHERS "TLSv1.2:!NULL"
97 #ifndef COAP_OPENSSL_PSK_CIPHERS
98 #define COAP_OPENSSL_PSK_CIPHERS "PSK:!NULL"
102 typedef struct coap_dtls_context_t {
105 HMAC_CTX *cookie_hmac;
108 } coap_dtls_context_t;
110 typedef struct coap_tls_context_t {
113 } coap_tls_context_t;
118 typedef struct sni_entry {
120 #if OPENSSL_VERSION_NUMBER < 0x10101000L
127 typedef struct psk_sni_entry {
129 #if OPENSSL_VERSION_NUMBER < 0x10101000L
135 typedef struct coap_openssl_context_t {
136 coap_dtls_context_t dtls;
137 #if !COAP_DISABLE_TCP
138 coap_tls_context_t tls;
143 sni_entry *sni_entry_list;
144 size_t psk_sni_count;
145 psk_sni_entry *psk_sni_entry_list;
146 } coap_openssl_context_t;
148 #if OPENSSL_VERSION_NUMBER < 0x10101000L
149 static int psk_tls_server_name_call_back(SSL *ssl,
int *sd,
void *arg);
151 static int psk_tls_client_hello_call_back(SSL *ssl,
int *al,
void *arg);
155 if (SSLeay() < 0x10100000L) {
159 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
167 if (SSLeay() < 0x10101000L) {
176 #if !COAP_DISABLE_TCP
177 if (SSLeay() < 0x10100000L) {
181 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
182 if (SSLeay() < 0x10101000L) {
202 static ENGINE* ssl_engine = NULL;
205 SSL_load_error_strings();
207 ENGINE_load_dynamic();
213 ENGINE_finish(ssl_engine);
215 ENGINE_free(ssl_engine);
226 return c_session->
tls;
241 typedef struct coap_ssl_st {
249 static int coap_dgram_create(BIO *a) {
250 coap_ssl_data *data = NULL;
251 data = malloc(
sizeof(coap_ssl_data));
255 BIO_set_data(a, data);
256 memset(data, 0x00,
sizeof(coap_ssl_data));
260 static int coap_dgram_destroy(BIO *a) {
264 data = (coap_ssl_data *)BIO_get_data(a);
270 static int coap_dgram_read(BIO *a,
char *out,
int outl) {
272 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
275 if (data != NULL && data->pdu_len > 0) {
276 if (outl < (
int)data->pdu_len) {
277 memcpy(out, data->pdu, outl);
280 memcpy(out, data->pdu, data->pdu_len);
281 ret = (int)data->pdu_len;
283 if (!data->peekmode) {
290 BIO_clear_retry_flags(a);
292 BIO_set_retry_read(a);
297 static int coap_dgram_write(BIO *a,
const char *in,
int inl) {
299 coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
302 if (data->session->sock.flags ==
COAP_SOCKET_EMPTY && data->session->endpoint == NULL) {
304 BIO_clear_retry_flags(a);
308 BIO_clear_retry_flags(a);
310 BIO_set_retry_write(a);
312 BIO_clear_retry_flags(a);
318 static int coap_dgram_puts(BIO *a,
const char *pstr) {
319 return coap_dgram_write(a, pstr, (
int)strlen(pstr));
322 static long coap_dgram_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
324 coap_ssl_data *data = BIO_get_data(a);
329 case BIO_CTRL_GET_CLOSE:
330 ret = BIO_get_shutdown(a);
332 case BIO_CTRL_SET_CLOSE:
333 BIO_set_shutdown(a, (
int)num);
336 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
337 data->peekmode = (unsigned)num;
339 case BIO_CTRL_DGRAM_CONNECT:
342 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
343 case BIO_CTRL_DGRAM_GET_MTU:
344 case BIO_CTRL_DGRAM_SET_MTU:
345 case BIO_CTRL_DGRAM_QUERY_MTU:
346 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
351 case BIO_CTRL_DGRAM_MTU_DISCOVER:
352 case BIO_CTRL_DGRAM_SET_CONNECTED:
355 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
356 data->timeout =
coap_ticks_from_rt_us((uint64_t)((
struct timeval*)ptr)->tv_sec * 1000000 + ((
struct timeval*)ptr)->tv_usec);
360 case BIO_C_FILE_SEEK:
361 case BIO_C_FILE_TELL:
363 case BIO_CTRL_PENDING:
364 case BIO_CTRL_WPENDING:
365 case BIO_CTRL_DGRAM_GET_PEER:
366 case BIO_CTRL_DGRAM_SET_PEER:
367 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
368 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
369 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
370 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
371 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
372 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
373 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
374 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
383 coap_dtls_generate_cookie(SSL *ssl,
384 unsigned char *cookie,
385 unsigned int *cookie_len) {
386 coap_dtls_context_t *dtls =
387 (coap_dtls_context_t *)SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl));
388 coap_ssl_data *data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl));
389 int r = HMAC_Init_ex(dtls->cookie_hmac, NULL, 0, NULL, NULL);
390 r &= HMAC_Update(dtls->cookie_hmac,
391 (
const uint8_t*)&data->session->addr_info.local.addr,
392 (
size_t)data->session->addr_info.local.size);
393 r &= HMAC_Update(dtls->cookie_hmac,
394 (
const uint8_t*)&data->session->addr_info.remote.addr,
395 (
size_t)data->session->addr_info.remote.size);
396 r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
401 coap_dtls_verify_cookie(SSL *ssl,
402 const uint8_t *cookie,
403 unsigned int cookie_len) {
406 if (coap_dtls_generate_cookie(ssl, hmac, &len) &&
407 cookie_len == len && memcmp(cookie, hmac, len) == 0)
414 coap_dtls_psk_client_callback(
418 unsigned int max_identity_len,
420 unsigned int max_psk_len
422 size_t hint_len = 0, identity_len = 0, psk_len;
424 coap_openssl_context_t *o_context;
428 if (c_session == NULL || c_session->
context == NULL ||
432 if (o_context == NULL)
441 hint_len = strlen(hint);
452 lhint.
s = (
const uint8_t*)hint;
458 if (psk_info == NULL)
470 memcpy(identity, psk_info->
identity.
s, identity_len);
471 identity[identity_len] =
'\000';
478 memcpy(psk, psk_info->
key.
s, psk_len);
480 return (
unsigned int)psk_len;
483 (
const uint8_t*)hint,
487 max_identity_len - 1,
488 (uint8_t*)psk, max_psk_len);
489 if (identity_len < max_identity_len)
490 identity[identity_len] = 0;
491 return (
unsigned)psk_len;
495 coap_dtls_psk_server_callback(
497 const char *identity,
499 unsigned int max_psk_len
501 size_t identity_len = 0;
506 if (c_session == NULL || c_session->
context == NULL ||
513 identity_len = strlen(identity);
524 (
int)identity_len, identity);
528 lidentity.
length = identity_len;
529 lidentity.
s = (
const uint8_t*)identity;
537 if (psk_key->
length > max_psk_len)
539 memcpy(psk, psk_key->
s, psk_key->
length);
541 return (
unsigned int)psk_key->
length;
545 (
const uint8_t*)identity,
551 static void coap_dtls_info_callback(
const SSL *ssl,
int where,
int ret) {
554 int w = where &~SSL_ST_MASK;
556 if (w & SSL_ST_CONNECT)
557 pstr =
"SSL_connect";
558 else if (w & SSL_ST_ACCEPT)
563 if (where & SSL_CB_LOOP) {
567 }
else if (where & SSL_CB_ALERT) {
569 pstr = (where & SSL_CB_READ) ?
"read" :
"write";
570 if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) {
572 if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY)
576 coap_log(log_level,
"* %s: SSL3 alert %s:%s:%s\n",
579 SSL_alert_type_string_long(ret),
580 SSL_alert_desc_string_long(ret));
581 }
else if (where & SSL_CB_EXIT) {
587 while ((e = ERR_get_error()))
590 ERR_lib_error_string(e), ERR_func_error_string(e));
592 }
else if (ret < 0) {
594 int err = SSL_get_error(ssl, ret);
595 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE && err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT && err != SSL_ERROR_WANT_X509_LOOKUP) {
599 while ((e = ERR_get_error()))
602 ERR_lib_error_string(e), ERR_func_error_string(e));
608 if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
612 #if !COAP_DISABLE_TCP
613 static int coap_sock_create(BIO *a) {
618 static int coap_sock_destroy(BIO *a) {
623 static int coap_sock_read(BIO *a,
char *out,
int outl) {
630 BIO_set_retry_read(a);
633 BIO_clear_retry_flags(a);
639 static int coap_sock_write(BIO *a,
const char *in,
int inl) {
644 BIO_clear_retry_flags(a);
646 BIO_set_retry_read(a);
649 BIO_clear_retry_flags(a);
653 (errno == EPIPE || errno == ECONNRESET)) {
677 static int coap_sock_puts(BIO *a,
const char *pstr) {
678 return coap_sock_write(a, pstr, (
int)strlen(pstr));
681 static long coap_sock_ctrl(BIO *a,
int cmd,
long num,
void *ptr) {
692 case BIO_CTRL_SET_CLOSE:
698 case BIO_CTRL_GET_CLOSE:
706 static void coap_set_user_prefs(SSL_CTX *ctx) {
707 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
709 #ifdef COAP_OPENSSL_SIGALGS
710 SSL_CTX_set1_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
711 SSL_CTX_set1_client_sigalgs_list(ctx, COAP_OPENSSL_SIGALGS);
714 #if OPENSSL_VERSION_NUMBER >= 0x10101000L && defined(COAP_OPENSSL_GROUPS)
715 SSL_CTX_set1_groups_list(ctx, COAP_OPENSSL_GROUPS);
720 coap_openssl_context_t *context;
723 context = (coap_openssl_context_t *)
coap_malloc(
sizeof(coap_openssl_context_t));
725 uint8_t cookie_secret[32];
727 memset(context, 0,
sizeof(coap_openssl_context_t));
730 context->dtls.ctx = SSL_CTX_new(DTLS_method());
731 if (!context->dtls.ctx)
733 SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
734 SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
735 SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
736 coap_set_user_prefs(context->dtls.ctx);
737 memset(cookie_secret, 0,
sizeof(cookie_secret));
738 if (!RAND_bytes(cookie_secret, (
int)
sizeof(cookie_secret))) {
741 "Insufficient entropy for random cookie generation");
742 coap_prng(cookie_secret,
sizeof(cookie_secret));
744 context->dtls.cookie_hmac = HMAC_CTX_new();
745 if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (
int)
sizeof(cookie_secret), EVP_sha256(), NULL))
747 SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
748 SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
749 SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
750 SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
751 context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM,
"coapdgram");
752 if (!context->dtls.meth)
754 context->dtls.bio_addr = BIO_ADDR_new();
755 if (!context->dtls.bio_addr)
757 BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
758 BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
759 BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
760 BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
761 BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
762 BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
764 #if !COAP_DISABLE_TCP
766 context->tls.ctx = SSL_CTX_new(TLS_method());
767 if (!context->tls.ctx)
769 SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
770 SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
771 coap_set_user_prefs(context->tls.ctx);
772 SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
773 context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET,
"coapsock");
774 if (!context->tls.meth)
776 BIO_meth_set_write(context->tls.meth, coap_sock_write);
777 BIO_meth_set_read(context->tls.meth, coap_sock_read);
778 BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
779 BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
780 BIO_meth_set_create(context->tls.meth, coap_sock_create);
781 BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
796 coap_openssl_context_t *o_context =
800 if (!setup_data || !o_context)
803 SSL_CTX_set_psk_server_callback(o_context->dtls.ctx,
804 coap_dtls_psk_server_callback);
805 #if !COAP_DISABLE_TCP
806 SSL_CTX_set_psk_server_callback(o_context->tls.ctx,
807 coap_dtls_psk_server_callback);
813 SSL_CTX_use_psk_identity_hint(o_context->dtls.ctx, hint);
814 #if !COAP_DISABLE_TCP
815 SSL_CTX_use_psk_identity_hint(o_context->tls.ctx, hint);
819 #if OPENSSL_VERSION_NUMBER < 0x10101000L
820 SSL_CTX_set_tlsext_servername_arg(o_context->dtls.ctx,
822 SSL_CTX_set_tlsext_servername_callback(o_context->dtls.ctx,
823 psk_tls_server_name_call_back);
824 #if !COAP_DISABLE_TCP
825 SSL_CTX_set_tlsext_servername_arg(o_context->tls.ctx,
827 SSL_CTX_set_tlsext_servername_callback(o_context->tls.ctx,
828 psk_tls_server_name_call_back);
831 SSL_CTX_set_client_hello_cb(o_context->dtls.ctx,
832 psk_tls_client_hello_call_back,
834 #if !COAP_DISABLE_TCP
835 SSL_CTX_set_client_hello_cb(o_context->tls.ctx,
836 psk_tls_client_hello_call_back,
842 if (!o_context->dtls.ssl) {
844 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
845 if (!o_context->dtls.ssl)
847 bio = BIO_new(o_context->dtls.meth);
849 SSL_free (o_context->dtls.ssl);
850 o_context->dtls.ssl = NULL;
853 SSL_set_bio(o_context->dtls.ssl, bio, bio);
854 SSL_set_app_data(o_context->dtls.ssl, NULL);
855 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
858 o_context->psk_pki_enabled |= IS_PSK;
866 coap_openssl_context_t *o_context =
870 if (!setup_data || !o_context)
873 if (!o_context->dtls.ssl) {
875 o_context->dtls.ssl = SSL_new(o_context->dtls.ctx);
876 if (!o_context->dtls.ssl)
878 bio = BIO_new(o_context->dtls.meth);
880 SSL_free (o_context->dtls.ssl);
881 o_context->dtls.ssl = NULL;
884 SSL_set_bio(o_context->dtls.ssl, bio, bio);
885 SSL_set_app_data(o_context->dtls.ssl, NULL);
886 SSL_set_options(o_context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
889 o_context->psk_pki_enabled |= IS_PSK;
894 map_key_type(
int asn1_private_key_type
896 switch (asn1_private_key_type) {
914 "*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
915 asn1_private_key_type);
920 #if !COAP_DISABLE_TCP
921 static uint8_t coap_alpn[] = { 4,
'c',
'o',
'a',
'p' };
925 const unsigned char **out,
926 unsigned char *outlen,
927 const unsigned char *in,
931 unsigned char *tout = NULL;
934 return SSL_TLSEXT_ERR_NOACK;
935 ret = SSL_select_next_proto(&tout,
942 return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
947 add_ca_to_cert_store(X509_STORE *st, X509 *x509)
952 while ((e = ERR_get_error()) != 0) {
955 if (!X509_STORE_add_cert(st, x509)) {
956 while ((e = ERR_get_error()) != 0) {
957 int r = ERR_GET_REASON(e);
958 if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
961 ERR_reason_error_string(e),
962 ERR_lib_error_string(e),
963 ERR_func_error_string(e));
970 missing_ENGINE_load_cert (
const char *cert_id)
977 params.cert_id = cert_id;
981 if (!ENGINE_ctrl_cmd(ssl_engine,
"LOAD_CERT_CTRL", 0, ¶ms, NULL, 1)) {
987 #if OPENSSL_VERSION_NUMBER < 0x10101000L
989 setup_pki_server(SSL_CTX *ctx,
996 if (!(SSL_CTX_use_certificate_file(ctx,
998 SSL_FILETYPE_PEM))) {
1000 "*** setup_pki: (D)TLS: %s: Unable to configure "
1001 "Server Certificate\n",
1008 "*** setup_pki: (D)TLS: No Server Certificate defined\n");
1014 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1016 SSL_FILETYPE_PEM))) {
1018 "*** setup_pki: (D)TLS: %s: Unable to configure "
1019 "Server Private Key\n",
1026 "*** setup_pki: (D)TLS: No Server Private Key defined\n");
1032 STACK_OF(X509_NAME) *cert_names;
1036 char *rw_var = NULL;
1038 if (cert_names != NULL)
1039 SSL_CTX_set_client_CA_list(ctx, cert_names);
1042 "*** setup_pki: (D)TLS: %s: Unable to configure "
1049 st = SSL_CTX_get_cert_store(ctx);
1050 in = BIO_new(BIO_s_file());
1053 if (!BIO_read_filename(in, rw_var)) {
1060 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
1062 add_ca_to_cert_store(st, x);
1074 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1076 if (!cert || !SSL_CTX_use_certificate(ctx, cert)) {
1078 "*** setup_pki: (D)TLS: Unable to configure "
1079 "Server PEM Certificate\n");
1080 if (bp) BIO_free(bp);
1081 if (cert) X509_free(cert);
1084 if (bp) BIO_free(bp);
1085 if (cert) X509_free(cert);
1089 "*** setup_pki: (D)TLS: No Server Certificate defined\n");
1097 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1099 if (!pkey || !SSL_CTX_use_PrivateKey(ctx, pkey)) {
1101 "*** setup_pki: (D)TLS: Unable to configure "
1102 "Server PEM Private Key\n");
1103 if (bp) BIO_free(bp);
1104 if (pkey) EVP_PKEY_free(pkey);
1107 if (bp) BIO_free(bp);
1108 if (pkey) EVP_PKEY_free(pkey);
1112 "*** setup_pki: (D)TLS: No Server Private Key defined\n");
1123 st = SSL_CTX_get_cert_store(ctx);
1126 if ((x = PEM_read_bio_X509(bp, NULL, NULL, NULL)) == NULL)
1128 add_ca_to_cert_store(st, x);
1129 SSL_CTX_add_client_CA(ctx, x);
1140 if (!(SSL_CTX_use_certificate_ASN1(ctx,
1144 "*** setup_pki: (D)TLS: %s: Unable to configure "
1145 "Server Certificate\n",
1152 "*** setup_pki: (D)TLS: No Server Certificate defined\n");
1159 if (!(SSL_CTX_use_PrivateKey_ASN1(pkey_type, ctx,
1163 "*** setup_pki: (D)TLS: %s: Unable to configure "
1164 "Server Private Key\n",
1171 "*** setup_pki: (D)TLS: No Server Private Key defined\n");
1181 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1183 "*** setup_pki: (D)TLS: %s: Unable to configure "
1186 if (x509) X509_free(x509);
1189 st = SSL_CTX_get_cert_store(ctx);
1190 add_ca_to_cert_store(st, x509);
1197 ssl_engine = ENGINE_by_id(
"pkcs11");
1200 "*** setup_pki: (D)TLS: No PKCS11 support\nn");
1203 if (!ENGINE_init(ssl_engine)) {
1205 ENGINE_free(ssl_engine);
1208 "*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1215 if (ENGINE_ctrl_cmd_string(ssl_engine,
"PIN",
1218 "*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1227 "pkcs11:", 7) == 0) {
1228 EVP_PKEY* pkey = ENGINE_load_private_key(ssl_engine,
1234 "*** setup_pki: (D)TLS: %s: Unable to load "
1235 "Server Private Key\n",
1239 if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
1241 "*** setup_pki: (D)TLS: %s: Unable to configure "
1242 "Server Private Key\n",
1244 EVP_PKEY_free(pkey);
1247 EVP_PKEY_free(pkey);
1250 if (!(SSL_CTX_use_PrivateKey_file(ctx,
1252 SSL_FILETYPE_ASN1))) {
1254 "*** setup_pki: (D)TLS: %s: Unable to configure "
1255 "Server Private Key\n",
1263 "*** setup_pki: (D)TLS: No Server Private Key defined\n");
1270 "pkcs11:", 7) == 0) {
1273 x509 = missing_ENGINE_load_cert(
1277 "*** setup_pki: (D)TLS: %s: Unable to load "
1278 "Server Certificate\n",
1282 if (!SSL_CTX_use_certificate(ctx, x509)) {
1284 "*** setup_pki: (D)TLS: %s: Unable to configure "
1285 "Server Certificate\n",
1293 if (!(SSL_CTX_use_certificate_file(ctx,
1295 SSL_FILETYPE_ASN1))) {
1297 "*** setup_pki: (D)TLS: %s: Unable to configure "
1298 "Server Certificate\n",
1306 "*** setup_pki: (D)TLS: No Server Certificate defined\n");
1317 x509 = missing_ENGINE_load_cert (
1321 "*** setup_pki: (D)TLS: %s: Unable to load "
1322 "Server CA Certificate\n",
1326 if (!SSL_CTX_add_client_CA(ctx, x509)) {
1328 "*** setup_pki: (D)TLS: %s: Unable to configure "
1334 st = SSL_CTX_get_cert_store(ctx);
1335 add_ca_to_cert_store(st, x509);
1340 X509 *x509 = fp ? d2i_X509_fp(fp, NULL) : NULL;
1342 if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
1344 "*** setup_pki: (D)TLS: %s: Unable to configure "
1347 if (x509) X509_free(x509);
1350 st = SSL_CTX_get_cert_store(ctx);
1351 add_ca_to_cert_store(st, x509);
1359 "*** setup_pki: (D)TLS: Unknown key type %d\n",
1369 setup_pki_ssl(SSL *ssl,
1374 "RPK Support not available in OpenSSL\n");
1381 if (!(SSL_use_certificate_file(ssl,
1383 SSL_FILETYPE_PEM))) {
1385 "*** setup_pki: (D)TLS: %s: Unable to configure "
1396 "*** setup_pki: (D)TLS: No %s Certificate defined\n",
1402 if (!(SSL_use_PrivateKey_file(ssl,
1404 SSL_FILETYPE_PEM))) {
1406 "*** setup_pki: (D)TLS: %s: Unable to configure "
1407 "Client Private Key\n",
1416 "*** setup_pki: (D)TLS: No %s Private Key defined\n",
1425 char *rw_var = NULL;
1426 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1429 STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(setup_data->
pki_key.
key.
pem.
ca_file);
1431 if (cert_names != NULL)
1432 SSL_set_client_CA_list(ssl, cert_names);
1435 "*** setup_pki: (D)TLS: %s: Unable to configure "
1444 in = BIO_new(BIO_s_file());
1447 if (!BIO_read_filename(in, rw_var)) {
1452 st = SSL_CTX_get_cert_store(ctx);
1454 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
1456 add_ca_to_cert_store(st, x);
1468 X509 *cert = bp ? PEM_read_bio_X509(bp, NULL, 0, NULL) : NULL;
1470 if (!cert || !SSL_use_certificate(ssl, cert)) {
1472 "*** setup_pki: (D)TLS: Unable to configure "
1473 "Server PEM Certificate\n");
1474 if (bp) BIO_free(bp);
1475 if (cert) X509_free(cert);
1478 if (bp) BIO_free(bp);
1479 if (cert) X509_free(cert);
1483 "*** setup_pki: (D)TLS: No Server Certificate defined\n");
1491 EVP_PKEY *pkey = bp ? PEM_read_bio_PrivateKey(bp, NULL, 0, NULL) : NULL;
1493 if (!pkey || !SSL_use_PrivateKey(ssl, pkey)) {
1495 "*** setup_pki: (D)TLS: Unable to configure "
1496 "Server PEM Private Key\n");
1497 if (bp) BIO_free(bp);
1498 if (pkey) EVP_PKEY_free(pkey);
1501 if (bp) BIO_free(bp);
1502 if (pkey) EVP_PKEY_free(pkey);
1506 "*** setup_pki: (D)TLS: No Server Private Key defined\n");
1514 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1516 X509_STORE *st = SSL_CTX_get_cert_store(ctx);
1520 if ((x = PEM_read_bio_X509(bp, NULL, 0, NULL)) == NULL)
1522 add_ca_to_cert_store(st, x);
1523 SSL_add_client_CA(ssl, x);
1534 if (!(SSL_use_certificate_ASN1(ssl,
1538 "*** setup_pki: (D)TLS: %s: Unable to configure "
1549 "*** setup_pki: (D)TLS: No %s Certificate defined\n",
1556 if (!(SSL_use_PrivateKey_ASN1(pkey_type, ssl,
1560 "*** setup_pki: (D)TLS: %s: Unable to configure "
1571 "*** setup_pki: (D)TLS: No %s Private Key defined",
1581 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1584 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
1586 "*** setup_pki: (D)TLS: %s: Unable to configure "
1595 st = SSL_CTX_get_cert_store(ctx);
1596 add_ca_to_cert_store(st, x509);
1603 ssl_engine = ENGINE_by_id(
"pkcs11");
1606 "*** setup_pki: (D)TLS: No PKCS11 support - need OpenSSL pkcs11 engine\n");
1609 if (!ENGINE_init(ssl_engine)) {
1611 ENGINE_free(ssl_engine);
1614 "*** setup_pki: (D)TLS: PKCS11 engine initialize failed\n");
1621 if (ENGINE_ctrl_cmd_string(ssl_engine,
1625 "*** setup_pki: (D)TLS: PKCS11: %s: Unable to set pin\n",
1634 "pkcs11:", 7) == 0) {
1635 EVP_PKEY* pkey = ENGINE_load_private_key(ssl_engine,
1641 "*** setup_pki: (D)TLS: %s: Unable to load "
1647 if (!SSL_use_PrivateKey(ssl, pkey)) {
1649 "*** setup_pki: (D)TLS: %s: Unable to configure "
1653 EVP_PKEY_free(pkey);
1656 EVP_PKEY_free(pkey);
1659 if (!(SSL_use_PrivateKey_file(ssl,
1661 SSL_FILETYPE_ASN1))) {
1663 "*** setup_pki: (D)TLS: %s: Unable to configure "
1673 "*** setup_pki: (D)TLS: No Server Private Key defined\n");
1680 "pkcs11:", 7) == 0) {
1683 x509 = missing_ENGINE_load_cert(
1687 "*** setup_pki: (D)TLS: %s: Unable to load "
1693 if (!SSL_use_certificate(ssl, x509)) {
1695 "*** setup_pki: (D)TLS: %s: Unable to configure "
1705 if (!(SSL_use_certificate_file(ssl,
1707 SSL_FILETYPE_ASN1))) {
1709 "*** setup_pki: (D)TLS: %s: Unable to configure "
1719 "*** setup_pki: (D)TLS: No Server Certificate defined\n");
1729 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1731 x509 = missing_ENGINE_load_cert(
1735 "*** setup_pki: (D)TLS: %s: Unable to load "
1736 "%s CA Certificate\n",
1741 if (!SSL_add_client_CA(ssl, x509)) {
1743 "*** setup_pki: (D)TLS: %s: Unable to configure "
1744 "%s CA Certificate\n",
1750 st = SSL_CTX_get_cert_store(ctx);
1751 add_ca_to_cert_store(st, x509);
1756 X509 *x509 = fp ? d2i_X509_fp(fp, NULL) : NULL;
1757 SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
1759 if (!x509 || !SSL_add_client_CA(ssl, x509)) {
1761 "*** setup_pki: (D)TLS: %s: Unable to configure "
1765 if (x509) X509_free(x509);
1768 st = SSL_CTX_get_cert_store(ctx);
1769 add_ca_to_cert_store(st, x509);
1777 "*** setup_pki: (D)TLS: Unknown key type %d\n",
1785 get_san_or_cn_from_cert(X509* x509) {
1789 STACK_OF(GENERAL_NAME) *san_list;
1792 san_list = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
1794 int san_count = sk_GENERAL_NAME_num(san_list);
1796 for (
n = 0;
n < san_count;
n++) {
1797 const GENERAL_NAME * name = sk_GENERAL_NAME_value (san_list,
n);
1799 if (name->type == GEN_DNS) {
1800 const char *dns_name = (
const char *)ASN1_STRING_get0_data(name->d.dNSName);
1803 if (ASN1_STRING_length(name->d.dNSName) != (int)strlen (dns_name))
1805 cn = OPENSSL_strdup(dns_name);
1806 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1810 sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1813 X509_NAME_oneline(X509_get_subject_name(x509), buffer,
sizeof(buffer));
1816 n = (int)strlen(buffer) - 3;
1819 if (((cn[0] ==
'C') || (cn[0] ==
'c')) &&
1820 ((cn[1] ==
'N') || (cn[1] ==
'n')) &&
1829 char * ecn = strchr(cn,
'/');
1831 return OPENSSL_strndup(cn, ecn-cn);
1834 return OPENSSL_strdup(cn);
1842 tls_verify_call_back(
int preverify_ok, X509_STORE_CTX *ctx) {
1843 SSL *ssl = X509_STORE_CTX_get_ex_data(ctx,
1844 SSL_get_ex_data_X509_STORE_CTX_idx());
1846 coap_openssl_context_t *context =
1849 int depth = X509_STORE_CTX_get_error_depth(ctx);
1850 int err = X509_STORE_CTX_get_error(ctx);
1851 X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
1852 char *cn = get_san_or_cn_from_cert(x509);
1853 int keep_preverify_ok = preverify_ok;
1855 if (!preverify_ok) {
1857 case X509_V_ERR_CERT_NOT_YET_VALID:
1858 case X509_V_ERR_CERT_HAS_EXPIRED:
1862 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1866 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1870 case X509_V_ERR_UNABLE_TO_GET_CRL:
1874 case X509_V_ERR_CRL_NOT_YET_VALID:
1875 case X509_V_ERR_CRL_HAS_EXPIRED:
1879 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1880 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1890 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1891 X509_STORE_CTX_set_error(ctx, err);
1893 if (!preverify_ok) {
1894 if (err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
1896 " %s: %s: '%s' depth=%d\n",
1898 "Unknown CA", cn ? cn :
"?", depth);
1902 " %s: %s: '%s' depth=%d\n",
1904 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1909 " %s: %s: overridden: '%s' depth=%d\n",
1911 X509_verify_cert_error_string(err), cn ? cn :
"?", depth);
1916 int length = i2d_X509(x509, NULL);
1918 uint8_t *base_buf2 = base_buf = OPENSSL_malloc(length);
1921 i2d_X509(x509, &base_buf2);
1923 depth, preverify_ok,
1926 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1929 X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1933 OPENSSL_free(base_buf);
1936 return preverify_ok;
1939 #if OPENSSL_VERSION_NUMBER < 0x10101000L
1948 tls_secret_call_back(SSL *ssl,
1951 STACK_OF(SSL_CIPHER) *peer_ciphers,
1956 int psk_requested = 0;
1961 assert(session != NULL);
1962 assert(session->
context != NULL);
1963 if (session == NULL ||
1971 for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) {
1972 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
1975 SSL_CIPHER_get_name(peer_cipher));
1976 if (strstr (SSL_CIPHER_get_name (peer_cipher),
"PSK")) {
1982 if (!psk_requested) {
1989 SSL_VERIFY_CLIENT_ONCE |
1990 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1991 tls_verify_call_back);
1994 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2003 X509_VERIFY_PARAM *param;
2005 param = X509_VERIFY_PARAM_new();
2006 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2007 SSL_set1_param(ssl, param);
2008 X509_VERIFY_PARAM_free(param);
2032 SSL_set_cipher_list (ssl, COAP_OPENSSL_PSK_CIPHERS);
2033 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2047 tls_server_name_call_back(SSL *ssl,
2054 return SSL_TLSEXT_ERR_NOACK;
2060 coap_openssl_context_t *context =
2062 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2065 if (!sni || !sni[0]) {
2068 for (i = 0; i < context->sni_count; i++) {
2069 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2073 if (i == context->sni_count) {
2079 return SSL_TLSEXT_ERR_ALERT_FATAL;
2084 ctx = SSL_CTX_new(DTLS_method());
2087 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2088 SSL_CTX_set_app_data(ctx, &context->dtls);
2089 SSL_CTX_set_read_ahead(ctx, 1);
2090 coap_set_user_prefs(ctx);
2091 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2092 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2093 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2094 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2096 #if !COAP_DISABLE_TCP
2099 ctx = SSL_CTX_new(TLS_method());
2102 SSL_CTX_set_app_data(ctx, &context->tls);
2103 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2104 coap_set_user_prefs(ctx);
2105 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2106 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2109 sni_setup_data = *setup_data;
2110 sni_setup_data.
pki_key = *new_entry;
2111 setup_pki_server(ctx, &sni_setup_data);
2113 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2114 (context->sni_count+1)*
sizeof(sni_entry));
2115 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2116 context->sni_entry_list[context->sni_count].ctx = ctx;
2117 context->sni_count++;
2119 SSL_set_SSL_CTX (ssl, context->sni_entry_list[i].ctx);
2120 SSL_clear_options (ssl, 0xFFFFFFFFL);
2121 SSL_set_options (ssl, SSL_CTX_get_options (context->sni_entry_list[i].ctx));
2128 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2129 return SSL_TLSEXT_ERR_OK;
2132 return SSL_TLSEXT_ERR_ALERT_WARNING;
2143 psk_tls_server_name_call_back(SSL *ssl,
2150 return SSL_TLSEXT_ERR_NOACK;
2156 coap_openssl_context_t *o_context =
2158 const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
2162 if (!sni || !sni[0]) {
2165 for (i = 0; i < o_context->psk_sni_count; i++) {
2166 if (!strcasecmp(sni, (
char*)o_context->psk_sni_entry_list[i].sni)) {
2170 if (i == o_context->psk_sni_count) {
2177 return SSL_TLSEXT_ERR_ALERT_FATAL;
2182 ctx = SSL_CTX_new(DTLS_method());
2185 SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
2186 SSL_CTX_set_app_data(ctx, &o_context->dtls);
2187 SSL_CTX_set_read_ahead(ctx, 1);
2188 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2189 SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
2190 SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
2191 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2192 SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
2194 #if !COAP_DISABLE_TCP
2197 ctx = SSL_CTX_new(TLS_method());
2200 SSL_CTX_set_app_data(ctx, &o_context->tls);
2201 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2202 SSL_CTX_set_cipher_list(ctx, COAP_OPENSSL_CIPHERS);
2203 SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
2204 SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
2208 o_context->psk_sni_entry_list =
2209 OPENSSL_realloc(o_context->psk_sni_entry_list,
2210 (o_context->psk_sni_count+1)*
sizeof(psk_sni_entry));
2211 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2212 OPENSSL_strdup(sni);
2213 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2215 o_context->psk_sni_entry_list[o_context->psk_sni_count].ctx =
2217 o_context->psk_sni_count++;
2219 SSL_set_SSL_CTX (ssl, o_context->psk_sni_entry_list[i].ctx);
2220 SSL_clear_options (ssl, 0xFFFFFFFFL);
2221 SSL_set_options (ssl,
2222 SSL_CTX_get_options (o_context->psk_sni_entry_list[i].ctx));
2224 &o_context->psk_sni_entry_list[i].psk_info.key);
2225 snprintf(lhint,
sizeof(lhint),
"%.*s",
2226 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2227 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2228 SSL_use_psk_identity_hint(ssl, lhint);
2235 SSL_set_session_secret_cb(ssl, tls_secret_call_back, arg);
2236 return SSL_TLSEXT_ERR_OK;
2239 return SSL_TLSEXT_ERR_ALERT_WARNING;
2251 tls_client_hello_call_back(SSL *ssl,
2256 coap_openssl_context_t *dtls_context;
2258 int psk_requested = 0;
2259 const unsigned char *out;
2263 *al = SSL_AD_INTERNAL_ERROR;
2264 return SSL_CLIENT_HELLO_ERROR;
2267 assert(session != NULL);
2268 assert(session->
context != NULL);
2270 if (session == NULL ||
2273 *al = SSL_AD_INTERNAL_ERROR;
2274 return SSL_CLIENT_HELLO_ERROR;
2277 setup_data = &dtls_context->setup_data;
2285 size_t len = SSL_client_hello_get0_ciphers(ssl, &out);
2286 STACK_OF(SSL_CIPHER) *peer_ciphers = NULL;
2287 STACK_OF(SSL_CIPHER) *scsvc = NULL;
2289 if (len && SSL_bytes_to_cipher_list(ssl, out, len,
2290 SSL_client_hello_isv2(ssl),
2291 &peer_ciphers, &scsvc)) {
2293 for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) {
2294 const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
2297 SSL_CIPHER_get_name(peer_cipher),
2298 SSL_CIPHER_get_protocol_id(peer_cipher));
2299 if (strstr (SSL_CIPHER_get_name (peer_cipher),
"PSK")) {
2305 sk_SSL_CIPHER_free(peer_ciphers);
2306 sk_SSL_CIPHER_free(scsvc);
2309 if (psk_requested) {
2315 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2321 return SSL_CLIENT_HELLO_SUCCESS;
2331 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_client_certificate_type,
2334 for (ii = 0; ii < outlen; ii++) {
2350 *al = SSL_AD_UNSUPPORTED_EXTENSION;
2351 return SSL_CLIENT_HELLO_ERROR;
2360 coap_openssl_context_t *context =
2362 const char *sni =
"";
2363 char *sni_tmp = NULL;
2366 if (SSL_client_hello_get0_ext (ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2368 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2369 out[2] == TLSEXT_NAMETYPE_host_name &&
2370 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
2374 sni_tmp = OPENSSL_malloc(outlen+1);
2375 sni_tmp[outlen] =
'\000';
2376 memcpy(sni_tmp, out, outlen);
2380 for (i = 0; i < context->sni_count; i++) {
2381 if (!strcasecmp(sni, context->sni_entry_list[i].sni)) {
2385 if (i == context->sni_count) {
2392 *al = SSL_AD_UNRECOGNIZED_NAME;
2393 return SSL_CLIENT_HELLO_ERROR;
2397 context->sni_entry_list = OPENSSL_realloc(context->sni_entry_list,
2398 (context->sni_count+1)*
sizeof(sni_entry));
2399 context->sni_entry_list[context->sni_count].sni = OPENSSL_strdup(sni);
2400 context->sni_entry_list[context->sni_count].pki_key = *new_entry;
2401 context->sni_count++;
2404 OPENSSL_free(sni_tmp);
2406 sni_setup_data = *setup_data;
2407 sni_setup_data.
pki_key = context->sni_entry_list[i].pki_key;
2408 setup_pki_ssl(ssl, &sni_setup_data, 1);
2411 setup_pki_ssl(ssl, setup_data, 1);
2420 SSL_VERIFY_CLIENT_ONCE |
2421 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2422 tls_verify_call_back);
2425 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2434 X509_VERIFY_PARAM *param;
2436 param = X509_VERIFY_PARAM_new();
2437 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2438 SSL_set1_param(ssl, param);
2439 X509_VERIFY_PARAM_free(param);
2446 return SSL_CLIENT_HELLO_SUCCESS;
2457 psk_tls_client_hello_call_back(SSL *ssl,
2462 coap_openssl_context_t *o_context;
2464 const unsigned char *out;
2470 if (!c_session || !c_session->
context) {
2483 const char *sni =
"";
2484 char *sni_tmp = NULL;
2488 if (SSL_client_hello_get0_ext (ssl, TLSEXT_TYPE_server_name, &out, &outlen) &&
2490 (((out[0]<<8) + out[1] +2) == (int)outlen) &&
2491 out[2] == TLSEXT_NAMETYPE_host_name &&
2492 (((out[3]<<8) + out[4] +2 +3) == (int)outlen)) {
2496 sni_tmp = OPENSSL_malloc(outlen+1);
2498 sni_tmp[outlen] =
'\000';
2499 memcpy(sni_tmp, out, outlen);
2505 for (i = 0; i < o_context->psk_sni_count; i++) {
2506 if (strcasecmp(sni, o_context->psk_sni_entry_list[i].sni) == 0) {
2510 if (i == o_context->psk_sni_count) {
2514 psk_sni_entry *tmp_entry;
2520 *al = SSL_AD_UNRECOGNIZED_NAME;
2521 return SSL_CLIENT_HELLO_ERROR;
2525 OPENSSL_realloc(o_context->psk_sni_entry_list,
2526 (o_context->psk_sni_count+1)*
sizeof(sni_entry));
2528 o_context->psk_sni_entry_list = tmp_entry;
2529 o_context->psk_sni_entry_list[o_context->psk_sni_count].sni =
2530 OPENSSL_strdup(sni);
2531 if (o_context->psk_sni_entry_list[o_context->psk_sni_count].sni) {
2532 o_context->psk_sni_entry_list[o_context->psk_sni_count].psk_info =
2534 o_context->psk_sni_count++;
2539 OPENSSL_free(sni_tmp);
2542 &o_context->psk_sni_entry_list[i].psk_info.hint)
2547 &o_context->psk_sni_entry_list[i].psk_info.key)
2551 if (o_context->psk_sni_entry_list[i].psk_info.hint.s) {
2552 snprintf(lhint,
sizeof(lhint),
"%.*s",
2553 (
int)o_context->psk_sni_entry_list[i].psk_info.hint.length,
2554 o_context->psk_sni_entry_list[i].psk_info.hint.s);
2555 SSL_use_psk_identity_hint(ssl, lhint);
2558 return SSL_CLIENT_HELLO_SUCCESS;
2561 *al = SSL_AD_INTERNAL_ERROR;
2562 return SSL_CLIENT_HELLO_ERROR;
2571 coap_openssl_context_t *context =
2576 context->setup_data = *setup_data;
2577 if (!context->setup_data.verify_peer_cert) {
2581 context->setup_data.allow_self_signed = 1;
2582 context->setup_data.allow_expired_certs = 1;
2583 context->setup_data.cert_chain_validation = 1;
2584 context->setup_data.cert_chain_verify_depth = 10;
2585 context->setup_data.check_cert_revocation = 1;
2586 context->setup_data.allow_no_crl = 1;
2587 context->setup_data.allow_expired_crl = 1;
2588 context->setup_data.allow_bad_md_hash = 1;
2589 context->setup_data.allow_short_rsa_length = 1;
2592 if (context->dtls.ctx) {
2594 #if OPENSSL_VERSION_NUMBER < 0x10101000L
2595 if (!setup_pki_server(context->dtls.ctx, setup_data))
2604 #if OPENSSL_VERSION_NUMBER < 0x10101000L
2605 if (SSLeay() >= 0x10101000L) {
2607 "OpenSSL compiled with %lux, linked with %lux, so "
2608 "no certificate checking\n",
2609 OPENSSL_VERSION_NUMBER, SSLeay());
2611 SSL_CTX_set_tlsext_servername_arg(context->dtls.ctx, &context->setup_data);
2612 SSL_CTX_set_tlsext_servername_callback(context->dtls.ctx,
2613 tls_server_name_call_back);
2615 SSL_CTX_set_client_hello_cb(context->dtls.ctx,
2616 tls_client_hello_call_back,
2620 #if !COAP_DISABLE_TCP
2621 if (context->tls.ctx) {
2623 #if OPENSSL_VERSION_NUMBER < 0x10101000L
2624 if (!setup_pki_server(context->tls.ctx, setup_data))
2633 #if OPENSSL_VERSION_NUMBER < 0x10101000L
2634 if (SSLeay() >= 0x10101000L) {
2636 "OpenSSL compiled with %lux, linked with %lux, so "
2637 "no certificate checking\n",
2638 OPENSSL_VERSION_NUMBER, SSLeay());
2640 SSL_CTX_set_tlsext_servername_arg(context->tls.ctx, &context->setup_data);
2641 SSL_CTX_set_tlsext_servername_callback(context->tls.ctx,
2642 tls_server_name_call_back);
2644 SSL_CTX_set_client_hello_cb(context->tls.ctx,
2645 tls_client_hello_call_back,
2649 SSL_CTX_set_alpn_select_cb(context->tls.ctx, server_alpn_callback, NULL);
2654 if (!context->dtls.ssl) {
2656 context->dtls.ssl = SSL_new(context->dtls.ctx);
2657 if (!context->dtls.ssl)
2659 bio = BIO_new(context->dtls.meth);
2661 SSL_free (context->dtls.ssl);
2662 context->dtls.ssl = NULL;
2665 SSL_set_bio(context->dtls.ssl, bio, bio);
2666 SSL_set_app_data(context->dtls.ssl, NULL);
2667 SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
2670 context->psk_pki_enabled |= IS_PKI;
2676 const char *ca_file,
2679 coap_openssl_context_t *context =
2681 if (context->dtls.ctx) {
2682 if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) {
2684 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
2688 #if !COAP_DISABLE_TCP
2689 if (context->tls.ctx) {
2690 if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) {
2692 ca_file ? ca_file :
"NULL", ca_dir ? ca_dir :
"NULL");
2703 coap_openssl_context_t *context =
2705 return context->psk_pki_enabled ? 1 : 0;
2711 coap_openssl_context_t *context = (coap_openssl_context_t *)handle;
2713 if (context->dtls.ssl)
2714 SSL_free(context->dtls.ssl);
2715 if (context->dtls.ctx)
2716 SSL_CTX_free(context->dtls.ctx);
2717 if (context->dtls.cookie_hmac)
2718 HMAC_CTX_free(context->dtls.cookie_hmac);
2719 if (context->dtls.meth)
2720 BIO_meth_free(context->dtls.meth);
2721 if (context->dtls.bio_addr)
2722 BIO_ADDR_free(context->dtls.bio_addr);
2723 #if !COAP_DISABLE_TCP
2724 if ( context->tls.ctx )
2725 SSL_CTX_free( context->tls.ctx );
2726 if ( context->tls.meth )
2727 BIO_meth_free( context->tls.meth );
2729 for (i = 0; i < context->sni_count; i++) {
2730 OPENSSL_free(context->sni_entry_list[i].sni);
2731 #if OPENSSL_VERSION_NUMBER < 0x10101000L
2732 SSL_CTX_free(context->sni_entry_list[i].ctx);
2735 if (context->sni_count)
2736 OPENSSL_free(context->sni_entry_list);
2737 for (i = 0; i < context->psk_sni_count; i++) {
2738 OPENSSL_free((
char*)context->psk_sni_entry_list[i].sni);
2739 #if OPENSSL_VERSION_NUMBER < 0x10101000L
2740 SSL_CTX_free(context->psk_sni_entry_list[i].ctx);
2743 if (context->psk_sni_count)
2744 OPENSSL_free(context->psk_sni_entry_list);
2750 SSL *nssl = NULL, *ssl = NULL;
2751 coap_ssl_data *data;
2752 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
2755 nssl = SSL_new(dtls->ctx);
2758 nbio = BIO_new(dtls->meth);
2761 SSL_set_bio(nssl, nbio, nbio);
2762 SSL_set_app_data(nssl, NULL);
2763 SSL_set_options(nssl, SSL_OP_COOKIE_EXCHANGE);
2764 SSL_set_mtu(nssl, (
long)session->
mtu);
2768 SSL_set_app_data(ssl, session);
2770 data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl));
2771 data->session = session;
2777 (uint8_t*)hint,
sizeof(hint) - 1);
2778 if (hint_len > 0 && hint_len <
sizeof(hint)) {
2780 SSL_use_psk_identity_hint(ssl, hint);
2784 r = SSL_accept(ssl);
2786 int err = SSL_get_error(ssl, r);
2787 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
2807 coap_openssl_context_t *context =
2810 if (context->psk_pki_enabled & IS_PSK) {
2815 SSL_set_tlsext_host_name (ssl, setup_data->
client_sni) != 1) {
2819 SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback);
2820 SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
2821 SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS);
2824 SSL_set_max_proto_version(ssl, DTLS1_2_VERSION);
2826 #if !COAP_DISABLE_TCP
2828 SSL_set_max_proto_version(ssl, TLS1_2_VERSION);
2832 "CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n");
2835 if (context->psk_pki_enabled & IS_PKI) {
2837 if (!setup_pki_ssl(ssl, setup_data, 0))
2840 #if !COAP_DISABLE_TCP
2842 SSL_set_alpn_protos(ssl, coap_alpn,
sizeof(coap_alpn));
2847 SSL_set_tlsext_host_name (ssl, setup_data->
client_sni) != 1) {
2853 X509_VERIFY_PARAM *param;
2855 param = X509_VERIFY_PARAM_new();
2856 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
2857 SSL_set1_param(ssl, param);
2858 X509_VERIFY_PARAM_free(param);
2865 SSL_VERIFY_CLIENT_ONCE |
2866 SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2867 tls_verify_call_back);
2869 SSL_set_verify(ssl, SSL_VERIFY_NONE, tls_verify_call_back);
2882 coap_ssl_data *data;
2884 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
2885 coap_dtls_context_t *dtls = &context->dtls;
2887 ssl = SSL_new(dtls->ctx);
2890 bio = BIO_new(dtls->meth);
2893 data = (coap_ssl_data *)BIO_get_data(bio);
2894 data->session = session;
2895 SSL_set_bio(ssl, bio, bio);
2896 SSL_set_app_data(ssl, session);
2897 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
2898 SSL_set_mtu(ssl, (
long)session->
mtu);
2900 if (!setup_client_ssl_session(session, ssl))
2905 r = SSL_connect(ssl);
2907 int ret = SSL_get_error(ssl, r);
2908 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
2924 SSL *ssl = (SSL *)session->
tls;
2926 SSL_set_mtu(ssl, (
long)session->
mtu);
2930 SSL *ssl = (SSL *)session->
tls;
2932 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
2933 int r = SSL_shutdown(ssl);
2934 if (r == 0) r = SSL_shutdown(ssl);
2937 session->
tls = NULL;
2944 const uint8_t *data,
size_t data_len) {
2946 SSL *ssl = (SSL *)session->
tls;
2948 assert(ssl != NULL);
2951 r = SSL_write(ssl, data, (
int)data_len);
2954 int err = SSL_get_error(ssl, r);
2955 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
2959 if (err == SSL_ERROR_ZERO_RETURN)
2961 else if (err == SSL_ERROR_SSL)
2991 SSL *ssl = (SSL *)session->
tls;
2992 coap_ssl_data *ssl_data;
2995 ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl));
2996 return ssl_data->timeout;
3000 SSL *ssl = (SSL *)session->
tls;
3004 (DTLSv1_handle_timeout(ssl) < 0)) {
3011 const uint8_t *data,
size_t data_len) {
3012 coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->
context->
dtls_context)->dtls;
3013 coap_ssl_data *ssl_data;
3016 SSL_set_mtu(dtls->ssl, (
long)session->
mtu);
3017 ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(dtls->ssl));
3018 assert(ssl_data != NULL);
3019 if (ssl_data->pdu_len) {
3023 ssl_data->session = session;
3024 ssl_data->pdu = data;
3025 ssl_data->pdu_len = (unsigned)data_len;
3026 r = DTLSv1_listen(dtls->ssl, dtls->bio_addr);
3028 int err = SSL_get_error(dtls->ssl, r);
3029 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3047 const uint8_t *data,
size_t data_len) {
3048 coap_ssl_data *ssl_data;
3049 SSL *ssl = (SSL *)session->
tls;
3052 assert(ssl != NULL);
3054 int in_init = SSL_in_init(ssl);
3056 ssl_data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl));
3057 assert(ssl_data != NULL);
3059 if (ssl_data->pdu_len) {
3063 ssl_data->pdu = data;
3064 ssl_data->pdu_len = (unsigned)data_len;
3067 r = SSL_read(ssl, pdu, (
int)
sizeof(pdu));
3072 int err = SSL_get_error(ssl, r);
3073 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3074 if (in_init && SSL_is_init_finished(ssl)) {
3082 if (err == SSL_ERROR_ZERO_RETURN)
3084 else if (err == SSL_ERROR_SSL)
3102 if (ssl_data && ssl_data->pdu_len) {
3104 coap_log(
LOG_DEBUG,
"coap_dtls_receive: ret %d: remaining data %u\n", r, ssl_data->pdu_len);
3105 ssl_data->pdu_len = 0;
3106 ssl_data->pdu = NULL;
3112 unsigned int overhead = 37;
3113 const SSL_CIPHER *s_ciph = NULL;
3114 if (session->
tls != NULL)
3115 s_ciph = SSL_get_current_cipher(session->
tls);
3117 unsigned int ivlen, maclen, blocksize = 1, pad = 0;
3119 const EVP_CIPHER *e_ciph;
3123 e_ciph = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(s_ciph));
3125 switch (EVP_CIPHER_mode(e_ciph)) {
3126 case EVP_CIPH_GCM_MODE:
3127 ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
3128 maclen = EVP_GCM_TLS_TAG_LEN;
3131 case EVP_CIPH_CCM_MODE:
3132 ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
3133 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3134 if (strstr(cipher,
"CCM8"))
3140 case EVP_CIPH_CBC_MODE:
3141 e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
3142 blocksize = EVP_CIPHER_block_size(e_ciph);
3143 ivlen = EVP_CIPHER_iv_length(e_ciph);
3145 maclen = EVP_MD_size(e_md);
3148 case EVP_CIPH_STREAM_CIPHER:
3155 SSL_CIPHER_description(s_ciph, cipher,
sizeof(cipher));
3162 overhead = DTLS1_RT_HEADER_LENGTH + ivlen + maclen + blocksize - 1 + pad;
3167 #if !COAP_DISABLE_TCP
3172 coap_openssl_context_t *context = ((coap_openssl_context_t *)session->
context->
dtls_context);
3173 coap_tls_context_t *tls = &context->tls;
3176 ssl = SSL_new(tls->ctx);
3179 bio = BIO_new(tls->meth);
3182 BIO_set_data(bio, session);
3183 SSL_set_bio(ssl, bio, bio);
3184 SSL_set_app_data(ssl, session);
3186 if (!setup_client_ssl_session(session, ssl))
3189 r = SSL_connect(ssl);
3191 int ret = SSL_get_error(ssl, r);
3192 if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
3194 if (ret == SSL_ERROR_WANT_READ)
3196 if (ret == SSL_ERROR_WANT_WRITE) {
3198 #ifdef COAP_EPOLL_SUPPORT
3211 *connected = SSL_is_init_finished(ssl);
3224 coap_tls_context_t *tls = &((coap_openssl_context_t *)session->
context->
dtls_context)->tls;
3228 ssl = SSL_new(tls->ctx);
3231 bio = BIO_new(tls->meth);
3234 BIO_set_data(bio, session);
3235 SSL_set_bio(ssl, bio, bio);
3236 SSL_set_app_data(ssl, session);
3241 if (hint_len > 0 && hint_len <
sizeof(hint)) {
3243 SSL_use_psk_identity_hint(ssl, hint);
3247 r = SSL_accept(ssl);
3249 int err = SSL_get_error(ssl, r);
3250 if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
3252 if (err == SSL_ERROR_WANT_READ)
3254 if (err == SSL_ERROR_WANT_WRITE) {
3256 #ifdef COAP_EPOLL_SUPPORT
3269 *connected = SSL_is_init_finished(ssl);
3280 SSL *ssl = (SSL *)session->
tls;
3282 if (!SSL_in_init(ssl) && !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
3283 int r = SSL_shutdown(ssl);
3284 if (r == 0) r = SSL_shutdown(ssl);
3287 session->
tls = NULL;
3294 const uint8_t *data,
3297 SSL *ssl = (SSL *)session->
tls;
3303 in_init = !SSL_is_init_finished(ssl);
3305 r = SSL_write(ssl, data, (
int)data_len);
3308 int err = SSL_get_error(ssl, r);
3309 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3310 if (in_init && SSL_is_init_finished(ssl)) {
3316 if (err == SSL_ERROR_WANT_READ)
3318 if (err == SSL_ERROR_WANT_WRITE) {
3320 #ifdef COAP_EPOLL_SUPPORT
3332 if (err == SSL_ERROR_ZERO_RETURN)
3334 else if (err == SSL_ERROR_SSL)
3338 }
else if (in_init && SSL_is_init_finished(ssl)) {
3363 SSL *ssl = (SSL *)session->
tls;
3369 in_init = !SSL_is_init_finished(ssl);
3371 r = SSL_read(ssl, data, (
int)data_len);
3373 int err = SSL_get_error(ssl, r);
3374 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3375 if (in_init && SSL_is_init_finished(ssl)) {
3381 if (err == SSL_ERROR_WANT_READ)
3383 if (err == SSL_ERROR_WANT_WRITE) {
3385 #ifdef COAP_EPOLL_SUPPORT
3395 if (err == SSL_ERROR_ZERO_RETURN)
3397 else if (err == SSL_ERROR_SSL)
3401 }
else if (in_init && SSL_is_init_finished(ssl)) {
3425 SHA256_CTX *digest_ctx = OPENSSL_malloc(
sizeof(SHA256_CTX));
3428 SHA256_Init(digest_ctx);
3435 OPENSSL_free(digest_ctx);
3440 const uint8_t *data,
3442 return SHA256_Update(digest_ctx, data, data_len);
3448 int ret = SHA256_Final((uint8_t*)digest_buffer, digest_ctx);
3460 #pragma GCC diagnostic ignored "-Wunused-function"
Pulls together all the internal only header files.
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
const char * coap_socket_strerror(void)
#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)
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
void * coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_spsk_t *setup_data COAP_UNUSED)
void coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
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)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
int coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_new_client_session(coap_session_t *session 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)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
void * coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
void * coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
int coap_dtls_hello(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
static int dtls_log_level
int coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_cpsk_t *setup_data 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)
void coap_dtls_free_context(void *handle COAP_UNUSED)
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
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.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
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_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.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
#define COAP_DTLS_HINT_LENGTH
int coap_tls_is_supported(void)
Check whether TLS is available.
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
@ 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.
#define COAP_EVENT_DTLS_RENEGOTIATE
#define COAP_EVENT_DTLS_ERROR
#define COAP_EVENT_DTLS_CLOSED
(D)TLS events for COAP_PROTO_DTLS and COAP_PROTO_TLS
#define COAP_EVENT_DTLS_CONNECTED
void coap_dtls_set_log_level(int level)
Sets the (D)TLS logging level to the specified level.
const char * coap_session_str(const coap_session_t *session)
Get session description.
int coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
#define coap_log(level,...)
Logging function.
@ COAP_LOG_CIPHERS
CipherInfo.
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).
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
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).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_STATE_HANDSHAKE
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
The CoAP stack's global state is stored in a coap_context_t object.
size_t(* get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
size_t(* get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
size_t(* get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
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::@2 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.
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
unsigned int max_retransmit
maximum re-transmit count (default 4)
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
coap_session_state_t state
current state of relationaship with peer
coap_proto_t proto
protocol used
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu
int dtls_event
Tracking any (D)TLS events on this sesison.
void * tls
security parameters
coap_context_t * context
session's context
coap_socket_flags_t flags
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