3 L]@sdZddlmZmZmZmZddlZddlZddlZddl m Z ddl m Z mZmZejdkrlddlmZn dd lmZd d Zdd d ZddZdddZdS)zq Encoding DER to PEM and decoding PEM to DER. Exports the following items: - armor() - detect() - unarmor() )unicode_literalsdivisionabsolute_importprint_functionN)unwrap) type_namestr_clsbyte_cls)StringIO)BytesIOcCs8t|tsttdt||jddkp6|jddkS)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 -----BEGINrs ---- BEGINr) isinstancer TypeErrorr _type_namefind)Z byte_stringr6/tmp/pip-install-wfra5znf/asn1crypto/asn1crypto/pem.pydetects  rcCs4t|tsttdt|t|ts:ttdt||jjd}t}|j d|j ||j d|rxD|D]<}|j |jd|j d|j ||jd|j dqvW|j dt j |}t |}d}x6||kr |j |||d |j d|d 7}qW|j d |j ||j d|j 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 )rr rrrr upperencoder writebase64 b64encodelengetvalue)r der_bytesheadersoutputkeyZ b64_bytesZb64_lenirrrarmor2s<                r%c cs8t|tsttdt|d}i}d}d}d}d}x|jdD]}|dkrNq@|dkrtjd|}|shq@|jdj d}d }d }q@|d kr|j d dkrd }n(|j d} | j d d\} } | j || <q@|d kr@|dddkr t j|} ||| fVd}i}d}d}d }q@||7}q@W| s(| r4ttddS)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 ZtrashNFs1^(?:---- |-----)BEGIN ([A-Z0-9 ]+)(?: ----|-----)rrTr!:body:r--------- z| pem_bytes does not appear to contain PEM-encoded data - no BEGIN/END combination found r)r+r,)rr rrr splitlinesrematchgroupdecodersplitstripr b64decode ValueError) pem_bytesstater!Z base64_dataZ object_typeZ found_startZ found_endlineZtype_name_matchZ decoded_linenamevaluer rrr_unarmorpsT        r;FcCst|}|st|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. )r;next)r6multiple generatorrrrunarmorsr?)r )N)F)__doc__ __future__rrrrrr.sys_errorsr_typesrrr r version_info cStringIOr r iorr%r;r?rrrr s    >T