tls_mosq.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright (c) 2013-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #ifdef WITH_TLS
  16. #ifdef WIN32
  17. # include <winsock2.h>
  18. # include <ws2tcpip.h>
  19. #else
  20. # include <arpa/inet.h>
  21. # include <sys/socket.h>
  22. # include <strings.h>
  23. #endif
  24. #include <string.h>
  25. #include <openssl/conf.h>
  26. #include <openssl/x509v3.h>
  27. #include <openssl/ssl.h>
  28. #ifdef WITH_BROKER
  29. # include "mosquitto_broker_internal.h"
  30. #endif
  31. #include "mosquitto_internal.h"
  32. #include "logging_mosq.h"
  33. #include "tls_mosq.h"
  34. extern int tls_ex_index_mosq;
  35. int mosquitto__server_certificate_verify(int preverify_ok, X509_STORE_CTX *ctx)
  36. {
  37. /* Preverify should have already checked expiry, revocation.
  38. * We need to verify the hostname. */
  39. struct mosquitto *mosq;
  40. SSL *ssl;
  41. X509 *cert;
  42. /* Always reject if preverify_ok has failed. */
  43. if(!preverify_ok) return 0;
  44. ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  45. mosq = SSL_get_ex_data(ssl, tls_ex_index_mosq);
  46. if(!mosq) return 0;
  47. if(mosq->tls_insecure == false
  48. #ifndef WITH_BROKER
  49. && mosq->port != 0 /* no hostname checking for unix sockets */
  50. #endif
  51. ){
  52. if(X509_STORE_CTX_get_error_depth(ctx) == 0){
  53. /* FIXME - use X509_check_host() etc. for sufficiently new openssl (>=1.1.x) */
  54. cert = X509_STORE_CTX_get_current_cert(ctx);
  55. /* This is the peer certificate, all others are upwards in the chain. */
  56. #if defined(WITH_BROKER)
  57. preverify_ok = mosquitto__verify_certificate_hostname(cert, mosq->bridge->addresses[mosq->bridge->cur_address].address);
  58. #else
  59. preverify_ok = mosquitto__verify_certificate_hostname(cert, mosq->host);
  60. #endif
  61. if (preverify_ok != 1) {
  62. log__printf(mosq, MOSQ_LOG_ERR, "Error: host name verification failed.");
  63. }
  64. return preverify_ok;
  65. }else{
  66. return preverify_ok;
  67. }
  68. }else{
  69. return preverify_ok;
  70. }
  71. }
  72. static int mosquitto__cmp_hostname_wildcard(char *certname, const char *hostname)
  73. {
  74. size_t i;
  75. size_t len;
  76. if(!certname || !hostname){
  77. return 1;
  78. }
  79. if(certname[0] == '*'){
  80. if(certname[1] != '.'){
  81. return 1;
  82. }
  83. certname += 2;
  84. len = strlen(hostname);
  85. for(i=0; i<len-1; i++){
  86. if(hostname[i] == '.'){
  87. hostname += i+1;
  88. break;
  89. }
  90. }
  91. return strcasecmp(certname, hostname);
  92. }else{
  93. return strcasecmp(certname, hostname);
  94. }
  95. }
  96. /* This code is based heavily on the example provided in "Secure Programming
  97. * Cookbook for C and C++".
  98. */
  99. int mosquitto__verify_certificate_hostname(X509 *cert, const char *hostname)
  100. {
  101. int i;
  102. char name[256];
  103. X509_NAME *subj;
  104. bool have_san_dns = false;
  105. STACK_OF(GENERAL_NAME) *san;
  106. const GENERAL_NAME *nval;
  107. const unsigned char *data;
  108. unsigned char ipv6_addr[16];
  109. unsigned char ipv4_addr[4];
  110. int ipv6_ok;
  111. int ipv4_ok;
  112. #ifdef WIN32
  113. ipv6_ok = InetPton(AF_INET6, hostname, &ipv6_addr);
  114. ipv4_ok = InetPton(AF_INET, hostname, &ipv4_addr);
  115. #else
  116. ipv6_ok = inet_pton(AF_INET6, hostname, &ipv6_addr);
  117. ipv4_ok = inet_pton(AF_INET, hostname, &ipv4_addr);
  118. #endif
  119. san = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
  120. if(san){
  121. for(i=0; i<sk_GENERAL_NAME_num(san); i++){
  122. nval = sk_GENERAL_NAME_value(san, i);
  123. if(nval->type == GEN_DNS){
  124. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  125. data = ASN1_STRING_data(nval->d.dNSName);
  126. #else
  127. data = ASN1_STRING_get0_data(nval->d.dNSName);
  128. #endif
  129. if(data && !mosquitto__cmp_hostname_wildcard((char *)data, hostname)){
  130. sk_GENERAL_NAME_pop_free(san, GENERAL_NAME_free);
  131. return 1;
  132. }
  133. have_san_dns = true;
  134. }else if(nval->type == GEN_IPADD){
  135. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  136. data = ASN1_STRING_data(nval->d.iPAddress);
  137. #else
  138. data = ASN1_STRING_get0_data(nval->d.iPAddress);
  139. #endif
  140. if(nval->d.iPAddress->length == 4 && ipv4_ok){
  141. if(!memcmp(ipv4_addr, data, 4)){
  142. sk_GENERAL_NAME_pop_free(san, GENERAL_NAME_free);
  143. return 1;
  144. }
  145. }else if(nval->d.iPAddress->length == 16 && ipv6_ok){
  146. if(!memcmp(ipv6_addr, data, 16)){
  147. sk_GENERAL_NAME_pop_free(san, GENERAL_NAME_free);
  148. return 1;
  149. }
  150. }
  151. }
  152. }
  153. sk_GENERAL_NAME_pop_free(san, GENERAL_NAME_free);
  154. if(have_san_dns){
  155. /* Only check CN if subjectAltName DNS entry does not exist. */
  156. return 0;
  157. }
  158. }
  159. subj = X509_get_subject_name(cert);
  160. if(X509_NAME_get_text_by_NID(subj, NID_commonName, name, sizeof(name)) > 0){
  161. name[sizeof(name) - 1] = '\0';
  162. if (!mosquitto__cmp_hostname_wildcard(name, hostname)) return 1;
  163. }
  164. return 0;
  165. }
  166. #endif