libcurl, openssl, and ca-bundle - on Windows
整理一下在 Windows 上,應付 libcurl、openssl 與 ca-bundle 的筆記。
我們需要讓使用的 libcurl 支援 SSL,目標是要讓後續的開發,只在 IDE 進行即可,必須要做到從 repository 裡 checkout 出來,只要打開 IDE 就可以做完所有的事。
原本 curl 的 source tarball 裡,有包含了 lib/curllib.dsp 這個 VC6 的 project 檔,但很可惜的,這是給 newbie 用的,所以不包含 3rd-party 組件,如 OpenSSL。見 docs/INSTALL 檔:
Intentionally, these reference VC++ 6.0 projects and configurations don't use third party libraries, such as OpenSSL or Zlib, to allow proper compilation and configuration for all new users without further requirements.
但是我們已經使用這個 curllib.dsp 建立好了 for IDE 的開發環境,所以必須想辦法,把「老鳥的手段」放進「菜鳥的籃子」裡。
為了少編譯一個 OpenSSL 套件[1],我選擇使用 GnuWin32 預先編譯好的 native openssl package,抓下安裝程式,安裝到預設的目錄即可。
OpenSSL 共有兩個 DLL 檔:libeay32.dll 與 libssl32.dll。可是,GnuWin32 這個預先編譯好的套件,雖然說包含了 development files,但沒有包含 VC6 用的 DLL import library 或 def 檔,所以必須自己生出來。
一開始我先試著從 GnuWin32 版 OpenSSL 的 source tarball (openssl-0.9.8h-1-src.zip),解出裡面的 libeay32.def 與 ssleay32.def,以製作對應的 import libraries:
SHELL> RENAME ssleay32.def libssl32.def SHELL> "C:\Program Files\Microsoft VisualStudio\VC98\Bin\VCVARS32.BAT" SHELL> LIB /DEF:libeay32.def SHELL> LIB /DEF:libssl32.def
其中,ssleay32.def 其實是 libssl32.dll 對應的 DEF 檔,故先正名之。然後引入 VC6 開發環境,呼叫 LIB.exe 工具,從 DEF 檔做出 import libraries。
測試結果發現,程式一啟動,就跳出視窗抱怨,找不到 libeay32.dll 序號 968 的 API。用 depends.exe 查,ordinal 968 這個 API 根本就是 N/A,從 libeay32.def 裡也找不到編號 968。
所以只好自己來,利用 DUMPBIN.exe 工具,直接從 DLL 裡列出 exported symbols,如下:
SHELL> DUMPBIN /EXPORTS libeay32.dll > libeay32.def SHELL> DUMPBIN /EXPORTS libssl32.dl
然後編輯這兩個 .DEF 檔,把無關的東西去掉,僅剩下 exported symbols,弄成下面這個樣子:
EXPORTS
symbol1
symbol2
...
從第二行開始,使用 TAB 字元縮排。
然後重複上面的程序,利用 LIB.exe 工具,製作出 import libraries,這次就可以正常使用了。把產生的 .def、.lib 與 .exp 檔,放到 %ProgramFiles%\GnuWin32\lib 目錄下,把兩個 DLL,放到 %ProgramFiles%\GnuWin32\bin 目錄下,然後如下修改 curllib.dsp 的 project setting,即可正常編譯出具備 SSL 功能的 libcurl:
- Add preprocessor definition:
USE_SSLEAY; - Add include directories:
%ProgramFiles%\GnuWin32\include與%ProgramFiles%\GnuWin32\include\openssl; - Add additional library path:
%ProgramFiles%\GnuWin32\lib; - Add additional libraries:
libeay32.lib與libssl32.lib。
不過,要正常使用 SSL 功能,還是必須想辦法搞到 CA 給 curl 用才行。目前最新的 curl 版本,已經不再內附 CA certifications 了,所以我們要自己生才行。查 curl 文件,我們需要 PEM 格式的 CA。本來想說直接開 IE 或 firefox 把裡面用的 CA 匯出,可是發現沒辦法匯出成 PEM 格式。還好翻查 FreeBSD 裡灌的 curl,在 /usr/local/share/curl 目錄下,可以找到 curl-ca-bundle.crt 檔[2],將之如下傳給 CURLOPT_CAINFO 選項即可:
curl_easy_setopt(handle, CURLOPT_CAINFO, "/path/to/curl-ca-bundle.crt");
至此,大功告成。
參考資料:



One Comment
very useful info. I am trying it now. Really hope can get the curl and ssl working...I have already spent nearly few days on this...
Post a Comment