Skip to content

Commit 41df5fa

Browse files
author
Ryan Leckey
committed
Initial commit.
0 parents  commit 41df5fa

5 files changed

Lines changed: 617 additions & 0 deletions

File tree

setup.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#! /usr/bin/env python
2+
import sys
3+
import subprocess
4+
from os import path
5+
from distutils.core import setup
6+
from distutils.extension import Extension
7+
from Cython.Distutils import build_ext
8+
9+
10+
#
11+
# HACK
12+
#
13+
14+
def getoutput(command):
15+
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
16+
out, err = process.communicate()
17+
return out.decode('utf8')
18+
19+
20+
def get_lxml_include_dirs():
21+
from os import environ
22+
lxml_home = environ.get("LXML_HOME")
23+
if lxml_home is None:
24+
# `LXML_HOME` not specified -- derive from installed `lxml`
25+
import lxml
26+
lxml_home = path.dirname(lxml.__file__)
27+
else:
28+
if not exists(lxml_home):
29+
sys.exit("The directory specified via envvar `LXML_HOME` does not exist")
30+
lxml_home = path.join(lxml_home, "src", "lxml")
31+
# check that it contains what is needed
32+
lxml_include = path.join(lxml_home, "includes")
33+
if not (path.exists(path.join(lxml_home, "etreepublic.pxd")) \
34+
or path.exists(path.join(lxml_include, "etreepublic.pxd"))):
35+
sys.exit("The lxml installation lacks the mandatory `etreepublic.pxd`. You may need to install `lxml` manually or set envvar `LXML_HOME` to an `lxml` installation with `etreepublic.pxd`")
36+
return [lxml_home, lxml_include]
37+
38+
# we must extend our cflags once `lxml` is installed.
39+
# To this end, we override `Extension`
40+
class Extension(Extension, object):
41+
lxml_extended = False
42+
43+
def get_include_dirs(self):
44+
ids = self.__dict__["include_dirs"]
45+
if self.lxml_extended: return ids
46+
# ensure `lxml` headers come before ours
47+
# this should make sure to use its headers rather than our old copy
48+
# ids.extend(get_lxml_include_dirs())
49+
ids[0:0] = get_lxml_include_dirs()
50+
self.lxml_extended = True
51+
return ids
52+
53+
def set_include_dirs(self, ids): self.__dict__["include_dirs"] = ids
54+
55+
include_dirs = property(get_include_dirs, set_include_dirs)
56+
57+
58+
define_macros = []
59+
include_dirs = ['src']
60+
library_dirs = []
61+
libraries = []
62+
63+
def extract_cflags(cflags):
64+
global define_macros, include_dirs
65+
list = cflags.split(' ')
66+
for flag in list:
67+
if flag == '':
68+
continue
69+
flag = flag.replace("\\\"", "")
70+
if flag[:2] == "-I":
71+
if flag[2:] not in include_dirs:
72+
include_dirs.append(flag[2:])
73+
elif flag[:2] == "-D":
74+
t = tuple(flag[2:].split('='))
75+
if len(t) == 1:
76+
t = (t[0], None)
77+
if t not in define_macros:
78+
define_macros.append(t)
79+
else:
80+
print("Warning : cflag %s skipped" % flag)
81+
82+
def extract_libs(libs):
83+
global library_dirs, libraries
84+
list = libs.split(' ')
85+
for flag in list:
86+
if flag == '':
87+
continue
88+
if flag[:2] == "-l":
89+
if flag[2:] not in libraries:
90+
libraries.append(flag[2:])
91+
elif flag[:2] == "-L":
92+
if flag[2:] not in library_dirs:
93+
library_dirs.append(flag[2:])
94+
else:
95+
print("Warning : linker flag %s skipped" % flag)
96+
97+
98+
libxml2_cflags = getoutput('pkg-config libxml-2.0 --cflags')
99+
if libxml2_cflags[:2] not in ["-I", "-D"]:
100+
libxml2_cflags = getoutput('xml2-config --cflags')
101+
if libxml2_cflags[:2] not in ["-I", "-D"]:
102+
print("Error : cannot get LibXML2 pre-processor and compiler flags")
103+
104+
libxml2_libs = getoutput('pkg-config libxml-2.0 --libs')
105+
if libxml2_libs[:2] not in ["-l", "-L"]:
106+
libxml2_libs = getoutput('xml2-config --libs')
107+
if libxml2_libs[:2] not in ["-l", "-L"]:
108+
print("Error : cannot get LibXML2 linker flags")
109+
110+
xmlsec1_crypto = 'openssl'
111+
cmd = 'pkg-config xmlsec1-%s --cflags' % xmlsec1_crypto
112+
xmlsec1_cflags = getoutput(cmd)
113+
if xmlsec1_cflags[:2] not in ["-I", "-D"]:
114+
cmd = 'xmlsec1-config --cflags --crypto=%s' % xmlsec1_crypto
115+
xmlsec1_cflags = getoutput(cmd)
116+
if xmlsec1_cflags[:2] not in ["-I", "-D"]:
117+
print("Error : cannot get XMLSec1 pre-processor and compiler flags")
118+
119+
cmd = 'pkg-config xmlsec1-%s --libs' % xmlsec1_crypto
120+
xmlsec1_libs = getoutput(cmd)
121+
if xmlsec1_libs[:2] not in ["-l", "-L"]:
122+
cmd = 'xmlsec1-config --libs --crypto=%s' % xmlsec1_crypto
123+
xmlsec1_libs = getoutput(cmd)
124+
if xmlsec1_libs[:2] not in ["-l", "-L"]:
125+
print("Error : cannot get XMLSec1 linker flags")
126+
127+
#print(libxml2_cflags)
128+
#print libxml2_libs
129+
#print xmlsec1_cflags
130+
#print xmlsec1_libs
131+
132+
extract_cflags(libxml2_cflags)
133+
extract_libs(libxml2_libs)
134+
135+
extract_cflags(xmlsec1_cflags)
136+
extract_libs(xmlsec1_libs)
137+
138+
#
139+
# END HACK
140+
#
141+
142+
setup(
143+
name='xmlsec',
144+
version='0.1.0',
145+
description='Python bindings for the XML Security Library.',
146+
setup_requires=["lxml >= 3.0",],
147+
install_requires=[
148+
"lxml >= 3.0"
149+
],
150+
cmdclass={'build_ext': build_ext},
151+
ext_modules=[
152+
Extension(
153+
'xmlsec', ['xmlsec.pyx'],
154+
define_macros=define_macros,
155+
include_dirs=include_dirs,
156+
library_dirs=library_dirs,
157+
libraries=libraries,
158+
depends=["src/" + f for f in "cxmlsec.pxd cxmlsec.h lxml.etree.h lxml-version.h lxml.etree_api.h".split()]
159+
)
160+
],
161+
)

test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import xmlsec
2+
from lxml import etree
3+
4+
# Create the signature template.
5+
_xml = xmlsec.create_signature_template()
6+
7+
# Rewrap to change the namespace mapping.
8+
nsmap = {'ds': _xml.nsmap[None]}
9+
xml = etree.Element(_xml.tag, attrib=_xml.attrib, nsmap=nsmap)
10+
xml.extend(_xml.getchildren())
11+
12+
# Add a reference.
13+
ref = xmlsec.add_reference(xml, uri=b'#_34275907093489075620748690')
14+
15+
# Add an eveloped transformation declaration.
16+
xmlsec.add_transform(ref, xmlsec.method.ENVELOPED)
17+
18+
# Add <ds:KeyInfo/> and <ds:X509Data/> nodes to store the key information.
19+
info = xmlsec.ensure_key_info(xml)
20+
xmlsec.add_x509_data(info)
21+
22+
# Create a digitial signature context.
23+
ctx = xmlsec.SignatureContext()
24+
25+
# Load private key, assuming that there is no password.
26+
key = ctx.load('key.pem')
27+
28+
# Load certificate and add to the key.
29+
key.load_certificate('cert.pem')
30+
31+
# Sign the template.
32+
ctx.sign(xml)
33+
34+
# Print out the message.
35+
text = etree.tostring(xml, pretty_print=True).decode('utf8')
36+
print(text)

xmlsec.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <xmlsec/crypto.h>
2+
#include <xmlsec/xmltree.h>
3+
#include <xmlsec/xmldsig.h>
4+
#include <xmlsec/xmlenc.h>
5+
#include <xmlsec/errors.h>

xmlsec.pxd

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
from tree cimport xmlNode, xmlDoc, xmlChar, const_xmlChar
3+
from etreepublic cimport LXML_VERSION_STRING
4+
5+
cdef extern from "libxml/parser.h":
6+
cdef const_xmlChar* XML_DEFAULT_VERSION
7+
8+
cdef extern from "xmlsec.h":
9+
# [xmlsec.c]
10+
int xmlSecInit() nogil
11+
int xmlSecShutdown() nogil
12+
13+
# [app.c]
14+
int xmlSecCryptoInit() nogil
15+
int xmlSecCryptoShutdown() nogil
16+
17+
int xmlSecCryptoAppInit(char* name) nogil
18+
int xmlSecCryptoAppShutdown() nogil
19+
20+
# [transforms.c]
21+
cdef struct _xmlSecTransformKlass:
22+
const_xmlChar* name
23+
const_xmlChar* href
24+
25+
ctypedef _xmlSecTransformKlass *xmlSecTransformId
26+
27+
xmlSecTransformId xmlSecTransformInclC14NId
28+
xmlSecTransformId xmlSecTransformInclC14NWithCommentsId
29+
xmlSecTransformId xmlSecTransformInclC14N11Id
30+
xmlSecTransformId xmlSecTransformInclC14N11WithCommentsId
31+
xmlSecTransformId xmlSecTransformExclC14NId
32+
xmlSecTransformId xmlSecTransformExclC14NWithCommentsId
33+
xmlSecTransformId xmlSecTransformEnvelopedId
34+
xmlSecTransformId xmlSecTransformXPathId
35+
xmlSecTransformId xmlSecTransformXPath2Id
36+
xmlSecTransformId xmlSecTransformXPointerId
37+
xmlSecTransformId xmlSecTransformXsltId
38+
xmlSecTransformId xmlSecTransformRemoveXmlTagsC14NId
39+
xmlSecTransformId xmlSecTransformVisa3DHackId
40+
xmlSecTransformId xmlSecTransformAes128CbcId
41+
xmlSecTransformId xmlSecTransformAes192CbcId
42+
xmlSecTransformId xmlSecTransformAes256CbcId
43+
xmlSecTransformId xmlSecTransformKWAes128Id
44+
xmlSecTransformId xmlSecTransformKWAes192Id
45+
xmlSecTransformId xmlSecTransformKWAes256Id
46+
xmlSecTransformId xmlSecTransformDes3CbcId
47+
xmlSecTransformId xmlSecTransformKWDes3Id
48+
xmlSecTransformId xmlSecTransformDsaSha1Id
49+
xmlSecTransformId xmlSecTransformHmacMd5Id
50+
xmlSecTransformId xmlSecTransformHmacRipemd160Id
51+
xmlSecTransformId xmlSecTransformHmacSha1Id
52+
xmlSecTransformId xmlSecTransformHmacSha224Id
53+
xmlSecTransformId xmlSecTransformHmacSha256Id
54+
xmlSecTransformId xmlSecTransformHmacSha384Id
55+
xmlSecTransformId xmlSecTransformHmacSha512Id
56+
xmlSecTransformId xmlSecTransformMd5Id
57+
xmlSecTransformId xmlSecTransformRipemd160Id
58+
xmlSecTransformId xmlSecTransformRsaMd5Id
59+
xmlSecTransformId xmlSecTransformRsaRipemd160Id
60+
xmlSecTransformId xmlSecTransformRsaSha1Id
61+
xmlSecTransformId xmlSecTransformRsaSha224Id
62+
xmlSecTransformId xmlSecTransformRsaSha256Id
63+
xmlSecTransformId xmlSecTransformRsaSha384Id
64+
xmlSecTransformId xmlSecTransformRsaSha512Id
65+
xmlSecTransformId xmlSecTransformRsaPkcs1Id
66+
xmlSecTransformId xmlSecTransformRsaOaepId
67+
xmlSecTransformId xmlSecTransformSha1Id
68+
xmlSecTransformId xmlSecTransformSha224Id
69+
xmlSecTransformId xmlSecTransformSha256Id
70+
xmlSecTransformId xmlSecTransformSha384Id
71+
xmlSecTransformId xmlSecTransformSha512Id
72+
73+
# [templates.c]
74+
xmlNode* xmlSecTmplSignatureCreate(
75+
xmlDoc* document,
76+
xmlSecTransformId c14n_method,
77+
xmlSecTransformId sign_method,
78+
const_xmlChar* id) nogil
79+
80+
xmlNode* xmlSecTmplSignatureAddReference(
81+
xmlNode* node,
82+
xmlSecTransformId digest_method,
83+
const_xmlChar* id,
84+
const_xmlChar* uri,
85+
const_xmlChar* type) nogil
86+
87+
xmlNode* xmlSecTmplReferenceAddTransform(
88+
xmlNode* node,
89+
xmlSecTransformId method) nogil
90+
91+
xmlNode* xmlSecTmplSignatureEnsureKeyInfo(
92+
xmlNode* node,
93+
const_xmlChar* id) nogil
94+
95+
xmlNode* xmlSecTmplKeyInfoAddKeyName(
96+
xmlNode* node,
97+
const_xmlChar* name) nogil
98+
99+
xmlNode* xmlSecTmplKeyInfoAddX509Data(xmlNode* node) nogil
100+
101+
# [xmldsig.c]
102+
ctypedef void* xmlSecKeysMngrPtr
103+
104+
struct _xmlSecDSigCtx:
105+
pass
106+
## void * userData
107+
## unsigned int flags
108+
## unsigned int flags2
109+
## xmlSecKeyInfoCtx keyInfoReadCtx
110+
## xmlSecKeyInfoCtx keyInfoWriteCtx
111+
## xmlSecTransformCtx transformCtx
112+
## xmlSecTransformUriType enabledReferenceUris
113+
## xmlSecPtrListPtr enabledReferenceTransforms
114+
## xmlSecTransformCtxPreExecuteCallback referencePreExecuteCallback
115+
## xmlSecTransformId defSignMethodId
116+
## xmlSecTransformId defC14NMethodId
117+
## xmlSecTransformId defDigestMethodId
118+
# xmlSecKeyPtr signKey
119+
## xmlSecTransformOperation operation
120+
## xmlSecBufferPtr result
121+
# xmlSecDSigStatus status
122+
## xmlSecTransformPtr signMethod
123+
## xmlSecTransformPtr c14nMethod
124+
## xmlSecTransformPtr preSignMemBufMethod
125+
## xmlNodePtr signValueNode
126+
## xmlChar * id
127+
## xmlSecPtrList signedInfoReferences
128+
## xmlSecPtrList manifestReferences
129+
## void * reserved0
130+
## void * reserved1
131+
132+
ctypedef _xmlSecDSigCtx* xmlSecDSigCtxPtr
133+
134+
xmlSecDSigCtxPtr xmlSecDSigCtxCreate(xmlSecKeysMngrPtr manager) nogil
135+
136+
void xmlSecDSigCtxDestroy(xmlSecDSigCtxPtr ctx) nogil

0 commit comments

Comments
 (0)