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 +4 -4
- data/lib/xml/kit/crypto.rb +14 -0
- data/lib/xml/kit/crypto/symmetric_cipher.rb +20 -8
- data/lib/xml/kit/decryption.rb +10 -9
- data/lib/xml/kit/encryption.rb +14 -7
- data/lib/xml/kit/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03f1eb698452e6bde1528e6899c935996309a1b0e4fdec3995ad04d8401b71e4
|
4
|
+
data.tar.gz: d52d39605ff43af034bf4a913faa35757a34d2abc82f9aca970683132015229d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ab2b253526d12fc2495afc4261b4ceb8861d4644dd0ae5fcea226d7413df058001cdb6f8e6cb40a63254bcb6546f9a86fd5e9472e41818a1003ac5834442e0
|
7
|
+
data.tar.gz: ac71bd4d5416fadfa85763ed40839cd0e5e33ff2e4fd05ea98e3cdc8d0e29225e528614b33e7cbb1a8ac77b171e991973875b9cf7a53dc01817024d169f2dd4a
|
data/lib/xml/kit/crypto.rb
CHANGED
@@ -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
|
-
|
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 =
|
39
|
-
cipher.update(data)
|
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[
|
57
|
+
@cipher ||= OpenSSL::Cipher.new(ALGORITHMS[algorithm])
|
46
58
|
end
|
47
59
|
end
|
48
60
|
end
|
data/lib/xml/kit/decryption.rb
CHANGED
@@ -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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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,
|
66
|
-
|
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
|
data/lib/xml/kit/encryption.rb
CHANGED
@@ -15,11 +15,12 @@ module Xml
|
|
15
15
|
asymmetric_algorithm: ::Xml::Kit::Crypto::RsaCipher::ALGORITHM
|
16
16
|
)
|
17
17
|
@symmetric_algorithm = symmetric_algorithm
|
18
|
-
|
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
|
-
|
22
|
-
@asymmetric_cipher_value = Base64.
|
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
|
32
|
-
|
33
|
-
|
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
|
data/lib/xml/kit/version.rb
CHANGED
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.
|
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
|
+
date: 2018-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|