I try to learn how to work with Opensll library. I make this simple client

#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>

#include <stdio.h>

int main()
{
    /* Define varialbles */
    SSL * ssl;
    SSL_CTX * ctx;
    X509 * server_cert;
    int p,err;
    char * request = "GET / HTTP/1.1\x0D\x0AHost: 
www.verisign.com\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A";
    char r[1024];
    BIO * bio;
    
    
    /* Set up the library */
    ERR_load_BIO_strings();
    SSL_load_error_strings();
    OpenSSL_add_all_algorithms();
    SSL_library_init();
    
    
    /* Set up the SSL context */
    ctx = SSL_CTX_new(SSLv23_client_method());
    
    
    /* Load the trust store */
    SSL_CTX_use_certificate_file(ctx, "cakey.pem", SSL_FILETYPE_PEM);
    SSL_CTX_use_PrivateKey_file(ctx, "cakey.pem", SSL_FILETYPE_PEM);
    if(! SSL_CTX_load_verify_locations(ctx, "cacert.pem", NULL))
    {
        fprintf(stderr, "Error loading trust store\n");
        ERR_print_errors_fp(stderr);
        SSL_CTX_free(ctx);
        return 0;
    }
    
          
    /* Setup the connection */
    bio = BIO_new_ssl_connect(ctx);
        

    /* Set the SSL_MODE_AUTO_RETRY flag */
    BIO_get_ssl(bio, & ssl);
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
        

    /* Create and setup the connection */
    BIO_set_conn_hostname(bio, "www.verisign.com:https");
    if(BIO_do_connect(bio) <= 0)
    {
        fprintf(stderr, "Error attempting to connect\n");
        ERR_print_errors_fp(stderr);
        BIO_free_all(bio);
        SSL_CTX_free(ctx);
        return 0;
    }
    
        
    /*information about connection*/
    printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
    server_cert = SSL_get_peer_certificate (ssl);
    printf ("%s\n",SSL_get_version(ssl));
    printf ("Server certificate: %s\n\n\n", server_cert);
        

    /* Send the request */
    BIO_write(bio, request, strlen(request));
   
    
    /* Read in the response */            
    do{
       p = BIO_read(bio, r, 1023);
       if(p <= 0) {break;}
       r[p] = 0;
       printf("%s", r);
    }while (BIO_pending(bio)>0);
    
    
    /* Close the connection and free the context */
    err = SSL_shutdown(ssl);
    BIO_free_all(bio);
    SSL_CTX_free(ctx);
    return 0;
}

I have a problem in the section (Read in the response). For my futher work i 
need to know if in the internal openssl buffer are still some data. I thing 
that best method to recognize it will be the BIO_pending(), but this function 
gives me everytime number 0, that there aren`t any data. But when I try to read 
(BIO_read()) again some data are still there.

Please can you advise me how to change my code.

P.S. I`m sorry for my English. :o(
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to