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