交叉編譯:libcurl/openssl支持SSL功能

1. 主機編譯https客戶端

  1. 準備主機開發環境
    sudo apt-get install libcurl4-openssl-dev
  2. 使用libcurl庫編寫代碼
  • 利用libcurl進行http連接
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();
if(curl) {

curl_easy_setopt(curl, CURLOPT_URL, "http://xx.xx.xx:8080/");

res = curl_easy_perform(curl);

if(res != CURLE_OK)
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));

curl_easy_cleanup(curl);
}
curl_global_cleanup();
  • 利用libcurl進行https單向認證
curl_easy_setopt(curl, CURLOPT_URL, "https://xx.xx.xx:8443/");

//簽發服務端證書的根證書
curl_easy_setopt(curl, CURLOPT_CAINFO, "root.crt");
curl_easy_setopt(curl, CURLOPT_CAPATH, "root.crt");

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);//開啓檢查服務端成熟
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);//不檢查域名
  • 利用libcurl進行https雙向認證
    在單向基礎上添加客戶端證書和私鑰
curl_easy_setopt(curl, CURLOPT_SSLCERT, "client.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "client_private.key");
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "123456");
  • 計算訪問耗時
struct timeval tpstart,tpend;
int timeuse;
gettimeofday(&tpstart, NULL);
......
gettimeofday(&tpend,NULL);
timeuse=1000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_usec-tpstart.tv_usec)/1000;
printf("use time:%dms\n", timeuse);
  1. 編譯主機客戶端代碼
    gcc testhppts.c -o testhttps -l curl

需要注意,設置根證書時,在主機Ubuntu X86僅設置 curl_easy_setopt(curl, CURLOPT_CAINFO, "root.crt"); 即可訪問https,在arm板上使用交叉編譯的libcurl會報錯,需要添加 curl_easy_setopt(curl, CURLOPT_CAPATH, "root.crt"); 才能正常訪問。

curl_easy_perform() failed: SSL peer certificate or SSH remote key was not OK

2. 交叉編譯:移植https客戶端

  1. 首先下載arm gcc交叉編譯器:https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads

  2. 解壓、加入環境變量:這裏使用開發板提供商的交叉編譯器
    export PATH=~/xxx/gcc-arm-linux-gnueabihf/bin:$PATH

  3. 編譯
    arm-linux-gnueabihf-gcc testhttps.c -o testhttps -l curl

    testhttps.c:2:23: fatal error: curl/curl.h: No such file or directory
    #include <curl/curl.h>
    

直接編譯報錯,我們需要arm版的libcurl。默認配置的libcurl不支持ssl,加上--with-ssl才支持,又必須依賴openssl,所以我們先交叉編譯openssl

  1. 交叉編譯openssl
    https://www.openssl.org/source/:openssl-1.1.0l.tar.gz

    cd openssl-1.1.0l
    ./config no-asm shared --cross-compile-prefix=arm-linux-gnueabihf- --prefix=/xxx/openssl_build
    //先刪除makefile中的兩處-m64,否則編譯報錯
    make
    make install
    

    no-asm:不使用匯編優化加速編譯,會導致問題
    shared:編譯動態庫
    --prefix=/xxx/openssl_build:指向編譯庫安裝目錄

  2. 交叉編譯libcurl
    下載源碼並解壓:https://curl.haxx.se/download.html

    cd curl-7.66.0
    env LDFLAGS=-R/xxx/openssl_build/lib ./configure --host=arm-linux-gnueabihf --build=x86_64-pc-linux-gnu --prefix=/xxx/curl_build --with-ssl=/xxx/openssl_build
    make
    make install
    

    configure之前一定要加 env LDFLAGS=-R/xxx/openssl_build/lib ,否則編譯時找不到libssl.so libcrypto.so。

  3. 交叉編譯https客戶端
    指定依賴的交叉編譯庫的路徑:將curl、openssl庫安裝目錄下的頭文件、so拷貝到include、lib目錄
    arm-linux-gnueabihf-gcc testhttps.c -o testhttps -lcurl -lssl -lcrypto -I/xxx/include -L/xxx/lib

  4. 拷貝程序、so到arm開發板

    • 查看so依賴
    $ readelf -d testhttps
    Dynamic section at offset 0xdc0 contains 27 entries:
    Tag        Type                         Name/Value
    0x00000001 (NEEDED)                     Shared library: [libcurl.so.4]
    0x00000001 (NEEDED)                     Shared library: [libssl.so.1.1]
    0x00000001 (NEEDED)                     Shared library: [libcrypto.so.1.1]
    

    交叉編譯的curl、openssl的lib下會有多個名字的so,多個符號鏈接指向同一個so。我們只需要將一個so拷貝到arm開發板,按so依賴表裏的名字命名即可。

  5. 執行
    如果so拷貝到了系統lib路徑,可以直接執行程序./testhttps。
    如果so拷貝到了自定義路徑,那麼需要在執行前執行export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/xxx/lib

2. https訪問速度測試

  • 局域網:arm板800M-C語言,1024位ca證書,helloworld網頁147字節
類型 1 2 3 平均(ms)
http 46 48 44 46
https單向認證 245 208 237 230
https雙向認證 252 249 270 257
  • 局域網:電腦I7-C語言,1024位ca證書,helloworld網頁147字節
類型 1 2 3 平均(ms)
http 13 19 13 15
https單向認證 149 147 146 147
https雙向認證 168 175 172 171

由測試結果可看出,在嵌入式設備上(低CPU、低內存),https的訪問速度,java和C不在同一數量級上,C程序大概比java快40倍。

  • 局域網:arm板800M-java,1024位ca證書,helloworld網頁147字節
類型 1 2 3 平均(ms)
http 1311 1320 1318 1316
https單向認證 8927 8711 8827 8821
https雙向認證 9643 9742 9793 9726
  • 局域網:電腦I7-java,1024位ca證書,helloworld網頁147字節
類型 1 2 3 平均(ms)
http 78 58 62 66
https單向認證 3323 3236 3161 3240
https雙向認證 3655 3544 3541 3580
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章