X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fverify.c;h=34a081493e81b1a41ef044ff69dffb77d2c8ef99;hb=b7ce41b809a3b3533d398c0da8fd9787c149434c;hp=1df9b511686b8e2f17447513ffe45e53c1f7c434;hpb=718112774884f6b9420d55b506bcdc85012ca366;p=tlsproxy%2Ftlsproxy.git diff --git a/src/verify.c b/src/verify.c index 1df9b51..34a0814 100644 --- a/src/verify.c +++ b/src/verify.c @@ -1,7 +1,7 @@ /* * Verify established TLS connections. * - * Copyright (C) 2011 Simon Ruderich + * Copyright (C) 2011-2013 Simon Ruderich * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,9 +20,9 @@ #include "tlsproxy.h" #include "verify.h" -/* errno */ +#include #include -/* gnutls_x509_*() */ + #include @@ -32,7 +32,7 @@ static int get_certificate_path(const char *format, int verify_tls_connection(gnutls_session_t session, const char *hostname) { int result; - char path[1024]; + char path[TLSPROXY_MAX_PATH_LENGTH]; size_t size; unsigned int status; @@ -41,15 +41,16 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { unsigned int cert_list_size; FILE *file; char buffer[66]; /* one line in a PEM file is 64 bytes + '\n' + '\0' */ - char server_cert[8192]; - char stored_cert[8192]; + char server_cert[16384]; + char stored_cert[16384]; result = gnutls_certificate_verify_peers2(session, &status); /* Verification failed (!= invalid certificate but worse), no need for any * more checks. */ - if (0 > result) { - LOG(LOG_WARNING, - "verify_tls_connection(): gnutls_certificate_verify_peers2() failed: %s", + if (result < 0) { + LOG(WARNING, + "verify_tls_connection(): " + "gnutls_certificate_verify_peers2() failed: %s", gnutls_strerror(result)); return -1; } @@ -58,40 +59,39 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { || status & GNUTLS_CERT_REVOKED || status & GNUTLS_CERT_NOT_ACTIVATED || status & GNUTLS_CERT_INSECURE_ALGORITHM) { - LOG(LOG_WARNING, - "verify_tls_connection(): invalid server certificate"); + LOG(WARNING, "verify_tls_connection(): invalid server certificate"); return -1; } /* We only handle X509 certificates for now. Let validation fail to * prevent an attacker from changing the certificate type to prevent * detection. */ - if (GNUTLS_CRT_X509 != gnutls_certificate_type_get(session)) { - LOG(LOG_WARNING, - "verify_tls_connection(): no X509 server certificate"); + if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) { + LOG(WARNING, "verify_tls_connection(): no X509 server certificate"); return -1; } /* Get server certificate. */ - if (0 > (result = gnutls_x509_crt_init(&cert))) { - LOG(LOG_WARNING, + result = gnutls_x509_crt_init(&cert); + if (result < 0) { + LOG(WARNING, "verify_tls_connection(): gnutls_x509_crt_init() failed: %s", gnutls_strerror(result)); return -1; } cert_list = gnutls_certificate_get_peers(session, &cert_list_size); - if (NULL == cert_list) { - LOG(LOG_WARNING, + if (cert_list == NULL) { + LOG(WARNING, "verify_tls_connection(): gnutls_certificate_get_peers() failed"); gnutls_x509_crt_deinit(cert); return -1; } - if (0 > (result = gnutls_x509_crt_import(cert, &cert_list[0], - GNUTLS_X509_FMT_DER))) { - LOG(LOG_WARNING, + result = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER); + if (result < 0) { + LOG(WARNING, "verify_tls_connection(): gnutls_x509_crt_import() failed: %s", gnutls_strerror(result)); gnutls_x509_crt_deinit(cert); @@ -103,8 +103,8 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { size = sizeof(server_cert); result = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, server_cert, &size); - if (GNUTLS_E_SUCCESS != result) { - LOG(LOG_WARNING, + if (result != GNUTLS_E_SUCCESS) { + LOG(WARNING, "verify_tls_connection(): gnutls_x509_crt_export() failed: %s", gnutls_strerror(result)); gnutls_x509_crt_deinit(cert); @@ -114,48 +114,61 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { gnutls_x509_crt_deinit(cert); /* Open stored server certificate file. */ - if (0 != server_certificate_path(&file, hostname, path, sizeof(path))) { - LOG(LOG_DEBUG, "server certificate:\n%s", server_cert); + if (server_certificate_file(&file, hostname, path, sizeof(path)) != 0) { + LOG(DEBUG, "server certificate:\n%s", server_cert); return -1; } size = 1; /* '\0' */ stored_cert[0] = '\0'; /* for strcat() */ - while (NULL != fgets(buffer, sizeof(buffer), file)) { + while (fgets(buffer, sizeof(buffer), file) != NULL) { size += strlen(buffer); /* Make sure the buffer is big enough. */ - if (sizeof(stored_cert) <= size) { - LOG(LOG_WARNING, "verify_tls_connection(): '%s' too big", path); + if (size >= sizeof(stored_cert)) { + LOG(WARNING, "verify_tls_connection(): '%s' too big", path); fclose(file); - LOG(LOG_DEBUG, "server certificate:\n%s", server_cert); + LOG(DEBUG, "server certificate:\n%s", server_cert); return -1; } strcat(stored_cert, buffer); } if (ferror(file)) { + LOG(WARNING, "verify_tls_connection(): failed to read from '%s': %s", + path, strerror(errno)); fclose(file); - LOG(LOG_WARNING, - "verify_tls_connection(): failed to read from '%s': %s", - path, strerror(errno)); - LOG(LOG_DEBUG, "server certificate:\n%s", server_cert); + LOG(DEBUG, "server certificate:\n%s", server_cert); return -1; } fclose(file); /* Check if the server certificate matches our stored certificate. */ - if (0 != strcmp(stored_cert, server_cert)) { - LOG(LOG_ERROR, - "verify_tls_connection(): server certificate changed!", - path, strerror(errno)); + if (strcmp(stored_cert, server_cert)) { + LOG(ERROR, "verify_tls_connection(): server certificate changed!", + path, strerror(errno)); - LOG(LOG_WARNING, "server certificate:\n%s", server_cert); + LOG(WARNING, "server certificate:\n%s", server_cert); return -2; } + /* Check that the proxy certificate file exists and is readable for this + * domain. This ensures we send an "invalid" certificate even if the proxy + * certificate doesn't exist. */ + if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) { + return -1; + } + file = fopen(path, "r"); + if (file == NULL) { + LOG(WARNING, + "verify_tls_connection(): proxy certificate doesn't exist: '%s'", + path); + return -1; + } + fclose(file); + return 0; } @@ -165,26 +178,25 @@ static int get_certificate_path(const char *format, int result; /* Hostname too long. */ + assert(size > strlen(format)); if (size - strlen(format) <= strlen(hostname)) { - LOG(LOG_WARNING, - "get_certificate_path(): hostname too long: '%s'", - hostname); + LOG(WARNING, "get_certificate_path(): hostname too long: '%s'", + hostname); return -1; } /* Try to prevent path traversals in hostnames. */ - if (NULL != strstr(hostname, "..")) { - LOG(LOG_WARNING, - "get_certificate_path(): possible path traversal: '%s'", - hostname); + if (strstr(hostname, "..") != NULL) { + LOG(WARNING, "get_certificate_path(): possible path traversal: '%s'", + hostname); return -1; } /* Safe as format is no user input. */ result = snprintf(buffer, size, format, hostname); if (result < 0) { - LOG_PERROR(LOG_ERROR, "get_certificate_path(): snprintf failed"); + LOG_PERROR(ERROR, "get_certificate_path(): snprintf failed"); return -1; } else if ((size_t)result >= size) { - LOG(LOG_ERROR, "get_certificate_path(): snprintf buffer too short"); + LOG(ERROR, "get_certificate_path(): snprintf buffer too short"); return -1; } @@ -196,26 +208,23 @@ int proxy_certificate_path(const char *hostname, char *path, size_t size) { hostname, path, size); } -int server_certificate_path(FILE **file, const char *hostname, +int server_certificate_file(FILE **file, const char *hostname, char *path, size_t size) { - if (0 != get_certificate_path(STORED_SERVER_CERT_FORMAT, - hostname, path, size)) { - LOG_PERROR(LOG_ERROR, - "server_certificate_path(): failed to get path"); + if (get_certificate_path(STORED_SERVER_CERT_FORMAT, + hostname, path, size) != 0) { + LOG_PERROR(ERROR, "server_certificate_file(): failed to get path"); return -1; } /* Open the stored certificate file. */ *file = fopen(path, "rb"); - if (NULL == *file) { + if (*file == NULL) { if (global_passthrough_unknown) { - LOG(LOG_DEBUG, - "server_certificate_path(): failed to open '%s': %s", - path, strerror(errno)); + LOG(DEBUG, "server_certificate_file(): failed to open '%s': %s", + path, strerror(errno)); } else { - LOG(LOG_WARNING, - "server_certificate_path(): failed to open '%s': %s", - path, strerror(errno)); + LOG(WARNING, "server_certificate_file(): failed to open '%s': %s", + path, strerror(errno)); } /* Couldn't open the file, special case. */ return -2;