Sha256: f7d96411453e6841be34dbf011280f0ef2f15d45acdf51f4b98e5a2ba0dd7780

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents


module Ccrypto
  module Ruby
    module PEMStore
      include TR::CondUtils
      include DataConversion

      class PEMStoreException < KeyBundleStorageException; end

      module ClassMethods
        def is_pem?(input)
          if is_empty?(input)
            false
          else
            begin
              (input =~ /BEGIN/) != nil
            rescue ArgumentError => ex
              if ex.message =~ /invalid byte sequence/
                false
              else
                raise KeypairEngineException, ex
              end
            end
          end
        end

        def from_pem(input, &block)

          begin
            # try with no password first to check if the keystore is really encrypted
            # If not the library will prompt at command prompt which might halt the flow of program
            pKey = OpenSSL::PKey.read(input,"")
            ECCKeyBundle.new(pKey)
          rescue OpenSSL::PKey::PKeyError => ex
            raise PEMStoreException, "block is required" if not block
            pass = block.call(:store_pass)
            begin
              pKey = OpenSSL::PKey.read(input, pass)
              ECCKeyBundle.new(pKey)
            rescue OpenSSL::PKey::PKeyError => exx
              raise PEMStoreException, exx
            end
          end

        end
      end
      def self.included(klass)
        klass.extend(ClassMethods)
      end

      def to_pem(&block)
        raise PEMStoreException, "Block is required" if not block
        kcipher = block.call(:store_cipher) 
        kpass = block.call(:store_pass)

        kcipher = "AES-256-GCM" if is_empty?(kcipher)

        keypair = block.call(:keypair)
        raise PEMStoreException, "Keypair is required" if is_empty?(keypair)

        if not_empty?(kpass)
          kCipher = OpenSSL::Cipher.new(kcipher)
          keypair.export(kCipher, kpass)
        else
          keypair.export
        end

      end
      
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ccrypto-ruby-0.1.2 lib/ccrypto/ruby/keybundle_store/pem_store.rb
ccrypto-ruby-0.1.1 lib/ccrypto/ruby/keybundle_store/pem_store.rb