By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

OpenSSL Server and Client Problems

P: n/a
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(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);
}

I hope you can help me to find the error.

Thank you very much.

Patrick

Jun 27 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Patrick <co******@googlemail.comwrote in message...
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 <winsock2.h // non-standard
>
#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).

--
Bob R
POVrookie
Jun 28 '07 #2

P: n/a
BobR wrote:
Patrick <co******@googlemail.comwrote in message...
>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.
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
Jun 28 '07 #3

P: n/a
On 28 Jun, 01:15, "BobR" <removeBadB...@worldnet.att.netwrote:
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.
_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

Jun 28 '07 #4

P: n/a
On 28 Jun., 12:35, dasjotre <dasjo...@googlemail.comwrote:
On 28 Jun, 01:15, "BobR" <removeBadB...@worldnet.att.netwrote:
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.

_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

Jun 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.