live
     ˆÌËÊ392É¾Ç ˆ    ‹      •’ÁNƒ@†ï>Å¸o2ëE—5
MlRÛ¦Åc<,ì6R–ìN­õé…RSMlL/ÀÂ7_æ†Çã(yžôá>yÂäñn8ˆ€�">]DˆqwzÁÙ9bÄ€Dõâjµ
V�u9&S,hQöÐ“3ŠG¼}'Ž€Zªæœ•Z*¥?ÀÎí’¼Qº‡Kï°4)NÛ«“n�¹3j#åØU5ÜŠxjÕºõŸìjJ6�Èt£l�àTÀ»,M^…ŒlÍ7‹¼ËB†&³•Ç´”Õ[�›9YRÈ^Ñøµá�Š¶Zp	…Óó�ÝDáèzÆLŒäBs”3sËÄPz‚…Ufn´ÚÏ:xf>÷ãŽ‰µÏœ©ÉØj‡bñgÔÌ–¾–MÖË&Aá~R¢þ›†Ì~cr;í�’x0íF¢ZÅ®»ÿétE§3²n½í½�T©¯¯¿�°íÌ™¼ & NáOò�Ð¸]Ž›ÅâØ-î¾¿û†        ˆÄ�þD„³mÊè†óÏ×�üÿÃƒxM½ÊÂ¾Ç  B    #!/usr/bin/python
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

"""Pythonic XML Security Library implementation"""
from __future__ import print_function
import base64
import hashlib
import os
from cStringIO import StringIO
from M2Crypto import BIO, EVP, RSA, X509, m2

# if lxml is not installed, use c14n.py native implementation
try:
    import lxml.etree
except ImportError:
    lxml = None
    
# Features:
#  * Uses M2Crypto and lxml (libxml2) but it is independent from libxmlsec1
#  * Sign, Verify, Encrypt & Decrypt XML documents

# Enveloping templates ("by reference": signature is parent):
SIGN_REF_TMPL = """
<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
  <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
  <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
  <Reference URI="%(ref_uri)s">
    <Transforms>
      <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    </Transforms>
    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    <DigestValue>%(digest_value)s</DigestValue>
  </Reference>
</SignedInfo>
"""
SIGNED_TMPL = """
<?xml version="1.0" encoding="UTF-8"?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
%(signed_info)s
<SignatureValue>%(signature_value)s</SignatureValue>
%(key_info)s
%(ref_xml)s
</Signature>
"""

# Enveloped templates (signature is child, the reference is the root object):
SIGN_ENV_TMPL = """
<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
  <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
  <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
  <Reference URI="">
    <Transforms>
       <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
       <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
    </Transforms>
    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
    <DigestValue>%(digest_value)s</DigestValue>
  </Reference>
</SignedInfo>
"""
SIGNATURE_TMPL = """<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
%(signed_info)s
<SignatureValue>%(signature_value)s</SignatureValue>
%(key_info)s
</Signature>"""

KEY_INFO_RSA_TMPL = """
<KeyInfo>
  <KeyValue>
    <RSAKeyValue>
      <Modulus>%(modulus)s</Modulus>
      <Exponent>%(exponent)s</Exponent>
    </RSAKeyValue>
  </KeyValue>
</KeyInfo>
"""

KEY_INFO_X509_TMPL = """
<KeyInfo>
    <X509Data>
        <X509IssuerSerial>
            <X509IssuerName>%(issuer_name)s</X509IssuerName>
            <X509SerialNumber>%(serial_number)s</X509SerialNumber>
        </X509IssuerSerial>
    </X509Data>
</KeyInfo>
"""

def canonicalize(xml, c14n_exc=True):
    "Return the canonical (c14n) form of the xml document for hashing"
    # UTF8, normalization of line feeds/spaces, quoting, attribute ordering...
    output = StringIO()
    if lxml is not None:
        # use faster libxml2 / lxml canonicalization function if available
        et = lxml.etree.parse(StringIO(xml))
        et.write_c14n(output, exclusive=c14n_exc)
    else:
        # use pure-python implementation: c14n.py (avoid recursiv