Skip to content

Commit edd6be8

Browse files
committed
Merge pull request #20 from bgaifullin/master
Fixed memory corruption in _KeyData
2 parents 7e221f9 + a66c75c commit edd6be8

File tree

9 files changed

+153
-21
lines changed

9 files changed

+153
-21
lines changed

src/xmlsec.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#include <xmlsec/xmltree.h>
1+
#include <xmlsec/xmlsec.h>
2+
#include <xmlsec/errors.h>
3+
#include <xmlsec/templates.h>
24
#include <xmlsec/xmldsig.h>
35
#include <xmlsec/xmlenc.h>
4-
#include <xmlsec/templates.h>
5-
#include <xmlsec/errors.h>
6+
#include <xmlsec/xmltree.h>
67
#include <xmlsec/openssl/app.h>
78
#include <xmlsec/openssl/crypto.h>
9+
#include <xmlsec/openssl/x509.h>

src/xmlsec/key.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ cdef extern from "xmlsec.h": # xmlsec/keys.h
9898

9999
int xmlSecOpenSSLAppDefaultKeysMngrInit(xmlSecKeysMngrPtr) nogil
100100

101+
int xmlSecOpenSSLKeysMngrInit(xmlSecKeysMngrPtr) nogil
102+
101103
int xmlSecOpenSSLAppDefaultKeysMngrAdoptKey(xmlSecKeysMngrPtr, xmlSecKeyPtr) nogil
102104

103105
int xmlSecOpenSSLAppKeysMngrCertLoad(

src/xmlsec/key.pyx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,11 @@ cdef class KeysManager(object):
238238
handle = xmlSecKeysMngrCreate()
239239
if handle == NULL:
240240
raise InternalError("failed to create keys manager", -1)
241-
242241
rv = xmlSecOpenSSLAppDefaultKeysMngrInit(handle)
243242
if rv < 0:
244-
raise InternalError("failed to initialize keys manager", rv)
243+
xmlSecKeysMngrDestroy(handle)
244+
raise InternalError("failed to init manager", rv)
245+
245246
self._handle = handle
246247

247248
def __dealloc__(self):

src/xmlsec/utils.pyx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ def init():
1616
This is called upon library import and does not need to be called
1717
again (unless @ref _shutdown is called explicitly).
1818
"""
19-
r = xmlSecInit()
20-
if r != 0:
19+
if xmlSecInit() < 0:
2120
return False
2221

23-
r = xmlSecOpenSSLInit()
24-
if r != 0:
22+
if xmlSecOpenSSLAppInit(NULL) < 0:
23+
xmlSecShutdown()
2524
return False
2625

27-
r = xmlSecOpenSSLAppInit(NULL)
28-
if r != 0:
26+
if xmlSecOpenSSLInit() < 0:
27+
xmlSecOpenSSLAppShutdown()
28+
xmlSecShutdown()
2929
return False
3030

3131
return True
@@ -37,17 +37,17 @@ def shutdown():
3737
This is called automatically upon interpreter termination and
3838
should not need to be called explicitly.
3939
"""
40-
r = xmlSecOpenSSLAppShutdown()
41-
if r != 0:
40+
if xmlSecOpenSSLShutdown() < 0:
4241
return False
4342

44-
r = xmlSecOpenSSLShutdown()
45-
if r != 0:
43+
if xmlSecOpenSSLAppShutdown() < 0:
4644
return False
4745

48-
r = xmlSecShutdown()
49-
return r == 0
46+
if xmlSecShutdown() < 0:
47+
return False
48+
49+
return True
5050

5151

5252
def enable_debug_trace(flag):
53-
xmlSecErrorsDefaultCallbackEnableOutput(<int>flag)
53+
xmlSecErrorsDefaultCallbackEnableOutput(1 if flag else 0)

tests/examples/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ def compare(name, result):
1818

1919
# Compare the results.
2020
assert expected_text == result_text
21-

tests/examples/sign4-doc.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
XML Security Library example: Original XML doc file for sign3 example.
4+
-->
5+
<Envelope xmlns="urn:envelope" ID="ef115a20-cf73-11e5-aed1-3c15c2c2cc88">
6+
<Data>
7+
Hello, World!
8+
</Data>
9+
</Envelope>

tests/examples/sign4-res.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<Envelope xmlns="urn:envelope" ID="ef115a20-cf73-11e5-aed1-3c15c2c2cc88">
2+
<Data>
3+
Hello, World!
4+
</Data>
5+
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
6+
<ds:SignedInfo>
7+
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
8+
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
9+
<ds:Reference URI="#ef115a20-cf73-11e5-aed1-3c15c2c2cc88">
10+
<ds:Transforms>
11+
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
12+
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
13+
</ds:Transforms>
14+
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
15+
<ds:DigestValue>ERS7F/ifxZoyTe0mhco+HeAEKGo=</ds:DigestValue>
16+
</ds:Reference>
17+
</ds:SignedInfo>
18+
<ds:SignatureValue>G+mZNFqh9w0wIkDSbuYwvVDu7CMP8PEsw7jfwiZBC8nyF3loAtYKKAkdi6Zy3dJs
19+
tU8qKfhzvabmCjdIrGkFTdtlCNCVKDMzwogFtxEX4Oh77X6jjx4b22XNJx4AbnUG
20+
JV/EcsD+po8s5qVEXw62lRRd8cMDafbzOA/rBH96CMNgZhzxyaF9VRLa/vbt1ht2
21+
hE1KkdZCB4Y0Lv3QyeDL2jax3NFks9FUv8IqoWYQSvywdMLY2ZMiQ9UpPeVfMizi
22+
trd5zDUSD/s3hyIEs4gD5NJF3HZPD/Fe2Zw1PBPj9eLADdEzcdueyCdPJ2YioFIi
23+
1sMW/qPDhR/DoOJwGpUxwQ==</ds:SignatureValue>
24+
<ds:KeyInfo>
25+
<ds:X509Data>
26+
<ds:X509Certificate>MIIE3zCCBEigAwIBAgIB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FTMAkG
42+
A1UdEwQCMAAwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRp
43+
ZmljYXRlMB0GA1UdDgQWBBQkhCzy1FkgYosuXIaQo6owuicanDCB+AYDVR0jBIHw
44+
MIHtgBS0ue+a5pcOaGUemM76VQ2JBttMfKGB0aSBzjCByzELMAkGA1UEBhMCVVMx
45+
EzARBgNVBAgTCkNhbGlmb3JuaWExEjAQBgNVBAcTCVN1bm55dmFsZTE9MDsGA1UE
46+
ChM0WE1MIFNlY3VyaXR5IExpYnJhcnkgKGh0dHA6Ly93d3cuYWxla3NleS5jb20v
47+
eG1sc2VjKTEZMBcGA1UECxMQUm9vdCBDZXJ0aWZpY2F0ZTEWMBQGA1UEAxMNQWxl
48+
a3NleSBTYW5pbjEhMB8GCSqGSIb3DQEJARYSeG1sc2VjQGFsZWtzZXkuY29tggEA
49+
MA0GCSqGSIb3DQEBBAUAA4GBALU/mzIxSv8vhDuomxFcplzwdlLZbvSQrfoNkMGY
50+
1UoS3YJrN+jZLWKSyWE3mIaPpElqXiXQGGkwD5iPQ1iJMbI7BeLvx6ZxX/f+c8Wn
51+
ss0uc1NxfahMaBoyG15IL4+beqO182fosaKJTrJNG3mc//ANGU9OsQM9mfBEt4oL
52+
NJ2D</ds:X509Certificate>
53+
</ds:X509Data>
54+
</ds:KeyInfo>
55+
</ds:Signature></Envelope>

tests/examples/test_sign.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,70 @@ def test_sign_generated_template_pem_with_x509():
158158
compare('sign3-res.xml', template)
159159

160160

161+
def test_sign_generated_template_pem_with_x509_with_custom_ns():
162+
"""
163+
Should sign a file using a dynamicaly created template, key from PEM
164+
file and an X509 certificate with custom ns.
165+
"""
166+
167+
# Load document file.
168+
template = parse_xml('sign4-doc.xml')
169+
xmlsec.tree.add_ids(template, ["ID"])
170+
elem_id = template.get('ID', None)
171+
if elem_id:
172+
elem_id = '#' + elem_id
173+
# Create a signature template for RSA-SHA1 enveloped signature.
174+
signature_node = xmlsec.template.create(
175+
template,
176+
xmlsec.Transform.EXCL_C14N,
177+
xmlsec.Transform.RSA_SHA1, ns='ds')
178+
179+
assert signature_node is not None
180+
181+
# Add the <ds:Signature/> node to the document.
182+
template.append(signature_node)
183+
184+
# Add the <ds:Reference/> node to the signature template.
185+
ref = xmlsec.template.add_reference(signature_node, xmlsec.Transform.SHA1, uri=elem_id)
186+
187+
# Add the enveloped transform descriptor.
188+
xmlsec.template.add_transform(ref, xmlsec.Transform.ENVELOPED)
189+
# Add the excl_c14n transform descriptor.
190+
xmlsec.template.add_transform(ref, xmlsec.Transform.EXCL_C14N)
191+
192+
# Add the <ds:KeyInfo/> and <ds:KeyName/> nodes.
193+
key_info = xmlsec.template.ensure_key_info(signature_node)
194+
xmlsec.template.add_x509_data(key_info)
195+
196+
# Create a digital signature context (no key manager is needed).
197+
ctx = xmlsec.SignatureContext()
198+
199+
# Load private key (assuming that there is no password).
200+
filename = path.join(BASE_DIR, 'rsakey.pem')
201+
key = xmlsec.Key.from_file(filename, xmlsec.KeyFormat.PEM)
202+
203+
assert key is not None
204+
205+
# Load the certificate and add it to the key.
206+
filename = path.join(BASE_DIR, 'rsacert.pem')
207+
key.load_cert_from_file(filename, xmlsec.KeyFormat.PEM)
208+
209+
# Set key name to the file name (note: this is just a test).
210+
key.name = path.basename(filename)
211+
212+
# Set the key on the context.
213+
ctx.key = key
214+
215+
assert ctx.key is not None
216+
assert ctx.key.name == path.basename(filename)
217+
218+
# Sign the template.
219+
ctx.sign(signature_node)
220+
221+
# Assert the contents of the XML document against the expected result.
222+
compare('sign4-res.xml', template)
223+
224+
161225
def test_sign_binary():
162226
ctx = xmlsec.SignatureContext()
163227
filename = path.join(BASE_DIR, 'rsakey.pem')

tests/examples/test_verify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from .base import parse_xml, BASE_DIR
55

66

7-
@mark.parametrize('index', range(1, 4))
7+
@mark.parametrize('index', range(1, 5))
88
def test_verify_with_pem_file(index):
99
"""Should verify a signed file using a key from a PEM file.
1010
"""
1111

1212
# Load the XML document.
1313
template = parse_xml('sign%d-res.xml' % index)
14-
14+
xmlsec.tree.add_ids(template, ["ID"])
1515
# Find the <Signature/> node.
1616
signature_node = xmlsec.tree.find_node(template, xmlsec.Node.SIGNATURE)
1717

0 commit comments

Comments
 (0)