
W                 @   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 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, Gd d   d e'  Z- Gd d   d e(  Z. Gd d   d e  Z/ Gd d   d e'  Z0 Gd d   d e'  Z1 Gd d   d e'  Z2 Gd d   d  Z3 Gd d   d e$ e3  Z4 Gd d   d e# e3  Z5 Gd d   d e  Z6 Gd d    d  e"  Z7 Gd! d"   d" e"  Z8 Gd# d$   d$ e'  Z9 Gd% d&   d& e'  Z: Gd' d(   d( e'  Z; Gd) d*   d* e'  Z< Gd+ d,   d, e'  Z= Gd- d.   d. e"  Z> Gd/ d0   d0 e  Z? Gd1 d2   d2 e  Z@ Gd3 d4   d4 e'  ZA Gd5 d6   d6 e'  ZB Gd7 d8   d8 e'  ZC Gd9 d:   d: e)  ZD Gd; d<   d< e"  ZE Gd= d>   d> e e'  ZF Gd? d@   d@ e'  ZG GdA dB   dB e'  ZH GdC dD   dD e'  ZI GdE dF   dF e'  ZJ GdG dH   dH e"  ZK GdI dJ   dJ e e'  ZL GdK dL   dL e'  ZM d 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)Any	Asn1Value	BitStringChoiceIntegerIntegerOctetStringNullObjectIdentifierOctetBitStringOctetStringParsableOctetStringParsableOctetBitStringSequence
SequenceOfSetOf)int_from_bytesint_to_bytesc               @   s7   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.   r/   r0   B   s   r0   c               @   s(   e  Z d  Z d Z d d d d i Z d S)RSAPrivateKeyVersionzX
    Original Name: Version
    Source: https://tools.ietf.org/html/rfc3447#page-45
    r   z	two-primer   ZmultiN)r)   r*   r+   r,   _mapr.   r.   r.   r/   r2   J   s   r2   c               @   s   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,   r2   r   r0   r-   r.   r.   r.   r/   r4   V   s   									r4   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
    r6   r7   N)r)   r*   r+   r,   r   r-   r.   r.   r.   r/   r9   i   s   	r9   c               @   sR   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
    r5   pqg
public_keyprivate_keyN)r)   r*   r+   r,   r   r-   r.   r.   r.   r/   r:   t   s   					r:   c               @   s4   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   s   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       @s   width)intmathceillogmaxr%   )clsxyZx_bytesZy_bytesZ	num_bytesZbyte_stringr.   r.   r/   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  k r t 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   s   NrA   s   s   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_lenrI   rJ   r.   r.   r/   	to_coords   s    		
z_ECPoint.to_coordsN)r)   r*   r+   r,   classmethodrK   rR   r.   r.   r.   r/   r@      s   r@   c               @   s   e  Z d  Z d S)ECPointN)r)   r*   r+   r.   r.   r.   r/   rT      s   rT   c               @   s   e  Z d  Z d S)ECPointBitStringN)r)   r*   r+   r.   r.   r.   r/   rU      s   rU   c               @   s.   e  Z d  Z d Z d d d d d d i Z d S)	SpecifiedECDomainVersionz:
    Source: http://www.secg.org/sec1-v2.pdf page 104
    r   ZecdpVer1rA   ZecdpVer2   ZecdpVer3N)r)   r*   r+   r,   r3   r.   r.   r.   r/   rV      s   rV   c               @   s(   e  Z d  Z d Z d d d d i Z d S)	FieldTypezR
    Original Name: None
    Source: http://www.secg.org/sec1-v2.pdf page 101
    z1.2.840.10045.1.1prime_fieldz1.2.840.10045.1.2characteristic_two_fieldN)r)   r*   r+   r,   r3   r.   r.   r.   r/   rX      s   rX   c               @   s.   e  Z d  Z d Z d d d d d d i Z d S)	CharacteristicTwoBasiszR
    Original Name: None
    Source: http://www.secg.org/sec1-v2.pdf page 102
    z1.2.840.10045.1.2.1.1gn_basisz1.2.840.10045.1.2.1.2tp_basisz1.2.840.10045.1.2.1.3pp_basisN)r)   r*   r+   r,   r3   r.   r.   r.   r/   r[      s   r[   c               @   s7   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.   r/   r_      s   		r_   c               @   sU   e  Z d  Z d Z d e f d e f d e f g Z d	 Z d e	 d e d e
 i Z d S)
CharacteristicTwoz`
    Original Name: Characteristic-two
    Source: http://www.secg.org/sec1-v2.pdf page 101
    mbasis
parametersr\   r]   r^   N)rb   z
parameters)r)   r*   r+   r,   r   r[   r   r-   	_oid_pairr   r_   
_oid_specsr.   r.   r.   r/   r`     s   		r`   c               @   sF   e  Z d  Z d Z d e f d e f g Z d Z d e d e	 i Z
 d S)FieldIDz:
    Source: http://www.secg.org/sec1-v2.pdf page 100
    
field_typerc   rY   rZ   N)rg   z
parameters)r)   r*   r+   r,   rX   r   r-   rd   r   r`   re   r.   r.   r.   r/   rf     s   	rf   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seedr8   TN)r)   r*   r+   r,   r   r   r-   r.   r.   r.   r/   rh   -  s   		rh   c            
   @   sm   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
    r5   field_idcurvebaseorderZcofactorr8   ThashN)r)   r*   r+   r,   rV   rf   rh   rT   r   r   r-   r.   r.   r.   r/   rl   9  s   					rl   c            P   @   s  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* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ i( Z dR S)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1.2.840.10045.3.0.1Z
c2pnb163v1z1.2.840.10045.3.0.2Z
c2pnb163v2z1.2.840.10045.3.0.3Z
c2pnb163v3z1.2.840.10045.3.0.4Z
c2pnb176w1z1.2.840.10045.3.0.5Z
c2tnb191v1z1.2.840.10045.3.0.6Z
c2tnb191v2z1.2.840.10045.3.0.7Z
c2tnb191v3z1.2.840.10045.3.0.8Z
c2onb191v4z1.2.840.10045.3.0.9Z
c2onb191v5z1.2.840.10045.3.0.10Z
c2pnb208w1z1.2.840.10045.3.0.11Z
c2tnb239v1z1.2.840.10045.3.0.12Z
c2tnb239v2z1.2.840.10045.3.0.13Z
c2tnb239v3z1.2.840.10045.3.0.14Z
c2onb239v4z1.2.840.10045.3.0.15Z
c2onb239v5z1.2.840.10045.3.0.16Z
c2pnb272w1z1.2.840.10045.3.0.17Z
c2pnb304w1z1.2.840.10045.3.0.18Z
c2tnb359v1z1.2.840.10045.3.0.19Z
c2pnb368w1z1.2.840.10045.3.0.20Z
c2tnb431r1z1.2.840.10045.3.1.2Z
prime192v2z1.2.840.10045.3.1.3Z
prime192v3z1.2.840.10045.3.1.4Z
prime239v1z1.2.840.10045.3.1.5Z
prime239v2z1.2.840.10045.3.1.6Z
prime239v3z1.3.132.0.1Z	sect163k1z1.3.132.0.15Z	sect163r2z1.2.840.10045.3.1.1	secp192r1z1.3.132.0.33	secp224r1z1.3.132.0.26Z	sect233k1z1.2.840.10045.3.1.7	secp256r1z1.3.132.0.27Z	sect233r1z1.3.132.0.16Z	sect283k1z1.3.132.0.17Z	sect283r1z1.3.132.0.34	secp384r1z1.3.132.0.36Z	sect409k1z1.3.132.0.37Z	sect409r1z1.3.132.0.35	secp521r1z1.3.132.0.38Z	sect571k1z1.3.132.0.39Z	sect571r1N)r)   r*   r+   r,   r3   r.   r.   r.   r/   rr   I  sR   rr   c               @   s7   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,   rl   rr   r   Z_alternativesr.   r.   r.   r/   rx     s   		rx   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,   r3   r.   r.   r.   r/   r|     s   r|   c               @   sj   e  Z d  Z d Z d e f d e f d e d d d d d	 d
 i f d e d d d d d	 d
 i f g Z d S)ECPrivateKeyz:
    Source: http://www.secg.org/sec1-v2.pdf page 108
    r5   r?   rc   tag_typeZexplicittagr   r8   Tr>   r   N)	r)   r*   r+   r,   r|   r   rx   rU   r-   r.   r.   r.   r/   r}     s
   		r}   c               @   s7   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.   r/   r     s   		r   c               @   s7   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.   r/   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   r1   r.   r.   r.   r/   r     s   r   c               @   s.   e  Z d  Z d Z d d d d d d i 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
    z1.2.840.113549.1.1.1rsaz1.2.840.10040.4.1dsaz1.2.840.10045.2.1ecN)r)   r*   r+   r,   r3   r.   r.   r.   r/   r     s   r   c               @   sO   e  Z d  Z d Z d e f d e d d i f g Z d	 Z d e d e	 i Z
 d S)
PrivateKeyAlgorithmzm
    Original Name: PrivateKeyAlgorithmIdentifier
    Source: https://tools.ietf.org/html/rfc5208#page-3
    	algorithmrc   r8   Tr   r   N)z	algorithmz
parameters)r)   r*   r+   r,   r   r   r-   rd   r   rx   re   r.   r.   r.   r/   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 d	 d
 d i 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
    r5   private_key_algorithmr?   Z
attributesr~   Zimplicitr   r   r8   Tc             C   s+   |  d d j  } d t d t d t i | S)Nr   r   r   r   r   )rL   r4   r   r}   )rP   r   r.   r.   r/   _private_key_spec  s
    	z PrivateKeyInfo._private_key_specNc             C   s  t  | t  r; t  | t  r; t t d t |     | d k rq t  | t  se t j |  } t   } n | d k r t  | t	  s t	 j |  } t
   } | d | d <| d | d <| d | d <| d } | d } nh | d	 k r2t  | t  st 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   rc   zU
                algorithm must be one of "rsa", "dsa", "ec", not %s
                r   r   r5   r   )
isinstancer   r   	TypeErrorr   r   r4   loadr   r:   r   r}   copyrO   reprr   r   
_algorithmr   _public_key)rH   r?   r   paramsr>   Zprivate_key_algo	containerr.   r.   r/   wrap  sJ     	


	
		

	zPrivateKeyInfo.wrapc       
   
   C   s  |  j  d k rK |  d d } t t | d j |  d j j | d j   S|  j  d k r |  d j } t d | d d	 | d	 i  S|  j  d
 k r|  j \ } } | d k r t t d    | d k r_| d d d k r t t d    t	 | d d t
 | d d  t
 | d d   } |  d d j d j   \ } } t | | |  } nU | d k r| d k rt t d |    d t d t d t d t d t i | } | |  d j d j }	 t j |	 j |	 j  Sd 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   rc   r=   r?   r;   r   r6   r7   r   r{   z
                    Unable to compute public key for EC key using Implicit CA
                    parameters
                    ry   rm   rg   rZ   z
                        Unable to compute public key for EC key over a
                        characteristic two field
                        rn   ri   rj   ro   rz   rs   rt   ru   rv   rw   z
                        Unable to compute public key for EC named curve %s,
                        parameters not currently included
                        N)z	secp192r1z	secp224r1z	secp256r1z	secp384r1z	secp521r1)r   r   powrL   parsedr9   rn   rO   r   r   r$   chosenrR   r   r   r   r	   r
   r   rU   rK   rI   rJ   )
rP   r   keyZ
curve_typeZdetailsrn   Zbase_xZbase_yZ
base_pointZpublic_pointr.   r.   r/   _compute_public_keyL  sN    	

!	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 d	 | d	 d
 | d
 d |  j d |  d j i  S|  j  d k r |  d j } |  d d | d <|  j | d <| Sd S)z
        Unwraps the private key into an RSAPrivateKey, DSAPrivateKey or
        ECPrivateKey object

        :return:
            An RSAPrivateKey, DSAPrivateKey or ECPrivateKey object
        r   r?   r   r   rc   r5   r   r;   r<   r=   r>   r   N)r   r   r:   r>   )rP   r   outputr.   r.   r/   r     s     	


	zPrivateKeyInfo.unwrapc             C   sr   |  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   rc   r{   N)r   rO   r   upperr   namerL   )rP   r   r   valuer.   r.   r/   rn     s    			zPrivateKeyInfo.curvec             C   sf   |  j  d k r- t t d |  j  j      t j |  d d d j d  d } | d k rb d	 Sd
 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   rc   r<   rA         sha1sha2)r   rO   r   r   rD   rF   rL   )rP   byte_lenr.   r.   r/   	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   rL   )rP   r.   r.   r/   r     s    zPrivateKeyInfo.algorithmc             C   s   |  j  d k r |  j d k r5 |  d j d j } nJ |  j d k r\ |  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?   r6   r   r   rc   r;   r   rA   r   r   )	_bit_sizer   r   rL   rC   rD   rE   rF   )rP   r'   r6   r.   r.   r/   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   )rC   rD   rE   r   )rP   r.   r.   r/   	byte_size  s    zPrivateKeyInfo.byte_sizec             C   ss   |  j  d k rl |  j d k r] |  d j } | d rK | 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   )rP   r   r.   r.   r/   r>     s    
zPrivateKeyInfo.public_keyc             C   s0   t  d d |  j d |  d d i d |  j i  S)z\
        :return:
            A PublicKeyInfo object derived from this private key.
        r   rc   r   r>   )PublicKeyInfor   r>   )rP   r.   r.   r/   public_key_info*  s
    	zPrivateKeyInfo.public_key_infoc             C   s  |  j  d k r|  d d } |  d j } |  j d k rZ d | d j | d j f } nH|  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 rd | j j } | j d  } | | 7} n | j d k r*| } nx | 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	  r| j d  } t
 j |  j   |  _  |  j  S)aZ  
        Creates a fingerprint that can be compared with a public key to see if
        the two form a pair.

        This fingerprint is not compatiable 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   rc   r?   r   z%d:%dr6   r7   r   z%d:%d:%d:%dr;   r<   r=   r   r>   rz   z%s:zutf-8r{   ry   rm      :rn   ri   rj   )_fingerprintr   r   rL   r>   r   r   encoder   r   hashlibsha256digest)rP   r   r   to_hashr>   r.   r.   r/   fingerprint9  sD    
	


	
zPrivateKeyInfo.fingerprint)r)   r*   r+   r,   r   r   r   r   r-   r   _spec_callbacksr   r   r   r   rS   r   r   r   propertyrn   r   r   r   r   r>   r   r   r.   r.   r.   r/   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.   r/   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
    rk   Zpgen_counterN)r)   r*   r+   r,   r   r   r-   r.   r.   r.   r/   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<   jr8   TZvalidation_paramsN)r)   r*   r+   r,   r   r   r-   r.   r.   r.   r/   r     s   			r   c               @   s4   e  Z d  Z d Z d d d d d d d d	 i Z d
 S)PublicKeyAlgorithmIdzM
    Original Name: None
    Source: https://tools.ietf.org/html/rfc3279
    z1.2.840.113549.1.1.1r   z1.2.840.10040.4.1r   z1.2.840.10045.2.1r   z1.2.840.10046.2.1dhN)r)   r*   r+   r,   r3   r.   r.   r.   r/   r     s
   r   c               @   sU   e  Z d  Z d Z d e f d e d d i f g Z d
 Z d e d e	 d e
 i Z d	 S)PublicKeyAlgorithmzd
    Original Name: AlgorithmIdentifier
    Source: https://tools.ietf.org/html/rfc5280#page-18
    r   rc   r8   Tr   r   r   N)z	algorithmz
parameters)r)   r*   r+   r,   r   r   r-   rd   r   rx   r   re   r.   r.   r.   r/   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   s7   |  d d j  } d t d t d t d  f d t i | S)Nr   r   r   r   r   )rL   r9   r   rU   )rP   r   r.   r.   r/   _public_key_spec  s    	zPublicKeyInfo._public_key_specNc             C   s   t  | t  r; t  | t  r; t t d t |     | d k rb t 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   rc   r>   )r   r   r   r   r   r   rO   r   r   r   r   r   dumpr    )rH   r>   r   Zalgor   r.   r.   r/   r     s"     		
zPublicKeyInfo.wrapc             C   s]   |  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anri   zj
            Only RSA public keys may be unwrapped - this key is %s %s public
            key
            N)r   r   r   rO   r   )rP   key_typeZa_anr.   r.   r/   r     s    	zPublicKeyInfo.unwrapc             C   sr   |  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   rc   r{   N)r   rO   r   r   r   r   rL   )rP   r   r   r   r.   r.   r/   rn   #  s    			zPublicKeyInfo.curvec             C   s   |  j  d k r- t t d |  j  j      |  d d } | j d k rN d St j | d j d  d } | d	 k r{ d
 Sd 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   rc   Nr<   rA   r   r   r   r   )r   rO   r   r   rL   rD   rF   )rP   rc   r   r.   r.   r/   r   E  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   rL   )rP   r.   r.   r/   r   d  s    zPublicKeyInfo.algorithmc             C   s   |  j  d k r |  j d k rC t |  d j  d d d |  _  n |  j d k ri |  d j d j } n$ |  j d	 k r |  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   rA   r   r   r6   r   r   rc   r;   r   )	r   r   rM   rL   r   rC   rD   rE   rF   )rP   r'   r6   r.   r.   r/   r   o  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   )rC   rD   rE   r   )rP   r.   r.   r/   r     s    zPublicKeyInfo.byte_sizec             C   s8   |  j  d k r1 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   )rP   r.   r.   r/   r     s    "zPublicKeyInfo.sha1c             C   s8   |  j  d k r1 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   )rP   r.   r.   r/   r     s    "zPublicKeyInfo.sha256c             C   s  |  j  d k r|  d d j } |  d d } | d k rh |  d j } d | d j | d j f } n4| d	 k r |  d j } d
 | d j | d j | d j | j f } n | d k r|  d } | j d k rd | j j } | j d  } | | j 7} n | j d k r!| j } n{ | 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  r| j d  } t j	 |  j
   |  _  |  j  S)a[  
        Creates a fingerprint that can be compared with a private key to see if
        the two form a pair.

        This fingerprint is not compatiable 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   rc   r   r>   z%d:%dr6   r7   r   z%d:%d:%d:%dr;   r<   r=   r   rz   z%s:zutf-8r{   ry   rm   r   rn   ri   rj   )r   rL   r   r   r   r   r   r   r   r   r   )rP   r   r   r   r   r.   r.   r/   r     sB    




zPublicKeyInfo.fingerprint)r)   r*   r+   r,   r   r    r-   r   r   r   r   r   r   r   rS   r   r   r   rn   r   r   r   r   r   r   r   r.   r.   r.   r/   r     s(   	+"	r   )Nr,   Z
__future__r   r   r   r   r   rD   Z_elliptic_curver   r   r	   r
   r   r   r   Z_errorsr   Z_typesr   r   r   Zalgosr   r   r   Zcorer   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   utilr$   r%   r&   r0   r2   r4   r9   r:   r@   rT   rU   rV   rX   r[   r_   r`   rf   rh   rl   rr   rx   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r.   r.   r.   r/   <module>   sV   "4	dF7 