eassl2 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ gem 'simplecov', :require => false
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ multi_json (1.0.4)
10
+ rake (0.9.2.2)
11
+ rcov (0.9.11)
12
+ simplecov (0.5.4)
13
+ multi_json (~> 1.0.3)
14
+ simplecov-html (~> 0.5.3)
15
+ simplecov-html (0.5.3)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.0.0)
22
+ jeweler (~> 1.5.2)
23
+ rcov
24
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,57 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see COPYING.txt file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under this terms.
43
+
44
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
45
+ files under the ./missing directory. See each file for the copying
46
+ condition.
47
+
48
+ 5. The scripts and library files supplied as input to or produced as
49
+ output from the software do not automatically fall under the
50
+ copyright of the software, but belong to whomever generated them,
51
+ and may be sold commercially, and may be aggregated with this
52
+ software.
53
+
54
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
55
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
56
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57
+ PURPOSE.
data/README.txt ADDED
@@ -0,0 +1,10 @@
1
+ EaSSL is a library aimed at making openSSL certificate generation and
2
+ management easier and more ruby-ish.
3
+
4
+ Patch from https://github.com/openrain/eassl-fix applied onto source
5
+ of eassl-0.1.1643 from rubyforge, jeweler-ized, and switched from MD5
6
+ to SHA1 hash for CSR signing
7
+
8
+ Ruby license, inherited from the rubyforge project
9
+
10
+ This version, 2.0.0, is published as the "eassl2" gem.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "eassl2"
16
+ gem.homepage = "http://github.com/chrisa/eassl"
17
+ gem.license = "Ruby"
18
+ gem.summary = %Q{EaSSL is a library aimed at making openSSL certificate generation and management easier and more ruby-ish.}
19
+ gem.description = %Q{This gem is a more featureful but still drop-in replacement for eassl 0.1.1643}
20
+ gem.email = "chris@nodnol.org"
21
+ gem.authors = ["Paul Nicholson", "Paul Meserve", "Chris Andrews"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "eassl #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/Readme.mkd ADDED
@@ -0,0 +1,44 @@
1
+ eassl
2
+ ====================
3
+ EaSSL is a library aimed at making openSSL certificate generation and management easier and more ruby-ish.
4
+
5
+ Patch from https://github.com/openrain/eassl-fix applied onto source of eassl-0.1.1643 from rubyforge, jeweler-ized, and switched from MD5 to SHA1 hash for CSR signing
6
+
7
+ Ruby license, inherited from the rubyforge project
8
+
9
+ Installation
10
+ ------------
11
+ We recommend installing with Bundler:
12
+ gem 'eassl', :git => 'git://github.com/pogodan/eassl.git'
13
+
14
+ Use
15
+ -------------
16
+ Generating a CSR and private key:
17
+ options = {
18
+ :department => 'web sites',
19
+ :common_name => 'www.mydomain.com',
20
+ :organization, => 'My Org'
21
+ :email => 'test@test.com',
22
+ :city => 'Fargo',
23
+ :state => 'North Dakota',
24
+ :country => 'USA'
25
+ }
26
+
27
+ ea_key = EaSSL::Key.new
28
+ ea_name = EaSSL::CertificateName.new(options)
29
+ ea_csr = EaSSL::SigningRequest.new(:name => ea_name, :key => ea_key)
30
+
31
+ csr = ea_csr.ssl.to_s
32
+ key = ea_key.private_key.to_s
33
+
34
+ Contributing
35
+ -------------
36
+ (boilerplate Jeweler text)
37
+
38
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
39
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
40
+ * Fork the project
41
+ * Start a feature/bugfix branch
42
+ * Commit and push until you are happy with your contribution
43
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
44
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/eassl2.gemspec ADDED
@@ -0,0 +1,93 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{eassl2}
8
+ s.version = "2.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Paul Nicholson}, %q{Paul Meserve}, %q{Chris Andrews}]
12
+ s.date = %q{2012-07-20}
13
+ s.description = %q{This gem is a drop-in replacement for eassl 0.1.1643}
14
+ s.email = %q{chris@nodnol.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.txt"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.txt",
25
+ "Rakefile",
26
+ "Readme.mkd",
27
+ "VERSION",
28
+ "eassl.gemspec",
29
+ "lib/eassl.rb",
30
+ "lib/eassl/authority_certificate.rb",
31
+ "lib/eassl/certificate.rb",
32
+ "lib/eassl/certificate_authority.rb",
33
+ "lib/eassl/certificate_name.rb",
34
+ "lib/eassl/key.rb",
35
+ "lib/eassl/serial.rb",
36
+ "lib/eassl/signing_request.rb",
37
+ "test/CA/cacert.pem",
38
+ "test/CA/cakey.pem",
39
+ "test/CA/serial.txt",
40
+ "test/certificate.pem",
41
+ "test/csr.pem",
42
+ "test/encrypted_key.pem",
43
+ "test/helper.rb",
44
+ "test/test_eassl.rb",
45
+ "test/test_eassl_authority_certificate.rb",
46
+ "test/test_eassl_certificate.rb",
47
+ "test/test_eassl_certificate_authority.rb",
48
+ "test/test_eassl_key.rb",
49
+ "test/test_eassl_key_csr.rb",
50
+ "test/test_eassl_sign_cert.rb",
51
+ "test/test_eassl_signing_request.rb",
52
+ "test/unencrypted_key.pem",
53
+ "test/unencrypted_key2.pem"
54
+ ]
55
+ s.homepage = %q{http://github.com/chrisa/eassl}
56
+ s.licenses = [%q{Ruby}]
57
+ s.require_paths = [%q{lib}]
58
+ s.rubygems_version = %q{1.8.6}
59
+ s.summary = %q{EaSSL is a library aimed at making openSSL certificate generation and management easier and more ruby-ish.}
60
+ s.test_files = [
61
+ "test/helper.rb",
62
+ "test/test_eassl.rb",
63
+ "test/test_eassl_authority_certificate.rb",
64
+ "test/test_eassl_certificate.rb",
65
+ "test/test_eassl_certificate_authority.rb",
66
+ "test/test_eassl_key.rb",
67
+ "test/test_eassl_key_csr.rb",
68
+ "test/test_eassl_sign_cert.rb",
69
+ "test/test_eassl_signing_request.rb"
70
+ ]
71
+
72
+ if s.respond_to? :specification_version then
73
+ s.specification_version = 3
74
+
75
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
76
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
77
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
78
+ s.add_development_dependency(%q<rcov>, [">= 0"])
79
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
80
+ else
81
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
82
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
83
+ s.add_dependency(%q<rcov>, [">= 0"])
84
+ s.add_dependency(%q<simplecov>, [">= 0"])
85
+ end
86
+ else
87
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
88
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
89
+ s.add_dependency(%q<rcov>, [">= 0"])
90
+ s.add_dependency(%q<simplecov>, [">= 0"])
91
+ end
92
+ end
93
+
@@ -0,0 +1,59 @@
1
+ require 'openssl'
2
+ require 'eassl'
3
+ module EaSSL
4
+ # Author:: Paul Nicholson (mailto:paul@webpowerdesign.net)
5
+ # Co-Author:: Adam Williams (mailto:adam@thewilliams.ws)
6
+ # Copyright:: Copyright (c) 2006 WebPower Design
7
+ # License:: Distributes under the same terms as Ruby
8
+ class AuthorityCertificate
9
+ def initialize(options)
10
+ @options = {
11
+ :key => nil, #required
12
+ :name => {}, #required, CertificateName
13
+ }.update(options)
14
+ end
15
+
16
+ def ssl
17
+ unless @ssl
18
+ cert = OpenSSL::X509::Certificate.new
19
+ cert.not_before = Time.now
20
+ cert.subject = cert.issuer = CertificateName.new({ :common_name => "CA" }.update(@options[:name])).name
21
+ cert.not_after = cert.not_before + (365 * 5) * 24 * 60 * 60
22
+ cert.public_key = @options[:key].public_key
23
+ cert.serial = 1
24
+ cert.version = 2 # X509v3
25
+
26
+ ef = OpenSSL::X509::ExtensionFactory.new
27
+ ef.subject_certificate = cert
28
+ ef.issuer_certificate = cert
29
+ cert.extensions = [
30
+ ef.create_extension("basicConstraints","CA:TRUE"),
31
+ ef.create_extension("keyUsage", "cRLSign, keyCertSign"),
32
+ ef.create_extension("subjectKeyIdentifier", "hash"),
33
+ ef.create_extension("nsComment", "Ruby/OpenSSL/EaSSL Generated Certificate"),
34
+ ]
35
+ cert.add_extension(ef.create_extension("authorityKeyIdentifier", "keyid:always,issuer:always"))
36
+ cert.sign(@options[:key].private_key, OpenSSL::Digest::SHA1.new)
37
+ @ssl = cert
38
+ end
39
+ @ssl
40
+ end
41
+
42
+ def method_missing(method)
43
+ ssl.send(method)
44
+ end
45
+
46
+ def load(pem_string)
47
+ begin
48
+ @ssl = OpenSSL::X509::Certificate.new(pem_string)
49
+ rescue
50
+ raise "CertificateLoader: Error loading certificate"
51
+ end
52
+ self
53
+ end
54
+
55
+ def self.load(pem_file_path)
56
+ new({}).load(File.read(pem_file_path))
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,87 @@
1
+ require 'openssl'
2
+ require 'eassl'
3
+ module EaSSL
4
+ # Author:: Paul Nicholson (mailto:paul@webpowerdesign.net)
5
+ # Co-Author:: Adam Williams (mailto:adam@thewilliams.ws)
6
+ # Copyright:: Copyright (c) 2006 WebPower Design
7
+ # License:: Distributes under the same terms as Ruby
8
+ class Certificate
9
+ def initialize(options)
10
+ @options = {
11
+ :days_valid => (365 * 5),
12
+ :signing_request => nil, #required
13
+ :ca_certificate => nil, #required
14
+ :comment => "Ruby/OpenSSL/EaSSL Generated Certificate",
15
+ :type => "server"
16
+ }.update(options)
17
+ end
18
+
19
+ def ssl
20
+ unless @ssl
21
+ @ssl = OpenSSL::X509::Certificate.new
22
+ @ssl.not_before = Time.now
23
+ @ssl.subject = @options[:signing_request].subject
24
+ @ssl.issuer = @options[:ca_certificate]? @options[:ca_certificate].subject : @ssl.subject
25
+ @ssl.not_after = @ssl.not_before + @options[:days_valid] * 24 * 60 * 60
26
+ @ssl.public_key = @options[:signing_request].public_key
27
+ @ssl.serial = @options[:serial] || 2
28
+ @ssl.version = 2 # X509v3
29
+
30
+ ef = OpenSSL::X509::ExtensionFactory.new
31
+ ef.subject_certificate = @ssl
32
+ ef.issuer_certificate = @options[:ca_certificate]? @options[:ca_certificate].ssl : @ssl
33
+ @ssl.extensions = [
34
+ ef.create_extension("basicConstraints","CA:FALSE"),
35
+ ef.create_extension("subjectKeyIdentifier", "hash"),
36
+
37
+ ef.create_extension("nsComment", @options[:comment]),
38
+ ]
39
+ # this extension must be added separately, after the others.
40
+ # presumably needs subjectKeyIdentifier to already be in place
41
+ @ssl.add_extension(ef.create_extension("authorityKeyIdentifier", "keyid:always,issuer:always"))
42
+
43
+ if @options[:type] == 'server'
44
+ @ssl.add_extension(ef.create_extension("keyUsage", "digitalSignature,keyEncipherment"))
45
+ @ssl.add_extension(ef.create_extension("extendedKeyUsage", "serverAuth"))
46
+ end
47
+ if @options[:type] == 'client'
48
+ @ssl.add_extension(ef.create_extension("keyUsage", "nonRepudiation,digitalSignature,keyEncipherment"))
49
+ @ssl.add_extension(ef.create_extension("extendedKeyUsage", "clientAuth,emailProtection"))
50
+ end
51
+ end
52
+ @ssl
53
+ end
54
+
55
+ def sign(ca_key)
56
+ ssl.sign(ca_key.private_key, OpenSSL::Digest::SHA1.new)
57
+ end
58
+
59
+ def to_pem
60
+ ssl.to_pem
61
+ end
62
+
63
+ # Returns a SHA1 fingerprint of the certificate in the OpenSSL style
64
+ def sha1_fingerprint
65
+ Digest::SHA1.hexdigest(ssl.to_der).upcase.gsub(/(..)/, '\1:').chop
66
+ end
67
+
68
+ # This method is used to intercept and pass-thru calls to openSSL methods and instance
69
+ # variables.
70
+ def method_missing(method)
71
+ ssl.send(method)
72
+ end
73
+
74
+ def self.load(pem_file_path)
75
+ new({}).load(File.read(pem_file_path))
76
+ end
77
+
78
+ def load(pem_string)
79
+ begin
80
+ @ssl = OpenSSL::X509::Certificate.new(pem_string)
81
+ rescue
82
+ raise "CertificateLoader: Error loading certificate"
83
+ end
84
+ self
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,46 @@
1
+ require 'openssl'
2
+ require 'eassl'
3
+ module EaSSL
4
+ # Author:: Paul Nicholson (mailto:paul@webpowerdesign.net)
5
+ # Co-Author:: Adam Williams (mailto:adam@thewilliams.ws)
6
+ # Copyright:: Copyright (c) 2006 WebPower Design
7
+ # License:: Distributes under the same terms as Ruby
8
+ class CertificateAuthority
9
+ attr_reader :key, :certificate, :serial
10
+ def initialize(options = {})
11
+ if options[:key] && options[:certificate] && options[:serial]
12
+ @key = options[:key]
13
+ @certificate = options[:certificate]
14
+ @serial = options[:serial]
15
+ else
16
+ options[:name] ||= {}
17
+ @key = Key.new({:password => 'ca_ssl_password'}.update(options))
18
+ @certificate = AuthorityCertificate.new(:key => @key, :name => options[:name])
19
+ @serial = Serial.new(:next => 1)
20
+ end
21
+ end
22
+
23
+ def self.load(options)
24
+ key = Key.load(File.join(options[:ca_path], 'cakey.pem'), options[:ca_password])
25
+ certificate = AuthorityCertificate.load(File.join(options[:ca_path], 'cacert.pem'))
26
+ serial = Serial.load(File.join(options[:ca_path], 'serial.txt'))
27
+ self.new(:key => key, :certificate => certificate, :serial => serial)
28
+ end
29
+
30
+ def create_certificate(signing_request, type='server', days_valid=nil)
31
+ options = {
32
+ :signing_request => signing_request,
33
+ :ca_certificate => @certificate,
34
+ :serial => @serial.issue_serial,
35
+ :type => type
36
+ }
37
+ if days_valid
38
+ options[:days_valid] = days_valid
39
+ end
40
+ cert = Certificate.new(options)
41
+ @serial.save!
42
+ cert.sign(@key)
43
+ cert
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,41 @@
1
+ require 'openssl'
2
+ require 'eassl'
3
+ module EaSSL
4
+ # Author:: Paul Nicholson (mailto:paul@webpowerdesign.net)
5
+ # Co-Author:: Adam Williams (mailto:adam@thewilliams.ws)
6
+ # Copyright:: Copyright (c) 2006 WebPower Design
7
+ # License:: Distributes under the same terms as Ruby
8
+ class CertificateName
9
+ def initialize(options)
10
+ @options = {
11
+ :country => "US",
12
+ :state => "North Carolina",
13
+ :city => "Fuquay Varina",
14
+ :organization => "WebPower Design",
15
+ :department => "Web Security",
16
+ :common_name => nil, # required
17
+ :email => "eassl@rubyforge.org",
18
+ }.update(options)
19
+ end
20
+
21
+ def ssl
22
+ OpenSSL::X509::Name.new([
23
+ ['C', @options[:country], OpenSSL::ASN1::PRINTABLESTRING],
24
+ ['ST', @options[:state], OpenSSL::ASN1::PRINTABLESTRING],
25
+ ['L', @options[:city], OpenSSL::ASN1::PRINTABLESTRING],
26
+ ['O', @options[:organization], OpenSSL::ASN1::UTF8STRING],
27
+ ['OU', @options[:department], OpenSSL::ASN1::UTF8STRING],
28
+ ['CN', @options[:common_name], OpenSSL::ASN1::UTF8STRING],
29
+ ['emailAddress', @options[:email], OpenSSL::ASN1::UTF8STRING]
30
+ ])
31
+ end
32
+
33
+ def name
34
+ ssl
35
+ end
36
+
37
+ def options
38
+ @options
39
+ end
40
+ end
41
+ end
data/lib/eassl/key.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'openssl'
2
+ require 'eassl'
3
+ module EaSSL
4
+ # == EaSSL::Key creates and manages openSSL keys
5
+ #
6
+ # Author:: Paul Nicholson (mailto:paul@webpowerdesign.net)
7
+ # Co-Author:: Adam Williams (mailto:adam@thewilliams.ws)
8
+ # Copyright:: Copyright (c) 2006 WebPower Design
9
+ # License:: Distributes under the same terms as Ruby
10
+ #
11
+ # ==== Usage
12
+ #
13
+ # ===== Availible Methods - including methods provided by openSSL::PKey:
14
+ # * public_key
15
+ # * private_key
16
+ # * to_text
17
+ class Key
18
+ # Create new Key using the provided options or using the defaults
19
+ def initialize(options = {}) #:params: options
20
+ @options = {
21
+ :bits => 2048,
22
+ :password => 'ssl_password',
23
+ }.update(options)
24
+ end
25
+
26
+ def ssl
27
+ unless @ssl
28
+ # <Should use some kind of logger on this>
29
+ # $stderr.puts "Generating #{@options[:bits]} bit key\n"
30
+ @ssl = OpenSSL::PKey::RSA::new(@options[:bits])
31
+ end
32
+ @ssl
33
+ end
34
+
35
+ # This method is used to intercept and pass-thru calls to openSSL methods and instance
36
+ # variables.
37
+ def method_missing(method) # :nodoc:
38
+ ssl.send(method)
39
+ end
40
+
41
+ def private_key
42
+ ssl
43
+ end
44
+
45
+ # Returns the length of the key in bits
46
+ def length
47
+ ssl.n.num_bytes * 8
48
+ end
49
+
50
+ # Export the encrypted key, returns a string
51
+ def to_pem
52
+ ssl.export(OpenSSL::Cipher::DES.new('EDE3-CBC'), @options[:password])
53
+ end
54
+
55
+ # Decrypt and load a PEM encoded Key from the file system with the provided password.
56
+ def self.load(pem_file_path, password=nil)
57
+ new.load(File.read(pem_file_path), password)
58
+ end
59
+
60
+ # Decrypt and load a PEM encoded Key from provided string with the provided password.
61
+ def load(pem_string, password=nil)
62
+ begin
63
+ @ssl = OpenSSL::PKey::RSA::new(pem_string, password || @options[:password])
64
+ rescue
65
+ raise "KeyLoader: Error decrypting key with password"
66
+ end
67
+ self
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,33 @@
1
+ require 'eassl'
2
+ module EaSSL
3
+ # Author:: Chris Andrews (mailto:chris@nodnol.org)
4
+ # Copyright:: Copyright (c) 2011 Chris Andrews
5
+ # License:: Distributes under the same terms as Ruby
6
+ class Serial
7
+ attr_reader :next
8
+ def initialize(options = {})
9
+ @next = options[:next]
10
+ @path = options[:path]
11
+ end
12
+
13
+ def self.load(serial_file_path)
14
+ hex_string = (File.read(serial_file_path))
15
+ self.new(:next => Integer("0x#{hex_string}"), :path => serial_file_path)
16
+ end
17
+
18
+ def save!
19
+ if @path
20
+ hex_string = sprintf("%04X", @next)
21
+ File.open(@path, 'w') do |io|
22
+ io.write "#{hex_string}\n"
23
+ end
24
+ end
25
+ end
26
+
27
+ def issue_serial
28
+ @next = @next + 1
29
+ @next - 1
30
+ end
31
+ end
32
+ end
33
+