spec/acceptance/helpers/pkiutils.rb
# Extracts the certificate for a specified subject from a PKCS #7
# PEM-formatted file and writes it to a PEM-formatted output file.
#
# @fails if a certificat for the specified subject does not exist
#        in the PKCS #7 input file
#
# +host+:          Host object for the server where the input file
#                  resides
# +pkcs7_file+:    PCKS #7 input file derived from a CMC response
#                  file. It contains the CA cert chain for the cert
#                  having the specified subject.
# +cert_file+:     Output certificate file
# +cert_subject+:  Certificate subject, excluding the 'cn=' or 'CN='
Extracts the certificate for a specified subject from a PKCS #7
PEM-formatted file and writes it to a PEM-formatted output file.
Extracts the certificate for a specified subject from a PKCS #7 PEM - formatted file and writes it to a PEM - formatted output file .
def extract_cert_from_pkcs7(host, pkcs7_file, cert_file, cert_subject)
        cert_chain_file = "#{pkcs7_file}.pem"
        on(host, "openssl pkcs7 -print_certs -in #{pkcs7_file} -out #{cert_chain_file}")
        cert_chains_raw = on(host, "cat #{cert_chain_file}").stdout
        cert_chains = []
        cert_lines = []
        cert_found = false
        cert_chains_raw.split("\n").each do |line|
          next if line.strip.empty?
          if line.match (/^subject/)
            cert_found = true
            cert_lines = []
          elsif line.match(/END CERTIFICATE/)
            cert_found = false
            cert_lines << line
            cert_chains << cert_lines.join("\n")
          end
          cert_lines << line if cert_found
        end
        cert_content = nil
        cert_chains.each do |cert|
          unless cert.match(/^subject.*CA Signing Certificate$/)
            if cert.match(/CN\s*=\s*#{cert_subject}/)
              cert_content = cert.gsub(/^.*BEGIN /m,'-----BEGIN ')
            end
          end
        end
        if cert_content
          create_remote_file(host, cert_file, cert_content)
        else
          on(host, "openssl pkcs7 -print_certs -in #{pkcs7_file}")
          fail("Certificate for '#{cert_subject}' does not exist in #{pkcs7_file} on #{host}")
        end
      end
def extract_cert_from_pkcs7 ( host , pkcs7_file , cert_file , cert_subject ) cert_chain_file = "#{pkcs7_file}.pem" on ( host , "openssl pkcs7 -print_certs -in #{pkcs7_file} -out #{cert_chain_file}" ) cert_chains_raw = on ( host , "cat #{cert_chain_file}" ) . stdout cert_chains = [ ] cert_lines = [ ] cert_found = false cert_chains_raw . split ( "\n" ) . each do | line | next if line . strip . empty? if line . match ( / ^subject / ) cert_found = true cert_lines = [ ] elsif line . match ( / END CERTIFICATE / ) cert_found = false cert_lines << line cert_chains << cert_lines . join ( "\n" ) end cert_lines << line if cert_found end cert_content = nil cert_chains . each do | cert | unless cert . match ( / ^subject.*CA Signing Certificate$ / ) if cert . match ( / CN \s *= \s * #{ cert_subject } / ) cert_content = cert . gsub ( / ^.*BEGIN  /m , '-----BEGIN ' ) end end end if cert_content create_remote_file ( host , cert_file , cert_content ) else on ( host , "openssl pkcs7 -print_certs -in #{pkcs7_file}" ) fail ( "Certificate for '#{cert_subject}' does not exist in #{pkcs7_file} on #{host}" ) end end
Extracts the certificate for a specified subject from a PKCS #7
PEM-formatted file and writes it to a PEM-formatted output file.
Extracts the certificate for a specified subject from a PKCS #7 PEM - formatted file and writes it to a PEM - formatted output file .
# Extract certs in the cert chain from PCKS #7 # To aid debug, print out certs from the PKCS #7 file
host
<not_specific>
pkcs7_file
<not_specific>
cert_file
<not_specific>
cert_subject
<not_specific>
fails
if a certificat for the specified subject does not exist in the PKCS #7 input file +host+: Host object for the server where the input file resides +pkcs7_file+: PCKS #7 input file derived from a CMC response file. It contains the CA cert chain for the cert having the specified subject. +cert_file+: Output certificate file +cert_subject+: Certificate subject, excluding the 'cn=' or 'CN='
if a certificat for the specified subject does not exist in the PKCS #7 input file + host + : Host object for the server where the input file resides + pkcs7_file + : PCKS #7 input file derived from a CMC response file . It contains the CA cert chain for the cert having the specified subject . + cert_file + : Output certificate file + cert_subject + : Certificate subject excluding the ' cn = ' or ' CN = '