Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt/Decrypt not working (maybe wrong usage?) #13

Open
andreixk opened this issue Jul 18, 2017 · 2 comments
Open

Encrypt/Decrypt not working (maybe wrong usage?) #13

andreixk opened this issue Jul 18, 2017 · 2 comments

Comments

@andreixk
Copy link

andreixk commented Jul 18, 2017

I created a simple xml file, generated a self-signed certificate and key:

openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

then tried to encrypt it and then decrypt the encrypted data:

from wsse import encryption
plain = open('plain.xml','rb').read()
encr = encryption.encrypt(plain,'test/cert.pem')
print(encryption.decrypt(envelope=encr,keyfile='test/key.pem'))

The result is this error:

Traceback (most recent call last):
  File "/private/var/www/html/apps/moh/test.py", line 9, in <module>
    print(encryption.decrypt(envelope=encrd,keyfile='test/key.pem'))
  File "/private/var/www/html/apps/moh/wsse/encryption.py", line 196, in decrypt
    )[0]
IndexError: list index out of range

plain.xml:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://hcv.health.ontario.ca/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<SOAP-ENV:Header>
		<wsse:Security mustUnderstand="true">
			<wsse:UsernameToken>
				<wsse:Username>test_user</wsse:Username>
				<wsse:Password>test_pass</wsse:Password>
			</wsse:UsernameToken>
			<wsu:Timestamp>
				<wsu:Created>2017-07-18T01:32:47.577170+00:00</wsu:Created>
				<wsu:Expires>2017-07-18T01:34:17.577170+00:00</wsu:Expires>
			</wsu:Timestamp>
		</wsse:Security>
	</SOAP-ENV:Header>
	<ns0:Body>
		<ns1:validate>
			<requests>
				<hcvRequest>
					<healthNumber>9999999999</healthNumber>
					<versionCode>YX</versionCode>
				</hcvRequest>
			</requests>
			<locale>en</locale>
		</ns1:validate>
	</ns0:Body>
</SOAP-ENV:Envelope>
@kentwongg
Copy link

Did you ever figure this out? I know the index out of range error is being caused by a bad xpath query.

@wilmer05
Copy link

wilmer05 commented Jun 23, 2020

Helllo,

I had the same problem, the reason this is not working is because the Id attribute is preceded by a namespace which is WSU_NS, so you have to change the code from the decrypt function from

enc_data = doc.xpath( "//enc:EncryptedData[@Id='%s']" % referenced_id, namespaces={'enc': ENC_NS}, )[0]

to

enc_data = doc.xpath( "//enc:EncryptedData[@ns0:Id='%s']" % referenced_id, namespaces={'enc': ENC_NS, 'ns0': WSU_NS}, )[0]

After this there is another error arising and is that after encryption with this library the EncryptedData Element doesn't have any KeyInfo element inside, so the decrypt function will fail here

https://github.com/orcasgit/py-wsse/blob/master/wsse/encryption.py#L204

since the variable key_info is None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants