B
    M]                 @   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 rld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   s8   t | tsttdt| | ddkp6| 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
   -----BEGINs
   ---- BEGIN)
isinstancer
   	TypeErrorr   
_type_namefind)Zbyte_string r   -lib/python3.7/site-packages/asn1crypto/pem.pydetect   s
    
r   c             C   s4  t |tsttdt| t | ts:ttdt| |  d} t }|	d |	|  |	d |rxD|D ]<}|	|d |	d |	|| d |	d qvW |	d t
|}t|}d}x6||k r|	|||d	   |	d |d	7 }qW |	d
 |	|  |	d | S )a  
    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
    zA
            der_bytes must be a byte string, not %s
            zD
            type_name must be a unicode string, not %s
            asciis   -----BEGIN s   -----
s   :    
r   @   s	   -----END )r   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   s4  t | tsttdt| d}i }d}d}d}d}x| dD ]}|dkrNq@|dkrtd|}|shq@|d	d}d	}d
}q@|d
kr|
ddkrd}n(|	d}	|	dd\}
}| ||
< q@|dkr@|dd dkrt|}|||fV  d}i }d}d}d	}q@||7 }q@W |r$|s0t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       :r   Zbody:r      )s   -----s   ---- z|
            pem_bytes does not appear to contain PEM-encoded data - no
            BEGIN/END combination found
            )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   )N)F)__doc__Z
__future__r   r   r   r   r   r*   sysZ_errorsr   Z_typesr   r   r	   r
   version_infoZ	cStringIOr   r   ior   r$   r6   r9   r   r   r   r   <module>
   s   

>T