+ LOG(DEBUG1, "connection to client closed");
+
+ LOG(DEBUG1, "connection finished");
+}
+
+
+static int initialize_tls_session_client(int peer_socket,
+ const char *hostname,
+ gnutls_session_t *session,
+ gnutls_certificate_credentials_t *x509_cred) {
+ int result;
+ int use_invalid_cert;
+ char path[TLSPROXY_MAX_PATH_LENGTH];
+
+ /* The "invalid" hostname is special. If it's used we send an invalid
+ * certificate to let the client know something is wrong. */
+ use_invalid_cert = (!strcmp(hostname, "invalid"));
+
+ if (proxy_certificate_path(hostname, path, sizeof(path)) != 0) {
+ LOG(ERROR,
+ "initialize_tls_session_client(): "
+ "failed to get proxy certificate path");
+ return -1;
+ }
+
+ result = gnutls_certificate_allocate_credentials(x509_cred);
+ if (result != GNUTLS_E_SUCCESS) {
+ LOG(ERROR,
+ "initialize_tls_session_client(): "
+ "gnutls_certificate_allocate_credentials(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+
+ /* Load proxy CA file, this CA "list" is send to the client. */
+ if (!use_invalid_cert) {
+ result = gnutls_certificate_set_x509_trust_file(*x509_cred,
+ PROXY_CA_PATH,
+ GNUTLS_X509_FMT_PEM);
+ if (result <= 0) {
+ LOG(ERROR,
+ "initialize_tls_session_client(): can't read CA file: '%s'",
+ PROXY_CA_PATH);
+ gnutls_certificate_free_credentials(*x509_cred);
+ return -1;
+ } else if (result != 1) {
+ /* Must contain only one CA, our proxy CA. */
+ LOG(ERROR, "initialize_tls_session_client(): multiple CAs found");
+ gnutls_certificate_free_credentials(*x509_cred);
+ return -1;
+ }
+ }
+ /* If the invalid hostname was specified do nothing, we use a self-signed
+ * certificate in this case. */
+
+ /* And certificate for this website and proxy's private key. */
+ if (!use_invalid_cert) {
+ result = gnutls_certificate_set_x509_key_file(*x509_cred,
+ path,
+ PROXY_KEY_PATH,
+ GNUTLS_X509_FMT_PEM);
+ /* If the invalid hostname was specified load our special "invalid"
+ * certificate. */
+ } else {
+ result = gnutls_certificate_set_x509_key_file(*x509_cred,
+ PROXY_INVALID_CERT_PATH,
+ PROXY_KEY_PATH,
+ GNUTLS_X509_FMT_PEM);
+ }
+ if (result != GNUTLS_E_SUCCESS) {
+ LOG(ERROR,
+ "initialize_tls_session_client(): "
+ "can't read server certificate ('%s') or key file ('%s'): %s",
+ path, PROXY_KEY_PATH, gnutls_strerror(result));
+ gnutls_certificate_free_credentials(*x509_cred);
+ /* Could be a missing certificate. */
+ return -2;
+ }
+
+ gnutls_certificate_set_dh_params(*x509_cred, global_tls_dh_params);
+
+ return initialize_tls_session_both(GNUTLS_SERVER,
+ peer_socket, session, x509_cred);
+}
+static int initialize_tls_session_server(int peer_socket,
+ gnutls_session_t *session,
+ gnutls_certificate_credentials_t *x509_cred) {
+ int result;
+
+ result = gnutls_certificate_allocate_credentials(x509_cred);
+ if (result != GNUTLS_E_SUCCESS) {
+ LOG(ERROR,
+ "initialize_tls_session_server(): "
+ "gnutls_certificate_allocate_credentials(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+
+ return initialize_tls_session_both(GNUTLS_CLIENT,
+ peer_socket, session, x509_cred);
+}
+static int initialize_tls_session_both(unsigned int flags,
+ int peer_socket,
+ gnutls_session_t *session,
+ gnutls_certificate_credentials_t *x509_cred) {
+ int result;
+
+ *session = NULL;
+
+ result = gnutls_init(session, flags);
+ if (result != GNUTLS_E_SUCCESS) {
+ LOG(ERROR,
+ "initialize_tls_session_both(): gnutls_init(): %s",
+ gnutls_strerror(result));
+ goto err;
+ }
+ result = gnutls_priority_set(*session, global_tls_priority_cache);
+ if (result != GNUTLS_E_SUCCESS) {
+ LOG(ERROR,
+ "initialize_tls_session_both(): gnutls_priority_set(): %s",
+ gnutls_strerror(result));
+ goto err;
+ }
+ result = gnutls_credentials_set(*session,
+ GNUTLS_CRD_CERTIFICATE, *x509_cred);
+ if (result != GNUTLS_E_SUCCESS) {
+ LOG(ERROR,
+ "initialize_tls_session_both(): gnutls_credentials_set(): %s",
+ gnutls_strerror(result));
+ goto err;
+ }