0

I have following test code to get familiar with the OpenSSL library:

#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bio.h>


RSA* createRSA2(const char* key) {
    RSA* rsa = NULL;
    BIO* keybio;
    keybio = BIO_new_mem_buf(key, -1); // !!!
    if (!keybio) {
        return;
    }
    rsa = PEM_read_bio_RSA_PUBKEY(keybio, NULL, NULL, NULL);  // !!!
    if (!rsa) {
        return;
    }
    BIO_free(keybio); // !!!
    return rsa;
}

int main(void) {
    char publicKey[] = "-----BEGIN PUBLIC KEY-----\n"\
        "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy8Dbv8prpJ/0kKhlGeJY\n"\
        "ozo2t60EG8L0561g13R29LvMR5hyvGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+\n"\
        "vw1HocOAZtWK0z3r26uA8kQYOKX9Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQAp\n"\
        "fc9jB9nTzphOgM4JiEYvlV8FLhg9yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68\n"\
        "i6T4nNq7NWC+UNVjQHxNQMQMzU6lWCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoV\n"\
        "PpY72+eVthKzpMeyHkBn7ciumk5qgLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUy\n"\
        "wQIDAQAB\n"\
        "-----END PUBLIC KEY-----\n";
    
    const char* msg = "TESTMESSAGE";
    RSA* rsaKey = createRSA2(publicKey);

    printf("DONE\n");

    RSA_free(rsaKey);

    return 0;
}

It does not really do anything currently. Before continuing playing around with my example I would like to know how I can statically link the OpenSSL library in order to get a standalone exe.

Currently I have following setup:

  1. Downloaded OpenSSL for Windows (https://slproweb.com/products/Win32OpenSSL.html)
  2. Changed Properties -> VC++ Directories -> Include Directories to include the include directory of OpenSSL.
  3. C/C++ -> Code Generation -> Changed Runtime Library to "Multi-threaded /MT"
  4. Linker -> General -> Changed Additional Library Directories to include the "lib" directory of OpenSSL.
  5. Linker -> Input -> Changed Additional Dependencies to contain libssl.lib and libcrypto.lib.

This code compiles fine, however Visual Studio also generates a DLL (libcrypto-1_1.dll) where the application depends on. If I change libssl.lib and libcrypto.lib to libssl_static.lib and libcrypto_static.lib I receive linking errors like the following:

Linking errors

How am I able to statically link OpenSSL to receive a standalone executable?

EDIT: here is the picture as text:

Error   LNK2019 unresolved external symbol __imp__WSAStartup@8 referenced in function _BIO_sock_init    Project2    C:\Users\User\source\repos\Project2\Project2\libcrypto_static.lib(b_sock.obj)   1   
Error   LNK2019 unresolved external symbol __imp__WSACleanup@0 referenced in function _bio_sock_cleanup_int Project2    C:\Users\User\source\repos\Project2\Project2\libcrypto_static.lib(b_sock.obj)   1   
Error   LNK2019 unresolved external symbol __imp__WSAGetLastError@0 referenced in function _BIO_accept  Project2    C:\Users\User\source\repos\Project2\Project2\libcrypto_static.lib(b_sock.obj)   1   
qweqwe
  • 33
  • 6
  • All the linker error messages you get will be available in a tab as text. Please [edit] your question to copy-paste that text. – Some programmer dude Jun 26 '20 at 12:32
  • As for your problem, when you link with a static library, you need to link with the dependencies of that library as well. Like the Windows socket library for OpenSSL. – Some programmer dude Jun 26 '20 at 12:33
  • I edited the original question. Might you tell me how I am able to link with these dependencies / find out which I need? – qweqwe Jun 26 '20 at 12:42

1 Answers1

0

To solve the unresolved Winsock errors, look at the OpenSSL project. Read the Notes-Windows.txt. You need to link with ws2_32.lib, crypt32.lib (the other libraries are usually linked by default in Windows projects).

For static linked libraries, you may need to build openssl *_static.lib. Sequence: clone project, install build utilities, edit makefile for static linking.