module R509::ASN1

Module for holding various classes related to parsed ASN.1 objects

Public Class Methods

general_name_parser(names) click to toggle source

@param [Array,R509::ASN1::GeneralNames] names An array of strings. Can be dNSName, iPAddress, URI, or rfc822Name.

You can also supply a directoryName, but this must be an R509::Subject or array of arrays

@return [R509::ASN1::GeneralNames]

# File lib/r509/asn1.rb, line 26
def self.general_name_parser(names)
  if names.nil? || names.is_a?(R509::ASN1::GeneralNames)
    return names
  elsif !names.is_a?(Array)
    raise ArgumentError, "You must supply an array or existing R509::ASN1 GeneralNames object to general_name_parser"
  end
  general_names = R509::ASN1::GeneralNames.new
  names.uniq!
  names.map do |domain|
    if self.ip_check(domain)
      ip = IPAddr.new(domain.strip)
      general_names.create_item(:tag => 7, :value => ip.to_s)
    else
      case domain
      when R509::Subject, Array
        subject = R509::Subject.new(domain)
        general_names.create_item(:tag => 4, :value => subject)
      when /:\/\// # URI
        general_names.create_item(:tag => 6, :value => domain.strip)
      when /@/ # rfc822Name
        general_names.create_item(:tag => 1, :value => domain.strip)
      else # dNSName
        general_names.create_item(:tag => 2, :value => domain.strip)
      end
    end
  end
  general_names
end
get_extension_payload(ext) click to toggle source

parses the ASN.1 payload and gets the extension data out for further processing by the subclasses

# File lib/r509/asn1.rb, line 9
def self.get_extension_payload(ext)
  asn = OpenSSL::ASN1.decode ext
  # Our extension object. Here's the structure:
  #   Extension  ::=  SEQUENCE  {
  #        extnID      OBJECT IDENTIFIER,
  #        critical    BOOLEAN DEFAULT FALSE,
  #        extnValue   OCTET STRING
  #                    -- contains the DER encoding of an ASN.1 value
  #                    -- corresponding to the extension type identified
  #                    -- by extnID
  #        }
  OpenSSL::ASN1.decode(asn.entries.last.value).value
end
ip_check(domain) click to toggle source

Checks if a given string is an IP or not.

@param [String] domain The string to check

@return [Boolean]

# File lib/r509/asn1.rb, line 60
def self.ip_check(domain)
  begin
    IPAddr.new(domain.strip)
    true
  rescue
    false
  end
end