3
Zq                 @   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               @   s&   e Zd ZdZdefdefdefgZdS )OtherPrimeInfoz=
    Source: https://tools.ietf.org/html/rfc3447#page-46
    primeZexponentcoefficientN)__name__
__module____qualname____doc__r   _fields r/   r/   1/usr/lib/python3/dist-packages/asn1crypto/keys.pyr'   6   s   r'   c               @   s   e Zd ZdZeZdS )OtherPrimeInfosz=
    Source: https://tools.ietf.org/html/rfc3447#page-46
    N)r*   r+   r,   r-   r'   _child_specr/   r/   r/   r0   r1   B   s   r1   c               @   s   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-primeZmulti)r   r   N)r*   r+   r,   r-   _mapr/   r/   r/   r0   r3   J   s   r3   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_exponentZprivate_exponentZprime1Zprime2Z	exponent1Z	exponent2r)   Zother_prime_infosoptionalTN)r*   r+   r,   r-   r3   r   r1   r.   r/   r/   r/   r0   r5   V   s   r5   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
    r7   r8   N)r*   r+   r,   r-   r   r.   r/   r/   r/   r0   r:   i   s   r:   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
    r6   pqg
public_keyprivate_keyN)r*   r+   r,   r-   r   r.   r/   r/   r/   r0   r;   t   s   r;   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jtj|dd }t tjtj|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yZx_bytesZy_bytesZ	num_bytesZbyte_stringr/   r/   r0   from_coords   s    
z_ECPoint.from_coordsc             C   s   | j }|dd }|dkrZ|dd }t|d }t|d| }t||d }||fS |tddgkrvttdttd	dS )
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   rC   NrB         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dataZ
first_byteZ	remainingZ	field_lenrK   rL   r/   r/   r0   	to_coords   s    	z_ECPoint.to_coordsN)r*   r+   r,   r-   classmethodrM   rV   r/   r/   r/   r0   rA      s   rA   c               @   s   e Zd ZdS )ECPointN)r*   r+   r,   r/   r/   r/   r0   rX      s   rX   c               @   s   e Zd ZdS )ECPointBitStringN)r*   r+   r,   r/   r/   r/   r0   rY      s   rY   c               @   s   e Zd ZdZddddZdS )SpecifiedECDomainVersionz:
    Source: http://www.secg.org/sec1-v2.pdf page 104
    ZecdpVer1ZecdpVer2ZecdpVer3)r   rB      N)r*   r+   r,   r-   r4   r/   r/   r/   r0   rZ      s   rZ   c               @   s   e Zd ZdZdddZdS )	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.2N)r*   r+   r,   r-   r4   r/   r/   r/   r0   r\      s   r\   c               @   s   e Zd ZdZddddZdS )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.3N)r*   r+   r,   r-   r4   r/   r/   r/   r0   r_      s   r_   c               @   s&   e Zd ZdZdefdefdefgZdS )Pentanomialz:
    Source: http://www.secg.org/sec1-v2.pdf page 102
    Zk1Zk2Zk3N)r*   r+   r,   r-   r   r.   r/   r/   r/   r0   rc      s   rc   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`   ra   rb   N)rf   rg   )r*   r+   r,   r-   r   r_   r   r.   	_oid_pairr   rc   
_oid_specsr/   r/   r/   r0   rd     s   
rd   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_typerg   )r]   r^   N)rk   rg   )r*   r+   r,   r-   r\   r   r.   rh   r   rd   ri   r/   r/   r/   r0   rj     s   
rj   c               @   s,   e Zd ZdZdefdefdeddifgZdS )Curvez:
    Source: http://www.secg.org/sec1-v2.pdf page 104
    abseedr9   TN)r*   r+   r,   r-   r   r   r.   r/   r/   r/   r0   rl   -  s   rl   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
    r6   field_idcurvebaseorderZcofactorr9   ThashN)r*   r+   r,   r-   rZ   rj   rl   rX   r   r   r.   r/   r/   r/   r0   rp   9  s   rp   c            )   @   sf   e Zd ZdZ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#d$d%d&d'd(d)d*(Zd+S ),
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
c2pnb163v1Z
c2pnb163v2Z
c2pnb163v3Z
c2pnb176w1Z
c2tnb191v1Z
c2tnb191v2Z
c2tnb191v3Z
c2onb191v4Z
c2onb191v5Z
c2pnb208w1Z
c2tnb239v1Z
c2tnb239v2Z
c2tnb239v3Z
c2onb239v4Z
c2onb239v5Z
c2pnb272w1Z
c2pnb304w1Z
c2tnb359v1Z
c2pnb368w1Z
c2tnb431r1Z
prime192v2Z
prime192v3Z
prime239v1Z
prime239v2Z
prime239v3Z	sect163k1Z	sect163r2	secp192r1	secp224r1Z	sect233k1	secp256r1Z	sect233r1Z	sect283k1Z	sect283r1	secp384r1Z	sect409k1Z	sect409r1	secp521r1Z	sect571k1Z	sect571r1)(z1.2.840.10045.3.0.1z1.2.840.10045.3.0.2z1.2.840.10045.3.0.3z1.2.840.10045.3.0.4z1.2.840.10045.3.0.5z1.2.840.10045.3.0.6z1.2.840.10045.3.0.7z1.2.840.10045.3.0.8z1.2.840.10045.3.0.9z1.2.840.10045.3.0.10z1.2.840.10045.3.0.11z1.2.840.10045.3.0.12z1.2.840.10045.3.0.13z1.2.840.10045.3.0.14z1.2.840.10045.3.0.15z1.2.840.10045.3.0.16z1.2.840.10045.3.0.17z1.2.840.10045.3.0.18z1.2.840.10045.3.0.19z1.2.840.10045.3.0.20z1.2.840.10045.3.1.2z1.2.840.10045.3.1.3z1.2.840.10045.3.1.4z1.2.840.10045.3.1.5z1.2.840.10045.3.1.6z1.3.132.0.1z1.3.132.0.15z1.2.840.10045.3.1.1z1.3.132.0.33z1.3.132.0.26z1.2.840.10045.3.1.7z1.3.132.0.27z1.3.132.0.16z1.3.132.0.17z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.39N)r*   r+   r,   r-   r4   r/   r/   r/   r0   rv   I  sR   rv   c               @   s&   e Zd ZdZdefdefdefgZdS )ECDomainParametersz:
    Source: http://www.secg.org/sec1-v2.pdf page 102
    	specifiednamedimplicit_caN)r*   r+   r,   r-   rp   rv   r   Z_alternativesr/   r/   r/   r0   r|     s   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   ZecPrivkeyVer1N)r*   r+   r,   r-   r4   r/   r/   r/   r0   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
    r6   r@   rg   r   T)Zexplicitr9   r?   r   N)	r*   r+   r,   r-   r   r   r|   rY   r.   r/   r/   r/   r0   r     s
   r   c               @   s&   e Zd ZdZdefdefdefgZdS )	DSAParamsz
    Parameters for a DSA public or private key

    Original Name: Dss-Parms
    Source: https://tools.ietf.org/html/rfc3279#page-9
    r<   r=   r>   N)r*   r+   r,   r-   r   r.   r/   r/   r/   r0   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+   r,   r-   r   r$   r   r.   r/   r/   r/   r0   r     s   r   c               @   s   e Zd ZdZeZdS )
Attributesz<
    Source: https://tools.ietf.org/html/rfc5208#page-3
    N)r*   r+   r,   r-   r   r2   r/   r/   r/   r0   r     s   r   c               @   s   e Zd ZdZddddZdS )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)z1.2.840.113549.1.1.1z1.2.840.10040.4.1z1.2.840.10045.2.1N)r*   r+   r,   r-   r4   r/   r/   r/   r0   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
    	algorithmrg   r9   T)r   r   N)r   rg   )r*   r+   r,   r-   r   r   r.   rh   r   r|   ri   r/   r/   r/   r0   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 )"PrivateKeyInfoz<
    Source: https://tools.ietf.org/html/rfc5208#page-3
    r6   private_key_algorithmr@   Z
attributesr   T)Zimplicitr9   c             C   s   | d d j }tttd| S )Nr   r   )r   r   r   )rP   r5   r   r   )rT   r   r/   r/   r0   _private_key_spec  s
    z PrivateKeyInfo._private_key_specNc             C   sD  t |t r*t |t r*ttdt||dkrNt |tsFtj|}t }n|dkrt |t	sjt	j|}t
 }|d |d< |d |d< |d |d< |d }|d }nH|d	krt |tstj|}n|j }|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   r<   r=   r>   r?   r@   r   rg   zU
                algorithm must be one of "rsa", "dsa", "ec", not %s
                r   r   r6   r   )
isinstancer   r   	TypeErrorr   r   r5   loadr   r;   r   r   copyrS   reprr   r   
_algorithmr   _public_key)rJ   r@   r   paramsr?   Zprivate_key_algo	containerr/   r/   r0   wrap  sJ    






zPrivateKeyInfo.wrapc       
      C   sf  | j dkr:| d d }tt|d j| d jj|d jS | j dkrd| d j}t|d |d	 d
S | j dkrb| j\}}|dkrttd|dkr|d d dkrttdt	|d d t
|d d t
|d d }| d d jd j \}}t|||}n6|dkr>|d kr*ttd|tttttd| }|| d jd j }	tj|	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   rg   r>   r@   r<   r   r7   r8   )r7   r8   r   r   z
                    Unable to compute public key for EC key using Implicit CA
                    parameters
                    r}   rq   rk   r^   z
                        Unable to compute public key for EC key over a
                        characteristic two field
                        rr   rm   rn   rs   r~   rw   rx   ry   rz   r{   z
                        Unable to compute public key for EC named curve %s,
                        parameters not currently included
                        )rw   rx   ry   rz   r{   N)rw   rx   ry   rz   r{   )r   r   powrP   parsedr:   rr   rS   r   r   r%   chosenrV   r   r   r   r	   r
   r   rY   rM   rK   rL   )
rT   r   keyZ
curve_typeZdetailsrr   Zbase_xZbase_yZ
base_pointZpublic_pointr/   r/   r0   _compute_public_keyL  sN    	








z"PrivateKeyInfo._compute_public_keyc             C   s   | j dkr| d jS | j dkrT| d d }td|d |d |d	 | j| d jd
S | j dkr| 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   r@   r   r   rg   r   r<   r=   r>   )r6   r<   r=   r>   r?   r@   r   r?   N)r   r   r;   r?   )rT   r   outputr/   r/   r0   r     s     	





zPrivateKeyInfo.unwrapc             C   sP   | j dkrttd| j j | d d }|j}|jdkr@d}n|j}|j|fS )a#  
        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   zK
                Only EC keys have a curve, this key is %s
                r   rg   r   N)r   rS   r   upperr   namerP   )rT   r   r   valuer/   r/   r0   rr     s    

zPrivateKeyInfo.curvec             C   sL   | j dkrttd| j j tj| d d d jdd }|dkrHd	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   zt
                Only DSA keys are generated using a hash algorithm, this key is
                %s
                r   rg   r=   rB         sha1sha2)r   rS   r   r   rF   rH   rP   )rT   byte_lenr/   r/   r0   	hash_algo  s    
zPrivateKeyInfo.hash_algoc             C   s    | j dkr| d d j| _ | j S )zO
        :return:
            A unicode string of "rsa", "dsa" or "ec"
        Nr   r   )r   rP   )rT   r/   r/   r0   r     s    
zPrivateKeyInfo.algorithmc             C   s   | j dkr| jdkr&| d jd j}n8| jdkrD| d d d j}n| jd	kr^| d jd j}ttjtj|d
| _ | j d }|dkr|  j d| 7  _ | j S )zU
        :return:
            The bit size of the private key, as an integer
        Nr   r@   r7   r   r   rg   r<   r   rB   r   r   )	_bit_sizer   r   rP   rE   rF   rG   rH   )rT   r(   r7   r/   r/   r0   bit_size  s    




zPrivateKeyInfo.bit_sizec             C   s   t tj| jd S )zV
        :return:
            The byte size of the private key, as an integer
        r   )rE   rF   rG   r   )rT   r/   r/   r0   	byte_size  s    zPrivateKeyInfo.byte_sizec             C   sR   | j dkrL| jdkrB| d j}|d r6|d j | _ qL| j | _ n
| j | _ | 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   r@   r?   )r   r   r   untagr   )rT   r   r/   r/   r0   r?     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   rg   )r   rg   )r   r?   )PublicKeyInfor   r?   )rT   r/   r/   r0   public_key_info*  s    zPrivateKeyInfo.public_key_infoc             C   sj  | j dkrd| d d }| d j}| jdkrFd|d j|d jf }n| jd	kr|| j}d
|d j|d j|d j|jf }n| jdkr>|d j}|dkr| jj}|jdkrd|jj }|jd}||7 }nr|jdkr|}nb|jdkr>d|jd d j }|jd}|d|jd d j 7 }|d|jd d j 7 }||7 }t|t	rT|jd}t
j|j | _ | 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   rg   r@   r   z%d:%dr7   r8   r   z%d:%d:%d:%dr<   r=   r>   r   r?   r~   z%s:zutf-8r   r}   rq      :rr   rm   rn   )_fingerprintr   r   rP   r?   r   r   encoder   r   hashlibsha256digest)rT   r   r   to_hashr?   r/   r/   r0   fingerprint9  sD    









zPrivateKeyInfo.fingerprint)r*   r+   r,   r-   r   r   r    r   r.   r   _spec_callbacksr   r   r   r   rW   r   r   r   propertyrr   r   r   r   r   r?   r   r   r/   r/   r/   r0   r     s,   	DI"	r   c               @   s    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
    Zencryption_algorithmZencrypted_dataN)r*   r+   r,   r-   r   r   r.   r/   r/   r/   r0   r   v  s   r   c               @   s    e Zd ZdZdefdefgZdS )ValidationParmsz=
    Source: https://tools.ietf.org/html/rfc3279#page-10
    ro   Zpgen_counterN)r*   r+   r,   r-   r   r   r.   r/   r/   r/   r0   r     s   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 )
DomainParametersz=
    Source: https://tools.ietf.org/html/rfc3279#page-10
    r<   r>   r=   jr9   TZvalidation_paramsN)r*   r+   r,   r-   r   r   r.   r/   r/   r/   r0   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)z1.2.840.113549.1.1.1z1.2.840.113549.1.1.7z1.2.840.10040.4.1z1.2.840.10045.2.1z1.2.840.10046.2.1N)r*   r+   r,   r-   r4   r/   r/   r/   r0   r     s   r   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   rg   r9   T)r   r   r   r   N)r   rg   )r*   r+   r,   r-   r   r   r.   rh   r   r|   r   r   ri   r/   r/   r/   r0   r     s   r   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   r?   c             C   s&   | d d j }ttttd ftd| S )Nr   )r   r   r   r   r   )rP   r:   r   rY   )rT   r   r/   r/   r0   _public_key_spec  s    zPublicKeyInfo._public_key_specNc             C   s   t |t r*t |t r*ttdt||dkrDttdt|t }t	||d< t
 |d< |  }||d< t |tr|j j }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   rg   r?   )r   r   r   r   r   r   rS   r   r   r   r   r   dumpr!   )rJ   r?   r   Zalgor   r/   r/   r0   r     s"    

zPublicKeyInfo.wrapc             C   sB   | j dkr| d jS | j j }|dkr*dnd}ttd||dS )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   r?   ZECZanrm   zj
            Only RSA public keys may be unwrapped - this key is %s %s public
            key
            N)r   r   r   rS   r   )rT   key_typeZa_anr/   r/   r0   r     s    	


zPublicKeyInfo.unwrapc             C   sP   | j dkrttd| j j | d d }|j}|jdkr@d}n|j}|j|fS )a#  
        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   zK
                Only EC keys have a curve, this key is %s
                r   rg   r   N)r   rS   r   r   r   r   rP   )rT   r   r   r   r/   r/   r0   rr   '  s    

zPublicKeyInfo.curvec             C   s^   | j dkrttd| j j | d d }|jdkr8dS tj|d jdd }|d	krZd
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   zt
                Only DSA keys are generated using a hash algorithm, this key is
                %s
                r   rg   Nr=   rB   r   r   r   r   )r   rS   r   r   rP   rF   rH   )rT   rg   r   r/   r/   r0   r   I  s    

zPublicKeyInfo.hash_algoc             C   s    | j dkr| d d j| _ | j S )zO
        :return:
            A unicode string of "rsa", "dsa" or "ec"
        Nr   )r   rP   )rT   r/   r/   r0   r   h  s    
zPublicKeyInfo.algorithmc             C   s   | j dkr| jdkr2t| d jd d d | _ nt| jdkrN| d jd j}n| jd	krj| d
 d d j}ttjtj|d| _ | j d }|dkr|  j d| 7  _ | j S )zT
        :return:
            The bit size of the public key, as an integer
        Nr   r?   r   rB   r   r   r7   r   r   rg   r<   r   )	r   r   rQ   rP   r   rE   rF   rG   rH   )rT   r(   r7   r/   r/   r0   r   s  s    




zPublicKeyInfo.bit_sizec             C   s   t tj| jd S )zU
        :return:
            The byte size of the public key, as an integer
        r   )rE   rF   rG   r   )rT   r/   r/   r0   r     s    zPublicKeyInfo.byte_sizec             C   s(   | j dkr"tjt| d j | _ | j S )ze
        :return:
            The SHA1 hash of the DER-encoded bytes of this public key info
        Nr?   )_sha1r   r   r   r   )rT   r/   r/   r0   r     s    
zPublicKeyInfo.sha1c             C   s(   | j dkr"tjt| d j | _ | j S )zh
        :return:
            The SHA-256 hash of the DER-encoded bytes of this public key info
        Nr?   )_sha256r   r   r   r   )rT   r/   r/   r0   r     s    
zPublicKeyInfo.sha256c             C   sj  | j dkrd| d d j}| d d }|dkrR| d j}d|d j|d jf }n|d	kr| d j}d
|d j|d j|d j|jf }n|dkr>| d }|jdkrd|jj }|jd}||j7 }nv|jdkr|j}nd|jdkr>d|jd d j }|jd}|d|jd d j 7 }|d|jd d j 7 }||j7 }t|trT|jd}tj	|j
 | _ | 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   rg   r   r?   z%d:%dr7   r8   r   z%d:%d:%d:%dr<   r=   r>   r   r~   z%s:zutf-8r   r}   rq   r   rr   rm   rn   )r   rP   r   r   r   r   r   r   r   r   r   )rT   r   r   r   r   r/   r/   r0   r     sB    








zPublicKeyInfo.fingerprint)r*   r+   r,   r-   r   r!   r.   r   r   r   r   r   r   r   rW   r   r   r   rr   r   r   r   r   r   r   r   r/   r/   r/   r0   r     s(   
+"	r   )Or-   Z
__future__r   r   r   r   r   rF   Z_elliptic_curver   r   r	   r
   r   r   r   Z_errorsr   Z_typesr   r   r   Zalgosr   r   r   r   Zcorer   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   utilr%   r&   r'   r1   r3   r5   r:   r;   rA   rX   rY   rZ   r\   r_   rc   rd   rj   rl   rp   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r/   r/   r/   r0   <module>   sZ   $	DF7   