Maverickgugu
Maverickgugu

Reputation: 777

Running a C code with Open SSL

I am not able to compile the following code. I run it in a MACOSX with the following command in the terminal:
$gcc filename.c -lssl

I understand that MACOS already has an inbuilt openssl (which i can call as a command in the terminal). But I am not sure if i am linking it to the library in the correct fashion.

#include <stdio.h>
#include <openssl/evp.h>
main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

if(!argv[1]) {
    printf("Usage: mdtest digestname\n");
    exit(1);
}

md = EVP_get_digestbyname(argv[1]);

if(!md) {
    printf("Unknown message digest %s\n", argv[1]);
    exit(1);
}

EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}

My error is:
Undefined symbols: "_EVP_MD_CTX_cleanup", referenced from: _main in ccfZG7WJ.o
ld: symbol(s) not found collect2: ld returned 1 exit status

I am surprised by the error since it shows "Undefined Symbols" instead of "Header file not found". Can you please help me debug this issue.

Thanks!

Upvotes: 2

Views: 905

Answers (3)

indiv
indiv

Reputation: 17846

OpenSSL has 2 libraries: libssl and libcrypto. Include libcrypto, too.

gcc filename.c -lssl -lcrypto

(You actually don't need -lssl at all for this program, but it doesn't hurt to include it)

libssl is for making actual SSL or TLS connections, like when connecting to a web server with https. It also exports SSL_load_error_strings, which is useful even when working only with the cryptography library.

libcrypto is the cryptography library. It has encryption algorithms, key and certificate generation and verification functions, message authentication algorithms, and so on.

Upvotes: 3

Charlie Martin
Charlie Martin

Reputation: 112336

You're right to suspect the library isn't being linked correctly. There are two possibilities:

  • the library isn't being linked at all
  • the library being linked doesn't include that symbol, suggesting a version mismatch.

The second one seems more probable, but the first is easier to check. A man -k ssl should point out the library man pages.

Upvotes: 1

Elalfer
Elalfer

Reputation: 5338

Most probably in your case "Undefined Symbols" means that you are not linking the correct library.

Upvotes: 0

Related Questions