ó
MvWc        	   @   sÎ  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z m	 Z	 m
 Z
 m Z y d d l Z Wn e k
 r‹ d Z n Xd Z d Z d Z d Z d	 Z d
 Z e d „ Z d „  Z d d e e e d „ Z e d „ Z d „  Z e d „ Z e d „ Z e d „ Z e  d k rÊd Z! e e! ƒ GHe e! d d d ƒ Z" e e" GHd Z! e e! d d d d d e d e ƒZ" e! e e" GHe e# d ƒ j$ ƒ  ƒ Z% e e" d e" d e% d e ƒsÊt& ‚ n  d S(   s,   Pythonic XML Security Library implementationiÿÿÿÿN(   t   StringIO(   t   BIOt   EVPt   RSAt   X509t   m2s÷  
<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>
sÊ   
<?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>
sO  
<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>
s•   <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
%(signed_info)s
<SignatureValue>%(signature_value)s</SignatureValue>
%(key_info)s
</Signature>s£   
<KeyInfo>
  <KeyValue>
    <RSAKeyValue>
      <Modulus>%(modulus)s</Modulus>
      <Exponent>%(exponent)s</Exponent>
    </RSAKeyValue>
  </KeyValue>
</KeyInfo>
sì   
<KeyInfo>
    <X509Data>
        <X509IssuerSerial>
            <X509IssuerName>%(issuer_name)s</X509IssuerName>
            <X509SerialNumber>%(serial_number)s</X509SerialNumber>
        </X509IssuerSerial>
    </X509Data>
</KeyInfo>
c         C   sv   t  ƒ  } t d k	 rC t j j t  |  ƒ ƒ } | j | d | ƒn) d d l m } | |  ƒ j | d | ƒ| j ƒ  S(   s@   Return the canonical (c14n) form of the xml document for hashingt	   exclusivei   (   t   SimpleXMLElementN(	   R    t   lxmlt   Nonet   etreet   parset
   write_c14nt	   simplexmlR   t   getvalue(   t   xmlt   c14n_exct   outputt   etR   (    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   canonicalizeb   s    	c         C   s   t  j t j |  ƒ j ƒ  ƒ S(   s/   Create a SHA1 hash and return the base64 string(   t   base64t	   b64encodet   hashlibt   sha1t   digest(   t   payload(    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   sha1_hash_digestq   s    c            s±   t  |  | ƒ } | i | d 6t | ƒ d 6}	 t  |	 | ƒ }	 t j | ‡  f d †  ƒ }
 |
 j t j |	 ƒ j ƒ  ƒ } i | d 6| d 6|	 d 6t j	 | ƒ d 6t
 |
 | | ƒ d 6S(   sI   Sign an XML document usign RSA (templates: enveloped -ref- or enveloping)t   ref_urit   digest_valuec             s   ˆ  S(   N(    (   t   argst   kwargs(   t   password(    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   <lambda>   s    t   ref_xmlt   signed_infot   signature_valuet   key_info(   R   R   R   t   load_keyt   signR   R   R   R   R   R$   (   R   R   t   private_keyR   t   certR   t   sign_templatet   key_info_templateR!   R"   t   pkeyt	   signature(    (   R   sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   rsa_signv   s    c         C   sª   | j  d ƒ r0 t j | ƒ } t j | ƒ } n t j t ƒ } t j ƒ  } | j	 | ƒ | j
 d d ƒ | j ƒ  | j t |  | ƒ ƒ | j t j | ƒ ƒ } | d k S(   sD   Verify a XML document signature usign RSA-SHA1, return True if valids   -----BEGIN PUBLIC KEY-----t   mdR   i   (   t
   startswithR   t   MemoryBufferR   t   load_pub_key_biot   load_pub_keyt   certificateR   t   PKeyt
   assign_rsat   reset_contextt   verify_initt   verify_updateR   t   verify_finalR   t	   b64decode(   R   R,   t   keyR   t   biot   rsat   pubkeyt   ret(    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt
   rsa_verifyŒ   s    
c         C   s©   t  j |  j d ƒ } t j t j |  j ƒ ƒ j d ƒ j d ƒ } | rU t	 | ƒ n d	 } | i | d 6| d 6| r‡ | j ƒ  j ƒ  n d d 6| r  | j ƒ  n d d 6S(
   sH   Convert private key (PEM) to XML Signature format (RSAKeyValue/X509Data)i   t   hexR   t   modulust   exponentt    t   issuer_namet   serial_numberN(   R   R   t   eR   t	   bn_to_hext	   mpi_to_bnt   nt   decodet   encodet   x509_parse_certR	   t
   get_issuert   as_textt   get_serial_number(   R+   R(   R*   RC   RB   t   x509(    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyR$   ¡   s    -c         C   sy   | r- t  j |  ƒ } t j | t j ƒ } nH |  j d ƒ rc t  j |  ƒ } t j | t j ƒ } n t j |  d ƒ } | S(   sE   Create a X509 certificate from binary DER, plain text PEM or filenames   -----BEGIN CERTIFICATE-----i   (   R   R0   R   t   load_cert_biot
   FORMAT_DERR/   t
   FORMAT_PEMt	   load_cert(   R(   t   binaryR<   RQ   (    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyRM   ±   s    c         C   s%   t  |  | ƒ } | j ƒ  j ƒ  j ƒ  S(   s:   Return the public key (PEM format) from a X509 certificate(   RM   t
   get_pubkeyt   get_rsat   as_pem(   R(   RV   RQ   (    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   x509_extract_rsa_public_key¾   s    c         C   s.   t  |  ƒ } t  | | ƒ } | j | j ƒ  ƒ S(   sG   Validate the certificate's authenticity using a certification authority(   RM   t   verifyRW   (   t   cacertR(   RV   t   cat   crt(    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   x509_verifyÄ   s    t   __main__sL   <Object xmlns="http://www.w3.org/2000/09/xmldsig#" Id="object">data</Object>s   #objects   no_encriptada.keyR   s=   <?xml version="1.0" encoding="UTF-8"?><Object>data%s</Object>RD   R)   R   s   zunimercado.crtR"   R#   ('   t   __doc__R   R   t   ost	   cStringIOR    t   M2CryptoR   R   R   R   R   t
   lxml.etreeR   t   ImportErrorR	   t   SIGN_REF_TMPLt   SIGNED_TMPLt   SIGN_ENV_TMPLt   SIGNATURE_TMPLt   KEY_INFO_RSA_TMPLt   KEY_INFO_X509_TMPLt   TrueR   R   R-   R@   R$   t   FalseRM   RZ   R_   t   __name__t
   sample_xmlt   varst   opent   readt
   public_keyt   AssertionError(    (    (    sL   /data/av2000/soap/soapenv/lib/python2.7/site-packages/pysimplesoap/xmlsec.pyt   <module>   sH   (
					