libcoap  4.2.0
coap_openssl.c
Go to the documentation of this file.
1 /*
2 * coap_openssl.c -- Datagram Transport Layer Support for libcoap with openssl
3 *
4 * Copyright (C) 2017 Jean-Claude Michelou <jcm@spinetix.com>
5 * Copyright (C) 2018 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * This file is part of the CoAP library libcoap. Please see README for terms
8 * of use.
9 */
10 
11 #include "coap_config.h"
12 
13 #ifdef HAVE_OPENSSL
14 
15 /*
16  * OpenSSL 1.1.0 has support for making decisions during receipt of
17  * the Client Hello - the call back function is set up using
18  * SSL_CTX_set_tlsext_servername_callback() which is called later in the
19  * Client Hello processing - but called every Client Hello.
20  * Certificates and Preshared Keys have to be set up in the SSL CTX before
21  * SSL_Accept() is called, making the code messy to decide whether this is a
22  * PKI or PSK incoming request to handle things accordingly if both are
23  * defined. SNI has to create a new SSL CTX to handle different server names
24  * with different crtificates.
25  *
26  * OpenSSL 1.1.1 introduces a new function SSL_CTX_set_client_hello_cb().
27  * The call back is invoked early on in the Client Hello processing giving
28  * the ability to easily use different Preshared Keys, Certificates etc.
29  * Certificates do not have to be set up in the SSL CTX before SSL_Accept is
30  * called.
31  * Later in the Client Hello code, the callback for
32  * SSL_CTX_set_tlsext_servername_callback() is still called, but only if SNI
33  * is being used by the client, so cannot be used for doing things the
34  * OpenSSL 1.1.0 way.
35  *
36  * OpenSSL 1.1.1 supports TLS1.3.
37  *
38  * Consequently, this code has to have compile time options to include /
39  * exclude code based on whether compiled against 1.1.0 or 1.1.1, as well as
40  * have additional run time checks.
41  *
42  */
43 #include "net.h"
44 #include "mem.h"
45 #include "coap_debug.h"
46 #include "prng.h"
47 #include <openssl/ssl.h>
48 #include <openssl/err.h>
49 #include <openssl/rand.h>
50 #include <openssl/hmac.h>
51 #include <openssl/x509v3.h>
52 
53 #if OPENSSL_VERSION_NUMBER < 0x10100000L
54 #error Must be compiled against OpenSSL 1.1.0 or later
55 #endif
56 
57 #ifdef __GNUC__
58 #define UNUSED __attribute__((unused))
59 #else
60 #define UNUSED
61 #endif /* __GNUC__ */
62 
63 /* RFC6091/RFC7250 */
64 #ifndef TLSEXT_TYPE_client_certificate_type
65 #define TLSEXT_TYPE_client_certificate_type 19
66 #endif
67 #ifndef TLSEXT_TYPE_server_certificate_type
68 #define TLSEXT_TYPE_server_certificate_type 20
69 #endif
70 
71 /* This structure encapsulates the OpenSSL context object. */
72 typedef struct coap_dtls_context_t {
73  SSL_CTX *ctx;
74  SSL *ssl; /* OpenSSL object for listening to connection requests */
75  HMAC_CTX *cookie_hmac;
76  BIO_METHOD *meth;
77  BIO_ADDR *bio_addr;
78 } coap_dtls_context_t;
79 
80 typedef struct coap_tls_context_t {
81  SSL_CTX *ctx;
82  BIO_METHOD *meth;
83 } coap_tls_context_t;
84 
85 #define IS_PSK 0x1
86 #define IS_PKI 0x2
87 
88 typedef struct sni_entry {
89  char *sni;
90 #if OPENSSL_VERSION_NUMBER < 0x10101000L
91  SSL_CTX *ctx;
92 #else /* OPENSSL_VERSION_NUMBER >= 0x10101000L */
93  coap_dtls_key_t pki_key;
94 #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */
95 } sni_entry;
96 
97 typedef struct coap_openssl_context_t {
98  coap_dtls_context_t dtls;
99  coap_tls_context_t tls;
100  coap_dtls_pki_t setup_data;
101  int psk_pki_enabled;
102  size_t sni_count;
103  sni_entry *sni_entry_list;
104 } coap_openssl_context_t;
105 
106 int coap_dtls_is_supported(void) {
107  if (SSLeay() < 0x10100000L) {
108  coap_log(LOG_WARNING, "OpenSSL version 1.1.0 or later is required\n");
109  return 0;
110  }
111 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
112  /*
113  * For 1.1.1, we need to use SSL_CTX_set_client_hello_cb()
114  * which is not in 1.1.0 instead of SSL_CTX_set_tlsext_servername_callback()
115  *
116  * However, there could be a runtime undefined external reference error
117  * as SSL_CTX_set_client_hello_cb() is not there in 1.1.0.
118  */
119  if (SSLeay() < 0x10101000L) {
120  coap_log(LOG_WARNING, "OpenSSL version 1.1.1 or later is required\n");
121  return 0;
122  }
123 #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */
124  return 1;
125 }
126 
127 int coap_tls_is_supported(void) {
128  if (SSLeay() < 0x10100000L) {
129  coap_log(LOG_WARNING, "OpenSSL version 1.1.0 or later is required\n");
130  return 0;
131  }
132 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
133  if (SSLeay() < 0x10101000L) {
134  coap_log(LOG_WARNING, "OpenSSL version 1.1.1 or later is required\n");
135  return 0;
136  }
137 #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */
138  return 1;
139 }
140 
143  static coap_tls_version_t version;
144  version.version = SSLeay();
145  version.built_version = OPENSSL_VERSION_NUMBER;
146  version.type = COAP_TLS_LIBRARY_OPENSSL;
147  return &version;
148 }
149 
150 void coap_dtls_startup(void) {
151  SSL_load_error_strings();
152  SSL_library_init();
153 }
154 
155 static int dtls_log_level = 0;
156 
157 void coap_dtls_set_log_level(int level) {
158  dtls_log_level = level;
159 }
160 
161 int coap_dtls_get_log_level(void) {
162  return dtls_log_level;
163 }
164 
165 typedef struct coap_ssl_st {
166  coap_session_t *session;
167  const void *pdu;
168  unsigned pdu_len;
169  unsigned peekmode;
170  coap_tick_t timeout;
171 } coap_ssl_data;
172 
173 static int coap_dgram_create(BIO *a) {
174  coap_ssl_data *data = NULL;
175  data = malloc(sizeof(coap_ssl_data));
176  if (data == NULL)
177  return 0;
178  BIO_set_init(a, 1);
179  BIO_set_data(a, data);
180  memset(data, 0x00, sizeof(coap_ssl_data));
181  return 1;
182 }
183 
184 static int coap_dgram_destroy(BIO *a) {
185  coap_ssl_data *data;
186  if (a == NULL)
187  return 0;
188  data = (coap_ssl_data *)BIO_get_data(a);
189  if (data != NULL)
190  free(data);
191  return 1;
192 }
193 
194 static int coap_dgram_read(BIO *a, char *out, int outl) {
195  int ret = 0;
196  coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
197 
198  if (out != NULL) {
199  if (data != NULL && data->pdu_len > 0) {
200  if (outl < (int)data->pdu_len) {
201  memcpy(out, data->pdu, outl);
202  ret = outl;
203  } else {
204  memcpy(out, data->pdu, data->pdu_len);
205  ret = (int)data->pdu_len;
206  }
207  if (!data->peekmode) {
208  data->pdu_len = 0;
209  data->pdu = NULL;
210  }
211  } else {
212  ret = -1;
213  }
214  BIO_clear_retry_flags(a);
215  if (ret < 0)
216  BIO_set_retry_read(a);
217  }
218  return ret;
219 }
220 
221 static int coap_dgram_write(BIO *a, const char *in, int inl) {
222  int ret = 0;
223  coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a);
224 
225  if (data->session) {
226  if (data->session->sock.flags == COAP_SOCKET_EMPTY && data->session->endpoint == NULL) {
227  /* socket was closed on client due to error */
228  BIO_clear_retry_flags(a);
229  return -1;
230  }
231  ret = (int)coap_session_send(data->session, (const uint8_t *)in, (size_t)inl);
232  BIO_clear_retry_flags(a);
233  if (ret <= 0)
234  BIO_set_retry_write(a);
235  } else {
236  BIO_clear_retry_flags(a);
237  ret = -1;
238  }
239  return ret;
240 }
241 
242 static int coap_dgram_puts(BIO *a, const char *pstr) {
243  return coap_dgram_write(a, pstr, (int)strlen(pstr));
244 }
245 
246 static long coap_dgram_ctrl(BIO *a, int cmd, long num, void *ptr) {
247  long ret = 1;
248  coap_ssl_data *data = BIO_get_data(a);
249 
250  (void)ptr;
251 
252  switch (cmd) {
253  case BIO_CTRL_GET_CLOSE:
254  ret = BIO_get_shutdown(a);
255  break;
256  case BIO_CTRL_SET_CLOSE:
257  BIO_set_shutdown(a, (int)num);
258  ret = 1;
259  break;
260  case BIO_CTRL_DGRAM_SET_PEEK_MODE:
261  data->peekmode = (unsigned)num;
262  break;
263  case BIO_CTRL_DGRAM_CONNECT:
264  case BIO_C_SET_FD:
265  case BIO_C_GET_FD:
266  case BIO_CTRL_DGRAM_SET_DONT_FRAG:
267  case BIO_CTRL_DGRAM_GET_MTU:
268  case BIO_CTRL_DGRAM_SET_MTU:
269  case BIO_CTRL_DGRAM_QUERY_MTU:
270  case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
271  ret = -1;
272  break;
273  case BIO_CTRL_DUP:
274  case BIO_CTRL_FLUSH:
275  case BIO_CTRL_DGRAM_MTU_DISCOVER:
276  case BIO_CTRL_DGRAM_SET_CONNECTED:
277  ret = 1;
278  break;
279  case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
280  data->timeout = coap_ticks_from_rt_us((uint64_t)((struct timeval*)ptr)->tv_sec * 1000000 + ((struct timeval*)ptr)->tv_usec);
281  ret = 1;
282  break;
283  case BIO_CTRL_RESET:
284  case BIO_C_FILE_SEEK:
285  case BIO_C_FILE_TELL:
286  case BIO_CTRL_INFO:
287  case BIO_CTRL_PENDING:
288  case BIO_CTRL_WPENDING:
289  case BIO_CTRL_DGRAM_GET_PEER:
290  case BIO_CTRL_DGRAM_SET_PEER:
291  case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
292  case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
293  case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
294  case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
295  case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
296  case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
297  case BIO_CTRL_DGRAM_MTU_EXCEEDED:
298  case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
299  default:
300  ret = 0;
301  break;
302  }
303  return ret;
304 }
305 
306 static int coap_dtls_generate_cookie(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len) {
307  coap_dtls_context_t *dtls = (coap_dtls_context_t *)SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl));
308  coap_ssl_data *data = (coap_ssl_data*)BIO_get_data(SSL_get_rbio(ssl));
309  int r = HMAC_Init_ex(dtls->cookie_hmac, NULL, 0, NULL, NULL);
310  r &= HMAC_Update(dtls->cookie_hmac, (const uint8_t*)&data->session->local_addr.addr, (size_t)data->session->local_addr.size);
311  r &= HMAC_Update(dtls->cookie_hmac, (const uint8_t*)&data->session->remote_addr.addr, (size_t)data->session->remote_addr.size);
312  r &= HMAC_Final(dtls->cookie_hmac, cookie, cookie_len);
313  return r;
314 }
315 
316 static int coap_dtls_verify_cookie(SSL *ssl, const uint8_t *cookie, unsigned int cookie_len) {
317  uint8_t hmac[32];
318  unsigned len = 32;
319  if (coap_dtls_generate_cookie(ssl, hmac, &len) && cookie_len == len && memcmp(cookie, hmac, len) == 0)
320  return 1;
321  else
322  return 0;
323 }
324 
325 static unsigned coap_dtls_psk_client_callback(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *buf, unsigned max_len) {
326  size_t hint_len = 0, identity_len = 0, psk_len;
327  coap_session_t *session = (coap_session_t*)SSL_get_app_data(ssl);
328 
329  if (hint)
330  hint_len = strlen(hint);
331  else
332  hint = "";
333 
334  coap_log(LOG_DEBUG, "got psk_identity_hint: '%.*s'\n", (int)hint_len, hint);
335 
336  if (session == NULL || session->context == NULL || session->context->get_client_psk == NULL)
337  return 0;
338 
339  psk_len = session->context->get_client_psk(session, (const uint8_t*)hint, hint_len, (uint8_t*)identity, &identity_len, max_identity_len - 1, (uint8_t*)buf, max_len);
340  if (identity_len < max_identity_len)
341  identity[identity_len] = 0;
342  return (unsigned)psk_len;
343 }
344 
345 static unsigned coap_dtls_psk_server_callback(SSL *ssl, const char *identity, unsigned char *buf, unsigned max_len) {
346  size_t identity_len = 0;
347  coap_session_t *session = (coap_session_t*)SSL_get_app_data(ssl);
348 
349  if (identity)
350  identity_len = strlen(identity);
351  else
352  identity = "";
353 
354  coap_log(LOG_DEBUG, "got psk_identity: '%.*s'\n",
355  (int)identity_len, identity);
356 
357  if (session == NULL || session->context == NULL || session->context->get_server_psk == NULL)
358  return 0;
359 
360  return (unsigned)session->context->get_server_psk(session, (const uint8_t*)identity, identity_len, (uint8_t*)buf, max_len);
361 }
362 
363 static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) {
364  coap_session_t *session = (coap_session_t*)SSL_get_app_data(ssl);
365  const char *pstr;
366  int w = where &~SSL_ST_MASK;
367 
368  if (w & SSL_ST_CONNECT)
369  pstr = "SSL_connect";
370  else if (w & SSL_ST_ACCEPT)
371  pstr = "SSL_accept";
372  else
373  pstr = "undefined";
374 
375  if (where & SSL_CB_LOOP) {
376  if (dtls_log_level >= LOG_DEBUG)
377  coap_log(LOG_DEBUG, "* %s: %s:%s\n",
378  coap_session_str(session), pstr, SSL_state_string_long(ssl));
379  } else if (where & SSL_CB_ALERT) {
380  pstr = (where & SSL_CB_READ) ? "read" : "write";
381  if (dtls_log_level >= LOG_INFO)
382  coap_log(LOG_INFO, "* %s: SSL3 alert %s:%s:%s\n",
383  coap_session_str(session),
384  pstr,
385  SSL_alert_type_string_long(ret),
386  SSL_alert_desc_string_long(ret));
387  if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL)
389  } else if (where & SSL_CB_EXIT) {
390  if (ret == 0) {
391  if (dtls_log_level >= LOG_WARNING) {
392  unsigned long e;
393  coap_log(LOG_WARNING, "* %s: %s:failed in %s\n",
394  coap_session_str(session), pstr, SSL_state_string_long(ssl));
395  while ((e = ERR_get_error()))
396  coap_log(LOG_WARNING, "* %s: %s at %s:%s\n",
397  coap_session_str(session), ERR_reason_error_string(e),
398  ERR_lib_error_string(e), ERR_func_error_string(e));
399  }
400  } else if (ret < 0) {
401  if (dtls_log_level >= LOG_WARNING) {
402  int err = SSL_get_error(ssl, ret);
403  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) {
404  long e;
405  coap_log(LOG_WARNING, "* %s: %s:error in %s\n",
406  coap_session_str(session), pstr, SSL_state_string_long(ssl));
407  while ((e = ERR_get_error()))
408  coap_log(LOG_WARNING, "* %s: %s at %s:%s\n",
409  coap_session_str(session), ERR_reason_error_string(e),
410  ERR_lib_error_string(e), ERR_func_error_string(e));
411  }
412  }
413  }
414  }
415 
416  if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK)
418 }
419 
420 static int coap_sock_create(BIO *a) {
421  BIO_set_init(a, 1);
422  return 1;
423 }
424 
425 static int coap_sock_destroy(BIO *a) {
426  (void)a;
427  return 1;
428 }
429 
430 static int coap_sock_read(BIO *a, char *out, int outl) {
431  int ret = 0;
432  coap_session_t *session = (coap_session_t *)BIO_get_data(a);
433 
434  if (out != NULL) {
435  ret = (int)coap_socket_read(&session->sock, (uint8_t*)out, (size_t)outl);
436  if (ret == 0) {
437  BIO_set_retry_read(a);
438  ret = -1;
439  } else {
440  BIO_clear_retry_flags(a);
441  }
442  }
443  return ret;
444 }
445 
446 static int coap_sock_write(BIO *a, const char *in, int inl) {
447  int ret = 0;
448  coap_session_t *session = (coap_session_t *)BIO_get_data(a);
449 
450  ret = (int)coap_socket_write(&session->sock, (const uint8_t*)in, (size_t)inl);
451  BIO_clear_retry_flags(a);
452  if (ret == 0) {
453  BIO_set_retry_read(a);
454  ret = -1;
455  } else {
456  BIO_clear_retry_flags(a);
457  }
458  return ret;
459 }
460 
461 static int coap_sock_puts(BIO *a, const char *pstr) {
462  return coap_sock_write(a, pstr, (int)strlen(pstr));
463 }
464 
465 static long coap_sock_ctrl(BIO *a, int cmd, long num, void *ptr) {
466  int r = 1;
467  (void)a;
468  (void)ptr;
469  (void)num;
470 
471  switch (cmd) {
472  case BIO_C_SET_FD:
473  case BIO_C_GET_FD:
474  r = -1;
475  break;
476  case BIO_CTRL_SET_CLOSE:
477  case BIO_CTRL_DUP:
478  case BIO_CTRL_FLUSH:
479  r = 1;
480  break;
481  default:
482  case BIO_CTRL_GET_CLOSE:
483  r = 0;
484  break;
485  }
486  return r;
487 }
488 
489 void *coap_dtls_new_context(struct coap_context_t *coap_context) {
490  coap_openssl_context_t *context;
491  (void)coap_context;
492 
493  context = (coap_openssl_context_t *)coap_malloc(sizeof(coap_openssl_context_t));
494  if (context) {
495  uint8_t cookie_secret[32];
496 
497  memset(context, 0, sizeof(coap_openssl_context_t));
498 
499  /* Set up DTLS context */
500  context->dtls.ctx = SSL_CTX_new(DTLS_method());
501  if (!context->dtls.ctx)
502  goto error;
503  SSL_CTX_set_min_proto_version(context->dtls.ctx, DTLS1_2_VERSION);
504  SSL_CTX_set_app_data(context->dtls.ctx, &context->dtls);
505  SSL_CTX_set_read_ahead(context->dtls.ctx, 1);
506  SSL_CTX_set_cipher_list(context->dtls.ctx, "TLSv1.2:TLSv1.0");
507  if (!RAND_bytes(cookie_secret, (int)sizeof(cookie_secret))) {
508  if (dtls_log_level >= LOG_WARNING)
510  "Insufficient entropy for random cookie generation");
511  prng(cookie_secret, sizeof(cookie_secret));
512  }
513  context->dtls.cookie_hmac = HMAC_CTX_new();
514  if (!HMAC_Init_ex(context->dtls.cookie_hmac, cookie_secret, (int)sizeof(cookie_secret), EVP_sha256(), NULL))
515  goto error;
516  SSL_CTX_set_cookie_generate_cb(context->dtls.ctx, coap_dtls_generate_cookie);
517  SSL_CTX_set_cookie_verify_cb(context->dtls.ctx, coap_dtls_verify_cookie);
518  SSL_CTX_set_info_callback(context->dtls.ctx, coap_dtls_info_callback);
519  SSL_CTX_set_options(context->dtls.ctx, SSL_OP_NO_QUERY_MTU);
520  context->dtls.meth = BIO_meth_new(BIO_TYPE_DGRAM, "coapdgram");
521  if (!context->dtls.meth)
522  goto error;
523  context->dtls.bio_addr = BIO_ADDR_new();
524  if (!context->dtls.bio_addr)
525  goto error;
526  BIO_meth_set_write(context->dtls.meth, coap_dgram_write);
527  BIO_meth_set_read(context->dtls.meth, coap_dgram_read);
528  BIO_meth_set_puts(context->dtls.meth, coap_dgram_puts);
529  BIO_meth_set_ctrl(context->dtls.meth, coap_dgram_ctrl);
530  BIO_meth_set_create(context->dtls.meth, coap_dgram_create);
531  BIO_meth_set_destroy(context->dtls.meth, coap_dgram_destroy);
532 
533  /* Set up TLS context */
534  context->tls.ctx = SSL_CTX_new(TLS_method());
535  if (!context->tls.ctx)
536  goto error;
537  SSL_CTX_set_app_data(context->tls.ctx, &context->tls);
538  SSL_CTX_set_min_proto_version(context->tls.ctx, TLS1_VERSION);
539  SSL_CTX_set_cipher_list(context->tls.ctx, "TLSv1.2:TLSv1.0");
540  SSL_CTX_set_info_callback(context->tls.ctx, coap_dtls_info_callback);
541  context->tls.meth = BIO_meth_new(BIO_TYPE_SOCKET, "coapsock");
542  if (!context->tls.meth)
543  goto error;
544  BIO_meth_set_write(context->tls.meth, coap_sock_write);
545  BIO_meth_set_read(context->tls.meth, coap_sock_read);
546  BIO_meth_set_puts(context->tls.meth, coap_sock_puts);
547  BIO_meth_set_ctrl(context->tls.meth, coap_sock_ctrl);
548  BIO_meth_set_create(context->tls.meth, coap_sock_create);
549  BIO_meth_set_destroy(context->tls.meth, coap_sock_destroy);
550  }
551 
552  return context;
553 
554 error:
555  coap_dtls_free_context(context);
556  return NULL;
557 }
558 
559 int
561  const char *identity_hint,
562  coap_dtls_role_t role
563 ) {
564  coap_openssl_context_t *context = ((coap_openssl_context_t *)ctx->dtls_context);
565  BIO *bio;
566 
567  if (role == COAP_DTLS_ROLE_SERVER) {
568  SSL_CTX_set_psk_server_callback(context->dtls.ctx, coap_dtls_psk_server_callback);
569  SSL_CTX_set_psk_server_callback(context->tls.ctx, coap_dtls_psk_server_callback);
570  SSL_CTX_use_psk_identity_hint(context->dtls.ctx, identity_hint ? identity_hint : "");
571  SSL_CTX_use_psk_identity_hint(context->tls.ctx, identity_hint ? identity_hint : "");
572  }
573  if (!context->dtls.ssl) {
574  /* This is set up to handle new incoming sessions to a server */
575  context->dtls.ssl = SSL_new(context->dtls.ctx);
576  if (!context->dtls.ssl)
577  return 0;
578  bio = BIO_new(context->dtls.meth);
579  if (!bio) {
580  SSL_free (context->dtls.ssl);
581  context->dtls.ssl = NULL;
582  return 0;
583  }
584  SSL_set_bio(context->dtls.ssl, bio, bio);
585  SSL_set_app_data(context->dtls.ssl, NULL);
586  SSL_set_options(context->dtls.ssl, SSL_OP_COOKIE_EXCHANGE);
587  SSL_set_mtu(context->dtls.ssl, COAP_DEFAULT_MTU);
588  }
589  context->psk_pki_enabled |= IS_PSK;
590  return 1;
591 }
592 
593 static int
594 map_key_type(int asn1_private_key_type
595 ) {
596  switch (asn1_private_key_type) {
597  case COAP_ASN1_PKEY_NONE: return EVP_PKEY_NONE;
598  case COAP_ASN1_PKEY_RSA: return EVP_PKEY_RSA;
599  case COAP_ASN1_PKEY_RSA2: return EVP_PKEY_RSA2;
600  case COAP_ASN1_PKEY_DSA: return EVP_PKEY_DSA;
601  case COAP_ASN1_PKEY_DSA1: return EVP_PKEY_DSA1;
602  case COAP_ASN1_PKEY_DSA2: return EVP_PKEY_DSA2;
603  case COAP_ASN1_PKEY_DSA3: return EVP_PKEY_DSA3;
604  case COAP_ASN1_PKEY_DSA4: return EVP_PKEY_DSA4;
605  case COAP_ASN1_PKEY_DH: return EVP_PKEY_DH;
606  case COAP_ASN1_PKEY_DHX: return EVP_PKEY_DHX;
607  case COAP_ASN1_PKEY_EC: return EVP_PKEY_EC;
608  case COAP_ASN1_PKEY_HMAC: return EVP_PKEY_HMAC;
609  case COAP_ASN1_PKEY_CMAC: return EVP_PKEY_CMAC;
610  case COAP_ASN1_PKEY_TLS1_PRF: return EVP_PKEY_TLS1_PRF;
611  case COAP_ASN1_PKEY_HKDF: return EVP_PKEY_HKDF;
612  default:
614  "*** setup_pki: DTLS: Unknown Private Key type %d for ASN1\n",
615  asn1_private_key_type);
616  break;
617  }
618  return 0;
619 }
620 static uint8_t coap_alpn[] = { 4, 'c', 'o', 'a', 'p' };
621 
622 static int
623 server_alpn_callback (SSL *ssl UNUSED,
624  const unsigned char **out,
625  unsigned char *outlen,
626  const unsigned char *in,
627  unsigned int inlen,
628  void *arg UNUSED
629 ) {
630  unsigned char *tout = NULL;
631  int ret;
632  if (inlen == 0)
633  return SSL_TLSEXT_ERR_NOACK;
634  ret = SSL_select_next_proto(&tout,
635  outlen,
636  coap_alpn,
637  sizeof(coap_alpn),
638  in,
639  inlen);
640  *out = tout;
641  return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK;
642 }
643 
644 static void
645 add_ca_to_cert_store(X509_STORE *st, X509 *x509)
646 {
647  long e;
648 
649  /* Flush out existing errors */
650  while ((e = ERR_get_error()) != 0) {
651  }
652 
653  if (!X509_STORE_add_cert(st, x509)) {
654  while ((e = ERR_get_error()) != 0) {
655  int r = ERR_GET_REASON(e);
656  if (r != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
657  /* Not already added */
658  coap_log(LOG_WARNING, "***setup_pki: (D)TLS: %s at %s:%s\n",
659  ERR_reason_error_string(e),
660  ERR_lib_error_string(e),
661  ERR_func_error_string(e));
662  }
663  }
664  }
665 }
666 
667 #if OPENSSL_VERSION_NUMBER < 0x10101000L
668 static int
669 setup_pki_server(SSL_CTX *ctx,
670  coap_dtls_pki_t* setup_data
671 ) {
672  switch (setup_data->pki_key.key_type) {
673  case COAP_PKI_KEY_PEM:
674  if (setup_data->pki_key.key.pem.public_cert &&
675  setup_data->pki_key.key.pem.public_cert[0]) {
676  if (!(SSL_CTX_use_certificate_file(ctx,
677  setup_data->pki_key.key.pem.public_cert,
678  SSL_FILETYPE_PEM))) {
680  "*** setup_pki: (D)TLS: %s: Unable to configure "
681  "Server Certificate\n",
682  setup_data->pki_key.key.pem.public_cert);
683  return 0;
684  }
685  }
686  else {
688  "*** setup_pki: (D)TLS: No Server Certificate defined\n");
689  return 0;
690  }
691 
692  if (setup_data->pki_key.key.pem.private_key &&
693  setup_data->pki_key.key.pem.private_key[0]) {
694  if (!(SSL_CTX_use_PrivateKey_file(ctx,
695  setup_data->pki_key.key.pem.private_key,
696  SSL_FILETYPE_PEM))) {
698  "*** setup_pki: (D)TLS: %s: Unable to configure "
699  "Server Private Key\n",
700  setup_data->pki_key.key.pem.private_key);
701  return 0;
702  }
703  }
704  else {
706  "*** setup_pki: (D)TLS: No Server Private Key defined\n");
707  return 0;
708  }
709 
710  if (setup_data->pki_key.key.pem.ca_file &&
711  setup_data->pki_key.key.pem.ca_file[0]) {
712  STACK_OF(X509_NAME) *cert_names;
713  X509_STORE *st;
714  BIO *in;
715  X509 *x = NULL;
716  char *rw_var = NULL;
717  cert_names = SSL_load_client_CA_file(setup_data->pki_key.key.pem.ca_file);
718  if (cert_names != NULL)
719  SSL_CTX_set_client_CA_list(ctx, cert_names);
720  else {
722  "*** setup_pki: (D)TLS: %s: Unable to configure "
723  "client CA File\n",
724  setup_data->pki_key.key.pem.ca_file);
725  return 0;
726  }
727  st = SSL_CTX_get_cert_store(ctx);
728  in = BIO_new(BIO_s_file());
729  /* Need to do this to not get a compiler warning about const parameters */
730  memcpy(&rw_var, &setup_data->pki_key.key.pem.ca_file, sizeof (rw_var));
731  if (!BIO_read_filename(in, rw_var)) {
732  BIO_free(in);
733  X509_free(x);
734  break;
735  }
736 
737  for (;;) {
738  if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
739  break;
740  add_ca_to_cert_store(st, x);
741  }
742  BIO_free(in);
743  X509_free(x);
744  }
745  break;
746 
747  case COAP_PKI_KEY_ASN1:
748  if (setup_data->pki_key.key.asn1.public_cert &&
749  setup_data->pki_key.key.asn1.public_cert_len > 0) {
750  if (!(SSL_CTX_use_certificate_ASN1(ctx,
751  setup_data->pki_key.key.asn1.public_cert_len,
752  setup_data->pki_key.key.asn1.public_cert))) {
754  "*** setup_pki: (D)TLS: %s: Unable to configure "
755  "Server Certificate\n",
756  "ASN1");
757  return 0;
758  }
759  }
760  else {
762  "*** setup_pki: (D)TLS: No Server Certificate defined\n");
763  return 0;
764  }
765 
766  if (setup_data->pki_key.key.asn1.private_key &&
767  setup_data->pki_key.key.asn1.private_key_len > 0) {
768  int pkey_type = map_key_type(setup_data->pki_key.key.asn1.private_key_type);
769  if (!(SSL_CTX_use_PrivateKey_ASN1(pkey_type, ctx,
770  setup_data->pki_key.key.asn1.private_key,
771  setup_data->pki_key.key.asn1.private_key_len))) {
773  "*** setup_pki: (D)TLS: %s: Unable to configure "
774  "Server Private Key\n",
775  "ASN1");
776  return 0;
777  }
778  }
779  else {
781  "*** setup_pki: (D)TLS: No Server Private Key defined\n");
782  return 0;
783  }
784 
785  if (setup_data->pki_key.key.asn1.ca_cert &&
786  setup_data->pki_key.key.asn1.ca_cert_len > 0) {
787  /* Need to use a temp variable as it gets incremented*/
788  const uint8_t *p = setup_data->pki_key.key.asn1.ca_cert;
789  X509* x509 = d2i_X509(NULL, &p, setup_data->pki_key.key.asn1.ca_cert_len);
790  X509_STORE *st;
791  if (!x509 || !SSL_CTX_add_client_CA(ctx, x509)) {
793  "*** setup_pki: (D)TLS: %s: Unable to configure "
794  "client CA File\n",
795  "ASN1");
796  X509_free(x509);
797  return 0;
798  }
799  st = SSL_CTX_get_cert_store(ctx);
800  add_ca_to_cert_store(st, x509);
801  X509_free(x509);
802  }
803  break;
804  default:
806  "*** setup_pki: (D)TLS: Unknown key type %d\n",
807  setup_data->pki_key.key_type);
808  return 0;
809  }
810 
811  return 1;
812 }
813 #endif /* OPENSSL_VERSION_NUMBER < 0x10101000L */
814 
815 static int
816 setup_pki_ssl(SSL *ssl,
817  coap_dtls_pki_t* setup_data, coap_dtls_role_t role
818 ) {
819  switch (setup_data->pki_key.key_type) {
820  case COAP_PKI_KEY_PEM:
821  if (setup_data->pki_key.key.pem.public_cert &&
822  setup_data->pki_key.key.pem.public_cert[0]) {
823  if (!(SSL_use_certificate_file(ssl,
824  setup_data->pki_key.key.pem.public_cert,
825  SSL_FILETYPE_PEM))) {
827  "*** setup_pki: (D)TLS: %s: Unable to configure "
828  "%s Certificate\n",
829  setup_data->pki_key.key.pem.public_cert,
830  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
831  return 0;
832  }
833  }
834  else if (role == COAP_DTLS_ROLE_SERVER ||
835  (setup_data->pki_key.key.pem.private_key &&
836  setup_data->pki_key.key.pem.private_key[0])) {
838  "*** setup_pki: (D)TLS: No %s Certificate defined\n",
839  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
840  return 0;
841  }
842  if (setup_data->pki_key.key.pem.private_key &&
843  setup_data->pki_key.key.pem.private_key[0]) {
844  if (!(SSL_use_PrivateKey_file(ssl,
845  setup_data->pki_key.key.pem.private_key,
846  SSL_FILETYPE_PEM))) {
848  "*** setup_pki: (D)TLS: %s: Unable to configure "
849  "Client Private Key\n",
850  setup_data->pki_key.key.pem.private_key);
851  return 0;
852  }
853  }
854  else if (role == COAP_DTLS_ROLE_SERVER ||
855  (setup_data->pki_key.key.pem.public_cert &&
856  setup_data->pki_key.key.pem.public_cert[0])) {
858  "*** setup_pki: (D)TLS: No %s Private Key defined\n",
859  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
860  return 0;
861  }
862  if (setup_data->pki_key.key.pem.ca_file &&
863  setup_data->pki_key.key.pem.ca_file[0]) {
864  X509_STORE *st;
865  BIO *in;
866  X509 *x = NULL;
867  char *rw_var = NULL;
868  SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
869 
870  if (role == COAP_DTLS_ROLE_SERVER) {
871  STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(setup_data->pki_key.key.pem.ca_file);
872 
873  if (cert_names != NULL)
874  SSL_set_client_CA_list(ssl, cert_names);
875  else {
877  "*** setup_pki: (D)TLS: %s: Unable to configure "
878  "%s CA File\n",
879  setup_data->pki_key.key.pem.ca_file,
880  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
881  return 0;
882  }
883  }
884 
885  /* Add CA to the trusted root CA store */
886  in = BIO_new(BIO_s_file());
887  /* Need to do this to not get a compiler warning about const parameters */
888  memcpy(&rw_var, &setup_data->pki_key.key.pem.ca_file, sizeof (rw_var));
889  if (!BIO_read_filename(in, rw_var)) {
890  BIO_free(in);
891  X509_free(x);
892  break;
893  }
894  st = SSL_CTX_get_cert_store(ctx);
895  for (;;) {
896  if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
897  break;
898  add_ca_to_cert_store(st, x);
899  }
900  BIO_free(in);
901  X509_free(x);
902  }
903  break;
904 
905  case COAP_PKI_KEY_ASN1:
906  if (setup_data->pki_key.key.asn1.public_cert &&
907  setup_data->pki_key.key.asn1.public_cert_len > 0) {
908  if (!(SSL_use_certificate_ASN1(ssl,
909  setup_data->pki_key.key.asn1.public_cert,
910  setup_data->pki_key.key.asn1.public_cert_len))) {
912  "*** setup_pki: (D)TLS: %s: Unable to configure "
913  "%s Certificate\n",
914  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client",
915  "ASN1");
916  return 0;
917  }
918  }
919  else if (role == COAP_DTLS_ROLE_SERVER ||
920  (setup_data->pki_key.key.asn1.private_key &&
921  setup_data->pki_key.key.asn1.private_key[0])) {
923  "*** setup_pki: (D)TLS: No %s Certificate defined\n",
924  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
925  return 0;
926  }
927  if (setup_data->pki_key.key.asn1.private_key &&
928  setup_data->pki_key.key.asn1.private_key_len > 0) {
929  int pkey_type = map_key_type(setup_data->pki_key.key.asn1.private_key_type);
930  if (!(SSL_use_PrivateKey_ASN1(pkey_type, ssl,
931  setup_data->pki_key.key.asn1.private_key,
932  setup_data->pki_key.key.asn1.private_key_len))) {
934  "*** setup_pki: (D)TLS: %s: Unable to configure "
935  "%s Private Key\n",
936  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client",
937  "ASN1");
938  return 0;
939  }
940  }
941  else if (role == COAP_DTLS_ROLE_SERVER ||
942  (setup_data->pki_key.key.asn1.public_cert &&
943  setup_data->pki_key.key.asn1.public_cert_len > 0)) {
945  "*** setup_pki: (D)TLS: No %s Private Key defined",
946  role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
947  return 0;
948  }
949  if (setup_data->pki_key.key.asn1.ca_cert &&
950  setup_data->pki_key.key.asn1.ca_cert_len > 0) {
951  /* Need to use a temp variable as it gets incremented*/
952  const uint8_t *p = setup_data->pki_key.key.asn1.ca_cert;
953  X509* x509 = d2i_X509(NULL, &p, setup_data->pki_key.key.asn1.ca_cert_len);
954  X509_STORE *st;
955  SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
956 
957  if (role == COAP_DTLS_ROLE_SERVER) {
958  if (!x509 || !SSL_add_client_CA(ssl, x509)) {
960  "*** setup_pki: (D)TLS: %s: Unable to configure "
961  "client CA File\n",
962  "ASN1");
963  X509_free(x509);
964  return 0;
965  }
966  }
967 
968  /* Add CA to the trusted root CA store */
969  st = SSL_CTX_get_cert_store(ctx);
970  add_ca_to_cert_store(st, x509);
971  X509_free(x509);
972  }
973  break;
974  default:
976  "*** setup_pki: (D)TLS: Unknown key type %d\n",
977  setup_data->pki_key.key_type);
978  return 0;
979  }
980  return 1;
981 }
982 
983 static char*
984 get_common_name_from_cert(X509* x509) {
985  if (x509) {
986  char *cn;
987  int n;
988  STACK_OF(GENERAL_NAME) *san_list;
989  char buffer[256];
990 
991  san_list = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
992  if (san_list) {
993  int san_count = sk_GENERAL_NAME_num(san_list);
994 
995  for (n = 0; n < san_count; n++) {
996  const GENERAL_NAME * name = sk_GENERAL_NAME_value (san_list, n);
997 
998  if (name->type == GEN_DNS) {
999  const char *dns_name = (const char *)ASN1_STRING_get0_data(name->d.dNSName);
1000 
1001  /* Make sure that there is not an embedded NUL in the dns_name */
1002  if (ASN1_STRING_length(name->d.dNSName) != (int)strlen (dns_name))
1003  continue;
1004  cn = OPENSSL_strdup(dns_name);
1005  sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1006  return cn;
1007  }
1008  }
1009  sk_GENERAL_NAME_pop_free(san_list, GENERAL_NAME_free);
1010  }
1011  /* Otherwise look for the CN= field */
1012  X509_NAME_oneline(X509_get_subject_name(x509), buffer, sizeof(buffer));
1013 
1014  /* Need to emulate strcasestr() here. Looking for CN= */
1015  n = strlen(buffer) - 3;
1016  cn = buffer;
1017  while (n > 0) {
1018  if (((cn[0] == 'C') || (cn[0] == 'c')) &&
1019  ((cn[1] == 'N') || (cn[1] == 'n')) &&
1020  (cn[2] == '=')) {
1021  cn += 3;
1022  break;
1023  }
1024  cn++;
1025  n--;
1026  }
1027  if (n > 0) {
1028  char * ecn = strchr(cn, '/');
1029  if (ecn) {
1030  return OPENSSL_strndup(cn, ecn-cn);
1031  }
1032  else {
1033  return OPENSSL_strdup(cn);
1034  }
1035  }
1036  }
1037  return NULL;
1038 }
1039 
1040 static int
1041 tls_verify_call_back(int preverify_ok, X509_STORE_CTX *ctx) {
1042  SSL *ssl = X509_STORE_CTX_get_ex_data(ctx,
1043  SSL_get_ex_data_X509_STORE_CTX_idx());
1044  coap_session_t *session = SSL_get_app_data(ssl);
1045  coap_openssl_context_t *context =
1046  ((coap_openssl_context_t *)session->context->dtls_context);
1047  coap_dtls_pki_t *setup_data = &context->setup_data;
1048  int depth = X509_STORE_CTX_get_error_depth(ctx);
1049  int err = X509_STORE_CTX_get_error(ctx);
1050  X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
1051  char *cn = get_common_name_from_cert(x509);
1052  int keep_preverify_ok = preverify_ok;
1053 
1054  if (!preverify_ok) {
1055  switch (err) {
1056  case X509_V_ERR_CERT_NOT_YET_VALID:
1057  case X509_V_ERR_CERT_HAS_EXPIRED:
1058  if (setup_data->allow_expired_certs)
1059  preverify_ok = 1;
1060  break;
1061  case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1062  if (setup_data->allow_self_signed)
1063  preverify_ok = 1;
1064  break;
1065  case X509_V_ERR_UNABLE_TO_GET_CRL:
1066  if (setup_data->allow_no_crl)
1067  preverify_ok = 1;
1068  break;
1069  case X509_V_ERR_CRL_NOT_YET_VALID:
1070  case X509_V_ERR_CRL_HAS_EXPIRED:
1071  if (setup_data->allow_expired_crl)
1072  preverify_ok = 1;
1073  break;
1074  default:
1075  break;
1076  }
1077  if (!preverify_ok) {
1079  " %s: %s: '%s' depth=%d\n",
1080  coap_session_str(session),
1081  X509_verify_cert_error_string(err), cn ? cn : "?", depth);
1082  /* Invoke the CN callback function for this failure */
1083  keep_preverify_ok = 1;
1084  }
1085  else {
1087  " %s: %s: overridden: '%s' depth=%d\n",
1088  coap_session_str(session),
1089  X509_verify_cert_error_string(err), cn ? cn : "?", depth);
1090  }
1091  }
1092  /* Certificate - depth == 0 is the Client Cert */
1093  if (setup_data->validate_cn_call_back && keep_preverify_ok) {
1094  int length = i2d_X509(x509, NULL);
1095  uint8_t *base_buf;
1096  uint8_t *base_buf2 = base_buf = OPENSSL_malloc(length);
1097 
1098  /* base_buf2 gets moved to the end */
1099  i2d_X509(x509, &base_buf2);
1100  if (!setup_data->validate_cn_call_back(cn, base_buf, length, session,
1101  depth, preverify_ok,
1102  setup_data->cn_call_back_arg)) {
1103  if (depth == 0) {
1104  X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
1105  }
1106  else {
1107  X509_STORE_CTX_set_error(ctx, X509_V_ERR_INVALID_CA);
1108  }
1109  preverify_ok = 0;
1110  }
1111  OPENSSL_free(base_buf);
1112  }
1113  OPENSSL_free(cn);
1114  return preverify_ok;
1115 }
1116 
1117 #if OPENSSL_VERSION_NUMBER < 0x10101000L
1118 /*
1119  * During the SSL/TLS initial negotiations, tls_secret_call_back() is called so
1120  * it is possible to determine whether this is a PKI or PSK incoming
1121  * request and adjust the Ciphers if necessary
1122  *
1123  * Set up by SSL_set_session_secret_cb() in tls_server_name_call_back()
1124  */
1125 static int
1126 tls_secret_call_back(SSL *ssl,
1127  void *secret UNUSED,
1128  int *secretlen UNUSED,
1129  STACK_OF(SSL_CIPHER) *peer_ciphers,
1130  const SSL_CIPHER **cipher UNUSED,
1131  void *arg
1132 ) {
1133  int ii;
1134  int psk_requested = 0;
1135  coap_session_t *session = SSL_get_app_data(ssl);
1136  coap_dtls_pki_t *setup_data = (coap_dtls_pki_t*)arg;
1137 
1138  if (session && session->context->psk_key && session->context->psk_key_len) {
1139  /* Is PSK being requested - if so, we need to change algorithms */
1140  for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) {
1141  const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii);
1142 
1143  if (strstr (SSL_CIPHER_get_name (peer_cipher), "PSK")) {
1144  psk_requested = 1;
1145  break;
1146  }
1147  }
1148  }
1149  if (!psk_requested) {
1150  if (session) {
1151  coap_log(LOG_DEBUG, " %s: Using PKI ciphers\n",
1152  coap_session_str(session));
1153  }
1154  else {
1155  coap_log(LOG_DEBUG, "Using PKI ciphers\n");
1156  }
1157  if (setup_data->verify_peer_cert) {
1158  if (setup_data->require_peer_cert) {
1159  SSL_set_verify(ssl,
1160  SSL_VERIFY_PEER |
1161  SSL_VERIFY_CLIENT_ONCE |
1162  SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1163  tls_verify_call_back);
1164  }
1165  else {
1166  SSL_set_verify(ssl,
1167  SSL_VERIFY_PEER |
1168  SSL_VERIFY_CLIENT_ONCE,
1169  tls_verify_call_back);
1170  }
1171  }
1172  else {
1173  SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
1174  }
1175 
1176  /* Check CA Chain */
1177  if (setup_data->cert_chain_validation)
1178  SSL_set_verify_depth(ssl, setup_data->cert_chain_verify_depth);
1179 
1180  /* Certificate Revocation */
1181  if (setup_data->check_cert_revocation) {
1182  X509_VERIFY_PARAM *param;
1183 
1184  param = X509_VERIFY_PARAM_new();
1185  X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
1186  SSL_set1_param(ssl, param);
1187  X509_VERIFY_PARAM_free(param);
1188  }
1189  }
1190  else {
1191  if (session) {
1192  if (session->context->psk_key && session->context->psk_key_len) {
1193  memcpy(secret, session->context->psk_key, session->context->psk_key_len);
1194  *secretlen = session->context->psk_key_len;
1195  }
1196  coap_log(LOG_DEBUG, " %s: Setting PSK ciphers\n",
1197  coap_session_str(session));
1198  }
1199  else {
1200  coap_log(LOG_DEBUG, "Setting PSK ciphers\n");
1201  }
1202  /*
1203  * Force a PSK algorithm to be used, so we do PSK
1204  */
1205  SSL_set_cipher_list (ssl, "PSK:!NULL");
1206  SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback);
1207  }
1208  if (setup_data->additional_tls_setup_call_back) {
1209  /* Additional application setup wanted */
1210  if (!setup_data->additional_tls_setup_call_back(ssl, setup_data))
1211  return 0;
1212  }
1213  return 0;
1214 }
1215 
1216 /*
1217  * During the SSL/TLS initial negotiations, tls_server_name_call_back() is called
1218  * so it is possible to set up an extra callback to determine whether this is
1219  * a PKI or PSK incoming request and adjust the Ciphers if necessary
1220  *
1221  * Set up by SSL_CTX_set_tlsext_servername_callback() in coap_dtls_context_set_pki()
1222  */
1223 static int
1224 tls_server_name_call_back(SSL *ssl,
1225  int *sd UNUSED,
1226  void *arg
1227 ) {
1228  coap_dtls_pki_t *setup_data = (coap_dtls_pki_t*)arg;
1229 
1230  if (!ssl) {
1231  return SSL_TLSEXT_ERR_NOACK;
1232  }
1233 
1234  if (setup_data->validate_sni_call_back) {
1235  /* SNI checking requested */
1236  coap_session_t *session = (coap_session_t*)SSL_get_app_data(ssl);
1237  coap_openssl_context_t *context =
1238  ((coap_openssl_context_t *)session->context->dtls_context);
1239  const char *sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1240  size_t i;
1241 
1242  if (!sni || !sni[0]) {
1243  sni = "";
1244  }
1245  for (i = 0; i < context->sni_count; i++) {
1246  if (!strcmp(sni, context->sni_entry_list[i].sni)) {
1247  break;
1248  }
1249  }
1250  if (i == context->sni_count) {
1251  SSL_CTX *ctx;
1252  coap_dtls_pki_t sni_setup_data;
1253  coap_dtls_key_t *new_entry = setup_data->validate_sni_call_back(sni,
1254  setup_data->sni_call_back_arg);
1255  if (!new_entry) {
1256  return SSL_TLSEXT_ERR_ALERT_FATAL;
1257  }
1258  /* Need to set up a new SSL_CTX to switch to */
1259  if (session->proto == COAP_PROTO_DTLS) {
1260  /* Set up DTLS context */
1261  ctx = SSL_CTX_new(DTLS_method());
1262  if (!ctx)
1263  goto error;
1264  SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
1265  SSL_CTX_set_app_data(ctx, &context->dtls);
1266  SSL_CTX_set_read_ahead(ctx, 1);
1267  SSL_CTX_set_cipher_list(ctx, "TLSv1.2:TLSv1.0");
1268  SSL_CTX_set_cookie_generate_cb(ctx, coap_dtls_generate_cookie);
1269  SSL_CTX_set_cookie_verify_cb(ctx, coap_dtls_verify_cookie);
1270  SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
1271  SSL_CTX_set_options(ctx, SSL_OP_NO_QUERY_MTU);
1272  }
1273  else {
1274  /* Set up TLS context */
1275  ctx = SSL_CTX_new(TLS_method());
1276  if (!ctx)
1277  goto error;
1278  SSL_CTX_set_app_data(ctx, &context->tls);
1279  SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
1280  SSL_CTX_set_cipher_list(ctx, "TLSv1.2:TLSv1.0");
1281  SSL_CTX_set_info_callback(ctx, coap_dtls_info_callback);
1282  SSL_CTX_set_alpn_select_cb(ctx, server_alpn_callback, NULL);
1283  }
1284  memset(&sni_setup_data, 0, sizeof(sni_setup_data));
1285  sni_setup_data.pki_key.key_type = new_entry->key_type;
1286  sni_setup_data.pki_key.key.pem = new_entry->key.pem;
1287  sni_setup_data.pki_key.key.asn1<