記一次在mac上源碼編譯curl,使其支持NSS的過程


一、背景

在一次學習https原理的過程中,希望客戶端指定特定的cipher suites來抓包分析SSL/TLS的握手過程,就想到了使用curl工具,而不是使用瀏覽器。

接下來使用man curl找到—ciphers選項,同時man文檔中同時提到了需要到https://curl.haxx.se/docs/ssl-ciphers.html這個地方去尋找具體的ciphers suites列表,從中我們可以看到這樣一句話:The names of the known ciphers differ depending on which TLS backend that libcurl was built to use. This is an attempt to list known cipher names.大概意思就是不同的TLS實現(包括openssl、NSS、LibreSSL等),即使是同一個ciphers,curl工具能識別的名稱也是不同的。

在不同的操作系統中,系統自帶的curl工具,使用的SSL庫是不同的,可以使用curl -V來查看:

1.1、mac自帶的curl

mac-curl

1.2、redhat系列系統自帶的curl

redhat-curl

由於在上面提到的文檔中,沒有與LlibreSSL的ciphers有關的介紹,因此決定在mac上源碼編譯curl,使其支持NSS。

二、環境介紹

環境 版本
操作系統 macOS Mojave(10.14)
curl 7.65.1
NSS 3.44

三、過程

3.1、下載curl和nss源碼包

在https://curl.haxx.se/download/中選擇對應的curl源碼包進行下載

在https://ftp.mozilla.org/pub/security/nss/releases/中選擇對應的nss源碼包進行下載

Because NSS depends on the base library NSPR, you should download the archive that combines both NSS and NSPR:因為nss依賴NSPR,所以應該選擇NSS和NSPR的組合包,例如:https://ftp.mozilla.org/pub/security/nss/releases/NSS_3_45_RTM/src/nss-3.44-with-nspr-4.21.tar.gz

3.2、安裝NSS

3.2.1、源碼編譯

tar -zxf nss-3.44-with-nspr-4.21.tar.gz
cd nss-3.44
make -C nss nss_build_all USE_64=1

編譯過程參照https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Building ,編譯時會遇到下面的報錯,暫時還沒有解決掉,之所以還要進行這個明知會失敗的操作,是因為下面還需要從這個編譯生成的dist目錄中復制一些.h文件

Undefined symbols for architecture x86_64:
  "_PK11_FindRawCertsWithSubject", referenced from:
      nss_test::PK11FindRawCertsBySubjectTest_TestNoCertsImportedNoCertsFound_Test::TestBody() in pk11_find_certs_unittest.o
      nss_test::PK11FindRawCertsBySubjectTest_TestOneCertImportedNoCertsFound_Test::TestBody() in pk11_find_certs_unittest.o
      nss_test::PK11FindRawCertsBySubjectTest_TestMultipleMatchingCertsFound_Test::TestBody() in pk11_find_certs_unittest.o
      nss_test::PK11FindRawCertsBySubjectTest_TestNoCertsOnInternalSlots_Test::TestBody() in pk11_find_certs_unittest.o
      nss_test::PK11FindRawCertsBySubjectTest_TestFindEmptySubject_Test::TestBody() in pk11_find_certs_unittest.o
      nss_test::PK11FindRawCertsBySubjectTest_TestSearchForNullSubject_Test::TestBody() in pk11_find_certs_unittest.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Darwin18.0.0_cc_64_DBG.OBJ/pk11_gtest] Error 1
make[1]: *** [libs] Error 2
make: *** [libs] Error 2

3.2.2、brew安裝

$ brew install nss
#設置環境變量
export LDFLAGS="-L/usr/local/opt/nss/lib"
export CPPFLAGS="-I/usr/local/opt/nss/include/nss"

3.2.3、復制.h文件

#還是進入到上面由nss-3.44-with-nspr-4.21.tar.gz解壓出來的那個nss-3.44目錄中
cd nss-3.44
cp -r dist/Darwin18.0.0_cc_64_OPT.OBJ/include/* /usr/local/opt/nss/include/nss/

上面的Darwin18.0.0_cc_64_OPT.OBJ這一級目錄名與自己的操作系統版本有關,之所以要進行復制這個操作,是因為brew安裝的nss會缺少很多.h文件,導致編譯curl過程中引用NSS會失敗,原因暫時未知。

3.3、安裝curl

tar -zxf curl-7.65.1.tar.gz
cd curl-7.65.1
./configure --prefix=/usr/local/opt/curl --without-ssl --with-nss=/usr/local/opt/nss/
make && make install

3.4、驗證

curl-V

四、關於根證書庫的問題

上面基於NSS源碼編譯生成的curl沒有附帶ca證書庫,在使用curl訪問https的url時,會卡在證書驗證的環節上,如下圖所示:

curl-https

有人可能會說,可以給curl配置CA證書庫,但是由於一些原因,好像在mac平台上暫時無法實現,可以見下面的說明:

If libcurl was built with NSS support, then depending on the OS distribution, it is probably required to take some additional steps to use the system-wide CA cert db. RedHat ships with an additional module, libnsspem.so, which enables NSS to read the OpenSSL PEM CA bundle. On openSUSE you can install p11-kit-nss-trust which makes NSS use the system wide CA certificate store. NSS also has a new database format

Starting with version 7.19.7, libcurl automatically adds the 'sql:' prefix to the certdb directory (either the hardcoded default /etc/pki/nssdb or the directory configured with SSL_DIR environment variable). To check which certdb format your distribution provides, examine the default certdb location: /etc/pki/nssdb; the new certdb format can be identified by the filenames cert9.db, key4.db, pkcs11.txt; filenames of older versions are cert8.db, key3.db, secmod.db.

摘自:https://curl.haxx.se/docs/sslcerts.html ,大致意思就是redhat系列的操作系統會有一個額外的模塊libnsspem.so(/usr/lib64/libnsspem.so),可以使NSS能夠使用系統級的CA證書庫,openSUSE操作系統也可以通過安裝p11-kit-nss-trust來實現,mac系統中好像沒有相應的支持,上面的截圖中有一個錯誤警告"WARNING: failed to load NSS PEM library libnsspem.so. Using OpenSSL PEM certificates will not work",curl好像默認是去嘗試加載libnsspem.so這個庫文件。

因此在使用這個curl時,需要加上-k參數來跳過證書驗證的環節,也因此,我也僅僅只是用這個curl工具做一些特定的測試。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM