U
    ;gV3                     @   sX  d dl mZmZmZmZ d dlZd dlZddlmZ ddl	m
Z
mZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ ddl m!Z! e Z"e"dkr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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 ne"dks2e"dkrddl8m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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 n\ddl9m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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 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)gZ:d1d+dZ;d2d,dZ<d3d-dZ=d4d/dZ>d0d Z?dS )5    )unicode_literalsdivisionabsolute_importprint_functionN   )backend)	armorCertificateDHParametersEncryptedPrivateKeyInfoNullOrderedDict
Pbkdf2SaltPrivateKeyInfoPublicKeyInfo)_unwrap_private_key_info)pretty_message)	type_namestr_cls)pbkdf2pbkdf2_iteration_calculator)aes_cbc_pkcs7_encrypt)
rand_bytesmac)r	   dsa_sign
dsa_verify
ecdsa_signecdsa_verifygenerate_pairgenerate_dh_parametersload_certificateload_pkcs12load_private_keyload_public_key
PrivateKey	PublicKeyrsa_pkcs1v15_signrsa_pkcs1v15_verifyrsa_pss_signrsa_pss_verifyrsa_pkcs1v15_encryptrsa_pkcs1v15_decryptrsa_oaep_encryptrsa_oaep_decryptwinZ	winlegacyr	   r   r   dump_certificatedump_dh_parametersdump_openssl_private_keydump_private_keydump_public_keyr   r   r   r   r    r!   r"   r#   r$   r%   r-   r,   r+   r*   r&   r'   r(   r)   pemc                 C   s\   |t ddgkr"ttdt|t| ts>ttdt| |  }|dkrXt	d|}|S )a  
    Serializes an asn1crypto.algos.DHParameters object into a byte string

    :param dh_parameters:
        An asn1crypto.algos.DHParameters object

    :param encoding:
        A unicode string of "pem" or "der"

    :return:
        A byte string of the encoded DH parameters
    r4   derF
            encoding must be one of "pem", "der", not %s
            zp
            dh_parameters must be an instance of asn1crypto.algos.DHParameters,
            not %s
            zDH PARAMETERS)
set
ValueErrorr   repr
isinstancer
   	TypeErrorr   dumpr   )Zdh_parametersencodingoutput r?   7/tmp/pip-unpacked-wheel-x1gypflw/oscrypto/asymmetric.pyr0      s    

c                 C   st   |t ddgkr"ttdt|t| t}t| tsL|sLttdt| |rV| j	} | 
 }|dkrptd|}|S )a#  
    Serializes a public key object into a byte string

    :param public_key:
        An oscrypto.asymmetric.PublicKey or asn1crypto.keys.PublicKeyInfo object

    :param encoding:
        A unicode string of "pem" or "der"

    :return:
        A byte string of the encoded public key
    r4   r5   r6   z
            public_key must be an instance of oscrypto.asymmetric.PublicKey or
            asn1crypto.keys.PublicKeyInfo, not %s
            z
PUBLIC KEY)r7   r8   r   r9   r:   r%   r   r;   r   asn1r<   r   )
public_keyr=   is_oscryptor>   r?   r?   r@   r3      s"    

c                 C   st   |t ddgkr"ttdt|t| t}t| tsL|sLttdt| |rV| j	} | 
 }|dkrptd|}|S )a&  
    Serializes a certificate object into a byte string

    :param certificate:
        An oscrypto.asymmetric.Certificate or asn1crypto.x509.Certificate object

    :param encoding:
        A unicode string of "pem" or "der"

    :return:
        A byte string of the encoded certificate
    r4   r5   r6   z
            certificate must be an instance of oscrypto.asymmetric.Certificate
            or asn1crypto.x509.Certificate, not %s
            CERTIFICATE)r7   r8   r   r9   r:   r	   Asn1Certificater;   r   rA   r<   r   )certificater=   rC   r>   r?   r?   r@   r/      s"    

   c                 C   sh  |t ddgkr"ttdt||dk	rZt|tsFttdt||dkrZttdt| t}t| t	s|sttdt| |r| j
} |  }|dk	r<d	}d
}d}t|}	t|||dd}
|
dk rd}
|d}t|||	|
|}t||d\}}tddtd|	d|
|t ddd||ddd|d }|dkrd|dkrVd}nd}t||}|S )aM  
    Serializes a private key object into a byte string of the PKCS#8 format

    :param private_key:
        An oscrypto.asymmetric.PrivateKey or asn1crypto.keys.PrivateKeyInfo
        object

    :param passphrase:
        A unicode string of the passphrase to encrypt the private key with.
        A passphrase of None will result in no encryption. A blank string will
        result in a ValueError to help ensure that the lack of passphrase is
        intentional.

    :param encoding:
        A unicode string of "pem" or "der"

    :param target_ms:
        Use PBKDF2 with the number of iterations that takes about this many
        milliseconds on the current machine.

    :raises:
        ValueError - when a blank string is provided for the passphrase

    :return:
        A byte string of the encoded and encrypted public key
    r4   r5   r6   NM
                passphrase must be a unicode string, not %s
                 x
                passphrase may not be a blank string - pass None to disable
                encryption
                z
            private_key must be an instance of oscrypto.asymmetric.PrivateKey
            or asn1crypto.keys.PrivateKeyInfo, not %s
            Z
aes256_cbc    sha256T)	target_msquieti'  utf-8Zpbes2r   Z	specified)namevalue)	algorithm
parameters)saltZiteration_countZprf)Zkey_derivation_funcZencryption_scheme)encryption_algorithmZencrypted_datazPRIVATE KEYzENCRYPTED PRIVATE KEY)r7   r8   r   r9   r:   r   r;   r   r$   r   rA   r<   r   r   encoder   r   r   r   r   r   )private_key
passphraser=   rM   rC   r>   cipher
key_lengthZkdf_hmacZkdf_saltZ
iterationspassphrase_byteskeyiv
ciphertextobject_typer?   r?   r@   r2      sz    






c           
      C   sl  |dk	r8t |ts$ttdt||dkr8ttdt | t}t | tsb|sbttdt| |rl| j} t	| 
 }d}|dk	r*td}t }d|d< d	t|d
 |d< d}|d}t||dd   }|t|kr|t|| |dd   7 }q|d| }t|||\}}| jdkr<d}	n"| jdkrNd}	n| jdkr^d}	t|	||dS )a   
    Serializes a private key object into a byte string of the PEM formats used
    by OpenSSL. The format chosen will depend on the type of private key - RSA,
    DSA or EC.

    Do not use this method unless you really must interact with a system that
    does not support PKCS#8 private keys. The encryption provided by PKCS#8 is
    far superior to the OpenSSL formats. This is due to the fact that the
    OpenSSL formats don't stretch the passphrase, making it very easy to
    brute-force.

    :param private_key:
        An oscrypto.asymmetric.PrivateKey or asn1crypto.keys.PrivateKeyInfo
        object

    :param passphrase:
        A unicode string of the passphrase to encrypt the private key with.
        A passphrase of None will result in no encryption. A blank string will
        result in a ValueError to help ensure that the lack of passphrase is
        intentional.

    :raises:
        ValueError - when a blank string is provided for the passphrase

    :return:
        A byte string of the encoded and encrypted public key
    NrH   rI   rJ   z
            private_key must be an instance of oscrypto.asymmetric.PrivateKey or
            asn1crypto.keys.PrivateKeyInfo, not %s
               z4,ENCRYPTEDz	Proc-TypezAES-128-CBC,%sasciizDEK-InforO   r      eczEC PRIVATE KEYrsazRSA PRIVATE KEYdsazDSA PRIVATE KEY)headers)r:   r   r;   r   r   r8   r$   r   rA   r   r<   r   r   binasciihexlifydecoderV   hashlibmd5digestlenr   rR   r   )
rW   rX   rC   r>   rf   r]   rZ   r[   r\   r_   r?   r?   r@   r1   u  sN    



$)r4   )r4   )r4   )r4   rG   )@
__future__r   r   r   r   rj   rg   rI   r   Z_asn1r   r	   rE   r
   r   r   r   r   r   r   _asymmetricr   _errorsr   _typesr   r   kdfr   r   Z	symmetricr   utilr   Z_backendZ_mac.asymmetricr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Z_win.asymmetricZ_openssl.asymmetric__all__r0   r3   r/   r2   r1   r?   r?   r?   r@   <module>   s`   ,
^^\
%
)
)
v