o
    Œõ±jV3  ã                   @   s"  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 nee"dks–e"dkrÅd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 g d¢Z:ddd„Z;ddd„Z<ddd„Z=ddd„Z>dd„ Z?dS )é    )Ú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ÚwinÚ	winlegacy)r	   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ƒvrttdt|ƒƒƒ‚t| tƒsttdt| ƒƒƒ‚|  ¡ }|dkr,t	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
    r5   ÚderúF
            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   )Údh_parametersÚencodingÚoutput© rA   ú†/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/oscrypto/asymmetric.pyr1   ˆ   s   ü
û
r1   c                 C   ót   |t ddgƒvrttdt|ƒƒƒ‚t| tƒ}t| tƒs&|s&ttdt| ƒƒƒ‚|r+| j	} |  
¡ }|dkr8t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
    r5   r6   r7   zŽ
            public_key must be an instance of oscrypto.asymmetric.PublicKey or
            asn1crypto.keys.PublicKeyInfo, not %s
            z
PUBLIC KEY)r8   r9   r   r:   r;   r%   r   r<   r   Úasn1r=   r   )Ú
public_keyr?   Úis_oscryptor@   rA   rA   rB   r4   ­   ó"   ü
û
r4   c                 C   rC   )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
    r5   r6   r7   z�
            certificate must be an instance of oscrypto.asymmetric.Certificate
            or asn1crypto.x509.Certificate, not %s
            ÚCERTIFICATE)r8   r9   r   r:   r;   r	   ÚAsn1Certificater<   r   rD   r=   r   )Úcertificater?   rF   r@   rA   rA   rB   r0   Ö   rG   r0   éÈ   c                 C   sb  |t ddgƒvrttdt|ƒƒƒ‚|dur-t|tƒs#ttdt|ƒƒƒ‚|dkr-ttdƒƒ‚t| tƒ}t| t	ƒsB|sBttdt| ƒƒƒ‚|rG| j
} |  ¡ }|dur�d	}d
}d}t|ƒ}	t|||dd�}
|
dk rgd}
| d¡}t|||	|
|ƒ}t||dƒ\}}tddtd|	d�|
|tƒ dœdœdœ||dœdœdœ|dœƒ ¡ }|dkr¯|du r¨d}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
    r5   r6   r7   NúM
                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
            Ú
aes256_cbcé    Úsha256T)Ú	target_msÚquieti'  úutf-8Úpbes2r   Ú	specified)ÚnameÚvalue)Ú	algorithmÚ
parameters)ÚsaltÚiteration_countÚprf)Úkey_derivation_funcÚencryption_scheme)Úencryption_algorithmÚencrypted_datazPRIVATE KEYzENCRYPTED PRIVATE KEY)r8   r9   r   r:   r;   r   r<   r   r$   r   rD   r=   r   r   Úencoder   r   r   r   r   r   )Úprivate_keyÚ
passphraser?   rR   rF   r@   ÚcipherÚ
key_lengthÚkdf_hmacÚkdf_saltÚ
iterationsÚpassphrase_bytesÚkeyÚivÚ
ciphertextÚobject_typerA   rA   rB   r3   ÿ   s~   ü
üÿ
û
þþúþþñþèç
r3   c           
      C   sl  |durt |tƒsttdt|ƒƒƒ‚|dkrttdƒƒ‚t | tƒ}t | tƒs1|s1ttdt| ƒƒƒ‚|r6| j} t	| ƒ 
¡ }d}|dur˜tdƒ}tƒ }d|d< d	t |¡ d
¡ |d< d}| d¡}t ||dd…  ¡ ¡ }|t|ƒkrŠ|t || |dd…  ¡ ¡ 7 }|t|ƒkss|d|… }t|||ƒ\}}| jdkr d}	n| jdkr¨d}	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
    NrL   rM   rN   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-InforT   r   é   ÚeczEC PRIVATE KEYÚrsazRSA PRIVATE KEYÚdsazDSA PRIVATE KEY)Úheaders)r;   r   r<   r   r   r9   r$   r   rD   r   r=   r   r   ÚbinasciiÚhexlifyÚdecoderb   ÚhashlibÚmd5ÚdigestÚlenr   rY   r   )
rc   rd   rF   r@   ru   rl   rf   rj   rk   rn   rA   rA   rB   r2   u  sP   
üÿ
û
"ÿ


r2   )r5   )r5   rK   )@Ú
__future__r   r   r   r   ry   rv   rM   r   Ú_asn1r   r	   rI   r
   r   r   r   r   r   r   Ú_asymmetricr   Ú_errorsr   Ú_typesr   r   Úkdfr   r   Ú	symmetricr   Úutilr   Ú_backendÚ_mac.asymmetricr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Ú_win.asymmetricÚ_openssl.asymmetricÚ__all__r1   r4   r0   r3   r2   rA   rA   rA   rB   Ú<module>   s.   ,^^\

%
)
)v