|
| 1 | +import logging |
| 2 | +import six |
| 3 | +import xmlsec |
| 4 | +import xmlsec.exceptions |
| 5 | +import xmlsec.crypto |
| 6 | +from lxml.builder import ElementMaker |
| 7 | +from lxml import etree as etree |
| 8 | +from xmlsec.utils import pem2b64, b64e |
| 9 | + |
| 10 | +NS = {'ds': 'http://www.w3.org/2000/09/xmldsig#'} |
| 11 | +NSDefault = {None: 'http://www.w3.org/2000/09/xmldsig#'} |
| 12 | +DS = ElementMaker(namespace=NS['ds'], nsmap=NSDefault) |
| 13 | + |
| 14 | +class Signer(object): |
| 15 | + def __init__(self, key_spec, cert_spec=None, debug=False): |
| 16 | + """ |
| 17 | + :param key_spec: private key reference, see xmlsec.crypto.from_keyspec() for syntax. |
| 18 | + :param cert_spec: None or public key reference (to add cert to document), |
| 19 | + see xmlsec.crypto.from_keyspec() for syntax. |
| 20 | + """ |
| 21 | + self.log = logging.getLogger('xmlsec') |
| 22 | + self.debug = debug |
| 23 | + self.private = xmlsec.crypto.from_keyspec(key_spec, private=True) |
| 24 | + self.public = None |
| 25 | + if cert_spec is not None: |
| 26 | + self.public = xmlsec.crypto.from_keyspec(cert_spec) |
| 27 | + if self.public is None: |
| 28 | + raise xmlsec.exceptions.XMLSigException("Unable to load public key from '%s'" % cert_spec) |
| 29 | + if self.public.keysize and self.private.keysize: # XXX maybe one set and one not set should also raise exception? |
| 30 | + if self.public.keysize != self.private.keysize: |
| 31 | + raise xmlsec.exceptions.XMLSigException("Public and private key sizes do not match ({!s}, {!s})".format( |
| 32 | + self.public.keysize, self.private.keysize)) |
| 33 | + # This might be incorrect for PKCS#11 tokens if we have no public key |
| 34 | + self.log.debug("Using {!s} bit key".format(self.private.keysize)) |
| 35 | + |
| 36 | + |
| 37 | + def sign(self, t, reference_uri='', insert_index=0, sig_path=".//{%s}Signature" % NS['ds']): |
| 38 | + """ |
| 39 | + Sign an XML document. This means to 'complete' all Signature elements in the XML. |
| 40 | +
|
| 41 | + :param t: XML as lxml.etree |
| 42 | + :param sig_path: An xpath expression identifying the Signature template element |
| 43 | + :param reference_uri: Envelope signature reference URI |
| 44 | + :param insert_index: Insertion point for the Signature element, |
| 45 | + Signature is inserted at beginning by default |
| 46 | + :returns: XML as lxml.etree (for convenience, 't' is modified in-place) |
| 47 | + """ |
| 48 | + sig_paths = t.findall(sig_path) |
| 49 | + templates = list(filter(xmlsec._is_template, sig_paths)) |
| 50 | + if not templates: |
| 51 | + tmpl = xmlsec.add_enveloped_signature(t, reference_uri=reference_uri, pos=insert_index) |
| 52 | + templates = [tmpl] |
| 53 | + |
| 54 | + assert templates, xmlsec.exceptions.XMLSigException("Failed to both find and add a signing template") |
| 55 | + |
| 56 | + if self.debug: |
| 57 | + with open("/tmp/sig-ref.xml", "w") as fd: |
| 58 | + fd.write(etree_to_string(root_elt(t))) |
| 59 | + |
| 60 | + for sig in templates: |
| 61 | + self.log.debug("processing sig template: %s" % etree.tostring(sig)) |
| 62 | + si = sig.find(".//{%s}SignedInfo" % NS['ds']) |
| 63 | + assert si is not None |
| 64 | + cm_alg = xmlsec._cm_alg(si) |
| 65 | + sig_alg = xmlsec._sig_alg(si) |
| 66 | + |
| 67 | + xmlsec._process_references(t, sig, verify_mode=False, sig_path=sig_path) |
| 68 | + # XXX create signature reference duplicates/overlaps process references unless a c14 is part of transforms |
| 69 | + self.log.debug("transform %s on %s" % (cm_alg, etree.tostring(si))) |
| 70 | + sic = xmlsec._transform(cm_alg, si) |
| 71 | + self.log.debug("SignedInfo C14N: %s" % sic) |
| 72 | + |
| 73 | + # sign hash digest and insert it into the XML |
| 74 | + if self.private.do_digest: |
| 75 | + digest = xmlsec.crypto._digest(sic, sig_alg) |
| 76 | + self.log.debug("SignedInfo digest: %s" % digest) |
| 77 | + b_digest = b64d(digest) |
| 78 | + tbs = xmlsec._signed_value(b_digest, private.keysize, private.do_padding, sig_alg) |
| 79 | + else: |
| 80 | + tbs = sic |
| 81 | + |
| 82 | + signed = self.private.sign(tbs, sig_alg) |
| 83 | + signature = b64e(signed) |
| 84 | + if isinstance(signature, six.binary_type): |
| 85 | + signature = six.text_type(signature, 'utf-8') |
| 86 | + self.log.debug("SignatureValue: %s" % signature) |
| 87 | + sv = sig.find(".//{%s}SignatureValue" % NS['ds']) |
| 88 | + if sv is None: |
| 89 | + si.addnext(DS.SignatureValue(signature)) |
| 90 | + else: |
| 91 | + sv.text = signature |
| 92 | + |
| 93 | + for cert_src in (self.public, self.private): |
| 94 | + if cert_src is not None and cert_src.cert_pem: |
| 95 | + # Insert cert_data as b64-encoded X.509 certificate into XML document |
| 96 | + sv_elt = si.getnext() |
| 97 | + sv_elt.addnext(DS.KeyInfo(DS.X509Data(DS.X509Certificate(pem2b64(cert_src.cert_pem))))) |
| 98 | + break # add the first we find, no more |
| 99 | + |
| 100 | + return t |
0 commit comments