2020-08-27 21:45:09 +00:00
|
|
|
//
|
|
|
|
// 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/
|
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
package security ;import (_ab "bytes";_dg "crypto/aes";_d "crypto/cipher";_ag "crypto/md5";_f "crypto/rand";_c "crypto/rc4";_dd "crypto/sha256";_e "crypto/sha512";_ga "encoding/binary";_bg "errors";_fb "fmt";_df "github.com/unidoc/unipdf/v3/common";_g "hash";
|
|
|
|
_a "io";_ad "math";);func (_bcd stdHandlerR4 )alg2 (_da *StdEncryptDict ,_ef []byte )[]byte {_df .Log .Trace ("\u0061\u006c\u0067\u0032");_gbc :=_bcd .paddedPass (_ef );_dbd :=_ag .New ();_dbd .Write (_gbc );_dbd .Write (_da .O );var _ce [4]byte ;_ga .LittleEndian .PutUint32 (_ce [:],uint32 (_da .P ));
|
|
|
|
_dbd .Write (_ce [:]);_df .Log .Trace ("\u0067o\u0020\u0050\u003a\u0020\u0025\u0020x",_ce );_dbd .Write ([]byte (_bcd .ID0 ));_df .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",_da .R ,_da .EncryptMetadata );
|
|
|
|
if (_da .R >=4)&&!_da .EncryptMetadata {_dbd .Write ([]byte {0xff,0xff,0xff,0xff});};_bdb :=_dbd .Sum (nil );if _da .R >=3{_dbd =_ag .New ();for _def :=0;_def < 50;_def ++{_dbd .Reset ();_dbd .Write (_bdb [0:_bcd .Length /8]);_bdb =_dbd .Sum (nil );};};
|
|
|
|
if _da .R >=3{return _bdb [0:_bcd .Length /8];};return _bdb [0:5];};func _ba (_be _d .Block )*ecb {return &ecb {_bgf :_be ,_ac :_be .BlockSize ()}};func (_dec stdHandlerR6 )alg10 (_fccb *StdEncryptDict ,_eef []byte )error {if _dbc :=_dbe ("\u0061\u006c\u00671\u0030","\u004b\u0065\u0079",32,_eef );
|
|
|
|
_dbc !=nil {return _dbc ;};_bbg :=uint64 (uint32 (_fccb .P ))|(_ad .MaxUint32 <<32);Perms :=make ([]byte ,16);_ga .LittleEndian .PutUint64 (Perms [:8],_bbg );if _fccb .EncryptMetadata {Perms [8]='T';}else {Perms [8]='F';};copy (Perms [9:12],"\u0061\u0064\u0062");
|
|
|
|
if _ ,_abe :=_a .ReadFull (_f .Reader ,Perms [12:16]);_abe !=nil {return _abe ;};_adb ,_cadc :=_bccf (_eef [:32]);if _cadc !=nil {return _cadc ;};_dgb :=_bc (_adb );_dgb .CryptBlocks (Perms ,Perms );_fccb .Perms =Perms [:16];return nil ;};type errInvalidField struct{Func string ;
|
|
|
|
Field string ;Exp int ;Got int ;};type stdHandlerR4 struct{Length int ;ID0 string ;};func _agf (_dadd ,_bgc ,_dfgg []byte )([]byte ,error ){var (_bee ,_ggd ,_fcg _g .Hash ;);_bee =_dd .New ();_egd :=make ([]byte ,64);_fca :=_bee ;_fca .Write (_dadd );K :=_fca .Sum (_egd [:0]);
|
|
|
|
_ddgg :=make ([]byte ,64*(127+64+48));_feg :=func (_fcgf int )([]byte ,error ){_fce :=len (_bgc )+len (K )+len (_dfgg );_fed :=_ddgg [:_fce ];_bdbd :=copy (_fed ,_bgc );_bdbd +=copy (_fed [_bdbd :],K [:]);_bdbd +=copy (_fed [_bdbd :],_dfgg );if _bdbd !=_fce {_df .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 ,_bg .New ("\u0077\u0072\u006f\u006e\u0067\u0020\u0073\u0069\u007a\u0065");};K1 :=_ddgg [:_fce *64];_fcc (K1 ,_fce );_dbdb ,_cac :=_bccf (K [0:16]);if _cac !=nil {return nil ,_cac ;};_eec :=_d .NewCBCEncrypter (_dbdb ,K [16:32]);_eec .CryptBlocks (K1 ,K1 );
|
|
|
|
E :=K1 ;_fad :=0;for _ed :=0;_ed < 16;_ed ++{_fad +=int (E [_ed ]%3);};var _gefd _g .Hash ;switch _fad %3{case 0:_gefd =_bee ;case 1:if _ggd ==nil {_ggd =_e .New384 ();};_gefd =_ggd ;case 2:if _fcg ==nil {_fcg =_e .New ();};_gefd =_fcg ;};_gefd .Reset ();
|
|
|
|
_gefd .Write (E );K =_gefd .Sum (_egd [:0]);return E ,nil ;};for _dceb :=0;;{E ,_bga :=_feg (_dceb );if _bga !=nil {return nil ,_bga ;};_dfc :=E [len (E )-1];_dceb ++;if _dceb >=64&&_dfc <=uint8 (_dceb -32){break ;};};return K [:32],nil ;};type ecbEncrypter ecb ;
|
|
|
|
var _ StdHandler =stdHandlerR4 {};func _bccf (_gdc []byte )(_d .Block ,error ){_gbg ,_deac :=_dg .NewCipher (_gdc );if _deac !=nil {_df .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",_deac );
|
|
|
|
return nil ,_deac ;};return _gbg ,nil ;};
|
2021-01-07 14:20:10 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// AuthEvent is an event type that triggers authentication.
|
|
|
|
type AuthEvent string ;type ecbDecrypter ecb ;const _cff ="\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";func (_gac stdHandlerR6 )alg2b (R int ,_bba ,_cab ,_ccaa []byte )([]byte ,error ){if R ==5{return _dfg (_bba );
|
|
|
|
};return _agf (_bba ,_cab ,_ccaa );};func (_fba stdHandlerR4 )alg4 (_gg []byte ,_afa []byte )([]byte ,error ){_ea ,_gad :=_c .NewCipher (_gg );if _gad !=nil {return nil ,_bg .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");
|
|
|
|
};_dbb :=[]byte (_cff );_ddb :=make ([]byte ,len (_dbb ));_ea .XORKeyStream (_ddb ,_dbb );return _ddb ,nil ;};func (_bd errInvalidField )Error ()string {return _fb .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",_bd .Func ,_bd .Field ,_bd .Exp ,_bd .Got );
|
|
|
|
};
|
2021-04-23 20:28:14 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// NewHandlerR4 creates a new standard security handler for R<=4.
|
|
|
|
func NewHandlerR4 (id0 string ,length int )StdHandler {return stdHandlerR4 {ID0 :id0 ,Length :length }};
|
2021-07-30 00:21:16 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// NewHandlerR6 creates a new standard security handler for R=5 and R=6.
|
|
|
|
func NewHandlerR6 ()StdHandler {return stdHandlerR6 {}};
|
2021-07-30 00:21:16 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// Allowed checks if a set of permissions can be granted.
|
|
|
|
func (_bb Permissions )Allowed (p2 Permissions )bool {return _bb &p2 ==p2 };func (_dgd stdHandlerR4 )alg3 (R int ,_aa ,_bdc []byte )([]byte ,error ){var _ffd []byte ;if len (_bdc )> 0{_ffd =_dgd .alg3Key (R ,_bdc );}else {_ffd =_dgd .alg3Key (R ,_aa );
|
|
|
|
};_cbe ,_fc :=_c .NewCipher (_ffd );if _fc !=nil {return nil ,_bg .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_add :=_dgd .paddedPass (_aa );_bede :=make ([]byte ,len (_add ));_cbe .XORKeyStream (_bede ,_add );
|
|
|
|
if R >=3{_fd :=make ([]byte ,len (_ffd ));for _dea :=0;_dea < 19;_dea ++{for _abb :=0;_abb < len (_ffd );_abb ++{_fd [_abb ]=_ffd [_abb ]^byte (_dea +1);};_eee ,_eb :=_c .NewCipher (_fd );if _eb !=nil {return nil ,_bg .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");
|
|
|
|
};_eee .XORKeyStream (_bede ,_bede );};};return _bede ,nil ;};func (_fegf stdHandlerR6 )alg11 (_dag *StdEncryptDict ,_cbeg []byte )([]byte ,error ){if _adgd :=_dbe ("\u0061\u006c\u00671\u0031","\u0055",48,_dag .U );_adgd !=nil {return nil ,_adgd ;};_afb :=make ([]byte ,len (_cbeg )+8);
|
|
|
|
_ade :=copy (_afb ,_cbeg );_ade +=copy (_afb [_ade :],_dag .U [32:40]);_bcdf ,_caf :=_fegf .alg2b (_dag .R ,_afb ,_cbeg ,nil );if _caf !=nil {return nil ,_caf ;};_bcdf =_bcdf [:32];if !_ab .Equal (_bcdf ,_dag .U [:32]){return nil ,nil ;};return _bcdf ,nil ;
|
|
|
|
};func (_ge *ecbDecrypter )CryptBlocks (dst ,src []byte ){if len (src )%_ge ._ac !=0{_df .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 ){_df .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{_ge ._bgf .Decrypt (dst ,src [:_ge ._ac ]);src =src [_ge ._ac :];dst =dst [_ge ._ac :];};};func _fcc (_aca []byte ,_gff int ){_dfe :=_gff ;for _dfe < len (_aca ){copy (_aca [_dfe :],_aca [:_dfe ]);_dfe *=2;};};const (EventDocOpen =AuthEvent ("\u0044o\u0063\u004f\u0070\u0065\u006e");
|
|
|
|
EventEFOpen =AuthEvent ("\u0045\u0046\u004f\u0070\u0065\u006e"););func _ff (_fg _d .Block )_d .BlockMode {return (*ecbDecrypter )(_ba (_fg ))};func _dbe (_fge ,_acf string ,_af int ,_dc []byte )error {if len (_dc )< _af {return errInvalidField {Func :_fge ,Field :_acf ,Exp :_af ,Got :len (_dc )};
|
|
|
|
};return nil ;};
|
2021-04-06 22:35:37 +00:00
|
|
|
|
2021-05-11 00:01:27 +00:00
|
|
|
// StdHandler is an interface for standard security handlers.
|
|
|
|
type StdHandler interface{
|
2021-04-23 20:28:14 +00:00
|
|
|
|
2021-05-11 00:01:27 +00:00
|
|
|
// 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.
|
2021-08-13 01:33:42 +00:00
|
|
|
GenerateParams (_db *StdEncryptDict ,_bca ,_gd []byte )([]byte ,error );
|
2021-04-23 20:28:14 +00:00
|
|
|
|
2021-05-11 00:01:27 +00:00
|
|
|
// 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.
|
2021-08-13 01:33:42 +00:00
|
|
|
Authenticate (_cb *StdEncryptDict ,_acc []byte )([]byte ,Permissions ,error );};func (_gfa stdHandlerR6 )alg13 (_bea *StdEncryptDict ,_gcb []byte )error {if _ccdf :=_dbe ("\u0061\u006c\u00671\u0033","\u004b\u0065\u0079",32,_gcb );_ccdf !=nil {return _ccdf ;
|
|
|
|
};if _febe :=_dbe ("\u0061\u006c\u00671\u0033","\u0050\u0065\u0072m\u0073",16,_bea .Perms );_febe !=nil {return _febe ;};_bccfd :=make ([]byte ,16);copy (_bccfd ,_bea .Perms [:16]);_fbc ,_deag :=_dg .NewCipher (_gcb [:32]);if _deag !=nil {return _deag ;
|
|
|
|
};_ffg :=_ff (_fbc );_ffg .CryptBlocks (_bccfd ,_bccfd );if !_ab .Equal (_bccfd [9:12],[]byte ("\u0061\u0064\u0062")){return _bg .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");
|
|
|
|
};_ffbaa :=Permissions (_ga .LittleEndian .Uint32 (_bccfd [0:4]));if _ffbaa !=_bea .P {return _bg .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");
|
|
|
|
};var _afd bool ;if _bccfd [8]=='T'{_afd =true ;}else if _bccfd [8]=='F'{_afd =false ;}else {return _bg .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 _afd !=_bea .EncryptMetadata {return _bg .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 ;};func (_cad stdHandlerR6 )alg9 (_ged *StdEncryptDict ,_edg []byte ,_gaa []byte )error {if _fff :=_dbe ("\u0061\u006c\u0067\u0039","\u004b\u0065\u0079",32,_edg );_fff !=nil {return _fff ;};if _gdcd :=_dbe ("\u0061\u006c\u0067\u0039","\u0055",48,_ged .U );
|
|
|
|
_gdcd !=nil {return _gdcd ;};var _bdd [16]byte ;if _ ,_faa :=_a .ReadFull (_f .Reader ,_bdd [:]);_faa !=nil {return _faa ;};_gefdf :=_bdd [0:8];_feb :=_bdd [8:16];_fbb :=_ged .U [:48];_fedf :=make ([]byte ,len (_gaa )+len (_gefdf )+len (_fbb ));_gdgf :=copy (_fedf ,_gaa );
|
|
|
|
_gdgf +=copy (_fedf [_gdgf :],_gefdf );_gdgf +=copy (_fedf [_gdgf :],_fbb );_dade ,_adg :=_cad .alg2b (_ged .R ,_fedf ,_gaa ,_fbb );if _adg !=nil {return _adg ;};O :=make ([]byte ,len (_dade )+len (_gefdf )+len (_feb ));_gdgf =copy (O ,_dade [:32]);_gdgf +=copy (O [_gdgf :],_gefdf );
|
|
|
|
_gdgf +=copy (O [_gdgf :],_feb );_ged .O =O ;_gdgf =len (_gaa );_gdgf +=copy (_fedf [_gdgf :],_feb );_dade ,_adg =_cad .alg2b (_ged .R ,_fedf ,_gaa ,_fbb );if _adg !=nil {return _adg ;};_cgd ,_adg :=_bccf (_dade [:32]);if _adg !=nil {return _adg ;};_fde :=make ([]byte ,_dg .BlockSize );
|
|
|
|
_ddf :=_d .NewCBCEncrypter (_cgd ,_fde );OE :=make ([]byte ,32);_ddf .CryptBlocks (OE ,_edg [:32]);_ged .OE =OE ;return nil ;};func _dfg (_efa []byte )([]byte ,error ){_baea :=_dd .New ();_baea .Write (_efa );return _baea .Sum (nil ),nil ;};func (_bbf stdHandlerR4 )alg7 (_cd *StdEncryptDict ,_beg []byte )([]byte ,error ){_baa :=_bbf .alg3Key (_cd .R ,_beg );
|
|
|
|
_gf :=make ([]byte ,len (_cd .O ));if _cd .R ==2{_adf ,_bge :=_c .NewCipher (_baa );if _bge !=nil {return nil ,_bg .New ("\u0066\u0061\u0069\u006c\u0065\u0064\u0020\u0063\u0069\u0070\u0068\u0065\u0072");};_adf .XORKeyStream (_gf ,_cd .O );}else if _cd .R >=3{_fggd :=append ([]byte {},_cd .O ...);
|
|
|
|
for _eba :=0;_eba < 20;_eba ++{_cda :=append ([]byte {},_baa ...);for _cdag :=0;_cdag < len (_baa );_cdag ++{_cda [_cdag ]^=byte (19-_eba );};_cca ,_bdf :=_c .NewCipher (_cda );if _bdf !=nil {return nil ,_bg .New ("\u0066\u0061\u0069\u006c\u0065\u0064\u0020\u0063\u0069\u0070\u0068\u0065\u0072");
|
|
|
|
};_cca .XORKeyStream (_gf ,_fggd );_fggd =append ([]byte {},_gf ...);};}else {return nil ,_bg .New ("\u0069n\u0076\u0061\u006c\u0069\u0064\u0020R");};_gge ,_cdab :=_bbf .alg6 (_cd ,_gf );if _cdab !=nil {return nil ,nil ;};return _gge ,nil ;};
|
2021-05-31 17:17:31 +00:00
|
|
|
|
2021-07-30 00:21:16 +00:00
|
|
|
// 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.
|
2021-05-31 17:17:31 +00:00
|
|
|
// It expects R, P and EncryptMetadata fields to be set.
|
2021-08-13 01:33:42 +00:00
|
|
|
func (_aaa stdHandlerR6 )GenerateParams (d *StdEncryptDict ,opass ,upass []byte )([]byte ,error ){_gcf :=make ([]byte ,32);if _ ,_gfc :=_a .ReadFull (_f .Reader ,_gcf );_gfc !=nil {return nil ,_gfc ;};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 _aaaf :=_aaa .alg8 (d ,_gcf ,upass );_aaaf !=nil {return nil ,_aaaf ;};if _ebf :=_aaa .alg9 (d ,_gcf ,opass );_ebf !=nil {return nil ,_ebf ;};if d .R ==5{return _gcf ,nil ;
|
|
|
|
};if _bf :=_aaa .alg10 (d ,_gcf );_bf !=nil {return nil ,_bf ;};return _gcf ,nil ;};func (_agd *ecbEncrypter )BlockSize ()int {return _agd ._ac };func _bc (_gb _d .Block )_d .BlockMode {return (*ecbEncrypter )(_ba (_gb ))};
|
2021-05-31 17:17:31 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// Permissions is a bitmask of access permissions for a PDF file.
|
|
|
|
type Permissions uint32 ;func (stdHandlerR4 )paddedPass (_bed []byte )[]byte {_cfc :=make ([]byte ,32);_de :=copy (_cfc ,_bed );for ;_de < 32;_de ++{_cfc [_de ]=_cff [_de -len (_bed )];};return _cfc ;};
|
2021-06-21 14:01:56 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// Authenticate implements StdHandler interface.
|
|
|
|
func (_bbc stdHandlerR6 )Authenticate (d *StdEncryptDict ,pass []byte )([]byte ,Permissions ,error ){return _bbc .alg2a (d ,pass );};func (_dfa stdHandlerR6 )alg8 (_fgb *StdEncryptDict ,_egf []byte ,_ec []byte )error {if _dfgc :=_dbe ("\u0061\u006c\u0067\u0038","\u004b\u0065\u0079",32,_egf );
|
|
|
|
_dfgc !=nil {return _dfgc ;};var _bgcc [16]byte ;if _ ,_gdg :=_a .ReadFull (_f .Reader ,_bgcc [:]);_gdg !=nil {return _gdg ;};_eeb :=_bgcc [0:8];_ggg :=_bgcc [8:16];_dbba :=make ([]byte ,len (_ec )+len (_eeb ));_eca :=copy (_dbba ,_ec );copy (_dbba [_eca :],_eeb );
|
|
|
|
_ddggf ,_beb :=_dfa .alg2b (_fgb .R ,_dbba ,_ec ,nil );if _beb !=nil {return _beb ;};U :=make ([]byte ,len (_ddggf )+len (_eeb )+len (_ggg ));_eca =copy (U ,_ddggf [:32]);_eca +=copy (U [_eca :],_eeb );copy (U [_eca :],_ggg );_fgb .U =U ;_eca =len (_ec );
|
|
|
|
copy (_dbba [_eca :],_ggg );_ddggf ,_beb =_dfa .alg2b (_fgb .R ,_dbba ,_ec ,nil );if _beb !=nil {return _beb ;};_afe ,_beb :=_bccf (_ddggf [:32]);if _beb !=nil {return _beb ;};_cba :=make ([]byte ,_dg .BlockSize );_ecaa :=_d .NewCBCEncrypter (_afe ,_cba );
|
|
|
|
UE :=make ([]byte ,32);_ecaa .CryptBlocks (UE ,_egf [:32]);_fgb .UE =UE ;return nil ;};func (_agdc stdHandlerR6 )alg12 (_bgcf *StdEncryptDict ,_efe []byte )([]byte ,error ){if _ggdb :=_dbe ("\u0061\u006c\u00671\u0032","\u0055",48,_bgcf .U );_ggdb !=nil {return nil ,_ggdb ;
|
|
|
|
};if _ceda :=_dbe ("\u0061\u006c\u00671\u0032","\u004f",48,_bgcf .O );_ceda !=nil {return nil ,_ceda ;};_bbe :=make ([]byte ,len (_efe )+8+48);_adbg :=copy (_bbe ,_efe );_adbg +=copy (_bbe [_adbg :],_bgcf .O [32:40]);_adbg +=copy (_bbe [_adbg :],_bgcf .U [0:48]);
|
|
|
|
_ffc ,_gbad :=_agdc .alg2b (_bgcf .R ,_bbe ,_efe ,_bgcf .U [0:48]);if _gbad !=nil {return nil ,_gbad ;};_ffc =_ffc [:32];if !_ab .Equal (_ffc ,_bgcf .O [:32]){return nil ,nil ;};return _ffc ,nil ;};var _ StdHandler =stdHandlerR6 {};type stdHandlerR6 struct{};
|
|
|
|
func (_eac stdHandlerR4 )alg5 (_fdf []byte ,_ebc []byte )([]byte ,error ){_eg :=_ag .New ();_eg .Write ([]byte (_cff ));_eg .Write ([]byte (_eac .ID0 ));_fgg :=_eg .Sum (nil );_df .Log .Trace ("\u0061\u006c\u0067\u0035");_df .Log .Trace ("\u0065k\u0065\u0079\u003a\u0020\u0025\u0020x",_fdf );
|
|
|
|
_df .Log .Trace ("\u0049D\u003a\u0020\u0025\u0020\u0078",_eac .ID0 );if len (_fgg )!=16{return nil ,_bg .New ("\u0068a\u0073\u0068\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006eo\u0074\u0020\u0031\u0036\u0020\u0062\u0079\u0074\u0065\u0073");};_aae ,_ebca :=_c .NewCipher (_fdf );
|
|
|
|
if _ebca !=nil {return nil ,_bg .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_dfb :=make ([]byte ,16);_aae .XORKeyStream (_dfb ,_fgg );_cgg :=make ([]byte ,len (_fdf ));for _fbf :=0;_fbf < 19;_fbf ++{for _cgf :=0;
|
|
|
|
_cgf < len (_fdf );_cgf ++{_cgg [_cgf ]=_fdf [_cgf ]^byte (_fbf +1);};_aae ,_ebca =_c .NewCipher (_cgg );if _ebca !=nil {return nil ,_bg .New ("\u0066a\u0069l\u0065\u0064\u0020\u0072\u0063\u0034\u0020\u0063\u0069\u0070\u0068");};_aae .XORKeyStream (_dfb ,_dfb );
|
|
|
|
_df .Log .Trace ("\u0069\u0020\u003d\u0020\u0025\u0064\u002c\u0020\u0065\u006b\u0065\u0079:\u0020\u0025\u0020\u0078",_fbf ,_cgg );_df .Log .Trace ("\u0069\u0020\u003d\u0020\u0025\u0064\u0020\u002d\u003e\u0020\u0025\u0020\u0078",_fbf ,_dfb );};_ffb :=make ([]byte ,32);
|
|
|
|
for _dce :=0;_dce < 16;_dce ++{_ffb [_dce ]=_dfb [_dce ];};_ ,_ebca =_f .Read (_ffb [16:32]);if _ebca !=nil {return nil ,_bg .New ("\u0066a\u0069\u006c\u0065\u0064 \u0074\u006f\u0020\u0067\u0065n\u0020r\u0061n\u0064\u0020\u006e\u0075\u006d\u0062\u0065r");
|
|
|
|
};return _ffb ,nil ;};func (_fbe *ecbEncrypter )CryptBlocks (dst ,src []byte ){if len (src )%_fbe ._ac !=0{_df .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 ){_df .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{_fbe ._bgf .Encrypt (dst ,src [:_fbe ._ac ]);src =src [_fbe ._ac :];dst =dst [_fbe ._ac :];};};func (_dcb stdHandlerR6 )alg2a (_cge *StdEncryptDict ,_gbe []byte )([]byte ,Permissions ,error ){if _ccd :=_dbe ("\u0061\u006c\u00672\u0061","\u004f",48,_cge .O );
|
|
|
|
_ccd !=nil {return nil ,0,_ccd ;};if _gbb :=_dbe ("\u0061\u006c\u00672\u0061","\u0055",48,_cge .U );_gbb !=nil {return nil ,0,_gbb ;};if len (_gbe )> 127{_gbe =_gbe [:127];};_ggc ,_deaf :=_dcb .alg12 (_cge ,_gbe );if _deaf !=nil {return nil ,0,_deaf ;};
|
|
|
|
var (_gga []byte ;_dee []byte ;_ega []byte ;);var _fe Permissions ;if len (_ggc )!=0{_fe =PermOwner ;_fgd :=make ([]byte ,len (_gbe )+8+48);_gdb :=copy (_fgd ,_gbe );_gdb +=copy (_fgd [_gdb :],_cge .O [40:48]);copy (_fgd [_gdb :],_cge .U [0:48]);_gga =_fgd ;
|
|
|
|
_dee =_cge .OE ;_ega =_cge .U [0:48];}else {_ggc ,_deaf =_dcb .alg11 (_cge ,_gbe );if _deaf ==nil &&len (_ggc )==0{_ggc ,_deaf =_dcb .alg11 (_cge ,[]byte (""));};if _deaf !=nil {return nil ,0,_deaf ;}else if len (_ggc )==0{return nil ,0,nil ;};_fe =_cge .P ;
|
|
|
|
_gbd :=make ([]byte ,len (_gbe )+8);_cdf :=copy (_gbd ,_gbe );copy (_gbd [_cdf :],_cge .U [40:48]);_gga =_gbd ;_dee =_cge .UE ;_ega =nil ;};if _eae :=_dbe ("\u0061\u006c\u00672\u0061","\u004b\u0065\u0079",32,_dee );_eae !=nil {return nil ,0,_eae ;};_dee =_dee [:32];
|
|
|
|
_ca ,_deaf :=_dcb .alg2b (_cge .R ,_gga ,_gbe ,_ega );if _deaf !=nil {return nil ,0,_deaf ;};_dgf ,_deaf :=_dg .NewCipher (_ca [:32]);if _deaf !=nil {return nil ,0,_deaf ;};_ccg :=make ([]byte ,_dg .BlockSize );_beded :=_d .NewCBCDecrypter (_dgf ,_ccg );
|
|
|
|
_ccaf :=make ([]byte ,32);_beded .CryptBlocks (_ccaf ,_dee );if _cge .R ==5{return _ccaf ,_fe ,nil ;};_deaf =_dcb .alg13 (_cge ,_ccaf );if _deaf !=nil {return nil ,0,_deaf ;};return _ccaf ,_fe ,nil ;};func (_bae stdHandlerR4 )alg6 (_cc *StdEncryptDict ,_dab []byte )([]byte ,error ){var (_ffba []byte ;
|
|
|
|
_cfe error ;);_dda :=_bae .alg2 (_cc ,_dab );if _cc .R ==2{_ffba ,_cfe =_bae .alg4 (_dda ,_dab );}else if _cc .R >=3{_ffba ,_cfe =_bae .alg5 (_dda ,_dab );}else {return nil ,_bg .New ("\u0069n\u0076\u0061\u006c\u0069\u0064\u0020R");};if _cfe !=nil {return nil ,_cfe ;
|
|
|
|
};_df .Log .Trace ("\u0063\u0068\u0065\u0063k:\u0020\u0025\u0020\u0078\u0020\u003d\u003d\u0020\u0025\u0020\u0078\u0020\u003f",string (_ffba ),string (_cc .U ));_ced :=_ffba ;_dge :=_cc .U ;if _cc .R >=3{if len (_ced )> 16{_ced =_ced [0:16];};if len (_dge )> 16{_dge =_dge [0:16];
|
|
|
|
};};if !_ab .Equal (_ced ,_dge ){return nil ,nil ;};return _dda ,nil ;};func (_cf *ecbDecrypter )BlockSize ()int {return _cf ._ac };
|
|
|
|
|
|
|
|
// GenerateParams generates and sets O and U parameters for the encryption dictionary.
|
|
|
|
// It expects R, P and EncryptMetadata fields to be set.
|
|
|
|
func (_fa stdHandlerR4 )GenerateParams (d *StdEncryptDict ,opass ,upass []byte )([]byte ,error ){O ,_cbd :=_fa .alg3 (d .R ,upass ,opass );if _cbd !=nil {_df .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",_cbd );
|
|
|
|
return nil ,_cbd ;};d .O =O ;_df .Log .Trace ("\u0067\u0065\u006e\u0020\u004f\u003a\u0020\u0025\u0020\u0078",O );_bdg :=_fa .alg2 (d ,upass );U ,_cbd :=_fa .alg5 (_bdg ,upass );if _cbd !=nil {_df .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",_cbd );
|
|
|
|
return nil ,_cbd ;};d .U =U ;_df .Log .Trace ("\u0067\u0065\u006e\u0020\u0055\u003a\u0020\u0025\u0020\u0078",U );return _bdg ,nil ;};const (PermOwner =Permissions (_ad .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););type ecb struct{_bgf _d .Block ;_ac int ;};
|
2021-06-21 14:01:56 +00:00
|
|
|
|
|
|
|
// Authenticate implements StdHandler interface.
|
2021-08-13 01:33:42 +00:00
|
|
|
func (_gc stdHandlerR4 )Authenticate (d *StdEncryptDict ,pass []byte )([]byte ,Permissions ,error ){_df .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");
|
|
|
|
_fbef ,_abf :=_gc .alg7 (d ,pass );if _abf !=nil {return nil ,0,_abf ;};if _fbef !=nil {_df .Log .Trace ("\u0074h\u0069\u0073\u002e\u0061u\u0074\u0068\u0065\u006e\u0074i\u0063a\u0074e\u0064\u0020\u003d\u0020\u0054\u0072\u0075e");return _fbef ,PermOwner ,nil ;
|
|
|
|
};_df .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");_fbef ,_abf =_gc .alg6 (d ,pass );if _abf !=nil {return nil ,0,_abf ;
|
|
|
|
};if _fbef !=nil {_df .Log .Trace ("\u0074h\u0069\u0073\u002e\u0061u\u0074\u0068\u0065\u006e\u0074i\u0063a\u0074e\u0064\u0020\u003d\u0020\u0054\u0072\u0075e");return _fbef ,d .P ,nil ;};return nil ,0,nil ;};
|
2021-06-21 14:01:56 +00:00
|
|
|
|
2021-08-13 01:33:42 +00:00
|
|
|
// 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 ;};func (_cg stdHandlerR4 )alg3Key (R int ,_dad []byte )[]byte {_bcb :=_ag .New ();_ddg :=_cg .paddedPass (_dad );_bcb .Write (_ddg );if R >=3{for _ee :=0;
|
|
|
|
_ee < 50;_ee ++{_cfd :=_bcb .Sum (nil );_bcb =_ag .New ();_bcb .Write (_cfd );};};_gbcd :=_bcb .Sum (nil );if R ==2{_gbcd =_gbcd [0:5];}else {_gbcd =_gbcd [0:_cg .Length /8];};return _gbcd ;};
|