class Gem::Security::Signer

常量

DEFAULT_OPTIONS

属性

cert_chain[读写]

用于签名的证书链,包括签名证书

digest_algorithm[只读]

用于创建签名的摘要算法

key[读写]

签名证书的私钥

options[只读]

公共类方法

new(key, cert_chain, passphrase = nil, options = {}) 点击切换源码

创建一个新的签名器,使用 RSA key 或密钥路径,以及包含 X509 证书、编码证书或证书路径的证书 chain

# File rubygems/security/signer.rb, line 68
def initialize(key, cert_chain, passphrase = nil, options = {})
  @cert_chain = cert_chain
  @key        = key
  @passphrase = passphrase
  @options = DEFAULT_OPTIONS.merge(options)

  unless @key
    default_key = File.join Gem.default_key_path
    @key = default_key if File.exist? default_key
  end

  unless @cert_chain
    default_cert = File.join Gem.default_cert_path
    @cert_chain = [default_cert] if File.exist? default_cert
  end

  @digest_name      = Gem::Security::DIGEST_NAME
  @digest_algorithm = Gem::Security.create_digest(@digest_name)

  if @key && !@key.is_a?(OpenSSL::PKey::PKey)
    @key = OpenSSL::PKey.read(File.read(@key), @passphrase)
  end

  if @cert_chain
    @cert_chain = @cert_chain.compact.map do |cert|
      next cert if OpenSSL::X509::Certificate === cert

      cert = File.read cert if File.exist? cert

      OpenSSL::X509::Certificate.new cert
    end

    load_cert_chain
  end
end
re_sign_cert(expired_cert, expired_cert_path, private_key) { |expired_cert_path, new_expired_cert_path| ... } 点击切换源码

尝试使用给定的私钥重新签名过期的证书

# File rubygems/security/signer.rb, line 43
def self.re_sign_cert(expired_cert, expired_cert_path, private_key)
  return unless expired_cert.not_after < Time.now

  expiry = expired_cert.not_after.strftime("%Y%m%d%H%M%S")
  expired_cert_file = "#{File.basename(expired_cert_path)}.expired.#{expiry}"
  new_expired_cert_path = File.join(Gem.user_home, ".gem", expired_cert_file)

  Gem::Security.write(expired_cert, new_expired_cert_path)

  re_signed_cert = Gem::Security.re_sign(
    expired_cert,
    private_key,
    (Gem::Security::ONE_DAY * Gem.configuration.cert_expiration_length_days)
  )

  Gem::Security.write(re_signed_cert, expired_cert_path)

  yield(expired_cert_path, new_expired_cert_path) if block_given?
end

公共实例方法

sign(data) 点击切换源码

使用给定的摘要算法对数据进行签名

# File rubygems/security/signer.rb, line 140
def sign(data)
  return unless @key

  raise Gem::Security::Exception, "no certs provided" if @cert_chain.empty?

  if @cert_chain.length == 1 && @cert_chain.last.not_after < Time.now
    alert("Your certificate has expired, trying to re-sign it...")

    re_sign_key(
      expiration_length: (Gem::Security::ONE_DAY * options[:expiration_length_days])
    )
  end

  full_name = extract_name @cert_chain.last

  Gem::Security::SigningPolicy.verify @cert_chain, @key, {}, {}, full_name

  @key.sign @digest_algorithm.new, data
end