Class: EaSSL::SigningRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/eassl/signing_request.rb

Overview

Author

Paul Nicholson ([email protected])

Co-Author

Adam Williams ([email protected])

Copyright

Copyright © 2006 WebPower Design

License

Distributes under the same terms as Ruby

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SigningRequest

Returns a new instance of SigningRequest.



11
12
13
14
15
16
17
18
19
# File 'lib/eassl/signing_request.rb', line 11

def initialize(options = {})
  @options = {
    :name       => {},                #required, CertificateName
    :key        => nil,               #required
    :digest     => OpenSSL::Digest::SHA512.new,
    :extensions => nil
  }.update(options)
  @options[:key] ||= Key.new(@options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

This method is used to intercept and pass-thru calls to openSSL methods and instance variables.



83
84
85
# File 'lib/eassl/signing_request.rb', line 83

def method_missing(method)
  ssl.send(method)
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



9
10
11
# File 'lib/eassl/signing_request.rb', line 9

def extensions
  @extensions
end

Class Method Details

.load(pem_file_path) ⇒ Object



87
88
89
# File 'lib/eassl/signing_request.rb', line 87

def self.load(pem_file_path)
  new.load(File.read(pem_file_path))
end

Instance Method Details

#keyObject



69
70
71
# File 'lib/eassl/signing_request.rb', line 69

def key
  @options[:key]
end

#load(pem_string) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/eassl/signing_request.rb', line 91

def load(pem_string)
  begin
    @ssl = OpenSSL::X509::Request.new(pem_string)
    @extensions = begin
      if attr = ssl.attributes.detect { |a| ['extReq','msExtReq'].include?(a.oid)}
        set = OpenSSL::ASN1.decode(attr.value)
        seq = set.value.first
        seq.value.collect { |e| OpenSSL::X509::Extension.new(e) }
      end
    end
  rescue
    raise "SigningRequestLoader: Error loading signing request"
  end
  self
end

#optionsObject



73
74
75
# File 'lib/eassl/signing_request.rb', line 73

def options
  @options
end

#sslObject



21
22
23
24
25
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/eassl/signing_request.rb', line 21

def ssl
  unless @ssl
    @ssl = OpenSSL::X509::Request.new
    @ssl.version = 0
    @ssl.subject = CertificateName.new(@options[:name].options).name
    @ssl.public_key = key.public_key
    
    @extensions = Array.new
    ef = OpenSSL::X509::ExtensionFactory.new
    
    case @options[:type]
    when 'subordinate'
      @extensions << ef.create_extension("basicConstraints","CA:TRUE")
    when 'server'
      @extensions << ef.create_extension("basicConstraints","CA:FALSE")
      @extensions << ef.create_extension("keyUsage", "digitalSignature,keyEncipherment")
      @extensions << ef.create_extension("extendedKeyUsage", "serverAuth")
    when 'client'
      @extensions << ef.create_extension("basicConstraints","CA:FALSE")
      @extensions << ef.create_extension("keyUsage", "nonRepudiation,digitalSignature,keyEncipherment")
      @extensions << ef.create_extension("extendedKeyUsage", "clientAuth,emailProtection")
    when 'peer'
      @extensions << ef.create_extension("basicConstraints","CA:FALSE")
      @extensions << ef.create_extension("keyUsage", "digitalSignature,keyEncipherment")
      @extensions << ef.create_extension("extendedKeyUsage", "serverAuth,clientAuth")
    when 'custom'
      @options[:extensions].each do |ext|
        @extensions << ef.create_extensions(ext[:name], ext[:value])
      end
    end
    
    if @options[:subject_alt_name]
      subjectAltName = @options[:subject_alt_name].map {|d| d.is_a?(Hash) ? "#{d[:name]}: #{d[:value]}" : "DNS: #{d}"  }.join(',')
      @extensions << ef.create_extension("subjectAltName", subjectAltName)
    end

    if @extensions.count > 0
      seq = OpenSSL::ASN1::Sequence.new(extensions)
      set = OpenSSL::ASN1::Set.new([seq])
      attr = OpenSSL::X509::Attribute.new('extReq', set)
      @ssl.add_attribute(attr)
    end

    @ssl.sign(key.private_key, @options[:digest])
  end
  @ssl
end

#to_pemObject



77
78
79
# File 'lib/eassl/signing_request.rb', line 77

def to_pem
  ssl.to_pem
end