main
1# frozen_string_literal: true
2
3RSpec.describe ::Xml::Kit::EncryptedKey do
4 describe '#to_xml' do
5 subject { described_class.new(id: id, asymmetric_cipher: asymmetric_cipher, key_info: key_info) }
6
7 let(:asymmetric_cipher) { ::Xml::Kit::Crypto.cipher_for(algorithm, private_key.public_key) }
8 let(:algorithm) { ::Xml::Kit::Crypto::RsaCipher::ALGORITHM }
9 let(:key_info) { ::Xml::Kit::KeyInfo.new }
10 let(:id) { ::Xml::Kit::Id.generate }
11 let(:private_key) { OpenSSL::PKey::RSA.new(2048) }
12 let(:result) { Hash.from_xml(subject.to_xml) }
13
14 before do
15 key_info.key_name = 'samlkey'
16 end
17
18 specify { expect(result.key?('EncryptedKey')).to be_present }
19 specify { expect(result['EncryptedKey']['Id']).to eql(id) }
20 specify { expect(result['EncryptedKey']['xmlns']).to eql(::Xml::Kit::Namespaces::XMLENC) }
21 specify { expect(result['EncryptedKey']['EncryptionMethod']['Algorithm']).to eql(algorithm) }
22 specify { expect(result['EncryptedKey']['CipherData']['CipherValue']).to be_present }
23 specify { expect(private_key.private_decrypt(Base64.decode64(result['EncryptedKey']['CipherData']['CipherValue']))).to eql(subject.symmetric_cipher.key) }
24 specify { expect(subject.to_xml).to match_xsd('xenc-schema') }
25 specify { expect(result['EncryptedKey'].key?('KeyInfo')).to be(true) }
26
27 context 'with custom symmetric cipher' do
28 subject { described_class.new(id: id, asymmetric_cipher: asymmetric_cipher, key_info: key_info, symmetric_cipher: symmetric_cipher) }
29
30 let(:symmetric_cipher) { instance_double(Xml::Kit::Crypto::SymmetricCipher, key: 'symmetric_key', encrypt: 'CIPHERTEXT', algorithm: 'symmetric_cipher') }
31
32 specify { expect(private_key.private_decrypt(Base64.decode64(result['EncryptedKey']['CipherData']['CipherValue']))).to eql(symmetric_cipher.key) }
33 end
34 end
35end