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.

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/r509/subject.rb', line 90

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