Class: R509::Subject

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

Overview

subject class. Used for building OpenSSL::X509::Name objects in a sane fashion

Examples:

subject = R509::Subject.new
subject.CN= "test.test"
subject.organization= "r509 LLC"
subject = R509::Subject.new([['CN','test.test'],['O','r509 LLC']])
# you can also use the friendly getter/setters with custom OIDs
R509::OIDMapper.register("1.2.3.4.5.6.7.8","COI","customOID")
subject = R509::Subject.new
subject.COI="test"
# or
subject.customOID="test"
# or
subject.custom_oid="test"

Instance Method Summary (collapse)

Constructor Details

- (Subject) initialize(arg = nil)

A new instance of Subject

Parameters:

  • arg (Array, OpenSSL::X509::Name, R509::Subject, DER, nil) (defaults to: nil)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/r509/subject.rb', line 22

def initialize(arg=nil)
  if arg.kind_of?(Array)
    @array = arg
  elsif arg.kind_of?(OpenSSL::X509::Name)
    sanitizer = R509::NameSanitizer.new
    @array = sanitizer.sanitize(arg)
  elsif arg.kind_of?(R509::Subject)
    @array = arg.to_a
  else
    @array = []
    if not (begin OpenSSL::ASN1.decode(arg) rescue nil end).nil?
      parse_asn1(arg)
    end
  end

  # see if X509 thinks this is okay
  name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_sym, *args, &block) (private)

Try to build methods for getting/setting various subject attributes dynamically. this will also cache methods that get built via instance_eval. This code will also allow you to set subject items for custom oids defined via R509::OIDMapper



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/r509/subject.rb', line 118

def method_missing(method_sym, *args, &block)
  if method_sym.to_s =~ /(.*)=$/
    sn = oid_check($1)
    if not sn.nil?
      define_dynamic_setter(method_sym,sn)
      send(method_sym, args.first)
    else
      return super(method_sym, *args, &block)
    end
  else
    sn = oid_check(method_sym)
    if not sn.nil?
      define_dynamic_getter(method_sym,sn)
      send(method_sym)
    else
      return super(method_sym, *args, &block)
    end
  end
end

Instance Method Details

- (Object) [](key)

get value for key



52
53
54
55
56
57
58
59
# File 'lib/r509/subject.rb', line 52

def [](key)
  @array.each do |item|
    if key == item[0]
      return item[1]
    end
  end
  return nil
end

- (Object) []=(key, value)

set key and value



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/r509/subject.rb', line 62

def []=(key, value)
  added = false
  @array = @array.map{ |item|
    if key == item[0]
      added = true
      [key, value]
    else
      item
    end
  }

  if not added
    @array << [key, value]
  end

  # see if X509 thinks this is okay
  name

  @array
end

- (Object) delete(key)

Parameters:

  • key (String)

    item you want deleted



84
85
86
87
88
# File 'lib/r509/subject.rb', line 84

def delete(key)
  @array = @array.select do |item|
    item[0] != key
  end
end

- (Boolean) empty?

Returns:

  • (Boolean)


47
48
49
# File 'lib/r509/subject.rb', line 47

def empty?
  @array.empty?
end

- (OpenSSL::X509::Name) name

Returns:

  • (OpenSSL::X509::Name)


42
43
44
# File 'lib/r509/subject.rb', line 42

def name
  OpenSSL::X509::Name.new(@array)
end

- (Array) to_a

Array of form [['CN','langui.sh']]

Returns:

  • (Array)

    Array of form [['CN','langui.sh']]



96
97
98
# File 'lib/r509/subject.rb', line 96

def to_a
  @array
end

- (String) to_s

String of form /CN=something.com/O=whatever/L=Locality

Returns:

  • (String)

    string of form /CN=something.com/O=whatever/L=Locality



91
92
93
# File 'lib/r509/subject.rb', line 91

def to_s
  name.to_s
end