Can't link OpenSSL code
Asked Answered
H

3

6

I am trying to build an openssl simple program. Here is the complete code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"

int main(int argc, char* argv[])
{
    AES_KEY aesKey_;
    unsigned char userKey_[16];
    unsigned char in_[16];
    unsigned char out_[16];
    strcpy(userKey_,"0123456789123456");
    strcpy(in_,"0123456789123456");

    fprintf(stdout,"Original message: %s", in_);
    AES_set_encrypt_key(userKey_, 128, &aesKey_);
    AES_encrypt(in_, out_, &aesKey_);

    AES_set_decrypt_key(userKey_, 128, &aesKey_);
    AES_decrypt(out_, in_,&aesKey_);
    fprintf(stdout,"Recovered Original message: %s", in_);      
    return 0;
}

I try to compile it using this command:

gcc -I/home/aleksei/openSSL0.9.8/include -o app -L . -lssl -lcrypto tema1.c

and I get this:

 /tmp/ccT1XMid.o: In function `main':
 tema1.c:(.text+0x8d): undefined reference to `AES_set_encrypt_key'
 tema1.c:(.text+0xa7): undefined reference to `AES_encrypt'
 tema1.c:(.text+0xbf): undefined reference to `AES_set_decrypt_key'
 tema1.c:(.text+0xd9): undefined reference to `AES_decrypt'
 collect2: ld returned 1 exit status

I am under Ubuntu 10.04. How can I get this to work ?

Hyetology answered 12/6, 2012 at 20:26 Comment(14)
The error is telling you that the linker cannot find the defintion for those functions. Even though you list the libraries, you specify the current library for the library paths, which might be the problem. Do you have libssl.so and libcrypto.so in the current directory?Allsopp
Yes I have them in the current directory.Hyetology
Even with this command gcc -I/home/aleksei/openSSL0.9.8/include -o app -L /home/aleksei/openSSL0.9.8/lib/ -lssl -lcrypto tema1.c it still gives me the same result. How can I make the linker find the definitions for those functions?Hyetology
If I write a location that doesn't exist to look for the libs it gives me this: /usr/bin/ld: cannot find -lssl . So it means that the linker finds them, but what?Hyetology
Is there a dependency on the order you specify the libraries? Maybe try reversing them or adding another -lssl after the -lcryptoKalk
Dump the symbols in the library. nm libcrypto.so | grep AES_. Do any of the missing symbols come up? If not (or their letter is U instead of T), then the symbols really aren't in the libcrypto library. They're in my copy of 0.9.8r that I compiled with default config but I presume this is an OpenSSL that you built yourself, yes?Mirabel
@Kalk I don't know if there is a dependency.Hyetology
@Mirabel it says T before them, they are all there.Hyetology
It is a libcrypto.a, not libcrypto.so. So they are static libs. I have just followed the instructions from the openssl website and I'm trying to get them to work now.Hyetology
Sorry, I have found an example on the web about linking and noticed that the name of the .c file was after the gcc command. Switched it like so and worked fine. Thank you for your time and sorry for such dumb questions.Hyetology
@Mirabel thanks, that is way I can get the library to load from the current directory.Hyetology
@lxClan: I deleted that comment because I forgot that the library names have to be at the end of the line. Sorry if it was confusing.Mirabel
Possible duplicate of Linking problem of OpenSSL library in existing C project.Bixler
Also, you should not use AES_encrypt and friends. You should be using EVP_* functions. See EVP Symmetric Encryption and Decryption on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides both confidentiality and authenticity. See EVP Authenticated Encryption and Decryption on the OpenSSL wiki.Bixler
M
8

You may be trying to statically link, but the -L option and -lcrypto are looking for a file to link with dynamically. To statically link to a specific library, just specify your .a file on the compiler command line after all your source files.

E.g.,

gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
Mirabel answered 12/6, 2012 at 22:9 Comment(0)
C
3

For those of you who have this same problem but are using Windows, Mingw and this OpenSSL for Windows (at this time: Win32 OpenSSL v1.0.2a). You need to link to libeay32.a that is located in C:\OpenSSL-Win32\lib\MinGW\ (after installing OpenSSL).

In my case I am using CMake and the powerful CLion IDE, so I had to rename the library to libeay32.dll.a because CMake wasn't locating the library. This is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)
project(openssl_1_0_2a)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories(C:\\OpenSSL-Win32\\include)

set(SOURCE_FILES main.cpp)

link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)

add_executable(openssl_1_0_2a ${SOURCE_FILES})

target_link_libraries(openssl_1_0_2a eay32)

I made the test with this example (which is borrowed from this answer):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"

int main(int argc, char* argv[])
{
    AES_KEY aesKey_;
    unsigned char userKey_[16];
    unsigned char in_[16] = {0};
    unsigned char out_[16] = {0};
    strcpy((char *) userKey_,"0123456789123456");
    strcpy((char *) in_,"0123456789123456");

    fprintf(stdout,"Original message: %s\n", in_);
    AES_set_encrypt_key(userKey_, 128, &aesKey_);
    AES_encrypt(in_, out_, &aesKey_);

    AES_set_decrypt_key(userKey_, 128, &aesKey_);
    AES_decrypt(out_, in_,&aesKey_);
    fprintf(stdout,"Recovered Original message: %s XXX \n", in_);
    return 0;
}
Canebrake answered 15/5, 2015 at 19:32 Comment(0)
O
0

I think the order of the parameter should be reset like follows:

gcc -I/home/aleksei/openSSL0.9.8/include -o app  tema1.c -L . -lssl -lcrypto
Occasionalism answered 26/2, 2022 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.