openssl-extensions 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,8 @@
1
1
  module OpenSSLExtensions
2
2
 
3
+ ##
4
+ # Ensures that the current Ruby was compiled with OpenSSL support enabled.
5
+ #
3
6
  def self.check_dependencies!
4
7
  begin
5
8
  require 'openssl'
@@ -2,4 +2,5 @@ require 'openssl-extensions'
2
2
  OpenSSLExtensions.check_dependencies!
3
3
 
4
4
  require 'openssl-extensions/x509/certificate'
5
+ require 'openssl-extensions/x509/certificate_chain'
5
6
  require 'openssl-extensions/x509/name'
@@ -1,3 +1,3 @@
1
1
  module OpenSSLExtensions
2
- Version = '0.0.1'
2
+ Version = '0.0.2'
3
3
  end
@@ -1,12 +1,17 @@
1
1
  require 'openssl-extensions/x509'
2
2
 
3
+ ##
4
+ # Returned with requesting an OpenSSLExtensions::X509::Certificate.authority_key_identifier.
5
+ # If available, this collects the issuer_name (issuer's common name),
6
+ # serial_number, and key_id (fingerprint).
7
+ #
3
8
  class OpenSSLExtensions::X509::AuthorityKeyIdentifier
4
9
 
5
10
  attr_reader :issuer_name, :serial_number, :key_id
6
11
  alias :serial :serial_number
7
12
 
8
13
  def initialize(extension_string)
9
- parse(extension_string.dup)
14
+ parse(extension_string.dup) if extension_string
10
15
  end
11
16
 
12
17
  def parse(string)
@@ -1,12 +1,16 @@
1
1
  require 'openssl-extensions/x509'
2
2
  require 'openssl-extensions/x509/authority_key_identifier'
3
3
 
4
+ ##
5
+ # Extends OpenSSL::X509::Certificate with shortcut methods.
6
+ #
4
7
  module OpenSSLExtensions::X509::Certificate
5
8
 
6
9
  def subject_alternative_names
7
10
  names_string = read_extension_by_oid('subjectAltName')
8
11
  names_string ? names_string.scan(%r{DNS:([^,]+)}).flatten : []
9
12
  end
13
+ alias :sans :subject_alternative_names
10
14
 
11
15
  def subject_key_identifier
12
16
  read_extension_by_oid('subjectKeyIdentifier')
@@ -21,6 +25,35 @@ module OpenSSLExtensions::X509::Certificate
21
25
  end
22
26
  protected :read_extension_by_oid
23
27
 
28
+ ##
29
+ # Returns +true+ if this certificate is authorized to sign for other certificates (useful for determining CA roots
30
+ # and intermediary certificates).
31
+ #
32
+ def allows_certificate_signing?
33
+ usage = read_extension_by_oid('keyUsage')
34
+ usage.nil? || !!(usage.match(%r{\bCertificate Sign\b}))
35
+ end
36
+
37
+ ##
38
+ # Returns +true+ if the certificate given is the issuer certificate for this certificate.
39
+ #
40
+ def issuing_certificate?(issuer)
41
+ (self.authority_key_identifier.key_id &&
42
+ issuer.subject_key_identifier &&
43
+ self.authority_key_identifier.key_id == issuer.subject_key_identifier) ||
44
+ (!self.authority_key_identifier.key_id &&
45
+ self.issuer.common_name == issuer.subject.common_name &&
46
+ self.issuer.country == issuer.subject.country &&
47
+ self.issuer.organization == issuer.subject.organization)
48
+ end
49
+
50
+ ##
51
+ # Equality is tested by comparing the generated PEM signatures.
52
+ #
53
+ def ==(other)
54
+ to_pem == other.to_pem
55
+ end
56
+
24
57
  end
25
58
 
26
59
  OpenSSL::X509::Certificate.send(:include, OpenSSLExtensions::X509::Certificate)
@@ -0,0 +1,44 @@
1
+ require 'openssl-extensions/x509'
2
+ require 'openssl-extensions/x509/certificate'
3
+
4
+ ##
5
+ # Provides a thin wrapper to an Array which contains the full certificate
6
+ # chain. This array, however, has been reorganized to be in the proper
7
+ # order for the chain as follows:
8
+ #
9
+ # [Site Certificate, Intermediary #1, ..., CA Root]
10
+ #
11
+ # Where +Intermediary #1+ is the issuing certificate of the
12
+ # +Site Certificate+, followed by +#2+ which issued +#1+, down to the
13
+ # final root signing certificate in last position.
14
+ #
15
+ class OpenSSLExtensions::X509::CertificateChain
16
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
17
+
18
+ def initialize(peer_certificate, certificates)
19
+ @certificates = []
20
+ reorganize!(peer_certificate, certificates)
21
+ end
22
+
23
+ def method_missing(method, *args, &block)
24
+ @certificates.send(method, *args, &block)
25
+ end
26
+ private :method_missing
27
+
28
+ def reorganize!(site_certificate, certificates)
29
+ return unless site_certificate && !certificates.empty?
30
+ certificate = nil
31
+
32
+ @certificates << (certificates.delete(site_certificate) || site_certificate || certificates.delete(certificates.detect { |c| c.subject_key_identifier.nil? }))
33
+ certificate = @certificates.first
34
+
35
+ until certificate.nil?
36
+ if certificate = certificates.detect { |authority| authority.allows_certificate_signing? && certificate.issuing_certificate?(authority) }
37
+ @certificates << certificates.delete(certificate)
38
+ else
39
+ authority = nil
40
+ end
41
+ end
42
+ end
43
+ private :reorganize!
44
+ end
@@ -1,5 +1,8 @@
1
1
  require 'openssl-extensions/x509'
2
2
 
3
+ ##
4
+ # Extends OpenSSL::X509::Name with additional shortcut methods.
5
+ #
3
6
  module OpenSSLExtensions::X509::Name
4
7
  def organization
5
8
  read_entry_by_oid('O')
@@ -21,6 +24,10 @@ module OpenSSLExtensions::X509::Name
21
24
  read_entry_by_oid('L')
22
25
  end
23
26
 
27
+ def location
28
+ [locality, state, country].compact.join(', ')
29
+ end
30
+
24
31
  def state
25
32
  read_entry_by_oid('ST')
26
33
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nathaniel Bibler
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-01 00:00:00 -04:00
17
+ date: 2010-10-04 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,7 @@ files:
47
47
  - lib/openssl-extensions/version.rb
48
48
  - lib/openssl-extensions/x509/authority_key_identifier.rb
49
49
  - lib/openssl-extensions/x509/certificate.rb
50
+ - lib/openssl-extensions/x509/certificate_chain.rb
50
51
  - lib/openssl-extensions/x509/name.rb
51
52
  - lib/openssl-extensions/x509.rb
52
53
  - lib/openssl-extensions.rb