
ärVï  ã               @   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
 d „  Z d d d „ Z d d „  Z d d d „ Z d S)zq
Encoding DER to PEM and decoding PEM to DER. Exports the following items:

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

é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )Úunwrap)Ú	type_nameÚstr_clsÚbyte_clsé   )ÚStringIO)ÚBytesIOc             C   sR   t  |  t ƒ s* t t d t |  ƒ ƒ ƒ ‚ |  j d ƒ d k pQ |  j d ƒ d k S)zð
    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
    zC
            byte_string must be a byte string, not %s
            s
   -----BEGINr   s
   ---- BEGINéÿÿÿÿr   )Ú
isinstancer
   Ú	TypeErrorr   r   Úfind)Zbyte_string© r   ú0/usr/lib/python3/dist-packages/asn1crypto/pem.pyÚdetect   s
    r   c             C   sŸ  t  | t ƒ s+ t t d |  | ƒ ƒ ƒ ‚ t  |  t ƒ sU t t d |  |  ƒ ƒ ƒ ‚ |  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 ƒ t	 j
 | ƒ } t | ƒ } d } xB | | k  rm| j | | | d	 … ƒ | j d ƒ | d	 7} q,W| j d
 ƒ | j |  ƒ | j d ƒ | j ƒ  S)a  
    Armors a DER-encoded byte string in PEM

    :param der_bytes:
        A byte string to be armored

    :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 headers:
        An OrderedDict of the header lines to write after the BEGIN line

    :return:
        A byte string of the PEM block
    zA
            der_bytes must be a byte string, not %s
            zD
            type_name must be a unicode string, not %s
            Úasciis   -----BEGIN s   -----
s   : s   
r   é@   s	   -----END )r   r
   r   r   r	   ÚupperÚencoder   ÚwriteÚbase64Z	b64encodeÚlenÚgetvalue)r   Ú	der_bytesÚheadersÚoutputÚkeyZ	b64_bytesZb64_lenÚir   r   r   Úarmor2   s<    	r"   c             c   s·  t  |  t ƒ s* t t d t |  ƒ ƒ ƒ ‚ d } i  } d } d } d } d } xB|  j d ƒ D]1} | d k rs q^ | d k rÁ t j d | ƒ } | sš q^ | j d ƒ j	 d ƒ } d	 } d
 } q^ | d
 k r%| j
 d ƒ d k rë d } n: | j	 d ƒ }	 |	 j d d ƒ \ }
 } | j ƒ  | |
 <q^ | d k r^ | d d … d k r…t j | ƒ } | | | f Vd } i  } d } d } d	 } q^ | | 7} q^ W| s¡| r³t t d ƒ ƒ ‚ d S)ax  
    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.
    zA
            pem_bytes must be a byte string, not %s
            Ztrashó    NFs1   ^(?:---- |-----)BEGIN ([A-Z0-9 ]+)(?: ----|-----)r   r   Tr   ó   :Zbodyú:r   é   ó   -----ó   ---- z|
            pem_bytes does not appear to contain PEM-encoded data - no
            BEGIN/END combination found
            r   )r'   r(   )r   r
   r   r   r   Ú
splitlinesÚreÚmatchÚgroupÚdecoder   ÚsplitÚstripr   Z	b64decodeÚ
ValueError)Ú	pem_bytesÚstater   Zbase64_dataZobject_typeZfound_startZ	found_endÚlineZtype_name_matchZdecoded_lineÚnameÚvaluer   r   r   r   Ú_unarmorp   sT    	r6   Fc             C   s    t  |  ƒ } | s t | ƒ S| S)a˜  
    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.
    )r6   Únext)r1   ZmultipleÚ	generatorr   r   r   ÚunarmorÄ   s    
r9   )r   )Ú__doc__Z
__future__r   r   r   r   r   r*   ÚsysZ_errorsr   Z_typesr   r	   r
   Úversion_infoÚ	cStringIOr   r   Úior   r"   r6   r9   r   r   r   r   Ú<module>
   s   ">T