class R509::Subject::NameSanitizer

Sanitize an X509::Name. The to_a method replaces unknown OIDs with “UNDEF”, but the to_s method doesn't. What we want to do is build the array that would have been produced by to_a if it didn't throw away the OID. This method is not required as of ruby-1.9.3p125 and up.

Public Instance Methods

sanitize(name) click to toggle source

@option name [OpenSSL::X509::Name] @return [Array] array of the form [[“OID”, “VALUE], [”OID“, ”VALUE“]] with ”UNDEF“ replaced by the actual OID

# File lib/r509/subject.rb, line 205
def sanitize(name)
  line = name.to_s
  array = name.to_a.dup
  used_oids = []
  undefined_components(array).each do |component|
    begin
      # get the OID from the subject line that has this value
      oids = line.scan(/\/([\d\.]+)=#{component[:value]}/).flatten
      if oids.size == 1
        oid = oids.first
      else
        oid = oids.select { |match| !used_oids.include?(match) }.first
      end
      # replace the "UNDEF" OID name in the array at the index the UNDEF was found
      array[component[:index]][0] = oid
      # remove the first occurrence of this in the subject line (so we can handle the same oid/value pair multiple times)
      line = line.sub("/#{oid}=#{component[:value]}", "")
      # we record which OIDs we've used in case two different unknown OIDs have the same value
      used_oids << oid
    rescue
      # I don't expect this to happen, but if it does we'll just not replace UNDEF and continue
    end
  end
  array
end

Private Instance Methods

undefined_components(array) click to toggle source

get the components from to_a that are UNDEF @option array [Array<OpenSSL::X509::Name>] @return [Hash] @example

Return value looks like
{ :index => the index in the original array where we found an UNDEF, :value => the subject component value }
# File lib/r509/subject.rb, line 239
def undefined_components(array)
  components = []
  array.each_index do |index|
    components << { :index => index, :value => array[index][1] } if array[index][0] == "UNDEF"
  end
  components
end