o
    >e                     @   s  d dl Z 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
dgZzd dlZdZW n eyc   dZdZY nw dZdZG dd
 d
eZde	fddZ G dd deZ!G dd dZ"de	fddZ#dS )    N)ContextManagerListOptional)
CLIContext)	CLIConfig)SigningCommandPlugin)loggerreadable_file)ConfigurationError)PKCS11PinEntryModePKCS11SignatureConfigTokenCriteria)SignerPKCS11Plugin
BEIDPluginTFPYHANKO_PKCS11_PINz7This subcommand requires python-pkcs11 to be installed.c                   @   P   e Zd ZdZdZeZdefddZde	e
j fddZdedee fd	d
ZdS )r   pkcs11z"use generic PKCS#11 device to signreturnc                 C      t S Npkcs11_availableself r   h/var/www/html/humari/django-venv/lib/python3.10/site-packages/pyhanko/cli/commands/signing/pkcs11_cli.pyis_available)      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-no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"   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,   st   	zPKCS11Plugin.click_optionscontextc                 K      t |fi |S r   )_pkcs11_signer_contextr   r1   kwargsr   r   r   create_signerj      zPKCS11Plugin.create_signerN__name__
__module____qualname__subcommand_namehelp_summaryUNAVAIL_MSGunavailable_messager.   r   r   r+   r,   r0   r   r   r   r6   r   r   r   r   r   $   s    >ctxc
              
   C   s
  ddl m}
 |r;| j}|d u rtdz	t||}W n< ty: } zd| }tj	||d t|d }~ww |r?|sDtd|rIt
jnt
j}t||||t||||	d}|j}|d u rntjtd }|rn| }|jt
jkr~|d u 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_notoken_criteria
prompt_pinraw_mechanismother_certs_to_pullzPKCS#11 user PIN: )prompt)user_pin)pyhanko.signr   configr+   ClickExceptionModuleConfigWrapperget_pkcs11_configr
   r   errorr   SKIPPROMPTr   r   rL   osenvirongetP11_PIN_ENV_VARstriprH   getpassPKCS11SigningContext)r@   libtoken_labelrD   rE   rF   skip_user_pin	p11_setuprI   
other_certr   
cli_configpkcs11_configemsgpinentry_modepinpin_envr   r   r   r3   p   sV   

r3   c                   @   r   )r   beidzuse Belgian eID to signr   c                 C   r   r   r   r   r   r   r   r      r   zBEIDPlugin.is_availablec                 C   s&   t jddtddt jdddtd dgS )Nr   z"path to libbeidpkcs11 library fileFr    r%   r&   r'   )r+   r,   r	   r/   r   r   r   r   r0      s   zBEIDPlugin.click_optionsr1   c                 K   r2   r   )_beid_signer_contextr4   r   r   r   r6      r7   zBEIDPlugin.create_signerNr8   r   r   r   r   r      s    c                   @   s"   e Zd ZdefddZdd ZdS )rP   rN   c                 C   s&   |j }|dd | _|di | _d S )Nzbeid-module-pathzpkcs11-setups)
raw_configrW   beid_module_pathpkcs11_setups)r   rN   config_dictr   r   r   __init__   s   zModuleConfigWrapper.__init__c                 C   s8   z| j | }W n ty   td| dw t|S )Nz There's no PKCS#11 setup named '')rl   KeyErrorr
   r   from_config)r   namesetupr   r   r   rQ      s   
z%ModuleConfigWrapper.get_pkcs11_configN)r9   r:   r;   r   rn   rQ   r   r   r   r   rP      s    rP   c                    sn   dd l ddlm  |s&| j}d }|d urt|j}|d u r#td|n|tj	 fdd}| S )Nr   )rh   z\The --lib option is mandatory unless beid-module-path is provided in the configuration file.c               
   3   s    z	 j d} W n" jy, } ztjd|d tdt|j d| d }~ww |   | V  W d    d S 1 sAw   Y  d S )N)rF   zPKCS#11 errorrA   zPKCS#11 error: [z] )	open_beid_sessionPKCS11Errorr   rR   r+   rO   r"   r9   
BEIDSigner)sessionrc   rh   rC   r   rF   r   r   manager   s   "z%_beid_signer_context.<locals>.manager)
r   rM   rh   rN   rP   rk   r+   rO   
contextlibcontextmanager)r@   r\   rF   ra   rk   ry   r   rx   r   ri      s    
ri   )$rz   rZ   rU   typingr   r   r   r+   pyhanko.cli._ctxr   pyhanko.cli.configr   pyhanko.cli.plugin_apir   pyhanko.cli.utilsr   r	   pyhanko.config.errorsr
   pyhanko.config.pkcs11r   r   r   rM   r   __all__r   r   ImportErrorrX   r>   r   r3   r   rP   ri   r   r   r   r   <module>   s:    L
A