Hello,

I am using openSSL 0.9.2b. I have written a simple client program
which try to establish the communication with a server understands
SSL.

The sample code is attached. It runs fine. But when I use 'purify'
to debug it, I found a lot of UMR (Uninitialized Memory Read) errors, 
most of them are from RAND-seed(), ssleay_rand().

Are those errors supposed to be there? or I missed some initialization
steps?

Thanks,
Renjie
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
/* OpenSSL headers*/
#include "ssl.h"
#include "x509.h" 
#include "err.h" 

int init_socket(char *hostName, int port);
void check_err();
void check_ssl_err();

void main (int argc, char* argv[])
{
  int         err = 0;
  int         sd;
  char*       str = NULL;
  char        buf[4096];
  SSL_METHOD  *meth = NULL;
  SSL_CTX     *ctx  = NULL;
  SSL         *ssl  = NULL;
  X509        *server_cert = NULL;

  sd = init_socket(argv[1], atoi(argv[2]));

  /* initialize SSL */
  SSL_load_error_strings();
  SSL_library_init();
  if((meth=SSLv3_client_method())==NULL) check_ssl_err();
  if((ctx=SSL_CTX_new(meth))==NULL) check_ssl_err();                    
  if((ssl=SSL_new(ctx))==NULL) check_ssl_err();

  /* establishing SSL connection through the socket */
  SSL_set_fd(ssl,sd);
  if((err=SSL_connect(ssl))==-1) check_ssl_err();

  /* communicate through SSL connection */
  err = SSL_write(ssl,request,strlen(request));  
  if(err==-1) check_ssl_err(); 
 
  do 
  {
    err = SSL_read(ssl,buf,sizeof(buf)-1);                     
    if(err==-1) check_ssl_err();
    buf[err] = '\0';
    printf ("Got %d chars:\n%s\n", err, buf);
  } while(err!=0);

  /* shutdown and clean up. */
  shutdown(sd,1);
  close(sd);
  SSL_free(ssl);
  SSL_CTX_free(ctx);
}

int init_socket(char *hostName, int port)
{
  int sd;
  int err;
  struct hostent  *host=NULL;
  struct sockaddr_in sa;

  if(hostName==NULL) return -1;
  if((host=gethostbyname(hostName))==NULL) check_err();
  bcopy(host->h_addr, &sa.sin_addr, sizeof(sa.sin_addr));
  sa.sin_family = AF_INET;   
  sa.sin_port = htons(port);    
  if((sd=socket(AF_INET, SOCK_STREAM,0))==-1) check_err();
  if((err=connect(sd, (struct sockaddr*) &sa,sizeof(sa)))==-1) check_err();
  return sd;
}

void check_err()
{
  perror("Error:");
  exit(1);
}

void check_ssl_err()
{
  int err;
  char *str;
  char buf[1024];

  err = ERR_get_error();
  str = ERR_error_string(err, buf);
  printf("%s\n", str);
  exit(1); 
}

Reply via email to