Class: R509::ASN1::GeneralName

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

Overview

This class parses ASN.1 GeneralName objects. At the moment it supports rfc822Name, dNSName, directoryName, uniformResourceIdentifier, and iPAddress

GeneralName ::= CHOICE {
     otherName                       [0]     OtherName,
     rfc822Name                      [1]     IA5String,
     dNSName                         [2]     IA5String,
     x400Address                     [3]     ORAddress,
     directoryName                   [4]     Name,
     ediPartyName                    [5]     EDIPartyName,
     uniformResourceIdentifier       [6]     IA5String,
     iPAddress                       [7]     OCTET STRING,
     registeredID                    [8]     OBJECT IDENTIFIER }

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (GeneralName) initialize(asn)

A new instance of GeneralName

Parameters:

  • asn (OpenSSL::ASN1::ASN1Data, Hash)

    ASN.1 input data. Can also pass a hash with :tag and :value keys



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/r509/asn1.rb', line 74

def initialize(asn)
  if asn.kind_of?(Hash) and asn.has_key?(:tag) and asn.has_key?(:value)
    # this is added via create_item
    @tag = asn[:tag]
    @type = R509::ASN1::GeneralName.map_tag_to_type(@tag)
    @serial_prefix = R509::ASN1::GeneralName.map_tag_to_serial_prefix(@tag)
    @value = asn[:value]
  else
    @tag = asn.tag
    @type = R509::ASN1::GeneralName.map_tag_to_type(@tag)
    @serial_prefix = R509::ASN1::GeneralName.map_tag_to_serial_prefix(@tag)
    value = asn.value
    case @tag
    when 1 then @value = value
    when 2 then @value = value
    when 4 then @value = R509::Subject.new(value.first.to_der)
    when 6 then @value = value
    when 7
      if value.size == 4 or value.size == 16
        ip = IPAddr.new_ntoh(value)
        @value = ip.to_s
      elsif value.size == 8 #IPv4 with netmask
        ip = IPAddr.new_ntoh(value[0,4])
        netmask = IPAddr.new_ntoh(value[4,4])
        @value = ip.to_s + "/" + netmask.to_s
      elsif value.size == 32 #IPv6 with netmask
        ip = IPAddr.new_ntoh(value[0,16])
        netmask = IPAddr.new_ntoh(value[16,16])
        @value = ip.to_s + "/" + netmask.to_s
      end
    end
  end
end

Instance Attribute Details

- (Object) serial_prefix (readonly)

The prefix OpenSSL needs for this type when encoding it into an extension.



67
68
69
# File 'lib/r509/asn1.rb', line 67

def serial_prefix
  @serial_prefix
end

- (Object) tag (readonly)

Integer tag type. See GeneralName description at the top of this class



71
72
73
# File 'lib/r509/asn1.rb', line 71

def tag
  @tag
end

- (Object) type (readonly)

The type, represented as a symbolized version of the GeneralName (e.g. :dNSName)



65
66
67
# File 'lib/r509/asn1.rb', line 65

def type
  @type
end

- (Object) value (readonly)

Value of the GeneralName



69
70
71
# File 'lib/r509/asn1.rb', line 69

def value
  @value
end

Class Method Details

+ (String) map_tag_to_serial_prefix(tag)

Serial prefix

Parameters:

  • tag (Integer)

Returns:

  • (String)

    serial prefix



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/r509/asn1.rb', line 136

def self.map_tag_to_serial_prefix(tag)
  case tag
  when 1 then "email"
  when 2 then "DNS"
  when 4 then "dirName"
  when 6 then "URI"
  when 7 then "IP"
  else
    raise R509Error, "Unimplemented GeneralName tag: #{tag}. At this time R509 does not support GeneralName types other than rfc822Name, dNSName, uniformResourceIdentifier, iPAddress, and directoryName"
  end
end

+ (Symbol) map_tag_to_type(tag)

Symbol type

Parameters:

  • tag (Integer)

Returns:

  • (Symbol)

    symbol type



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/r509/asn1.rb', line 150

def self.map_tag_to_type(tag)
  case tag
  when 0 then :otherName
  when 1 then :rfc822Name
  when 2 then :dNSName
  when 3 then :x400Address
  when 4 then :directoryName
  when 5 then :ediPartyName
  when 6 then :uniformResourceIdentifier
  when 7 then :iPAddress
  when 8 then :registeredID
  else
    raise R509Error, "Invalid tag #{tag}"
  end
end

+ (Integer) map_type_to_tag(type)

Maps a GeneralName type to the integer tag representation

Parameters:

  • type (String, Symbol)

    of GeneralName

Returns:

  • (Integer)

    tag for the type



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/r509/asn1.rb', line 111

def self.map_type_to_tag(type)
  #        otherName                       [0]     OtherName,
  #        rfc822Name                      [1]     IA5String,
  #        dNSName                         [2]     IA5String,
  #        x400Address                     [3]     ORAddress,
  #        directoryName                   [4]     Name,
  #        ediPartyName                    [5]     EDIPartyName,
  #        uniformResourceIdentifier       [6]     IA5String,
  #        iPAddress                       [7]     OCTET STRING,
  #        registeredID                    [8]     OBJECT IDENTIFIER }
  case type
  when "otherName", :otherName then 0
  when "rfc822Name", :rfc822Name, "email" then 1
  when "dNSName", :dNSName, "DNS" then 2
  when "x400Address", :x400Address then 3
  when "directoryName", :directoryName, "dirName" then 4
  when "ediPartyName", :ediPartyName  then 5
  when "uniformResourceIdentifier", :uniformResourceIdentifier, "URI" then 6
  when "iPAddress", :iPAddress, "IP" then 7
  when "registeredID", :registeredID  then 8
  end
end

Instance Method Details

- (Hash) serialize_name

Used to serialize GeneralName objects when issuing new certificates inside R509::CertificateAuthority::Signer

Returns:

  • (Hash)

    conf section and name serialized for OpenSSL extension creation



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/r509/asn1.rb', line 182

def serialize_name
  if self.type == :directoryName
    conf_name = OpenSSL::Random.random_bytes(16).unpack("H*")[0]
    conf = []
    conf << "[#{conf_name}]"
    @value.to_a.each do |el|
      conf << "#{el[0]}=#{el[1]}"
    end
    conf = conf.join("\n")
    extension_string = self.serial_prefix + ":" + conf_name
  else
    conf = nil
    extension_string = self.serial_prefix + ":" + self.value
  end
  {
    :conf => conf,
    :extension_string => extension_string
  }
end