2013-09-14 04:59:02 -04:00
|
|
|
require 'rubygems/user_interaction'
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# A Gem::Security::Policy object encapsulates the settings for verifying
|
|
|
|
# signed gem files. This is the base class. You can either declare an
|
|
|
|
# instance of this or use one of the preset security policies in
|
|
|
|
# Gem::Security::Policies.
|
|
|
|
|
|
|
|
class Gem::Security::Policy
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
include Gem::UserInteraction
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
attr_accessor :only_signed
|
|
|
|
attr_accessor :only_trusted
|
|
|
|
attr_accessor :verify_chain
|
|
|
|
attr_accessor :verify_data
|
|
|
|
attr_accessor :verify_root
|
|
|
|
attr_accessor :verify_signer
|
|
|
|
|
|
|
|
##
|
|
|
|
# Create a new Gem::Security::Policy object with the given mode and
|
|
|
|
# options.
|
|
|
|
|
|
|
|
def initialize name, policy = {}, opt = {}
|
2013-03-05 17:40:53 -05:00
|
|
|
require 'openssl'
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
@name = name
|
|
|
|
|
|
|
|
@opt = opt
|
|
|
|
|
|
|
|
# Default to security
|
|
|
|
@only_signed = true
|
|
|
|
@only_trusted = true
|
|
|
|
@verify_chain = true
|
|
|
|
@verify_data = true
|
|
|
|
@verify_root = true
|
|
|
|
@verify_signer = true
|
|
|
|
|
|
|
|
policy.each_pair do |key, val|
|
|
|
|
case key
|
|
|
|
when :verify_data then @verify_data = val
|
|
|
|
when :verify_signer then @verify_signer = val
|
|
|
|
when :verify_chain then @verify_chain = val
|
|
|
|
when :verify_root then @verify_root = val
|
|
|
|
when :only_trusted then @only_trusted = val
|
|
|
|
when :only_signed then @only_signed = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Verifies each certificate in +chain+ has signed the following certificate
|
|
|
|
# and is valid for the given +time+.
|
|
|
|
|
|
|
|
def check_chain chain, time
|
2013-02-07 21:58:19 -05:00
|
|
|
raise Gem::Security::Exception, 'missing signing chain' unless chain
|
|
|
|
raise Gem::Security::Exception, 'empty signing chain' if chain.empty?
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
begin
|
|
|
|
chain.each_cons 2 do |issuer, cert|
|
|
|
|
check_cert cert, issuer, time
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue Gem::Security::Exception => e
|
|
|
|
raise Gem::Security::Exception, "invalid signing chain: #{e.message}"
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Verifies that +data+ matches the +signature+ created by +public_key+ and
|
|
|
|
# the +digest+ algorithm.
|
|
|
|
|
|
|
|
def check_data public_key, digest, signature, data
|
|
|
|
raise Gem::Security::Exception, "invalid signature" unless
|
|
|
|
public_key.verify digest.new, signature, data.digest
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Ensures that +signer+ is valid for +time+ and was signed by the +issuer+.
|
|
|
|
# If the +issuer+ is +nil+ no verification is performed.
|
|
|
|
|
|
|
|
def check_cert signer, issuer, time
|
2013-02-07 21:58:19 -05:00
|
|
|
raise Gem::Security::Exception, 'missing signing certificate' unless
|
|
|
|
signer
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
message = "certificate #{signer.subject}"
|
|
|
|
|
|
|
|
if not_before = signer.not_before and not_before > time then
|
|
|
|
raise Gem::Security::Exception,
|
|
|
|
"#{message} not valid before #{not_before}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if not_after = signer.not_after and not_after < time then
|
|
|
|
raise Gem::Security::Exception, "#{message} not valid after #{not_after}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if issuer and not signer.verify issuer.public_key then
|
|
|
|
raise Gem::Security::Exception,
|
|
|
|
"#{message} was not issued by #{issuer.subject}"
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Ensures the public key of +key+ matches the public key in +signer+
|
|
|
|
|
|
|
|
def check_key signer, key
|
2013-02-07 21:58:19 -05:00
|
|
|
unless signer and key then
|
|
|
|
return true unless @only_signed
|
|
|
|
|
|
|
|
raise Gem::Security::Exception, 'missing key or signature'
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
raise Gem::Security::Exception,
|
|
|
|
"certificate #{signer.subject} does not match the signing key" unless
|
|
|
|
signer.public_key.to_pem == key.public_key.to_pem
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Ensures the root certificate in +chain+ is self-signed and valid for
|
|
|
|
# +time+.
|
|
|
|
|
|
|
|
def check_root chain, time
|
2013-02-07 21:58:19 -05:00
|
|
|
raise Gem::Security::Exception, 'missing signing chain' unless chain
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
root = chain.first
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
raise Gem::Security::Exception, 'missing root certificate' unless root
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
raise Gem::Security::Exception,
|
2012-12-06 00:26:46 -05:00
|
|
|
"root certificate #{root.subject} is not self-signed " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"(issuer #{root.issuer})" if
|
|
|
|
root.issuer.to_s != root.subject.to_s # HACK to_s is for ruby 1.8
|
|
|
|
|
|
|
|
check_cert root, root, time
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Ensures the root of +chain+ has a trusted certificate in +trust_dir+ and
|
|
|
|
# the digests of the two certificates match according to +digester+
|
|
|
|
|
|
|
|
def check_trust chain, digester, trust_dir
|
2013-02-07 21:58:19 -05:00
|
|
|
raise Gem::Security::Exception, 'missing signing chain' unless chain
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
root = chain.first
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
raise Gem::Security::Exception, 'missing root certificate' unless root
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
path = Gem::Security.trust_dir.cert_path root
|
|
|
|
|
|
|
|
unless File.exist? path then
|
|
|
|
message = "root cert #{root.subject} is not trusted"
|
|
|
|
|
|
|
|
message << " (root of signing cert #{chain.last.subject})" if
|
|
|
|
chain.length > 1
|
|
|
|
|
|
|
|
raise Gem::Security::Exception, message
|
|
|
|
end
|
|
|
|
|
|
|
|
save_cert = OpenSSL::X509::Certificate.new File.read path
|
|
|
|
save_dgst = digester.digest save_cert.public_key.to_s
|
|
|
|
|
|
|
|
pkey_str = root.public_key.to_s
|
|
|
|
cert_dgst = digester.digest pkey_str
|
|
|
|
|
|
|
|
raise Gem::Security::Exception,
|
2012-12-06 00:26:46 -05:00
|
|
|
"trusted root certificate #{root.subject} checksum " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"does not match signing root certificate checksum" unless
|
|
|
|
save_dgst == cert_dgst
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
|
|
|
# Extracts the email or subject from +certificate+
|
|
|
|
|
|
|
|
def subject certificate # :nodoc:
|
|
|
|
certificate.extensions.each do |extension|
|
|
|
|
next unless extension.oid == 'subjectAltName'
|
|
|
|
|
|
|
|
return extension.value
|
|
|
|
end
|
|
|
|
|
|
|
|
certificate.subject.to_s
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def inspect # :nodoc:
|
2013-02-07 00:56:53 -05:00
|
|
|
("[Policy: %s - data: %p signer: %p chain: %p root: %p " +
|
|
|
|
"signed-only: %p trusted-only: %p]") % [
|
2012-11-29 01:52:18 -05:00
|
|
|
@name, @verify_chain, @verify_data, @verify_root, @verify_signer,
|
|
|
|
@only_signed, @only_trusted,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2013-09-14 04:59:02 -04:00
|
|
|
# For +full_name+, verifies the certificate +chain+ is valid, the +digests+
|
|
|
|
# match the signatures +signatures+ created by the signer depending on the
|
|
|
|
# +policy+ settings.
|
2012-11-29 01:52:18 -05:00
|
|
|
#
|
|
|
|
# If +key+ is given it is used to validate the signing certificate.
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def verify chain, key = nil, digests = {}, signatures = {},
|
|
|
|
full_name = '(unknown)'
|
|
|
|
if signatures.empty? then
|
|
|
|
if @only_signed then
|
|
|
|
raise Gem::Security::Exception,
|
|
|
|
"unsigned gems are not allowed by the #{name} policy"
|
|
|
|
elsif digests.empty? then
|
|
|
|
# lack of signatures is irrelevant if there is nothing to check
|
|
|
|
# against
|
|
|
|
else
|
|
|
|
alert_warning "#{full_name} is not signed"
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt = @opt
|
|
|
|
digester = Gem::Security::DIGEST_ALGORITHM
|
|
|
|
trust_dir = opt[:trust_dir]
|
|
|
|
time = Time.now
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
_, signer_digests = digests.find do |algorithm, file_digests|
|
2012-11-29 01:52:18 -05:00
|
|
|
file_digests.values.first.name == Gem::Security::DIGEST_NAME
|
|
|
|
end
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
if @verify_data then
|
|
|
|
raise Gem::Security::Exception, 'no digests provided (probable bug)' if
|
|
|
|
signer_digests.nil? or signer_digests.empty?
|
|
|
|
else
|
|
|
|
signer_digests = {}
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
signer = chain.last
|
|
|
|
|
|
|
|
check_key signer, key if key
|
|
|
|
|
|
|
|
check_cert signer, nil, time if @verify_signer
|
|
|
|
|
|
|
|
check_chain chain, time if @verify_chain
|
|
|
|
|
|
|
|
check_root chain, time if @verify_root
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
if @only_trusted then
|
|
|
|
check_trust chain, digester, trust_dir
|
|
|
|
elsif signatures.empty? and digests.empty? then
|
|
|
|
# trust is irrelevant if there's no signatures to verify
|
|
|
|
else
|
|
|
|
alert_warning "#{subject signer} is not trusted for #{full_name}"
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
signatures.each do |file, _|
|
|
|
|
digest = signer_digests[file]
|
|
|
|
|
|
|
|
raise Gem::Security::Exception, "missing digest for #{file}" unless
|
|
|
|
digest
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
signer_digests.each do |file, digest|
|
|
|
|
signature = signatures[file]
|
|
|
|
|
|
|
|
raise Gem::Security::Exception, "missing signature for #{file}" unless
|
|
|
|
signature
|
|
|
|
|
|
|
|
check_data signer.public_key, digester, signature, digest if @verify_data
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Extracts the certificate chain from the +spec+ and calls #verify to ensure
|
|
|
|
# the signatures and certificate chain is valid according to the policy..
|
|
|
|
|
|
|
|
def verify_signatures spec, digests, signatures
|
|
|
|
chain = spec.cert_chain.map do |cert_pem|
|
|
|
|
OpenSSL::X509::Certificate.new cert_pem
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
verify chain, nil, digests, signatures, spec.full_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
alias to_s name # :nodoc:
|
|
|
|
|
|
|
|
end
|
|
|
|
|