mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-25 15:59:41 +08:00
Copy to master from devel - ready for 0.8
This commit is contained in:
1 parent
db169c48c3
commit
5287278fa5
74 files changed
+5163
-4637
No files matched your search
@@ -2,12 +2,11 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include "../../structures.h"
|
||||
#include "../../proxy.h"
|
||||
#include "my_ssl.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#ifndef _WIN32
|
||||
#include <sys/file.h>
|
||||
#endif
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/x509.h>
|
||||
@@ -16,6 +15,12 @@
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include "../../proxy.h"
|
||||
#include "my_ssl.h"
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct _ssl_conn {
|
||||
SSL_CTX *ctx;
|
||||
SSL *ssl;
|
||||
@@ -110,20 +115,23 @@ SSL_CERT ssl_copy_cert(SSL_CERT cert)
|
||||
static char hash_name[sizeof(src_cert->sha1_hash)*2 + 1];
|
||||
static char cache_name[200];
|
||||
|
||||
pthread_mutex_lock(&ssl_file_mutex);
|
||||
bin2hex(src_cert->sha1_hash, sizeof(src_cert->sha1_hash), hash_name, sizeof(hash_name));
|
||||
sprintf(cache_name, "%s%s.pem", cert_path, hash_name);
|
||||
/* check if certificate is already cached */
|
||||
fcache = fopen(cache_name, "rb");
|
||||
if ( fcache != NULL ) {
|
||||
#ifndef _WIN32
|
||||
flock(fileno(fcache), LOCK_SH);
|
||||
#endif
|
||||
dst_cert = PEM_read_X509(fcache, &dst_cert, NULL, NULL);
|
||||
#ifndef _WIN32
|
||||
flock(fileno(fcache), LOCK_UN);
|
||||
#endif
|
||||
fclose(fcache);
|
||||
if ( dst_cert != NULL ){
|
||||
pthread_mutex_unlock(&ssl_file_mutex);
|
||||
return dst_cert;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&ssl_file_mutex);
|
||||
|
||||
/* proceed if certificate is not cached */
|
||||
dst_cert = X509_dup(src_cert);
|
||||
@@ -163,18 +171,22 @@ SSL_CERT ssl_copy_cert(SSL_CERT cert)
|
||||
|
||||
/* write to cache */
|
||||
|
||||
pthread_mutex_lock(&ssl_file_mutex);
|
||||
fcache = fopen(cache_name, "wb");
|
||||
if ( fcache != NULL ) {
|
||||
#ifndef _WIN32
|
||||
flock(fileno(fcache), LOCK_EX);
|
||||
#endif
|
||||
PEM_write_X509(fcache, dst_cert);
|
||||
#ifndef _WIN32
|
||||
flock(fileno(fcache), LOCK_UN);
|
||||
#endif
|
||||
fclose(fcache);
|
||||
}
|
||||
pthread_mutex_unlock(&ssl_file_mutex);
|
||||
return dst_cert;
|
||||
}
|
||||
|
||||
|
||||
SSL_CONN ssl_handshake_to_server(SOCKET s, SSL_CERT *server_cert, char **errSSL)
|
||||
SSL_CONN ssl_handshake_to_server(SOCKET s, char * hostname, SSL_CERT *server_cert, char **errSSL)
|
||||
{
|
||||
int err = 0;
|
||||
X509 *cert;
|
||||
@@ -204,6 +216,7 @@ SSL_CONN ssl_handshake_to_server(SOCKET s, SSL_CERT *server_cert, char **errSSL)
|
||||
ssl_conn_free(conn);
|
||||
return NULL;
|
||||
}
|
||||
if(hostname && *hostname)SSL_set_tlsext_host_name(conn->ssl, hostname);
|
||||
err = SSL_connect(conn->ssl);
|
||||
if ( err == -1 ) {
|
||||
*errSSL = ERR_error_string(ERR_get_error(), errbuf);
|
||||
@@ -212,6 +225,10 @@ SSL_CONN ssl_handshake_to_server(SOCKET s, SSL_CERT *server_cert, char **errSSL)
|
||||
}
|
||||
|
||||
cert = SSL_get_peer_certificate(conn->ssl);
|
||||
if(!cert) {
|
||||
ssl_conn_free(conn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* TODO: Verify certificate */
|
||||
|
||||
@@ -312,10 +329,14 @@ void ssl_conn_free(SSL_CONN connection)
|
||||
{
|
||||
ssl_conn *conn = (ssl_conn *) connection;
|
||||
|
||||
SSL_shutdown(conn->ssl);
|
||||
SSL_free(conn->ssl);
|
||||
SSL_CTX_free(conn->ctx);
|
||||
free(conn);
|
||||
if(conn){
|
||||
if(conn->ssl){
|
||||
SSL_shutdown(conn->ssl);
|
||||
SSL_free(conn->ssl);
|
||||
}
|
||||
if(conn->ctx) SSL_CTX_free(conn->ctx);
|
||||
free(conn);
|
||||
}
|
||||
}
|
||||
|
||||
void _ssl_cert_free(SSL_CERT cert)
|
||||
|
||||
@@ -18,7 +18,7 @@ SSL_CERT ssl_copy_cert(SSL_CERT cert);
|
||||
//
|
||||
// SSL/TLS handshakes
|
||||
//
|
||||
SSL_CONN ssl_handshake_to_server(SOCKET s, SSL_CERT *server_cert, char **errSSL);
|
||||
SSL_CONN ssl_handshake_to_server(SOCKET s, char * hostname, SSL_CERT *server_cert, char **errSSL);
|
||||
SSL_CONN ssl_handshake_to_client(SOCKET s, SSL_CERT server_cert, char **errSSL);
|
||||
|
||||
//
|
||||
|
||||
@@ -4,17 +4,15 @@
|
||||
|
||||
please read License Agreement
|
||||
|
||||
$Id: ssl_plugin.c,v 1.9 2010-11-11 11:32:33 v.dubrovin Exp $
|
||||
*/
|
||||
|
||||
#include "../../structures.h"
|
||||
#include "../../proxy.h"
|
||||
#include <openssl/rsa.h> /* SSLeay stuff */
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#include "../../proxy.h"
|
||||
#include "my_ssl.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
@@ -54,13 +52,15 @@ struct SSLqueue {
|
||||
|
||||
|
||||
/*
|
||||
Todo: use hashtable
|
||||
TO DO: use hashtable
|
||||
*/
|
||||
static struct SSLqueue *searchSSL(SOCKET s){
|
||||
struct SSLqueue *sslq;
|
||||
struct SSLqueue *sslq = NULL;
|
||||
pthread_mutex_lock(&ssl_mutex);
|
||||
for(sslq = SSLq; sslq; sslq = sslq->next)
|
||||
if(sslq->s == s) return sslq;
|
||||
return NULL;
|
||||
if(sslq->s == s) break;
|
||||
pthread_mutex_unlock(&ssl_mutex);
|
||||
return sslq;
|
||||
}
|
||||
|
||||
static void addSSL(SOCKET s, SSL_CERT cert, SSL_CONN conn, struct clientparam* param){
|
||||
@@ -235,7 +235,7 @@ int dossl(struct clientparam* param, SSL_CONN* ServerConnp, SSL_CONN* ClientConn
|
||||
ul = ((unsigned long)ssl_connect_timeout)*1000;
|
||||
setsockopt(param->remsock, SOL_SOCKET, SO_SNDTIMEO, (char *)&ul, 4);
|
||||
}
|
||||
ServerConn = ssl_handshake_to_server(param->remsock, &ServerCert, &errSSL);
|
||||
ServerConn = ssl_handshake_to_server(param->remsock, (char *)param->hostname, &ServerCert, &errSSL);
|
||||
if ( ServerConn == NULL || ServerCert == NULL ) {
|
||||
param->res = 8011;
|
||||
param->srv->logfunc(param, (unsigned char *)"SSL handshake to server failed");
|
||||
@@ -325,6 +325,47 @@ static struct filter ssl_filter = {
|
||||
ssl_filter_close
|
||||
};
|
||||
|
||||
int mitm = 0;
|
||||
|
||||
static int h_mitm(int argc, unsigned char **argv){
|
||||
if((mitm&1)) return 1;
|
||||
if(mitm) usleep(100*SLEEPTIME);
|
||||
ssl_filter.next = pl->conf->filters;
|
||||
pl->conf->filters = &ssl_filter;
|
||||
mitm++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int h_nomitm(int argc, unsigned char **argv){
|
||||
struct filter * sf;
|
||||
if(!(mitm&1)) return 1;
|
||||
if(mitm) usleep(100*SLEEPTIME);
|
||||
if(pl->conf->filters == &ssl_filter) pl->conf->filters = ssl_filter.next;
|
||||
else for(sf = pl->conf->filters; sf && sf->next; sf=sf->next){
|
||||
if(sf->next == &ssl_filter) {
|
||||
sf->next = ssl_filter.next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mitm++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int h_certpath(int argc, unsigned char **argv){
|
||||
size_t len;
|
||||
len = strlen(argv[1]);
|
||||
if(!len || (argv[1][len - 1] != '/' && argv[1][len - 1] != '\\')) return 1;
|
||||
if(cert_path && *cert_path) free(cert_path);
|
||||
cert_path = strdup(argv[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct commands ssl_commandhandlers[] = {
|
||||
{ssl_commandhandlers+1, "ssl_mitm", h_mitm, 1, 1},
|
||||
{ssl_commandhandlers+2, "ssl_nomitm", h_nomitm, 1, 1},
|
||||
{NULL, "ssl_certcache", h_certpath, 2, 2},
|
||||
};
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
@@ -333,15 +374,9 @@ __declspec(dllexport)
|
||||
int ssl_plugin (struct pluginlink * pluginlink,
|
||||
int argc, char** argv){
|
||||
pl = pluginlink;
|
||||
if(argc > 1) {
|
||||
if(cert_path && *cert_path) free(cert_path);
|
||||
cert_path = strdup(argv[1]);
|
||||
}
|
||||
if(!ssl_loaded){
|
||||
ssl_loaded = 1;
|
||||
pthread_mutex_init(&ssl_mutex, NULL);
|
||||
ssl_filter.next = pl->conf->filters;
|
||||
pl->conf->filters = &ssl_filter;
|
||||
memcpy(&sso, pl->so, sizeof(struct sockfuncs));
|
||||
pl->so->_send = ssl_send;
|
||||
pl->so->_recv = ssl_recv;
|
||||
@@ -349,10 +384,13 @@ __declspec(dllexport)
|
||||
pl->so->_recvfrom = ssl_recvfrom;
|
||||
pl->so->_closesocket = ssl_closesocket;
|
||||
pl->so->_poll = ssl_poll;
|
||||
ssl_commandhandlers[2].next = pl->commandhandlers->next;
|
||||
pl->commandhandlers->next = ssl_commandhandlers;
|
||||
}
|
||||
else{
|
||||
else {
|
||||
ssl_release();
|
||||
}
|
||||
|
||||
ssl_init();
|
||||
tcppmfunc = (PROXYFUNC)pl->findbyname("tcppm");
|
||||
if(!tcppmfunc){return 13;}
|
||||
|
||||
Reference in new issue
Block a user