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 d dlmZmZmZ d d	lmZ d
gZzd dlZdZW n ek
r   dZdZY nX dZdZG dd
 d
eZedddZG dd dZ dS )    N)ContextManagerListOptional)
CLIContext)	CLIConfig)SigningCommandPlugin)loggerreadable_file)ConfigurationError)PKCS11PinEntryModePKCS11SignatureConfigTokenCriteria)SignerPKCS11PluginTFZPYHANKO_PKCS11_PINz7This subcommand requires python-pkcs11 to be installed.c                   @   sN   e Zd ZdZdZeZedddZe	e
j dddZeee dd	d
ZdS )r   pkcs11z"use generic PKCS#11 device to sign)returnc                 C   s   t S N)pkcs11_availableself r   K/tmp/pip-unpacked-wheel-fbovo6ns/pyhanko/cli/commands/signing/pkcs11_cli.pyis_available(   s    zPKCS11Plugin.is_availablec                 C   s   t jddtddt jddtddt jddtddt jd	d
tdddt jddtddt jdddtd dt jdtddddddt jdtdddt jdtddddg	S )N)z--libzpath to PKCS#11 moduleF)helptyperequired)z--token-labelzPKCS#11 token label)z--cert-labelzcertificate label)z--raw-mechanismzinvoke raw PKCS#11 mechanismT)r   r   is_flagr   )z--key-labelz	key label)z	--slot-nozspecify PKCS#11 slot to use)r   r   r   default)z--skip-user-pinz7do not prompt for PIN (e.g. if the token has a PIN pad))r   Zshow_defaultr   r   r   r   )z--p11-setupzCname of preconfigured PKCS#11 profile (overrides all other options))r   r   r   )z--other-certz3label of other cert to pull (multiple uses allowed))r   r   r   multiple)clickOptionr	   strboolintr   r   r   r   click_options+   sz       	zPKCS11Plugin.click_options)contextr   c                 K   s   t |f|S r   )_pkcs11_signer_context)r   r%   kwargsr   r   r   create_signeri   s    zPKCS11Plugin.create_signerN)__name__
__module____qualname__subcommand_nameZhelp_summaryUNAVAIL_MSGZunavailable_messager"   r   r   r   r    r$   r   r   r   r(   r   r   r   r   r   #   s   ?)ctxc
              
   C   s  ddl m}
 |r| j}|d kr(tdzt||}W q tk
r~ } z&d| }tj	||d t|W 5 d }~X Y qX n>|r|std|rt
jnt
j}t||||t||||	d}|j}|d krtjtd }|r| }|jt
jkr|d krtjdd	}|
j||d
S )Nr   )r   z4The --p11-setup option requires a configuration filez#Error while reading PKCS#11 config )exc_infoz3The parameters --lib and --cert-label are required.)module_path
cert_label	key_labelslot_noZtoken_criteria
prompt_pinraw_mechanismZother_certs_to_pullzPKCS#11 user PIN: )prompt)user_pin)pyhanko.signr   configr   ZClickExceptionModuleConfigWrapperget_pkcs11_configr
   r   errorr   ZSKIPZPROMPTr   r   r7   osenvirongetP11_PIN_ENV_VARstripr4   getpassZPKCS11SigningContext)r.   libZtoken_labelr1   r2   r3   Zskip_user_pinZ	p11_setupr5   Z
other_certr   Z
cli_configZpkcs11_configemsgZpinentry_modeZpinZpin_envr   r   r   r&   o   sR    
r&   c                   @   s"   e Zd ZedddZdd ZdS )r:   )r9   c                 C   s   |j }|di | _d S )Nzpkcs11-setups)Z
raw_configr?   pkcs11_setups)r   r9   Zconfig_dictr   r   r   __init__   s    zModuleConfigWrapper.__init__c                 C   s>   z| j | }W n$ tk
r2   td| dY nX t|S )Nz There's no PKCS#11 setup named '')rF   KeyErrorr
   r   Zfrom_config)r   namesetupr   r   r   r;      s
    z%ModuleConfigWrapper.get_pkcs11_configN)r)   r*   r+   r   rG   r;   r   r   r   r   r:      s   r:   )!rB   r=   typingr   r   r   r   Zpyhanko.cli._ctxr   Zpyhanko.cli.configr   Zpyhanko.cli.plugin_apir   Zpyhanko.cli.utilsr   r	   Zpyhanko.config.errorsr
   Zpyhanko.config.pkcs11r   r   r   r8   r   __all__r   r   ImportErrorr@   r-   r   r&   r:   r   r   r   r   <module>   s.   
MA