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 /* Compatibility for older GnuTLS versions. Define the constants in a way
30 * which doesn't affect the status check below. */
31 #ifndef GNUTLS_CERT_SIGNER_NOT_CA
32 # define GNUTLS_CERT_SIGNER_NOT_CA 0
34 #ifndef GNUTLS_CERT_SIGNATURE_FAILURE
35 # define GNUTLS_CERT_SIGNATURE_FAILURE 0
37 #ifndef GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED
38 # define GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED 0
40 #ifndef GNUTLS_CERT_UNEXPECTED_OWNER
41 # define GNUTLS_CERT_UNEXPECTED_OWNER 0
43 #ifndef GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE
44 # define GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE 0
46 #ifndef GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE
47 # define GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE 0
49 #ifndef GNUTLS_CERT_MISMATCH
50 # define GNUTLS_CERT_MISMATCH 0
54 static int get_certificate_path(const char *format,
55 const char *hostname, char *buffer, size_t size);
58 int verify_tls_connection(gnutls_session_t session, const char *hostname) {
60 char path[TLSPROXY_MAX_PATH_LENGTH];
64 gnutls_x509_crt_t cert;
65 const gnutls_datum_t *cert_list;
66 unsigned int cert_list_size;
68 char buffer[66]; /* one line in a PEM file is 64 bytes + '\n' + '\0' */
69 char server_cert[16384];
70 char stored_cert[16384];
72 result = gnutls_certificate_verify_peers2(session, &status);
73 /* Verification failed (!= invalid certificate but worse), no need for any
77 "verify_tls_connection(): "
78 "gnutls_certificate_verify_peers2() failed: %s",
79 gnutls_strerror(result));
82 /* Definitely an invalid certificate, abort. */
83 if (status & GNUTLS_CERT_REVOKED
84 || status & GNUTLS_CERT_SIGNER_NOT_CA
85 || status & GNUTLS_CERT_INSECURE_ALGORITHM
86 || status & GNUTLS_CERT_NOT_ACTIVATED
87 || status & GNUTLS_CERT_EXPIRED
88 || status & GNUTLS_CERT_SIGNATURE_FAILURE
89 || status & GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED
90 || status & GNUTLS_CERT_UNEXPECTED_OWNER
91 || status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE
92 || status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE
93 || status & GNUTLS_CERT_MISMATCH) {
94 LOG(WARNING, "verify_tls_connection(): invalid server certificate");
98 /* We only handle X509 certificates for now. Let validation fail to
99 * prevent an attacker from changing the certificate type to prevent
101 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
102 LOG(WARNING, "verify_tls_connection(): no X509 server certificate");
106 /* Get server certificate. */
108 result = gnutls_x509_crt_init(&cert);
111 "verify_tls_connection(): gnutls_x509_crt_init() failed: %s",
112 gnutls_strerror(result));
116 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
117 if (cert_list == NULL) {
119 "verify_tls_connection(): gnutls_certificate_get_peers() failed");
120 gnutls_x509_crt_deinit(cert);
124 result = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
127 "verify_tls_connection(): gnutls_x509_crt_import() failed: %s",
128 gnutls_strerror(result));
129 gnutls_x509_crt_deinit(cert);
133 /* Export the certificate as PEM to compare it with the known certificate
135 size = sizeof(server_cert);
136 result = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM,
138 if (result != GNUTLS_E_SUCCESS) {
140 "verify_tls_connection(): gnutls_x509_crt_export() failed: %s",
141 gnutls_strerror(result));
142 gnutls_x509_crt_deinit(cert);
145 /* We got the certificate as PEM, free the crt struct. */
146 gnutls_x509_crt_deinit(cert);
148 /* Open stored server certificate file. */
149 if (server_certificate_file(&file, hostname, path, sizeof(path)) != 0) {
150 LOG(DEBUG1, "server certificate:\n%s", server_cert);
155 stored_cert[0] = '\0'; /* for strcat() */
157 while (fgets(buffer, sizeof(buffer), file) != NULL) {
158 size += strlen(buffer);
159 /* Make sure the buffer is big enough. */
160 if (size >= sizeof(stored_cert)) {
161 LOG(WARNING, "verify_tls_connection(): '%s' too big", path);
164 LOG(DEBUG1, "server certificate:\n%s", server_cert);
168 strcat(stored_cert, buffer);
171 LOG(WARNING, "verify_tls_connection(): failed to read from '%s': %s",
172 path, strerror(errno));
175 LOG(DEBUG1, "server certificate:\n%s", server_cert);
180 /* Check if the server certificate matches our stored certificate. */
181 if (strcmp(stored_cert, server_cert)) {
182 LOG(ERROR, "verify_tls_connection(): server certificate changed!",
183 path, strerror(errno));
185 LOG(WARNING, "server certificate:\n%s", server_cert);
189 /* Check that the proxy certificate file exists and is readable for this
190 * domain. This ensures we send an "invalid" certificate even if the proxy
191 * certificate doesn't exist. */
192 if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
195 file = fopen(path, "r");
198 "verify_tls_connection(): proxy certificate doesn't exist: '%s'",
208 static int get_certificate_path(const char *format,
209 const char *hostname, char *buffer, size_t size) {
212 /* Hostname too long. */
213 assert(size > strlen(format));
214 if (size - strlen(format) <= strlen(hostname)) {
215 LOG(WARNING, "get_certificate_path(): hostname too long: '%s'",
219 /* Try to prevent path traversals in hostnames. */
220 if (strstr(hostname, "..") != NULL) {
221 LOG(WARNING, "get_certificate_path(): possible path traversal: '%s'",
225 /* Safe as format is no user input. */
226 result = snprintf(buffer, size, format, hostname);
228 LOG_PERROR(ERROR, "get_certificate_path(): snprintf failed");
230 } else if ((size_t)result >= size) {
231 LOG(ERROR, "get_certificate_path(): snprintf buffer too short");
238 int proxy_certificate_path(const char *hostname, char *path, size_t size) {
239 return get_certificate_path(PROXY_SERVER_CERT_FORMAT,
240 hostname, path, size);
243 int server_certificate_file(FILE **file, const char *hostname,
244 char *path, size_t size) {
245 if (get_certificate_path(STORED_SERVER_CERT_FORMAT,
246 hostname, path, size) != 0) {
247 LOG_PERROR(ERROR, "server_certificate_file(): failed to get path");
251 /* Open the stored certificate file. */
252 *file = fopen(path, "rb");
254 if (global_passthrough_unknown) {
255 LOG(DEBUG1, "server_certificate_file(): failed to open '%s': %s",
256 path, strerror(errno));
258 LOG(WARNING, "server_certificate_file(): failed to open '%s': %s",
259 path, strerror(errno));
261 /* Couldn't open the file, special case. */