require 'ccrypto/keystore' require_relative 'pkcs12_keystore' require_relative 'jks_keystore' module Ccrypto module Java module Keystore def self.map_keystore_type(key) case key.to_sym when :pkcs12, :p12, :PKCS12, :P12, :Pkcs12 :pkcs12 when :jks, :Jks, :JKS, :java :jks else raise Ccrypto::Keystore::KeystoreException, "Unsupported keystore type '#{key}'" end end def self.load_keystore(bin, eng, &block) mEng = map_keystore_type(eng) case mEng when :pkcs12 PKCS12Keystore.from_p12(bin, &block) when :jks JKSKeystore.from_jks(bin, &block) else raise Ccrypto::Keystore::KeystoreException, "Unsupported keystore type '#{eng}'" end end def self.load_keystore_file(path, eng, &block) raise Ccrypto::Keystore::KeystoreException, "Given keystore path to load '#{path}' does not exist" if not File.exist?(path) raise Ccrypto::Keystore::KeystoreException, "Given keystore path to load '#{path}' cannot be read" if not File.readable?(path) load_keystore(File.read(path), eng, &block) end def self.convert_keystore(bin, fromEng, toEng, &block) srcEng = map_keystore_type(fromEng) mtEng = map_keystore_type(toEng) kp, cert, chain = load_keystore(bin, srcEng) do |opt| case opt when :store_pass block.call(:source_store_pass) else block.call(opt) end end outEng = map_keystore_type(mtEng) case outEng when :pkcs12 PKCS12Keystore.to_p12 do |k| case k when :keypair kp when :cert cert when :cert_chain chain when :store_pass block.call(:dest_store_pass) else block.call(k) end end when :jks JKSKeystore.to_jks do |k| case k when :keypair kp when :cert cert when :cert_chain chain when :store_pass block.call(:dest_store_pass) else block.call(k) end end else raise Ccrypto::Keystore::KeystoreException, "Unsupported keystore type '#{eng}'" end end def self.convert_keystore_file(path, fromEng, toEng, &block) raise Ccrypto::Keystore::KeystoreException, "Given keystore path to load '#{path}' does not exist" if not File.exist?(path) raise Ccrypto::Keystore::KeystoreException, "Given keystore path to load '#{path}' cannot be read" if not File.readable?(path) convert_keystore(File.read(path), fromEng, toEng, &block) end end end end