Does anyone have code to demonstrate a simple SSL client/server?
The best documentation I was able to find is at
http://www.columbia.edu/~ariel/ssleay/
and even that documentation is sparse and out of date.

following the documentation at
http://www.columbia.edu/~ariel/ssleay/ssl_ctx.html
to create an SSL enabled server

my code crashes when I connect the socket descriptor to the SSL "handle"
using SSL_set_fd(ssl,newsockfd)

My code is below. can anyone help?

thanks,
Son

---
this is compiled on Redhat 5.2 
gcc -o t ssl_biotest.c -DNOPROTO -I/opt/openssl/include -L/opt/openssl/lib 
-lssl -lcrypto

--
#include <bio.h>
#include <ssl.h>

#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <string.h>

#define PORT 1234
#define CERT_FILE "server.pem"

main()
{
  int sockfd, newsockfd, clilen;
  int opt=1;
  struct sockaddr_in cli_addr, serv_addr;
  SSL* ssl;
  SSL_CTX* ctx;
  char buff[100];

  if( (sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
      printf("can't create socket\n");
      exit(-1);
    }

  bzero((char*)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(PORT);

  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));

  if( bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
    {
      close(sockfd);
      printf("can't bind\n");
      exit(-2);
    }

  listen(sockfd,5);

  
  newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
  ctx = SSL_CTX_new(SSLv23_server_method());
  
  ssl = SSL_new(ctx);
  printf("0\n");
  SSL_set_fd(ssl,newsockfd);
  printf("0.1\n");
  if (!SSL_CTX_use_certificate_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
    {
      printf("1\n");
      return 0;
    }

  if (!SSL_CTX_use_PrivateKey_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
    {
      printf("2\n");
      return 0;
    }

  if (!SSL_CTX_check_private_key(ctx))
    {
      printf("3\n");
      return 0;
    }

  SSL_accept(ssl);
  printf("reading...\n");
  SSL_read(ssl,buff,10);
  printf("buff=%s\n",buff);

  close(sockfd);
}


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to