如何从 CLI 中的 saml 响应的 509 证书属性中获取证书的指纹

问题描述 投票:0回答:1

从 SAML 响应中,我需要从响应的

<ds:X509Certificate>
属性中找到证书的指纹。

类似于我从 https://www.samltool.com/fingerprint.php 获取它的方式,但是从 CLI 获取。

我发现有一种方法可以从像 https://www.jvt.me/posts/2019/04/03/openssl-fingerprint-x509-pem/ 这样的证书文件中获取它,但不能从响应中获取。

macos openssl x509certificate saml-2.0
1个回答
0
投票

假设您在

$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 解码为 XML
  • 使用
    tidy -xml -indent
    将XML格式化为可读形式(用于视觉检查)并将其存储在
    ~/tmp/samlresponse.xml
  • 使用
    xpath
    提取X509证书,
  • 使用
    sed
    删除空行
  • 使用
    awk
    -----BEGIN CERTIFICATE-----
    -----END CERTIFICATE-----
    包装它,使其符合 PEM
  • 使用 openssl(但不是 macOS 附带的 openssl,它必须是与 Homebrew 一起安装的较新版本)加载 PEM 格式的 X509 并计算指纹
© www.soinside.com 2019 - 2024. All rights reserved.