
    >wg                         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ddZy)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                     t        | t              st        t        dt	        |                   | j                  d      dk7  xs | j                  d      dk7  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)byte_strings    E/var/www/horilla/myenv/lib/python3.12/site-packages/asn1crypto/pem.pydetectr      s_     k8, {#	
  	 M*b0YK4D4D]4SWY4YY    c                    t        |t              s t        t        dt	        |      z              t        | t
              st        t        dt	        |                   | j                         j                  d      } t               }|j                  d       |j                  |        |j                  d       |r}|D ]g  }|j                  |j                  d             |j                  d       |j                  ||   j                  d             |j                  d       i |j                  d       t        j                  |      }t        |      }d}||k  r3|j                  |||d	z           |j                  d       |d	z  }||k  r3|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 )r   r   r   r   r   r
   upperencoder   writebase64	b64encodelengetvalue)r	   	der_bytesheadersoutputkey	b64_bytesb64_lenis           r   armorr+   2   s   ( i*Y'(
  	 i) y!	
  	 !((1IYF
LL 
LL
LL 	 CLLG,-LLLL,,W56LL		 
 	U  +I)nG	A
g+YqR()U	R g+ LL
LL
LL??r   c              #     K   t        | t              st        t        dt	        |                   d}i }d}d}d}d}| j                  d      D ]  }|dk(  r	|dk(  r>t        j                  d|      }|s'|j                  d      j                  d      }d	}d
}L|d
k(  rQ|j                  d      dk(  rd}n:|j                  d      }	|	j                  dd      \  }
}|j                         ||
<   |dk(  s|dd dv r't        j                  |      }|||f d}i }d}d}d	}||z  } |r|st        t        d            yw)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
            trashr   NFs1   ^(?:---- |-----)BEGIN ([A-Z0-9 ]+)(?: ----|-----)r   r   Tr%      :r   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    	b64decode
ValueError)	pem_bytesstater%   base64_dataobject_typefound_start	found_endlinetype_name_matchdecoded_linenamevaluer$   s                r   _unarmorrF   p   s    $ i* y!	
  	 EGKKKI$$U+ & 3;G !hh'[]abO")//299'BKKEIyy"$#{{73*00a8e %F?Aay00",,[9	"GY77!" 	4KM& P i
  	 (s   C3E6AEc                 6    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.
    )rF   next)r;   multiple	generators      r   unarmorrK      s!    * #IIr   )N)F)__doc__
__future__r   r   r   r   r    r3   sys_errorsr   _typesr	   r   r
   r   version_info	cStringIOr   r   ior   r+   rF   rK    r   r   <module>rU      sQ    S R  	 
  > >d-Z.;|Qhr   