a
    ©ˆcò  ã                   @   s$   d dl Z dd„ Zdd„ Zdd„ ZdS )é    Nc                 C   s   t  t j¡}| | |¡ |S )N)ÚsslÚ
SSLContextÚPROTOCOL_TLS_SERVERÚload_cert_chain)Ú	cert_fileÚ	pkey_fileÚcontext© r	   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/wslink/ssl_context.pyÚload_ssl_context   s    r   c           
      C   s®   dd l }dd l}dd l}ddlm} | ¡ \}}| ¡ \}}	| |j|	¡ | |j|¡ | ||  	|j
j¡¡ | ||j|j
j|jj| ¡ d�¡ | |¡ | |¡ ||	fS )Nr   )Úserialization)ÚencodingÚformatZencryption_algorithm)ÚatexitÚosÚtempfileÚcryptography.hazmat.primitivesr   ÚmkstempÚregisterÚremoveÚwriteZpublic_bytesÚEncodingZPEMZprivate_bytesZPrivateFormatZTraditionalOpenSSLZNoEncryptionÚclose)
ÚcertÚpkeyr   r   r   r   Zcert_handler   Zpkey_handler   r	   r	   r
   Úsave_ssl_files
   s(    ýþ	

r   c                 C   s8  z<ddl m} ddlm} ddlm} ddlm} dd l}W n t	yX   t
dƒd ‚Y n0 d| › d| › �}|jd	d
d�}| | |jd¡| |j|¡g¡}| ddd¡}	| ¡  |¡ |¡ | ¡ ¡ | ¡ ¡ |j ¡ |	 ¡ |j ¡ |	d  ¡j| |jg¡dd�j| | |¡g¡dd�j||  ¡ d�}
t!|
|ƒS )Nr   )Úx509)ÚNameOID)Úhashes)Úrsaz<Using ad-hoc certificates requires the cryptography library.z*.z/CN=i  i   )Zpublic_exponentZkey_sizezDummy Certificateé   im  F)Úcritical)Zprivate_keyÚ	algorithm)"Zcryptographyr   Zcryptography.x509.oidr   r   r   Z)cryptography.hazmat.primitives.asymmetricr   ÚdatetimeÚImportErrorÚ	TypeErrorZgenerate_private_keyÚNameZNameAttributeZORGANIZATION_NAMEZCOMMON_NAMEÚ	timedeltaZCertificateBuilderZsubject_nameZissuer_nameZ
public_keyZserial_numberZrandom_serial_numberZnot_valid_beforeÚtodayZnot_valid_afterÚadd_extensionZExtendedKeyUsageZOID_SERVER_AUTHZSubjectAlternativeNameZDNSNameÚsignÚSHA256r   )Úhostr   r   r   r   r#   Zcnr   ÚsubjectZone_dayr   r	   r	   r
   Úgenerate_ssl_pair$   sR    ÿþþÿÿþýüûúùø	÷ÿr.   )r   r   r   r.   r	   r	   r	   r
   Ú<module>   s   