Skip to content

Commit 8bc522a

Browse files
author
Guillaume Rousse
committed
rewrite sign() function as a call to Signer.sign() method
1 parent 43e4983 commit 8bc522a

File tree

1 file changed

+3
-67
lines changed

1 file changed

+3
-67
lines changed

src/xmlsec/__init__.py

+3-67
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from xmlsec.exceptions import XMLSigException
1515
from xmlsec import constants
1616
from xmlsec.utils import parse_xml, pem2b64, unescape_xml_entities, delete_elt, root_elt, b64d, b64e, etree_to_string
17+
from xmlsec.Signer import Signer
1718
import xmlsec.crypto
1819
import pyconfig
1920

@@ -427,73 +428,8 @@ def sign(t, key_spec, cert_spec=None, reference_uri='', insert_index=0, sig_path
427428
Signature is inserted at beginning by default
428429
:returns: XML as lxml.etree (for convenience, 't' is modified in-place)
429430
"""
430-
private = xmlsec.crypto.from_keyspec(key_spec, private=True)
431-
432-
public = None
433-
if cert_spec is not None:
434-
public = xmlsec.crypto.from_keyspec(cert_spec)
435-
if public is None:
436-
raise XMLSigException("Unable to load public key from '%s'" % cert_spec)
437-
if public.keysize and private.keysize: # XXX maybe one set and one not set should also raise exception?
438-
if public.keysize != private.keysize:
439-
raise XMLSigException("Public and private key sizes do not match ({!s}, {!s})".format(
440-
public.keysize, private.keysize))
441-
# This might be incorrect for PKCS#11 tokens if we have no public key
442-
log.debug("Using {!s} bit key".format(private.keysize))
443-
sig_paths = t.findall(sig_path)
444-
templates = list(filter(_is_template, sig_paths))
445-
if not templates:
446-
tmpl = add_enveloped_signature(t, reference_uri=reference_uri, pos=insert_index)
447-
templates = [tmpl]
448-
449-
assert templates, XMLSigException("Failed to both find and add a signing template")
450-
451-
if config.debug_write_to_files:
452-
with open("/tmp/sig-ref.xml", "w") as fd:
453-
fd.write(etree_to_string(root_elt(t)))
454-
455-
for sig in templates:
456-
log.debug("processing sig template: %s" % etree.tostring(sig))
457-
si = sig.find(".//{%s}SignedInfo" % NS['ds'])
458-
assert si is not None
459-
cm_alg = _cm_alg(si)
460-
sig_alg = _sig_alg(si)
461-
462-
_process_references(t, sig, verify_mode=False, sig_path=sig_path)
463-
# XXX create signature reference duplicates/overlaps process references unless a c14 is part of transforms
464-
log.debug("transform %s on %s" % (cm_alg, etree.tostring(si)))
465-
sic = _transform(cm_alg, si)
466-
log.debug("SignedInfo C14N: %s" % sic)
467-
468-
# sign hash digest and insert it into the XML
469-
if private.do_digest:
470-
digest = xmlsec.crypto._digest(sic, sig_alg)
471-
log.debug("SignedInfo digest: %s" % digest)
472-
b_digest = b64d(digest)
473-
tbs = _signed_value(b_digest, private.keysize, private.do_padding, sig_alg)
474-
else:
475-
tbs = sic
476-
477-
signed = private.sign(tbs, sig_alg)
478-
signature = b64e(signed)
479-
if isinstance(signature, six.binary_type):
480-
signature = six.text_type(signature, 'utf-8')
481-
log.debug("SignatureValue: %s" % signature)
482-
sv = sig.find(".//{%s}SignatureValue" % NS['ds'])
483-
if sv is None:
484-
si.addnext(DS.SignatureValue(signature))
485-
else:
486-
sv.text = signature
487-
488-
for cert_src in (public, private):
489-
if cert_src is not None and cert_src.cert_pem:
490-
# Insert cert_data as b64-encoded X.509 certificate into XML document
491-
sv_elt = si.getnext()
492-
sv_elt.addnext(DS.KeyInfo(DS.X509Data(DS.X509Certificate(pem2b64(cert_src.cert_pem)))))
493-
break # add the first we find, no more
494-
495-
return t
496-
431+
signer = Signer(key_spec=key_spec, cert_spec=cert_spec)
432+
return signer.sign(t, reference_uri, insert_index, sig_path)
497433

498434
def _cm_alg(si):
499435
cm = si.find(".//{%s}CanonicalizationMethod" % NS['ds'])

0 commit comments

Comments
 (0)