Sha256: 8487c83eb4b9b6e22fa451c5959a8b5ecbd04120c2afe74f6b33d3d3c4e8a4f4

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require 'xml/kit/key_info/key_value'
require 'xml/kit/key_info/retrieval_method'
require 'xml/kit/key_info/rsa_key_value'

module Xml
  module Kit
    # An implementation of the KeyInfo element.
    # https://www.w3.org/TR/xmldsig-core1/#sec-KeyInfo
    #
    # @since 0.3.0
    class KeyInfo
      include Templatable
      attr_accessor :key_name
      attr_accessor :x509_data
      attr_accessor :encrypted_key

      def initialize(x509: nil, encrypted_key: nil)
        @encrypted_key = encrypted_key
        @x509_data = x509
        yield self if block_given?
      end

      def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM)
        return encrypted_key.asymmetric_cipher if encrypted_key

        if x509_data
          return Crypto.cipher_for(
            derive_algorithm_from(x509_data.public_key),
            x509_data.public_key
          )
        end

        super(algorithm: algorithm)
      end

      def symmetric_cipher
        return super if encrypted_key.nil?

        encrypted_key.symmetric_cipher
      end

      def key_value
        @key_value ||= KeyValue.new
      end

      def retrieval_method
        @retrieval_method ||= RetrievalMethod.new
      end

      def subject_key_identifier
        ski = x509_data.extensions.find { |x| x.oid == 'subjectKeyIdentifier' }
        return if ski.nil?

        Base64.strict_encode64(ski.value)
      end

      private

      def derive_algorithm_from(key)
        case key
        when OpenSSL::PKey::RSA
          "#{::Xml::Kit::Namespaces::XMLENC}rsa-1_5"
        else
          raise ::Xml::Kit::Error, "#{key.try(:class)} is not supported"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xml-kit-0.6.0 lib/xml/kit/key_info.rb
xml-kit-0.5.0 lib/xml/kit/key_info.rb
xml-kit-0.4.0 lib/xml/kit/key_info.rb