o
    >e!                     @   s   d dl Z d dlZd dlmZmZmZ d dlZd dl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mZ d dlmZ d d	lmZmZ d d
lmZmZmZ ddgZG dd dZG dd deZdefddZ G dd deZ!defddZ"dS )    N)ContextManagerListOptional)
CLIContext)
grab_certs)	CLIConfig)SigningCommandPluginregister_signing_plugin)_warn_empty_passphraseloggerreadable_file)ConfigurationError)PemDerSignatureConfigPKCS12SignatureConfig)Signersigner_from_p12_configsigner_from_pemder_configPemderPluginPKCS12Pluginc                   @   s*   e Zd ZdefddZdd Zdd ZdS )	KeyFileConfigWrapperconfigc                 C   s&   |j }|di | _|di | _d S )Nzpemder-setupszpkcs12-setups)
raw_configgetpemder_setupspkcs12_setups)selfr   config_dict r   d/var/www/html/humari/django-venv/lib/python3.10/site-packages/pyhanko/cli/commands/signing/simple.py__init__   s   zKeyFileConfigWrapper.__init__c                 C   8   z| j | }W n ty   td| dw t|S )Nz There's no PKCS#12 setup named '')r   KeyErrorr   r   from_configr   namesetupr   r   r   get_pkcs12_config       
z&KeyFileConfigWrapper.get_pkcs12_configc                 C   r    )Nz There's no PEM/DER setup named 'r!   )r   r"   r   r   r#   r$   r   r   r   get_pemder_config'   r(   z&KeyFileConfigWrapper.get_pemder_configN)__name__
__module____qualname__r   r   r'   r)   r   r   r   r   r      s    r   c                   @   s>   e Zd ZdZdZdeej fddZde	de
e fddZd	S )
r   pemderz$read key material from PEM/DER filesreturnc                 C   sp   t jddtddt jddtddt jdtdd	d
t jdtdddt jdddt dddt jddtddddgS )N)z--keyz)file containing the private key (PEM/DER)F)helptyperequired)z--certz2file containing the signer's certificate (PEM/DER)z--chainTzkfile(s) containing the chain of trust for the signer's certificate (PEM/DER). May be passed multiple times.r0   multipler/   )z--pemder-setupzCname of preconfigured PEM/DER profile (overrides all other options)r0   r1   r/   z
--passfilez2file containing the passphrase for the private keyrstdinr/   r1   r0   show_defaultz	--no-passz*assume the private key file is unencryptedr/   r0   is_flagdefaultr:   )clickOptionr   strFileboolr   r   r   r   click_options3   sP   	zPemderPlugin.click_optionscontextc                       t j fdd}| S )Nc                   3       t  fi V  d S N)_pemder_signerr   rF   kwargsr   r   _mf      z&PemderPlugin.create_signer.<locals>._m
contextlibcontextmanagerr   rF   rL   rM   r   rK   r   create_signerc      zPemderPlugin.create_signerN)r*   r+   r,   subcommand_namehelp_summaryr   r?   r@   rE   r   r   r   rS   r   r   r   r   r   /   s    0ctxc              
   C   s  |r5| j }|d u rtdz	t||}W n1 ty4 }	 zd| }
tj|
|	d t|
d }	~	ww |r9|s>tdt||t	|| j
d}|jd urR|j}n*|d urd|  d}|  n|jrz|sztjddd}|syt  d }nd }t||d	S )
Nz7The --pemder-setup option requires a configuration filez"Error while reading PEM/DER setup exc_infozXEither both the --key and --cert options, or the --pemder-setup option must be provided.)key_file	cert_fileother_certs
prefer_pssutf-8zKey passphrase: prompt)provided_key_passphrase)r   r?   ClickExceptionr   r)   r   r   errorr   r   r]   key_passphrasereadlinestripencodecloseprompt_passphrasegetpassr
   r   )rW   keycertchainpemder_setuppassfileno_pass
cli_configpemder_configemsg
passphraser   r   r   rJ   m   sR   	




rJ   c                   @   sR   e Zd ZdZdZdeej fddZdeej	 fddZ
dedee fd	d
ZdS )r   pkcs12z$read key material from PKCS#12 filesr.   c                 C   s   t jdtddgS )N)pfxF)r0   r1   )r?   Argumentr   rD   r   r   r   click_extra_arguments   s   z"PKCS12Plugin.click_extra_argumentsc                 C   sP   t jdtdddt jdtdddt jd	d
dt dddt jddtddddgS )N)z--p12-setupFzCname of preconfigured PKCS#12 profile (overrides all other options)r5   r2   TzPEM/DER file(s) containing extra certificates to embed (e.g. chain of trust not embedded in the PKCS#12 file)May be passed multiple times.r3   r6   z4file containing the passphrase for the PKCS#12 file.r7   r8   r9   r;   z&assume the PKCS#12 file is unencryptedr<   )r?   r@   rA   r   rB   rC   rD   r   r   r   rE      s8   zPKCS12Plugin.click_optionsrF   c                    rG   )Nc                   3   rH   rI   )_pkcs12_signerr   rK   r   r   rM      rN   z&PKCS12Plugin.create_signer.<locals>._mrO   rR   r   rK   r   rS      rT   zPKCS12Plugin.create_signerN)r*   r+   r,   rU   rV   r   r?   rx   ry   r@   rE   r   r   r   rS   r   r   r   r   r      s    "c              
   C   s   |r5| j }|d u rtdz	t||}W n. ty4 } zd| }	tj|	|d t|	d }~ww |s<tdt|t	|| j
d}|jd urO|j}
n*|d ura|  d}
|  n|jrw|swtjddd}
|
svt  d }
nd }
t||
d	S )
Nz4The --p12-setup option requires a configuration filez#Error while reading PKCS#12 config rX   zCEither the PFX argument or the --p12-setup option must be provided.)pfx_filer\   r]   r^   zPKCS#12 passphrase: r_   )provided_pfx_passphrase)r   r?   rb   r   r'   r   r   rc   r   r   r]   pfx_passphrasere   rf   rg   rh   ri   rj   r
   r   )rW   rw   rm   ro   	p12_setuprp   rq   pkcs12_configrs   rt   ru   r   r   r   rz      sT   




rz   )#rP   rj   typingr   r   r   r?   pyhanko.cli._ctxr   pyhanko.cli._trustr   pyhanko.cli.configr   pyhanko.cli.plugin_apir   r	   pyhanko.cli.utilsr
   r   r   pyhanko.config.errorsr   pyhanko.config.local_keysr   r   pyhanko.sign.signers.pdf_cmsr   r   r   __all__r   r   rJ   r   rz   r   r   r   r   <module>   s(    >
63