X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fverify.c;h=e361e01b56c676c3ac32f9d4532e43c275e0632d;hb=8c4e41d4ed8b3d8c3a39d3bef01feec98dc82161;hp=6ea5f1b23d07f45be840f813035410129765d466;hpb=d778944d0403b093d43d9cbd94bd298bfc3a8d42;p=tlsproxy%2Ftlsproxy.git diff --git a/src/verify.c b/src/verify.c index 6ea5f1b..e361e01 100644 --- a/src/verify.c +++ b/src/verify.c @@ -20,9 +20,8 @@ #include "tlsproxy.h" #include "verify.h" -/* errno */ #include -/* gnutls_x509_*() */ + #include @@ -41,13 +40,13 @@ 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) { + if (result < 0) { LOG(LOG_WARNING, "verify_tls_connection(): gnutls_certificate_verify_peers2() failed: %s", gnutls_strerror(result)); @@ -66,7 +65,7 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { /* 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)) { + if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) { LOG(LOG_WARNING, "verify_tls_connection(): no X509 server certificate"); return -1; @@ -74,7 +73,8 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { /* Get server certificate. */ - if (0 > (result = gnutls_x509_crt_init(&cert))) { + result = gnutls_x509_crt_init(&cert); + if (result < 0) { LOG(LOG_WARNING, "verify_tls_connection(): gnutls_x509_crt_init() failed: %s", gnutls_strerror(result)); @@ -82,15 +82,15 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { } cert_list = gnutls_certificate_get_peers(session, &cert_list_size); - if (NULL == cert_list) { + if (cert_list == NULL) { LOG(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))) { + result = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER); + if (result < 0) { LOG(LOG_WARNING, "verify_tls_connection(): gnutls_x509_crt_import() failed: %s", gnutls_strerror(result)); @@ -103,7 +103,7 @@ 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) { + if (result != GNUTLS_E_SUCCESS) { LOG(LOG_WARNING, "verify_tls_connection(): gnutls_x509_crt_export() failed: %s", gnutls_strerror(result)); @@ -114,7 +114,7 @@ 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_file(&file, hostname, path, sizeof(path))) { + if (server_certificate_file(&file, hostname, path, sizeof(path)) != 0) { LOG(LOG_DEBUG, "server certificate:\n%s", server_cert); return -1; } @@ -122,10 +122,10 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { 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) { + if (size >= sizeof(stored_cert)) { LOG(LOG_WARNING, "verify_tls_connection(): '%s' too big", path); fclose(file); @@ -136,10 +136,10 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { strcat(stored_cert, buffer); } if (ferror(file)) { - fclose(file); LOG(LOG_WARNING, "verify_tls_connection(): failed to read from '%s': %s", path, strerror(errno)); + fclose(file); LOG(LOG_DEBUG, "server certificate:\n%s", server_cert); return -1; @@ -147,7 +147,7 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { fclose(file); /* Check if the server certificate matches our stored certificate. */ - if (0 != strcmp(stored_cert, server_cert)) { + if (strcmp(stored_cert, server_cert)) { LOG(LOG_ERROR, "verify_tls_connection(): server certificate changed!", path, strerror(errno)); @@ -159,11 +159,11 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) { /* 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 (0 != proxy_certificate_path(hostname, path, sizeof(path))) { + if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) { return -1; } file = fopen(path, "r"); - if (NULL == file) { + if (file == NULL) { LOG(LOG_WARNING, "verify_tls_connection(): proxy certificate doesn't exist: '%s'", path); @@ -187,7 +187,7 @@ static int get_certificate_path(const char *format, return -1; } /* Try to prevent path traversals in hostnames. */ - if (NULL != strstr(hostname, "..")) { + if (strstr(hostname, "..") != NULL) { LOG(LOG_WARNING, "get_certificate_path(): possible path traversal: '%s'", hostname); @@ -213,8 +213,8 @@ int proxy_certificate_path(const char *hostname, char *path, size_t size) { 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)) { + if (get_certificate_path(STORED_SERVER_CERT_FORMAT, + hostname, path, size) != 0) { LOG_PERROR(LOG_ERROR, "server_certificate_file(): failed to get path"); return -1; @@ -222,7 +222,7 @@ int server_certificate_file(FILE **file, const char *hostname, /* Open the stored certificate file. */ *file = fopen(path, "rb"); - if (NULL == *file) { + if (*file == NULL) { if (global_passthrough_unknown) { LOG(LOG_DEBUG, "server_certificate_file(): failed to open '%s': %s",