<commit_before>require 'savon'
require 'nokogiri'
require 'openssl'
require 'base64'
require 'gibberish'

private_key = OpenSSL::PKey::RSA.new File.read 'keys/private.pem'
cert = OpenSSL::X509::Certificate.new File.read 'keys/cert.pem'

def load_soap_request
  f = File.open("xml_templates/get_user_info_soap_request.xml")
  soap_request = Nokogiri::XML(f)
  f.close
  soap_request
end

def load_application_request_signature
  f = File.open("xml_templates/application_request_signature.xml")
  application_request_signature = Nokogiri::XML(f)
  f.close
  application_request_signature
end

def load_soap_request_header
  f = File.open("xml_templates/soap_request_header_template.xml")
  soap_request_header = Nokogiri::XML(f)
  f.close
  soap_request_header
end

def process_application_request
  #Load the application request from template
  f = File.open("xml_templates/get_user_info_application_request.xml")
  application_request = Nokogiri::XML(f)
  f.close

  # Change the customer id of the application request to Nordea's testing ID
  customer_id = application_request.at_css "CustomerId"
  customer_id.content = "11111111"

  #Set the timestamp
  timestamp = application_request.at_css "Timestamp"
  timestamp.content = Time.now

  #Canonicalize the application request
  canon_application_request = application_request.canonicalize
  canon_application_request
end

def sign_application_request(application_request, application_request_signature, private_key, cert)
  #Take digest from application request and set it to the signature
  ### For some reason OpenSSL returns binary hashes so using Gibberish for hashing ###
  #digest = OpenSSL::Digest.new('sha1', application_request)
  #signature_digest = application_request_signature.at_css "DigestValue"
  #signature_digest.content = digest
  digest = Gibberish::SHA1(application_request)
  signature_digest = application_request_signature.at_css "DigestValue"
  signature_digest.content = digest

  #Sign the digest with private key and base64 code it
  digest_sign = OpenSSL::Digest::SHA1.new
  signature = private_key.sign(digest_sign, digest.to_s)
  signature_base64 = Base64.encode64(signature)

  #Add the base64 coded signature to the signature element
  signature_signature = application_request_signature.at_css "SignatureValue"
  signature_signature.content = signature_base64.gsub(/\s+/, "")

  #Format the certificate and add the it to the certificate element
  cert_formatted = cert.to_s.split('-----BEGIN CERTIFICATE-----')[1].split('-----END CERTIFICATE-----')[0].gsub(/\s+/, "")
  signature_certificate = application_request_signature.at_css "X509Certificate"
  signature_certificate.content = cert_formatted

  #Convert application request to XML and add the signature element to it
  application_request_xml  = Nokogiri::XML(application_request)
  software_id = application_request_xml.at_css "SoftwareId"
  software_id.add_next_sibling(application_request_signature.root)

  #Canonicalize the whole application request
  application_request_canon = application_request_xml.canonicalize

  #Base64 code the whole application request
  application_request_base64 = Base64.encode64(application_request_canon)
  application_request_base64

end

def process_soap_request(soap_request, application_request_base64)
  #Add the base64 coded application request to the soap envelope after removing whitespaces
  soap_request_application_request = soap_request.xpath("//mod:ApplicationRequest", 'mod' => 'http://model.bxd.fi').first
  soap_request_application_request.content = application_request_base64.gsub(/\s+/, "")

  #Add the testing sender id
  soap_request_sender_id = soap_request.xpath("//mod:SenderId", 'mod' => 'http://model.bxd.fi').first
  soap_request_sender_id.content = "11111111"

  #Add timestamp
  soap_request_timestamp = soap_request.xpath("//mod:Timestamp", 'mod' => 'http://model.bxd.fi').first
  soap_request_timestamp.content = Time.now

  #Canonicalize the request
  soap_request.canonicalize
end

def sign_soap_request(soap_request, soap_request_header, private_key, cert)
  #Take digest from soap request and put it to the signature
  ### For some reason OpenSSL returns binary hashes so using Gibberish for hashing ###
  #digest = OpenSSL::Digest.new('sha1', soap_request)
  #signature_digest = soap_request_header.xpath("//ds:DigestValue", 'ds' => 'http://www.w3.org/2000/09/xmldsig#').first
  #signature_digest.content = digest
  digest = Gibberish::SHA1(soap_request)
  signature_digest = soap_request_header.xpath("//ds:DigestValue", 'ds' => 'http://www.w3.org/2000/09/xmldsig#').first
  signature_digest.content = digest

  #Sign the digest with private key and base64 code it
  digest_sign = OpenSSL::Digest::SHA1.new
  signature = private_key.sign(digest_sign, digest.to_s)
  signature_base64 = Base64.encode64(signature)

  #Add the base64 coded signature to the signature element
  signature_signature = soap_request_header.xpath("//ds:SignatureValue", 'ds' => 'http://www.w3.org/2000/09/xmldsig#').first
  signature_signature.content = signature_base64.gsub(/\s+/, "")

  #Format the certificate and add the it to the certificate element
  cert_formatted = cert.to_s.split('-----BEGIN CERTIFICATE-----')[1].split('-----END CERTIFICATE-----')[0].gsub(/\s+/, "")<commit_msg>Changed the digest to base64 coded form since that is apparently the correct way.<commit_after>require 'savon'
require 'nokogiri'
require 'openssl'
require 'base64'

private_key = OpenSSL::PKey::RSA.new File.read 'keys/private.pem'
cert = OpenSSL::X509::Certificate.new File.read 'keys/cert.pem'

def load_soap_request
  f = File.open("xml_templates/get_user_info_soap_request.xml")
  soap_request = Nokogiri::XML(f)
  f.close
  soap_request
end

def load_application_request_signature
  f = File.open("xml_templates/application_request_signature.xml")
  application_request_signature = Nokogiri::XML(f)
  f.close
  application_request_signature
end

def load_soap_request_header
  f = File.open("xml_templates/soap_request_header_template.xml")
  soap_request_header = Nokogiri::XML(f)
  f.close
  soap_request_header
end

def process_application_request
  #Load the application request from template
  f = File.open("xml_templates/get_user_info_application_request.xml")
  application_request = Nokogiri::XML(f)
  f.close

  # Change the customer id of the application request to Nordea's testing ID
  customer_id = application_request.at_css "CustomerId"
  customer_id.content = "11111111"

  #Set the timestamp
  timestamp = application_request.at_css "Timestamp"
  timestamp.content = Time.now

  #Canonicalize the application request
  canon_application_request = application_request.canonicalize
  canon_application_request
end

def sign_application_request(application_request, application_request_signature, private_key, cert)
  #Take digest from application request, base64 code it and set it to the signature
  sha1 = OpenSSL::Digest::SHA1.new
  digestbin = sha1.digest(application_request)
  digest = Base64.encode64(digestbin)
  signature_digest = application_request_signature.at_css "DigestValue"
  signature_digest.content = digest

  #Sign the digest with private key and base64 code it
  digest_sign = OpenSSL::Digest::SHA1.new
  signature = private_key.sign(digest_sign, digest.to_s)
  signature_base64 = Base64.encode64(signature)

  #Add the base64 coded signature to the signature element
  signature_signature = application_request_signature.at_css "SignatureValue"
  signature_signature.content = signature_base64.gsub(/\s+/, "")

  #Format the certificate and add the it to the certificate element
  cert_formatted = cert.to_s.split('-----BEGIN CERTIFICATE-----')[1].split('-----END CERTIFICATE-----')[0].gsub(/\s+/, "")
  signature_certificate = application_request_signature.at_css "X509Certificate"
  signature_certificate.content = cert_formatted

  #Convert application request to XML and add the signature element to it
  application_request_xml  = Nokogiri::XML(application_request)
  software_id = application_request_xml.at_css "SoftwareId"
  software_id.add_next_sibling(application_request_signature.root)

  #Canonicalize the whole application request
  application_request_canon = application_request_xml.canonicalize

  #Base64 code the whole application request
  application_request_base64 = Base64.encode64(application_request_canon)
  application_request_base64

end

def process_soap_request(soap_request, application_request_base64)
  #Add the base64 coded application request to the soap envelope after removing whitespaces
  soap_request_application_request = soap_request.xpath("//mod:ApplicationRequest", 'mod' => 'http://model.bxd.fi').first
  soap_request_application_request.content = application_request_base64.gsub(/\s+/, "")

  #Add the testing sender id
  soap_request_sender_id = soap_request.xpath("//mod:SenderId", 'mod' => 'http://model.bxd.fi').first
  soap_request_sender_id.content = "11111111"

  #Add timestamp
  soap_request_timestamp = soap_request.xpath("//mod:Timestamp", 'mod' => 'http://model.bxd.fi').first
  soap_request_timestamp.content = Time.now

  #Canonicalize the request
  soap_request.canonicalize
end

def sign_soap_request(soap_request, soap_request_header, private_key, cert)
  #Take digest from soap request, base64 code it and put it to the signature
  soap_request_xml  = Nokogiri::XML(soap_request)
  soap_request_body = soap_request_xml.xpath("//soapenv:Body", 'soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/').first
  sha1 = OpenSSL::Digest::SHA1.new
  digestbin = sha1.digest(soap_request_body)
  digest = Base64.encode64(digestbin)
  signature_digest = soap_request_header.xpath("//ds:DigestValue", 'ds' => 'http://www.w3.org/2000/09/xmldsig#').first
  signature_digest.content = digest

  #Sign the digest with private key and base64 code it
  digest_sign = OpenSSL::Digest::SHA1.new
  signature = private_key.sign(digest_sign, digest.to_s)
  signature_base64 = Base64.encode64(signature)

  #Add the base64 coded signature to the signature element
  signature_signature = soap_request_header.xpath("//ds:SignatureValue", 'ds' => 'http://www.w3.org/2000/09/xmldsig#').first
  signature_signature.content = signature_base64.gsub(/\s+/, "")

  #Format the certificate and add the it to the certificate element
  cert_formatted = cert.to_s.split('-----BEGIN CERTIFICATE-----')[1].split('-----END CERTIFICATE-----')[0].gsub(/\s+/, "")
{'fraction_non_alphanumeric': 0.10245822930670252, 'fraction_numerical': 0.01910889187632034, 'mean_word_length': 5.608502538071066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 5, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1755373', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3180, 'n_words': 706}