Helo,
i want to write open SSL based c,c++ application for client socket in vc++ without using any
API of winsock or MFC functionality.
can u guide me how do i implement it.
I search a lot on google but not getting exactly.

Regards,
Amit

download http://www.openssl.org/source/openssl-0.9.8g.tar.gz
unzip the tarball, read INSTALL.W32
build the library using vc++
http://www.openssl.org/support/faq.html#BUILD7
http://www.openssl.org/support/faq.html#PROG2

Thx for replying me.
Actually i m new in windod side programming.
My doughts are not clear till now.

I want to write client socket application using c,c++ language on windows platform on vc++(vc++ console application or win32 static library).
i dont want to use any Winsock API or MFC class for creating and connecting socket.
because when i create socket using winsock API and after that if i use
SSL_read() on the fd retured by winsock API, then it will not working.

for simplicity,
IS it possible to write openssl -based client socket application using c,c++ on windows-vc++ platform without using any winsock class and MFC class
if possible then what i have to do
(1) what openssl version i have to use for windows
(2) what API i have to use for creating socket,connecting socket, reading data and writing data.

Regards,
Amit

> IS it possible to write openssl -based client socket application using c,c++ on
> windows-vc++ platform without using any winsock class and MFC class
yes.

> if possible then what i have to do
> (1) what openssl version i have to use for windows
the current release version of a library 0.9.8

> (2) what API i have to use for creating socket,connecting socket, reading data and writing data.
read the openssl documentation.
a tutorial would definitely help, there are several on the web. eg.
http://www.ibm.com/developerworks/linux/library/l-openssl.html

> IS it possible to write openssl -based client socket application using c,c++ on
> windows-vc++ platform without using any winsock class and MFC class
yes.

> if possible then what i have to do
> (1) what openssl version i have to use for windows
the current release version of a library 0.9.8

> (2) what API i have to use for creating socket,connecting socket, reading data and writing data.
read the openssl documentation.
a tutorial would definitely help, there are several on the web. eg.
http://www.ibm.com/developerworks/linux/library/l-openssl.html

Thx Sir for replying me quickly.
I have one more dought that
if i use WinSock API for Openssl then it will support SSL functionality.
I mean if i creare socket, bind socket and connect socket using winsock API and after that i use SSL_read() function on the fd return by winsock API then it will create problem or it will work.
Regards,
Amit

> IS it possible to write openssl -based client socket application using c,c++ on
> windows-vc++ platform without using any winsock class and MFC class
yes.

> if possible then what i have to do
> (1) what openssl version i have to use for windows
the current release version of a library 0.9.8

> (2) what API i have to use for creating socket,connecting socket, reading data and writing data.
read the openssl documentation.
a tutorial would definitely help, there are several on the web. eg.
http://www.ibm.com/developerworks/linux/library/l-openssl.html

Thx Sir for replying me quickly.
I have one more dought that
if i use WinSock API for Openssl then it will support SSL functionality.
I mean if i creare socket, bind socket and connect socket using winsock API and after that i use SSL_read() function on the fd return by winsock API then it will create problem or it will work.
Regards,
Amit

my sample code is given below which is not working .


This is the Code for the server:

#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <winsock2.h>

#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#define MAXHOSTNAMELEN 100

int startWinsock();

int main ()
{
int x = startWinsock();

if ( !x)
printf( "%i\n", x );

struct sockaddr_in host_addr;
int size;
int s;
struct hostent *host;
char hostname[MAXHOSTNAMELEN];
char buf[1000];
char request[1000];

SSL_CTX *ctx;
SSL *ssl;
int err;

printf("\nEnter Hostname: ");
scanf("%s", &hostname);
host = gethostbyname(hostname);
if (host == NULL) {
fprintf(stderr, "Unknown Host %s\n", hostname);
return -1;
}
fflush(stdout);
s = socket(PF_INET, SOCK_STREAM, 0);
if (s < 0) {
fprintf(stderr, "Socket Error\n");
return -1;
}
host_addr.sin_family = AF_INET;
host_addr.sin_addr = *((struct in_addr *)host->h_addr);
host_addr.sin_port = htons(334);
if (connect(s, (struct sockaddr *)&host_addr,
sizeof(host_addr)) == -1) {
closesocket(s);
fprintf(stderr, "Connection Error\n");
return -1;
}
SSL_load_error_strings();
SSL_library_init();
ctx=SSL_CTX_new(SSLv23_client_method());
ssl=SSL_new(ctx);
if(!ssl) {
closesocket(s);
fprintf(stderr, "SSL creation error\n");
return -1;
}
SSL_set_fd(ssl, s);
err=SSL_connect(ssl);
if(!err) {
closesocket(s);
fprintf(stderr, "SSL connect error\nretval: %d\n",
err);
err=SSL_get_error(ssl, err);
fprintf(stderr, "SSL error: %d\n", err);
return -1;
}

//fgets( request, sizeof( request ), stdin );


if(!err) {
closesocket(s);
fprintf(stderr, "SSL write error\n");
return -1;
}

while(true)
{
sprintf( request,"Hallo, Welt!" );
err=SSL_write(ssl, request, strlen(request));

int read_size = SSL_read(ssl, buf, sizeof(buf) );
if ( read_size > 0 )
{
buf[read_size]='\0';
printf("Getting %d Bytes of Data\nData: %s\n", read_size, buf);
}
else
{
switch( SSL_get_error( ssl, read_size ) )
{
case SSL_ERROR_ZERO_RETURN:
printf( "ZERO" );
break;

case SSL_ERROR_NONE:
printf( "No Error" );
break;

case SSL_ERROR_SSL:
printf( "SSL ERROR" );
break;
}
break;
}
Sleep(1);
}

SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
fflush(stdout);
closesocket(s);
return 0;
}

int startWinsock()
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2,0),&wsa);
}

And this for the client:

#define _CRT_SECURE_NO_DEPRECATE

#include <windows.h>
#include <winsock.h>
#include <stdio.h>

#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

//Prototypen
int startWinsock(void);

int main()
{
long rc;
SOCKET acceptSocket;
SOCKET connectedSocket = NULL;
SOCKADDR_IN addr;
char buf[1024];
char buf2[1024];

SSL_CTX *ctx;
SSL *ssl;
int err;

// Winsock starten
rc=startWinsock();
if(rc!=0)
{
printf("Fehler: startWinsock, fehler code: %d\n",rc);
return 1;
}
else
{
printf("Winsock gestartet!\n");
}

// Socket erstellen
acceptSocket=socket(AF_INET,SOCK_STREAM,0);
if(acceptSocket==INVALID_SOCKET)
{
printf("Fehler: Der Socket konnte nicht erstellt werden, fehler
code: %d\n",WSAGetLastError());
return 1;
}
else
{
printf("Socket erstellt!\n");
}

// Socket binden
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(334);
addr.sin_addr.s_addr=INADDR_ANY;
rc=bind(acceptSocket,(SOCKADDR*)&addr,sizeof(SOCKA DDR_IN));
if(rc==SOCKET_ERROR)
{
printf("Fehler: bind, fehler code: %d\n",WSAGetLastError());
return 1;
}
else
{
printf("Socket an port gebunden\n");
}

// In den listen Modus
rc=listen(acceptSocket,10);
if(rc==SOCKET_ERROR)
{
printf("Fehler: listen, fehler code: %d\n",WSAGetLastError());
return 1;
}
else
{
printf("acceptSocket ist im listen Modus....\n");
}

// Verbindung annehmen
connectedSocket=accept(acceptSocket,NULL,NULL);
if(connectedSocket==INVALID_SOCKET)
{
printf("Fehler: accept, fehler code: %d\n",WSAGetLastError());
return 1;
}
else
{
printf("Neue Verbindung wurde akzeptiert!\n");
}

SSL_load_error_strings();
SSL_library_init();
ctx=SSL_CTX_new(SSLv23_server_method());
ssl=SSL_new(ctx);
if(!ssl) {
closesocket(connectedSocket);
fprintf(stderr, "SSL creation error\n");
return -1;
}
SSL_set_fd(ssl, connectedSocket);
err=SSL_accept(ssl);
if(!err) {
closesocket(connectedSocket);
fprintf(stderr, "SSL accept error\nretval: %d\n",
err);
err=SSL_get_error(ssl, err);
fprintf(stderr, "SSL error: %d\n", err);
return -1;
}

// Daten austauschen
while(true)
{
int read_size = SSL_read(ssl, buf, sizeof(buf) );
if ( read_size > 0 )
{
buf[read_size]='\0';
printf("Getting %d Bytes of Data\nData: %s\n", read_size, buf);
}
//else break;

sprintf( buf2,"Du mich auch %s\r\n", "x" );
err=SSL_write(ssl, buf2, strlen(buf2));
if(!err) {
closesocket(connectedSocket);
fprintf(stderr, "SSL write error\n");
return -1;
}

Sleep(1000);
}
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
fflush(stdout);
closesocket(acceptSocket);
closesocket(connectedSocket);
WSACleanup();
return 0;
}

int startWinsock(void)
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2,0),&wsa);
}

Amit

my sample code is given below which is not working .

one more thing i wan to know that

WINSOCK2 is really supporting SSL functionality or not? if supported then how do i use it

Amit

This article has been dead for over six months. Start a new discussion instead.