Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-fan / lib / tls_mosq.c
1 /*
2 Copyright (c) 2013 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9    this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifdef WITH_TLS
31
32 #ifdef WIN32
33 #  include <winsock2.h>
34 #  include <ws2tcpip.h>
35 #else
36 #  include <arpa/inet.h>
37 #endif
38
39 #include <string.h>
40 #include <strings.h>
41 #include <openssl/conf.h>
42 #include <openssl/x509v3.h>
43 #include <openssl/ssl.h>
44
45 #ifdef WITH_BROKER
46 #  include "mosquitto_broker.h"
47 #endif
48 #include "mosquitto_internal.h"
49 #include "tls_mosq.h"
50
51 extern int tls_ex_index_mosq;
52
53 int _mosquitto_server_certificate_verify(int preverify_ok, X509_STORE_CTX *ctx)
54 {
55         /* Preverify should have already checked expiry, revocation.
56          * We need to verify the hostname. */
57         struct mosquitto *mosq;
58         SSL *ssl;
59         X509 *cert;
60
61         /* Always reject if preverify_ok has failed. */
62         if(!preverify_ok) return 0;
63
64         ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
65         mosq = SSL_get_ex_data(ssl, tls_ex_index_mosq);
66         if(!mosq) return 0;
67
68         if(mosq->tls_insecure == false){
69                 if(X509_STORE_CTX_get_error_depth(ctx) == 0){
70                         /* FIXME - use X509_check_host() etc. for sufficiently new openssl (>=1.1.x) */
71                         cert = X509_STORE_CTX_get_current_cert(ctx);
72                         /* This is the peer certificate, all others are upwards in the chain. */
73 #if defined(WITH_BROKER)
74                         return _mosquitto_verify_certificate_hostname(cert, mosq->bridge->addresses[mosq->bridge->cur_address].address);
75 #else
76                         return _mosquitto_verify_certificate_hostname(cert, mosq->host);
77 #endif
78                 }else{
79                         return preverify_ok;
80                 }
81         }else{
82                 return preverify_ok;
83         }
84 }
85
86 /* This code is based heavily on the example provided in "Secure Programming
87  * Cookbook for C and C++".
88  */
89 int _mosquitto_verify_certificate_hostname(X509 *cert, const char *hostname)
90 {
91         int i;
92         char name[256];
93         X509_NAME *subj;
94         bool have_san_dns = false;
95         STACK_OF(GENERAL_NAME) *san;
96         const GENERAL_NAME *nval;
97         const unsigned char *data;
98         unsigned char ipv6_addr[16];
99         unsigned char ipv4_addr[4];
100         int ipv6_ok;
101         int ipv4_ok;
102
103 #ifdef WIN32
104         ipv6_ok = InetPton(AF_INET6, hostname, &ipv6_addr);
105         ipv4_ok = InetPton(AF_INET, hostname, &ipv4_addr);
106 #else
107         ipv6_ok = inet_pton(AF_INET6, hostname, &ipv6_addr);
108         ipv4_ok = inet_pton(AF_INET, hostname, &ipv4_addr);
109 #endif
110
111         san = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
112         if(san){
113                 for(i=0; i<sk_GENERAL_NAME_num(san); i++){
114                         nval = sk_GENERAL_NAME_value(san, i);
115                         if(nval->type == GEN_DNS){
116                                 data = ASN1_STRING_data(nval->d.dNSName);
117                                 if(data && !strcasecmp((char *)data, hostname)){
118                                         return 1;
119                                 }
120                                 have_san_dns = true;
121                         }else if(nval->type == GEN_IPADD){
122                                 data = ASN1_STRING_data(nval->d.iPAddress);
123                                 if(nval->d.iPAddress->length == 4 && ipv4_ok){
124                                         if(!memcmp(ipv4_addr, data, 4)){
125                                                 return 1;
126                                         }
127                                 }else if(nval->d.iPAddress->length == 16 && ipv6_ok){
128                                         if(!memcmp(ipv6_addr, data, 16)){
129                                                 return 1;
130                                         }
131                                 }
132                         }
133                 }
134                 if(have_san_dns){
135                         /* Only check CN if subjectAltName DNS entry does not exist. */
136                         return 0;
137                 }
138         }
139         subj = X509_get_subject_name(cert);
140         if(X509_NAME_get_text_by_NID(subj, NID_commonName, name, sizeof(name)) > 0){
141                 name[sizeof(name) - 1] = '\0';
142                 if (!strcasecmp(name, hostname)) return 1;
143         }
144         return 0;
145 }
146
147 #endif
148