简体   繁体   English

在ruby中获取Web服务器证书

[英]Getting a web server certificate in ruby

我正在尝试编写一些ruby代码来从Web服务器的SSL证书中获取Common Name(CN)值,但是在ruby中似乎没有任何简单的方法可以做到这一点。

Well, I would beg to differ. 好吧,我希望有所不同。 It's documented well enough but not too many examples are available, which makes it a bit, but not too complicated :) 它的文档已经足够好,但是没有太多示例可用,这使它有点儿,但也不太复杂:)

require 'openssl'

raw_cert = File.read (path_to_your_cert) # if your cert is in PEM or DER format

OR 要么

raw_cert = OpenSSL::PKCS12.new(File.read(path_to_your_cert), your_pwd) # If you want to read a .p12 cert

cert = OpenSSL::X509::Certificate.new(raw_cert)
cert.subject
=> **************/CN=<Your Common Name>/***************

So you can parse cert.subject to find out the common name you need. 因此,您可以解析cert.subject来查找所需的通用名称。

You can read more in-depth on SSL certs at http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/X509/Certificate.html 您可以在http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/X509/Certificate.html上详细了解SSL证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM