Sha256: 5762ba5efae98f1ae8af8cecb23f249f2550078c194ba2ca992f59cc19a63091

Contents?: true

Size: 1.36 KB

Versions: 17

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'openssl'

module BBK
  module Utils
    class Crypt

      def self.full_check(key_path, cert_path, *cacert_chain)
        errors = []
        errors << 'Invalid key and cert pair' unless valid_key_cert?(key_path, cert_path)
        errors << 'Invalid cert and cacert pair' unless valid_cert_sign?(cert_path,
                                                                         *cacert_chain.compact)
        if errors.empty?
          nil
        else
          errors
        end
      end

      def self.valid_key_cert?(key_path, cert_path)
        raise "Key file #{key_path} not exists" unless File.exist? key_path
        raise "Cert file #{cert_path} not exists" unless File.exist? cert_path

        key = OpenSSL::PKey::RSA.new(File.read(key_path))
        cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
        cert.check_private_key(key)
      end

      def self.valid_cert_sign?(cert_path, *ca_certs_paths)
        raise "Cert file #{cert_path} not exists" unless File.exist? cert_path
        raise "Not all files in ca chain #{ca_certs_paths} exists" unless ca_certs_paths.all? {|pth| File.exist? pth }

        store = ca_certs_paths.reduce(OpenSSL::X509::Store.new) {|st, c| st.add_file(c) }
        cert = OpenSSL::X509::Certificate.new File.read(cert_path)
        store.verify(cert)
      end

    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
bbk-utils-1.1.0.274232 lib/bbk/utils/crypt.rb
bbk-utils-1.1.0.273317 lib/bbk/utils/crypt.rb
bbk-utils-1.1.0.181866 lib/bbk/utils/crypt.rb
bbk-utils-1.1.0.149110 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.147183 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.136047 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.133029 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.102369 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.100402 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.100401 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.97958 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.97943 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.93642 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.89770 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.84207 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.72916 lib/bbk/utils/crypt.rb
bbk-utils-1.0.1.72735 lib/bbk/utils/crypt.rb