473,749 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(h ostname);
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_f amily = AF_INET;
host_addr.sin_a ddr = *((struct in_addr *)host->h_addr);
host_addr.sin_p ort = htons(334);
if (connect(s, (struct sockaddr *)&host_addr,
sizeof(host_add r)) == -1) {
closesocket(s);
fprintf(stderr, "Connection Error\n");
return -1;
}
SSL_load_error_ strings();
SSL_library_ini t();
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_err or(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(s sl, 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(ss l);
SSL_free(ssl);
SSL_CTX_free(ct x);
fflush(stdout);
closesocket(s);
return 0;
}

int startWinsock()
{
WSADATA wsa;
return WSAStartup(MAKE WORD(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(vo id);

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=so cket(AF_INET,SO CK_STREAM,0);
if(acceptSocket ==INVALID_SOCKE T)
{
printf("Fehler: Der Socket konnte nicht erstellt werden, fehler
code: %d\n",WSAGetLas tError());
return 1;
}
else
{
printf("Socket erstellt!\n");
}

// Socket binden
memset(&addr,0, sizeof(SOCKADDR _IN));
addr.sin_family =AF_INET;
addr.sin_port=h tons(334);
addr.sin_addr.s _addr=INADDR_AN Y;
rc=bind(acceptS ocket,(SOCKADDR *)&addr,sizeof( SOCKADDR_IN));
if(rc==SOCKET_E RROR)
{
printf("Fehler: bind, fehler code: %d\n",WSAGetLas tError());
return 1;
}
else
{
printf("Socket an port gebunden\n");
}

// In den listen Modus
rc=listen(accep tSocket,10);
if(rc==SOCKET_E RROR)
{
printf("Fehler: listen, fehler code: %d\n",WSAGetLas tError());
return 1;
}
else
{
printf("acceptS ocket ist im listen Modus....\n");
}

// Verbindung annehmen
connectedSocket =accept(acceptS ocket,NULL,NULL );
if(connectedSoc ket==INVALID_SO CKET)
{
printf("Fehler: accept, fehler code: %d\n",WSAGetLas tError());
return 1;
}
else
{
printf("Neue Verbindung wurde akzeptiert!\n") ;
}

SSL_load_error_ strings();
SSL_library_ini t();
ctx=SSL_CTX_new (SSLv23_server_ method());
ssl=SSL_new(ctx );
if(!ssl) {
closesocket(con nectedSocket);
fprintf(stderr, "SSL creation error\n");
return -1;
}
SSL_set_fd(ssl, connectedSocket );
err=SSL_accept( ssl);
if(!err) {
closesocket(con nectedSocket);
fprintf(stderr, "SSL accept error\nretval: %d\n",
err);
err=SSL_get_err or(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(s sl, buf2, strlen(buf2));
if(!err) {
closesocket(con nectedSocket);
fprintf(stderr, "SSL write error\n");
return -1;
}

Sleep(1000);
}
SSL_shutdown(ss l);
SSL_free(ssl);
SSL_CTX_free(ct x);
fflush(stdout);
closesocket(acc eptSocket);
closesocket(con nectedSocket);
WSACleanup();
return 0;
}

int startWinsock(vo id)
{
WSADATA wsa;
return WSAStartup(MAKE WORD(2,0),&wsa) ;
}

I hope you can help me to find the error.

Thank you very much.

Patrick

Jun 27 '07 #1
4 6597

Patrick <co******@googl email.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******@googl email.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.ne twrote:
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...@googl email.comwrote:
On 28 Jun, 01:15, "BobR" <removeBadB...@ worldnet.att.ne twrote:
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
6330
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(): http://www.canadiandropshipping.com/hello.php3 Does anyone know why this ssl curl test fails? http://www.canadiandropshipping.com...t/diag_curl.php
3
3082
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 code: from ZSI import * u='' n='https://shahzad.fnal.gov/edg-voms-admin/uscms/services/VOMSAdmin'
1
2223
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 office, the SSL handshake fails. The reaon is quite obvious, there is a proxy server in between my computer and the target server, and the proxy does not know that the requests should just be forwarded to the server.
0
2026
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 libeay32.lib ssleay32.lib - moved lib and dll files into the lib_release directory - included "openssl/ssl.h" in client.c and vio.c - added des_key_file.cpp to the mysqld project and get the errors below. Any suggestions?
2
5287
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: ../configure --with-openssl --enable-odbc --with-perl --enable-multibyte
0
1611
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 I am getting problems accessing https: pages through PHP
2
2189
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 it's built with "--with-openssl=shared" in the ./configure command line. If install the latest version of OpenSSL, do I need to relink PHP?
1
4942
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 html site. But with the other browsers I got either no html page at all or got the site just after loading the browser twice. I dont know if the problem are the certificates or the c++ code. Platform / OS / Version: IDE: embeddedVisualC++...
6
6151
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 end with some unreadable characters. I found out that the operating system of the switch is redhat and the protocol = SSH-2.0-openssh_3.9p1. My problem:
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.