U
    <g
                     @   s  d dl Z d dl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 d dlmZ d dlmZ d d	lmZ d d
lmZ ddgZejdddejdedejdejddddejdddedejddddejdddddd Zejddddd Zejd d!deddd"Zejd#d$dejdedejdejddddejdd%deded&d' Z ejd(d)dejdedejdejddddejd*edd+d,ejd-d.edd/ejd0de!d1d2d3d4ejd5d6edddd7ed8d9 Z"ej#d:d;d<Z$ejd=d>dejdedejdejddddejd?edejd0de!d@dAd3d4edBdC Z%dS )D    N)cli_root)pyhanko_exception_manager)_warn_empty_passphrasereadable_file)load_certs_from_pemder)crypt)StandardSecurityHandler)PubKeyPermissions)PdfFileReader)copy_into_new_writerdecryptencrypt_filez encrypt PDF files (AES-256 only)encrypt)helpnameinfile)typeoutfileTF)writabledir_okayz
--passwordz!password to encrypt the file with)r   requiredr   z--recipientzIcertificate(s) corresponding to entities that can decrypt the output file)readabler   )r   multipler   r   c           	      C   s   |r|rt dn|s(|s(tjdd}d }|r>tt|d}t h t| dR}t|}t|}|rr|	| n|j
|d t|d}|| W 5 Q R X W 5 Q R X W 5 Q R X d S )Nz2Specify either a password or a list of recipients.zOutput file password: prompt)Z
cert_filesrb)Z
owner_passwb)clickClickExceptiongetpasslistr   r   openr
   r   Zencrypt_pubkeyr   write)	r   r   passwordZ	recipientZrecipient_certsinfrwoutf r(   >/tmp/pip-unpacked-wheel-w101_d3s/pyhanko/cli/commands/crypt.pyr      s$    z6decrypt PDF files (any standard PDF encryption scheme)c                   C   s   d S )Nr(   r(   r(   r(   r)   r   =   s    z--forcez1ignore access restrictions (use at your own risk))r   r   r   is_flagdefaultzdecrypt using passwordr#   z!password to decrypt the file withc           	      C   s   t   t| d}t|}|jd kr2tdnt|jtsHtd|sXtjdd}|	|}|j
tjjkr|stdn|j
tjjkrtdt|}t|d}|| W 5 Q R X W 5 Q R X W 5 Q R X d S )	Nr   File is not encrypted.zIFile is not encrypted with the standard (password-based) security handlerzFile password: r   zjPassword specified was the user password, not the owner password. Pass --force to decrypt the file anyway.zPassword didn't match.r   )r   r!   r
   security_handlerr   r   
isinstancer   r   r   statusr   
AuthStatusUSERFAILEDr   r"   )	r   r   r#   forcer$   r%   auth_resultr&   r'   r(   r(   r)   decrypt_with_passwordO   s*    


r5   z#decrypt using private key (PEM/DER)Zpemderz--keyz5file containing the recipient's private key (PEM/DER))r   r   r   z--certz5file containing the recipient's certificate (PEM/DER))r   r   r   z
--passfiler   z2file containing the passphrase for the private keystdin)r   r   r   show_defaultz	--no-passz*assume the private key file is unencrypted)r   r   r*   r+   r7   c           	      C   sh   |d k	r|  }|  n*|s@tjddd}|sDt  d }nd }tjj|||d}t|| || d S )NKey passphrase: r   utf-8)Zkey_passphrase)	readcloser   encoder   r   SimpleEnvelopeKeyDecrypterload_decrypt_pubkey)	r   r   keycertpassfiler3   Zno_pass
passphrasesedkr(   r(   r)   decrypt_with_pemders   s     
  rE   )rD   c           	      C   s   t   t|d}t|}|jd kr0tdt|jtjsHtd|	| }|j
tjjkr|s|jrtj|jkrtdn|j
tjjkrtdt|}t|d}|| W 5 Q R X W 5 Q R X W 5 Q R X d S )Nr   r,   z:File was not encrypted with a public-key security handler.zhChange of encryption is typically not allowed with user access. Pass --force to decrypt the file anyway.zFailed to decrypt the file.r   )r   r!   r
   r-   r   r   r.   r   ZPubKeySecurityHandlerZdecrypt_pubkeyr/   r0   r1   Zpermission_flagsr	   ZALLOW_ENCRYPTION_CHANGEr2   r   r"   )	rD   r   r   r3   r$   r%   r4   r&   r'   r(   r(   r)   r?      s6    



r?   z#decrypt using private key (PKCS#12)pkcs12pfxr%   z3file containing the passphrase for the PKCS#12 filec                 C   sX   |d krt j ddd}n|  d}|  tjj||d}t|| || d S )Nr8   r   r9   )rC   )	r   r<   readlinestripr;   r   r=   load_pkcs12r?   )r   r   rG   rB   r3   rC   rD   r(   r(   r)   decrypt_with_pkcs12   s     rK   )&r   r   Zpyhanko.cli._rootr   Zpyhanko.cli.runtimer   Zpyhanko.cli.utilsr   r   Zpyhanko.keysr   Zpyhanko.pdf_utilsr   Zpyhanko.pdf_utils.cryptr   Z#pyhanko.pdf_utils.crypt.permissionsr	   Zpyhanko.pdf_utils.readerr
   Zpyhanko.pdf_utils.writerr   __all__commandargumentPathoptionstrr   groupr   boolZdecrypt_force_flagr5   ZFilerE   r=   r?   rK   r(   r(   r(   r)   <module>   s   

!