Reputation: 151
I'm trying to write some ruby code to grab the Common Name (CN) value from a web server's SSL certificate but there doesn't seem to be any simple way to do this in ruby.
Upvotes: 2
Views: 1793
Reputation: 894
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.
You can read more in-depth on SSL certs at http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/X509/Certificate.html
Upvotes: 3