Openssl 如何读取X509 V3证书的密钥用法?

Openssl 如何读取X509 V3证书的密钥用法?,openssl,x509,digital-certificate,Openssl,X509,Digital Certificate,我想读取证书中的密钥使用字段。openssl中是否有可用的API?您可以尝试在openssl中使用以下命令 openssl x509 -in <certificate to check> -purpose -noout -text opensslx509-in-purpose-noout-text 这将打印出证书用途和证书本身的列表。您需要在结果文件中找到的以前的解决方案/输出字符串“Key Usage”。 我得到了以下解决方案,它将字符串精确地放入密钥使用X509证书中 ope

我想读取证书中的密钥使用字段。openssl中是否有可用的API?

您可以尝试在openssl中使用以下命令

openssl x509 -in <certificate to check> -purpose -noout -text
opensslx509-in-purpose-noout-text

这将打印出证书用途和证书本身的列表。

您需要在结果文件中找到的以前的解决方案/输出字符串“Key Usage”。 我得到了以下解决方案,它将字符串精确地放入密钥使用X509证书中

openssl s_client -showcerts -connect SERVER_HERE:443 </dev/null 2>/dev/null|openssl x509 -text |grep v "$(grep -E -A1 "Key Usage")"
openssl s_客户端-showcerts-connect服务器_此处:443/dev/null | openssl x509-text | grep v“$(grep-E-A1“密钥使用”)”
上面的命令获取证书,解析为文本,找到字符串“Key Usage”,并在结果上显示下一行,该行表示X509上此特定字段的值


//干杯

7年后…

较新版本的
openssl
允许您使用
-ext
标志查询证书扩展

打印密钥用法:


我需要在c代码中得到它。。谢谢你的评论。我会检查这条命令的源代码检查下面的链接你会得到答案
$> openssl x509 -noout -ext keyUsage < test.crt
X509v3 Key Usage: critical
    Digital Signature, Key Encipherment
$> openssl x509 -noout -ext extendedKeyUsage < test.crt
X509v3 Extended Key Usage: 
    TLS Web Server Authentication, TLS Web Client Authentication
$> openssl x509 -noout \
   -ext keyUsage,extendedKeyUsage < test.crt