<commit_before>require 'hiera/backend/eyaml/encryptor'
require 'hiera/backend/eyaml/utils'
require 'hiera/backend/eyaml/options'
require "rubygems"
require "pkcs11"

class Hiera
  module Backend
    module Eyaml
      module Encryptors
        class Pkcs11 < Encryptor
          include PKCS11
          self.options = {

            :slot_id => { :desc    => "The slot to use for the session",
                            :type    => :integer,
                            :default => 4 },

            :key_label => { :desc    => "The label of the public/private key to use",
                            :type    => :string,
                            :default => "badkeyname" },

            :offline => { :desc    => "Work in offline mode using offline publickey",
                          :type    => :boolean,
                          :default => false },

            :offline_publickey => { :desc    => "Local path to the Public key used in offline mode",
                                    :type    => :string,
                                    :default => "/etc/puppetlabs/puppet/ssl/keys/pkcs11.publickey.pem" },


            :hsm_library => { :desc    => "HSM Shared object library path",
                              :type    => :string,
                              :default => "/opt/nfast/toolkits/pkcs11/libcknfast.so" },


            :hsm_usertype => { :desc    => "HSM Softcard user type CKU_<foo>",
                               :type    => :string,
                               :default => "#{:USER}" },

            :hsm_password => { :desc    => "HSM Softcard Password",
                               :type    => :string,
                               :default => "<PASSWORD>" },
          }

          self.tag = "PKCS11"

          def self.encrypt plaintext
             self.session(:encrypt,plaintext)
          end

          def self.decrypt ciphertext
             self.session(:decrypt,ciphertext)
          end

          def self.session(action,text)

            hsm_usertype  = self.option :hsm_usertype
            hsm_password  = self.option :hsm_password
            hsm_library   = self.option :hsm_library
	    key_label     = self.option :key_label
            slot_id       = self.option :slot_id

            pkcs11 = PKCS11.open(hsm_library)

            # Convert slotid from Human to array value
            pkcs11.active_slots[(slot_id - 1 )].open do |session|
              session.login(hsm_usertype,hsm_password)
              puts session.find_objects(:CLASS => PKCS11::CKO_PUBLIC_KEY).first[:LABEL]
              public_key  = session.find_objects(:CLASS => PKCS11::CKO_PUBLIC_KEY).select { |obj| obj[:LABEL] == key_label}.first
              private_key = session.find_objects(:CLASS => PKCS11::CKO_PRIVATE_KEY).select { |obj| obj[:LABEL] == key_label}.first

              raise "No public key found for label #{key_label}" if public_key.nil?
              raise "No private key found for label #{key_label}" if private_key.nil?

              if action == :encrypt
                result = session.encrypt(:RSA_PKCS,public_key,text)
              elsif action == :decrypt
                result = session.decrypt(:RSA_PKCS,private_key,text)
              end
              session.logout
              result
            end<commit_msg>Move include and require for PE 3.4<commit_after>require 'hiera/backend/eyaml/encryptor'
require 'hiera/backend/eyaml/utils'
require 'hiera/backend/eyaml/options'
require "rubygems"

class Hiera
  module Backend
    module Eyaml
      module Encryptors
        class Pkcs11 < Encryptor
          self.options = {

            :slot_id => { :desc    => "The slot to use for the session",
                            :type    => :integer,
                            :default => 4 },

            :key_label => { :desc    => "The label of the public/private key to use",
                            :type    => :string,
                            :default => "badkeyname" },

            :offline => { :desc    => "Work in offline mode using offline publickey",
                          :type    => :boolean,
                          :default => false },

            :offline_publickey => { :desc    => "Local path to the Public key used in offline mode",
                                    :type    => :string,
                                    :default => "/etc/puppetlabs/puppet/ssl/keys/pkcs11.publickey.pem" },


            :hsm_library => { :desc    => "HSM Shared object library path",
                              :type    => :string,
                              :default => "/opt/nfast/toolkits/pkcs11/libcknfast.so" },


            :hsm_usertype => { :desc    => "HSM Softcard user type CKU_<foo>",
                               :type    => :string,
                               :default => "USER" },

            :hsm_password => { :desc    => "HSM Softcard Password",
                               :type    => :string,
                               :default => "<PASSWORD>" },
          }

          self.tag = "PKCS11"

          def self.encrypt plaintext
             self.session(:encrypt,plaintext)
          end

          def self.decrypt ciphertext
             self.session(:decrypt,ciphertext)
          end

          def self.session(action,text)

            require "pkcs11"
            include PKCS11

            hsm_usertype  = self.option :hsm_usertype
            hsm_password  = self.option :hsm_password
            hsm_library   = self.option :hsm_library
	    key_label     = self.option :key_label
            slot_id       = self.option :slot_id

            pkcs11 = PKCS11.open(hsm_library)

            # Convert slotid from Human to array value
            pkcs11.active_slots[(slot_id - 1 )].open do |session|
              session.login(hsm_usertype,hsm_password)
              puts session.find_objects(:CLASS => PKCS11::CKO_PUBLIC_KEY).first[:LABEL]
              public_key  = session.find_objects(:CLASS => PKCS11::CKO_PUBLIC_KEY).select { |obj| obj[:LABEL] == key_label}.first
              private_key = session.find_objects(:CLASS => PKCS11::CKO_PRIVATE_KEY).select { |obj| obj[:LABEL] == key_label}.first

              raise "No public key found for label #{key_label}" if public_key.nil?
              raise "No private key found for label #{key_label}" if private_key.nil?

              if action == :encrypt
                result = session.encrypt(:RSA_PKCS,public_key,text)
              elsif action == :decrypt
                result = session.decrypt(:RSA_PKCS,private_key,text)
              end
              session.logout
              result
            end
{'fraction_non_alphanumeric': 0.10191178684329369, 'fraction_numerical': 0.008128857443925937, 'mean_word_length': 1.4800298618887644, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1779234', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1807, 'n_words': 413}