SSL_CTX* ctx;
SSL* ssl;
X509* client_cert;
char* str;
unsigned char buf[100000]={0x00,};
//memset(buf,0x00,100000);
int stat=0;
SSL_METHOD *meth;
SOCKET client = (SOCKET)Parameter;
/* SSL preliminaries. We keep the certificate and key with the context. */
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
meth = TLSv1_server_method();
// create a new SSL_CTX object as framework for TLS/SSL enabled functions
ctx = SSL_CTX_new (meth);
if (!ctx) {
ERR_print_errors_fp(stderr);
return 0;
//exit(2);
}
if (SSL_CTX_use_certificate_file(ctx, "./stunnel.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
return 0;
//exit(3);
}
if (SSL_CTX_use_PrivateKey_file(ctx, "./stunnel.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
return 0;
//exit(4);
}
if (!SSL_CTX_check_private_key(ctx)) {
fprintf(stderr,"Private key does not match the certificate public key\n");
return 0;
//exit(5);
}
ssl = SSL_new (ctx);
//CHK_NULL(ssl);
// connect the SSL object with a file descriptor
SSL_set_fd (ssl, client);
stat = SSL_accept (ssl);
//CHK_SSL(err);
// Get the cipher - opt
printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
// Get client's certificate (note: beware of dynamic allocation) - opt
client_cert = SSL_get_peer_certificate (ssl);
if (client_cert != NULL)
{
printf ("Client certificate:\n");
str = X509_NAME_oneline (X509_get_subject_name (client_cert), 0, 0);
//CHK_NULL(str);
printf ("\t subject: %s\n", str);
free (str);
str = X509_NAME_oneline (X509_get_issuer_name (client_cert), 0, 0);
//CHK_NULL(str);
printf ("\t issuer: %s\n", str);
free (str);
/* We could do all sorts of certificate verification stuff here before
deallocating the certificate. */
X509_free (client_cert);
}
else
{
printf ("Client does not have certificate.\n");
}
int size=0;
// DATA EXCHANGE - Receive message and send reply.
stat = SSL_read (ssl, buf, sizeof(buf));
int i=0;
for(i=0;i<sizeof(buf);i++){
if (buf[i]=='$') break;
}
char* buf1=new char[i-1];
memset(buf1,0x00,sizeof(buf1));
for (int j=0;j<=i-1;j++){
buf1[j]=buf[j];
}
//CHK_SSL(err);
size+=atoi((char*)buf1);
buf1=0x00;
// stat = SSL_write (ssl, "I hear you.", strlen("I hear you."));
//CHK_SSL(err);
/* Clean up. */
closesocket(client);
client = 0;
//close (client);
SSL_free (ssl);
SSL_CTX_free (ctx);
CTCPComm TCPComm;
stat=TCPComm.Connect(gServerConfig.m_bsSVRDestIP,gServerConfig.m_nSVRDestPort);
ByteString bsMsg;
bsMsg.setBuffer(buf+i+1, size);
//bsMsg=(char*)buf;
/*std::string strMsg((char*)bsMsg);
//str=(char*)bsMsg;
size_t nSize = 0;
std::string find_token="\\r\\n";
while ((nSize = strMsg.find(find_token)) != std::string::npos)
strMsg.replace(nSize, find_token.size(), "");
ByteString bsResult((char*) strMsg.c_str());*/
stat=TCPComm.Send(bsMsg);
//UFilePlus::save("bsMsg.acr",bsMsg);
TCPComm.Close();
//Get the data form client
//rVal = recv(client,buf,10000,0);
//here we are performing simple check, the data came form client
//is valid or not
//at this point you can check your own data also, which needs some modification
/*if(strcmp(buf,"Data On Socket"))
{
//Send back the data to the client
//rVal = send(client,"YES",3,0);
}
else
{
//Send back the data to the client
//rVal = send(client,"NO",2,0);
}*/
buf1=0x00;