本文介紹如何在 Windows 和 Linux 系統上比對根憑證,包含兩個系統的主要差異、比較方法,以及如何使用原生工具進行憑證管理。
主要差異
Windows 和 Linux 的根憑證通常不完全相同,因為:
1. 儲存位置不同
- Windows: 儲存在 Windows Certificate Store (可用
certmgr.msc 查看)
- Linux: 通常在
/etc/ssl/certs/ 或 /etc/pki/tls/certs/
2. 憑證來源不同
- Windows: 由 Microsoft 維護和更新
- Linux: 依發行版不同,通常使用
ca-certificates 套件
比較方法
1. 匯出並比較憑證指紋
Windows:
1
2
3
4
|
# 列出所有根憑證的指紋
Get-ChildItem -Path Cert:\LocalMachine\Root |
Select-Object Subject, Thumbprint |
Sort-Object Subject
|
Output:
1
2
3
4
5
6
7
|
Subject
-------
CN=AAA Certificate Services, O=Comodo CA Limited, L=Salford, S=Greater Manchester, C=GB
CN=Actalis Authentication Root CA, O=Actalis S.p.A./03358520967, L=Milan, C=IT
CN=AddTrust External CA Root, OU=AddTrust External TTP Network, O=AddTrust AB, C=SE
CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE
....
|
Linux:
1
2
3
4
|
# 列出所有根憑證的指紋
for cert in /etc/ssl/certs/*.pem; do
openssl x509 -in "$cert" -noout -subject 2>/dev/null
done | sort
|
Output:
1
2
3
4
5
6
|
subject=C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
subject=C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 1 G3
subject=C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 2
subject=C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 2 G3
subject=C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 3
subject=C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 3 G3
|
2. 檢查特定憑證
如果您想確認某個特定的 CA 憑證是否存在於兩個系統:
1
2
|
# 計算憑證的 SHA256 指紋
openssl x509 -in certificate.crt -noout -fingerprint -sha256
|
3. 匯出進行比對
您也可以將憑證匯出為標準格式後比較:
- Windows: 匯出為
.cer 或 .crt 格式
- Linux: 複製
.pem 檔案
然後用 openssl 比較憑證內容是否相同。
Windows 指紋格式
Windows 預設顯示的 Thumbprint 是 SHA-1 格式,看起來像:
1
|
A1B2C3D4E5F6789012345678901234567890ABCD
|
(40 個十六進位字元,沒有冒號分隔)
Linux openssl 預設格式
Linux 的 openssl x509 -fingerprint 預設也是 SHA-1,但格式不同:
1
|
SHA1 Fingerprint=A1:B2:C3:D4:E5:F6:78:90:12:34:56:78:90:12:34:56:78:90:AB:CD
|
(有冒號分隔)
如何比較指紋格式
如果要比較 Windows 的 SHA-1 thumbprint 和 Linux 的 openssl 輸出:
移除 Linux 的冒號:
1
2
|
# Linux 端
openssl x509 -in cert.pem -noout -fingerprint | sed 's/://g'
|
或者 Windows 加上冒號:
1
2
3
4
|
# Windows 端 - 將指紋加上冒號
$thumbprint = "A1B2C3D4E5F6789012345678901234567890ABCD"
$thumbprint -replace '(.{2})','$1:' -replace ':$',''
# 結果: A1:B2:C3:D4:E5:F6:78:90:12:34:56:78:90:12:34:56:78:90:AB:CD
|
Windows 原生方法 (不需 openssl)
Windows PowerShell 預設沒有 openssl,以下提供純 Windows 原生的方法:
1. 使用 PowerShell 查看根憑證
1
2
3
4
5
|
# 列出所有根憑證及其指紋
Get-ChildItem -Path Cert:\LocalMachine\Root |
Select-Object Subject, Thumbprint, NotAfter |
Sort-Object Subject |
Format-Table -AutoSize
|
2. 匯出特定憑證的詳細資訊
1
2
3
4
|
# 查看特定憑證的詳細資訊
Get-ChildItem -Path Cert:\LocalMachine\Root |
Where-Object {$_.Subject -like "*DigiCert*"} |
Format-List *
|
3. 匯出憑證進行比較
1
2
3
4
5
6
|
# 匯出所有根憑證到檔案
$certs = Get-ChildItem -Path Cert:\LocalMachine\Root
foreach ($cert in $certs) {
$filename = "C:\temp\certs\" + $cert.Thumbprint + ".cer"
Export-Certificate -Cert $cert -FilePath $filename
}
|
4. 比較兩個系統的憑證清單
1
2
3
4
5
|
# 匯出憑證清單到文字檔
Get-ChildItem -Path Cert:\LocalMachine\Root |
Select-Object Subject, Thumbprint |
Sort-Object Subject |
Out-File "C:\temp\windows_root_certs.txt"
|
相關連結