xml-kit 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa2eb376d5c11a9777e3a3a6073754c9783c73bdc24bbb45c7ac5877e3e99472
4
- data.tar.gz: 6eb275d2a3791889c12a04a5e2b1143807bf4e1ac1968491f93ef32faeccd868
3
+ metadata.gz: 03f1eb698452e6bde1528e6899c935996309a1b0e4fdec3995ad04d8401b71e4
4
+ data.tar.gz: d52d39605ff43af034bf4a913faa35757a34d2abc82f9aca970683132015229d
5
5
  SHA512:
6
- metadata.gz: 0447e7510327c68ee2f9f500b980d1d36255b044a3825dfd504245ea111eb367f998a0bc6a507d54e5f64472de76a2766460085c808b8ca380254fdbe12feaa0
7
- data.tar.gz: 7f9c69571ddd52a638daf3b20062cc4b2d06cfd7cd83f6a517405330daddc8c8bb8d62fbd054b02ad8ad1eda6a9229ef0221f97d90b4d4ee3318420175eb3405
6
+ metadata.gz: 91ab2b253526d12fc2495afc4261b4ceb8861d4644dd0ae5fcea226d7413df058001cdb6f8e6cb40a63254bcb6546f9a86fd5e9472e41818a1003ac5834442e0
7
+ data.tar.gz: ac71bd4d5416fadfa85763ed40839cd0e5e33ff2e4fd05ea98e3cdc8d0e29225e528614b33e7cbb1a8ac77b171e991973875b9cf7a53dc01817024d169f2dd4a
@@ -14,6 +14,20 @@ module Xml
14
14
  def self.cipher_for(algorithm, key)
15
15
  CIPHERS.find { |x| x.matches?(algorithm) }.new(algorithm, key)
16
16
  end
17
+
18
+ def self.cipher_registry(&block)
19
+ BlockRegistry.new(&block)
20
+ end
21
+
22
+ class BlockRegistry
23
+ def initialize(&factory)
24
+ @factory = factory
25
+ end
26
+
27
+ def cipher_for(algorithm, key)
28
+ @factory.call(algorithm, key)
29
+ end
30
+ end
17
31
  end
18
32
  end
19
33
  end
@@ -12,11 +12,12 @@ module Xml
12
12
  "#{::Xml::Kit::Namespaces::XMLENC}aes256-cbc" => 'AES-256-CBC',
13
13
  }.freeze
14
14
 
15
- attr_reader :key
15
+ attr_reader :algorithm, :key, :padding
16
16
 
17
- def initialize(algorithm, key = nil)
17
+ def initialize(algorithm, key = nil, padding = nil)
18
18
  @algorithm = algorithm
19
19
  @key = key || cipher.random_key
20
+ @padding = padding
20
21
  end
21
22
 
22
23
  def self.matches?(algorithm)
@@ -30,19 +31,30 @@ module Xml
30
31
  end
31
32
 
32
33
  def decrypt(cipher_text)
34
+ result = default_decrypt(
35
+ cipher_text[0...cipher.iv_len],
36
+ cipher_text[cipher.iv_len..-1]
37
+ )
38
+ return result if padding.nil?
39
+
40
+ padding_size = result.bytes.last
41
+ result[0...-padding_size]
42
+ end
43
+
44
+ protected
45
+
46
+ def default_decrypt(initialization_vector, data)
33
47
  cipher.decrypt
34
- iv = cipher_text[0..cipher.iv_len - 1]
35
- data = cipher_text[cipher.iv_len..-1]
36
- # cipher.padding = 0
48
+ cipher.padding = padding unless padding.nil?
37
49
  cipher.key = @key
38
- cipher.iv = iv
39
- cipher.update(data) + cipher.final
50
+ cipher.iv = initialization_vector
51
+ cipher.update(data) << cipher.final
40
52
  end
41
53
 
42
54
  private
43
55
 
44
56
  def cipher
45
- @cipher ||= OpenSSL::Cipher.new(ALGORITHMS[@algorithm])
57
+ @cipher ||= OpenSSL::Cipher.new(ALGORITHMS[algorithm])
46
58
  end
47
59
  end
48
60
  end
@@ -5,10 +5,11 @@ module Xml
5
5
  # {include:file:spec/saml/xml_decryption_spec.rb}
6
6
  class Decryption
7
7
  # The list of private keys to use to attempt to decrypt the document.
8
- attr_reader :private_keys
8
+ attr_reader :cipher_registry, :private_keys
9
9
 
10
- def initialize(private_keys:)
10
+ def initialize(private_keys:, cipher_registry: ::Xml::Kit::Crypto)
11
11
  @private_keys = private_keys
12
+ @cipher_registry = cipher_registry
12
13
  end
13
14
 
14
15
  # Decrypts an EncryptedData section of an XML document.
@@ -31,11 +32,11 @@ module Xml
31
32
  # @param hash [Hash] the XML document converted to a [Hash] using Hash.from_xml.
32
33
  def decrypt_hash(hash)
33
34
  encrypted_data = hash['EncryptedData']
34
- symmetric_key = symmetric_key_from(encrypted_data)
35
- cipher_value = encrypted_data['CipherData']['CipherValue']
36
- cipher_text = Base64.decode64(cipher_value)
37
- algorithm = encrypted_data['EncryptionMethod']['Algorithm']
38
- to_plaintext(cipher_text, symmetric_key, algorithm)
35
+ to_plaintext(
36
+ Base64.decode64(encrypted_data['CipherData']['CipherValue']),
37
+ symmetric_key_from(encrypted_data),
38
+ encrypted_data['EncryptionMethod']['Algorithm']
39
+ )
39
40
  end
40
41
 
41
42
  # Decrypts an EncryptedData Nokogiri::XML::Element.
@@ -62,8 +63,8 @@ module Xml
62
63
  raise DecryptionError, private_keys
63
64
  end
64
65
 
65
- def to_plaintext(cipher_text, symmetric_key, algorithm)
66
- Crypto.cipher_for(algorithm, symmetric_key).decrypt(cipher_text)
66
+ def to_plaintext(cipher_text, private_key, algorithm)
67
+ cipher_registry.cipher_for(algorithm, private_key).decrypt(cipher_text)
67
68
  end
68
69
  end
69
70
  end
@@ -15,11 +15,12 @@ module Xml
15
15
  asymmetric_algorithm: ::Xml::Kit::Crypto::RsaCipher::ALGORITHM
16
16
  )
17
17
  @symmetric_algorithm = symmetric_algorithm
18
- @symmetric_cipher_value = Base64.encode64(symmetric_cipher.encrypt(raw_xml)).delete("\n")
18
+ symmetric_cipher = symmetric(symmetric_algorithm)
19
+ @symmetric_cipher_value = Base64.strict_encode64(symmetric_cipher.encrypt(raw_xml))
19
20
 
20
21
  @asymmetric_algorithm = asymmetric_algorithm
21
- cipher = Crypto.cipher_for(asymmetric_algorithm, public_key)
22
- @asymmetric_cipher_value = Base64.encode64(cipher.encrypt(symmetric_cipher.key)).delete("\n")
22
+ asymmetric_cipher = asymmetric(asymmetric_algorithm, public_key)
23
+ @asymmetric_cipher_value = Base64.strict_encode64(asymmetric_cipher.encrypt(symmetric_cipher.key))
23
24
  end
24
25
 
25
26
  def to_xml(xml: ::Builder::XmlMarkup.new)
@@ -28,10 +29,16 @@ module Xml
28
29
 
29
30
  private
30
31
 
31
- def symmetric_cipher
32
- @symmetric_cipher ||= ::Xml::Kit::Crypto::SymmetricCipher.new(
33
- symmetric_algorithm
34
- )
32
+ def symmetric(algorithm)
33
+ return algorithm unless algorithm.is_a?(String)
34
+
35
+ ::Xml::Kit::Crypto::SymmetricCipher.new(algorithm)
36
+ end
37
+
38
+ def asymmetric(algorithm, public_key)
39
+ return algorithm unless algorithm.is_a?(String)
40
+
41
+ ::Xml::Kit::Crypto.cipher_for(algorithm, public_key)
35
42
  end
36
43
  end
37
44
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Xml
4
4
  module Kit
5
- VERSION = '0.1.14'.freeze
5
+ VERSION = '0.2.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo khan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-21 00:00:00.000000000 Z
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel