U
    Nf!                     @   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edddZ G dd deZ!ed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ed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)Z
raw_configgetpemder_setupspkcs12_setups)selfr   Zconfig_dict r   G/tmp/pip-unpacked-wheel-fbovo6ns/pyhanko/cli/commands/signing/simple.py__init__   s    zKeyFileConfigWrapper.__init__c                 C   s>   z| j | }W n$ tk
r2   td| dY nX t|S )Nz There's no PKCS#12 setup named '')r   KeyErrorr   r   from_configr   namesetupr   r   r   get_pkcs12_config    s
    z&KeyFileConfigWrapper.get_pkcs12_configc                 C   s>   z| j | }W n$ tk
r2   td| dY nX t|S )Nz There's no PEM/DER setup named 'r   )r   r   r   r   r    r!   r   r   r   get_pemder_config'   s
    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eej dddZe	e
e dddZd	S )
r   Z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.r,   multipler+   )z--pemder-setupzCname of preconfigured PEM/DER profile (overrides all other options)r,   r-   r+   z
--passfilez2file containing the passphrase for the private keyrstdinr+   r-   r,   show_defaultz	--no-passz*assume the private key file is unencryptedr+   r,   Zis_flagdefaultr6   )clickOptionr   strFileboolr   r   r   r   click_options3   sP    	zPemderPlugin.click_optionscontextr*   c                    s   t j fdd}| S )Nc                   3   s   t  fV  d S N)_pemder_signerr   rB   kwargsr   r   _mf   s    z&PemderPlugin.create_signer.<locals>._m
contextlibcontextmanagerr   rB   rF   rG   r   rE   r   create_signerc   s    zPemderPlugin.create_signerN)r&   r'   r(   subcommand_namehelp_summaryr   r:   r;   r@   r   r   r   rL   r   r   r   r   r   /   s   1)ctxc              
   C   s  |rv| j }|d krtdzt||}W q tk
rr }	 z&d| }
tj|
|	d t|
W 5 d }	~	X Y qX n*|r~|stdnt||t	|| j
d}|jd k	r|j}nZ|d k	r|  d}|  n6|jr|stjddd}|st  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)Zprovided_key_passphrase)r   r:   ClickExceptionr   r%   r   r   errorr   r   rU   Zkey_passphrasereadlinestripencodecloseprompt_passphrasegetpassr
   r   )rO   keycertchainZpemder_setuppassfileno_pass
cli_configZpemder_configemsg
passphraser   r   r   rD   m   sN    	


 rD   c                   @   sP   e Zd ZdZdZeej dddZeej	 dddZ
eee dd	d
ZdS )r   Zpkcs12z$read key material from PKCS#12 filesr)   c                 C   s   t jdtddgS )N)pfxF)r,   r-   )r:   Argumentr   r?   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)r1   r.   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.r/   r2   z4file containing the passphrase for the PKCS#12 file.r3   r4   r5   r7   z&assume the PKCS#12 file is unencryptedr8   )r:   r;   r<   r   r=   r>   r?   r   r   r   r@      s8    zPKCS12Plugin.click_optionsrA   c                    s   t j fdd}| S )Nc                   3   s   t  fV  d S rC   )_pkcs12_signerr   rE   r   r   rG      s    z&PKCS12Plugin.create_signer.<locals>._mrH   rK   r   rE   r   rL      s    zPKCS12Plugin.create_signerN)r&   r'   r(   rM   rN   r   r:   rk   rl   r;   r@   r   r   r   rL   r   r   r   r   r      s   #c              
   C   s  |rv| j }|d krtdzt||}W q tk
rr } z&d| }	tj|	|d t|	W 5 d }~X Y qX n$|stdnt|t	|| j
d}|jd k	r|j}
nT|d k	r|  d}
|  n0|jr|stjddd}
|
st  d }
nd }
t||
d	S )
Nz4The --p12-setup option requires a configuration filez#Error while reading PKCS#12 config rP   zCEither the PFX argument or the --p12-setup option must be provided.)Zpfx_filerT   rU   rV   zPKCS#12 passphrase: rW   )Zprovided_pfx_passphrase)r   r:   rY   r   r$   r   r   rZ   r   r   rU   Zpfx_passphraser[   r\   r]   r^   r_   r`   r
   r   )rO   rj   rc   rd   Z	p12_setupre   rf   Zpkcs12_configrg   rh   ri   r   r   r   rm      sP    



 rm   )#rI   r`   typingr   r   r   r:   Zpyhanko.cli._ctxr   Zpyhanko.cli._trustr   Zpyhanko.cli.configr   Zpyhanko.cli.plugin_apir   r	   Zpyhanko.cli.utilsr
   r   r   Zpyhanko.config.errorsr   Zpyhanko.config.local_keysr   r   Zpyhanko.sign.signers.pdf_cmsr   r   r   __all__r   r   rD   r   rm   r   r   r   r   <module>   s$   ?63