]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blob - src/verify.c
Rename log level constants from LOG_* to just *.
[tlsproxy/tlsproxy.git] / src / verify.c
1 /*
2  * Verify established TLS connections.
3  *
4  * Copyright (C) 2011-2013  Simon Ruderich
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "tlsproxy.h"
21 #include "verify.h"
22
23 #include <errno.h>
24
25 #include <gnutls/x509.h>
26
27
28 static int get_certificate_path(const char *format,
29         const char *hostname, char *buffer, size_t size);
30
31
32 int verify_tls_connection(gnutls_session_t session, const char *hostname) {
33     int result;
34     char path[TLSPROXY_MAX_PATH_LENGTH];
35
36     size_t size;
37     unsigned int status;
38     gnutls_x509_crt_t cert;
39     const gnutls_datum_t *cert_list;
40     unsigned int cert_list_size;
41     FILE *file;
42     char buffer[66]; /* one line in a PEM file is 64 bytes + '\n' + '\0' */
43     char server_cert[16384];
44     char stored_cert[16384];
45
46     result = gnutls_certificate_verify_peers2(session, &status);
47     /* Verification failed (!= invalid certificate but worse), no need for any
48      * more checks. */
49     if (result < 0) {
50         LOG(WARNING,
51             "verify_tls_connection(): gnutls_certificate_verify_peers2() failed: %s",
52             gnutls_strerror(result));
53         return -1;
54     }
55     /* Definitely an invalid certificate, abort. */
56     if (status & GNUTLS_CERT_EXPIRED
57             || status & GNUTLS_CERT_REVOKED
58             || status & GNUTLS_CERT_NOT_ACTIVATED
59             || status & GNUTLS_CERT_INSECURE_ALGORITHM) {
60         LOG(WARNING,
61             "verify_tls_connection(): invalid server certificate");
62         return -1;
63     }
64
65     /* We only handle X509 certificates for now. Let validation fail to
66      * prevent an attacker from changing the certificate type to prevent
67      * detection. */
68     if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
69         LOG(WARNING,
70             "verify_tls_connection(): no X509 server certificate");
71         return -1;
72     }
73
74     /* Get server certificate. */
75
76     result = gnutls_x509_crt_init(&cert);
77     if (result < 0) {
78         LOG(WARNING,
79             "verify_tls_connection(): gnutls_x509_crt_init() failed: %s",
80             gnutls_strerror(result));
81         return -1;
82     }
83
84     cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
85     if (cert_list == NULL) {
86         LOG(WARNING,
87             "verify_tls_connection(): gnutls_certificate_get_peers() failed");
88         gnutls_x509_crt_deinit(cert);
89         return -1;
90     }
91
92     result = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
93     if (result < 0) {
94         LOG(WARNING,
95             "verify_tls_connection(): gnutls_x509_crt_import() failed: %s",
96             gnutls_strerror(result));
97         gnutls_x509_crt_deinit(cert);
98         return -1;
99     }
100
101     /* Export the certificate as PEM to compare it with the known certificate
102      * stored on disk. */
103     size = sizeof(server_cert);
104     result = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM,
105                                     server_cert, &size);
106     if (result != GNUTLS_E_SUCCESS) {
107         LOG(WARNING,
108             "verify_tls_connection(): gnutls_x509_crt_export() failed: %s",
109             gnutls_strerror(result));
110         gnutls_x509_crt_deinit(cert);
111         return -1;
112     }
113     /* We got the certificate as PEM, free the crt struct. */
114     gnutls_x509_crt_deinit(cert);
115
116     /* Open stored server certificate file. */
117     if (server_certificate_file(&file, hostname, path, sizeof(path)) != 0) {
118         LOG(DEBUG, "server certificate:\n%s", server_cert);
119         return -1;
120     }
121
122     size = 1; /* '\0' */
123     stored_cert[0] = '\0'; /* for strcat() */
124
125     while (fgets(buffer, sizeof(buffer), file) != NULL) {
126         size += strlen(buffer);
127         /* Make sure the buffer is big enough. */
128         if (size >= sizeof(stored_cert)) {
129             LOG(WARNING, "verify_tls_connection(): '%s' too big", path);
130             fclose(file);
131
132             LOG(DEBUG, "server certificate:\n%s", server_cert);
133             return -1;
134         }
135
136         strcat(stored_cert, buffer);
137     }
138     if (ferror(file)) {
139         LOG(WARNING,
140             "verify_tls_connection(): failed to read from '%s': %s",
141             path, strerror(errno));
142         fclose(file);
143
144         LOG(DEBUG, "server certificate:\n%s", server_cert);
145         return -1;
146     }
147     fclose(file);
148
149     /* Check if the server certificate matches our stored certificate. */
150     if (strcmp(stored_cert, server_cert)) {
151         LOG(ERROR,
152             "verify_tls_connection(): server certificate changed!",
153             path, strerror(errno));
154
155         LOG(WARNING, "server certificate:\n%s", server_cert);
156         return -2;
157     }
158
159     /* Check that the proxy certificate file exists and is readable for this
160      * domain. This ensures we send an "invalid" certificate even if the proxy
161      * certificate doesn't exist. */
162     if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
163         return -1;
164     }
165     file = fopen(path, "r");
166     if (file == NULL) {
167         LOG(WARNING,
168             "verify_tls_connection(): proxy certificate doesn't exist: '%s'",
169             path);
170         return -1;
171     }
172     fclose(file);
173
174     return 0;
175 }
176
177
178 static int get_certificate_path(const char *format,
179         const char *hostname, char *buffer, size_t size) {
180     int result;
181
182     /* Hostname too long. */
183     if (size - strlen(format) <= strlen(hostname)) {
184         LOG(WARNING,
185             "get_certificate_path(): hostname too long: '%s'",
186             hostname);
187         return -1;
188     }
189     /* Try to prevent path traversals in hostnames. */
190     if (strstr(hostname, "..") != NULL) {
191         LOG(WARNING,
192             "get_certificate_path(): possible path traversal: '%s'",
193             hostname);
194         return -1;
195     }
196     /* Safe as format is no user input. */
197     result = snprintf(buffer, size, format, hostname);
198     if (result < 0) {
199         LOG_PERROR(ERROR, "get_certificate_path(): snprintf failed");
200         return -1;
201     } else if ((size_t)result >= size) {
202         LOG(ERROR, "get_certificate_path(): snprintf buffer too short");
203         return -1;
204     }
205
206     return 0;
207 }
208
209 int proxy_certificate_path(const char *hostname, char *path, size_t size) {
210     return get_certificate_path(PROXY_SERVER_CERT_FORMAT,
211                                 hostname, path, size);
212 }
213
214 int server_certificate_file(FILE **file, const char *hostname,
215                             char *path, size_t size) {
216     if (get_certificate_path(STORED_SERVER_CERT_FORMAT,
217                              hostname, path, size) != 0) {
218         LOG_PERROR(ERROR,
219                    "server_certificate_file(): failed to get path");
220         return -1;
221     }
222
223     /* Open the stored certificate file. */
224     *file = fopen(path, "rb");
225     if (*file == NULL) {
226         if (global_passthrough_unknown) {
227             LOG(DEBUG,
228                 "server_certificate_file(): failed to open '%s': %s",
229                 path, strerror(errno));
230         } else {
231             LOG(WARNING,
232                 "server_certificate_file(): failed to open '%s': %s",
233                 path, strerror(errno));
234         }
235         /* Couldn't open the file, special case. */
236         return -2;
237     }
238
239     return 0;
240 }