Thread: Need help with OpenSSL

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    7

    Need help with OpenSSL

    Hi,

    I'm currently working on a project for a class in shcool, i need to develop a basic client to interact with the openssl server, using a trustore and CA's.

    I simply run the openssl server with this command (I must be in /certs folder) in one terminal:
    openssl s_server -cert crt.pem -key key.pem

    and then I run my code to emulate the client:

    Code:
    #include "openssl/ssl.h"
    #include "openssl/bio.h"
    #include "openssl/err.h"
    
    #include "stdio.h"
    #include "string.h"
    
    int main(int argc, char *argv[])
    { 
    
        BIO * bio;
        SSL * ssl;
        SSL_CTX * ctx;   
    
        char buffer1[1024], buffer2[1024];
        int j,p;	
        char *hostname, *port;
    
        /* Verificar se foram colocados todos os argumentos */
        if ( argc != 3 )
        {
    
            printf("Sintaxe: &#37;s <hostname> <portnum>\n", argv[0]);
            exit(0);
        }	
    
        hostname = argv[1];
        port = argv[2];
    
    	
    
        /* Preparar a biblioteca */
        SSL_library_init();
        ERR_load_BIO_strings();
        SSL_load_error_strings();	/* Para o registo e msgs de erro */	
    
        OpenSSL_add_all_algorithms();
         ENGINE_load_builtin_engines();	
    
        /* Criar o contexto */
        ctx = SSL_CTX_new(SSLv23_client_method());
    
        /* Carregar o trust store */
        if(! SSL_CTX_load_verify_locations(ctx, "/root/Desktop/Trab2_cr/certs/TrustStore.pem", NULL))
        {
            fprintf(stderr, "Erro ao carregar o trust store\n");
            ERR_print_errors_fp(stderr);
            SSL_CTX_free(ctx);
            return 0;
        }
    
    
    
        /* Realizar a liga&#231;ao ao servidor */ 	
        bio = BIO_new_ssl_connect(ctx);
    
    
        /* Ligar a flag SSL_MODE_AUTO_RETRY */
        BIO_get_ssl(bio, & ssl);
        SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
    
        /* Criar e configurar a liga&#231;ao */
        BIO_set_conn_hostname(bio, hostname);
        BIO_set_conn_port(bio, port);
    
        if(BIO_do_connect(bio) <= 0)
    
        {
            fprintf(stderr, "Erro ao tentar ligar ao servidor\n");
            ERR_print_errors_fp(stderr);
            BIO_free_all(bio);
            SSL_CTX_free(ctx);
            return 0;
    
        }
    
        /* Validar o certificado do servidor */
        if(SSL_get_verify_result(ssl) != X509_V_OK)
        {
    
            fprintf(stderr, "Erro ao validar o certificado: %i\n", SSL_get_verify_result(ssl));
            BIO_free_all(bio);
            SSL_CTX_free(ctx);
            return 0;
    
        }
    
        printf("Ligado com encriptacao do tipo %s \n", SSL_get_cipher(ssl));
    
        /* Enviar/receber mensagens */
        for(;;)
        {
    
    		/* Enviar msg*/
    		scanf("%s", buffer1);	
       		j = BIO_write(bio, buffer1, strlen(buffer1));
    		if(j <= 0)
    			break;
    		buffer1[j] = 0;		
    
    		/* Receber msg */
                    p = BIO_read(bio, buffer2, 1023);
                    if(p <= 0) 
    			break;
                    buffer2[p] = 0;
    		printf("%s\n", buffer2);
        }
    
        /* Close the connection and free the context */
        BIO_free_all(bio);
        SSL_CTX_free(ctx);
        return 0;
    
    }
    The problem with this is that,when I send a message to server I must wait for his reply, or else things get messed up because of the for() cycle:
    Code:
    /* Send/receive messages */
    for(;;)
    {
    		/* Enviar msg*/
    		scanf("%s", buffer1);
       		j = BIO_write(bio, buffer1, strlen(buffer1));
    		if(j <= 0)
    			break;
    		buffer1[j] = 0;		
    
    		/* Receber msg */
                    p = BIO_read(bio, buffer2, 1023);
                    if(p <= 0)
    			break;
                    buffer2[p] = 0;
    		printf("%s\n", buffer2);
        }
    Do any of the experts have any sugestion to change this in a way that i don't need to wait for the server reply?!

    I don't want my homework done, I just want sugestions so that i can then try to figure it out by my own.


    Thanks in advance.

    PS- sorry for the formating on the code, did my best for align it manually.
    Last edited by Ricardo_R5; 05-07-2007 at 06:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using OpenSSL with Dev-C++
    By Smiley10 in forum C Programming
    Replies: 2
    Last Post: 07-08-2006, 10:27 AM
  2. OpenSSL AES library documentation
    By kronixx in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 12:24 PM
  3. openssl on win2k
    By rzcodeman in forum Networking/Device Communication
    Replies: 4
    Last Post: 04-09-2004, 07:58 PM
  4. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 07:46 PM