xmlenc 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/xmlenc.rb +25 -0
- data/lib/xmlenc/algorithms/aes_cbc.rb +38 -0
- data/lib/xmlenc/algorithms/des3_cbc.rb +31 -0
- data/lib/xmlenc/algorithms/rsa_15.rb +13 -0
- data/lib/xmlenc/algorithms/rsa_oaep_mgf1p.rb +25 -0
- data/lib/xmlenc/encrypted_data.rb +44 -0
- data/lib/xmlenc/encrypted_document.rb +28 -0
- data/lib/xmlenc/encrypted_key.rb +53 -0
- data/lib/xmlenc/version.rb +3 -0
- data/spec/fixtures/encrypted_document.xml +61 -0
- data/spec/fixtures/key.pem +27 -0
- data/spec/fixtures/phaos/enc-content-aes256-kt-rsa1_5.xml +63 -0
- data/spec/fixtures/phaos/enc-element-3des-kt-rsa_oaep_sha1.xml +63 -0
- data/spec/fixtures/phaos/enc-element-aes128-kt-rsa1_5.xml +61 -0
- data/spec/fixtures/phaos/enc-element-aes128-kt-rsa_oaep_sha1.xml +63 -0
- data/spec/fixtures/phaos/enc-text-aes256-kt-rsa_oaep_sha1.xml +67 -0
- data/spec/fixtures/phaos/key.txt +117 -0
- data/spec/fixtures/phaos/payment.xml +9 -0
- data/spec/fixtures/phaos/rsa-priv-key.pem +27 -0
- data/spec/lib/xmlenc/.keep +0 -0
- data/spec/lib/xmlenc/encrypted_data_spec.rb +80 -0
- data/spec/lib/xmlenc/encrypted_document_spec.rb +31 -0
- data/spec/lib/xmlenc/encrypted_key_spec.rb +98 -0
- data/spec/lib/xmlenc/phaos_compat_spec.rb +114 -0
- data/spec/spec_helper.rb +20 -0
- data/xmlenc.gemspec +25 -0
- metadata +148 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
-----BEGIN RSA PRIVATE KEY-----
|
2
|
+
MIIEogIBAAKCAQEAgj3TOyUtgg99oEfsm8h9JTZBxUkzYkXVUOHxIwnkFwp4y9Zn
|
3
|
+
rGja/j+kpRyKvYP5CkNdq0e58/r7GLXj45iqd03XjsFNTdjy4OIOgf7JxMG7z+hE
|
4
|
+
B1LT2swTs10GILFWPByRl3/BEsnekLZdoqNoJrvnttVkxgu3x80Ji3/AZD8Ub/kB
|
5
|
+
GOSPyu6pn3OdnMTc5q4r1qUe985lQzCZvCMw6AoGeCyJodNu2MbveNeH+YPjRgLC
|
6
|
+
QfzvOFRq+9qMtE8XfUJZdNhPZhgdsOGf8uJauTcIHbAyw7BhxPy6RikWW5yiWUmB
|
7
|
+
ya+7t4y1TQJzham/0y0zU3TAA7b/rDrU7xmNPwIDAQABAoIBAFK+PIiC4hqTBNjj
|
8
|
+
WOrPwNH3WmmgS8jPXOp54NzF1+bbfErj+BGMvDRy8oMDUxF72qgujD/Y3canWQcl
|
9
|
+
55Yc04/gIGZNYHNotUUx2M21tTIPcuZvRWjxsi57ILj/DHmQsJyX+dcqDtuE7KU7
|
10
|
+
dtlwvyZ8koWRpOg9YZDKLpo1m2ET0RjyNEm9fvt6m3ZwSawA9Yu0xSVvVIukNIl7
|
11
|
+
eimQzBYeGQwGwhMgMoybQWjwIOpnbvcRfT/iAh9n/AyXcjtOMGwWhifjel+U1XMM
|
12
|
+
hrcL3mA5xb+gGQpZ7TToTfmOEJeVCh9BRKpgniH4JVClhiTOeN4VTLZYg/BGmGTj
|
13
|
+
rEZKX4ECgYEAigDXtsEns9+QVsQKYolI/GE0Edkd1OkyGw11N3YIMNNzOotcDKoU
|
14
|
+
0/Jt1hI7NaWRgi9/LyphmEM/dBaNnrNW6GPIETSjUQ9FuK2LywkNUiM2D+UFsLlq
|
15
|
+
EJ4S50/7kdXOC3t9rm9tiIPyqwJKNxRmyAAKfiRbYiEtqqR5zdGXcBECgYEA8ZoG
|
16
|
+
xTcXI7pqmkWcI87siHhth1jFCGv3EMwAeIy4tk4HBkxjZoBy/hU2mxviYPhuFEPj
|
17
|
+
JTKEbiQRebVLwaTJxs543deGQ1shdVHT1005wyGyhuIVXYC56Xhu+8B2y32650/X
|
18
|
+
wRTSUXxP/eQ+Qb2MeDPZ0XgpHhU74Za4F812eE8CgYABNwqvKDoyQjiiGu3Aelbz
|
19
|
+
KePseE3j7v2q8U5j450k2Oe9zzZLQkAWsZ638McmrMOAMuzavHPJhGYNnpk2mXud
|
20
|
+
Zit/w0fg0dKaUqTVb8n3PCogr7KCIM/HP60I7lJXsVs0DK1JmN+NASRkzwtaOsrA
|
21
|
+
3gc5nxZS4dnmE7ai0kKUEQKBgEfA/xHDctPhoZd/5QedhRJi8eSosJv3tMEAVYN4
|
22
|
+
B8PdGKj+NudSbtSXaEhY17sFDWxnXSbHAh3UCHylI4K3dlXdLHh2ciG/BwjY7Jmd
|
23
|
+
DfKuJ1939fB3Mr/sTMnoN1/oT4IzbFij3nLlALze9L6BlR5+B8r2bP/KPqaZJMOs
|
24
|
+
+epxAoGAdV6yhdRcUrSBg6e8Q2f1or7LeYzi6xWjf1RRjqPOgf6VB/xiMmF6RnZK
|
25
|
+
uRUzK9ieRnAhtDFJTS1nPbfVLPSsASzxAaGQGdZ3AsHD4jp03jCyKtBaPKdDzeVr
|
26
|
+
rrIYPcyZ4OBeoFWUvzlDEtEcqJc3ONwpNWal7nhKEbb/AjqFmpg=
|
27
|
+
-----END RSA PRIVATE KEY-----
|
File without changes
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Xmlenc::EncryptedData do
|
4
|
+
let(:encrypted_xml) { File.read('spec/fixtures/encrypted_document.xml') }
|
5
|
+
let(:doc) { Nokogiri::XML::Document.parse(encrypted_xml) }
|
6
|
+
let(:encrypted_data_node) { doc.at_xpath('//xenc:EncryptedData', Xmlenc::NAMESPACES) }
|
7
|
+
let(:key) { %w(ba1407b67c847b0a85a33c93286c401d).pack('H*') }
|
8
|
+
subject { described_class.new(encrypted_data_node) }
|
9
|
+
|
10
|
+
describe 'document' do
|
11
|
+
it 'returns the nokogiri document' do
|
12
|
+
expect(subject.document).to be_a(Nokogiri::XML::Document)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'encryption_method' do
|
17
|
+
it 'returns the encryption method' do
|
18
|
+
expect(subject.encryption_method).to be == encrypted_data_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'cipher_value' do
|
23
|
+
it 'returns the cipher value' do
|
24
|
+
expect(subject.cipher_value).to be == <<-CV.gsub(/[\n\s]/, '')
|
25
|
+
u2vogkwlvFqeknJ0lYTBZkWS/eX8LR1fDPFMfyK1/UY0EyZfHvbONfDHcC/HLv/f
|
26
|
+
aAOOO2Y0GqsknP0LYT1OznkiJrzx134cmJCgbyrYXd3Mp21Pq3rs66JJ34Qt3/+I
|
27
|
+
EyJBUSMT8TdT3fBD44BtOqH2op/hy2g3hQPFZul4GiHBEnNJL/4nU1yad3bMvtAB
|
28
|
+
mzhx80lJvPGLcruj5V77WMvkvZfoeEqMq4qPWK02ZURsJsq0iZcJDi39NB7OCiON
|
29
|
+
CV
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'decrypt' do
|
34
|
+
describe 'aes128-cbc' do
|
35
|
+
it 'replaces returns the decrypted value' do
|
36
|
+
expect(subject.decrypt(key).gsub(/[\n\s]/, '')).to be == <<-XML.gsub(/[\n\s]/, '')
|
37
|
+
<CreditCard Currency="USD" Limit="5,000">
|
38
|
+
<Number>4019 2445 0277 5567</Number>
|
39
|
+
<Issuer>Bank of the Internet</Issuer>
|
40
|
+
<Expiration Time="04/02"/>
|
41
|
+
</CreditCard>
|
42
|
+
XML
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'aes256-cbc' do
|
47
|
+
it 'replaces returns the decrypted value' do
|
48
|
+
encrypted_data_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES).replace <<-XML
|
49
|
+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"></EncryptionMethod>
|
50
|
+
XML
|
51
|
+
subject.stub(:cipher_value) { 'DpNYC0Np5hHaQAUyHWpM3MQ99wkDFtGRc7TywqxmhI4sJKDXM5SRjVlKf6st5wOz' }
|
52
|
+
key = %w(b0621c35317af207b92e3a6b317a122a93772a7261e3f13a4297eb64a91af10a).pack('H*')
|
53
|
+
|
54
|
+
expect(subject.decrypt(key)).to be == '4019 2445 0277 5567'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'des3-cbc' do
|
59
|
+
it 'replaces returns the decrypted value' do
|
60
|
+
encrypted_data_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES).replace <<-XML
|
61
|
+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"></EncryptionMethod>
|
62
|
+
XML
|
63
|
+
subject.stub(:cipher_value) { 'kY6scZxpyRXQbaDZp+LbuvSFYgmI3pQrfsrCVt3/9sZzpeUTPXJEatQ5KPOXYpJC
|
64
|
+
Gid01h/T8PIezic0Ooz/jU+r3kYMKesMYiXin4CXTZYcGhd0TjmOd4kg1vlhE8kt
|
65
|
+
WLC7JDzFLPAqXbOug3ghmWunFiUETbGJaF5V4AHIoZrYP+RS3DTLgJcATuDeWyOd
|
66
|
+
ueqnLefXiCDNqgSTsK4OyNlX0fpUJgKbL+Mhf5vsqxyIqDsS/p6cRA==' }
|
67
|
+
|
68
|
+
key = %w(3219e991eccd9186bf75a83ef8982fd0df4558fd1a837aa2).pack('H*')
|
69
|
+
|
70
|
+
expect(subject.decrypt(key).gsub(/[\n\s]/, '')).to be == <<-XML.gsub(/[\n\s]/, '')
|
71
|
+
<CreditCard Currency="USD" Limit="5,000">
|
72
|
+
<Number>4019 2445 0277 5567</Number>
|
73
|
+
<Issuer>Bank of the Internet</Issuer>
|
74
|
+
<Expiration Time="04/02"/>
|
75
|
+
</CreditCard>
|
76
|
+
XML
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Xmlenc::EncryptedDocument do
|
4
|
+
let(:plain_xml) { File.read('spec/fixtures/phaos/payment.xml') }
|
5
|
+
let(:encrypted_xml) { File.read('spec/fixtures/encrypted_document.xml') }
|
6
|
+
let(:private_key) { OpenSSL::PKey::RSA.new(File.read('spec/fixtures/phaos/rsa-priv-key.pem')) }
|
7
|
+
subject { described_class.new(encrypted_xml) }
|
8
|
+
|
9
|
+
describe 'document' do
|
10
|
+
it 'returns the nokogiri document' do
|
11
|
+
expect(subject.document).to be_a(Nokogiri::XML::Document)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'encrypted_keys' do
|
16
|
+
it 'returns the encrypted keys' do
|
17
|
+
expect(subject.encrypted_keys.count).to be == 1
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'converts the elements to EncryptedKey' do
|
21
|
+
all_converted = subject.encrypted_keys.all? { |ek| ek.is_a?(Xmlenc::EncryptedKey) }
|
22
|
+
expect(all_converted).to be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'decrypt' do
|
27
|
+
it 'replaces the encrypted data with the unencrypted elements' do
|
28
|
+
expect(subject.decrypt(private_key).chomp).to be == plain_xml
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Xmlenc::EncryptedKey do
|
4
|
+
let(:encrypted_xml) { File.read('spec/fixtures/encrypted_document.xml') }
|
5
|
+
let(:doc) { Nokogiri::XML::Document.parse(encrypted_xml) }
|
6
|
+
let(:encrypted_key_node) { doc.at_xpath('//xenc:EncryptedKey', Xmlenc::NAMESPACES) }
|
7
|
+
let(:private_key) { OpenSSL::PKey::RSA.new(File.read('spec/fixtures/phaos/rsa-priv-key.pem')) }
|
8
|
+
subject { described_class.new(encrypted_key_node) }
|
9
|
+
|
10
|
+
describe 'document' do
|
11
|
+
it 'returns the nokogiri document' do
|
12
|
+
expect(subject.document).to be_a(Nokogiri::XML::Document)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'encryption_method' do
|
17
|
+
it 'returns the encryption method' do
|
18
|
+
expect(subject.encryption_method).to be == encrypted_key_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'cipher_value' do
|
23
|
+
it 'returns the cipher value' do
|
24
|
+
expect(subject.cipher_value).to be == <<-CV.gsub(/[\n\s]/, '')
|
25
|
+
cCxxYh3xGBTqlXbhmKxWzNMlHeE28E7vPrMyM5V4T+t1Iy2csj1BoQ7cqBjEhqEy
|
26
|
+
Eot4WNRYsY7P44mWBKurj2mdWQWgoxHvtITP9AR3JTMxUo3TF5ltW76DLDsEvWlE
|
27
|
+
uZKam0PYj6lYPKd4npUULeZyR/rDRrth/wFIBD8vbQlUsBHapNT9MbQfSKZemOuT
|
28
|
+
UJL9PNgsosySpKrX564oQw398XsxfTFxi4hqbdqzA/CLL418X01hUjIHdyv6XnA2
|
29
|
+
98Bmfv9WMPpX05udR4raDv5X8NWxjH00hAhasM3qumxoyCT6mAGfqvE23I+OXtrN
|
30
|
+
lUvE9mMjANw4zweCHsOcfw==
|
31
|
+
CV
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'encrypted_data' do
|
36
|
+
it 'returns the encrypted data element' do
|
37
|
+
expect(subject.encrypted_data).to be_a(Xmlenc::EncryptedData)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'decrypt' do
|
42
|
+
describe 'with rsa 15' do
|
43
|
+
it 'returns the decrypted key' do
|
44
|
+
expect(subject.decrypt(private_key)).to be == ['ba1407b67c847b0a85a33c93286c401d'].pack('H*')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'with rsa oaep' do
|
49
|
+
before :each do
|
50
|
+
subject.stub(:cipher_value).and_return <<-CV.gsub(/[\n\s]/, '')
|
51
|
+
ZF0JPSfv75/8M+O2O/xi+8N1b9KT94a4l1D1Q65hnX6F00t+wAWZSkcDUoD/
|
52
|
+
y2/ERKGUyuQwsG6l58e4MwYpmDI4RhHrUYLCQBacAehqVZhwNxv99L7ANsqr
|
53
|
+
ZJoT7N0kER9MbmuIZGb4qisLDfZtzIGKKUUiA3ARfQny4MUxFovSmVUF2Ojq
|
54
|
+
SBXUVV/PjMLifVTVyqCMv08YwmM4abj33tKOEMtiZqAa09lUIpnCUzq2IASh
|
55
|
+
SRNBzWIHe+ndoB6G2p6ufk0TuRidwdQZkZwTW/2PjK1x7KejaqADWaOIImKh
|
56
|
+
SBMpGzkVfDuv8aAFXOtf+LV67Ov6hJAt7FB65tE9Hg==
|
57
|
+
CV
|
58
|
+
|
59
|
+
encrypted_key_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES).replace <<-XML
|
60
|
+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
|
61
|
+
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
|
62
|
+
</EncryptionMethod>
|
63
|
+
XML
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'with unsupported digest method' do
|
67
|
+
it 'raises an unsupported error' do
|
68
|
+
encrypted_key_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES).replace <<-XML
|
69
|
+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
|
70
|
+
<ds:DigestMethod Algorithm="unsupported" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
|
71
|
+
</EncryptionMethod>
|
72
|
+
XML
|
73
|
+
|
74
|
+
expect {
|
75
|
+
subject.decrypt(private_key)
|
76
|
+
}.to raise_error(Xmlenc::UnsupportedError)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'returns the decrypted key' do
|
81
|
+
expect(subject.decrypt(private_key)).to be == ['3219e991eccd9186bf75a83ef8982fd0df4558fd1a837aa2'].pack('H*')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'with unsupported algorithm' do
|
86
|
+
it 'raises an unsupported error' do
|
87
|
+
encrypted_key_node.at_xpath('./xenc:EncryptionMethod', Xmlenc::NAMESPACES).replace <<-XML
|
88
|
+
<EncryptionMethod Algorithm="unsupported"></EncryptionMethod>
|
89
|
+
XML
|
90
|
+
|
91
|
+
expect {
|
92
|
+
subject.decrypt(private_key)
|
93
|
+
}.to raise_error(Xmlenc::UnsupportedError)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Phaos compatibility tests' do
|
4
|
+
let(:plain_xml) { File.read('spec/fixtures/phaos/payment.xml') }
|
5
|
+
let(:private_key) { OpenSSL::PKey::RSA.new(File.read('spec/fixtures/phaos/rsa-priv-key.pem')) }
|
6
|
+
let(:doc) { Nokogiri::XML::Document.parse(encrypted_xml) }
|
7
|
+
|
8
|
+
describe 'element 3des rsa oaep sha1' do
|
9
|
+
let(:encrypted_xml) { File.read('spec/fixtures/phaos/enc-element-3des-kt-rsa_oaep_sha1.xml') }
|
10
|
+
|
11
|
+
it 'decrypts the correct element' do
|
12
|
+
key_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedKey//xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
13
|
+
data_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedData/xenc:CipherData/xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
14
|
+
|
15
|
+
key = private_key.private_decrypt(key_cipher, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
|
16
|
+
|
17
|
+
cipher = OpenSSL::Cipher::Cipher.new('des-ede3-cbc')
|
18
|
+
cipher.decrypt
|
19
|
+
cipher.key = key
|
20
|
+
cipher.iv = data_cipher[0...cipher.iv_len]
|
21
|
+
result = cipher.update(data_cipher[cipher.iv_len..-1])
|
22
|
+
result << cipher.final
|
23
|
+
|
24
|
+
doc.at_xpath('//xenc:EncryptedData', Xmlenc::NAMESPACES).replace(result)
|
25
|
+
expect(doc.to_xml.chomp).to be == plain_xml
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'element aes 128 cbc rsa oaep sha1' do
|
30
|
+
let(:encrypted_xml) { File.read('spec/fixtures/phaos/enc-element-aes128-kt-rsa_oaep_sha1.xml') }
|
31
|
+
|
32
|
+
it 'decrypts the correct element' do
|
33
|
+
key_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedKey//xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
34
|
+
data_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedData/xenc:CipherData/xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
35
|
+
|
36
|
+
key = private_key.private_decrypt(key_cipher, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
|
37
|
+
|
38
|
+
cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
|
39
|
+
cipher.decrypt
|
40
|
+
cipher.key = key
|
41
|
+
cipher.iv = data_cipher[0...cipher.iv_len]
|
42
|
+
result = cipher.update(data_cipher[cipher.iv_len..-1])
|
43
|
+
result << cipher.final
|
44
|
+
|
45
|
+
doc.at_xpath('//xenc:EncryptedData', Xmlenc::NAMESPACES).replace(result)
|
46
|
+
expect(doc.to_xml.chomp).to be == plain_xml
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'element aes 128 cbc rsa 1.5' do
|
51
|
+
let(:encrypted_xml) { File.read('spec/fixtures/phaos/enc-element-aes128-kt-rsa1_5.xml') }
|
52
|
+
|
53
|
+
it 'decrypts the correct element' do
|
54
|
+
key_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedKey//xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
55
|
+
data_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedData/xenc:CipherData/xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
56
|
+
|
57
|
+
key = private_key.private_decrypt(key_cipher)
|
58
|
+
|
59
|
+
cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
|
60
|
+
cipher.decrypt
|
61
|
+
cipher.key = key
|
62
|
+
cipher.iv = data_cipher[0...cipher.iv_len]
|
63
|
+
result = cipher.update(data_cipher[cipher.iv_len..-1])
|
64
|
+
result << cipher.final
|
65
|
+
|
66
|
+
doc.at_xpath('//xenc:EncryptedData', Xmlenc::NAMESPACES).replace(result)
|
67
|
+
expect(doc.to_xml.chomp).to be == plain_xml
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe 'content aes 256 cbc rsa oaep sha1' do
|
72
|
+
let(:encrypted_xml) { File.read('spec/fixtures/phaos/enc-content-aes256-kt-rsa1_5.xml') }
|
73
|
+
|
74
|
+
it 'decrypts the correct element' do
|
75
|
+
key_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedKey//xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
76
|
+
data_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedData/xenc:CipherData/xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
77
|
+
|
78
|
+
key = private_key.private_decrypt(key_cipher)
|
79
|
+
|
80
|
+
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
|
81
|
+
cipher.decrypt
|
82
|
+
cipher.key = key
|
83
|
+
cipher.iv = data_cipher[0...cipher.iv_len]
|
84
|
+
result = cipher.update(data_cipher[cipher.iv_len..-1])
|
85
|
+
result << cipher.final
|
86
|
+
|
87
|
+
doc.at_xpath('//xenc:EncryptedData', Xmlenc::NAMESPACES).replace(result)
|
88
|
+
expect(doc.to_xml.chomp).to be == plain_xml
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'text aes 256 cbc rsa oaep sha1' do
|
93
|
+
let(:encrypted_xml) { File.read('spec/fixtures/phaos/enc-text-aes256-kt-rsa_oaep_sha1.xml') }
|
94
|
+
|
95
|
+
it 'decrypts the correct element' do
|
96
|
+
key_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedKey//xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
97
|
+
data_cipher = Base64.decode64(doc.at_xpath('//xenc:EncryptedData/xenc:CipherData/xenc:CipherValue', Xmlenc::NAMESPACES).content)
|
98
|
+
|
99
|
+
key = private_key.private_decrypt(key_cipher, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
|
100
|
+
|
101
|
+
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
|
102
|
+
cipher.decrypt
|
103
|
+
cipher.key = key
|
104
|
+
cipher.iv = data_cipher[0...cipher.iv_len]
|
105
|
+
result = cipher.update(data_cipher[cipher.iv_len..-1])
|
106
|
+
result << cipher.final
|
107
|
+
|
108
|
+
|
109
|
+
doc.at_xpath('//xenc:EncryptedData', Xmlenc::NAMESPACES).replace(result)
|
110
|
+
|
111
|
+
expect(doc.to_xml.gsub(/[\n\s]/, '')).to be == plain_xml.gsub(/[\n\s]/, '')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'xmlenc'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
data/xmlenc.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'xmlenc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "xmlenc"
|
8
|
+
spec.version = Xmlenc::VERSION
|
9
|
+
spec.authors = ["Benoist"]
|
10
|
+
spec.email = ["bclaassen@digidentity.eu"]
|
11
|
+
spec.description = %q{A (partial)implementation of the XMLENC specificiation}
|
12
|
+
spec.summary = %q{A (partial)implementation of the XMLENC specificiation}
|
13
|
+
spec.homepage = "https://github.com/digidentity/xmlenc"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "nokogiri", "~> 1.6"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xmlenc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benoist
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A (partial)implementation of the XMLENC specificiation
|
70
|
+
email:
|
71
|
+
- bclaassen@digidentity.eu
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/xmlenc.rb
|
83
|
+
- lib/xmlenc/algorithms/aes_cbc.rb
|
84
|
+
- lib/xmlenc/algorithms/des3_cbc.rb
|
85
|
+
- lib/xmlenc/algorithms/rsa_15.rb
|
86
|
+
- lib/xmlenc/algorithms/rsa_oaep_mgf1p.rb
|
87
|
+
- lib/xmlenc/encrypted_data.rb
|
88
|
+
- lib/xmlenc/encrypted_document.rb
|
89
|
+
- lib/xmlenc/encrypted_key.rb
|
90
|
+
- lib/xmlenc/version.rb
|
91
|
+
- spec/fixtures/encrypted_document.xml
|
92
|
+
- spec/fixtures/key.pem
|
93
|
+
- spec/fixtures/phaos/enc-content-aes256-kt-rsa1_5.xml
|
94
|
+
- spec/fixtures/phaos/enc-element-3des-kt-rsa_oaep_sha1.xml
|
95
|
+
- spec/fixtures/phaos/enc-element-aes128-kt-rsa1_5.xml
|
96
|
+
- spec/fixtures/phaos/enc-element-aes128-kt-rsa_oaep_sha1.xml
|
97
|
+
- spec/fixtures/phaos/enc-text-aes256-kt-rsa_oaep_sha1.xml
|
98
|
+
- spec/fixtures/phaos/key.txt
|
99
|
+
- spec/fixtures/phaos/payment.xml
|
100
|
+
- spec/fixtures/phaos/rsa-priv-key.pem
|
101
|
+
- spec/lib/xmlenc/.keep
|
102
|
+
- spec/lib/xmlenc/encrypted_data_spec.rb
|
103
|
+
- spec/lib/xmlenc/encrypted_document_spec.rb
|
104
|
+
- spec/lib/xmlenc/encrypted_key_spec.rb
|
105
|
+
- spec/lib/xmlenc/phaos_compat_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- xmlenc.gemspec
|
108
|
+
homepage: https://github.com/digidentity/xmlenc
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.0.6
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: A (partial)implementation of the XMLENC specificiation
|
132
|
+
test_files:
|
133
|
+
- spec/fixtures/encrypted_document.xml
|
134
|
+
- spec/fixtures/key.pem
|
135
|
+
- spec/fixtures/phaos/enc-content-aes256-kt-rsa1_5.xml
|
136
|
+
- spec/fixtures/phaos/enc-element-3des-kt-rsa_oaep_sha1.xml
|
137
|
+
- spec/fixtures/phaos/enc-element-aes128-kt-rsa1_5.xml
|
138
|
+
- spec/fixtures/phaos/enc-element-aes128-kt-rsa_oaep_sha1.xml
|
139
|
+
- spec/fixtures/phaos/enc-text-aes256-kt-rsa_oaep_sha1.xml
|
140
|
+
- spec/fixtures/phaos/key.txt
|
141
|
+
- spec/fixtures/phaos/payment.xml
|
142
|
+
- spec/fixtures/phaos/rsa-priv-key.pem
|
143
|
+
- spec/lib/xmlenc/.keep
|
144
|
+
- spec/lib/xmlenc/encrypted_data_spec.rb
|
145
|
+
- spec/lib/xmlenc/encrypted_document_spec.rb
|
146
|
+
- spec/lib/xmlenc/encrypted_key_spec.rb
|
147
|
+
- spec/lib/xmlenc/phaos_compat_spec.rb
|
148
|
+
- spec/spec_helper.rb
|