473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

OpenSSL Server and Client Problems

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
4 6549

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Fortune | last post by:
# uname -a Linux stargate.mxc-online.net 2.4.20-021stab022.2.777-smp #1 SMP Wed Jul 28 17:12:37 MSD 2004 i686 i686 i386 GNU/Linux I recompiled PHP with mcrypt, openssl, and curl phpinfo():...
3
by: Adil Hasan | last post by:
Hello, I'm having problems trying to use ZSI to connect to a https url. I give the command and I get prompted for my X509 cert pass-phrase, but the program dies with an openssl error. Here's my...
1
by: Jorge Rivera | last post by:
I am trying to write a client application that uses SSL. I am using openSSL in Red Hat Linux 9.0, using C++. When I am at home, my application works fine. However, when I return to the...
0
by: al | last post by:
I am trying to compile mysql for windows with openssl support. I have: - Defined HAVE_OPENSSL and HAVE_VIO in client.c (in libmysql/d projects) and vio.c - linked the libmysql project to...
2
by: Christopher Murtagh | last post by:
Greetings, I'm trying to build 7.3.4 and I've come across two problems, one during the configure and the other afterward. Problem 1) Trying to build with openssl support gives this: ...
0
by: bozzzza | last post by:
I have installed PHP, and the OPENSSL extension. The phpinfo() page confirms it is working : OpenSSL support enabled OpenSSL Version OpenSSL 0.9.8a 11 Oct 2005 I am using PHP Version 5.1.1...
2
by: Dan M | last post by:
I just learned that the version of OpenSSL I'm running (on a RedHat EL) server has some vulnerabilities that I'd like to close. I'm running PHP 5.1.4. My question is this: I've got PHP 5.1.4, and...
1
by: pawnee | last post by:
Explanation: I wrote a simple openssl server using code from basic examples. I tried it out with several browsers like firefox, opera, ie and safari. With firefox i get the certificate and then the...
6
by: =?Utf-8?B?U2hhcmllZg==?= | last post by:
Dear All, I must write a client program in C# which will communicate with a switch throught telnet. When I create a socket connection on port 22, the switch responds with some text and at the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.