通常Linux系統自帶OpenSSL,但是其so文件由於沒有debug信息,因此無法跟蹤內部函數,對於學習
不太方便,需要通過源碼重新安裝。
我的Linux系統是CentOS7,自帶的OpenSSL的版本是1.0.1e。在網上下載了OpenSSL1.0.1f后,通過
如下方法安裝
- ./config --prefix=/usr/local --openssldir=/usr/local/ssl
- make && make install
- ./config -d shared --prefix=/usr/local --openssldir=/usr/local/ssl
- make clean
- make && make install
先安裝靜態庫版本,再安裝動態庫版本。安裝目錄為/usr/local下。安裝動態庫時增加-d選項,因為
調試時使用動態庫,需要跟蹤代碼。
這樣后面就可以寫調試代碼調試,如下面的例子:
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
int main()
{
BIO *b = NULL;
int len = 0;
char *out = NULL;
b = BIO_new(BIO_s_mem());
if (NULL == b)
{
return 0;
}
len = BIO_write(b,"openssl",4);
len = BIO_printf(b,"%s","zcp");
len = BIO_ctrl_pending(b);
out = (char *)OPENSSL_malloc(len);
if (NULL == out)
{
return 0;
}
memset(out, 0, len);
len = BIO_read(b,out,len);
printf("out is : %s\n",out);
OPENSSL_free(out);
BIO_free(b);
return 0;
}
在當前路徑下創建一個新的動態庫軟鏈接:
ln -s /usr/local/lib64/libcrypto.so.1.0.0 libcrypto.so.10
然后gcc編譯時指明使用這個libcrypto:
gcc -g -DDEBUG -o openssl_mem_bio_test openssl_mem_bio_test.c -lcrypto -Wl,-rpath=.