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

- (Array) sanitize(name)

Array of the form [["OID", "VALUE], ["OID", "VALUE"]] with "UNDEF" replaced by the actual OID

Parameters:

  • name (Hash)

    a customizable set of options

Options Hash (name):

  • (OpenSSL::X509::Name)

Returns:

  • (Array)

    array of the form [["OID", "VALUE], ["OID", "VALUE"]] with "UNDEF" replaced by the actual OID



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/r509/subject.rb', line 183

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| not 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