Menu

simple ssl problem

Dr deo
2008-12-16
2012-09-26
  • Dr deo

    Dr deo - 2008-12-16

    i am trying to make a simple tcp connection without authentication, for starters, using the openssl library( i can use winsock but its hard to use for authentication so am trying out openssl).

    i realised that the following are required for using the openssl library(correct me if am wrong so that we are on the same foot here)
    1. an installation of the library (for example i downloaded mine as a package from devpaks.org)
    2.linking the project to libeay to avoid linker errors.

    all set, i was surprised when the code bellow did not work. the only error displayed was
    " F:\deo\deo\Makefile.win [Build Error] [Project1.exe] Error 1 "

    what does this error mean and how can it be corrected.

    some data you may find useful
    1. operating system : microsoft windows xp service pack 2
    2. compiler: devc++ bloodshed version 4.9.9.2


    source code

    //openssl related libraries

    include "openssl/ssl.h"

    include "openssl/bio.h"

    include "openssl/err.h"

    //and other libraries

    include "cstdio"

    include "cstring"

    //remember to link the project to libeay library
    //under "project" > "project options" > parameters > linker

    using namespace std;

    int main()
    {
    BIO * bio;
    int p;

    char * request = "GET / HTTP/1.1\x0D\x0AHost: www.verisign.com\x0D\x0A\x43onnection:   Close\x0D\x0A\x0D\x0A";
    

    //the above was our http request. it consists of GET /path_to_file HTTP_PROTOCOL_VERSION //then other headers. the "host" header is necessary for http 1.1 requests. each line is separated
    //a carriage return, linefeed pair which are encoded above in hexadecimal ie \r\n is equivalent to // \x0D\X0A

    char r[1024];//our receiving buffer where we store our reply
    
    // Set up the library
    
    ERR_load_BIO_strings();
    SSL_load_error_strings();
    OpenSSL_add_all_algorithms();
    
    // Create and setup the connection
    
    bio = BIO_new_connect("www.verisign.com:80");
    if(bio == NULL) //there was an error
     { 
       printf("BIO is null\n");//write error message
       system("pause");//pause dos screen
       exit(1); //abnormally terminate the program
      }
    
    if(BIO_do_connect(bio) <= 0)//there is also an error if this function returns o or -1
    {
        ERR_print_errors_fp(stderr);
        BIO_free_all(bio);
        system("pause");
        exit(1);
    }
    
    // Send the request
    
    BIO_write(bio, request, strlen(request));//send data
    
    // Read in the response
    
    for(;;)
    {
        p = BIO_read(bio, r, 1023);
        if(p <= 0) break;
        r[p] = 0;
        printf("%s", r);
    }
    
    // Close the connection and free the context
    
    BIO_free_all(bio);
    
    system("PAUSE");
    return EXIT_SUCCESS;
    

    }
    //thank you for even bothering to read to the end of the project. waiting to hear what you think

     
    • cpns

      cpns - 2008-12-16

      > what does this error mean and how can it be corrected.

      It means only that the build failed (i.e. a command issued by make returned an non-zero errorlevel). The reason for this will usually be indicated in earlier in the compile log (where the execution of the failing command is shown), which is why you should always post the entire "Compile Log" text. Even if the command issues no message itself, we will at least be able to determine what teh comand was and how it was invoked.

      If you are not looking at the "Compile Log" tab, you should be - not the "Compiler" tab - that filters the log, and only shows messages that ar in the expected format for compiler, pre-processor, and linker messages - that is not the only thing that could fail.

      The log also shows us exactly how the code was really built, rather than you describing it, and us taking your word for it. Accurate and simple, and less typing for you.

      Clifford

       
    • Dr deo

      Dr deo - 2008-12-17

      "Mechanically, how did you try to do the link?"

      1. I created an empty project.
      2. Added a source code file to it.
      3. In the menu bar chose "project" >"project options" >"parameters">"linker"

      4. then simply typed "libeay"

                                                                  trueleowdeo
        
       
      • Wayne Keen

        Wayne Keen - 2008-12-17

        You do realize that the command for linking a library contained in a file called (for example)

        libxyz.a

        is of the form

        -lxyz

        where that is a small L

        Wayne

         
    • Dr deo

      Dr deo - 2008-12-17

      Thanks but am curious....

      Linking with -leay produced this compile log;

      compile log

      Compiler: Default compiler
      Building Makefile: "F:\deo\deo\Makefile.win"
      Executing make...
      make.exe -f "F:\deo\deo\Makefile.win" all
      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

      g++.exe main.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" -leay32

      main.o(.text+0x4c):main.cpp: undefined reference to `SSL_load_error_strings'
      collect2: ld returned 1 exit status

      make.exe: *** [Project1.exe] Error 1

      Execution terminated

      Since the error was being caused by this SSL_load_error_strings', i simply deleted it and the project compiled fine. thanks alot. But for the sake of curiosity, is there a way to bypass the error without deletingSSL_load_error_strings' function?

      trueleowdeo

       
    • cpns

      cpns - 2008-12-17

      If you specify a -l option for a file that does not exist it will be ignored, so my guess is that either there is no file C:/Dev-Cpp/lib/libeay32.a, or if there is then it is not the one that contains SSL_load_error_strings.

      Use the nm.exe tool in c:\dev-cpp\bin to list the symbols in an archive:

      c:\dev-cpp\bin\nm -g libeay32.a

      The documentation does not help, but from its name, I would guess that you also need ot link libssl32.a (-lssl32). You could use nm to check:

      c:\dev-cpp\bin\nm -g libssl32.a

      Clifford

       
    • Dr deo

      Dr deo - 2008-12-19

      my project worked out perfectly. Thanks for the posts. this i learnt the following.
      1. that a library called libxxx.a is linked by using -lxxx.
      2. the two libraries i linked to the project are libeay32.a and libssl32.a
      ie (-leay32 and -lssl32)
      3. that i had to post the entire compile log :)
      4. to run the final executable file , i found that libeay32.dll and libssl32.dll
      had to be in the same directory as the executable

      trueleowdeo

       
    • cpns

      cpns - 2008-12-19

      Thanks, a summary is always useful.

      > 4. to run the final executable file , i found that libeay32.dll
      > and libssl32.dll had to be in the same directory as the
      > executable

      Not entirely true. They have to either be in the same folder as the executable or in a path specified in the PATH environment variable, which is used by the OS as a search list for executable files and DLLs.

      Clifford

       
    • Dr deo

      Dr deo - 2008-12-16

      Here is the compile log. Ok it seems to say that 'libeay' doesnot exist . thanks thats one step closer to the solution. probably i linked with the wrong library . What is the correct library you link with in openssl?

                                                                                  trueleowdeo
      

      compile log

      g++.exe main.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" libeay

      g++.exe: libeay: No such file or directory

      make.exe: *** [Project1.exe] Error 1

      Execution terminated

       
    • Wayne Keen

      Wayne Keen - 2008-12-17

      "g++.exe main.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" libeay "

      Mechanically, how did you try to do the link?

      Wayne

       
    • cpns

      cpns - 2008-12-17

      The log, the whole log and nothing but the log! Simply Copy & paste the text as is, no omissions! (right click in the "Compiler Log" window to copy all the text.

      As it happens in this case you have probably provided sufficient information, (but there may be other problems we could have spotted that you have hidden from us). Generally if you have a problem you are asking about, almost by definition you are not in a position to determine the required information, so post it all, it is at zero cost to yourself to do so.

      You need -leay not libeay as the option.

      The -l<xxx> option causes the linker to search for lib<xxx>.a in any of the paths specified by preceeding -L<path> options. You only have one of those, so -leay will look for C:/Dev-Cpp/lib/libeay.a. Personally I think it is a bad idea to put third-party libraries in the basic tool-chain installation, and would place the library elsewhere and add an additional library directory. However it is probable that that is where the DevPak placed it.

      Alternatively you could place the fully qualified path in the command line 9that is what tje "add object file or library" browse button does, but just specifying libeay, will look for a file of exactly that name (no extension) in the project directory.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.