4

在 Linux 信任自訂 CA 根憑證

 3 years ago
source link: https://blog.darkthread.net/blog/install-caroot-cert-in-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在 Linux 信任自訂 CA 根憑證

calendar.svg 2021-04-28 09:24 PM comment.svg 0 eye.svg 889

既然講了在 Java 信任自訂 CA 根憑證,就連在 Linux 怎麼做也一起說說。

需要信任自訂 CA 根憑證的場合,除了自行架設的內部網站的 SSL 憑證由自己的 CA 簽發(延伸閱讀:使用 OpenSSL 製作萬用字元 SSL 憑證),還有一種狀況是網站的 SSL 憑證被網管設備置換,而置換憑證的 CA 根憑證未被 Linux 信任,像是 docker 要下載 Image 就會因 x509: certificate signed by unknown authority 失敗。

前篇文章介紹過用 openssl s_client -showcerts 匯出憑證清單再手工擷取的做法,這次分享網友自動解析分檔的腳本:參考

openssl s_client -showcerts -verify 5 -connect www.xxx.yyy:443 < /dev/null | awk '/BEGIN/,/END/{ if(/BEGIN/){a++}; out="cert"a".crt"; print >out}' && for cert in .crt; do newname=$(openssl x509 -noout -subject -in $cert | sed -n 's/^.CN=(.)$/\1/; s/[ ,.]//g; s/__//g; s/^_//g;p').pem; mv $cert $newname; done

執行後網站憑證鏈上的憑證會各自存成如下檔名:

```'subject=C_=TW_ST=TAIWAN_L=TAIPEI_O=ORGNAME_CN=xxx_yyy_com_emailAddress=_someone@xxx_yyy_com.crt'``

感覺程序跟檔名複雜了點,若不需要全自動化,用 openssl s_client -showcerts 加手工擷取另存檔案似乎簡單些。

科普一下,憑證檔有分 DER(二進位格式) 跟 PEM(純文字格式,以 BEGIN CERTIFICATE、END CERTIFICATE 包夾),副檔名有 .der、.cer、.crt、.pem 等等,PEM 格式可用文字編譯器處理,比較方便,Windows 慣用 .cer,Linux 則以 .crt 為主。延伸閱讀:認識 PKI 架構下的數位憑證格式與憑證格式轉換的心得分享 by 保哥

拿到 CA 根憑證的 .crt 檔,再來只需將它複製到 Linux 的 CA 憑證目錄,再執行 update-ca-certificates 就行了。CA 憑證目錄在 Debian 與 Ubuntu 的位置是 /usr/local/share/ca-certificates/extra, 在 CentOS、RedHat 則是 /etc/pki/ca-trust/source/anchors/:參考:Adding trusted root certificates to the server

裝好憑證,docker 順利下載,收工。

and has 0 comments

Comments

Be the first to post a comment

Post a comment

Comment
Name Captcha 42 + 0 =

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK