英文:
How can I parse the certificate information output from the security command in Mac?
问题
I need to retrieve the attributes of a certificate that is stored in the keychain on my Mac from the command line. I can collect them manually from the Keychain Access app, but I want to do that with a script.
I used the security
command to get a certificate and "grep" to inspect the "subject" section:
security find-certificate -c "Apple Development" login.keychain | grep "subj"
and then got the following output (some omitted by "...").
"subj"<blob>=0x3081943...553 "014120...02US"
In the output above, what format is the data following "subj"
英文:
I need to retrieve the attributes of a certificate that is stored in the keychain on my Mac from the command line. I can collect them manually from the Keychain Access app, but I want to do that with a script.
I used the security
command to get a certificate and "grep" to inspect the "subject" section:
security find-certificate -c "Apple Development" login.keychain | grep "subj"
and then got the following output (some omitted by "...").
"subj"<blob>=0x3081943...553 "014120...02US"
In the output above, what format is the data following "subj"<blob>=
and how can I parse it? I found that decoding the first half of the hexadecimal sequence(0x30...) with UTF-8 yields the second half of the string (0\201...), but I don't know what 0\201\2241\...
means. I have tried other character codes, but they just give me garbled characters.
答案1
得分: 1
关于格式,证书以DER/PEM格式存储,这是ASN.1编码数据的表示方式。在输出中看到的是ASN.1二进制数据的十六进制表示。"blob" 表示该值或属性以二进制数据形式存储。
关于导出(用于证书),我强烈建议将 security
与 openssl
结合使用,如下所示:
security find certificate -p -c "Apple Development" login.keychain | openssl x509 -noout -subject
-p
选项在 security 命令中以PEM格式导出找到的证书,这是openssl可以使用的格式。然后,您可以将PEM数据传输到 openssl
命令,其中可以使用 -subject
选项轻松提取主题信息。
您可以查看 security 的手册页面 和 openssl x509 的手册页面。
英文:
As for the format, the certificates are stored in DER/PEM format, which is a representation of ASN.1 encoded data. What you see in the output is the hexadecimal representation of the ASN.1 binary data. The blob indicates that the value or attribute is stored as binary data.
As for exporting (for certificates), I would highly recommend combining security
with openssl
as follows:
security find certificate -p -c "Apple Development" login.keychain | openssl x509 -noout -subject
The -p
option in the security command exports the found certificate in PEM format, which is something openssl can use. You can then pipe the PEM data into the openssl
command, where one can easily extract the subject using the -subject
option.
You can check out both the man page of security and the man page of openssl x509.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论