Hello,
I am doing a client to send http code to a https server. I am probing
with the openssl s_server and I can send data to him, but when I try to
respond with some data to my client I receive no data at all. Perhaps it
had to be with my initialization of the SSL objects.
#include "sslModule.h"
SSL_METHOD *method = NULL;
SSL_CTX *ctx = NULL;
SSL *con = NULL;
BIO *bio = NULL;
int s;
int sslInit()
{
char *fName = "sslInit";
int returned;
/* SSL library initialization */
printf ("Initializing SSL library ...");
SSL_library_init();
/* Provide error messages */
printf ("Loading error strings ...");
SSL_load_error_strings();
/* Actions to seed PRNG */
printf ("Seeding PRNG ...");
RAND_seed (rnd_seed, sizeof rnd_seed);
returned = RAND_status();
if (returned == 1)
printf ("PRNG seeded with enough data\n");
else
printf ("PRNG not seeded\n");
return returned;
}
int sslStartConnection(char *host, int port)
{
char *fName = "sslStartConnection";
int returned = 0, i, j;
struct sockaddr_in ser_addr;
STACK_OF(X509) *sk;
STACK_OF(X509_NAME) *sk2;
char buf[BUFSIZ];
X509 *peer=NULL;
char *p;
SSL_CIPHER *c;
static char *space=" ";
X509_NAME *xn;
/* Creation of a new SSL_CTX object as framework to establish TLS/SSL
enabled connections */
printf ("Creating SSL_CTX object ...\n");
method = SSLv23_client_method();
ctx = SSL_CTX_new (method);
if (ctx != NULL)
printf ("SSL_CTX object creationg succeded\n");
else
{
printf ("Can't create SSL_CTX object\n");
return -1;
}
/* Create new SSL structure*/
printf ("Creating SSL structure ...\n");
con = SSL_new(ctx);
if (con != NULL)
printf ("SSL structure creation succeded\n");
else
{
printf ("Can't create SSL structure\n");
return -1;
}
/* Reset SSL object to allow a new connection */
printf ("Reset SSL object tu allow a new connection ...\n");
returned = SSL_clear(con);
if (!returned)
{
printf ("Can't reset SSL object\n");
return returned;
}
/* Open an internet socket and connect to server */
printf ("Connecting to %s on port %d ...\n", host, port);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf ("Error creating socket\n");
return s;
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = inet_addr (host);
ser_addr.sin_port = htons (port);
if (connect (s, &ser_addr, sizeof(ser_addr)) == -1)
{
printf ("Error connecting\n");
return -1;
}
printf ("Connected to server\n");
returned = SSL_set_fd (con, s);
if (returned < 1)
{
printf ("SSL_set_fd failed\n");
return returned;
}
/* Creating bio */
bio = BIO_new_socket(s, BIO_NOCLOSE);
SSL_set_bio (con, bio, bio);
SSL_set_connect_state(con);
returned = SSL_connect(con);
if (returned <= 0)
{
printf ("Handshake was not successful. Errno: %d\n", returned);
return returned;
}
printf ("HANDSHAKE WAS SUCCESSFUL. CONGRATULATIONS!!! (returned =
%d)\n", returned);
return 0;
}
int sslWrite(char *buf, int nbytes)
{
char *fName = "sslWrite";
int returned = 0;
char *eBuffer;
printf ("==>\n");
printf ("%s", buf);
returned = SSL_write (con, buf, nbytes);
switch (SSL_get_error(con,returned))
{
case SSL_ERROR_NONE:
printf ("SSL_ERROR_NONE\n");
printf ("nbytes: %d - returned: %d", nbytes, returned);
break;
case SSL_ERROR_WANT_WRITE:
printf("SSL_ERROR_WANT_WRITE write W BLOCK\n");
break;
case SSL_ERROR_WANT_READ:
printf("SSL_ERROR_WANT_READ write R BLOCK\n");
break;
case SSL_ERROR_WANT_X509_LOOKUP:
printf("SSL_ERROR_WANT_X509_LOOKUP write X BLOCK\n");
break;
case SSL_ERROR_ZERO_RETURN:
printf ("SSL_ERROR_ZERO_RETURN\n");
break;
case SSL_ERROR_SYSCALL:
printf("SSL_ERROR_SYSCALL write:errno=%d\n",
get_last_socket_error());
break;
case SSL_ERROR_SSL:
printf ("SSL_ERROR_SSL\n");
break;
}
return returned;
}
int sslRead(char *buf)
{
char *fName = "sslRead";
int returned = 0;
char eBuffer[BUFSIZ];
printf ("<==\n");
printf ("Reading some data ...\n");
returned = SSL_read (con, buf, 1024);
if (returned <= 0)
{
printf ("Error while reading from SSL connection. (returned: %d)\n",
returned);
returned = SSL_get_error (con, returned);
switch (SSL_get_error (con, returned))
{
case SSL_ERROR_NONE:
case SSL_ERROR_WANT_WRITE:
printf("SSL_ERROR_WANT_WRITE - read W BLOCK (returned: %d)\n",
returned);
break;
case SSL_ERROR_WANT_READ:
printf("SSL_ERROR_WANT_READ - read R BLOCK (returned: %d)\n",
returned);
break;
case SSL_ERROR_WANT_X509_LOOKUP:
printf("SSL_ERROR_WANT_X509_LOOKUP - read X BLOCK (returned:
%d)\n", returned);
break;
case SSL_ERROR_SYSCALL:
printf("SSL_ERROR_SYSCALL - read:errno=%d (returned:
%d)\n",get_last_socket_error(), returned);
break;
case SSL_ERROR_ZERO_RETURN:
printf("SSL_ERROR_ZERO_RETURN - closed (returned: %d)\n",
returned);
break;
case SSL_ERROR_SSL:
printf("SSL_ERROR_SSL (returned: %d\n", returned);
break;
}
}
else
printf ("%s\n", buf);
return returned;
}
int sslEndConnection()
{
char *fName = "sslEndConnection";
int returned = 0;
close (s);
/* Free SSL structure */
printf ("Free allocated SSL structure\n");
SSL_free(con);
/* Free SSL_CTX object */
printf ("Free allocated SSL_CTX object\n");
SSL_CTX_free(ctx);
return 0;
}
int main(int argc, char *argv[])
{
int returned;
if (argc != 3)
{
printf ("Usage: testssl <host> <port>");
return -1;
}
returned = sslInit();
returned = sslStartConnection(argv[1], atoi(argv[2]));
returned = sslWrite("GET / HTTP/1.0\r\n\r\n", 18); /* O whatelse data
*/
returned = sslRead(); /* Reading anything!!! */
returned = sslEndConnection();
return 0;
}
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]