Sha256: 5ee7482011fa057d9a110ada3a3408f88930fd9a87b58d00c5f2321ececa7981

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# This class implements the EncryptedPrivateKeyInfo type,
# which is defined in PKCS #8 as follows:
#
#   EncryptedPrivateKeyInfo ::=  SEQUENCE {
#      encryptionAlgorithm   AlgorithmIdentifier,
#      encryptedData   OCTET STRING }
#

require 'openssl'

module Keystores
  module Jks
    class EncryptedPrivateKeyInfo
      attr_accessor :encrypted_data, :algorithm, :encoded

      def initialize(opts = {})
        # Parses from encoded private key
        if opts.has_key?(:encoded)
          encoded = opts[:encoded]
          @asn1 = OpenSSL::ASN1.decode(encoded)
          @encrypted_data = @asn1.value[1].value
          @algorithm = @asn1.value[0].value[0].value
          @encoded = encoded
        else
          @algorithm = opts[:algorithm]
          @encrypted_data = opts[:encrypted_data]
          @encoded = encode(@algorithm, @encrypted_data)
        end
      end

      private

      # Java actually encodes:
      #
      #   EncryptedPrivateKeyInfo ::=  SEQUENCE {
      #      SEQUENCE {
      #      null,
      #      encryptionAlgorithm   AlgorithmIdentifier},
      #      encryptedData   OCTET STRING }
      def encode(algorithm, encrypted_data)
        a = OpenSSL::ASN1::ObjectId.new(algorithm)
        null = OpenSSL::ASN1::Null.new(nil)
        oid_sequence = OpenSSL::ASN1::Sequence.new([a, null])
        d = OpenSSL::ASN1::OctetString.new(encrypted_data)
        OpenSSL::ASN1::Sequence.new([oid_sequence, d]).to_der
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keystores-0.1.0 lib/keystores/jks/encrypted_private_key_info.rb