OpenSSL Server and Client Problems

P

Patrick

Hello,
I'm currently trying the OpenSSL Library, but I got some problems. I
want to create a server and client application that communicate
through the OpenSSL API, but this code doesn't work.
I tried to understand the error messages but for me they aren't
useful. And now I'm here and hope that somebody has experience and can
tell me the error.

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(SOCKADDR_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);
}

I hope you can help me to find the error.

Thank you very much.

Patrick
 
B

BobR

Patrick said:
Hello,
I'm currently trying the OpenSSL Library, but I got some problems.
<snip>. And now I'm here and hope that somebody has experience and can
tell me the error.

This is the Code for the server:
#define _CRT_SECURE_NO_DEPRECATE

You're in trouble right out of the gate. An underline followed by a capital
letter is not good, reserved to the implementation.

#include <cstdio> // > #include <stdio.h>

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

Well, your best bet is to try an 'OpenSSL' NG, they'll give you better
answers.
( And/Or, a windows NG. ( hint: <winsock2.h>).
Be sure to tell them the errors (first 3 should do, indicate line numbers),
and/or describe what it's doing and what you wanted/expected. "but this code
doesn't work" does not tell anything about your problem.

Most of your code looks like 'C' (which may be valid in 'C++') to me. You
should use the 'C++' 'features' which may improve your development time and
make life easier.

The FAQ has some suggestions for other NewsGroups to try:
FAQ: http://www.parashift.com/c++-faq-lite/

If you want help here, reduce your code to the minimum that exhibits the
problem, remove all openssl and windows stuff, and repost (with your
errors).
 
J

John Harrison

BobR said:
You're in trouble right out of the gate. An underline followed by a capital
letter is not good, reserved to the implementation.

Yes and the meaning of that particular macro is defined by the
implementation. It would be better defined in the compiler arguments
(-D_CRT_SECURE_NO_DEPRECATE) than the program code though.

john
 
D

dasjotre

You're in trouble right out of the gate. An underline followed by a capital
letter is not good, reserved to the implementation.

_CRT_SECURE_NO_DEPRECATE is part of MSVC
implementation. it is used to suppress warnings for
use of CRT functions that MS deems dangerous.

for openssl problems see mailing.openssl.users NG.

regards

DS
 
P

Patrick

_CRT_SECURE_NO_DEPRECATE is part of MSVC
implementation. it is used to suppress warnings for
use of CRT functions that MS deems dangerous.

for openssl problems see mailing.openssl.users NG.

regards

DS

It were already late when I posted that. Thanks for your answers. I
will check that out :)

Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top