macOS 2 種安裝 OpenSSL 的方法

本篇紀錄 macOS 下 2 種安裝 OpenSSL 的方法,第一種為使用 homebrew 安裝,第二種為下載 OpenSSL 原始碼編譯安裝。

我的使用環境為 macOS 10.13.4,Xcode 9.1。

  • 使用 homebrew 安裝 OpenSSL
  • 下載 OpenSSL 3 原始碼編譯與安裝 OpenSSL
  • OpenSSL 3 原始碼編譯失敗解決方法
  • 下載 OpenSSL 1.1.1 原始碼編譯與安裝 OpenSSL

使用 homebrew 安裝 OpenSSL

以下為 macOS homebrew 安裝 OpenSSL 的指令,

1
brew install openssl

或者指定 OpenSSL 安裝的版本,例如:指定安裝 OpenSSL 1.1.1,

1
brew install openssl@1.1.1o

或者指定安裝 OpenSSL 3,

1
brew install openssl@3

brew 安裝 OpenSSL 3 會編譯失敗的可以參考這篇,在 openssl@3.rb 的 configure_args 函式裡的 args 新增 no-asm 選項,然後再次安裝 brew install openssl@3

/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/openssl@3.rb
1
2
3
4
5
6
7
8
9
10
def configure_args
args = %W[
--prefix=#{prefix}
--openssldir=#{openssldir}
--libdir=#{lib}
no-ssl3
no-ssl3-method
no-zlib
no-asm
]

或者改裝 OpenSSL 1.1.1 的版本。

下載 OpenSSL 3 原始碼編譯與安裝 OpenSSL

這邊介紹 OpenSSL 3 的編譯安裝方式,使用下列指令下載 OpenSSL 3.0.3 原始碼並且解壓縮,

1
2
wget https://www.openssl.org/source/openssl-3.0.3.tar.gz
tar xvf openssl-3.0.3.tar.gz

接下來進行編譯與安裝 OpenSSL,zlib 選項非必要,無壓縮需求可以不加,預設安裝在 /usr/local/ssl 目錄下,也可自行指定 --prefix 選項帶入目錄,如果不用 shared 的方式則 ./config no-shared 即可,

1
2
3
4
5
6
cd openssl-3.0.3
./config shared
#./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib # 指定路徑
make -j4
make test
make install

OpenSSL 3 原始碼編譯失敗解決方法

我的 macOS 10.13.4 會出現這樣的編譯錯誤訊息,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
crypto/bn/rsaz-3k-avx512.s:1283:12: error: register %ymm20 is only available with AVX512
vmovdqu64 %ymm20,320(%rdi)
^~~~~~
crypto/bn/rsaz-3k-avx512.s:1284:12: error: register %ymm21 is only available with AVX512
vmovdqu64 %ymm21,352(%rdi)
^~~~~~
crypto/bn/rsaz-3k-avx512.s:1285:12: error: register %ymm22 is only available with AVX512
vmovdqu64 %ymm22,384(%rdi)
^~~~~~
crypto/bn/rsaz-3k-avx512.s:1286:12: error: register %ymm23 is only available with AVX512
vmovdqu64 %ymm23,416(%rdi)
^~~~~~
crypto/bn/rsaz-3k-avx512.s:1287:12: error: register %ymm24 is only available with AVX512
vmovdqu64 %ymm24,448(%rdi)
^~~~~~
crypto/bn/rsaz-3k-avx512.s:1288:12: error: register %ymm25 is only available with AVX512
vmovdqu64 %ymm25,480(%rdi)
^~~~~~

檢查 Xcode 版本,我的是 Xcode 9.1,

1
2
3
$ xcodebuild -version
Xcode 9.1
Build version 9B55

這篇下面的網友提供了一個解決方法 workaround,
就是在 configure 時加入 no-asm 的選項,這樣就可以編譯過了,但是似乎這個方法就沒法得到好的效能,

1
2
./config shared no-asm
make -j4

使用 make test 進行測試,

1
make test

make install 安裝到系統之前,使用 openssl version 指令測試執行 openssl 是否成功,出現找不到 libssl.3.dylib 動態函式庫的錯誤訊息

1
2
3
4
5
$ ./apps/openssl version
dyld: Library not loaded: /usr/local/lib/libssl.3.dylib
Referenced from: /Users/sheng/Desktop/github/openssl/./apps/openssl
Reason: image not found
Abort trap: 6

設定 DYLD_LIBRARY_PATH 要載入動態函式庫的路徑,因為 libssl.3.dylib 在這個編譯的目錄下,所以就設定一下 libssl.3.dylib 目錄的路徑(相對路徑或絕對路徑都可),這樣就成功執行起來了,macOS 的 DYLD_LIBRARY_PATH 是對應到 linux 的 LD_LIBRARY_PATH,在 macOS 下使用 LD_LIBRARY_PATH 是無效的,

1
2
3
4
$ DYLD_LIBRARY_PATH="./" ./apps/openssl version
# or
$ DYLD_LIBRARY_PATH="/Users/shengyu/openssl-3.0.3" ./apps/openssl version
OpenSSL 3.1.0-dev (Library: OpenSSL 3.1.0-dev )

下載 OpenSSL 1.1.1 原始碼編譯與安裝 OpenSSL

這邊也順便介紹一下的 OpenSSL 1.1.1x 的編譯安裝方式,基本上跟 OpenSSL 3 沒什麼太大差異,使用下列指令下載 OpenSSL 1.1.1o 原始碼並且解壓縮,

1
2
wget https://www.openssl.org/source/openssl-1.1.1o.tar.gz
tar xvf openssl-1.1.1o.tar.gz

接下來進行編譯與安裝 OpenSSL,zlib 選項非必要,無壓縮需求可以不加,預設安裝在 /usr/local/ssl 目錄下,也可自行指定 --prefix 選項帶入目錄,如果不用 shared 的方式則 ./config no-shared 即可,

1
2
3
4
5
6
cd openssl-1.1.1o
./config shared
#./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib # 指定路徑
make -j4
make test
make install

OpenSSL 1.1.1o 我可以順利編譯過沒有遇到 OpenSSL 3 的 AVX512 的問題。

其他參考
OpenSSL master (and 3.0 branch) won’t build on MacOS 10.11 (El Capitan) · Issue #16670 · openssl/openssl
https://github.com/openssl/openssl/issues/16670

DYLD_LIBRARY_PATH 的相關討論
language agnostic - Is it OK to use DYLD_LIBRARY_PATH on Mac OS X? And, what’s the dynamic library search algorithm with it? - Stack Overflow
https://stackoverflow.com/questions/3146274/is-it-ok-to-use-dyld-library-path-on-mac-os-x-and-whats-the-dynamic-library-s
How do I configure the LD_LIBRARY_PATH on Linux and DYLD_LIBRARY_PATH on MAC OS X to point to MCR?
https://www.mathworks.com/matlabcentral/answers/473971-how-do-i-configure-the-ld_library_path-on-linux-and-dyld_library_path-on-mac-os-x-to-point-to-mcr

其它相關文章推薦
Ubuntu 2 種安裝 OpenSSL 的方法
OpenSSL AES encryption 對稱式加密指令用法與範例
C/C++ OpenSSL AES encryption/decryption 加密解密範例