Commit 7101c05

mo <mo.khan@gmail.com>
2017-12-26 21:03:15
fix specs in xml-kit.
1 parent 3389355
xml-kit/lib/xml/kit/crypto/simple_cipher.rb
@@ -26,10 +26,6 @@ module Xml
           #cipher.padding = 0
           cipher.key = @private_key
           cipher.iv = iv
-
-          Saml::Kit.logger.debug ['-key', @private_key].inspect
-          Saml::Kit.logger.debug ['-iv', iv].inspect
-
           cipher.update(data) + cipher.final
         end
       end
xml-kit/lib/xml/kit/xml_decryption.rb
@@ -30,7 +30,7 @@ module Xml
             attempts -= 1
             return to_plaintext(cipher_text, private_key, encrypted_key["EncryptionMethod"]['Algorithm'])
           rescue OpenSSL::PKey::RSAError => error
-            Saml::Kit.logger.error(error)
+            #Saml::Kit.logger.error(error)
             raise if attempts.zero?
           end
         end
xml-kit/lib/xml/kit.rb
@@ -1,5 +1,7 @@
-require "xml/kit/version"
+require "base64"
+require "openssl"
 
+require "xml/kit/version"
 require "xml/kit/crypto"
 require "xml/kit/xml_decryption"
 
xml-kit/spec/support/certificate_helper.rb
@@ -0,0 +1,19 @@
+module CertificateHelper
+  def generate_key_pair(passphrase)
+    rsa_key = OpenSSL::PKey::RSA.new(2048)
+    public_key = rsa_key.public_key
+    certificate = OpenSSL::X509::Certificate.new
+    subject="/C=CA/ST=Alberta/L=Calgary/O=XmlKit/OU=XmlKit/CN=XmlKit"
+    certificate.subject = certificate.issuer = OpenSSL::X509::Name.parse(subject)
+    certificate.not_before = Time.now.to_i
+    certificate.not_after = (Date.today + 30).to_time.to_i
+    certificate.public_key = public_key
+    certificate.serial = 0x0
+    certificate.version = 2
+    certificate.sign(rsa_key, OpenSSL::Digest::SHA256.new)
+    [
+      certificate.to_pem,
+      rsa_key.to_pem(OpenSSL::Cipher.new('AES-256-CBC'), passphrase)
+    ]
+  end
+end
xml-kit/spec/xml/kit_spec.rb
@@ -2,8 +2,4 @@ RSpec.describe Xml::Kit do
   it "has a version number" do
     expect(Xml::Kit::VERSION).not_to be nil
   end
-
-  it "does something useful" do
-    expect(false).to eq(true)
-  end
 end
xml-kit/spec/xml/xml_decryption_spec.rb
@@ -4,7 +4,8 @@ RSpec.describe Xml::Kit::XmlDecryption do
     let(:password) { FFaker::Movie.title }
 
     it 'decrypts the data' do
-      certificate_pem, private_key_pem = Saml::Kit::SelfSignedCertificate.new(password).create
+      certificate_pem, private_key_pem = generate_key_pair(password)
+
       public_key = OpenSSL::X509::Certificate.new(certificate_pem).public_key
       private_key = OpenSSL::PKey::RSA.new(private_key_pem, password)
 
@@ -44,7 +45,7 @@ RSpec.describe Xml::Kit::XmlDecryption do
     end
 
     it 'attemps to decrypt with each encryption keypair' do
-      certificate_pem, private_key_pem = Saml::Kit::SelfSignedCertificate.new(password).create
+      certificate_pem, private_key_pem = generate_key_pair(password)
       public_key = OpenSSL::X509::Certificate.new(certificate_pem).public_key
       private_key = OpenSSL::PKey::RSA.new(private_key_pem, password)
 
@@ -79,7 +80,7 @@ RSpec.describe Xml::Kit::XmlDecryption do
         }
       }
 
-      _, other_private_key_pem = Saml::Kit::SelfSignedCertificate.new(password).create
+      _, other_private_key_pem = generate_key_pair(password)
       other_private_key = OpenSSL::PKey::RSA.new(other_private_key_pem, password)
 
       subject = described_class.new(configuration: double(private_keys: [other_private_key, private_key]))
@@ -88,7 +89,7 @@ RSpec.describe Xml::Kit::XmlDecryption do
     end
 
     it 'raise an error when it cannot decrypt the data' do
-      certificate_pem, _ = Saml::Kit::SelfSignedCertificate.new(password).create
+      certificate_pem, _ = generate_key_pair(password)
       public_key = OpenSSL::X509::Certificate.new(certificate_pem).public_key
 
       cipher = OpenSSL::Cipher.new('AES-128-CBC')
@@ -122,7 +123,7 @@ RSpec.describe Xml::Kit::XmlDecryption do
         }
       }
 
-      new_private_key_pem = Saml::Kit::SelfSignedCertificate.new(password).create[1]
+      new_private_key_pem = generate_key_pair(password)[1]
       new_private_key = OpenSSL::PKey::RSA.new(new_private_key_pem, password)
       subject = described_class.new(configuration: double(private_keys: [new_private_key]))
       expect do
xml-kit/spec/spec_helper.rb
@@ -1,6 +1,9 @@
 require "bundler/setup"
 require "xml/kit"
+require "ffaker"
+#require "active_support/testing/time_helpers"
 
+Dir[File.join(Dir.pwd, 'spec/support/**/*.rb')].each { |f| require f }
 RSpec.configure do |config|
   # Enable flags like --only-failures and --next-failure
   config.example_status_persistence_file_path = ".rspec_status"
@@ -11,4 +14,5 @@ RSpec.configure do |config|
   config.expect_with :rspec do |c|
     c.syntax = :expect
   end
+  config.include CertificateHelper
 end
xml-kit/xml-kit.gemspec
@@ -21,7 +21,9 @@ Gem::Specification.new do |spec|
   spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
   spec.require_paths = ["lib"]
 
+  spec.add_development_dependency "activesupport", ">= 4.2.0"
   spec.add_development_dependency "bundler", "~> 1.16"
+  spec.add_development_dependency "ffaker", "~> 2.7"
   spec.add_development_dependency "rake", "~> 10.0"
   spec.add_development_dependency "rspec", "~> 3.0"
 end