2

I'm trying to setup a patched version of OpenSSL to use DTLS and I'm having a lot of trouble. I'm assuming it is due to my lack of understanding of gcc and linking c libraries. In particular, I keep on seeing people say to link to the lib/ subfolder, but I cannot find one for OpenSSL. I also a question on building 32 bit OpenSSL, but I'm trying to do 64 bit.

OSX

Getting the source and patch:

wget ftp://ftp.openssl.org/source/openssl-1.0.1c.tar.gz # get latest stable OpenSSL
mv ~/Downloads/openssl-1.0.1c.tar.gz /usr/local/openssl-1.0.1c.tar.gz
cd /usr/local/openssl-1.0.1c.tar.gz
wget http://sctp.fh-muenster.de/dtls/dtls-bugs-1.0.1.patch # get the patch file

Building (64 bit, OpenSSL defaults to 32 bit):

export CFLAGS="-arch x86_64"
export LDFLAGS="-arch x86_64"
./Configure darwin64-x86_64-cc # 64 bit config command   
make # .a files should be built, great

Great, I have some libraries in the OpenSSL root directory:

/usr/local/openssl-1.0.1c$ ll lib*
-rw-r--r--  1 nflacco  staff  3286136 Jan  4 12:43 libcrypto.a
-rw-r--r--  1 nflacco  staff      260 Jan  4 12:43 libcrypto.pc
-rw-r--r--  1 nflacco  staff   570200 Jan  4 12:43 libssl.a
-rw-r--r--  1 nflacco  staff      275 Jan  4 12:43 libssl.pc

Now I'll try to compile a simple piece of code that uses the patched OpenSSL:

~$ gcc -L /usr/local/openssl-1.0.1c -lssl -lcrypto -I /usr/local/opt/openssl/include -o server server.c
ld: warning: _OPENSSL_ia32cap_P has different visibility (hidden) in /usr/local/openssl-1.0.1c/libcrypto.a(x86_64cpuid.o) and (default) in /usr/local/openssl-1.0.1c/libcrypto.a(cryptlib.o)
Undefined symbols for architecture x86_64:
  "_BIO_dgram_get_peer", referenced from:
      _generate_cookie_callback in ccfldIrE.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [server] Error 1

Ubuntu

Building:

./config
make

Checking for libraries (Disregard date, Ubuntu thinks it's Christmas):

/usr/local/openssl-1.0.1c$ ll lib*
-rw-r--r-- 1 root root 3170340 Dec 25 17:45 libcrypto.a
-rw-r--r-- 1 root root     264 Dec 25 17:46 libcrypto.pc
-rw-r--r-- 1 root root  534092 Dec 25 17:45 libssl.a
-rw-r--r-- 1 root root     279 Dec 25 17:46 libssl.pc

And, trying to compile:

gcc -L /usr/local/openssl-1.0.1c -lssl -lcrypto -I /usr/local/opt/openssl/include -o server server.c
/tmp/cc0DgDl1.o: In function `generate_cookie_callback':
server.c:(.text+0x8b): undefined reference to `RAND_bytes'
server.c:(.text+0xba): undefined reference to `SSL_get_rbio'
server.c:(.text+0xdc): undefined reference to `BIO_ctrl'
server.c:(.text+0x112): undefined reference to `CRYPTO_malloc'
/tmp/cc0DgDl1.o: In function `main':
server.c:(.text+0x163): undefined reference to `SSL_library_init'
server.c:(.text+0x168): undefined reference to `SSL_load_error_strings'
server.c:(.text+0x16d): undefined reference to `SSL_library_init'
/tmp/cc0DgDl1.o: In function `configure_server_ssl':
server.c:(.text+0x2f5): undefined reference to `SSL_CTX_set_cipher_list'
server.c:(.text+0x318): undefined reference to `SSL_CTX_ctrl'
server.c:(.text+0x333): undefined reference to `SSL_CTX_use_certificate_file'
server.c:(.text+0x35e): undefined reference to `SSL_CTX_use_PrivateKey_file'
server.c:(.text+0x379): undefined reference to `SSL_CTX_check_private_key'
server.c:(.text+0x3a4): undefined reference to `SSL_CTX_set_verify'
server.c:(.text+0x3c7): undefined reference to `SSL_CTX_ctrl'
server.c:(.text+0x3da): undefined reference to `SSL_CTX_set_cookie_generate_cb'
server.c:(.text+0x3ed): undefined reference to `SSL_CTX_set_cookie_verify_cb'
/tmp/cc0DgDl1.o: In function `start_server':
server.c:(.text+0x40b): undefined reference to `DTLSv1_server_method'
server.c:(.text+0x413): undefined reference to `SSL_CTX_new'
collect2: ld returned 1 exit status

UPDATE:

On Ubuntu I got it to compile by moving the libraries to the end of the compile command AND adding the flag -ldl to compile with no warnings:

gcc -L /usr/local/openssl-1.0.1c -I /usr/local/opt/openssl/include -o server server.c -lssl -lcrypto -ldl

On OSX, this command gives me the same error as earlier with not finding _BIO_dgram_get_peer.

4

1 回答 1

1

You have to place the libraries last on the command line:

gcc -L /usr/local/openssl-1.0.1c -I /usr/local/opt/openssl/include -o server server.c -lssl -lcrypto
#                                                                                     ^^^^^^^^^^^^^^

There are any number of duplicates of this problem here on this website explaining the whys and hows. The documentation for ld explains it well and is the ultimate reference.

于 2013-01-04T21:06:52.213 回答