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:
- Downloaded OpenSSL for Windows (https://slproweb.com/products/Win32OpenSSL.html)
- Changed Properties -> VC++ Directories -> Include Directories to include the include directory of OpenSSL.
- C/C++ -> Code Generation -> Changed Runtime Library to "Multi-threaded /MT"
- Linker -> General -> Changed Additional Library Directories to include the "lib" directory of OpenSSL.
- 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:
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