Hi i have some problems using openssl library. I got this error :
14742:error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version
number:s3_pkt.c:293:
and my code is below :
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
typedef struct {
int socket;
SSL *sslHandle;
SSL_CTX *sslContext;
} connection;
#define SERVER "10.0.0.61"
#define PORTNUMBER 110
int tcpConnect ()
{
int error, handle;
struct hostent *host;
struct sockaddr_in server;
host = gethostbyname (SERVER);
handle = socket (AF_INET, SOCK_STREAM, 0);
if (handle == -1)
{
perror ("Socket");
handle = 0;
}
else
{
server.sin_family = AF_INET;
server.sin_port = htons (PORTNUMBER);
server.sin_addr = *((struct in_addr *) host->h_addr);
bzero (&(server.sin_zero), 8);
error = connect (handle, (struct sockaddr *) &server,
sizeof (struct sockaddr));
if (error == -1)
{
perror ("Connect");
handle = 0;
}
}
return handle;
}
connection *sslConnect (void)
{
connection *c;
c = malloc (sizeof (connection));
c->sslHandle = NULL;
c->sslContext = NULL;
char sbuf[1024] = "STLS\r\n";
c->socket = tcpConnect ();
if (c->socket)
{
SSL_load_error_strings ();
SSL_library_init ();
c->sslContext = SSL_CTX_new (TLSv1_client_method());
if (c->sslContext == NULL)
ERR_print_errors_fp (stderr);
c->sslHandle = SSL_new (c->sslContext);
if (c->sslHandle == NULL)
ERR_print_errors_fp (stderr);
if(write(c->socket, sbuf, sizeof(sbuf)) < 0 ){
fprintf(stderr, "STLS write error : USER");
return NULL;
}
if (!SSL_set_fd (c->sslHandle, c->socket))
ERR_print_errors_fp (stderr);
if (SSL_connect (c->sslHandle) != 1){
fprintf(stderr, "ssl_connect \n");
ERR_print_errors_fp (stderr);
}
}
else
{
perror ("Connect failed");
}
return c;
}
void sslDisconnect (connection *c)
{
if (c->socket)
close (c->socket);
if (c->sslHandle)
{
SSL_shutdown (c->sslHandle);
SSL_free (c->sslHandle);
}
if (c->sslContext)
SSL_CTX_free (c->sslContext);
free (c);
}
char *sslRead (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int received, count = 0;
char buffer[1024];
if (c)
{
while (1)
{
if (!rc)
rc = malloc (readSize * sizeof (char) + 1);
else
rc = realloc (rc, (count + 1) *
readSize * sizeof (char) + 1);
received = SSL_read (c->sslHandle, buffer, readSize);
buffer[received] = '\0';
if (received > 0)
strcat (rc, buffer);
if (received < readSize)
break;
count++;
}
}
return rc;
}
void sslWrite (connection *c, char *text)
{
if (c)
SSL_write (c->sslHandle, text, strlen (text));
}
int main (int argc, char **argv)
{
connection *c;
char *response;
c = sslConnect ();
sslWrite (c, "USER aydin\r\n");
response = sslRead (c);
printf ("%s\n", response);
sslDisconnect (c);
free (response);
return 0;
}
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [email protected]
Automated List Manager [email protected]