+
+static int initialize_tls_session_client(int peer_socket,
+ const char *hostname,
+ gnutls_session_t *session,
+ gnutls_certificate_credentials_t *x509_cred) {
+ int result;
+ char path[1024];
+ /* The server certificate for the given hostname is stored in
+ * "./certificate-hostname-proxy.pem". */
+#define PATH_FORMAT "./certificate-%s-proxy.pem"
+
+ /* Hostname too long. */
+ if (sizeof(path) - strlen(PATH_FORMAT) <= strlen(hostname)) {
+ LOG(LOG_WARNING,
+ "initialize_tls_session_client(): hostname too long: '%s'",
+ hostname);
+ return -1;
+ }
+ /* Try to prevent path traversals in hostnames. */
+ if (NULL != strstr(hostname, "..")) {
+ LOG(LOG_WARNING,
+ "initialize_tls_session_client(): possible path traversal: '%s'",
+ hostname);
+ return -1;
+ }
+ snprintf(path, sizeof(path), PATH_FORMAT, hostname);
+#undef PATH_FORMAT
+
+ result = gnutls_certificate_allocate_credentials(x509_cred);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(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. */
+ result = gnutls_certificate_set_x509_trust_file(*x509_cred,
+ PROXY_CA_FILE,
+ GNUTLS_X509_FMT_PEM);
+ if (0 >= result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_client(): can't read CA file: '%s'",
+ PROXY_CA_FILE);
+ return -1;
+ }
+ /* And certificate for this website and proxy's private key. */
+ result = gnutls_certificate_set_x509_key_file(*x509_cred,
+ path, PROXY_KEY_FILE,
+ GNUTLS_X509_FMT_PEM);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_client(): \
+can't read server certificate ('%s') or key file ('%s'): %s",
+ path, PROXY_KEY_FILE, gnutls_strerror(result));
+ /* Could be a missing certificate. */
+ return -2;
+ }
+
+ gnutls_certificate_set_dh_params(*x509_cred, tls_dh_params);
+
+ result = gnutls_init(session, GNUTLS_SERVER);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_client(): gnutls_init(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+ result = gnutls_priority_set(*session, tls_priority_cache);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_client(): gnutls_priority_set(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+ result = gnutls_credentials_set(*session,
+ GNUTLS_CRD_CERTIFICATE, *x509_cred);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_client(): gnutls_credentials_set(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+
+ gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
+
+ return 0;
+}
+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 (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_server(): \
+gnutls_certificate_allocate_credentials(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+
+ result = gnutls_init(session, GNUTLS_CLIENT);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_server(): gnutls_init(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+ gnutls_priority_set(*session, tls_priority_cache);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_server(): gnutls_priority_set(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+ result = gnutls_credentials_set(*session,
+ GNUTLS_CRD_CERTIFICATE, *x509_cred);
+ if (GNUTLS_E_SUCCESS != result) {
+ LOG(LOG_ERROR,
+ "initialize_tls_session_server(): gnutls_credentials_set(): %s",
+ gnutls_strerror(result));
+ return -1;
+ }
+
+ gnutls_transport_set_ptr(*session, (gnutls_transport_ptr_t)peer_socket);
+
+ return 0;
+}
+
+