o
    q¨Êhq‰  ã                   @   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m	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZmZ ddlmZmZmZmZ dd	lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* dd
l+m,Z,m-Z- G dd„ de(ƒZ.G dd„ de)ƒZ/G dd„ de ƒZ0G dd„ de(ƒZ1G dd„ de(ƒZ2G dd„ de(ƒZ3G dd„ dƒZ4G dd„ de%e4ƒZ5G dd„ de$e4ƒZ6G dd„ de ƒZ7G dd „ d e#ƒZ8G d!d"„ d"e#ƒZ9G d#d$„ d$e(ƒZ:G d%d&„ d&e(ƒZ;G d'd(„ d(e(ƒZ<G d)d*„ d*e(ƒZ=G d+d,„ d,e(ƒZ>G d-d.„ d.e#ƒZ?G d/d0„ d0eƒZ@G d1d2„ d2e ƒZAG d3d4„ d4e(ƒZBG d5d6„ d6e(ƒZCG d7d8„ d8e(ƒZDG d9d:„ d:e*ƒZEG d;d<„ d<e#ƒZFG d=d>„ d>ee(ƒZGG d?d@„ d@e(ƒZHG dAdB„ dBe(ƒZIG dCdD„ dDe(ƒZJG dEdF„ dFe(ƒZKG dGdH„ dHe#ƒZLG dIdJ„ dJee(ƒZMG dKdL„ dLe(ƒZNdS )Ma'  
ASN.1 type classes for public and private keys. Exports the following items:

 - DSAPrivateKey()
 - ECPrivateKey()
 - EncryptedPrivateKeyInfo()
 - PrivateKeyInfo()
 - PublicKeyInfo()
 - RSAPrivateKey()
 - RSAPublicKey()

Other type classes are defined that help compose the types listed above.
é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )ÚSECP192R1_BASE_POINTÚSECP224R1_BASE_POINTÚSECP256R1_BASE_POINTÚSECP384R1_BASE_POINTÚSECP521R1_BASE_POINTÚ
PrimeCurveÚ
PrimePoint)Úunwrap)Ú	type_nameÚstr_clsÚbyte_cls)Ú_ForceNullParametersÚDigestAlgorithmÚEncryptionAlgorithmÚRSAESOAEPParams)ÚAnyÚ	Asn1ValueÚ	BitStringÚChoiceÚIntegerÚIntegerOctetStringÚNullÚObjectIdentifierÚOctetBitStringÚOctetStringÚParsableOctetStringÚParsableOctetBitStringÚSequenceÚ
SequenceOfÚSetOf)Úint_from_bytesÚint_to_bytesc                   @   ó&   e Zd ZdZdefdefdefgZdS )ÚOtherPrimeInfoú=
    Source: https://tools.ietf.org/html/rfc3447#page-46
    ÚprimeÚexponentÚcoefficientN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú_fields© r3   r3   úA/var/www/html/env/lib/python3.10/site-packages/asn1crypto/keys.pyr(   6   ó    ýr(   c                   @   ó   e Zd ZdZeZdS )ÚOtherPrimeInfosr)   N)r.   r/   r0   r1   r(   Ú_child_specr3   r3   r3   r4   r7   B   ó    r7   c                   @   ó   e Zd ZdZdddœZdS )ÚRSAPrivateKeyVersionzX
    Original Name: Version
    Source: https://tools.ietf.org/html/rfc3447#page-45
    z	two-primeÚmulti)r   r   N©r.   r/   r0   r1   Ú_mapr3   r3   r3   r4   r;   J   ó
    
þr;   c                   @   sV   e Zd ZdZdefdefdefdefdefdefdefd	efd
efdeddifg
ZdS )ÚRSAPrivateKeyz=
    Source: https://tools.ietf.org/html/rfc3447#page-45
    ÚversionÚmodulusÚpublic_exponentÚprivate_exponentÚprime1Úprime2Ú	exponent1Ú	exponent2r,   Úother_prime_infosÚoptionalTN)r.   r/   r0   r1   r;   r   r7   r2   r3   r3   r3   r4   r@   V   s    ör@   c                   @   s    e Zd ZdZdefdefgZdS )ÚRSAPublicKeyz=
    Source: https://tools.ietf.org/html/rfc3447#page-44
    rB   rC   Nr-   r3   r3   r3   r4   rK   i   ó
    þrK   c                   @   s8   e Zd ZdZdefdefdefdefdefdefgZdS )	ÚDSAPrivateKeya&  
    The ASN.1 structure that OpenSSL uses to store a DSA private key that is
    not part of a PKCS#8 structure. Reversed engineered from english-language
    description on linked OpenSSL documentation page.

    Original Name: None
    Source: https://www.openssl.org/docs/apps/dsa.html
    rA   ÚpÚqÚgÚ
public_keyÚprivate_keyNr-   r3   r3   r3   r4   rM   t   s    
úrM   c                   @   s$   e Zd ZdZedd„ ƒZdd„ ZdS )Ú_ECPointa
  
    In both PublicKeyInfo and PrivateKeyInfo, the EC public key is a byte
    string that is encoded as a bit string. This class adds convenience
    methods for converting to and from the byte string to a pair of integers
    that are the X and Y coordinates.
    c                 C   sj   t t t |d¡d ¡ƒ}t t t |d¡d ¡ƒ}t||ƒ}d}|t||d�7 }|t||d�7 }| |ƒS )a  
        Creates an ECPoint object from the X and Y integer coordinates of the
        point

        :param x:
            The X coordinate, as an integer

        :param y:
            The Y coordinate, as an integer

        :return:
            An ECPoint object
        é   g       @ó   )Úwidth)ÚintÚmathÚceilÚlogÚmaxr&   )ÚclsÚxÚyÚx_bytesÚy_bytesÚ	num_bytesÚbyte_stringr3   r3   r4   Úfrom_coords�   s   
z_ECPoint.from_coordsc                 C   s‚   | j }|dd… }|dkr-|dd… }t|ƒd }t|d|… ƒ}t||d… ƒ}||fS |tddgƒvr;ttdƒƒ‚ttd	ƒƒ‚)
z±
        Returns the X and Y coordinates for this EC point, as native Python
        integers

        :return:
            A 2-element tuple containing integers (X, Y)
        r   r   rU   NrT   ó   ó   zQ
                Invalid EC public key - first byte is incorrect
                z|
            Compressed representations of EC public keys are not supported due
            to patent US6252960
            )ÚnativeÚlenr%   ÚsetÚ
ValueErrorr   )ÚselfÚdataÚ
first_byteÚ	remainingÚ	field_lenr]   r^   r3   r3   r4   Ú	to_coords«   s   	ÿÿz_ECPoint.to_coordsN)r.   r/   r0   r1   Úclassmethodrc   ro   r3   r3   r3   r4   rS   ˆ   s
    
rS   c                   @   ó   e Zd ZdS )ÚECPointN©r.   r/   r0   r3   r3   r3   r4   rr   Î   ó    rr   c                   @   rq   )ÚECPointBitStringNrs   r3   r3   r3   r4   ru   Ó   rt   ru   c                   @   ó   e Zd ZdZddddœZdS )ÚSpecifiedECDomainVersionú:
    Source: http://www.secg.org/sec1-v2.pdf page 104
    ÚecdpVer1ÚecdpVer2ÚecdpVer3)r   rT   é   Nr=   r3   r3   r3   r4   rw   Ø   s    
ýrw   c                   @   r:   )Ú	FieldTypezR
    Original Name: None
    Source: http://www.secg.org/sec1-v2.pdf page 101
    Úprime_fieldÚcharacteristic_two_field)z1.2.840.10045.1.1z1.2.840.10045.1.2Nr=   r3   r3   r3   r4   r}   ã   r?   r}   c                   @   rv   )ÚCharacteristicTwoBasiszR
    Original Name: None
    Source: http://www.secg.org/sec1-v2.pdf page 102
    Úgn_basisÚtp_basisÚpp_basis)z1.2.840.10045.1.2.1.1z1.2.840.10045.1.2.1.2z1.2.840.10045.1.2.1.3Nr=   r3   r3   r3   r4   r€   ï   s    
ýr€   c                   @   r'   )ÚPentanomialú:
    Source: http://www.secg.org/sec1-v2.pdf page 102
    Úk1Úk2Úk3Nr-   r3   r3   r3   r4   r„   ü   r5   r„   c                   @   s6   e Zd ZdZdefdefdefgZdZe	ee
dœZdS )ÚCharacteristicTwoz`
    Original Name: Characteristic-two
    Source: http://www.secg.org/sec1-v2.pdf page 101
    ÚmÚbasisÚ
parameters)r‹   rŒ   )r�   r‚   rƒ   N)r.   r/   r0   r1   r   r€   r   r2   Ú	_oid_pairr   r„   Ú
_oid_specsr3   r3   r3   r4   r‰     s    ý
ýr‰   c                   @   s.   e Zd ZdZdefdefgZdZee	dœZ
dS )ÚFieldIDz:
    Source: http://www.secg.org/sec1-v2.pdf page 100
    Ú
field_typerŒ   )r�   rŒ   )r~   r   N)r.   r/   r0   r1   r}   r   r2   r�   r   r‰   rŽ   r3   r3   r3   r4   r�     s    þ
þr�   c                   @   s,   e Zd ZdZdefdefdeddifgZdS )ÚCurverx   ÚaÚbÚseedrJ   TN)r.   r/   r0   r1   r   r   r2   r3   r3   r3   r4   r‘   -  s    ýr‘   c                
   @   sJ   e Zd ZdZdefdefdefdefdefdedd	ifd
e	dd	ifgZ
dS )ÚSpecifiedECDomainz:
    Source: http://www.secg.org/sec1-v2.pdf page 103
    rA   Úfield_idÚcurveÚbaseÚorderÚcofactorrJ   TÚhashN)r.   r/   r0   r1   rw   r�   r‘   rr   r   r   r2   r3   r3   r3   r4   r•   9  s    ùr•   c                   @   sö   e Zd ZdZi dd“dd“dd“dd	“d
d“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“d d!“d"d#“i d$d%“d&d'“d(d)“d*d+“d,d-“d.d/“d0d1“d2d3“d4d5“d6d7“d8d9“d:d;“d<d=“d>d?“d@dA“dBdC“dDdE“¥dFdGdHdIdJdKdLœ¥ZdMS )NÚ
NamedCurvez§
    Various named curves

    Original Name: None
    Source: https://tools.ietf.org/html/rfc3279#page-23,
            https://tools.ietf.org/html/rfc5480#page-5
    z1.2.840.10045.3.0.1Ú
c2pnb163v1z1.2.840.10045.3.0.2Ú
c2pnb163v2z1.2.840.10045.3.0.3Ú
c2pnb163v3z1.2.840.10045.3.0.4Ú
c2pnb176w1z1.2.840.10045.3.0.5Ú
c2tnb191v1z1.2.840.10045.3.0.6Ú
c2tnb191v2z1.2.840.10045.3.0.7Ú
c2tnb191v3z1.2.840.10045.3.0.8Ú
c2onb191v4z1.2.840.10045.3.0.9Ú
c2onb191v5z1.2.840.10045.3.0.10Ú
c2pnb208w1z1.2.840.10045.3.0.11Ú
c2tnb239v1z1.2.840.10045.3.0.12Ú
c2tnb239v2z1.2.840.10045.3.0.13Ú
c2tnb239v3z1.2.840.10045.3.0.14Ú
c2onb239v4z1.2.840.10045.3.0.15Ú
c2onb239v5z1.2.840.10045.3.0.16Ú
c2pnb272w1z1.2.840.10045.3.0.17Ú
c2pnb304w1z1.2.840.10045.3.0.18Ú
c2tnb359v1z1.2.840.10045.3.0.19Ú
c2pnb368w1z1.2.840.10045.3.0.20Ú
c2tnb431r1z1.2.840.10045.3.1.2Ú
prime192v2z1.2.840.10045.3.1.3Ú
prime192v3z1.2.840.10045.3.1.4Ú
prime239v1z1.2.840.10045.3.1.5Ú
prime239v2z1.2.840.10045.3.1.6Ú
prime239v3z1.3.132.0.1Ú	sect163k1z1.3.132.0.15Ú	sect163r2z1.2.840.10045.3.1.1Ú	secp192r1z1.3.132.0.33Ú	secp224r1z1.3.132.0.26Ú	sect233k1z1.2.840.10045.3.1.7Ú	secp256r1z1.3.132.0.27Ú	sect233r1z1.3.132.0.16Ú	sect283k1z1.3.132.0.17Ú	sect283r1Ú	secp384r1Ú	sect409k1Ú	sect409r1Ú	secp521r1Ú	sect571k1Ú	sect571r1)z1.3.132.0.34z1.3.132.0.36z1.3.132.0.37z1.3.132.0.35z1.3.132.0.38z1.3.132.0.39Nr=   r3   r3   r3   r4   rœ   I  sœ    þýüûúùø	÷
öõôóòñðïîíìëêéèçæäãâá à!ß"Þ#Ý$Ü%Örœ   c                   @   s&   e Zd ZdZdefdefdefgZdS )ÚECDomainParametersr…   Ú	specifiedÚnamedÚimplicit_caN)r.   r/   r0   r1   r•   rœ   r   Ú_alternativesr3   r3   r3   r4   rÅ   €  r5   rÅ   c                   @   s   e Zd ZdZddiZdS )ÚECPrivateKeyVersionzR
    Original Name: None
    Source: http://www.secg.org/sec1-v2.pdf page 108
    r   ÚecPrivkeyVer1Nr=   r3   r3   r3   r4   rÊ   Œ  s    ÿrÊ   c                   @   s<   e Zd ZdZdefdefdedddœfded	ddœfgZd
S )ÚECPrivateKeyz:
    Source: http://www.secg.org/sec1-v2.pdf page 108
    rA   rR   rŒ   r   T)ÚexplicitrJ   rQ   r   N)	r.   r/   r0   r1   rÊ   r   rÅ   ru   r2   r3   r3   r3   r4   rÌ   —  s    ürÌ   c                   @   r'   )Ú	DSAParamsz‰
    Parameters for a DSA public or private key

    Original Name: Dss-Parms
    Source: https://tools.ietf.org/html/rfc3279#page-9
    rN   rO   rP   Nr-   r3   r3   r3   r4   rÎ   ¤  s    ýrÎ   c                   @   s&   e Zd ZdZdefdedeifgZdS )Ú	Attributezq
    Source: https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-X.501-198811-S!!PDF-E&type=items page 8
    ÚtypeÚvaluesÚspecN)r.   r/   r0   r1   r   r$   r   r2   r3   r3   r3   r4   rÏ   ³  s
    þrÏ   c                   @   r6   )Ú
Attributesú<
    Source: https://tools.ietf.org/html/rfc5208#page-3
    N)r.   r/   r0   r1   rÏ   r8   r3   r3   r3   r4   rÓ   ¾  r9   rÓ   c                   @   rv   )ÚPrivateKeyAlgorithmIdz»
    These OIDs for various public keys are reused when storing private keys
    inside of a PKCS#8 structure

    Original Name: None
    Source: https://tools.ietf.org/html/rfc3279
    ÚrsaÚdsaÚec)ú1.2.840.113549.1.1.1ú1.2.840.10040.4.1ú1.2.840.10045.2.1Nr=   r3   r3   r3   r4   rÕ   Æ  s    

úrÕ   c                   @   s4   e Zd ZdZdefdeddifgZdZee	dœZ
dS )	ÚPrivateKeyAlgorithmzm
    Original Name: PrivateKeyAlgorithmIdentifier
    Source: https://tools.ietf.org/html/rfc5208#page-3
    Ú	algorithmrŒ   rJ   T©rÝ   rŒ   )r×   rØ   N)r.   r/   r0   r1   rÕ   r   r2   r�   rÎ   rÅ   rŽ   r3   r3   r3   r4   rÜ   Ù  s    þ
þrÜ   c                   @   sÐ   e Zd ZdZdefdefdefdedddœfgZd	d
„ Z	de	iZ
dZdZdZdZedd„ ƒZdd„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZed d!„ ƒZdS )"ÚPrivateKeyInforÔ   rA   Úprivate_key_algorithmrR   Ú
attributesr   T)ÚimplicitrJ   c                 C   s   | d d j }tttdœ| S )Nrà   rÝ   )rÖ   r×   rØ   )rf   r@   r   rÌ   ©rj   rÝ   r3   r3   r4   Ú_private_key_spec÷  s   ýüz PrivateKeyInfo._private_key_specNc                 C   s>  t |tƒst |tƒsttdt|ƒƒƒ‚|dkr%t |tƒs!t |¡}tƒ }nP|dkrQt |t	ƒs3t	 |¡}t
ƒ }|d |d< |d |d< |d |d< |d }|d }n$|d	krlt |tƒs`t |¡}n| ¡ }|d
 }|d
= n	ttdt|ƒƒƒ‚tƒ }t|ƒ|d< ||d
< | ƒ }||_tdƒ|d< ||d< ||d< |dkr�||_|S )a'  
        Wraps a private key in a PrivateKeyInfo structure

        :param private_key:
            A byte string or Asn1Value object of the private key

        :param algorithm:
            A unicode string of "rsa", "dsa" or "ec"

        :return:
            A PrivateKeyInfo object
        zX
                private_key must be a byte string or Asn1Value, not %s
                rÖ   r×   rN   rO   rP   rQ   rR   rØ   rŒ   zU
                algorithm must be one of "rsa", "dsa", "ec", not %s
                rÝ   r   rA   rà   )Ú
isinstancer   r   Ú	TypeErrorr   r   r@   Úloadr   rM   rÎ   rÌ   Úcopyri   ÚreprrÜ   rÕ   Ú
_algorithmr   Ú_public_key)r\   rR   rÝ   ÚparamsrQ   Úprivate_key_algoÚ	containerr3   r3   r4   Úwrap  sN   ü





üzPrivateKeyInfo.wrapc           
      C   s^  | j dkr| d d }tt|d j| d jj|d jƒƒS | j dkr2| d j}t|d |d	 d
œƒS | j dkr­| j\}}|dkrFttdƒƒ‚|dkr‚|d d dkrXttdƒƒ‚t	|d d t
|d d ƒt
|d d ƒƒ}| d d jd  ¡ \}}t|||ƒ}n|dkr›|dvr‘ttd|ƒƒ‚tttttdœ| }|| d jd j }	t |	j|	j¡S dS )zæ
        Computes the public key corresponding to the current private key.

        :return:
            For RSA keys, an RSAPublicKey object. For DSA keys, an Integer
            object. For EC keys, an ECPointBitString.
        r×   rà   rŒ   rP   rR   rN   rÖ   rB   rC   )rB   rC   rØ   rÈ   z‚
                    Unable to compute public key for EC key using Implicit CA
                    parameters
                    rÆ   r–   r�   r   z‘
                        Unable to compute public key for EC key over a
                        characteristic two field
                        r—   r’   r“   r˜   rÇ   )r¸   r¹   r»   r¿   rÂ   zŸ
                        Unable to compute public key for EC named curve %s,
                        parameters not currently included
                        N)rÝ   r   Úpowrf   ÚparsedrK   r—   ri   r   r   r%   Úchosenro   r   r   r   r	   r
   r   ru   rc   r]   r^   )
rj   rì   ÚkeyÚ
curve_typeÚdetailsr—   Úbase_xÚbase_yÚ
base_pointÚpublic_pointr3   r3   r4   Ú_compute_public_keyL  s`   
	
ý

þ

ÿÿ
ýû	ûúÑz"PrivateKeyInfo._compute_public_keyc                 C   sŠ   | j dkr
| d jS | j dkr*| d d }td|d |d |d	 | j| d jd
œƒS | j dkrC| d j}| d d |d< | j|d< |S dS )zÂ
        Unwraps the private key into an RSAPrivateKey, DSAPrivateKey or
        ECPrivateKey object

        :return:
            An RSAPrivateKey, DSAPrivateKey or ECPrivateKey object
        rÖ   rR   r×   rà   rŒ   r   rN   rO   rP   )rA   rN   rO   rP   rQ   rR   rØ   rQ   N)rÝ   rñ   rM   rQ   )rj   rì   Úoutputr3   r3   r4   r   •  s$   
	

ú
	

üzPrivateKeyInfo.unwrapc                 C   óP   | j dkrttd| j  ¡ ƒƒ‚| d d }|j}|jdkr d}n|j}|j|fS )á#  
        Returns information about the curve used for an EC key

        :raises:
            ValueError - when the key is not an EC key

        :return:
            A two-element tuple, with the first element being a unicode string
            of "implicit_ca", "specified" or "named". If the first element is
            "implicit_ca", the second is None. If "specified", the second is
            an OrderedDict that is the native version of SpecifiedECDomain. If
            "named", the second is a unicode string of the curve name.
        rØ   úK
                Only EC keys have a curve, this key is %s
                rà   rŒ   rÈ   N©rÝ   ri   r   Úupperrò   Únamerf   ©rj   rì   rò   Úvaluer3   r3   r4   r—   ²  ó   
ü

zPrivateKeyInfo.curvec                 C   sL   | j dkrttd| j  ¡ ƒƒ‚t | d d d jd¡d }|dkr$d	S d
S )zò
        Returns the name of the family of hash algorithms used to generate a
        DSA key

        :raises:
            ValueError - when the key is not a DSA key

        :return:
            A unicode string of "sha1" or "sha2"
        r×   út
                Only DSA keys are generated using a hash algorithm, this key is
                %s
                rà   rŒ   rO   rT   é   é   Úsha1Úsha2)rÝ   ri   r   r   rX   rZ   rf   )rj   Úbyte_lenr3   r3   r4   Ú	hash_algoÔ  s   
ûzPrivateKeyInfo.hash_algoc                 C   s    | j du r| d d j| _ | j S )úO
        :return:
            A unicode string of "rsa", "dsa" or "ec"
        Nrà   rÝ   ©rê   rf   ©rj   r3   r3   r4   rÝ   î  ó   
zPrivateKeyInfo.algorithmc                 C   s    | j du rM| jdkr| d jd j}n| jdkr"| d d d j}n| jd	kr/| d jd j}tt t |d
¡¡ƒ| _ | j d }|dkrM|  j d| 7  _ | j S )zU
        :return:
            The bit size of the private key, as an integer
        NrÖ   rR   rB   r×   rà   rŒ   rN   rØ   rT   r  r   )Ú	_bit_sizerÝ   rñ   rf   rW   rX   rY   rZ   ©rj   r*   rB   r3   r3   r4   Úbit_sizeù  s   




zPrivateKeyInfo.bit_sizec                 C   ó   t t | jd ¡ƒS )zV
        :return:
            The byte size of the private key, as an integer
        r  ©rW   rX   rY   r  r  r3   r3   r4   Ú	byte_size  ó   zPrivateKeyInfo.byte_sizec                 C   sZ   | j du r*| jdkr%| d j}|d r|d  ¡ | _ | j S |  ¡ | _ | j S |  ¡ | _ | j S )z¤
        :return:
            If an RSA key, an RSAPublicKey object. If a DSA key, an Integer
            object. If an EC key, an ECPointBitString object.
        NrØ   rR   rQ   )rë   rÝ   rñ   Úuntagrú   )rj   ró   r3   r3   r4   rQ     s   



ü
þzPrivateKeyInfo.public_keyc                 C   s    t | j| d d dœ| jdœƒS )z\
        :return:
            A PublicKeyInfo object derived from this private key.
        rà   rŒ   rÞ   )rÝ   rQ   )ÚPublicKeyInforÝ   rQ   r  r3   r3   r4   Úpublic_key_info*  s   
þûzPrivateKeyInfo.public_key_infoc                 C   sb  | j du r®| d d }| d j}| jdkr"d|d j|d jf }nz| jd	kr=| j}d
|d j|d j|d j|jf }n_| jdkrœ|d j}|du rO| jj}|jdkrdd|jj }| d¡}||7 }n8|jdkrl|}n0|jdkrœd|jd d j }| d¡}|d|jd d j 7 }|d|jd d j 7 }||7 }t|t	ƒr¦| d¡}t
 |¡ ¡ | _ | j S )aY  
        Creates a fingerprint that can be compared with a public key to see if
        the two form a pair.

        This fingerprint is not compatible with fingerprints generated by any
        other software.

        :return:
            A byte string that is a sha256 hash of selected components (based
            on the key type)
        Nrà   rŒ   rR   rÖ   ú%d:%drB   rC   r×   ú%d:%d:%d:%drN   rO   rP   rØ   rQ   rÇ   ú%s:úutf-8rÈ   rÆ   r–   ó   :r—   r’   r“   )Ú_fingerprintrñ   rÝ   rf   rQ   r  rò   Úencoderå   r   ÚhashlibÚsha256Údigest)rj   rì   ró   Úto_hashrQ   r3   r3   r4   Úfingerprint9  sH   


þ
ü









zPrivateKeyInfo.fingerprint)r.   r/   r0   r1   r   rÜ   r    rÓ   r2   rä   Ú_spec_callbacksrê   r  rë   r  rp   rï   rú   r   Úpropertyr—   r  rÝ   r  r  rQ   r  r%  r3   r3   r3   r4   rß   ë  sD    ü	ÿ
CI
!






rß   c                   @   ó    e Zd ZdZdefdefgZdS )ÚEncryptedPrivateKeyInfoz<
    Source: https://tools.ietf.org/html/rfc5208#page-4
    Úencryption_algorithmÚencrypted_dataN)r.   r/   r0   r1   r   r   r2   r3   r3   r3   r4   r)  v  rL   r)  c                   @   r(  )ÚValidationParmsú=
    Source: https://tools.ietf.org/html/rfc3279#page-10
    r”   Úpgen_counterN)r.   r/   r0   r1   r   r   r2   r3   r3   r3   r4   r,  ƒ  rL   r,  c                   @   s>   e Zd ZdZdefdefdefdeddifdeddifgZd	S )
ÚDomainParametersr-  rN   rP   rO   ÚjrJ   TÚvalidation_paramsN)r.   r/   r0   r1   r   r,  r2   r3   r3   r3   r4   r/  Ž  s    ûr/  c                   @   s    e Zd ZdZddddddœZdS )	ÚPublicKeyAlgorithmIdzM
    Original Name: None
    Source: https://tools.ietf.org/html/rfc3279
    rÖ   Ú
rsaes_oaepr×   rØ   Údh)rÙ   z1.2.840.113549.1.1.7rÚ   rÛ   z1.2.840.10046.2.1Nr=   r3   r3   r3   r4   r2  œ  s    
ör2  c                   @   s8   e Zd ZdZdefdeddifgZdZee	e
edœZdS )	ÚPublicKeyAlgorithmzd
    Original Name: AlgorithmIdentifier
    Source: https://tools.ietf.org/html/rfc5280#page-18
    rÝ   rŒ   rJ   TrÞ   )r×   rØ   r4  r3  N)r.   r/   r0   r1   r2  r   r2   r�   rÎ   rÅ   r/  r   rŽ   r3   r3   r3   r4   r5  °  s    þ
ür5  c                   @   s¸   e Zd ZdZdefdefgZdd„ ZdeiZdZ	dZ
dZdZdZedd„ ƒZd	d
„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdS )r  ze
    Original Name: SubjectPublicKeyInfo
    Source: https://tools.ietf.org/html/rfc5280#page-17
    rÝ   rQ   c                 C   s&   | d d j }ttttd ftdœ| S )NrÝ   )rÖ   r3  r×   rØ   r4  )rf   rK   r   ru   rã   r3   r3   r4   Ú_public_key_specÏ  s   ùøzPublicKeyInfo._public_key_specNc                 C   s�   t |tƒst |tƒsttdt|ƒƒƒ‚|dkr ttdt|ƒƒƒ‚tƒ }t	|ƒ|d< t
ƒ |d< | ƒ }||d< t |tƒr@| ¡  ¡ }t|ƒ|d< |S )a  
        Wraps a public key in a PublicKeyInfo structure

        :param public_key:
            A byte string or Asn1Value object of the public key

        :param algorithm:
            A unicode string of "rsa"

        :return:
            A PublicKeyInfo object
        zW
                public_key must be a byte string or Asn1Value, not %s
                rÖ   z>
                algorithm must "rsa", not %s
                rÝ   rŒ   rQ   )rå   r   r   ræ   r   r   ri   ré   r5  r2  r   r  Údumpr!   )r\   rQ   rÝ   Úalgorî   r3   r3   r4   rï   å  s&   üü

zPublicKeyInfo.wrapc                 C   s>   | j dkr
| d jS | j  ¡ }|dkrdnd}ttd||ƒƒ‚)zÖ
        Unwraps an RSA public key into an RSAPublicKey object. Does not support
        DSA or EC public keys since they do not have an unwrapped form.

        :return:
            An RSAPublicKey object
        rÖ   rQ   ÚECÚanr’   zj
            Only RSA public keys may be unwrapped - this key is %s %s public
            key
            )rÝ   rñ   r   ri   r   )rj   Úkey_typeÚa_anr3   r3   r4   r     s   
	

úzPublicKeyInfo.unwrapc                 C   rü   )rý   rØ   rþ   rÝ   rŒ   rÈ   Nrÿ   r  r3   r3   r4   r—   '  r  zPublicKeyInfo.curvec                 C   s^   | j dkrttd| j  ¡ ƒƒ‚| d d }|jdu rdS t |d jd¡d }|d	kr-d
S dS )a#  
        Returns the name of the family of hash algorithms used to generate a
        DSA key

        :raises:
            ValueError - when the key is not a DSA key

        :return:
            A unicode string of "sha1" or "sha2" or None if no parameters are
            present
        r×   r  rÝ   rŒ   NrO   rT   r  r  r  r	  )rÝ   ri   r   r   rf   rX   rZ   )rj   rŒ   r
  r3   r3   r4   r  I  s   
û
zPublicKeyInfo.hash_algoc                 C   s    | j du r| d d j| _ | j S )r  NrÝ   r  r  r3   r3   r4   rÝ   h  r  zPublicKeyInfo.algorithmc                 C   s°   | j du rU| jdkrt| d jƒd d d | _ | j S | jdkr)| d jd j}n| jd	kr7| d
 d d j}tt t |d¡¡ƒ| _ | j d }|dkrU|  j d| 7  _ | j S )zT
        :return:
            The bit size of the public key, as an integer
        NrØ   rQ   r   rT   r  rÖ   rB   r×   rÝ   rŒ   rN   r   )	r  rÝ   rg   rf   rñ   rW   rX   rY   rZ   r  r3   r3   r4   r  s  s   


÷

zPublicKeyInfo.bit_sizec                 C   r  )zU
        :return:
            The byte size of the public key, as an integer
        r  r  r  r3   r3   r4   r  ‰  r  zPublicKeyInfo.byte_sizec                 C   ó(   | j du rt t| d ƒ¡ ¡ | _ | j S )ze
        :return:
            The SHA1 hash of the DER-encoded bytes of this public key info
        NrQ   )Ú_sha1r!  r  r   r#  r  r3   r3   r4   r  ’  ó   
zPublicKeyInfo.sha1c                 C   r=  )zh
        :return:
            The SHA-256 hash of the DER-encoded bytes of this public key info
        NrQ   )Ú_sha256r!  r"  r   r#  r  r3   r3   r4   r"  �  r?  zPublicKeyInfo.sha256c                 C   sb  | j du r®| d d j}| d d }|dkr(| d j}d|d j|d jf }nt|d	krD| d j}d
|d j|d j|d j|jf }nX|dkrœ| d }|jdkrbd|jj }| d¡}||j7 }n:|jdkrk|j}n1|jdkrœd|jd d j }| d¡}|d|jd d j 7 }|d|jd d j 7 }||j7 }t|tƒr¦| d¡}t 	|¡ 
¡ | _ | j S )aZ  
        Creates a fingerprint that can be compared with a private key to see if
        the two form a pair.

        This fingerprint is not compatible with fingerprints generated by any
        other software.

        :return:
            A byte string that is a sha256 hash of selected components (based
            on the key type)
        NrÝ   rŒ   rÖ   rQ   r  rB   rC   r×   r  rN   rO   rP   rØ   rÇ   r  r  rÈ   rÆ   r–   r  r—   r’   r“   )r  rf   rñ   r  rò   r   rå   r   r!  r"  r#  )rj   r;  rì   ró   r$  r3   r3   r4   r%  ¨  sF   

þ
ü







zPublicKeyInfo.fingerprint)r.   r/   r0   r1   r5  r!   r2   r6  r&  rê   r  r  r>  r@  rp   rï   r   r'  r—   r  rÝ   r  r  r  r"  r%  r3   r3   r3   r4   r  Ä  s@    þÿ
*
!








r  )Or1   Ú
__future__r   r   r   r   r!  rX   Ú_elliptic_curver   r   r	   r
   r   r   r   Ú_errorsr   Ú_typesr   r   r   Úalgosr   r   r   r   Úcorer   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   Úutilr%   r&   r(   r7   r;   r@   rK   rM   rS   rr   ru   rw   r}   r€   r„   r‰   r�   r‘   r•   rœ   rÅ   rÊ   rÌ   rÎ   rÏ   rÓ   rÕ   rÜ   rß   r)  r,  r/  r2  r5  r  r3   r3   r3   r4   Ú<module>   s\   $	DF7   