import saml2 import xmlenc as xenc import xmldsig data1 = """ A23B45C56 """ def test_1(): ed = xenc.encrypted_data_from_string(data1) assert ed assert ed.mime_type == "text/xml" assert ed.cipher_data is not None cd = ed.cipher_data assert cd.cipher_value is not None assert cd.cipher_value.text == "A23B45C56" data2 = """ John Smith DEADBEEF """ # data2 = """ # # # John Smith # # DEADBEEF # """ def test_2(): ed = xenc.encrypted_data_from_string(data2) assert ed print ed assert ed.type == "http://www.w3.org/2001/04/xmlenc#Element" assert ed.encryption_method is not None em = ed.encryption_method assert em.algorithm == 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' assert ed.key_info is not None ki = ed.key_info assert ki.key_name[0].text == "John Smith" assert ed.cipher_data is not None cd = ed.cipher_data assert cd.cipher_value is not None assert cd.cipher_value.text == "DEADBEEF" data3 = """ Sally Doe DEADBEEF """ def test_3(): ed = xenc.encrypted_data_from_string(data3) assert ed print ed assert ed.encryption_method != None em = ed.encryption_method assert em.algorithm == 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' assert ed.key_info != None ki = ed.key_info assert ki.key_name[0].text == "Sally Doe" assert len(ki.retrieval_method) == 1 rm = ki.retrieval_method[0] assert rm.uri == "#EK" assert rm.type == "http://www.w3.org/2001/04/xmlenc#EncryptedKey" assert ed.cipher_data != None cd = ed.cipher_data assert cd.cipher_value != None assert cd.cipher_value.text == "DEADBEEF" data4 = """ John Smith xyzabc Sally Doe """ # data4 = """ # # # John Smith # # xyzabc # # # # Sally Doe # """ def test_4(): ek = xenc.encrypted_key_from_string(data4) assert ek print ek assert ek.encryption_method != None em = ek.encryption_method assert em.algorithm == 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' assert ek.key_info != None ki = ek.key_info assert ki.key_name[0].text == "John Smith" assert ek.reference_list != None rl = ek.reference_list assert len(rl.data_reference) dr = rl.data_reference[0] assert dr.uri == "#ED" assert ek.cipher_data != None cd = ek.cipher_data assert cd.cipher_value != None assert cd.cipher_value.text == "xyzabc" data5 = """ self::text()[parent::rep:CipherValue[@Id="example1"]] """ def test_5(): cr = xenc.cipher_reference_from_string(data5) assert cr print cr print cr.keyswv() trs = cr.transforms assert len(trs.transform) == 2 tr = trs.transform[0] assert tr.algorithm in ["http://www.w3.org/TR/1999/REC-xpath-19991116", "http://www.w3.org/2000/09/xmldsig#base64"] if tr.algorithm == "http://www.w3.org/2000/09/xmldsig#base64": pass elif tr.algorithm == "http://www.w3.org/TR/1999/REC-xpath-19991116": assert len(tr.x_path) == 1 xp = tr.x_path[0] assert xp.text.strip() == """self::text()[parent::rep:CipherValue[@Id="example1"]]""" data6 = """ self::xenc:EncryptedData[@Id="example1"] """ def test_6(): rl = xenc.reference_list_from_string(data6) assert rl print rl assert len(rl.data_reference) == 1 dr = rl.data_reference[0] assert dr.uri == "#invoice34" assert len(dr.extension_elements) == 1 ee = dr.extension_elements[0] assert ee.tag == "Transforms" assert ee.namespace == "http://www.w3.org/2000/09/xmldsig#" trs = saml2.extension_element_to_element(ee, xmldsig.ELEMENT_FROM_STRING, namespace=xmldsig.NAMESPACE) assert trs assert len(trs.transform) == 1 tr = trs.transform[0] assert tr.algorithm == "http://www.w3.org/TR/1999/REC-xpath-19991116" assert len(tr.x_path) == 1 assert tr.x_path[0].text.strip() == """self::xenc:EncryptedData[@Id="example1"]"""