unipdf/core/security/security.go
2020-09-14 09:32:45 +00:00

58 lines
24 KiB
Go

//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
package security ;import (_af "bytes";_fa "crypto/aes";_f "crypto/cipher";_ba "crypto/md5";_gd "crypto/rand";_a "crypto/rc4";_gc "crypto/sha256";_b "crypto/sha512";_e "encoding/binary";_fb "errors";_ca "fmt";_bd "github.com/unidoc/unipdf/v3/common";_ce "hash";_c "io";_d "math";);func (_bfbeb stdHandlerR6 )alg12 (_ea *StdEncryptDict ,_fcgg []byte )([]byte ,error ){if _adgcd :=_bc ("\u0061\u006c\u00671\u0032","\u0055",48,_ea .U );_adgcd !=nil {return nil ,_adgcd ;};if _ggg :=_bc ("\u0061\u006c\u00671\u0032","\u004f",48,_ea .O );_ggg !=nil {return nil ,_ggg ;};_eee :=make ([]byte ,len (_fcgg )+8+48);_cc :=copy (_eee ,_fcgg );_cc +=copy (_eee [_cc :],_ea .O [32:40]);_cc +=copy (_eee [_cc :],_ea .U [0:48]);_bab ,_gad :=_bfbeb .alg2b (_ea .R ,_eee ,_fcgg ,_ea .U [0:48]);if _gad !=nil {return nil ,_gad ;};_bab =_bab [:32];if !_af .Equal (_bab ,_ea .O [:32]){return nil ,nil ;};return _bab ,nil ;};func _fad (_dd _f .Block )*ecb {return &ecb {_gb :_dd ,_ceb :_dd .BlockSize ()}};const (EventDocOpen =AuthEvent ("\u0044o\u0063\u004f\u0070\u0065\u006e");EventEFOpen =AuthEvent ("\u0045\u0046\u004f\u0070\u0065\u006e"););func (_efc stdHandlerR4 )alg5 (_ec []byte ,_fcb []byte )([]byte ,error ){_db :=_ba .New ();_db .Write ([]byte (_fbc ));_db .Write ([]byte (_efc .ID0 ));_bga :=_db .Sum (nil );_bd .Log .Trace ("\u0061\u006c\u0067\u0035");_bd .Log .Trace ("\u0065k\u0065\u0079\u003a\u0020\u0025\u0020x",_ec );_bd .Log .Trace ("\u0049D\u003a\u0020\u0025\u0020\u0078",_efc .ID0 );if len (_bga )!=16{return nil ,_fb .New ("\u0068a\u0073\u0068\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006eo\u0074\u0020\u0031\u0036\u0020\u0062\u0079\u0074\u0065\u0073");};_caac ,_bfb :=_a .NewCipher (_ec );if _bfb !=nil {return nil ,_fb .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_dg :=make ([]byte ,16);_caac .XORKeyStream (_dg ,_bga );_fdb :=make ([]byte ,len (_ec ));for _ac :=0;_ac < 19;_ac ++{for _dcf :=0;_dcf < len (_ec );_dcf ++{_fdb [_dcf ]=_ec [_dcf ]^byte (_ac +1);};_caac ,_bfb =_a .NewCipher (_fdb );if _bfb !=nil {return nil ,_fb .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_caac .XORKeyStream (_dg ,_dg );_bd .Log .Trace ("\u0069\u0020\u003d\u0020\u0025\u0064\u002c\u0020\u0065\u006b\u0065\u0079:\u0020\u0025\u0020\u0078",_ac ,_fdb );_bd .Log .Trace ("\u0069\u0020\u003d\u0020\u0025\u0064\u0020\u002d\u003e\u0020\u0025\u0020\u0078",_ac ,_dg );};_cae :=make ([]byte ,32);for _dcfc :=0;_dcfc < 16;_dcfc ++{_cae [_dcfc ]=_dg [_dcfc ];};_ ,_bfb =_gd .Read (_cae [16:32]);if _bfb !=nil {return nil ,_fb .New ("\u0066a\u0069\u006c\u0065\u0064 \u0074\u006f\u0020\u0067\u0065n\u0020r\u0061n\u0064\u0020\u006e\u0075\u006d\u0062\u0065r");};return _cae ,nil ;};func _ffg (_ddc []byte )(_f .Block ,error ){_ecg ,_bef :=_fa .NewCipher (_ddc );if _bef !=nil {_bd .Log .Error ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075\u006c\u0064\u0020\u006e\u006f\u0074\u0020\u0063\u0072\u0065\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0063\u0069p\u0068\u0065r\u003a\u0020\u0025\u0076",_bef );return nil ,_bef ;};return _ecg ,nil ;};
// Authenticate implements StdHandler interface.
func (_dbe stdHandlerR4 )Authenticate (d *StdEncryptDict ,pass []byte )([]byte ,Permissions ,error ){_bd .Log .Trace ("\u0044\u0065b\u0075\u0067\u0067\u0069n\u0067\u0020a\u0075\u0074\u0068\u0065\u006e\u0074\u0069\u0063a\u0074\u0069\u006f\u006e\u0020\u002d\u0020\u006f\u0077\u006e\u0065\u0072 \u0070\u0061\u0073\u0073");_dfg ,_bgd :=_dbe .alg7 (d ,pass );if _bgd !=nil {return nil ,0,_bgd ;};if _dfg !=nil {_bd .Log .Trace ("\u0074h\u0069\u0073\u002e\u0061u\u0074\u0068\u0065\u006e\u0074i\u0063a\u0074e\u0064\u0020\u003d\u0020\u0054\u0072\u0075e");return _dfg ,PermOwner ,nil ;};_bd .Log .Trace ("\u0044\u0065bu\u0067\u0067\u0069n\u0067\u0020\u0061\u0075the\u006eti\u0063\u0061\u0074\u0069\u006f\u006e\u0020- \u0075\u0073\u0065\u0072\u0020\u0070\u0061s\u0073");_dfg ,_bgd =_dbe .alg6 (d ,pass );if _bgd !=nil {return nil ,0,_bgd ;};if _dfg !=nil {_bd .Log .Trace ("\u0074h\u0069\u0073\u002e\u0061u\u0074\u0068\u0065\u006e\u0074i\u0063a\u0074e\u0064\u0020\u003d\u0020\u0054\u0072\u0075e");return _dfg ,d .P ,nil ;};return nil ,0,nil ;};func _cac (_cef ,_efe ,_fae []byte )([]byte ,error ){var (_bbc ,_adb ,_beg _ce .Hash ;);_bbc =_gc .New ();_gec :=make ([]byte ,64);_eff :=_bbc ;_eff .Write (_cef );K :=_eff .Sum (_gec [:0]);_bag :=make ([]byte ,64*(127+64+48));_bgg :=func (_bad int )([]byte ,error ){_gef :=len (_efe )+len (K )+len (_fae );_abg :=_bag [:_gef ];_dcb :=copy (_abg ,_efe );_dcb +=copy (_abg [_dcb :],K [:]);_dcb +=copy (_abg [_dcb :],_fae );if _dcb !=_gef {_bd .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020u\u006e\u0065\u0078\u0070\u0065\u0063t\u0065\u0064\u0020\u0072\u006f\u0075\u006ed\u0020\u0069\u006e\u0070\u0075\u0074\u0020\u0073\u0069\u007ae\u002e");return nil ,_fb .New ("\u0077\u0072\u006f\u006e\u0067\u0020\u0073\u0069\u007a\u0065");};K1 :=_bag [:_gef *64];_dbgg (K1 ,_gef );_dbde ,_dfe :=_ffg (K [0:16]);if _dfe !=nil {return nil ,_dfe ;};_aba :=_f .NewCBCEncrypter (_dbde ,K [16:32]);_aba .CryptBlocks (K1 ,K1 );E :=K1 ;_egae :=0;for _fce :=0;_fce < 16;_fce ++{_egae +=int (E [_fce ]%3);};var _gfe _ce .Hash ;switch _egae %3{case 0:_gfe =_bbc ;case 1:if _adb ==nil {_adb =_b .New384 ();};_gfe =_adb ;case 2:if _beg ==nil {_beg =_b .New ();};_gfe =_beg ;};_gfe .Reset ();_gfe .Write (E );K =_gfe .Sum (_gec [:0]);return E ,nil ;};for _gac :=0;;{E ,_bdc :=_bgg (_gac );if _bdc !=nil {return nil ,_bdc ;};_adgf :=uint8 (E [len (E )-1]);_gac ++;if _gac >=64&&_adgf <=uint8 (_gac -32){break ;};};return K [:32],nil ;};func _dbgg (_bgda []byte ,_aed int ){_fbd :=_aed ;for _fbd < len (_bgda ){copy (_bgda [_fbd :],_bgda [:_fbd ]);_fbd *=2;};};type stdHandlerR6 struct{};func _aaa (_fdaa []byte )([]byte ,error ){_dbg :=_gc .New ();_dbg .Write (_fdaa );return _dbg .Sum (nil ),nil ;};func _gbb (_bf _f .Block )_f .BlockMode {return (*ecbDecrypter )(_fad (_bf ))};func _bc (_bcb ,_gbbe string ,_gee int ,_bcg []byte )error {if len (_bcg )< _gee {return errInvalidField {Func :_bcb ,Field :_gbbe ,Exp :_gee ,Got :len (_bcg )};};return nil ;};func (_de *ecbEncrypter )BlockSize ()int {return _de ._ceb };
// StdEncryptDict is a set of additional fields used in standard encryption dictionary.
type StdEncryptDict struct{R int ;P Permissions ;EncryptMetadata bool ;O ,U []byte ;OE ,UE []byte ;Perms []byte ;};type stdHandlerR4 struct{Length int ;ID0 string ;};
// Authenticate implements StdHandler interface.
func (_aeb stdHandlerR6 )Authenticate (d *StdEncryptDict ,pass []byte )([]byte ,Permissions ,error ){return _aeb .alg2a (d ,pass );};type ecbEncrypter ecb ;
// GenerateParams is the algorithm opposite to alg2a (R>=5).
// It generates U,O,UE,OE,Perms fields using AESv3 encryption.
// There is no algorithm number assigned to this function in the spec.
// It expects R, P and EncryptMetadata fields to be set.
func (_bgcf stdHandlerR6 )GenerateParams (d *StdEncryptDict ,opass ,upass []byte )([]byte ,error ){_ddf :=make ([]byte ,32);if _ ,_cdb :=_c .ReadFull (_gd .Reader ,_ddf );_cdb !=nil {return nil ,_cdb ;};d .U =nil ;d .O =nil ;d .UE =nil ;d .OE =nil ;d .Perms =nil ;if len (upass )> 127{upass =upass [:127];};if len (opass )> 127{opass =opass [:127];};if _aecf :=_bgcf .alg8 (d ,_ddf ,upass );_aecf !=nil {return nil ,_aecf ;};if _dcfa :=_bgcf .alg9 (d ,_ddf ,opass );_dcfa !=nil {return nil ,_dcfa ;};if d .R ==5{return _ddf ,nil ;};if _caaa :=_bgcf .alg10 (d ,_ddf );_caaa !=nil {return nil ,_caaa ;};return _ddf ,nil ;};type errInvalidField struct{Func string ;Field string ;Exp int ;Got int ;};func (_bfc stdHandlerR6 )alg10 (_fcg *StdEncryptDict ,_gcd []byte )error {if _gcc :=_bc ("\u0061\u006c\u00671\u0030","\u004b\u0065\u0079",32,_gcd );_gcc !=nil {return _gcc ;};_bbge :=uint64 (uint32 (_fcg .P ))|(_d .MaxUint32 <<32);Perms :=make ([]byte ,16);_e .LittleEndian .PutUint64 (Perms [:8],_bbge );if _fcg .EncryptMetadata {Perms [8]='T';}else {Perms [8]='F';};copy (Perms [9:12],"\u0061\u0064\u0062");if _ ,_aceg :=_c .ReadFull (_gd .Reader ,Perms [12:16]);_aceg !=nil {return _aceg ;};_dac ,_cdd :=_ffg (_gcd [:32]);if _cdd !=nil {return _cdd ;};_cgec :=_bdb (_dac );_cgec .CryptBlocks (Perms ,Perms );_fcg .Perms =Perms [:16];return nil ;};func (stdHandlerR4 )paddedPass (_ga []byte )[]byte {_da :=make ([]byte ,32);_fd :=copy (_da ,_ga );for ;_fd < 32;_fd ++{_da [_fd ]=_fbc [_fd -len (_ga )];};return _da ;};
// StdHandler is an interface for standard security handlers.
type StdHandler interface{
// GenerateParams uses owner and user passwords to set encryption parameters and generate an encryption key.
// It assumes that R, P and EncryptMetadata are already set.
GenerateParams (_eg *StdEncryptDict ,_cba ,_ge []byte )([]byte ,error );
// Authenticate uses encryption dictionary parameters and the password to calculate
// the document encryption key. It also returns permissions that should be granted to a user.
// In case of failed authentication, it returns empty key and zero permissions with no error.
Authenticate (_cg *StdEncryptDict ,_fc []byte )([]byte ,Permissions ,error );};func (_cb *ecbEncrypter )CryptBlocks (dst ,src []byte ){if len (src )%_cb ._ceb !=0{_bd .Log .Error ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0045\u0043\u0042\u0020\u0065\u006e\u0063\u0072\u0079\u0070\u0074\u003a \u0069\u006e\u0070\u0075\u0074\u0020\u006e\u006f\u0074\u0020\u0066\u0075\u006c\u006c\u0020\u0062\u006c\u006f\u0063\u006b\u0073");return ;};if len (dst )< len (src ){_bd .Log .Error ("\u0045R\u0052\u004fR\u003a\u0020\u0045C\u0042\u0020\u0065\u006e\u0063\u0072\u0079p\u0074\u003a\u0020\u006f\u0075\u0074p\u0075\u0074\u0020\u0073\u006d\u0061\u006c\u006c\u0065\u0072\u0020t\u0068\u0061\u006e\u0020\u0069\u006e\u0070\u0075\u0074");return ;};for len (src )> 0{_cb ._gb .Encrypt (dst ,src [:_cb ._ceb ]);src =src [_cb ._ceb :];dst =dst [_cb ._ceb :];};};func (_cdc stdHandlerR4 )alg4 (_deg []byte ,_eb []byte )([]byte ,error ){_cbf ,_adg :=_a .NewCipher (_deg );if _adg !=nil {return nil ,_fb .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_bdg :=[]byte (_fbc );_bae :=make ([]byte ,len (_bdg ));_cbf .XORKeyStream (_bae ,_bdg );return _bae ,nil ;};func (_bcbd stdHandlerR6 )alg2a (_eec *StdEncryptDict ,_dfd []byte )([]byte ,Permissions ,error ){if _cda :=_bc ("\u0061\u006c\u00672\u0061","\u004f",48,_eec .O );_cda !=nil {return nil ,0,_cda ;};if _dfgb :=_bc ("\u0061\u006c\u00672\u0061","\u0055",48,_eec .U );_dfgb !=nil {return nil ,0,_dfgb ;};if len (_dfd )> 127{_dfd =_dfd [:127];};_ebg ,_afa :=_bcbd .alg12 (_eec ,_dfd );if _afa !=nil {return nil ,0,_afa ;};var (_eea []byte ;_dce []byte ;_afac []byte ;);var _caf Permissions ;if len (_ebg )!=0{_caf =PermOwner ;_dec :=make ([]byte ,len (_dfd )+8+48);_fgdf :=copy (_dec ,_dfd );_fgdf +=copy (_dec [_fgdf :],_eec .O [40:48]);_fgdf +=copy (_dec [_fgdf :],_eec .U [0:48]);_eea =_dec ;_dce =_eec .OE ;_afac =_eec .U [0:48];}else {_ebg ,_afa =_bcbd .alg11 (_eec ,_dfd );if _afa ==nil &&len (_ebg )==0{_ebg ,_afa =_bcbd .alg11 (_eec ,[]byte (""));};if _afa !=nil {return nil ,0,_afa ;}else if len (_ebg )==0{return nil ,0,nil ;};_caf =_eec .P ;_gaf :=make ([]byte ,len (_dfd )+8);_dbb :=copy (_gaf ,_dfd );_dbb +=copy (_gaf [_dbb :],_eec .U [40:48]);_eea =_gaf ;_dce =_eec .UE ;_afac =nil ;};if _acf :=_bc ("\u0061\u006c\u00672\u0061","\u004b\u0065\u0079",32,_dce );_acf !=nil {return nil ,0,_acf ;};_dce =_dce [:32];_ede ,_afa :=_bcbd .alg2b (_eec .R ,_eea ,_dfd ,_afac );if _afa !=nil {return nil ,0,_afa ;};_eed ,_afa :=_fa .NewCipher (_ede [:32]);if _afa !=nil {return nil ,0,_afa ;};_bfg :=make ([]byte ,_fa .BlockSize );_ebb :=_f .NewCBCDecrypter (_eed ,_bfg );_gbf :=make ([]byte ,32);_ebb .CryptBlocks (_gbf ,_dce );if _eec .R ==5{return _gbf ,_caf ,nil ;};_afa =_bcbd .alg13 (_eec ,_gbf );if _afa !=nil {return nil ,0,_afa ;};return _gbf ,_caf ,nil ;};func (_fcd stdHandlerR6 )alg9 (_fed *StdEncryptDict ,_gfgc []byte ,_acff []byte )error {if _fadb :=_bc ("\u0061\u006c\u0067\u0039","\u004b\u0065\u0079",32,_gfgc );_fadb !=nil {return _fadb ;};if _ege :=_bc ("\u0061\u006c\u0067\u0039","\u0055",48,_fed .U );_ege !=nil {return _ege ;};var _gdcf [16]byte ;if _ ,_efa :=_c .ReadFull (_gd .Reader ,_gdcf [:]);_efa !=nil {return _efa ;};_cdg :=_gdcf [0:8];_fgf :=_gdcf [8:16];_dcba :=_fed .U [:48];_gcg :=make ([]byte ,len (_acff )+len (_cdg )+len (_dcba ));_gfce :=copy (_gcg ,_acff );_gfce +=copy (_gcg [_gfce :],_cdg );_gfce +=copy (_gcg [_gfce :],_dcba );_ebe ,_geeg :=_fcd .alg2b (_fed .R ,_gcg ,_acff ,_dcba );if _geeg !=nil {return _geeg ;};O :=make ([]byte ,len (_ebe )+len (_cdg )+len (_fgf ));_gfce =copy (O ,_ebe [:32]);_gfce +=copy (O [_gfce :],_cdg );_gfce +=copy (O [_gfce :],_fgf );_fed .O =O ;_gfce =len (_acff );_gfce +=copy (_gcg [_gfce :],_fgf );_ebe ,_geeg =_fcd .alg2b (_fed .R ,_gcg ,_acff ,_dcba );if _geeg !=nil {return _geeg ;};_agg ,_geeg :=_ffg (_ebe [:32]);if _geeg !=nil {return _geeg ;};_efd :=make ([]byte ,_fa .BlockSize );_eeda :=_f .NewCBCEncrypter (_agg ,_efd );OE :=make ([]byte ,32);_eeda .CryptBlocks (OE ,_gfgc [:32]);_fed .OE =OE ;return nil ;};func (_bg stdHandlerR4 )alg3 (R int ,_dc ,_egd []byte )([]byte ,error ){var _eeg []byte ;if len (_egd )> 0{_eeg =_bg .alg3Key (R ,_egd );}else {_eeg =_bg .alg3Key (R ,_dc );};_ega ,_dcd :=_a .NewCipher (_eeg );if _dcd !=nil {return nil ,_fb .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_ae :=_bg .paddedPass (_dc );_fac :=make ([]byte ,len (_ae ));_ega .XORKeyStream (_fac ,_ae );if R >=3{_caa :=make ([]byte ,len (_eeg ));for _gg :=0;_gg < 19;_gg ++{for _ade :=0;_ade < len (_eeg );_ade ++{_caa [_ade ]=_eeg [_ade ]^byte (_gg +1);};_ffd ,_ddg :=_a .NewCipher (_caa );if _ddg !=nil {return nil ,_fb .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_ffd .XORKeyStream (_fac ,_fac );};};return _fac ,nil ;};func (_gfc stdHandlerR4 )alg6 (_afc *StdEncryptDict ,_cag []byte )([]byte ,error ){var (_fadg []byte ;_cf error ;);_efg :=_gfc .alg2 (_afc ,_cag );if _afc .R ==2{_fadg ,_cf =_gfc .alg4 (_efg ,_cag );}else if _afc .R >=3{_fadg ,_cf =_gfc .alg5 (_efg ,_cag );}else {return nil ,_fb .New ("\u0069n\u0076\u0061\u006c\u0069\u0064\u0020R");};if _cf !=nil {return nil ,_cf ;};_bd .Log .Trace ("\u0063\u0068\u0065\u0063k:\u0020\u0025\u0020\u0078\u0020\u003d\u003d\u0020\u0025\u0020\u0078\u0020\u003f",string (_fadg ),string (_afc .U ));_bbd :=_fadg ;_bbb :=_afc .U ;if _afc .R >=3{if len (_bbd )> 16{_bbd =_bbd [0:16];};if len (_bbb )> 16{_bbb =_bbb [0:16];};};if !_af .Equal (_bbd ,_bbb ){return nil ,nil ;};return _efg ,nil ;};func (_abb stdHandlerR4 )alg3Key (R int ,_bca []byte )[]byte {_feg :=_ba .New ();_ag :=_abb .paddedPass (_bca );_feg .Write (_ag );if R >=3{for _fee :=0;_fee < 50;_fee ++{_ff :=_feg .Sum (nil );_feg =_ba .New ();_feg .Write (_ff );};};_ad :=_feg .Sum (nil );if R ==2{_ad =_ad [0:5];}else {_ad =_ad [0:_abb .Length /8];};return _ad ;};const _fbc ="\x28\277\116\136\x4e\x75\x8a\x41\x64\000\x4e\x56\377"+"\xfa\001\010\056\x2e\x00\xb6\xd0\x68\076\x80\x2f\014"+"\251\xfe\x64\x53\x69\172";
// Permissions is a bitmask of access permissions for a PDF file.
type Permissions uint32 ;func (_afb stdHandlerR4 )alg2 (_fbf *StdEncryptDict ,_fe []byte )[]byte {_bd .Log .Trace ("\u0061\u006c\u0067\u0032");_fg :=_afb .paddedPass (_fe );_ef :=_ba .New ();_ef .Write (_fg );_ef .Write (_fbf .O );var _fgd [4]byte ;_e .LittleEndian .PutUint32 (_fgd [:],uint32 (_fbf .P ));_ef .Write (_fgd [:]);_bd .Log .Trace ("\u0067o\u0020\u0050\u003a\u0020\u0025\u0020x",_fgd );_ef .Write ([]byte (_afb .ID0 ));_bd .Log .Trace ("\u0074\u0068\u0069\u0073\u002e\u0052\u0020\u003d\u0020\u0025d\u0020\u0065\u006e\u0063\u0072\u0079\u0070t\u004d\u0065\u0074\u0061\u0064\u0061\u0074\u0061\u0020\u0025\u0076",_fbf .R ,_fbf .EncryptMetadata );if (_fbf .R >=4)&&!_fbf .EncryptMetadata {_ef .Write ([]byte {0xff,0xff,0xff,0xff});};_ee :=_ef .Sum (nil );if _fbf .R >=3{_ef =_ba .New ();for _abf :=0;_abf < 50;_abf ++{_ef .Reset ();_ef .Write (_ee [0:_afb .Length /8]);_ee =_ef .Sum (nil );};};if _fbf .R >=3{return _ee [0:_afb .Length /8];};return _ee [0:5];};func (_be errInvalidField )Error ()string {return _ca .Sprintf ("\u0025s\u003a\u0020e\u0078\u0070\u0065\u0063t\u0065\u0064\u0020%\u0073\u0020\u0066\u0069\u0065\u006c\u0064\u0020\u0074o \u0062\u0065\u0020%\u0064\u0020b\u0079\u0074\u0065\u0073\u002c\u0020g\u006f\u0074 \u0025\u0064",_be .Func ,_be .Field ,_be .Exp ,_be .Got );};func (_aag stdHandlerR6 )alg13 (_acec *StdEncryptDict ,_bcgg []byte )error {if _effe :=_bc ("\u0061\u006c\u00671\u0033","\u004b\u0065\u0079",32,_bcgg );_effe !=nil {return _effe ;};if _aggf :=_bc ("\u0061\u006c\u00671\u0033","\u0050\u0065\u0072m\u0073",16,_acec .Perms );_aggf !=nil {return _aggf ;};_cgf :=make ([]byte ,16);copy (_cgf ,_acec .Perms [:16]);_fada ,_cgc :=_fa .NewCipher (_bcgg [:32]);if _cgc !=nil {return _cgc ;};_dcfcg :=_gbb (_fada );_dcfcg .CryptBlocks (_cgf ,_cgf );if !_af .Equal (_cgf [9:12],[]byte ("\u0061\u0064\u0062")){return _fb .New ("\u0064\u0065\u0063o\u0064\u0065\u0064\u0020p\u0065\u0072\u006d\u0069\u0073\u0073\u0069o\u006e\u0073\u0020\u0061\u0072\u0065\u0020\u0069\u006e\u0076\u0061\u006c\u0069\u0064");};_ded :=Permissions (_e .LittleEndian .Uint32 (_cgf [0:4]));if _ded !=_acec .P {return _fb .New ("\u0070\u0065r\u006d\u0069\u0073\u0073\u0069\u006f\u006e\u0073\u0020\u0076\u0061\u006c\u0069\u0064\u0061\u0074\u0069\u006f\u006e\u0020\u0066\u0061il\u0065\u0064");};_add :=true ;if _cgf [8]=='T'{_add =true ;}else if _cgf [8]=='F'{_add =false ;}else {return _fb .New ("\u0064\u0065\u0063\u006f\u0064\u0065\u0064 \u006d\u0065\u0074a\u0064\u0061\u0074\u0061 \u0065\u006e\u0063\u0072\u0079\u0070\u0074\u0069\u006f\u006e\u0020\u0066\u006c\u0061\u0067\u0020\u0069\u0073\u0020\u0069\u006e\u0076\u0061\u006c\u0069\u0064");};if _add !=_acec .EncryptMetadata {return _fb .New ("\u006d\u0065t\u0061\u0064\u0061\u0074a\u0020\u0065n\u0063\u0072\u0079\u0070\u0074\u0069\u006f\u006e \u0076\u0061\u006c\u0069\u0064\u0061\u0074\u0069\u006f\u006e\u0020\u0066a\u0069\u006c\u0065\u0064");};return nil ;};const (PermOwner =Permissions (_d .MaxUint32 );PermPrinting =Permissions (1<<2);PermModify =Permissions (1<<3);PermExtractGraphics =Permissions (1<<4);PermAnnotate =Permissions (1<<5);PermFillForms =Permissions (1<<8);PermDisabilityExtract =Permissions (1<<9);PermRotateInsert =Permissions (1<<10);PermFullPrintQuality =Permissions (1<<11););func (_gf *ecbDecrypter )BlockSize ()int {return _gf ._ceb };func (_cd *ecbDecrypter )CryptBlocks (dst ,src []byte ){if len (src )%_cd ._ceb !=0{_bd .Log .Error ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0045\u0043\u0042\u0020\u0064\u0065\u0063\u0072\u0079\u0070\u0074\u003a \u0069\u006e\u0070\u0075\u0074\u0020\u006e\u006f\u0074\u0020\u0066\u0075\u006c\u006c\u0020\u0062\u006c\u006f\u0063\u006b\u0073");return ;};if len (dst )< len (src ){_bd .Log .Error ("\u0045R\u0052\u004fR\u003a\u0020\u0045C\u0042\u0020\u0064\u0065\u0063\u0072\u0079p\u0074\u003a\u0020\u006f\u0075\u0074p\u0075\u0074\u0020\u0073\u006d\u0061\u006c\u006c\u0065\u0072\u0020t\u0068\u0061\u006e\u0020\u0069\u006e\u0070\u0075\u0074");return ;};for len (src )> 0{_cd ._gb .Decrypt (dst ,src [:_cd ._ceb ]);src =src [_cd ._ceb :];dst =dst [_cd ._ceb :];};};
// NewHandlerR6 creates a new standard security handler for R=5 and R=6.
func NewHandlerR6 ()StdHandler {return stdHandlerR6 {}};
// NewHandlerR4 creates a new standard security handler for R<=4.
func NewHandlerR4 (id0 string ,length int )StdHandler {return stdHandlerR4 {ID0 :id0 ,Length :length }};func (_dgb stdHandlerR4 )alg7 (_gag *StdEncryptDict ,_ed []byte )([]byte ,error ){_fda :=_dgb .alg3Key (_gag .R ,_ed );_ffb :=make ([]byte ,len (_gag .O ));if _gag .R ==2{_dga ,_fcf :=_a .NewCipher (_fda );if _fcf !=nil {return nil ,_fb .New ("\u0066\u0061\u0069\u006c\u0065\u0064\u0020\u0063\u0069\u0070\u0068\u0065\u0072");};_dga .XORKeyStream (_ffb ,_gag .O );}else if _gag .R >=3{_ace :=append ([]byte {},_gag .O ...);for _gdg :=0;_gdg < 20;_gdg ++{_gdf :=append ([]byte {},_fda ...);for _dgg :=0;_dgg < len (_fda );_dgg ++{_gdf [_dgg ]^=byte (19-_gdg );};_aa ,_fegb :=_a .NewCipher (_gdf );if _fegb !=nil {return nil ,_fb .New ("\u0066\u0061\u0069\u006c\u0065\u0064\u0020\u0063\u0069\u0070\u0068\u0065\u0072");};_aa .XORKeyStream (_ffb ,_ace );_ace =append ([]byte {},_ffb ...);};}else {return nil ,_fb .New ("\u0069n\u0076\u0061\u006c\u0069\u0064\u0020R");};_cbg ,_dbd :=_dgb .alg6 (_gag ,_ffb );if _dbd !=nil {return nil ,nil ;};return _cbg ,nil ;};func (_gdc stdHandlerR6 )alg2b (R int ,_cge ,_bdbg ,_bgae []byte )([]byte ,error ){if R ==5{return _aaa (_cge );};return _cac (_cge ,_bdbg ,_bgae );};func (_cddg stdHandlerR6 )alg11 (_edb *StdEncryptDict ,_agb []byte )([]byte ,error ){if _dcbb :=_bc ("\u0061\u006c\u00671\u0031","\u0055",48,_edb .U );_dcbb !=nil {return nil ,_dcbb ;};_dge :=make ([]byte ,len (_agb )+8);_bfbe :=copy (_dge ,_agb );_bfbe +=copy (_dge [_bfbe :],_edb .U [32:40]);_aggd ,_eca :=_cddg .alg2b (_edb .R ,_dge ,_agb ,nil );if _eca !=nil {return nil ,_eca ;};_aggd =_aggd [:32];if !_af .Equal (_aggd ,_edb .U [:32]){return nil ,nil ;};return _aggd ,nil ;};
// AuthEvent is an event type that triggers authentication.
type AuthEvent string ;
// Allowed checks if a set of permissions can be granted.
func (_ab Permissions )Allowed (p2 Permissions )bool {return _ab &p2 ==p2 };func _bdb (_bb _f .Block )_f .BlockMode {return (*ecbEncrypter )(_fad (_bb ))};
// GenerateParams generates and sets O and U parameters for the encryption dictionary.
// It expects R, P and EncryptMetadata fields to be set.
func (_fdf stdHandlerR4 )GenerateParams (d *StdEncryptDict ,opass ,upass []byte )([]byte ,error ){O ,_aaf :=_fdf .alg3 (d .R ,upass ,opass );if _aaf !=nil {_bd .Log .Debug ("\u0045R\u0052\u004fR\u003a\u0020\u0045r\u0072\u006f\u0072\u0020\u0067\u0065\u006ee\u0072\u0061\u0074\u0069\u006e\u0067 \u004f\u0020\u0066\u006f\u0072\u0020\u0065\u006e\u0063\u0072\u0079p\u0074\u0069\u006f\u006e\u0020\u0028\u0025\u0073\u0029",_aaf );return nil ,_aaf ;};d .O =O ;_bd .Log .Trace ("\u0067\u0065\u006e\u0020\u004f\u003a\u0020\u0025\u0020\u0078",O );_bcd :=_fdf .alg2 (d ,upass );U ,_aaf :=_fdf .alg5 (_bcd ,upass );if _aaf !=nil {_bd .Log .Debug ("\u0045R\u0052\u004fR\u003a\u0020\u0045r\u0072\u006f\u0072\u0020\u0067\u0065\u006ee\u0072\u0061\u0074\u0069\u006e\u0067 \u004f\u0020\u0066\u006f\u0072\u0020\u0065\u006e\u0063\u0072\u0079p\u0074\u0069\u006f\u006e\u0020\u0028\u0025\u0073\u0029",_aaf );return nil ,_aaf ;};d .U =U ;_bd .Log .Trace ("\u0067\u0065\u006e\u0020\u0055\u003a\u0020\u0025\u0020\u0078",U );return _bcd ,nil ;};type ecbDecrypter ecb ;var _ StdHandler =stdHandlerR4 {};type ecb struct{_gb _f .Block ;_ceb int ;};var _ StdHandler =stdHandlerR6 {};func (_adgc stdHandlerR6 )alg8 (_bbg *StdEncryptDict ,_dcea []byte ,_aec []byte )error {if _cfd :=_bc ("\u0061\u006c\u0067\u0038","\u004b\u0065\u0079",32,_dcea );_cfd !=nil {return _cfd ;};var _bee [16]byte ;if _ ,_fea :=_c .ReadFull (_gd .Reader ,_bee [:]);_fea !=nil {return _fea ;};_bec :=_bee [0:8];_ddb :=_bee [8:16];_abfc :=make ([]byte ,len (_aec )+len (_bec ));_cdac :=copy (_abfc ,_aec );_cdac +=copy (_abfc [_cdac :],_bec );_dda ,_fcbg :=_adgc .alg2b (_bbg .R ,_abfc ,_aec ,nil );if _fcbg !=nil {return _fcbg ;};U :=make ([]byte ,len (_dda )+len (_bec )+len (_ddb ));_cdac =copy (U ,_dda [:32]);_cdac +=copy (U [_cdac :],_bec );_cdac +=copy (U [_cdac :],_ddb );_bbg .U =U ;_cdac =len (_aec );_cdac +=copy (_abfc [_cdac :],_ddb );_dda ,_fcbg =_adgc .alg2b (_bbg .R ,_abfc ,_aec ,nil );if _fcbg !=nil {return _fcbg ;};_bdf ,_fcbg :=_ffg (_dda [:32]);if _fcbg !=nil {return _fcbg ;};_bege :=make ([]byte ,_fa .BlockSize );_beeg :=_f .NewCBCEncrypter (_bdf ,_bege );UE :=make ([]byte ,32);_beeg .CryptBlocks (UE ,_dcea [:32]);_bbg .UE =UE ;return nil ;};