Class: R509::NameSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/r509/subject.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#sanitize(name) ⇒ Array

Returns array of the form [[“OID”, “VALUE], [”OID“, ”VALUE“]] with ”UNDEF“ replaced by the actual OID.

Options Hash (name):

  • (OpenSSL::X509::Name)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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