Skip to content

Commit 3ab6bf9

Browse files
author
Leif Johansson
committed
logging cleanup
1 parent a4dfea1 commit 3ab6bf9

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

src/xmlsec/__init__.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
DS = ElementMaker(namespace=NS['ds'], nsmap=NSDefault)
2222

2323

24+
log = logging.getLogger('xmlsec')
25+
2426
class Config(object):
2527
"""
2628
This class holds a set of configuration parameters (using pyconfig) for pyXMLSecurity:
@@ -91,7 +93,7 @@ def _signed_value(data, key_size, do_pad, hash_alg): # TODO Do proper asn1 CMS
9193

9294
def _get_by_id(t, id_v):
9395
for id_a in config.id_attributes:
94-
logging.debug("Looking for #%s using id attribute '%s'" % (id_v, id_a))
96+
log.debug("Looking for #%s using id attribute '%s'" % (id_v, id_a))
9597
elts = t.xpath("//*[@%s='%s']" % (id_a, id_v))
9698
if elts is not None and len(elts) > 0:
9799
return elts[0]
@@ -173,21 +175,21 @@ def _process_references(t, sig, verify_mode=True, sig_path=".//{%s}Signature" %
173175
fd.write(obj)
174176

175177
hash_alg = _ref_digest(ref)
176-
logging.debug("using hash algorithm %s" % hash_alg)
178+
log.debug("using hash algorithm %s" % hash_alg)
177179
digest = xmlsec.crypto._digest(obj, hash_alg)
178-
logging.debug("computed %s digest %s for ref %s" % (hash_alg, digest, uri))
180+
log.debug("computed %s digest %s for ref %s" % (hash_alg, digest, uri))
179181
dv = ref.find(".//{%s}DigestValue" % NS['ds'])
180182

181183
if verify_mode:
182-
logging.debug("found %s digest %s for ref %s" % (hash_alg, dv.text, uri))
184+
log.debug("found %s digest %s for ref %s" % (hash_alg, dv.text, uri))
183185
computed_digest_binary = b64d(digest)
184186
digest_binary = b64d(dv.text)
185187
if digest_binary == computed_digest_binary: # no point in verifying signature if the digest doesn't match
186188
verified_objects[ref] = obj_copy
187189
else:
188-
logging.error("not returning ref %s - digest mismatch" % uri)
190+
log.error("not returning ref %s - digest mismatch" % uri)
189191
else: # signing - lets store the digest
190-
logging.debug("replacing digest in %s" % etree.tostring(dv))
192+
log.debug("replacing digest in %s" % etree.tostring(dv))
191193
dv.text = digest
192194

193195

@@ -301,12 +303,12 @@ def _verify(t, keyspec, sig_path=".//{%s}Signature" % NS['ds'], drop_signature=F
301303
if not sv:
302304
raise XMLSigException("No SignatureValue")
303305

304-
logging.debug("SignatureValue: {!s}".format(sv))
306+
log.debug("SignatureValue: {!s}".format(sv))
305307
this_cert = xmlsec.crypto.from_keyspec(keyspec, signature_element=sig)
306-
logging.debug("key size: {!s} bits".format(this_cert.keysize))
308+
log.debug("key size: {!s} bits".format(this_cert.keysize))
307309

308310
si = sig.find(".//{%s}SignedInfo" % NS['ds'])
309-
logging.debug("Found signedinfo {!s}".format(etree.tostring(si)))
311+
log.debug("Found signedinfo {!s}".format(etree.tostring(si)))
310312
cm_alg = _cm_alg(si)
311313
try:
312314
sig_digest_alg = _sig_alg(si)
@@ -316,12 +318,12 @@ def _verify(t, keyspec, sig_path=".//{%s}Signature" % NS['ds'], drop_signature=F
316318
refmap = _process_references(t, sig, verify_mode=True, sig_path=sig_path, drop_signature=drop_signature)
317319
for ref,obj in refmap.items():
318320

319-
logging.debug("transform %s on %s" % (cm_alg, etree.tostring(si)))
321+
log.debug("transform %s on %s" % (cm_alg, etree.tostring(si)))
320322
sic = _transform(cm_alg, si)
321-
logging.debug("SignedInfo C14N: %s" % sic)
323+
log.debug("SignedInfo C14N: %s" % sic)
322324
if this_cert.do_digest:
323325
digest = xmlsec.crypto._digest(sic, sig_digest_alg)
324-
logging.debug("SignedInfo digest: %s" % digest)
326+
log.debug("SignedInfo digest: %s" % digest)
325327
b_digest = b64d(digest)
326328
actual = _signed_value(b_digest, this_cert.keysize, True, sig_digest_alg)
327329
else:
@@ -331,7 +333,7 @@ def _verify(t, keyspec, sig_path=".//{%s}Signature" % NS['ds'], drop_signature=F
331333
raise XMLSigException("Failed to validate {!s} using sig digest {!s} and cm {!s}".format(etree.tostring(sig), sig_digest_alg, cm_alg))
332334
validated.append(obj)
333335
except XMLSigException, ex:
334-
logging.error(ex)
336+
log.error(ex)
335337

336338
if not validated:
337339
raise XMLSigException("No valid ds:Signature elements found")
@@ -432,7 +434,7 @@ def sign(t, key_spec, cert_spec=None, reference_uri='', insert_index=0, sig_path
432434
raise XMLSigException("Public and private key sizes do not match ({!s}, {!s})".format(
433435
public.keysize, private.keysize))
434436
# This might be incorrect for PKCS#11 tokens if we have no public key
435-
logging.debug("Using {!s} bit key".format(private.keysize))
437+
log.debug("Using {!s} bit key".format(private.keysize))
436438

437439
templates = filter(_is_template, t.findall(sig_path))
438440
if not templates:
@@ -446,30 +448,30 @@ def sign(t, key_spec, cert_spec=None, reference_uri='', insert_index=0, sig_path
446448
fd.write(etree.tostring(root_elt(t)))
447449

448450
for sig in templates:
449-
logging.debug("processing sig template: %s" % etree.tostring(sig))
451+
log.debug("processing sig template: %s" % etree.tostring(sig))
450452
si = sig.find(".//{%s}SignedInfo" % NS['ds'])
451453
assert si is not None
452454
cm_alg = _cm_alg(si)
453455
sig_alg = _sig_alg(si)
454456

455457
_process_references(t, sig, verify_mode=False, sig_path=sig_path)
456458
# XXX create signature reference duplicates/overlaps process references unless a c14 is part of transforms
457-
logging.debug("transform %s on %s" % (cm_alg, etree.tostring(si)))
459+
log.debug("transform %s on %s" % (cm_alg, etree.tostring(si)))
458460
sic = _transform(cm_alg, si)
459-
logging.debug("SignedInfo C14N: %s" % sic)
461+
log.debug("SignedInfo C14N: %s" % sic)
460462

461463
# sign hash digest and insert it into the XML
462464
if private.do_digest:
463465
digest = xmlsec.crypto._digest(sic, sig_alg)
464-
logging.debug("SignedInfo digest: %s" % digest)
466+
log.debug("SignedInfo digest: %s" % digest)
465467
b_digest = b64d(digest)
466468
tbs = _signed_value(b_digest, private.keysize, private.do_padding, sig_alg)
467469
else:
468470
tbs = sic
469471

470472
signed = private.sign(tbs, sig_alg)
471473
signature = b64e(signed)
472-
logging.debug("SignatureValue: %s" % signature)
474+
log.debug("SignatureValue: %s" % signature)
473475
sv = sig.find(".//{%s}SignatureValue" % NS['ds'])
474476
if sv is None:
475477
si.addnext(DS.SignatureValue(signature))

0 commit comments

Comments
 (0)