如何解析从Mac上的security命令输出的证书信息?

huangapple go评论44阅读模式
英文:

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"= 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.

英文:

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.

如何解析从Mac上的security命令输出的证书信息?

I used the security command to get a certificate and "grep" to inspect the "subject" section:

security find-certificate -c &quot;Apple Development&quot; login.keychain | grep &quot;subj&quot;

and then got the following output (some omitted by "...").

&quot;subj&quot;&lt;blob&gt;=0x3081943...553  &quot;014120...02US&quot;

In the output above, what format is the data following &quot;subj&quot;&lt;blob&gt;= 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" 表示该值或属性以二进制数据形式存储。

关于导出(用于证书),我强烈建议将 securityopenssl 结合使用,如下所示:

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 &quot;Apple Development&quot; 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.

huangapple
  • 本文由 发表于 2023年6月1日 12:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定