3 گa�g � @ s& d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlm Z ddlm Z ddlmZ dd lmZ ddl Z dd lmZ ddlmZ ddlmZ dd lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl"m$Z$ ddl"m%Z% ddl&Z&ddl'm(Z( ddl'm)Z) ddl*Z*ddl+Z,ddl-m.Z/ ddl0m1Z1 ddl0m2Z2 ddl0m3Z3 ddl4m5Z5 e�r�ddl6m7Z7 dd l8m9Z9 ej:e;�Z<d~e=e>e>e>e>e?e3j@d%�d&d'�ZAde=e>e>e>e>e3j@d(�d)d*�ZBd�e3j@eee> e e> f e>e?e?e3jCd,�d-d.�ZDe3j@e e> e>e3jCd/�d0d1�ZEeFe?d2�d3d4�ZGeFeFe?d5�d6d7�ZHe>eFe e=e3jCee> f d8�d9d:�ZId�e=e>ee> eFd<�d=d>�ZJe>e?d?�d@dA�ZKe2jLddB�dCdD�ZMe2jLddB�dEdF�ZNeedGdHee!f eFeFejOddI�dJdK�ZPe>e>ddL�dMdN�ZQe2jLddB�dOdP�ZReFe e(jSe=f dQ�dRdS�ZTe(jUfeFee=eFgee(jSe(jVf f e=ee(jSe(jVf dT�dUdV�ZWe(jUfeFee=eFgee(jSe(jVf f e=ee> dT�dWdX�ZXe(jUfeFe=ee> dY�dZd[�ZYeFee=eFgee(jSe(jVf f e=ee> d\�d]d^�ZZee(jSe(jVf ee> d_�d`da�Z[e(jUfeFe=ee> dY�dbdc�Z\e(jUfeFe=ee> dd�dedf�Z]e(jUfeee(jS ee&j^ f e=eFdg�dhdi�Z_e>ejdj�dkdl�Z`e>ejdj�dmdn�Zae>ee(jSgeeF f ejdo�dpdq�Zbe>e>dr�dsdt�Zcejddueje�Zfe>e e>e>f dv�dwdx�Zge>e=dj�dydz�Zhd�ee> e>e?e>d{�d|d}�ZidS )�z�Certbot client crypto utility functions. .. todo:: Make the transition to use PSS rather than PKCS1_v1_5 when the server is capable of handling the signatures. � N)�Callable)�List)�Optional)�Set)�Tuple)� TYPE_CHECKING)�Union)�x509)�InvalidSignature)�UnsupportedAlgorithm)�default_backend)�hashes)�ec)�DSAPublicKey)�ECDSA)�EllipticCurvePublicKey)�PKCS1v15)�RSAPublicKey)�Encoding)�NoEncryption)� PrivateFormat)�crypto)�SSL)�crypto_util)�errors)� interfaces)�util)�os)�Ed448PublicKey)�Ed25519PublicKey�rsa� secp256r1�key-certbot.pemT)�key_size�key_dir�key_type�elliptic_curve�keyname�strict_permissions�returnc C s� yt | |pd|d�}W nD tk rZ } z(tjddd� tjdt|�� |�W Y dd}~X nX tj|d|� tjt j j||�d d �\}} |� |j|� W dQ R X |dkr�tjd| | � ntjd | | � tj | |�S )a Initializes and saves a privkey. Inits key and saves it in PEM format on the filesystem. .. note:: keyname is the attempted filename, it may be different if a file already exists at the path. :param int key_size: key size in bits if key size is rsa. :param str key_dir: Key save directory. :param str key_type: Key Type [rsa, ecdsa] :param str elliptic_curve: Name of the elliptic curve if key type is ecdsa. :param str keyname: Filename of key :param bool strict_permissions: If true and key_dir exists, an exception is raised if the directory doesn't have 0700 permissions or isn't owned by the current user. :returns: Key :rtype: :class:`certbot.util.Key` :raises ValueError: If unable to generate the key given key_size. r! )�bitsr&