
    g!                        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      Z G d de      ZdefdZ  G d de      Z!defdZ"y)    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                   $    e Zd ZdefdZd Zd Zy)KeyFileConfigWrapperconfigc                 x    |j                   }|j                  di       | _        |j                  di       | _        y )Nzpemder-setupszpkcs12-setups)
raw_configgetpemder_setupspkcs12_setups)selfr   config_dicts      [/var/www/html/Beni/venv/lib/python3.12/site-packages/pyhanko/cli/commands/signing/simple.py__init__zKeyFileConfigWrapper.__init__   s3    ''(___bA(___bA    c                     	 | j                   |   }t        j                  |      S # t        $ r t        d| d      w xY w)Nz There's no PKCS#12 setup named '')r   KeyErrorr   r   from_configr   namesetups      r    get_pkcs12_configz&KeyFileConfigWrapper.get_pkcs12_config    S    	Q&&t,E %0077  	Q$'GvQ%OPP	Q   & ?c                     	 | j                   |   }t        j                  |      S # t        $ r t        d| d      w xY w)Nz There's no PEM/DER setup named 'r$   )r   r%   r   r   r&   r'   s      r    get_pemder_configz&KeyFileConfigWrapper.get_pemder_config'   r+   r,   N)__name__
__module____qualname__r   r!   r*   r.    r"   r    r   r      s    By B
88r"   r   c                   P    e Zd ZdZdZdeej                     fdZde	de
e   fdZy)r   pemderz$read key material from PEM/DER filesreturnc                 z   t        j                  ddt        d      t        j                  ddt        d      t        j                  dt        dd	
      t        j                  dt        dd      t        j                  dddt        j                  d      d      t        j                  ddt
        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.r8   multipler7   )z--pemder-setupzCname of preconfigured PEM/DER profile (overrides all other options)r8   r9   r7   z
--passfilez2file containing the passphrase for the private keyrstdinr7   r9   r8   show_defaultz	--no-passz*assume the private key file is unencryptedr7   r8   is_flagdefaultrB   )clickOptionr   strFileboolr   s    r    click_optionszPemderPlugin.click_options3   s    LL@"	 LLM"	 LL")	 LL#!	 LLLZZ_$ LLA!K-
 -	
r"   contextc                 F    t         j                  fd       } |       S )Nc               3   *   K   t         fi  y wN)_pemder_signerrN   kwargss   r    _mz&PemderPlugin.create_signer.<locals>._mf         3F33   
contextlibcontextmanagerr   rN   rT   rU   s    `` r    create_signerzPemderPlugin.create_signerc   &     
	"	"	4 
#	4 tr"   N)r/   r0   r1   subcommand_namehelp_summaryr   rG   rH   rM   r   r   r   r\   r2   r"   r    r   r   /   s<    O9L.
tELL1 .
`!		r"   ctxc                    |r?| j                   }|t        j                  d      	 t        |      j	                  |      }n;|r|st        j                  d      t        ||t        |      | j                        }|j                  |j                  }n|>|j                         j                         j                  d      }|j                          nD|j                   r6|s4t#        j"                  d      j                  d      }|st%                d }nd }t'        ||	      S # t
        $ r6}	d| }
t        j                  |
|	       t        j                  |
      d }	~	ww xY w)
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   rG   ClickExceptionr   r.   r   r   errorr   r   rg   key_passphrasereadlinestripencodecloseprompt_passphrasegetpassr   r   )r`   keycertchainpemder_setuppassfileno_pass
cli_configpemder_configemsg
passphrases               r    rR   rR   m   s[    ZZ
&&I 	,0<NNM d""'
 	

 ."5)~~	
 ##/"11
		&&(..077@
		(	(__,>?FFwO
"$J
$z = " 	,6|nECLLq)&&s++	,s   D' '	E&01E!!E&c                   v    e Zd ZdZdZdeej                     fdZdeej                     fdZ
dedee   fdZy)	r   pkcs12z$read key material from PKCS#12 filesr5   c                 <    t        j                  dt        d      gS )N)pfxF)r8   r9   )rG   Argumentr   rL   s    r    click_extra_argumentsz"PKCS12Plugin.click_extra_arguments   s    xmeLMMr"   c                 
   t        j                  dt        dd      t        j                  dt        dd      t        j                  d	d
dt        j                  d      d      t        j                  ddt
        ddd      gS )N)z--p12-setupFzCname of preconfigured PKCS#12 profile (overrides all other options)r=   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;   r>   z4file containing the passphrase for the PKCS#12 file.r?   r@   rA   rC   z&assume the PKCS#12 file is unencryptedrD   )rG   rH   rI   r   rJ   rK   rL   s    r    rM   zPKCS12Plugin.click_options   s    LL !	 LL"0	 LLKZZ_$ LL=!/
 	
r"   rN   c                 F    t         j                  fd       } |       S )Nc               3   *   K   t         fi  y wrQ   )_pkcs12_signerrS   s   r    rU   z&PKCS12Plugin.create_signer.<locals>._m   rV   rW   rX   r[   s    `` r    r\   zPKCS12Plugin.create_signer   r]   r"   N)r/   r0   r1   r^   r_   r   rG   r   r   rH   rM   r   r   r   r\   r2   r"   r    r   r      sS    O9LNtENN'; N 
tELL1  
D!		r"   c                    |r?| j                   }|t        j                  d      	 t        |      j	                  |      }n8|st        j                  d      t        |t        |      | j                        }|j                  |j                  }
n|>|j                         j                         j                  d      }
|j                          nD|j                   r6|s4t#        j"                  d      j                  d      }
|
st%                d }
nd }
t'        ||
	      S # t
        $ r6}d| }	t        j                  |	|       t        j                  |	      d }~ww xY w)
Nz4The --p12-setup option requires a configuration filez#Error while reading PKCS#12 config rb   zCEither the PFX argument or the --p12-setup option must be provided.)pfx_filerf   rg   rh   zPKCS#12 passphrase: ri   )provided_pfx_passphrase)r   rG   rl   r   r*   r   r   rm   r   r   rg   pfx_passphrasero   rp   rq   rr   rs   rt   r   r   )r`   r   rw   ry   	p12_setuprz   r{   pkcs12_configr}   r~   r   s              r    r   r      s[    *-**
&&F 	,0<NNM "" 
 	

 ."5)~~
 ##/"11
		&&(..077@
		(	(__,BCJJ

 "$J
!z ? " 	,7	{CCLLq)&&s++	,s   D$ $	E#-1EE#)#rY   rt   typingr   r   r   rG   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   rR   r   r   r2   r"   r    <module>r      s      1 1  ' ) ( P K K 4  >
*8 8*;' ;|3	3l0' 0f/
 /r"   