|
16 | 16 | from xmlsec.exceptions import XMLSigException
|
17 | 17 | from xmlsec import constants
|
18 | 18 | from xmlsec.utils import parse_xml, pem2b64, unescape_xml_entities, delete_elt, root_elt, b64d, b64e, etree_to_string
|
| 19 | +from xmlsec.Signer import Signer |
19 | 20 | import xmlsec.crypto
|
20 | 21 | import pyconfig
|
21 | 22 |
|
@@ -453,74 +454,8 @@ def sign(t, key_spec, cert_spec=None, reference_uri='', insert_index=0, sig_path
|
453 | 454 | Signature is inserted at beginning by default
|
454 | 455 | :returns: XML as lxml.etree (for convenience, 't' is modified in-place)
|
455 | 456 | """
|
456 |
| - private = xmlsec.crypto.from_keyspec(key_spec, private=True) |
457 |
| - |
458 |
| - public = None |
459 |
| - if cert_spec is not None: |
460 |
| - public = xmlsec.crypto.from_keyspec(cert_spec) |
461 |
| - if public is None: |
462 |
| - raise XMLSigException("Unable to load public key from '%s'" % cert_spec) |
463 |
| - if public.keysize and private.keysize: # XXX maybe one set and one not set should also raise exception? |
464 |
| - if public.keysize != private.keysize: |
465 |
| - raise XMLSigException("Public and private key sizes do not match ({!s}, {!s})".format( |
466 |
| - public.keysize, private.keysize)) |
467 |
| - # This might be incorrect for PKCS#11 tokens if we have no public key |
468 |
| - log.debug("Using {!s} bit key".format(private.keysize)) |
469 |
| - sig_paths = t.findall(sig_path) |
470 |
| - templates = list(filter(_is_template, sig_paths)) |
471 |
| - if not templates: |
472 |
| - tmpl = add_enveloped_signature(t, reference_uri=reference_uri, pos=insert_index) |
473 |
| - templates = [tmpl] |
474 |
| - |
475 |
| - assert templates, XMLSigException("Failed to both find and add a signing template") |
476 |
| - |
477 |
| - if config.debug_write_to_files: |
478 |
| - with open("/tmp/sig-ref.xml", "w") as fd: |
479 |
| - fd.write(etree_to_string(root_elt(t))) |
480 |
| - |
481 |
| - for sig in templates: |
482 |
| - log.debug("processing sig template: %s" % etree.tostring(sig)) |
483 |
| - si = sig.find(".//{%s}SignedInfo" % NS['ds']) |
484 |
| - assert si is not None |
485 |
| - cm_alg = _cm_alg(si) |
486 |
| - sig_uri = _sig_uri(si) |
487 |
| - |
488 |
| - _process_references(t, sig, verify_mode=False, sig_path=sig_path) |
489 |
| - # XXX create signature reference duplicates/overlaps process references unless a c14 is part of transforms |
490 |
| - log.debug("transform %s on %s" % (cm_alg, etree.tostring(si))) |
491 |
| - sic = _transform(cm_alg, si) |
492 |
| - log.debug("SignedInfo C14N: %s" % sic) |
493 |
| - |
494 |
| - # sign hash digest and insert it into the XML |
495 |
| - if private.do_digest: # assume pkcs1 v1.5 |
496 |
| - hash_alg = constants.sign_alg_xmldsig_sig_to_hashalg(sig_uri) |
497 |
| - digest = xmlsec.crypto._digest(sic, hash_alg) |
498 |
| - log.debug("SignedInfo digest: %s" % digest) |
499 |
| - b_digest = b64d(digest) |
500 |
| - tbs = _signed_value_pkcs1_v1_5(b_digest, private.keysize, private.do_padding, hash_alg) |
501 |
| - else: |
502 |
| - tbs = sic |
503 |
| - |
504 |
| - signed = private.sign(tbs, sig_uri) |
505 |
| - signature = b64e(signed) |
506 |
| - if isinstance(signature, six.binary_type): |
507 |
| - signature = six.text_type(signature, 'utf-8') |
508 |
| - log.debug("SignatureValue: %s" % signature) |
509 |
| - sv = sig.find(".//{%s}SignatureValue" % NS['ds']) |
510 |
| - if sv is None: |
511 |
| - si.addnext(DS.SignatureValue(signature)) |
512 |
| - else: |
513 |
| - sv.text = signature |
514 |
| - |
515 |
| - for cert_src in (public, private): |
516 |
| - if cert_src is not None and cert_src.cert_pem: |
517 |
| - # Insert cert_data as b64-encoded X.509 certificate into XML document |
518 |
| - sv_elt = si.getnext() |
519 |
| - sv_elt.addnext(DS.KeyInfo(DS.X509Data(DS.X509Certificate(pem2b64(cert_src.cert_pem))))) |
520 |
| - break # add the first we find, no more |
521 |
| - |
522 |
| - return t |
523 |
| - |
| 457 | + signer = Signer(key_spec=key_spec, cert_spec=cert_spec) |
| 458 | + return signer.sign(t, reference_uri, insert_index, sig_path) |
524 | 459 |
|
525 | 460 | def _cm_alg(si):
|
526 | 461 | cm = si.find(".//{%s}CanonicalizationMethod" % NS['ds'])
|
|
0 commit comments