summaryrefslogtreecommitdiff
path: root/lib/rubygems/gem_openssl.rb
blob: 5c33cac8c619a1d21efbbbf982c0efc53fba647c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
68
69
70
71
72
73
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
######################################################################
# This file is imported from the rubygems project.
# DO NOT make modifications in this repo. They _will_ be reverted!
# File a patch instead and assign it to Ryan Davis or Eric Hodel.
######################################################################

#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++

#--
# Some system might not have OpenSSL installed, therefore the core
# library file openssl might not be available.  We localize testing
# for the presence of OpenSSL in this file.
#++

module Gem
  class << self
    ##
    # Is SSL (used by the signing commands) available on this
    # platform?

    def ssl_available?
      @ssl_available
    end

    ##
    # Is SSL available?

    attr_writer :ssl_available

    ##
    # Ensure that SSL is available.  Throw an exception if it is not.

    def ensure_ssl_available
      unless ssl_available?
        raise Gem::Exception, "SSL is not installed on this system"
      end
    end
  end
end

begin
  require 'openssl'

  # Reference a constant defined in the .rb portion of ssl (just to
  # make sure that part is loaded too).

  Gem.ssl_available = !!OpenSSL::Digest::SHA1

  class OpenSSL::X509::Certificate # :nodoc:
    # Check the validity of this certificate.
    def check_validity(issuer_cert = nil, time = Time.now)
      ret = if @not_before && @not_before > time
              [false, :expired, "not valid before '#@not_before'"]
            elsif @not_after && @not_after < time
              [false, :expired, "not valid after '#@not_after'"]
            elsif issuer_cert && !verify(issuer_cert.public_key)
              [false, :issuer, "#{issuer_cert.subject} is not issuer"]
            else
              [true, :ok, 'Valid certificate']
            end

      # return hash
      { :is_valid => ret[0], :error => ret[1], :desc => ret[2] }
    end
  end

rescue LoadError, StandardError
  Gem.ssl_available = false
end

# :stopdoc:

module Gem::SSL

  # We make our own versions of the constants here.  This allows us
  # to reference the constants, even though some systems might not
  # have SSL installed in the Ruby core package.
  #
  # These constants are only used during load time.  At runtime, any
  # method that makes a direct reference to SSL software must be
  # protected with a Gem.ensure_ssl_available call.

  if Gem.ssl_available? then
    PKEY_RSA = OpenSSL::PKey::RSA
    DIGEST_SHA1 = OpenSSL::Digest::SHA1
  else
    PKEY_RSA = :rsa
    DIGEST_SHA1 = :sha1
  end

end

# :startdoc: