ó
l]›]c           @` sØ   d  Z  d d l m Z m Z m Z m 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 e j d k  rš d d l m Z n d d	 l m Z d
 „  Z d d „ Z d „  Z e d „ Z d S(   uq   
Encoding DER to PEM and decoding PEM to DER. Exports the following items:

 - armor()
 - detect()
 - unarmor()

i    (   t   unicode_literalst   divisiont   absolute_importt   print_functionNi   (   t   unwrap(   t	   type_namet   str_clst   byte_clsi   (   t   StringIO(   t   BytesIOc         C` sU   t  |  t ƒ s- t t d t |  ƒ ƒ ƒ ‚ n  |  j d ƒ d k pT |  j d ƒ d k S(   uð   
    Detect if a byte string seems to contain a PEM-encoded block

    :param byte_string:
        A byte string to look through

    :return:
        A boolean, indicating if a PEM-encoded block is contained in the byte
        string
    uC   
            byte_string must be a byte string, not %s
            s
   -----BEGINiÿÿÿÿs
   ---- BEGIN(   t
   isinstanceR   t	   TypeErrorR   t
   _type_namet   find(   t   byte_string(    (    s-   lib/python2.7/site-packages/asn1crypto/pem.pyt   detect   s
    c         C` s¥  t  | t ƒ s. t t d t | ƒ ƒ ƒ ‚ n  t  |  t ƒ s[ t t d t |  ƒ ƒ ƒ ‚ n  |  j ƒ  j d ƒ }  t ƒ  } | j	 d ƒ | j	 |  ƒ | j	 d ƒ | rxX | D]P } | j	 | j d ƒ ƒ | j	 d ƒ | j	 | | j d ƒ ƒ | j	 d ƒ q­ W| j	 d ƒ n  t
 j | ƒ } t | ƒ } d } x? | | k  rs| j	 | | | d	 !ƒ | j	 d ƒ | d	 7} q5W| j	 d
 ƒ | j	 |  ƒ | j	 d ƒ | j ƒ  S(   u  
    Armors a DER-encoded byte string in PEM

    :param type_name:
        A unicode string that will be capitalized and placed in the header
        and footer of the block. E.g. "CERTIFICATE", "PRIVATE KEY", etc. This
        will appear as "-----BEGIN CERTIFICATE-----" and
        "-----END CERTIFICATE-----".

    :param der_bytes:
        A byte string to be armored

    :param headers:
        An OrderedDict of the header lines to write after the BEGIN line

    :return:
        A byte string of the PEM block
    uA   
            der_bytes must be a byte string, not %s
            uD   
            type_name must be a unicode string, not %s
            u   asciis   -----BEGIN s   -----
s   : s   
i    i@   s	   -----END (   R
   R   R   R   R   R   t   uppert   encodeR	   t   writet   base64t	   b64encodet   lent   getvalue(   R   t	   der_bytest   headerst   outputt   keyt	   b64_bytest   b64_lent   i(    (    s-   lib/python2.7/site-packages/asn1crypto/pem.pyt   armor2   s<    	c         c` sÌ  t  |  t ƒ s- t t d t |  ƒ ƒ ƒ ‚ n  d } i  } d } d } t } t } xQ|  j t ƒ D]@} | d k ry qa n  | d k rÍ t j	 d | ƒ } | s£ qa n  | j
 d ƒ j d ƒ } t } d } qa n  | d k r4| j d ƒ d	 k r÷ d
 } q4| j d ƒ }	 |	 j d d ƒ \ }
 } | j ƒ  | |
 <qa n  | d
 k ra | d d !d k r”t j | ƒ } | | | f Vd } i  } d } d } t } qa n  | | 7} qa qa W| s³| rÈt t d ƒ ƒ ‚ n  d S(   ux  
    Convert a PEM-encoded byte string into one or more DER-encoded byte strings

    :param pem_bytes:
        A byte string of the PEM-encoded data

    :raises:
        ValueError - when the pem_bytes do not appear to be PEM-encoded bytes

    :return:
        A generator of 3-element tuples in the format: (object_type, headers,
        der_bytes). The object_type is a unicode string of what is between
        "-----BEGIN " and "-----". Examples include: "CERTIFICATE",
        "PUBLIC KEY", "PRIVATE KEY". The headers is a dict containing any lines
        in the form "Name: Value" that are right after the begin line.
    uA   
            pem_bytes must be a byte string, not %s
            u   trasht    s1   ^(?:---- |-----)BEGIN ([A-Z0-9 ]+)(?: ----|-----)i   u   asciiu   headerst   :iÿÿÿÿu   bodyu   :i    i   s   -----s   ---- u|   
            pem_bytes does not appear to contain PEM-encoded data - no
            BEGIN/END combination found
            N(   s   -----s   ---- (   R
   R   R   R   R   t   Nonet   Falset
   splitlinest   ret   matcht   groupt   decodet   TrueR   t   splitt   stripR   t	   b64decodet
   ValueError(   t	   pem_bytest   stateR   t   base64_datat   object_typet   found_startt	   found_endt   linet   type_name_matcht   decoded_linet   namet   valueR   (    (    s-   lib/python2.7/site-packages/asn1crypto/pem.pyt   _unarmorp   sT    	c         C` s    t  |  ƒ } | s t | ƒ S| S(   u˜  
    Convert a PEM-encoded byte string into a DER-encoded byte string

    :param pem_bytes:
        A byte string of the PEM-encoded data

    :param multiple:
        If True, function will return a generator

    :raises:
        ValueError - when the pem_bytes do not appear to be PEM-encoded bytes

    :return:
        A 3-element tuple (object_name, headers, der_bytes). The object_name is
        a unicode string of what is between "-----BEGIN " and "-----". Examples
        include: "CERTIFICATE", "PUBLIC KEY", "PRIVATE KEY". The headers is a
        dict containing any lines in the form "Name: Value" that are right
        after the begin line.
    (   R8   t   next(   R-   t   multiplet	   generator(    (    s-   lib/python2.7/site-packages/asn1crypto/pem.pyt   unarmorÄ   s    
(   i   (   t   __doc__t
   __future__R    R   R   R   R   R$   t   syst   _errorsR   t   _typesR   R   R   R   t   version_infot	   cStringIOR   R	   t   ioR   R!   R   R8   R"   R<   (    (    (    s-   lib/python2.7/site-packages/asn1crypto/pem.pyt   <module>
   s   "	>	T