从 SAML 响应中,我需要从响应的
<ds:X509Certificate>
属性中找到证书的指纹。
类似于我从 https://www.samltool.com/fingerprint.php 获取它的方式,但是从 CLI 获取。
我发现有一种方法可以从像 https://www.jvt.me/posts/2019/04/03/openssl-fingerprint-x509-pem/ 这样的证书文件中获取它,但不能从响应中获取。
假设您在
$SAMLRESPONSE
中有 SAMLResponse(base64 编码):
echo "$SAMLRESPONSE" | base64 -D | tidy -xml -indent > ~/tmp/samlresponse.xml
cat ~/tmp/samlresponse.xml
xpath -q -e '//Assertion//Signature//KeyInfo//X509Certificate/text()' ~/tmp/samlresponse.xml|sed -r '/^\s*$/d'|awk '{printf "-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n", $1}' > ~/tmp/cert.pem
/usr/local/opt/openssl/bin/openssl x509 -in ~/tmp/cert.pem -noout -fingerprint
会产生像
这样的输出SHA1 Fingerprint=9B:47:78:9B:2B:1C:90:09:F7:85:85:1A:18:4E:A0:26:EC:FA:1D:4E
上面的脚本
base64 -D
将 SAMLResponse 解码为 XMLtidy -xml -indent
将XML格式化为可读形式(用于视觉检查)并将其存储在~/tmp/samlresponse.xml
xpath
提取X509证书,sed
删除空行awk
用 -----BEGIN CERTIFICATE-----
和 -----END CERTIFICATE-----
包装它,使其符合 PEM