2 * Verify established TLS connections.
4 * Copyright (C) 2011-2013 Simon Ruderich
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.
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.
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/>.
26 #include <gnutls/x509.h>
29 static int get_certificate_path(const char *format,
30 const char *hostname, char *buffer, size_t size);
33 int verify_tls_connection(gnutls_session_t session, const char *hostname) {
35 char path[TLSPROXY_MAX_PATH_LENGTH];
39 gnutls_x509_crt_t cert;
40 const gnutls_datum_t *cert_list;
41 unsigned int cert_list_size;
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];
47 result = gnutls_certificate_verify_peers2(session, &status);
48 /* Verification failed (!= invalid certificate but worse), no need for any
52 "verify_tls_connection(): "
53 "gnutls_certificate_verify_peers2() failed: %s",
54 gnutls_strerror(result));
57 /* Definitely an invalid certificate, abort. */
58 if (status & GNUTLS_CERT_EXPIRED
59 || status & GNUTLS_CERT_REVOKED
60 || status & GNUTLS_CERT_NOT_ACTIVATED
61 || status & GNUTLS_CERT_INSECURE_ALGORITHM) {
62 LOG(WARNING, "verify_tls_connection(): invalid server certificate");
66 /* We only handle X509 certificates for now. Let validation fail to
67 * prevent an attacker from changing the certificate type to prevent
69 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
70 LOG(WARNING, "verify_tls_connection(): no X509 server certificate");
74 /* Get server certificate. */
76 result = gnutls_x509_crt_init(&cert);
79 "verify_tls_connection(): gnutls_x509_crt_init() failed: %s",
80 gnutls_strerror(result));
84 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
85 if (cert_list == NULL) {
87 "verify_tls_connection(): gnutls_certificate_get_peers() failed");
88 gnutls_x509_crt_deinit(cert);
92 result = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
95 "verify_tls_connection(): gnutls_x509_crt_import() failed: %s",
96 gnutls_strerror(result));
97 gnutls_x509_crt_deinit(cert);
101 /* Export the certificate as PEM to compare it with the known certificate
103 size = sizeof(server_cert);
104 result = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM,
106 if (result != GNUTLS_E_SUCCESS) {
108 "verify_tls_connection(): gnutls_x509_crt_export() failed: %s",
109 gnutls_strerror(result));
110 gnutls_x509_crt_deinit(cert);
113 /* We got the certificate as PEM, free the crt struct. */
114 gnutls_x509_crt_deinit(cert);
116 /* Open stored server certificate file. */
117 if (server_certificate_file(&file, hostname, path, sizeof(path)) != 0) {
118 LOG(DEBUG1, "server certificate:\n%s", server_cert);
123 stored_cert[0] = '\0'; /* for strcat() */
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);
132 LOG(DEBUG1, "server certificate:\n%s", server_cert);
136 strcat(stored_cert, buffer);
139 LOG(WARNING, "verify_tls_connection(): failed to read from '%s': %s",
140 path, strerror(errno));
143 LOG(DEBUG1, "server certificate:\n%s", server_cert);
148 /* Check if the server certificate matches our stored certificate. */
149 if (strcmp(stored_cert, server_cert)) {
150 LOG(ERROR, "verify_tls_connection(): server certificate changed!",
151 path, strerror(errno));
153 LOG(WARNING, "server certificate:\n%s", server_cert);
157 /* Check that the proxy certificate file exists and is readable for this
158 * domain. This ensures we send an "invalid" certificate even if the proxy
159 * certificate doesn't exist. */
160 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
163 file = fopen(path, "r");
166 "verify_tls_connection(): proxy certificate doesn't exist: '%s'",
176 static int get_certificate_path(const char *format,
177 const char *hostname, char *buffer, size_t size) {
180 /* Hostname too long. */
181 assert(size > strlen(format));
182 if (size - strlen(format) <= strlen(hostname)) {
183 LOG(WARNING, "get_certificate_path(): hostname too long: '%s'",
187 /* Try to prevent path traversals in hostnames. */
188 if (strstr(hostname, "..") != NULL) {
189 LOG(WARNING, "get_certificate_path(): possible path traversal: '%s'",
193 /* Safe as format is no user input. */
194 result = snprintf(buffer, size, format, hostname);
196 LOG_PERROR(ERROR, "get_certificate_path(): snprintf failed");
198 } else if ((size_t)result >= size) {
199 LOG(ERROR, "get_certificate_path(): snprintf buffer too short");
206 int proxy_certificate_path(const char *hostname, char *path, size_t size) {
207 return get_certificate_path(PROXY_SERVER_CERT_FORMAT,
208 hostname, path, size);
211 int server_certificate_file(FILE **file, const char *hostname,
212 char *path, size_t size) {
213 if (get_certificate_path(STORED_SERVER_CERT_FORMAT,
214 hostname, path, size) != 0) {
215 LOG_PERROR(ERROR, "server_certificate_file(): failed to get path");
219 /* Open the stored certificate file. */
220 *file = fopen(path, "rb");
222 if (global_passthrough_unknown) {
223 LOG(DEBUG1, "server_certificate_file(): failed to open '%s': %s",
224 path, strerror(errno));
226 LOG(WARNING, "server_certificate_file(): failed to open '%s': %s",
227 path, strerror(errno));
229 /* Couldn't open the file, special case. */