Warning: session_start(): Session cannot be started after headers have already been sent in /home/tvrreohg/public_html/manga.php on line 13
3 1øªj¿ã@s0ddlZddlZddlZddlmZmZddlmZmZm Z m Z m Z m Z ddl mZmZmZmZmZmZmZmZmZmZddlmZddlmZmZmZmZmZm Z m!Z!m"Z"m#Z#Gdd „d ƒZ$d Z%e&e&d œd d „Z'Gdd„dƒZ(Gdd„dƒZ)Gdd„deƒZ*Gdd„de+eƒZ,Gdd„dƒZ-Gdd„dƒZ.dS)éN)ÚEnumÚIntEnum)ÚAnyÚDictÚOptionalÚTupleÚUnionÚcast) ÚCryptAESÚ CryptBaseÚ CryptIdentityÚCryptRC4Úaes_cbc_decryptÚaes_cbc_encryptÚaes_ecb_decryptÚaes_ecb_encryptÚ rc4_decryptÚ rc4_encrypté)Úlogger_warning) Ú ArrayObjectÚByteStringObjectÚDictionaryObjectÚ NameObjectÚ NumberObjectÚ PdfObjectÚ StreamObjectÚTextStringObjectÚcreate_string_objectc@s@eZdZeeeddœdd„Zeedœdd„Zeedœdd „ZdS) Ú CryptFilterN)Ú stm_cryptÚ str_cryptÚef_cryptÚreturncCs||_||_||_dS)N)r r!r")Úselfr r!r"©r%ú2/tmp/pip-build-_d5lkt3n/pypdf/pypdf/_encryption.pyÚ__init__=szCryptFilter.__init__)Úobjr#csút|tƒr ˆjj|jƒ}t|ƒ}t|tƒrDˆjj|jƒƒ}t|ƒ}n²t|tƒrœtƒ}|j|ƒ|j ˆj j|j ƒƒx"|j ƒD]\}}ˆj |ƒ||<q|W|}nZt|tƒrÖtƒ}x"|j ƒD]\}}ˆj |ƒ||<q¶W|}n t|tƒröt‡fdd„|Dƒƒ}|S)Nc3s|]}ˆj|ƒVqdS)N)Úencrypt_object)Ú.0Úx)r$r%r&ú [sz-CryptFilter.encrypt_object..)Ú isinstancerr!ZencryptÚoriginal_bytesrZget_encoded_bytesrÚupdateÚset_datar Ú_dataÚitemsr)rr)r$r(ÚdataÚobj2ÚkeyÚvaluer%)r$r&r)Gs*       zCryptFilter.encrypt_objectcCsÌt|ttfƒr&|jj|jƒ}t|ƒ}n¢t|tƒrf|jj|j ƒ|_ x†|j ƒD]\}}|j |ƒ||<qJWnbt|t ƒr–xV|j ƒD]\}}|j |ƒ||<qzWn2t|t ƒrÈx&tt|ƒƒD]}|j ||ƒ||<q®W|S)N)r-rrr!Zdecryptr.rrr r1r2Údecrypt_objectrrÚrangeÚlen)r$r(r3r5r6Úir%r%r&r7^s    zCryptFilter.decrypt_object)Ú__name__Ú __module__Ú __qualname__r r'rr)r7r%r%r%r&r<s rs (¿N^NuŠAdNVÿú..¶Ðh>€/ ©þdSiz)r3r#cCs|tdd…S)Né )Ú_PADDING)r3r%r%r&Ú_paddingusr@c @s¸eZdZeeeeeeeeedœdd„ƒZeeeeedœdd„ƒZeeeeedœdd „ƒZ eeeeed œd d „ƒZ eeeeeeeeeed œ dd„ƒZ eeeeeeeeeedœ dd„ƒZ dS)ÚAlgV4)ÚpasswordÚrevÚkey_sizeÚo_entryÚPÚ id1_entryÚmetadata_encryptedr#c Csœt|ƒ}tj|ƒ}|j|ƒ|jtjd|ƒƒ|j|ƒ|dkrP| rP|jdƒ|jƒ} |d} |dkr�x&tdƒD]} tj| d| …ƒjƒ} qrW| d| …S)uÝ Algorithm 2: Computing an encryption key. a) Pad or truncate the password string to exactly 32 bytes. If the password string is more than 32 bytes long, use only its first 32 bytes; if it is less than 32 bytes long, pad it by appending the required number of additional bytes from the beginning of the following padding string: < 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 > That is, if the password string is n bytes long, append the first 32 - n bytes of the padding string to the end of the password string. If the password string is empty (zero-length), meaning there is no user password, substitute the entire padding string in its place. b) Initialize the MD5 hash function and pass the result of step (a) as input to this function. c) Pass the value of the encryption dictionary’s O entry to the MD5 hash function. ("Algorithm 3: Computing the encryption dictionary’s O (owner password) value" shows how the O value is computed.) d) Convert the integer value of the P entry to a 32-bit unsigned binary number and pass these bytes to the MD5 hash function, low-order byte first. e) Pass the first element of the file’s file identifier array (the value of the ID entry in the document’s trailer dictionary; see Table 15) to the MD5 hash function. f) (Security handlers of revision 4 or greater) If document metadata is not being encrypted, pass 4 bytes with the value 0xFFFFFFFF to the MD5 hash function. g) Finish the hash. h) (Security handlers of revision 3 or greater) Do the following 50 times: Take the output from the previous MD5 hash and pass the first n bytes of the output as input into a new MD5 hash, where n is the number of bytes of the encryption key as defined by the value of the encryption dictionary’s Length entry. i) Set the encryption key to the first n bytes of the output from the final MD5 hash, where n shall always be 5 for security handlers of revision 2 but, for security handlers of revision 3 or greater, shall depend on the value of the encryption dictionary’s Length entry. Args: password: The encryption secret as a bytes-string rev: The encryption revision (see PDF standard) key_size: The size of the key in bytes o_entry: The owner entry P: A set of flags specifying which operations shall be permitted when the document is opened with user access. If bit 2 is set to 1, all other bits are ignored and all operations are permitted. If bit 2 is set to 0, permission for operations are based on the values of the remaining flags defined in Table 24. id1_entry: metadata_encrypted: A boolean indicating if the metadata is encrypted. Returns: The u_hash digest of length key_size zxtdƒD]}tj|ƒjƒ}q(W|d|d…}|S)u Algorithm 3: Computing the encryption dictionary’s O (owner password) value. a) Pad or truncate the owner password string as described in step (a) of "Algorithm 2: Computing an encryption key". If there is no owner password, use the user password instead. b) Initialize the MD5 hash function and pass the result of step (a) as input to this function. c) (Security handlers of revision 3 or greater) Do the following 50 times: Take the output from the previous MD5 hash and pass it as input into a new MD5 hash. d) Create an RC4 encryption key using the first n bytes of the output from the final MD5 hash, where n shall always be 5 for security handlers of revision 2 but, for security handlers of revision 3 or greater, shall depend on the value of the encryption dictionary’s Length entry. e) Pad or truncate the user password string as described in step (a) of "Algorithm 2: Computing an encryption key". f) Encrypt the result of step (e), using an RC4 encryption function with the encryption key obtained in step (d). g) (Security handlers of revision 3 or greater) Do the following 19 times: Take the output from the previous invocation of the RC4 function and pass it as input to a new invocation of the function; use an encryption key generated by taking each byte of the encryption key obtained in step (d) and performing an XOR (exclusive or) operation between that byte and the single-byte value of the iteration counter (from 1 to 19). h) Store the output from the final invocation of the RC4 function as the value of the O entry in the encryption dictionary. Args: owner_password: rev: The encryption revision (see PDF standard) key_size: The size of the key in bytes Returns: The RC4 key rKrLNrJ)r@rMrNrQr8)rWrCrDrRZ o_hash_digestrUÚrc4_keyr%r%r&Úcompute_O_value_keyÎs)zAlgV4.compute_O_value_key)rXÚ user_passwordrCr#csVt|ƒ}t||ƒ}|dkrRx6tddƒD](‰tt‡fdd„|Dƒƒƒ}t||ƒ}q&W|S)zÝ See :func:`compute_O_value_key`. Args: rc4_key: user_password: rev: The encryption revision (see PDF standard) Returns: The RC4 encrypted rKréc3s|]}|ˆAVqdS)Nr%)r*r+)r:r%r&r,sz(AlgV4.compute_O_value..)r@rr8ÚbytesÚ bytearray)rXrZrCrRÚrc4_encr5r%)r:r&Úcompute_O_values  zAlgV4.compute_O_value)r5rCrGr#csx|dkrt|tƒ}|Stjtƒ}|j|ƒt||jƒƒ}x6tddƒD](‰tt‡fdd„|Dƒƒƒ}t||ƒ}qDWt |ƒS)uø Algorithm 4: Computing the encryption dictionary’s U (user password) value. (Security handlers of revision 2) a) Create an encryption key based on the user password string, as described in "Algorithm 2: Computing an encryption key". b) Encrypt the 32-byte padding string shown in step (a) of "Algorithm 2: Computing an encryption key", using an RC4 encryption function with the encryption key from the preceding step. c) Store the result of step (b) as the value of the U entry in the encryption dictionary. Args: key: rev: The encryption revision (see PDF standard) id1_entry: Returns: The value érr[c3s|]}|ˆAVqdS)Nr%)r*r+)r:r%r&r,Nsz(AlgV4.compute_U_value..) rr?rMrNr/rQr8r\r]r@)r5rCrGr6rSr^rXr%)r:r&Úcompute_U_values   zAlgV4.compute_U_value) rZrCrDrEÚu_entryrFrGrHr#c CsTtj|||||||ƒ}tj|||ƒ} |dkrD| dd…} |dd…}| |krPd}|S)uq Algorithm 6: Authenticating the user password. a) Perform all but the last step of "Algorithm 4: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 2)" or "Algorithm 5: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 3 or greater)" using the supplied password string. b) If the result of step (a) is equal to the value of the encryption dictionary’s U entry (comparing on the first 16 bytes in the case of security handlers of revision 3 or greater), the password supplied is the correct user password. The key obtained in step (a) (that is, in the first step of "Algorithm 4: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 2)" or "Algorithm 5: Computing the encryption dictionary’s U (user password) value (Security handlers of revision 3 or greater)") shall be used to decrypt the document. Args: user_password: The user password as a bytes stream rev: The encryption revision (see PDF standard) key_size: The size of the key in bytes o_entry: The owner entry u_entry: The user entry P: A set of flags specifying which operations shall be permitted when the document is opened with user access. If bit 2 is set to 1, all other bits are ignored and all operations are permitted. If bit 2 is set to 0, permission for operations are based on the values of the remaining flags defined in Table 24. id1_entry: metadata_encrypted: A boolean indicating if the metadata is encrypted. Returns: The key rKNéó)rArVra) rZrCrDrErbrFrGrHr5Úu_valuer%r%r&Úverify_user_passwordRs/  zAlgV4.verify_user_password) rWrCrDrErbrFrGrHr#c sxtj|||ƒ}|dkr"t||ƒ} n>|} x8tdddƒD](‰tt‡fdd„|Dƒƒƒ} t| | ƒ} q4Wtj| |||||||ƒS)uM Algorithm 7: Authenticating the owner password. a) Compute an encryption key from the supplied password string, as described in steps (a) to (d) of "Algorithm 3: Computing the encryption dictionary’s O (owner password) value". b) (Security handlers of revision 2 only) Decrypt the value of the encryption dictionary’s O entry, using an RC4 encryption function with the encryption key computed in step (a). (Security handlers of revision 3 or greater) Do the following 20 times: Decrypt the value of the encryption dictionary’s O entry (first iteration) or the output from the previous iteration (all subsequent iterations), using an RC4 encryption function with a different encryption key at each iteration. The key shall be generated by taking the original key (obtained in step (a)) and performing an XOR (exclusive or) operation between each byte of the key and the single-byte value of the iteration counter (from 19 to 0). c) The result of step (b) purports to be the user password. Authenticate this user password using "Algorithm 6: Authenticating the user password". If it is correct, the password supplied is the correct owner password. Args: owner_password: rev: The encryption revision (see PDF standard) key_size: The size of the key in bytes o_entry: The owner entry u_entry: The user entry P: A set of flags specifying which operations shall be permitted when the document is opened with user access. If bit 2 is set to 1, all other bits are ignored and all operations are permitted. If bit 2 is set to 0, permission for operations are based on the values of the remaining flags defined in Table 24. id1_entry: metadata_encrypted: A boolean indicating if the metadata is encrypted. Returns: bytes r`érc3s|]}|ˆAVqdS)Nr%)r*r+)r:r%r&r,Æsz.AlgV4.verify_owner_password..éÿÿÿÿrh)rArYrr8r\r]rf) rWrCrDrErbrFrGrHrXrZr5r%)r:r&Úverify_owner_passwordŒs 3 zAlgV4.verify_owner_passwordN) r;r<r=Ú staticmethodr\ÚintÚboolrVrYr_rarfrir%r%r%r&rAysFK2;0rAc @söeZdZeeeeeeedœdd„ƒZeeeeeedœdd„ƒZeeeeeedœdd „ƒZeeeee e d œd d „ƒZ eeeeeee e e e fd œdd„ƒZ eeeeeeefdœdd„ƒZeeeeeeeefdœdd„ƒZeeee edœdd„ƒZdS)ÚAlgV5)ÚRrBÚo_valueÚoe_valuerer#cCs‚|dd…}tj|||dd…|dd…ƒ|dd…kré(é0rdcss|] }dVqdS)rNr%)r*rUr%r%r&r,sz.AlgV5.verify_owner_password..rc)rmÚcalculate_hashr\r8r)rnrBrorpreÚivÚtmp_keyr5r%r%r&riÕs=   zAlgV5.verify_owner_password)rnrBreÚue_valuer#cCsn|dd…}tj|||dd…dƒ|dd…kr4dStdd„tdƒDƒƒ}tj|||dd …dƒ}t|||ƒS) a" See :func:`verify_owner_password`. Args: R: A number specifying which revision of the standard security handler shall be used to interpret this dictionary password: The user password u_value: A 32-byte string, based on the user password, that shall be used in determining whether to prompt the user for a password and, if so, whether a valid user or owner password was entered. ue_value: Returns: bytes Nrqr>rrrdcss|] }dVqdS)rNr%)r*rUr%r%r&r,3sz-AlgV5.verify_user_password..rcrs)rmrtr\r8r)rnrBrerwrurvr%r%r&rfs  $zAlgV5.verify_user_password)rnrBÚsaltÚudatar#c Cs¶tj|||ƒjƒ}|dkr"|Sd}x‚|d7}|||}t|dd…|dd…|dƒ}tjtjtjft|dd…ƒd}||ƒjƒ}|dkr(|d|dkr(Pq(W|dd…S) Nérrrcr>é@rKrh)rMÚsha256rQrÚsha384Úsha512Úsum) rnrBrxryÚkÚcountZk1ÚeZhash_fnr%r%r&rt7s    zAlgV5.calculate_hash)r5ÚpermsÚprHr#cCs>|rdnd}tjd|ƒd|d}t||ƒ}||dd…kS)at See :func:`verify_owner_password` and :func:`compute_perms_value`. Args: key: The owner password perms: p: A set of flags specifying which operations shall be permitted when the document is opened with user access. If bit 2 is set to 1, all other bits are ignored and all operations are permitted. If bit 2 is set to 0, permission for operations are based on the values of the remaining flags defined in Table 24. metadata_encrypted: Returns: A boolean óTóFz.)ÚsecretsÚ token_bytesrmrtr\r8r) rnrBr5Ú random_bytesÚval_saltÚkey_saltrervrurwr%r%r&ra|s    zAlgV5.compute_U_value)rnrBr5rer#c Cs|tjdƒ}|dd…}|dd…}tj||||ƒ||}tj||||dd…ƒ}tdd„tdƒDƒƒ} t|| |ƒ} || fS)uØ Algorithm 3.9 Computing the encryption dictionary’s O (owner password) and OE (owner encryption key) values. 1. Generate 16 random bytes of data using a strong random number generator. The first 8 bytes are the Owner Validation Salt. The second 8 bytes are the Owner Key Salt. Compute the 32-byte SHA-256 hash of the password concatenated with the Owner Validation Salt and then concatenated with the 48-byte U string as generated in Algorithm 3.8. The 48-byte string consisting of the 32-byte hash followed by the Owner Validation Salt followed by the Owner Key Salt is stored as the O key. 2. Compute the 32-byte SHA-256 hash of the password concatenated with the Owner Key Salt and then concatenated with the 48-byte U string as generated in Algorithm 3.8. Using this hash as the key, encrypt the file encryption key using AES-256 in CBC mode with no padding and an initialization vector of zero. The resulting 32-byte string is stored as the OE key. Args: R: password: key: u_value: A 32-byte string, based on the user password, that shall be used in determining whether to prompt the user for a password and, if so, whether a valid user or owner password was entered. Returns: A tuple (O value, OE value) rcNrJrscss|] }dVqdS)rNr%)r*rUr%r%r&r,Ész(AlgV5.compute_O_value..)rŽr�rmrtr\r8r) rnrBr5rer�r‘r’rorvrurpr%r%r&r_ s"    zAlgV5.compute_O_value)r5r„rHr#cCs@|rdnd}tjdƒ}tjd|ƒd|d|}t||ƒ}|S)uÞ Algorithm 3.10 Computing the encryption dictionary’s Perms (permissions) value. 1. Extend the permissions (contents of the P integer) to 64 bits by setting the upper 32 bits to all 1’s. (This allows for future extension without changing the format.) 2. Record the 8 bytes of permission in the bytes 0-7 of the block, low order byte first. 3. Set byte 8 to the ASCII value ' T ' or ' F ' according to the EncryptMetadata Boolean. 4. Set bytes 9-11 to the ASCII characters ' a ', ' d ', ' b '. 5. Set bytes 12-15 to 4 bytes of random data, which will be ignored. 6. Encrypt the 16-byte block using AES-256 in ECB mode with an initialization vector of zero, using the file encryption key as the key. The result (16 bytes) is stored as the Perms string, and checked for validity when the file is opened. Args: key: p: A set of flags specifying which operations shall be permitted when the document is opened with user access. If bit 2 is set to 1, all other bits are ignored and all operations are permitted. If bit 2 is set to 0, permission for operations are based on the values of the remaining flags defined in Table 24. metadata_encrypted: A boolean indicating if the metadata is encrypted. Returns: The perms value r…r†rIztk r6|jdƒ}Yq>Xn|}|S)Nzlatin-1zutf-8)r-ÚstrÚencodeÚ Exception)rBÚpwdr%r%r&Ú_encode_password¬s zEncryption._encode_passwordcCsF|j|ƒ}|jdkr|j|ƒn|j|ƒ\}}|tjkrB||_||_|S)NrI)rÁr¥Ú verify_v4Ú verify_v5r“r”r®r¯)r$rBrÀr5Úrcr%r%r&Úverify·s  " zEncryption.verifyc Csztj||j|j|jj|jj|j|j|j ƒ}|r8|t j fStj ||j|j|jj|jj|j|j|j ƒ}|rp|t j fSdt jfS)Nrd)rArirnr¦r­rŸr rFrGr¨r“r–rfr•r”)r$rBr5r%r%r&r¿s.  zEncryption.verify_v4cCs‚tj|j||jj|jj|jjƒ}tj}|sJtj |j||jj|jj ƒ}tj }|sXdtj fStj ||jj|j|jƒsztdtƒ||fS)Nrdzignore '/Perms' verify failed)rmrirnr­rŸr¡r r“r–rfr¢r•r”r‹r£rFr¨rr;)r$rBr5rÄr%r%r&rÃÛs  zEncryption.verify_v5)rZrWr#c Cs|j|ƒ}|r|j|ƒnd}|dkr(|}|jdkr@|j||ƒnjtj|jdƒ|_tj|j |||j|j |j ƒ}|d|j _ |d|j _|d|j _|d|j _|d|j _tƒ}t|jƒ|tdƒ<t|j ƒ|td ƒ<t|jƒ|td ƒ<t|j ƒ|td ƒ<td ƒ|td ƒ<t|j j ƒ|tdƒ<t|j jƒ|tdƒ<|jdk�r¸tƒ}tdƒ|tdƒ<t|jƒ|tdƒ<t|jdƒ|td ƒ<tƒ}||tdƒ<||tdƒ<tdƒ|tdƒ<tdƒ|tdƒ<|jdk�rt|j jƒ|tdƒ<t|j jƒ|tdƒ<t|j jƒ|tdƒ<|S)NrIrJz/Oz/Uz/OEz/UEz/Permsz/Vz/Rz/Lengthz/Pz /Standardz/Filterz/DocOpenz /AuthEventz/CFMz/StdCFz/CFz/StmFz/StrFr™)rÁr¥Úcompute_values_v4rŽr�r¦r¯rmr�rnrFr¨r­rŸr r¡r¢r£rrrrrª) r$rZrWZuser_pwdZ owner_pwdr­Údict_objZstd_cfrµr%r%r&Ú write_entryïsJ           zEncryption.write_entrycCsntj||j|jƒ}tj|||jƒ}tj||j|j||j|j|jƒ}tj ||j|jƒ}||_ ||j _ ||j _ dS)N)rArYrnr¦r_rVrFrGr¨rar¯r­rŸr )r$rZrWrXror5rer%r%r&rÆ#szEncryption.compute_values_v4)Úencryption_entryr©r#c CsÜ|jdƒdkrtdƒ‚d|kr&tdƒ‚d}d}d}|jddƒ}|d(krVtd|›d�ƒ‚|d k�r|d}|jddƒ}|jddƒ}|jd|ƒ}|dkr ||d}|dkr´||d}|dkrÈ||d}d)}||krätd|›d�ƒ‚||k�rþtd|›d�ƒ‚||k�rtd|›d�ƒ‚tt|dƒ}tt|dƒ} |jddƒ} |jd ƒ} | dk �rZ| jnd!} tƒ} tt|d"ƒj| _tt|d#ƒj| _ |jd$tƒƒj| _ |jd%tƒƒj| _ |jd&tƒƒj| _ t ||| | | || ||||d'� S)*Nz/Filterz /Standardz1only Standard PDF encryption handler is availablez /SubFilterz/SubFilter NOT supportedú/V2z/Vrrr`rKrIr™z Encryption V=z NOT supportedz/CFz/StmFú /Identityz/StrFz/EFFz/CFMú/AESV2ú/AESV3z StmF Method z NOT supported!z StrF Method z EFF Method z/Rz/Pz/Lengthrrz/EncryptMetadataTz/Oz/Uz/OEz/UEz/Perms) r¥rnr¦rFr¨r©r­r«rªr¬r§)rr`rKrIr™)rËrÊrÌrÍ)ÚgetÚNotImplementedErrorr rkr6ržrr.rŸr r¡r¢r£r¤) rÉr©Ú stm_filterÚ str_filterÚ ef_filterÚalg_verÚfiltersZallowed_methodsÚalg_revZ perm_flagsÚkey_bitsZencrypt_metadatar­r%r%r&Úread6sh            zEncryption.read)ÚalgÚ permissionsr©r#c Csd|\}}}d\}}}|tjkr*d\}}}n|tjtjfkrDd\}}}t||||d|d|||tƒd� S) Nú/V2ú/AESV2ú/AESV3T) r¥rnr¦rFr¨r©r­r«rªr¬r§)rÚrÚrÚ)rÛrÛrÛ)rÜrÜrÜ)r—r›rœr�r¤r) rØrÙr©rÓrÕrÖrÐrÑrÒr%r%r&Úmakews$     zEncryption.make)"r;r<r=Ú__doc__rkrrlr\r½rržr'r°rr)r7rjr³rr´r r¸rrÁr“rÅrrÂrÃrÈrÆr×r—rÝr%r%r%r&r¤ sF >  2@r¤)/rMrŽrOÚenumrrÚtypingrrrrrr Zpypdf._crypt_providersr r r r rrrrrrÚ_utilsrZgenericrrrrrrrrrrr?r\r@rArmr“Útupler—ržr¤r%r%r%r&Ús( 0 , 4]"