3 l_'@sddlmZmZmZddlmZddlmZmZddl m Z ddl m Z ddZ dd Zeje jGd d d eZd d Zeje jGdddeZeje jGdddeZdS))absolute_importdivisionprint_function)utils)UnsupportedAlgorithm_Reasons) serialization)dhcCs|j}|j}|j|}|j||jk|j||j}|jr|jd}|j ||j||j|j |d}|j ||j||j}|j|dk|S)Nz BIGNUM **r) _lib_ffiZ DHparams_dupopenssl_assertNULLgcZDH_freeZCRYPTOGRAPHY_IS_LIBRESSLnew DH_get0_pqgBN_dupZ DH_set0_pqg)dh_cdatabackendlibZffi param_cdataqZq_dupresr/usr/lib64/python3.6/dh.py_dh_params_dup s  rcCst||}t||S)N)r _DHParameters)rrrrrr_dh_cdata_to_parameterss rc@s,eZdZddZddZddZddZd S) rcCs||_||_dS)N)_backend _dh_cdata)selfrrrrr__init__&sz_DHParameters.__init__cCs|jjjd}|jjjd}|jjjd}|jjj|j||||jj|d|jjjk|jj|d|jjjk|d|jjjkrd}n|jj|d}t j |jj|d|jj|d|dS)Nz BIGNUM **r)pgr) rr rr rrr r _bn_to_intr DHParameterNumbers)r r"r#rq_valrrrparameter_numbers*sz_DHParameters.parameter_numberscCs |jj|S)N)rZgenerate_dh_private_key)r rrrgenerate_private_key;sz"_DHParameters.generate_private_keycCs~|tjjk rtd|jjjsl|jjjd}|jjj |j |jjj ||jjj |d|jjj krlt dt j|jj|||j S)Nz%Only PKCS3 serialization is supportedz BIGNUM **rz'DH X9.42 serialization is not supported)rZParameterFormatZPKCS3 ValueErrorrr Cryptography_HAS_EVP_PKEY_DHXr rrrrrrUNSUPPORTED_SERIALIZATIONZ_parameter_bytes)r encodingformatrrrrparameter_bytes>s   z_DHParameters.parameter_bytesN)__name__ __module__ __qualname__r!r'r(r.rrrrr$srcCsL|jjd}|jj|||jj|jj|j|d|jjk|jj|dS)Nz BIGNUM **r)r rr rrr Z BN_num_bits)rrr"rrr_get_dh_num_bitsRs r2c@sHeZdZddZeddZddZddZd d Zd d Z d dZ dS) _DHPrivateKeycCs&||_||_||_|jjj||_dS)N)rr _evp_pkeyr ZDH_size_key_size_bytes)r rrevp_pkeyrrrr![sz_DHPrivateKey.__init__cCst|j|jS)N)r2rr)r rrrkey_sizeasz_DHPrivateKey.key_sizecCsT|jjjd}|jjjd}|jjjd}|jjj|j||||jj|d|jjjk|jj|d|jjjk|d|jjjkrd}n|jj|d}|jjjd}|jjjd}|jjj |j|||jj|d|jjjk|jj|d|jjjkt j t j t j |jj|d|jj|d|d|jj|dd|jj|ddS)Nz BIGNUM **r)r"r#r)r'y)public_numbersx)rr rr rrr rr$ DH_get0_keyr ZDHPrivateNumbersDHPublicNumbersr%)r r"r#rr&pub_keyZpriv_keyrrrprivate_numberses,z_DHPrivateKey.private_numberscCs|jjjd|j}|jjjd}|jjj|j||jjj|jj|d|jjjk|jjj ||d|j}|dkr|jj }t d|nL|jj|dk|jjj |d|}|jt |}|dkrd||}|SdS)Nzunsigned char[]z BIGNUM **rr zKError computing shared key. Public key is likely invalid for this exchange.)rr rr5r r;rrr ZDH_compute_keyZ_consume_errors_with_textr)bufferlen)r Zpeer_public_keyZbufr=rZerrors_with_textkeyZpadrrrexchanges$  z_DHPrivateKey.exchangecCst|j|j}|jjjd}|jjj|j||jjj|jj|d|jjjk|jjj |d}|jj||jjjk|jjj |||jjj}|jj|dk|jj |}t |j||S)Nz BIGNUM **rr ) rrrr rr r;rr rZ DH_set0_keyZ_dh_cdata_to_evp_pkey _DHPublicKey)r rr=Z pub_key_duprr6rrr public_keys z_DHPrivateKey.public_keycCst|j|jS)N)rrr)r rrr parameterssz_DHPrivateKey.parameterscCs|tjjk rtd|jjjsl|jjjd}|jjj |j |jjj ||jjj |d|jjj krlt dt j|jj|||||j|j S)Nz0DH private keys support only PKCS8 serializationz BIGNUM **rz'DH X9.42 serialization is not supported)rZ PrivateFormatZPKCS8r)rr r*r rrrrrrr+Z_private_key_bytesr4)r r,r-Zencryption_algorithmrrrr private_bytess*   z_DHPrivateKey.private_bytesN) r/r0r1r!propertyr7r>rDrFrGrHrrrrr3Ys r3c@s8eZdZddZeddZddZddZd d Zd S) rEcCs&||_||_||_t|j|j|_dS)N)rrr4r2_key_size_bits)r rrr6rrrr!sz_DHPublicKey.__init__cCs|jS)N)rJ)r rrrr7sz_DHPublicKey.key_sizecCs|jjjd}|jjjd}|jjjd}|jjj|j||||jj|d|jjjk|jj|d|jjjk|d|jjjkrd}n|jj|d}|jjjd}|jjj |j||jjj|jj|d|jjjkt j t j |jj|d|jj|d|d|jj|ddS)Nz BIGNUM **r)r"r#r)r'r8) rr rr rrr rr$r;r r<r%)r r"r#rr&r=rrrr9s&z_DHPublicKey.public_numberscCst|j|jS)N)rrr)r rrrrGsz_DHPublicKey.parameterscCs|tjjk rtd|jjjsl|jjjd}|jjj |j |jjj ||jjj |d|jjj krlt dt j|jj||||jdS)Nz>DH public keys support only SubjectPublicKeyInfo serializationz BIGNUM **rz'DH X9.42 serialization is not supported)rZ PublicFormatZSubjectPublicKeyInfor)rr r*r rrrrrrr+Z_public_key_bytesr4)r r,r-rrrr public_bytess    z_DHPublicKey.public_bytesN) r/r0r1r!rIr7r9rGrKrrrrrEs  rEN)Z __future__rrrZ cryptographyrZcryptography.exceptionsrrZcryptography.hazmat.primitivesrZ)cryptography.hazmat.primitives.asymmetricr rrZregister_interfaceZDHParametersWithSerializationobjectrr2ZDHPrivateKeyWithSerializationr3ZDHPublicKeyWithSerializationrErrrrs   -v