Hi,
I am trying to use the openssl library on VC 7 with BIO_s_mem.
The first time I call SSL_Connect, I get the expected response with the data to be read. After receiving the response from the server and transferring the information from the socket to the BIO, SSL_Connect crashes.
I have attached the code I am using.
Thanks
Nadav
// TLSTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
typedef struct {} HelloRequest;
typedef enum {
hello_request=0
}HandShakeType;
typedef struct {
HandShakeType msgType;
unsigned int length;
HelloRequest body;
} Handshake;
/*The password code is not thread safe*/
static int password_cb(char *buf,int num,
int rwflag, void *userdata)
{
if(num<strlen("password")+1)
return(0);
strcpy(buf,"password");
return(strlen("password"));
}
int _tmain(int argc, _TCHAR* argv[])
{
SSL_library_init();
SSL_load_error_strings();
//OpenSSL_add_all_algorithms();
SSL_CTX *tlsSSL;
SSL_METHOD *meth = TLSv1_method();
tlsSSL = SSL_CTX_new(meth);
unsigned long ulMode = 1;
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2,2),&wsaData);
int sock =socket(AF_INET,SOCK_STREAM,0);
ioctlsocket(sock,FIONBIO,&ulMode);
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("62.1.205.36");
clientService.sin_port = htons(5555);
memset(clientService.sin_zero,0,8);
int nTimeWaited =0;
if(connect( sock, (SOCKADDR*) &clientService, sizeof(clientService) ) ==
SOCKET_ERROR && nTimeWaited<5)
{
if(WSAGetLastError() == WSAEWOULDBLOCK)
{
fd_set fsConnect;
FD_ZERO(&fsConnect);
FD_SET(sock, &fsConnect);
timeval sTimeoutVal;
sTimeoutVal.tv_sec = (long)30;
sTimeoutVal.tv_usec = (long)0;
//wait up to 30 seconds for the socket to complete connecting, unless
//the target computer, network or
//internet is hanging this should return with 1 right away
int retval = select(FD_SETSIZE, (fd_set *) NULL, &fsConnect, (fd_set *)
NULL, &sTimeoutVal);
if(retval != 1)
{
//connect timed out
//close socket and remove a winsock reference count
closesocket(sock);
WSACleanup();
return 1;
}
}
else
{
//connect failed right away, no need to select status of socket
//close socket and remove a winsock reference count
closesocket(sock);
WSACleanup();
return 1;
}
}
result =SSL_CTX_use_certificate_chain_file(tlsSSL,"e:\\client.pem");
SSL_CTX_set_default_passwd_cb(tlsSSL,password_cb);
SSL_CTX_use_PrivateKey_file(tlsSSL,"e:\\client.pem",SSL_FILETYPE_PEM);
result = SSL_CTX_load_verify_locations(tlsSSL,"e:\\root.pem",0);
SSL* newSSL = SSL_new(tlsSSL);
BIO *bRead = BIO_new(BIO_s_mem());
BIO *bWrite = BIO_new(BIO_s_mem());
SSL_set_bio(newSSL,bRead,bWrite);
unsigned long error_code=ERR_get_error();
char * string; //ERR_error_string(error_code,tempBuffer);
result = SSL_connect(newSSL);
result = SSL_get_error(newSSL,result);
error_code=ERR_get_error();
unsigned char buf[2048];
int nRead = BIO_ctrl_pending(bWrite);
result = BIO_read(bWrite,buf,nRead);
send(sock,(char*)buf,nRead,0);
int bytesRec = -1;
memset(buf,0,2048);
int totalBytes = 0;
while (bytesRec !=0 && !(totalBytes>0 && bytesRec==-1))
{
bytesRec = recv(sock,(char *)buf+totalBytes,2048,0);
if (bytesRec>0)
{
totalBytes+=bytesRec;
}
}
result = BIO_write(bRead,buf,totalBytes);
result = SSL_connect(newSSL); // HERE IS THE CRASH
result = SSL_get_error(newSSL,result);
error_code=ERR_get_error();
//nRead = BIO_ctrl_pending(bWrite);
//result = BIO_read(bWrite,buf,nRead);
return 0;
}
Nadav Golombick