]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blobdiff - src/verify.c
src/verify.c: Remove duplicated code.
[tlsproxy/tlsproxy.git] / src / verify.c
index 0052ca61cdb73bb5d2994bbe30df22a697b982c8..1df9b511686b8e2f17447513ffe45e53c1f7c434 100644 (file)
 #include <gnutls/x509.h>
 
 
+static int get_certificate_path(const char *format,
+        const char *hostname, char *buffer, size_t size);
+
+
 int verify_tls_connection(gnutls_session_t session, const char *hostname) {
     int result;
     char path[1024];
@@ -106,31 +110,12 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) {
         gnutls_x509_crt_deinit(cert);
         return -1;
     }
-    /* We got the certificate as PEM, free it. */
+    /* We got the certificate as PEM, free the crt struct. */
     gnutls_x509_crt_deinit(cert);
 
-    /* Hostname too long. */
-    if (sizeof(path) - strlen(STORED_SERVER_CERT_FORMAT) <= strlen(hostname)) {
-        LOG(LOG_WARNING,
-            "verify_tls_connection(): hostname too long: '%s'",
-            hostname);
-        return -1;
-    }
-    /* Try to prevent path traversals in hostnames. */
-    if (NULL != strstr(hostname, "..")) {
-        LOG(LOG_WARNING,
-            "verify_tls_connection(): possible path traversal: '%s'",
-            hostname);
-        return -1;
-    }
-    snprintf(path, sizeof(path), STORED_SERVER_CERT_FORMAT, hostname);
-
-    /* Open the stored certificate file. */
-    file = fopen(path, "rb");
-    if (NULL == file) {
-        LOG(LOG_WARNING,
-            "verify_tls_connection(): failed to open '%s': %s",
-            path, strerror(errno));
+    /* Open stored server certificate file. */
+    if (0 != server_certificate_path(&file, hostname, path, sizeof(path))) {
+        LOG(LOG_DEBUG, "server certificate:\n%s", server_cert);
         return -1;
     }
 
@@ -141,9 +126,10 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) {
         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);
+            LOG(LOG_WARNING, "verify_tls_connection(): '%s' too big", path);
             fclose(file);
+
+            LOG(LOG_DEBUG, "server certificate:\n%s", server_cert);
             return -1;
         }
 
@@ -154,6 +140,8 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) {
         LOG(LOG_WARNING,
             "verify_tls_connection(): failed to read from '%s': %s",
             path, strerror(errno));
+
+        LOG(LOG_DEBUG, "server certificate:\n%s", server_cert);
         return -1;
     }
     fclose(file);
@@ -163,6 +151,73 @@ int verify_tls_connection(gnutls_session_t session, const char *hostname) {
         LOG(LOG_ERROR,
             "verify_tls_connection(): server certificate changed!",
             path, strerror(errno));
+
+        LOG(LOG_WARNING, "server certificate:\n%s", server_cert);
+        return -2;
+    }
+
+    return 0;
+}
+
+
+static int get_certificate_path(const char *format,
+        const char *hostname, char *buffer, size_t size) {
+    int result;
+
+    /* Hostname too long. */
+    if (size - strlen(format) <= strlen(hostname)) {
+        LOG(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);
+        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");
+        return -1;
+    } else if ((size_t)result >= size) {
+        LOG(LOG_ERROR, "get_certificate_path(): snprintf buffer too short");
+        return -1;
+    }
+
+    return 0;
+}
+
+int proxy_certificate_path(const char *hostname, char *path, size_t size) {
+    return get_certificate_path(PROXY_SERVER_CERT_FORMAT,
+                                hostname, path, size);
+}
+
+int server_certificate_path(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");
+        return -1;
+    }
+
+    /* Open the stored certificate file. */
+    *file = fopen(path, "rb");
+    if (NULL == *file) {
+        if (global_passthrough_unknown) {
+            LOG(LOG_DEBUG,
+                "server_certificate_path(): failed to open '%s': %s",
+                path, strerror(errno));
+        } else {
+            LOG(LOG_WARNING,
+                "server_certificate_path(): failed to open '%s': %s",
+                path, strerror(errno));
+        }
+        /* Couldn't open the file, special case. */
         return -2;
     }