openSSL Tutorial

QuickStart Guide

by Matt Flood

Using Borland C++ Builder and openssl

NOTES:

Here's exactly what I did from start to finish in order to build a simple HTTPS client using openssl and Borland C++ Builder. Installing openssl is documented in INSTALL.W32, included in the source distribution, but I've documented my experience here.....

  1. You Should have Borland C++ Builder installed on your system (I'm using C++ Builder 5).
  2. You Should have a utility to decode .tar.gz files (winzip,zipmagic, tar, gzip, etc...)
  3. Download the openssl source from here:
    1. http://www.openssl.org/source/
    2. I downloaded http://www.openssl.org/source/openssl-0.9.6c.tar.gz
  4. unzip and untar the sourcecode.
    1. after that, my source code ended up in a folder:
      C:\Documents and Settings\poopoohead\Desktop\ssl\openssl\openssl-0.9.6c
  5. You need to install ActiveState perl if you don't already have it...
    1. Download the latest stable release ActivePerl from http://www.activestate.com/
    2. When you install it, just make sure every box is checked on every dialogue that pops up to make sure the
      environment is set up completely
  6. open up a command prompt (>).
  7. This step is to make sure you have paths set up for Borland's make and compiler programs. Run the following two commands and make sure they are recognized commands. If you don't know what I mean - type >dingo666, and you'll see what an unrecognized command looks like. If the following commands are unrecognized, you'll need to modify
    your system's PATH variable. I didn't have to.
    1. >make
    2. >bcc32
  8. Navigate to the base directory of the openssl source
    1. >cd C:\Documents and Settings\poopoohead\Desktop\ssl\openssl\openssl-0.9.6c
  9. Run the following to create your makefile:
    1. >ms\bcb4.bat
  10. run make to complete the build.
    1. >make -f bcb.mak
  11. if make ran successfully, you should now have 3 new folders under the install dir:
    1. inc32 (this is where the include files are....)
    2. out32 (this is where the .libs are)
    3. tmp32 (this is nothing to be concerned with)
  12. Now you're ready to build an ssl application!!
  13. Power up Borland C++ Builder
  14. Create a new app (should already be a new app ready to work with...)
  15. Add a Memo object to the main form (leave default name: Memo1), this is just so we can see the results or the request.
  16. You need to add the openssl include directory and libraries to your project
    1. From the menu: Project->Options (or just type shift + ctrl + F11)
    2. Click the "Directories/Conditionals" Tab
    3. Add the include directory:
      1. click the elipses [...] button to the far right of "include path"
      2. click the elipses [...] button that appears on the pop up dialog that appears
      3. navigate to your inc32 folder
      4. make sure "inc32" is highlighted, NOT the "openssl" subdirectory.....
      5. click [OK] to select the folder
      6. Click the [Add] button, then [OK]
    4. Add the openssl library path (I'm not sure this step is neccesary, but I did it anyway....):
      1. Do the same thing you did for the include directory above, adding "out32" to "Library Path"
    5. Click [OK] to close the properties dialog
    6. Now add the ssl libraries to your project:
      1. From the menu: Project->Add to Project (or just type shift + F11)
      2. Make sure "Files of Type" is set to "Libray File (.lib)"
      3. Navigate to your out32 folder
      4. select all library files that appear in that directory listing, then click [Open] button
  17. Add the following include directives to the application:
    #include <openssl/crypto.h>
    #include <openssl/x509.h>
    #include <openssl/pem.h>
    #include <openssl/ssl.h>
    #include <openssl/err.h>
  18. Create a SOCKET and connect to an https server on port 443 like you normally would....
  19. Assuming your socket is named sock, you then perform your ssl communications like this:
    
    
               SOCKET sock;
               // build regular SOCK_STREAM sock and connect to server
               // ... (I'm not going into these details)
               // pretend sock is now connected....
    
    
               // here's the data items we'll use....
              char buf[1000];
               char request[1000];
    
               SSL_CTX *ctx;
               SSL *ssl;
               int err;
    
               // initialize SSL stuff
               //
               SSL_load_error_strings();
               SSL_library_init();
    
    
               // build the SSL objects...
               //
               ctx=SSL_CTX_new(SSLv2_client_method());
               ssl=SSL_new(ctx);          
               
    
               // assign the socket you created for SSL to use
               //
               SSL_set_fd(ssl, sock);
    
    
               // communicate!!
               /////////////////////////////////////////////
                 err=SSL_connect(ssl);
                 sprintf(request, 
                        "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n","/" , 
                        "www.theserver.com");
                 err=SSL_write(ssl, request, strlen(request));
                 while(1)
                 {
                    int read_size;
                    read_size=SSL_read(ssl, buf, sizeof(buf)-1);
                    buf[read_size]='\0';
                    if(read_size > 0)
                   {
                         // I'm assuming you have a Memo object 
    //on your application form... // Memo1->SetSelTextBuf(buf); } else { break; } } // free the SSL stuff.... // SSL_shutdown(ssl); SSL_free(ssl); SSL_CTX_free(ctx);
     
  20. That's it, build and run the program...