U
    '1ew                     @   sd  d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ zddlZddlmZ W n ek
r   dZY nX dd ZdZdZejZdZdZ dZ!dZ"dZ#dZ$dZ%e&ddD ]Z'e%de'> B Z%qej(rda)dZ*dd Z+nej,Z+G dd dZ-G dd deZ.dZ/dd Z0d d! Z1ed"2d#d$ e/3 4 D Z5d%d& Z6dId'd(Z7d)d* Z8e5ddfd+d,Z9d-d. Z:dJd/d0Z;d1d2 Z<d3d4 Z=dKd6d7Z>G d8d9 d9e-eZ?dLd:d;Z@dMd<d=ZAdNd>d?ZBd@dA ZCdBdC ZDeEdDkr`dEdF ejFD ZGeGrFdGdF ejFD e_FeHeGZdHejFkrZe=  neD  dS )Oz3.3.0    N)hexlify	unhexlify)md5)BytesIO)asBytesint2ByterawBytesasNative)Canvas)	PDFObject)Flowable)	rl_config)sha256c                    s"    dkr|S t  fdd|D S )z9xor's each byte of the key with the number, which is <256r   c                 3   s   | ]} |A V  qd S N ).0knumr   </tmp/pip-unpacked-wheel-109iniqw/reportlab/lib/pdfencrypt.py	<genexpr>   s     zxorKey.<locals>.<genexpr>)bytes)r   keyr   r   r   xorKey   s     r                            s   ܫovp\CB~Eg8fSj'yGY-QL>iĴ'kpͪ| Ǘ;,\H}܅>|ŗTT!+E#Oj"ˌHBB~71.ש2}m3/WK;?#`W입#zO(zi!	|w,I_D`A}+r-%F(@\cL2(BMUGc                 C   s*   dd t | D }t|}t|  d a|S )Nc                 S   s   g | ]}t |t d   qS )   )_os_random_b_os_random_x)r   ir   r   r   
<listcomp>2   s     zos_urandom.<locals>.<listcomp>r"   )ranger   r$   )nbr   r   r   
os_urandom0   s    r*   c                   @   sL   e Zd ZdZdddZdd Zdd	 Zd
d ZdddZdd Z	dd Z
dS )StandardEncryptionr   Nr   c                 C   s   || _ |r|| _n|| _|dkr&tj}|dkr6d| _n<|dkrFd| _n,|dkrbtsZtdd| _ntd	t| || _|| _	|| _
|| _d | _ | _ | _ | _ | _ | _| _dS )
a*  
        This class defines the encryption properties to be used while creating a pdf document.
        Once initiated, a StandardEncryption object can be applied to a Canvas or a BaseDocTemplate.
        The userPassword parameter sets the user password on the encrypted pdf.
        The ownerPassword parameter sets the owner password on the encrypted pdf.
        The boolean flags canPrint, canModify, canCopy, canAnnotate determine wether a user can
        perform the corresponding actions on the pdf when only a user password has been supplied.
        If the user supplies the owner password while opening the pdf, all actions can be performed regardless
        of the flags.
        Note that the security provided by these encryption settings (and even more so for the flags) is very weak.
        N(   r         r"   Astrength==256 is not supported as package pyaes is not importable   Unknown encryption strength=%s)userPasswordownerPasswordr   encryptionStrengthrevisionpyaes
ValueErrorreprcanPrint	canModifycanCopycanAnnotateOUPr   OEUEPerms)selfr2   r3   r9   r:   r;   r<   strengthr   r   r   __init__<   s(    zStandardEncryption.__init__c                 C   s   | | _  | _ | _| _d S r   )r9   r:   r;   r<   )rC   valuer   r   r   setAllPermissions^   s    z$StandardEncryption.setAllPermissionsc                 C   sH   d}| j r|tB }| jr |tB }| jr.|tB }| jr<|tB }|tB }|S )Nr   )	r9   	printabler:   
modifiabler;   copypastabler<   annotatable
higherbits)rC   pr   r   r   permissionBitsc   s        z!StandardEncryption.permissionBitsc                 C   s:   | j std| jdkr tdt| j| j| j|| jdS )zencode a string, stream, textencryption not prepared!Nznot registered in PDF objectr5   )preparedr7   objnum	encodePDFr   versionr5   )rC   tr   r   r   encodek   s
    
zStandardEncryption.encodec                 C   s  t rtd| j  | jr td|r*|}n| }|j }trDd}t rptd| j	  td| j
  td|  t|  d | _trd| _t rtd	t| j  | jd
kr*d}td}td}ttd| _t rtdt|  tdt|  tdt| j  tt| j	d d | }| | | | _t rLtdt| j  tt| j	d d | }ttj| |d}	|	| j| _|  j|	 7  _t rtdt| j  td}
td}tt| j
d d |
 | j }| |
 | | _t rtdt| j  tt| j
d d | | j }ttj| |d}	|	| j| _|  j|	 7  _t rtdt| j  | jd@ | jd? d@ | jd? d@ | jd? d@ ddddtdtdtdtdddddg}ttj| j|d}	|	t|| _|  j|	 7  _t rtdt| j  n| jd krt | j	| j
| j| _t rbtd!t| j  t!| j	| j| j|| jd"| _t rtdt| j  t"| j| j|d#| _t rtd$t| j  d  | _#| _$d| _d S )%Nz-StandardEncryption.prepare(...) - revision %dzencryption already prepared!xxxxxxxxxxxxxxxxzuserPassword    = %rzownerPassword   = %rzinternalID      = %rl        izself.P          = %sr0   s                   r   r   zuvs      (hex)  = %szuks      (hex)  = %szself.key (hex)  = %s   zself.U (hex)  = %sivzself.UE (hex)  = %szself.O (hex)  = %szself.OE (hex)  = %s   r      Tadr)   r   zself.Perms (hex)  = %sr   r.   zself.O (as hex) = %srP   )r5   
documentIdzself.U (as hex) = %s)%DEBUGprintr5   rQ   r7   ZID	signaturedigest	CLOBBERIDr2   r3   intrN   r?   CLOBBERPERMISSIONSr8   r*   r   r   hexTextr   r>   r6   	EncrypterAESModeOfOperationCBCfeedrA   r=   r@   ordr   rB   computeOencryptionkeycomputeUrR   rT   )rC   document
overrideIDZ
internalIDZ
externalIDrZ   ZuvsZuksZmd	encrypterZovsZoksZpermsarrr   r   r   preparer   s     
   zStandardEncryption.preparec                 C   s   | j std|| _|| _d S )NrO   )rQ   r7   rR   rT   )rC   rR   rT   r   r   r   register   s    zStandardEncryption.registerc              	   C   s2   | j stdt| j| j| j| j| j| j| j	dS )NrO   r=   r@   r>   rA   r?   rB   r5   )
rQ   r7   StandardEncryptionDictionaryr=   r@   r>   rA   r?   rB   r5   rC   r   r   r   info   s    zStandardEncryption.info)Nr   r   r   r   N)N)__name__
__module____qualname__rQ   rE   rG   rN   rV   rt   ru   ry   r   r   r   r   r+   :   s   
"
|r+   c                   @   s    e Zd ZdZdd Zdd ZdS )rw   r   c                 C   s2   ||||||f\| _ | _| _| _| _| _|| _d S r   rv   )rC   r=   r@   r>   rA   r?   rB   r5   r   r   r   rE      s    (z%StandardEncryptionDictionary.__init__c           
      C   s0  ddl m}m}m} | }|dt| jt| j| jd}| jdkrd|d< d|d< d|d	< t| j|d
< t| j|d< t| j	|d< t| j
|d< t| j|d< |d|d< |d|d< d|d|dd}d||i}|||d< n6| jdkrd|d< d|d< d|d	< nd|d< d|d	< ||}	|	|S )Nr   )DummyDocPDFDictionaryPDFNameZStandard)Filterr=   r>   r?   r0   r"   LengthRVr=   r>   r@   rA   rB   ZStdCFZStrFZStmFr   ZDocOpenZAESV3)r   Z	AuthEventZCFMZCFr.   r-   r   r   )reportlab.pdfbase.pdfdocr}   r~   r   ri   r=   r>   r?   r5   r@   rA   rB   format)
rC   rq   r}   r~   r   dummydictZstdcfcfZpdfdictr   r   r   r      sD    
 
z#StandardEncryptionDictionary.formatN)rz   r{   r|   Z__RefOnly__rE   r   r   r   r   r   rw      s   rw   za
28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08
2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A
c                 C   s   dt tt|   S )z'a legitimate way to show strings in PDFz<%s>)r	   r   r   upper)textr   r   r   ri   -  s    ri   c                 C   s0   t | d dd t | d dd t| dd S )Nr   <zbad hex text>r   )equalityCheckr   )ri   r   r   r   	unHexText1  s    r    c                 c   s   | ]}t t|d V  qdS )r   N)chrrg   )r   cr   r   r   r   6  s     r   c                 C   sX   | d krTt j}|dkrd} n8|dkr*d} n*|dkrDts>tdd} ntdt| | S )	Nr,   r   r-   r.   r"   r/   r0   r1   )r   r4   r6   r7   r8   )r5   rD   r   r   r   checkRevision8  s    r   c              
   C   s  t |}t| t } | d d } |}d}tdD ]$}|d@ }|d? }|t|d 7 }q0tt| }	|	t| |	t| |	t| |	 }
|dkr|
d d }n.|d	krtd
D ]}t|
 }
q|
d d }trt	dt
dd | |||||fD   |S )Nr       r   r[   r   r"   r   r0   r.   2   r   z"encryptionkey(%s,%s,%s,%s,%s)==>%sc                 S   s   g | ]}t t|qS r   ri   strr   xr   r   r   r&   e  s     z!encryptionkey.<locals>.<listcomp>)r   r   	PadStringr'   r   r   updatere   rb   rc   tuple)passwordZOwnerKeyZPermissionsZFileId1r5   rM   ZpermissionsStringr%   bytehash	md5outputr   r   r   r   r   ro   G  s.     &ro   c                 C   s:  ddl m} |dks td| |s(| }t|t }|dd }t| t }|d d }t| }trtdt	tt	|t	|t	|t	||f  |dkr||d d 
|}nZ|d	krtd
D ]}	t| }q|d d }|}tdD ]}	t|	|}
||

|}qtr6tdtdd | |||fD   |S )Nr   ArcIVr`   zUnknown algorithm revision %sr   zEPadString=%s
ownerPad=%s
password=%s
userPad=%s
digest=%s
revision=%sr   r0   r.   r   r      zcomputeO(%s,%s,%s)==>%sc                 S   s   g | ]}t t|qS r   r   r   r   r   r   r&     s     zcomputeO.<locals>.<listcomp>)reportlab.lib.arcivr   AssertionErrorr   r   r   re   rb   rc   asciirV   r'   r   r   )r2   r3   r5   r   ZownerPadr   ZuserPadre   r=   r%   thisKeyr   r   r   rn   h  s0     ,

 "rn   c           
   	   C   s   t |}ddlm} |dkr,|| |}n|dkr|d k	sDtdtt}|t| |	 }|| |}t
ddD ]}t|| }	||	|}qzt|dk r|d	7 }q|}trtd
tdd | ||||fD   |S )Nr   r   r   r.   z+Revision 3 algorithm needs the document ID!r   r   r       zcomputeU(%s,%s,%s,%s)==>%sc                 S   s   g | ]}t t|qS r   r   r   r   r   r   r&     s     zcomputeU.<locals>.<listcomp>)r   r   r   rV   r   r   r   r   r   re   r'   r   lenrb   rc   r   )
ro   encodestringr5   ra   r   resulthtmpr(   r   r   r   r   rp     s&    

 $rp   c                 C   s6   t | |}|tkr2t|ttkr*tdtdd S )Nz&lengths don't match! (password failed)z;decode of U doesn't match fixed padstring (password failed))rp   r   r   r7   )ro   r>   decodedr   r   r   checkU  s
    
r   c              
   C   s  t |}|dkr| }|}tdD ]}|t|d@ 7 }|d? }q |}tdD ]}|t|d@ 7 }|d? }qJt| }|dkr|dd } n|dkr|} dd	lm}	 |	| |}
n|d
krXtd}t	
t	j| |d}t|}d}|dkrd|d  nd| }|dkrt|| }t|tr2|| d}n|t|7 }||| }
|
| 7 }
trtdtdd | |||||
fD   |
S )zEncodes a string or streamr`   r.   r[   r   r   N
   r   r   r0   r   rY   r   zutf-8zencodePDF(%s,%s,%s,%s,%s)==>%sc                 S   s   g | ]}t t|qS r   r   r   r   r   r   r&     s     zencodePDF.<locals>.<listcomp>)r   r'   r   r   re   r   r   rV   r*   r6   rj   rk   r   r   
isinstancer   r   rl   rb   rc   r   )r   ZobjectNumberZgenerationNumberstringr5   Znewkeyr(   r%   r   r   Z	encryptedrZ   rs   Z
string_lenpaddingZpadding_lenr   r   r   rS     sD    



 &rS   c                 C   s   | |kst d||| f d S )Nz%s
 expected=%s
 observed=%s)r   )Zobservedexpectedlabelr   r   r   r     s    r   c                  C   s   t dddd} | jd dd d}d}d	}tt| j|d
 tt| j|d tt| j|d t dddd} | jd dd d}d}d}tt| j|d tt| j|d tt| j|d d S )NZUserZOwnerr,   rD   rW   )rr   zB<FA7F558FACF8205D25A7F1ABFA02629F707AE7B0211A2BB26F5DF4C30F684301>zB<09F26CF46190AF8F93B304AD50C16B615DC43C228C9B2D2EA34951A80617B2B1>z<BB2C00EB3D>z40 bit O valuez40 bit U valuez40 bit key valueZuserpassZ	ownerpassr-   zB<68E5704AC779A5F0CD89704406587A52F25BF61CADC56A0F8DB6C4DB0052534D>zB<A9AE45CDE827FE0B7D6536267948836A00000000000000000000000000000000>z"<13DDE7585D9BE366C976DDD56AF541D1>z128 bit O valuez128 bit U valuez128 key value)r+   rt   r   ri   r=   r>   r   )encZ	expectedOZ	expectedUZexpectedKeyr   r   r   test  s     r   r,   c           	   	   C   s"   t |||||||d}|| j_dS )z2Applies encryption to the document being generatedr   N)r+   _docZencrypt)	Zcanvasr2   r3   r9   r:   r;   r<   rD   r   r   r   r   encryptCanvas  s       r   c                   @   s    e Zd ZdZdd Zdd ZdS )EncryptionFlowablezDrop this in your Platypus story and it will set up the encryption options.

    If you do it multiple times, the last one before saving will win.c                 C   s   dS )N)r   r   r   )rC   Z
availWidthZavailHeightr   r   r   wrap  s    zEncryptionFlowable.wrapc                 C   s&   t | j| j| j| j| j| j| j d S r   )r   canvr2   r3   r9   r:   r;   r<   rx   r   r   r   draw	  s    zEncryptionFlowable.drawN)rz   r{   r|   __doc__r   r   r   r   r   r   r     s   r   c                 C   s   t ddS )z*For use in Platypus.  Call before build().zNot implemented yetN)	Exception)dtr2   r3   r9   r:   r;   r<   rD   r   r   r   encryptDocTemplate  s    r   c              
   C   s   zddl m}m}	 W n tk
r0   tdY nX || ddd\}
}t|
 }|
d dd }t }t||d	}tr~d
|j	_
|	||}|D ],}||
| dd  || |  qt||||||||d |  | S )a  accepts a PDF file 'as a byte array in memory'; return encrypted one.

    This is a high level convenience and does not touch the hard disk in any way.
    If you are encrypting the same file over and over again, it's better to use
    pageCatcher and cache the results.r   )storeFormsInMemoryrestoreFormsInMemoryz~reportlab.lib.pdfencrypt.encryptPdfInMemory failed because rlextra cannot be imported.
See https://www.reportlab.com/downloadsr   )allZBBoxesZ
PageForms0r   N)Zpagesizez&[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]r   )Zrlextra.pageCatcher.pageCatcherr   r   ImportErrorlistkeysr   r
   rf   r   Z_IDZsetPageSizeZdoFormZshowPager   savegetvalue)inputPDFr2   r3   r9   r:   r;   r<   rD   r   r   ZbboxInfoZpickledFormsnamesZfirstPageSizebufr   Z	formNamesZformNamer   r   r   encryptPdfInMemory  s6    



    r   c	              
   C   s>   t | d }	t|	|||||||d}
t |d|
 t|
S )z>Creates encrypted file OUTPUTFILENAME.  Returns size in bytes.rbr   wb)openreadr   writer   )ZinputFileNameZoutputFileNamer2   r3   r9   r:   r;   r<   rD   r   Z	outputPDFr   r   r   encryptPdfOnDiskE  s        r   c                  C   s  t jd d  } d}dddddddd	d
dddddddddddddg}d}d}d}d}d}d}d}	| d }
t| dd  }t|dkr|d dks|d dkrt| d S t|dk rtd|d |kr|d }|dd  }tj|std| ntdd }d!D ].\}}|D ]}||kr|}|	| qqd|ksJd|krd|krd|
d}d}nd|kr||
d}d}zt||d  }W n   d}Y nX |	||d   |	| ndd"lm} dd#|d$fdd#|d$fdd%|d&fdd%|d&fdd'|d(fdd'|d(fdd)|d*fd	d)|d*fd
d+|d,fdd+|d,fdd-|d.fdd-|d.fdd/|	d0fdd/|	d0ff}d1}|D ]}|d |krd|
|d }|d |kr||d  d2krtd3|d  z||d  |krB|d |krt|d d4 t  nt|d d5 t  |r"td6|d7 ||d  f  |	||d   |	|d  W n   d8|d7  Y nX qd|d9krtd:| td;| td<|	 td=| td>| td?| td@| tdA| tdB| td<|	 tdC| |	dkr"|dDd  dEks|dDd  dFkr|d dD }n|}|dG }	t||	|||||||dH	}|rVtdI|	|||f  t|dkrtdJt|ddK |f nt| d S )LNan  PDFENCRYPT USAGE:

PdfEncrypt encrypts your PDF files.

Line mode usage:

% pdfencrypt.exe pdffile [-o ownerpassword] | [owner ownerpassword],
	[-u userpassword] | [user userpassword],
	[-p 1|0] | [printable 1|0],
	[-m 1|0] | [modifiable 1|0],
	[-c 1|0] | [copypastable 1|0],
	[-a 1|0] | [annotatable 1|0],
	[-s savefilename] | [savefile savefilename],
	[-v 1|0] | [verbose 1|0],
	[-e128], [encrypt128],
	[-h] | [help]

-o or owner set the owner password.
-u or user set the user password.
-p or printable set the printable attribute (must be 1 or 0).
-m or modifiable sets the modifiable attribute (must be 1 or 0).
-c or copypastable sets the copypastable attribute (must be 1 or 0).
-a or annotatable sets the annotatable attribute (must be 1 or 0).
-s or savefile sets the name for the output PDF file
-v or verbose prints useful output to the screen.
      (this defaults to 'pdffile_encrypted.pdf').
'-e128' or 'encrypt128' allows you to use 128 bit encryption (in beta).
'-e256' or 'encrypt256' allows you to use 256 bit encryption (in beta AES).

-h or help prints this message.

See PdfEncryptIntro.pdf for more information.
z-oownerz-uuser-prH   -mrI   -crJ   -arK   z-sZsavefilez-vverbosez-hhelp-e128
encrypt128-e256Z
encryptAESr   r   zencrypted.pdfr   r   z2Must include a filename and one or more arguments!zCan't open input file '%s'!z2First argument must be name of the PDF input file!r,   ))r-   )r   r   )r"   )r   Z
encrypt256)r   OWNERzOwner passwordUSERzUser password	PRINTABLEz'Printable'
MODIFIABLEz'Modifiable'COPYPASTABLEz'Copypastable'ANNOTATABLEz'Annotatable'SAVEFILEzOutput file)r   rH   r   rI   rJ   r   rK   r   )10z#%s value must be either '1' or '0'!z = int(argv[pos+1])z = argv[pos+1]z%s set to: '%s'.r.   zUnable to set %s.r   z
infile:z	STRENGTH:z	SAVEFILE:zUSER:zOWNER:z
PRINTABLE:zMODIFIABLE:zCOPYPASTABLE:zANNOTATABLE:zVERBOSE:z.pdfz.PDFz_encrypted.pdfr   zQwrote output file '%s'(%s bytes)
  owner password is '%s'
  user password is '%s'z4
Unrecognised arguments : %s
known arguments are:
%sr   )sysargvr   r   rc   r7   ospathisfileremoveindexrg   Zreportlab.rl_configr   execvarsr   r   )Zsys_argvusageZknown_modesr   r   r   r   r   r   r   Zcallerr   infileZSTRENGTHsZ_ar^   posargr   ZarglistZbinaryrequiredZthisargZtinfilefilesizer   r   r   scriptInterpT  s    "          
































$
   r   c                   C   s
   t   d S r   )r   r   r   r   r   main  s    r   __main__c                 C   s    g | ]}|d d dkr|qS N   z--debugr   r   r   r   r   r&     s      r&   c                 C   s    g | ]}|d d dkr|qS r   r   r   r   r   r   r&     s      z--test)N)N)Nr   r   r   r   r,   )Nr   r   r   r   r,   )Nr   r   r   r   r,   )Nr   r   r   r   r,   )I__version__r   r   binasciir   r   hashlibr   ior   Zreportlab.lib.utilsr   r   r   r	   Zreportlab.pdfgen.canvasr
   r   r   Zreportlab.platypus.flowablesr   Z	reportlabr   r6   r   r   r   rf   rh   debugrb   Z	reserved1Z	reserved2rH   rI   rJ   rK   rL   r'   r%   Z	invariantr$   r#   r*   urandomr+   rw   r   ri   r   joinstripsplitr   r   ro   rn   rp   r   rS   r   r   r   r   r   r   r   r   r   rz   r   r^   r   r   r   r   r   <module>   s   

 A. 
!
.          
         
	         
*         
 ,
 