Module: R509::ASN1

Defined in:
lib/r509/asn1.rb

Overview

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

Defined Under Namespace

Classes: GeneralName, GeneralNames

Class Method Summary collapse

Class Method Details

.general_name_parser(names) ⇒ R509::ASN1::GeneralNames

Parameters:

  • names (Array, R509::ASN1::GeneralNames)

    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

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object

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



9
10
11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Boolean

Checks if a given string is an IP or not.

Parameters:

  • domain (String)

    The string to check

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/r509/asn1.rb', line 60

def self.ip_check(domain)
  begin
    IPAddr.new(domain.strip)
    true
  rescue
    false
  end
end