Some utility methods, currently just used by the examples.
Extracts all PEM encoded certs from a raw string and returns a list of X509 certificate objects in the order they appear in the file.
This can be helpful for loading a chain of certificates, eg for a server.
Usage:
chain = cert_chain(File.read("chain.pem")) p chain # => [#<OpenSSL::X509::Certificate subject=/C=US/CN=my.hostname.com, issuer=/C=US/CN=Trusted CA...>, #<OpenSSL::X509::Certificate subject=/C=US/CN=Trusted CA ... >]
# File lib/packetthief/util.rb, line 29 def cert_chain(raw) rawchain = split_chain(raw) rawchain.map { |rawcert| OpenSSL::X509::Certificate.new(rawcert) } end
Extracts all PEM encoded certificates out of a raw string and returns each raw PEM encoded certificate in an array.
# File lib/packetthief/util.rb, line 7 def split_chain(raw) chain = [] remaining = raw certpat = %r-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----/ while m = certpat.match(remaining) remaining = m.post_match chain << m[0].strip end chain end