Skip to content

Commit ae9c5da

Browse files
authored
Merge pull request #60 from mehcode/add_supp_for_inc_namespaces_tag
Added binding for method xmlSecTmplTransformAddC14NInclNamespaces #59
2 parents d454dc9 + 4a685fc commit ae9c5da

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
dist: trusty
22
sudo: false
33
language: python
4+
notifications:
5+
email: false
6+
47
python:
58
- '2.7'
69
- '3.4'

doc/source/modules/constants.rst

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Namespaces
5858
- *XPointerNs* - http://www.w3.org/2001/04/xmldsig-more/xptr
5959
- *Soap11Ns* - http://schemas.xmlsoap.org/soap/envelope/
6060
- *Soap12Ns* - http://www.w3.org/2002/06/soap-envelope
61+
- *NsExcC14N* - http://www.w3.org/2001/10/xml-exc-c14n#
62+
- *NsExcC14NWithComments* - http://www.w3.org/2001/10/xml-exc-c14n#WithComments
6163

6264
Nodes
6365
*****

src/constants.c

+2
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
292292
PYXMLSEC_ADD_NS_CONSTANT(XPointerNs, "XPOINTER");
293293
PYXMLSEC_ADD_NS_CONSTANT(Soap11Ns, "SOAP11");
294294
PYXMLSEC_ADD_NS_CONSTANT(Soap12Ns, "SOAP12");
295+
PYXMLSEC_ADD_NS_CONSTANT(NsExcC14N, "EXC_C14N");
296+
PYXMLSEC_ADD_NS_CONSTANT(NsExcC14NWithComments, "EXC_C14N_WITH_COMMENT");
295297

296298
PYXMLSEC_CLOSE_NAMESPACE(nsCls);
297299

src/platform.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef int Py_ssize_t;
3737

3838
#if PY_MAJOR_VERSION >= 3
3939
#define PY3K 1
40+
#define PyString_Check PyUnicode_Check
4041
#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
4142

4243
#define PyString_FromString PyUnicode_FromString

src/template.c

+57
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,57 @@ static PyObject* PyXmlSec_TemplateEncryptedDataEnsureCipherValue(PyObject* self,
651651
return NULL;
652652
}
653653

654+
static char PyXmlSec_TemplateTransformAddC14NInclNamespaces__doc__[] = \
655+
"Adds 'inclusive' namespaces to the ExcC14N transform node *node*.\n\n"
656+
":param node: the pointer to <dsig:Transform/> node.\n"
657+
":param prefixList: the list of namespace prefixes, where 'default' indicates the default namespace (optional).";
658+
static PyObject* PyXmlSec_TemplateTransformAddC14NInclNamespaces(PyObject* self, PyObject *args, PyObject *kwargs) {
659+
static char *kwlist[] = { "node", "prefixes", NULL};
660+
661+
PyXmlSec_LxmlElementPtr node = NULL;
662+
PyObject* prefixes = NULL;
663+
// transform_add_c14n_inclusive_namespaces
664+
PYXMLSEC_DEBUG("template encrypted_data_ensure_cipher_value - start");
665+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O:transform_add_c14n_inclusive_namespaces", kwlist,
666+
PyXmlSec_LxmlElementConverter, &node, &prefixes))
667+
{
668+
prefixes = NULL;
669+
goto ON_FAIL;
670+
}
671+
if (PyList_Check(prefixes) || PyTuple_Check(prefixes)) {
672+
PyObject* sep = PyString_FromString(" ");
673+
prefixes = PyObject_CallMethod(sep, "join", "O", prefixes);
674+
Py_DECREF(sep);
675+
} else if (PyString_Check(prefixes)) {
676+
Py_INCREF(prefixes);
677+
} else {
678+
PyErr_SetString(PyExc_TypeError, "expected instance of str or list of str");
679+
prefixes = NULL;
680+
}
681+
682+
if (prefixes == NULL) {
683+
goto ON_FAIL;
684+
}
685+
686+
int res;
687+
const char* c_prefixes = PyString_AsString(prefixes);
688+
Py_BEGIN_ALLOW_THREADS;
689+
res = xmlSecTmplTransformAddC14NInclNamespaces(node->_c_node, XSTR(c_prefixes));
690+
Py_END_ALLOW_THREADS;
691+
if (res != 0) {
692+
PyXmlSec_SetLastError("cannot add 'inclusive' namespaces to the ExcC14N transform node");
693+
goto ON_FAIL;
694+
}
695+
696+
Py_DECREF(prefixes);
697+
PYXMLSEC_DEBUG("transform_add_c14n_inclusive_namespaces - ok");
698+
Py_RETURN_NONE;
699+
700+
ON_FAIL:
701+
PYXMLSEC_DEBUG("transform_add_c14n_inclusive_namespaces - fail");
702+
Py_XDECREF(prefixes);
703+
return NULL;
704+
}
654705

655706
static PyMethodDef PyXmlSec_TemplateMethods[] = {
656707
{
@@ -761,6 +812,12 @@ static PyMethodDef PyXmlSec_TemplateMethods[] = {
761812
METH_VARARGS|METH_KEYWORDS,
762813
PyXmlSec_TemplateEncryptedDataEnsureCipherValue__doc__
763814
},
815+
{
816+
"transform_add_c14n_inclusive_namespaces",
817+
(PyCFunction)PyXmlSec_TemplateTransformAddC14NInclNamespaces,
818+
METH_VARARGS|METH_KEYWORDS,
819+
PyXmlSec_TemplateTransformAddC14NInclNamespaces__doc__,
820+
},
764821
{NULL, NULL} /* sentinel */
765822
};
766823

tests/examples/test_templates.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import xmlsec
2+
from .base import parse_xml
3+
from lxml import etree
4+
5+
6+
def _check_transform_add_custom_c14n_inclusive_namespaces(prefixes, expected):
7+
template = parse_xml('sign2-doc.xml')
8+
assert template is not None
9+
10+
# Create a signature template for RSA-SHA1 enveloped signature.
11+
signature_node = xmlsec.template.create(template, xmlsec.Transform.EXCL_C14N, xmlsec.Transform.RSA_SHA1)
12+
assert signature_node is not None
13+
14+
# Add the <ds:Signature/> node to the document.
15+
template.append(signature_node)
16+
17+
# Add the <ds:Reference/> node to the signature template.
18+
ref = xmlsec.template.add_reference(signature_node, xmlsec.Transform.SHA1)
19+
20+
# Add the enveloped transform descriptor.
21+
transform = xmlsec.template.add_transform(ref, xmlsec.Transform.ENVELOPED)
22+
assert transform is not None
23+
24+
xmlsec.template.transform_add_c14n_inclusive_namespaces(transform, prefixes)
25+
ins = xmlsec.tree.find_child(transform, "InclusiveNamespaces", xmlsec.constants.NsExcC14N)
26+
assert ins is not None
27+
assert expected == ins.get("PrefixList")
28+
29+
30+
def test_transform_add_custom_c14n_inclusive_namespaces():
31+
_check_transform_add_custom_c14n_inclusive_namespaces(["ns1", "ns2"], "ns1 ns2")
32+
33+
34+
def test_transform_add_default_c14n_inclusive_namespaces():
35+
_check_transform_add_custom_c14n_inclusive_namespaces("default", "default")

0 commit comments

Comments
 (0)