From 4b9418b9073206b18a80a59409a817d7083c8f63 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 30 Sep 2022 15:26:47 +0300 Subject: [PATCH 01/26] [de] Protect document --- cell/model/WorkbookProtection.js | 17 ++--- common/editorscommon.js | 5 ++ word/Editor/DocumentProtection.js | 38 ++++++++--- word/Editor/Serialize2.js | 9 ++- word/Editor/SerializeXml.js | 20 +++--- word/api.js | 101 ++++++++++++++++++++++++++++++ word/apiDefines.js | 15 +++++ 7 files changed, 173 insertions(+), 32 deletions(-) diff --git a/cell/model/WorkbookProtection.js b/cell/model/WorkbookProtection.js index 0ddff4edd9..07af2fb1c7 100644 --- a/cell/model/WorkbookProtection.js +++ b/cell/model/WorkbookProtection.js @@ -174,11 +174,6 @@ return str; } - - function generateHashParams() { - return {spinCount: 100000, saltValue: AscCommon.randomBytes(16).base64(), algorithmName: c_oSerProtectedAlgorithmNameTypes.SHA_512}; - } - function getPasswordHash(password, getString) { var nResult = null; if (password.length) { @@ -573,10 +568,10 @@ //просталяю временный пароль, аспинхронная проверка пароля в asc_setProtectedSheet this.setSheet(!this.sheet); if (this.sheet && password) { - var hashParams = generateHashParams(); + var hashParams = AscCommon.generateHashParams(); this.saltValue = hashParams.saltValue; this.spinCount = hashParams.spinCount; - this.algorithmName = hashParams.algorithmName; + this.algorithmName = c_oSerProtectedAlgorithmNameTypes.SHA_512; } this.temporaryPassword = password; if (callback) { @@ -877,10 +872,10 @@ this.setLockStructure(!this.lockStructure); if (this.lockStructure && password) { - var hashParams = generateHashParams(); + var hashParams = AscCommon.generateHashParams(); this.workbookSaltValue = hashParams.saltValue; this.workbookSpinCount = hashParams.spinCount; - this.workbookAlgorithmName = hashParams.algorithmName; + this.workbookAlgorithmName = c_oSerProtectedAlgorithmNameTypes.SHA_512; } this.temporaryPassword = password; if (callback) { @@ -1316,10 +1311,10 @@ }; CProtectedRange.prototype.asc_setPassword = function (val) { if (val) { - var hashParams = generateHashParams(); + var hashParams = AscCommon.generateHashParams(); this.saltValue = hashParams.saltValue; this.spinCount = hashParams.spinCount; - this.algorithmName = hashParams.algorithmName; + this.algorithmName = c_oSerProtectedAlgorithmNameTypes.SHA_512; } //генерируем хэш this.temporaryPassword = val; diff --git a/common/editorscommon.js b/common/editorscommon.js index c2ff9caf5e..e498b96128 100644 --- a/common/editorscommon.js +++ b/common/editorscommon.js @@ -12840,6 +12840,10 @@ return sAction.indexOf("ppaction://hlink") === 0; } + function generateHashParams() { + return {spinCount: 100000, saltValue: AscCommon.randomBytes(16).base64()}; + } + //------------------------------------------------------------export--------------------------------------------------- window['AscCommon'] = window['AscCommon'] || {}; window["AscCommon"].getSockJs = getSockJs; @@ -13017,6 +13021,7 @@ window['AscCommon'].g_oCRC32 = g_oCRC32; window["AscCommon"].RangeTopBottomIterator = RangeTopBottomIterator; window["AscCommon"].IsLinkPPAction = IsLinkPPAction; + window["AscCommon"].generateHashParams = generateHashParams; })(window); window["asc_initAdvancedOptions"] = function(_code, _file_hash, _docInfo) diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 11b20ef22a..498011970a 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -44,14 +44,6 @@ var ECryptAlgoritmName = { SHA_512: 9, WHIRLPOOL: 10 }; - -var EDocProtect = { - Comments: 0, - Forms: 1, - None: 2, - ReadOnly: 3, - TrackedChanges: 4 -}; var ECryptAlgClass = { Custom: 0, Hash: 1 @@ -86,11 +78,39 @@ function CDocProtect() { this.cryptProviderTypeExtSource = null; } CDocProtect.prototype.isOnlyView = function () { - return this.edit === EDocProtect.ReadOnly; + return this.edit === Asc.c_oAscEDocProtect.ReadOnly; }; CDocProtect.prototype.getEnforcment = function () { return this.enforcment; }; +CDocProtect.prototype.getRestrictionType = function () { + var res = null; + switch (this.edit) { + case Asc.c_oAscEDocProtect.Comments: + res = Asc.c_oAscRestrictionType.OnlyComments; + break; + case Asc.c_oAscEDocProtect.Forms: + res = Asc.c_oAscRestrictionType.OnlyForms; + break; + case Asc.c_oAscEDocProtect.ReadOnly: + res = Asc.c_oAscRestrictionType.View; + break; + case Asc.c_oAscEDocProtect.TrackedChanges: + //asc_SetLocalTrackRevisions + //asc_SetGlobalTrackRevisions + res = Asc.c_oAscRestrictionType.OnlySignatures; + break; + } + return res; +}; +CDocProtect.prototype.generateHashParams = function () { + var params = AscCommon.generateHashParams(); + + this.saltValue = params.saltValue; + this.spinCount = params.spinCount; + //this.algorithmName = params.algorithmName; +}; + function CWriteProtection() { this.algorithmName = null; diff --git a/word/Editor/Serialize2.js b/word/Editor/Serialize2.js index 75521a7bbb..43d76ac1f4 100644 --- a/word/Editor/Serialize2.js +++ b/word/Editor/Serialize2.js @@ -8380,7 +8380,12 @@ function BinaryFileReader(doc, openParams) var docProtection = this.Document.Settings && this.Document.Settings.DocumentProtection; if (docProtection) { - if (docProtection.isOnlyView() && false !== docProtection.getEnforcment()) { + //this.Document.applyProtection() + var restrictionType = docProtection.getRestrictionType(); + if (/*docProtection.isOnlyView() && false !== docProtection.getEnforcment()*/restrictionType !== null) { + api && api.asc_addRestriction(restrictionType); + } else if (false !== docProtection.getEnforcment() ) { + //TODO ? api && api.asc_addRestriction(Asc.c_oAscRestrictionType.View); } } @@ -16292,7 +16297,7 @@ function Binary_SettingsTableReader(doc, oReadResult, stream) res = this.bcr.Read1(length, function(t, l){ return oThis.ReadDocProtect(t,l,oDocProtect); }); - //Settings.DocumentProtection = oDocProtect; + Settings.DocumentProtection = oDocProtect; } else if ( c_oSer_SettingsType.WriteProtection === type ) { diff --git a/word/Editor/SerializeXml.js b/word/Editor/SerializeXml.js index 52739586d2..057aa0803d 100644 --- a/word/Editor/SerializeXml.js +++ b/word/Editor/SerializeXml.js @@ -11028,29 +11028,29 @@ function fromXml_ST_DocProtect(val, def) { switch (val) { case "comments": - return EDocProtect.Comments; + return Asc.c_oAscEDocProtect.Comments; case "forms": - return EDocProtect.Forms; + return Asc.c_oAscEDocProtect.Forms; case "none": - return EDocProtect.None; + return Asc.c_oAscEDocProtect.None; case "readOnly": - return EDocProtect.ReadOnly; + return Asc.c_oAscEDocProtect.ReadOnly; case "trackedChanges": - return EDocProtect.TrackedChanges; + return Asc.c_oAscEDocProtect.TrackedChanges; } return def; } function toXml_ST_DocProtect(val) { switch (val) { - case EDocProtect.Comments: + case Asc.c_oAscEDocProtect.Comments: return "comments"; - case EDocProtect.Forms: + case Asc.c_oAscEDocProtect.Forms: return "forms"; - case EDocProtect.None: + case Asc.c_oAscEDocProtect.None: return "none"; - case EDocProtect.ReadOnly: + case Asc.c_oAscEDocProtect.ReadOnly: return "readOnly"; - case EDocProtect.TrackedChanges: + case Asc.c_oAscEDocProtect.TrackedChanges: return "trackedChanges"; } return null; diff --git a/word/api.js b/word/api.js index 8fb7f66b6f..5084580bad 100644 --- a/word/api.js +++ b/word/api.js @@ -12791,6 +12791,107 @@ background-repeat: no-repeat;\ return oDocument.PutImageToSelection(sImageSrc, nWidth, nHeight); }; + //asc_onChangeProtectWorkbook + asc_docs_api.prototype.asc_getDocumentProtection = function () { + let oDocument = this.private_GetLogicDocument(); + if (!oDocument) { + return; + } + + var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; + if (docProtection) { + return docProtection.edit; + } + }; + + asc_docs_api.prototype.asc_setDocumentProtection = function (props, password) { + //props -> c_oAscEDocProtect + + // Проверка глобального лока + /*if (this.collaborativeEditing.getGlobalLock() || !this.canEdit()) { + return false; + }*/ + + let oDocument = this.private_GetLogicDocument(); + if (!oDocument) { + return; + } + var t = this; + + var callback = function (res) { + t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); + + if (res) { + /*History.Create_NewPoint(); + History.StartTransaction(); + if (!t.wbModel.getWorksheet(i).setProtectedSheet(props, true)) { + t.handlers.trigger("asc_onError", c_oAscError.ID.LockedWorksheetRename, + c_oAscError.Level.NoCritical); + } else if (wsView) { + wsView.updateAfterChangeSheetProtection(); + } + t.handlers.trigger("asc_onChangeDocumentProtection"); + + History.EndTransaction();*/ + } else { + //t.handlers.trigger("asc_onError", c_oAscError.ID.LockedWorksheetRename, c_oAscError.Level.NoCritical); + } + }; + + var documentProtection = oDocument.Settings.DocumentProtection; + if (!documentProtection) { + documentProtection = new CDocProtect(); + documentProtection.generateHashParams(); + } + + var checkPassword = function (hash, doNotCheckPassword) { + if (doNotCheckPassword) { + //t.collaborativeEditing.lock([lockInfo], callback); + callback(); + } else { + if (props) { + documentProtection.hashValue = hash && hash[0] ? hash[0] : null; + //t.collaborativeEditing.lock([lockInfo], callback); + callback(); + } else { + if (hash && hash[0] === documentProtection.hashValue) { + documentProtection.hashValue = null; + documentProtection.saltValue = null; + documentProtection.spinCount = null; + documentProtection.algorithmName = null; + //t.collaborativeEditing.lock([lockInfo], callback); + callback(); + } else { + //неверный пароль + t.handlers.trigger("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, + c_oAscError.Level.NoCritical); + t.handlers.trigger("asc_onChangeDocumentProtection"); + t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); + } + } + props.temporaryPassword = null; + } + }; + + this.sync_StartAction(Asc.c_oAscAsyncActionType.BlockInteraction); + //props !== null && props !== Asc.c_oAscEDocProtect.None + + if (password != null) { + if (password === "") { + checkPassword([""]); + } else { + var checkHash = {password: password, salt: documentProtection.saltValue, spinCount: documentProtection.spinCount, + alg: props.algorithmName}; + AscCommon.calculateProtectHash([checkHash], checkPassword); + } + } else { + checkPassword(null, true); + } + + return true; + }; + + //-------------------------------------------------------------export--------------------------------------------------- window['Asc'] = window['Asc'] || {}; CAscSection.prototype['get_PageWidth'] = CAscSection.prototype.get_PageWidth; diff --git a/word/apiDefines.js b/word/apiDefines.js index f37dce0623..c1edd43989 100644 --- a/word/apiDefines.js +++ b/word/apiDefines.js @@ -876,6 +876,14 @@ c_oAscDateTimeFormat[lcid_enUS] = [ "HH:mm:ss" ]; +var c_oAscEDocProtect = { + Comments: 0, + Forms: 1, + None: 2, + ReadOnly: 3, + TrackedChanges: 4 +}; + //------------------------------------------------------------export--------------------------------------------------- var prot; window['Asc'] = window['Asc'] || {}; @@ -1057,3 +1065,10 @@ prot['Content'] = prot.Content; prot['Window'] = prot.Window; window['Asc']['c_oAscDateTimeFormat'] = window['Asc'].c_oAscDateTimeFormat = c_oAscDateTimeFormat; + +prot = window['Asc']['c_oAscEDocProtect'] = window['Asc'].c_oAscEDocProtect = c_oAscEDocProtect; +prot['Comments'] = prot.Comments; +prot['Forms'] = prot.Forms; +prot['None'] = prot.None; +prot['ReadOnly'] = prot.ReadOnly; +prot['TrackedChanges'] = prot.TrackedChanges; From f455bf0cf68d539c13d2e70b5cfd43ac5b5b3162 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 30 Sep 2022 15:50:29 +0300 Subject: [PATCH 02/26] [de] Export --- word/api.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/word/api.js b/word/api.js index 5084580bad..5a95d2790c 100644 --- a/word/api.js +++ b/word/api.js @@ -13650,6 +13650,8 @@ background-repeat: no-repeat;\ asc_docs_api.prototype["asc_editPointsGeometry"] = asc_docs_api.prototype.asc_editPointsGeometry; asc_docs_api.prototype["asc_getTableStylesPreviews"] = asc_docs_api.prototype.asc_getTableStylesPreviews; asc_docs_api.prototype["asc_generateTableStylesPreviews"] = asc_docs_api.prototype.asc_generateTableStylesPreviews; + asc_docs_api.prototype["asc_getDocumentProtection"] = asc_docs_api.prototype.asc_getDocumentProtection; + asc_docs_api.prototype["asc_setDocumentProtection"] = asc_docs_api.prototype.asc_setDocumentProtection; CDocInfoProp.prototype['get_PageCount'] = CDocInfoProp.prototype.get_PageCount; CDocInfoProp.prototype['put_PageCount'] = CDocInfoProp.prototype.put_PageCount; From f159b8a1cb364ec4a6f64246c31a5fcbf11f20ec Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 30 Sep 2022 17:33:40 +0300 Subject: [PATCH 03/26] [de] Protect --- cell/api.js | 6 +- cell/model/Workbook.js | 2 +- cell/model/WorkbookProtection.js | 97 ++++++++---------------------- common/SerializeCommonWordExcel.js | 33 ++++++++++ common/editorscommon.js | 77 ++++++++++++++++++++++++ word/Editor/DocumentProtection.js | 16 +++++ word/api.js | 3 +- 7 files changed, 155 insertions(+), 79 deletions(-) diff --git a/cell/api.js b/cell/api.js index 774433ec14..23a3ffc7d6 100644 --- a/cell/api.js +++ b/cell/api.js @@ -6853,7 +6853,7 @@ var editor; password: arr[i].temporaryPassword, salt: arr[i].saltValue, spinCount: arr[i].spinCount, - alg: AscCommonExcel.fromModelAlgoritmName(arr[i].algorithmName) + alg: AscCommon.fromModelAlgorithmName(arr[i].algorithmName) }); } } @@ -7082,7 +7082,7 @@ var editor; checkPassword([AscCommonExcel.getPasswordHash(props.temporaryPassword, true)]); } else { var checkHash = {password: props.temporaryPassword, salt: props.saltValue, spinCount: props.spinCount, - alg: AscCommonExcel.fromModelAlgoritmName(props.algorithmName)}; + alg: AscCommon.fromModelAlgorithmName(props.algorithmName)}; AscCommon.calculateProtectHash([checkHash], checkPassword); } } else { @@ -7197,7 +7197,7 @@ var editor; checkPassword([AscCommonExcel.getPasswordHash(props.temporaryPassword, true)]); } else { var checkHash = {password: props.temporaryPassword, salt: props.workbookSaltValue, spinCount: props.workbookSpinCount, - alg: AscCommonExcel.fromModelAlgoritmName(props.workbookAlgorithmName)}; + alg: AscCommon.fromModelAlgorithmName(props.algorithmName)}; AscCommon.calculateProtectHash([checkHash], checkPassword); } } else { diff --git a/cell/model/Workbook.js b/cell/model/Workbook.js index 300991ffef..f714fc04c9 100644 --- a/cell/model/Workbook.js +++ b/cell/model/Workbook.js @@ -10709,7 +10709,7 @@ password: val, salt: this.aProtectedRanges[i].saltValue, spinCount: this.aProtectedRanges[i].spinCount, - alg: AscCommonExcel.fromModelAlgoritmName(this.aProtectedRanges[i].algorithmName) + alg: AscCommon.fromModelAlgorithmName(this.aProtectedRanges[i].algorithmName) }); } } diff --git a/cell/model/WorkbookProtection.js b/cell/model/WorkbookProtection.js index 07af2fb1c7..e9eeb042d3 100644 --- a/cell/model/WorkbookProtection.js +++ b/cell/model/WorkbookProtection.js @@ -52,86 +52,38 @@ this.pivotTables = true; this.selectUnlockedCells = false;*/ - var c_oSerProtectedAlgorithmNameTypes = { - MD2: 1, - MD4: 2, - MD5: 3, - RIPEMD_128: 4, - RIPEMD_160: 5, - SHA_1: 6, - SHA_256: 7, - SHA_384: 8, - SHA_512: 9, - WHIRLPOOL: 10 - }; - - function fromModelAlgoritmName(alg) { - switch (alg) { - case c_oSerProtectedAlgorithmNameTypes.MD2 : - alg = AscCommon.HashAlgs.MD2; - break; - case c_oSerProtectedAlgorithmNameTypes.MD4 : - alg = AscCommon.HashAlgs.MD4; - break; - case c_oSerProtectedAlgorithmNameTypes.MD5 : - alg = AscCommon.HashAlgs.MD5; - break; - case c_oSerProtectedAlgorithmNameTypes.RIPEMD_160 : - alg = AscCommon.HashAlgs.RMD160; - break; - case c_oSerProtectedAlgorithmNameTypes.SHA_1 : - alg = AscCommon.HashAlgs.SHA1; - break; - case c_oSerProtectedAlgorithmNameTypes.SHA_256 : - alg = AscCommon.HashAlgs.SHA256; - break; - case c_oSerProtectedAlgorithmNameTypes.SHA_384 : - alg = AscCommon.HashAlgs.SHA384; - break; - case c_oSerProtectedAlgorithmNameTypes.SHA_512 : - alg = AscCommon.HashAlgs.SHA512; - break; - case c_oSerProtectedAlgorithmNameTypes.WHIRLPOOL : - alg = AscCommon.HashAlgs.WHIRLPOOL; - break; - default: - alg = AscCommon.HashAlgs.SHA256; - } - return alg; - } - function FromXml_ST_AlgorithmName(str) { var alg = null; switch (str) { case "MD2" : - alg = c_oSerProtectedAlgorithmNameTypes.MD2; + alg = AscCommon.c_oSerAlgorithmNameTypes.MD2; break; case "MD4" : - alg = c_oSerProtectedAlgorithmNameTypes.MD4; + alg = AscCommon.c_oSerAlgorithmNameTypes.MD4; break; case "MD5" : - alg = c_oSerProtectedAlgorithmNameTypes.MD5; + alg = AscCommon.c_oSerAlgorithmNameTypes.MD5; break; case "RIPEMD-128" : - alg = c_oSerProtectedAlgorithmNameTypes.RIPEMD_128; + alg = AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_128; break; case "RIPEMD-160" : - alg = c_oSerProtectedAlgorithmNameTypes.RIPEMD_160; + alg = AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_160; break; case "SHA-1" : - alg = c_oSerProtectedAlgorithmNameTypes.SHA1; + alg = AscCommon.c_oSerAlgorithmNameTypes.SHA1; break; case "SHA-256" : - alg = c_oSerProtectedAlgorithmNameTypes.SHA_256; + alg = AscCommon.c_oSerAlgorithmNameTypes.SHA_256; break; case "SHA-384" : - alg = c_oSerProtectedAlgorithmNameTypes.SHA_384; + alg = AscCommon.c_oSerAlgorithmNameTypes.SHA_384; break; case "SHA-512" : - alg = c_oSerProtectedAlgorithmNameTypes.SHA_512; + alg = AscCommon.c_oSerAlgorithmNameTypes.SHA_512; break; case "WHIRLPOOL" : - alg = c_oSerProtectedAlgorithmNameTypes.WHIRLPOOL; + alg = AscCommon.c_oSerAlgorithmNameTypes.WHIRLPOOL; break; } return alg; @@ -140,34 +92,34 @@ function ToXml_ST_AlgorithmName(alg) { var str = null; switch (alg) { - case c_oSerProtectedAlgorithmNameTypes.MD2: + case AscCommon.c_oSerAlgorithmNameTypes.MD2: str = "MD2"; break; - case c_oSerProtectedAlgorithmNameTypes.MD4: + case AscCommon.c_oSerAlgorithmNameTypes.MD4: str = "MD4"; break; - case c_oSerProtectedAlgorithmNameTypes.MD5: + case AscCommon.c_oSerAlgorithmNameTypes.MD5: str = "MD5"; break; - case c_oSerProtectedAlgorithmNameTypes.RIPEMD_128: + case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_128: str = "RIPEMD-128"; break; - case c_oSerProtectedAlgorithmNameTypes.RIPEMD_160: + case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_160: str = "RIPEMD-160"; break; - case c_oSerProtectedAlgorithmNameTypes.SHA1: + case AscCommon.c_oSerAlgorithmNameTypes.SHA1: str = "SHA-1"; break; - case c_oSerProtectedAlgorithmNameTypes.SHA_256 : + case AscCommon.c_oSerAlgorithmNameTypes.SHA_256 : str = "SHA-256"; break; - case c_oSerProtectedAlgorithmNameTypes.SHA_384 : + case AscCommon.c_oSerAlgorithmNameTypes.SHA_384 : str = "SHA-384"; break; - case c_oSerProtectedAlgorithmNameTypes.SHA_512: + case AscCommon.c_oSerAlgorithmNameTypes.SHA_512: str = "SHA-512"; break; - case c_oSerProtectedAlgorithmNameTypes.WHIRLPOOL: + case AscCommon.c_oSerAlgorithmNameTypes.WHIRLPOOL: str = "WHIRLPOOL"; break; } @@ -571,7 +523,7 @@ var hashParams = AscCommon.generateHashParams(); this.saltValue = hashParams.saltValue; this.spinCount = hashParams.spinCount; - this.algorithmName = c_oSerProtectedAlgorithmNameTypes.SHA_512; + this.algorithmName = AscCommon.c_oSerAlgorithmNameTypes.SHA_512; } this.temporaryPassword = password; if (callback) { @@ -875,7 +827,7 @@ var hashParams = AscCommon.generateHashParams(); this.workbookSaltValue = hashParams.saltValue; this.workbookSpinCount = hashParams.spinCount; - this.workbookAlgorithmName = c_oSerProtectedAlgorithmNameTypes.SHA_512; + this.workbookAlgorithmName = AscCommon.c_oSerAlgorithmNameTypes.SHA_512; } this.temporaryPassword = password; if (callback) { @@ -1314,7 +1266,7 @@ var hashParams = AscCommon.generateHashParams(); this.saltValue = hashParams.saltValue; this.spinCount = hashParams.spinCount; - this.algorithmName = c_oSerProtectedAlgorithmNameTypes.SHA_512; + this.algorithmName = AscCommon.c_oSerAlgorithmNameTypes.SHA_512; } //генерируем хэш this.temporaryPassword = val; @@ -1326,7 +1278,7 @@ return this.isLock; }; CProtectedRange.prototype.asc_checkPassword = function (val, callback) { - var checkHash = {password: val, salt: this.saltValue, spinCount: this.spinCount, alg: fromModelAlgoritmName(this.algorithmName)}; + var checkHash = {password: val, salt: this.saltValue, spinCount: this.spinCount, alg: AscCommon.fromModelAlgorithmName(this.algorithmName)}; AscCommon.calculateProtectHash([checkHash], function (hash) { callback(hash && hash[0] === this.hashValue); }); @@ -1434,7 +1386,6 @@ prot["asc_checkPassword"] = prot.asc_checkPassword; prot["asc_getId"] = prot.asc_getId; - window["AscCommonExcel"].fromModelAlgoritmName = fromModelAlgoritmName; window["AscCommonExcel"].getPasswordHash = getPasswordHash; window["AscCommonExcel"].FromXml_ST_AlgorithmName = FromXml_ST_AlgorithmName; window["AscCommonExcel"].ToXml_ST_AlgorithmName = ToXml_ST_AlgorithmName; diff --git a/common/SerializeCommonWordExcel.js b/common/SerializeCommonWordExcel.js index 7c51a4b7c9..9366e6a9ab 100644 --- a/common/SerializeCommonWordExcel.js +++ b/common/SerializeCommonWordExcel.js @@ -135,6 +135,35 @@ var c_oSerShdType = { nodeAttributeEnd: 0xFB }; + var c_oSerAlgorithmNameTypes = { + MD2: 1, + MD4: 2, + MD5: 3, + RIPEMD_128: 4, + RIPEMD_160: 5, + SHA_1: 6, + SHA_256: 7, + SHA_384: 8, + SHA_512: 9, + WHIRLPOOL: 10 + }; + + var c_oSerCryptAlgorithmSid = { + MD2: 1, + MD4: 2, + MD5: 3, + SHA_1: 4, + MAC: 5, + RIPEMD: 6, + RIPEMD_160: 7, + //SHA_384: 8, + HMAC: 9, + SHA_256: 12, + SHA_384: 13, + SHA_512: 14, + }; + + function BinaryCommonWriter(memory) { this.memory = memory; @@ -1532,4 +1561,8 @@ function isRealObject(obj) window['AscCommon'].GetStringUtf8 = GetStringUtf8; window['AscCommon'].g_nodeAttributeStart = c_nodeAttribute.nodeAttributeStart; window['AscCommon'].g_nodeAttributeEnd = c_nodeAttribute.nodeAttributeEnd; + window['AscCommon'].c_oSerAlgorithmNameTypes = c_oSerAlgorithmNameTypes; + window['AscCommon'].c_oSerCryptAlgorithmSid = c_oSerCryptAlgorithmSid; + + })(window); diff --git a/common/editorscommon.js b/common/editorscommon.js index e498b96128..7ade16afa0 100644 --- a/common/editorscommon.js +++ b/common/editorscommon.js @@ -12844,6 +12844,81 @@ return {spinCount: 100000, saltValue: AscCommon.randomBytes(16).base64()}; } + function fromModelAlgorithmName(alg) { + switch (alg) { + case AscCommon.c_oSerAlgorithmNameTypes.MD2 : + alg = AscCommon.HashAlgs.MD2; + break; + case AscCommon.c_oSerAlgorithmNameTypes.MD4 : + alg = AscCommon.HashAlgs.MD4; + break; + case AscCommon.c_oSerAlgorithmNameTypes.MD5 : + alg = AscCommon.HashAlgs.MD5; + break; + case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_160 : + alg = AscCommon.HashAlgs.RMD160; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_1 : + alg = AscCommon.HashAlgs.SHA1; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_256 : + alg = AscCommon.HashAlgs.SHA256; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_384 : + alg = AscCommon.HashAlgs.SHA384; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_512 : + alg = AscCommon.HashAlgs.SHA512; + break; + case AscCommon.c_oSerAlgorithmNameTypes.WHIRLPOOL : + alg = AscCommon.HashAlgs.WHIRLPOOL; + break; + default: + alg = AscCommon.HashAlgs.SHA256; + } + return alg; + } + + function fromModelCryptAlgorithmSid(alg) { + var res = null; + switch (alg) { + case AscCommon.c_oSerCryptAlgorithmSid.MD2 : + res = AscCommon.HashAlgs.MD2; + break; + case AscCommon.c_oSerCryptAlgorithmSid.MD4 : + res = AscCommon.HashAlgs.MD4; + break; + case AscCommon.c_oSerCryptAlgorithmSid.MD5 : + res = AscCommon.HashAlgs.MD5; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_1 : + res = AscCommon.HashAlgs.SHA1; + break; + case AscCommon.c_oSerAlgorithmNameTypes.MAC : + //alg = AscCommon.HashAlgs.SHA1; + break; + case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD : + //alg = AscCommon.HashAlgs.SHA256; + break; + case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_160 : + //alg = AscCommon.HashAlgs.SHA384; + break; + case AscCommon.c_oSerAlgorithmNameTypes.HMAC : + //alg = AscCommon.HashAlgs.SHA512; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_256 : + res = AscCommon.HashAlgs.SHA256; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_384 : + res = AscCommon.HashAlgs.SHA384; + break; + case AscCommon.c_oSerAlgorithmNameTypes.SHA_512 : + res = AscCommon.HashAlgs.SHA512; + break; + } + return res; + } + //------------------------------------------------------------export--------------------------------------------------- window['AscCommon'] = window['AscCommon'] || {}; window["AscCommon"].getSockJs = getSockJs; @@ -13022,6 +13097,8 @@ window["AscCommon"].RangeTopBottomIterator = RangeTopBottomIterator; window["AscCommon"].IsLinkPPAction = IsLinkPPAction; window["AscCommon"].generateHashParams = generateHashParams; + window["AscCommon"].fromModelAlgorithmName = fromModelAlgorithmName; + window["AscCommon"].fromModelCryptAlgorithmSid = fromModelCryptAlgorithmSid; })(window); window["asc_initAdvancedOptions"] = function(_code, _file_hash, _docInfo) diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 498011970a..93eebdacd5 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -110,6 +110,22 @@ CDocProtect.prototype.generateHashParams = function () { this.spinCount = params.spinCount; //this.algorithmName = params.algorithmName; }; +CDocProtect.prototype.generateHashParams = function () { + var params = AscCommon.generateHashParams(); + + this.saltValue = params.saltValue; + this.spinCount = params.spinCount; + //this.algorithmName = params.algorithmName; +}; +CDocProtect.prototype.getAlgorithmNameForCheck = function () { + if (this.algorithmName) { + return AscCommon.fromModelAlgorithmName(this.algorithmName); + } else if (this.cryptAlgorithmSid) { + return AscCommon.fromModelCryptAlgorithmSid(this.cryptAlgorithmSid); + } + return null; +}; + function CWriteProtection() { diff --git a/word/api.js b/word/api.js index 5a95d2790c..4863f97b92 100644 --- a/word/api.js +++ b/word/api.js @@ -12880,8 +12880,7 @@ background-repeat: no-repeat;\ if (password === "") { checkPassword([""]); } else { - var checkHash = {password: password, salt: documentProtection.saltValue, spinCount: documentProtection.spinCount, - alg: props.algorithmName}; + var checkHash = {password: password, salt: documentProtection.saltValue, spinCount: documentProtection.spinCount, alg: documentProtection.getAlgorithmNameForCheck()}; AscCommon.calculateProtectHash([checkHash], checkPassword); } } else { From 1de28a15ce36a97d620a1bb2605dd6c1768743b1 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Mon, 3 Oct 2022 12:37:35 +0300 Subject: [PATCH 04/26] [de] Fix --- word/api.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/word/api.js b/word/api.js index 4863f97b92..4205442bf4 100644 --- a/word/api.js +++ b/word/api.js @@ -12799,6 +12799,7 @@ background-repeat: no-repeat;\ } var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; + //TODO enforcment!!! - проверить данный флаг if (docProtection) { return docProtection.edit; } @@ -12834,7 +12835,7 @@ background-repeat: no-repeat;\ History.EndTransaction();*/ } else { - //t.handlers.trigger("asc_onError", c_oAscError.ID.LockedWorksheetRename, c_oAscError.Level.NoCritical); + t.handlers.trigger("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); } }; From 37405b8c27d4b4f9b04585a5c688da30885f327a Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Mon, 3 Oct 2022 12:50:35 +0300 Subject: [PATCH 05/26] [de] Protect --- word/Editor/DocumentProtection.js | 3 +++ word/api.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 93eebdacd5..37187e790b 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -125,6 +125,9 @@ CDocProtect.prototype.getAlgorithmNameForCheck = function () { } return null; }; +CDocProtect.prototype.isPassword = function () { + return this.algorithmName != null || this.cryptAlgorithmSid != null; +}; diff --git a/word/api.js b/word/api.js index 4205442bf4..67187a113c 100644 --- a/word/api.js +++ b/word/api.js @@ -12801,7 +12801,7 @@ background-repeat: no-repeat;\ var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; //TODO enforcment!!! - проверить данный флаг if (docProtection) { - return docProtection.edit; + return [docProtection.edit, docProtection.isPassword()]; } }; From 3eb617d44dc682a42e5242c63ff80eb64c731642 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Tue, 4 Oct 2022 10:58:02 +0300 Subject: [PATCH 06/26] [de] Add hash.js --- configs/word.json | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/word.json b/configs/word.json index e8b0787c30..9c16772ebb 100644 --- a/configs/word.json +++ b/configs/word.json @@ -54,6 +54,7 @@ "common/stringserialize.js", "common/random.js", + "common/hash/hash.js", "common/Drawings/Metafile.js", "common/libfont/textmeasurer.js", From eacb9be3c1dc428094974aa74ea9c4e0c6dc9d7e Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Tue, 4 Oct 2022 12:11:34 +0300 Subject: [PATCH 07/26] [de] Protect --- word/api.js | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/word/api.js b/word/api.js index 67187a113c..65e58849c2 100644 --- a/word/api.js +++ b/word/api.js @@ -12819,10 +12819,21 @@ background-repeat: no-repeat;\ } var t = this; + var calculatedHashValue; var callback = function (res) { t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); if (res) { + if (props) { + //устанавливаем + documentProtection.hashValue = calculatedHashValue; + } else { + //снимаем + documentProtection.hashValue = null; + documentProtection.saltValue = null; + documentProtection.spinCount = null; + documentProtection.algorithmName = null; + } /*History.Create_NewPoint(); History.StartTransaction(); if (!t.wbModel.getWorksheet(i).setProtectedSheet(props, true)) { @@ -12835,42 +12846,45 @@ background-repeat: no-repeat;\ History.EndTransaction();*/ } else { - t.handlers.trigger("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); + t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); } }; var documentProtection = oDocument.Settings.DocumentProtection; - if (!documentProtection) { - documentProtection = new CDocProtect(); - documentProtection.generateHashParams(); + var salt, alg, spinCount; + if (documentProtection) { + salt = documentProtection.saltValue; + spinCount = documentProtection.spinCount; + alg = documentProtection.cryptAlgorithmSid; + } else { + var params = AscCommon.generateHashParams(); + salt = params.saltValue; + spinCount = params.spinCount; + alg = AscCommon.c_oSerCryptAlgorithmSid.SHA_512; } var checkPassword = function (hash, doNotCheckPassword) { if (doNotCheckPassword) { //t.collaborativeEditing.lock([lockInfo], callback); - callback(); + callback(true); } else { if (props) { - documentProtection.hashValue = hash && hash[0] ? hash[0] : null; + //устанавливаем защиту + calculatedHashValue = hash && hash[0] ? hash[0] : null; //t.collaborativeEditing.lock([lockInfo], callback); - callback(); + callback(true); } else { - if (hash && hash[0] === documentProtection.hashValue) { - documentProtection.hashValue = null; - documentProtection.saltValue = null; - documentProtection.spinCount = null; - documentProtection.algorithmName = null; + //пробуем снять защиту + if (documentProtection && hash && hash[0] === documentProtection.hashValue) { //t.collaborativeEditing.lock([lockInfo], callback); - callback(); + callback(true); } else { //неверный пароль - t.handlers.trigger("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, - c_oAscError.Level.NoCritical); - t.handlers.trigger("asc_onChangeDocumentProtection"); + t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); + t.sendEvent("asc_onChangeDocumentProtection"); t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); } } - props.temporaryPassword = null; } }; @@ -12881,7 +12895,7 @@ background-repeat: no-repeat;\ if (password === "") { checkPassword([""]); } else { - var checkHash = {password: password, salt: documentProtection.saltValue, spinCount: documentProtection.spinCount, alg: documentProtection.getAlgorithmNameForCheck()}; + var checkHash = {password: password, salt: salt, spinCount: spinCount, alg: AscCommon.fromModelCryptAlgorithmSid(alg)}; AscCommon.calculateProtectHash([checkHash], checkPassword); } } else { From 829984f71fda973727482d428e77718a1dd350f8 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Tue, 4 Oct 2022 13:51:36 +0300 Subject: [PATCH 08/26] [de] Protect --- common/HistoryCommon.js | 1 + word/Editor/Document.js | 8 ++++++++ word/Editor/DocumentProtection.js | 20 +++++++++++++++++++- word/api.js | 19 +++++++++++++++++-- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/common/HistoryCommon.js b/common/HistoryCommon.js index 763409b869..e631042631 100644 --- a/common/HistoryCommon.js +++ b/common/HistoryCommon.js @@ -4320,6 +4320,7 @@ window['AscDFH'].historydescription_Document_AddComplexForm = 0x0195; window['AscDFH'].historydescription_Document_CorrectFormTextByFormat = 0x0196; window['AscDFH'].historydescription_Document_ConvertMathView = 0x0197; + window['AscDFH'].historydescription_Document_SetDocumentProtection = 0x0198; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/word/Editor/Document.js b/word/Editor/Document.js index 4162f1bdc0..e3d86397ed 100644 --- a/word/Editor/Document.js +++ b/word/Editor/Document.js @@ -16318,6 +16318,14 @@ CDocument.prototype.IsSpecialFormsSettingsDefault = function() { return this.Settings.SpecialFormsSettings.IsDefault(); }; +CDocument.prototype.SetProtection = function(props) +{ + if (!this.Settings.DocumentProtection) { + this.Settings.DocumentProtection = new CDocProtect(); + } + return this.Settings.DocumentProtection.setProps(props); +}; + CDocument.prototype.GetDocumentLayout = function() { return this.Layout; diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 37187e790b..44a9158e20 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -59,6 +59,15 @@ var ECryptProv = { }; function CDocProtect() { + this.Id = AscCommon.g_oIdCounter.Get_NewId(); + + /*this.Lock = new AscCommon.CLock(); // Зажат ли комментарий другим пользователем + if (false === AscCommon.g_oIdCounter.m_bLoad) + { + this.Lock.Set_Type(AscCommon.locktype_Mine, false); + AscCommon.CollaborativeEditing.Add_Unlock2(this); + }*/ + this.algorithmName = null; this.edit = null; this.enforcment = null; @@ -76,6 +85,9 @@ function CDocProtect() { this.cryptProviderType = null; this.cryptProviderTypeExt = null; this.cryptProviderTypeExtSource = null; + + // Добавляем данный класс в таблицу Id (обязательно в конце конструктора) + AscCommon.g_oTableId.Add(this, this.Id); } CDocProtect.prototype.isOnlyView = function () { return this.edit === Asc.c_oAscEDocProtect.ReadOnly; @@ -128,7 +140,13 @@ CDocProtect.prototype.getAlgorithmNameForCheck = function () { CDocProtect.prototype.isPassword = function () { return this.algorithmName != null || this.cryptAlgorithmSid != null; }; - +CDocProtect.prototype.setProps = function (oProps) { + History.Add(new CChangesCorePr(this, this, oProps, null)); + this.title = oProps.title; + this.creator = oProps.creator; + this.description = oProps.description; + this.subject = oProps.subject; +}; function CWriteProtection() { diff --git a/word/api.js b/word/api.js index 65e58849c2..79a9d28f26 100644 --- a/word/api.js +++ b/word/api.js @@ -12824,8 +12824,21 @@ background-repeat: no-repeat;\ t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); if (res) { - if (props) { + + + + + + oDocument.StartAction(AscDFH.historydescription_Document_SetDocumentProtection); + + //this.SetCheckBoxChecked(false); + + oDocument.SetProtection(props, salt, spinCount, alg, calculatedHashValue); + + + /*if (props) { //устанавливаем + oDocument.SetProtection(props, salt, spinCount, alg, calculatedHashValue); documentProtection.hashValue = calculatedHashValue; } else { //снимаем @@ -12833,7 +12846,7 @@ background-repeat: no-repeat;\ documentProtection.saltValue = null; documentProtection.spinCount = null; documentProtection.algorithmName = null; - } + }*/ /*History.Create_NewPoint(); History.StartTransaction(); if (!t.wbModel.getWorksheet(i).setProtectedSheet(props, true)) { @@ -12845,6 +12858,8 @@ background-repeat: no-repeat;\ t.handlers.trigger("asc_onChangeDocumentProtection"); History.EndTransaction();*/ + + oDocument.FinalizeAction(); } else { t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); } From 79ae055d3295853d2af94615cb40acbf52d8e12f Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Wed, 5 Oct 2022 16:11:42 +0300 Subject: [PATCH 09/26] [se] Add protection into history --- common/HistoryCommon.js | 3 +- common/TableId.js | 1 + word/Editor/DocumentChanges.js | 325 ++++++++++++++++++++++++++++++ word/Editor/DocumentProtection.js | 59 +++++- word/api.js | 38 +--- 5 files changed, 385 insertions(+), 41 deletions(-) diff --git a/common/HistoryCommon.js b/common/HistoryCommon.js index e631042631..9a6184cbf9 100644 --- a/common/HistoryCommon.js +++ b/common/HistoryCommon.js @@ -1665,6 +1665,7 @@ window['AscDFH'].historyitem_type_VMLShapeType = 2109 << 16; window['AscDFH'].historyitem_type_VMLClientData = 2110 << 16; + window['AscDFH'].historyitem_type_DocumentProtection = 2111 << 16; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -4320,7 +4321,7 @@ window['AscDFH'].historydescription_Document_AddComplexForm = 0x0195; window['AscDFH'].historydescription_Document_CorrectFormTextByFormat = 0x0196; window['AscDFH'].historydescription_Document_ConvertMathView = 0x0197; - window['AscDFH'].historydescription_Document_SetDocumentProtection = 0x0198; + window['AscDFH'].historydescription_Document_DocumentProtection = 0x0198; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/common/TableId.js b/common/TableId.js index 633c5673ef..77c90e852b 100644 --- a/common/TableId.js +++ b/common/TableId.js @@ -381,6 +381,7 @@ this.m_oFactoryClass[AscDFH.historyitem_type_SmartArtNode ] = AscFormat.SmartArtNode; this.m_oFactoryClass[AscDFH.historyitem_type_SmartArtNodeData ] = AscFormat.SmartArtNodeData; this.m_oFactoryClass[AscDFH.historyitem_type_BuBlip ] = AscFormat.CBuBlip; + this.m_oFactoryClass[AscDFH.historyitem_type_DocumentProtection] = AscCommonWord.CDocProtect; if (window['AscCommonSlide']) { diff --git a/word/Editor/DocumentChanges.js b/word/Editor/DocumentChanges.js index 036c2cc8e9..81e0a1751c 100644 --- a/word/Editor/DocumentChanges.js +++ b/word/Editor/DocumentChanges.js @@ -723,3 +723,328 @@ CChangesDocumentSettingsTrackRevisions.prototype.CreateReverseChange = function( { return new CChangesDocumentSettingsTrackRevisions(this.Class, this.New, this.Old, this.UserId); }; + +function CChangesDocumentProtection(Class, Old, New) { + AscDFH.CChangesBase.call(this, Class, Old, New); + if (Old && New) { + this.OldAlgorithmName = Old.algorithmName; + this.OldEdit = Old.edit; + this.OldEnforcment = Old.enforcment; + this.OldFormatting = Old.formatting; + this.OldHashValue = Old.hashValue; + this.OldSaltValue = Old.saltValue; + this.OldSpinCount = Old.spinCount; + this.OldAlgIdExt = Old.algIdExt; + this.OldAlgIdExtSource = Old.algIdExtSource; + this.OldCryptAlgorithmClass = Old.cryptAlgorithmClass; + this.OldCryptAlgorithmSid = Old.cryptAlgorithmSid; + this.OldCryptAlgorithmType = Old.cryptAlgorithmType; + this.OldCryptProvider = Old.cryptProvider; + this.OldCryptProviderType = Old.cryptProviderType; + this.OldCryptProviderTypeExt = Old.cryptProviderTypeExt; + this.OldCryptProviderTypeExtSource = Old.cryptProviderTypeExtSource; + + this.NewAlgorithmName = New.algorithmName === Old.algorithmName ? undefined : New.algorithmName; + this.NewEdit = New.edit === Old.edit ? undefined : New.edit; + this.NewEnforcment = New.enforcment === Old.enforcment ? undefined : New.enforcment; + this.NewFormatting = New.formatting === Old.formatting ? undefined : New.formatting; + this.NewHashValue = New.hashValue === Old.hashValue ? undefined : New.hashValue; + this.NewSaltValue = New.saltValue === Old.saltValue ? undefined : New.saltValue; + this.NewSpinCount = New.spinCount === Old.spinCount ? undefined : New.spinCount; + this.NewAlgIdExt = New.algIdExt === Old.algIdExt ? undefined : New.algIdExt; + this.NewAlgIdExtSource = New.algIdExtSource === Old.algIdExtSource ? undefined : New.algIdExtSource; + this.NewCryptAlgorithmClass = New.cryptAlgorithmClass === Old.cryptAlgorithmClass ? undefined : New.cryptAlgorithmClass; + this.NewCryptAlgorithmSid = New.cryptAlgorithmSid === Old.cryptAlgorithmSid ? undefined : New.cryptAlgorithmSid; + this.NewCryptAlgorithmType = New.cryptAlgorithmType === Old.cryptAlgorithmType ? undefined : New.cryptAlgorithmType; + this.NewCryptProvider = New.cryptProvider === Old.cryptProvider ? undefined : New.cryptProvider; + this.NewCryptProviderType = New.cryptProviderType === Old.cryptProviderType ? undefined : New.cryptProviderType; + this.NewCryptProviderTypeExt = New.cryptProviderTypeExt === Old.cryptProviderTypeExt ? undefined : New.cryptProviderTypeExt; + this.NewCryptProviderTypeExtSource = New.cryptProviderTypeExtSource === Old.cryptProviderTypeExtSource ? undefined : New.cryptProviderTypeExtSource; + } else { + this.OldAlgorithmName = undefined; + this.OldEdit = undefined; + this.OldEnforcment = undefined; + this.OldFormatting = undefined; + this.OldHashValue = undefined; + this.OldSaltValue = undefined; + this.OldSpinCount = undefined; + this.OldAlgIdExt = undefined; + this.OldAlgIdExtSource = undefined; + this.OldCryptAlgorithmClass = undefined; + this.OldCryptAlgorithmSid = undefined; + this.OldCryptAlgorithmType = undefined; + this.OldCryptProvider = undefined; + this.OldCryptProviderType = undefined; + this.OldCryptProviderTypeExt = undefined; + this.OldCryptProviderTypeExtSource = undefined; + + this.NewAlgorithmName = undefined; + this.NewEdit = undefined; + this.NewEnforcment = undefined; + this.NewFormatting = undefined; + this.NewHashValue = undefined; + this.NewSaltValue = undefined; + this.NewSpinCount = undefined; + this.NewAlgIdExt = undefined; + this.NewAlgIdExtSource = undefined; + this.NewCryptAlgorithmClass = undefined; + this.NewCryptAlgorithmSid = undefined; + this.NewCryptAlgorithmType = undefined; + this.NewCryptProvider = undefined; + this.NewCryptProviderType = undefined; + this.NewCryptProviderTypeExt = undefined; + this.NewCryptProviderTypeExtSource = undefined; + } +} + +CChangesDocumentProtection.prototype = Object.create(AscDFH.CChangesBase.prototype); +CChangesDocumentProtection.prototype.constructor = CChangesDocumentProtection; +CChangesDocumentProtection.prototype.Type = AscDFH.historydescription_Document_DocumentProtection; +CChangesDocumentProtection.prototype.Undo = function () { + if (!this.Class) { + return; + } + + this.Class.algorithmName = this.OldAlgorithmName; + this.Class.edit = this.OldEdit; + this.Class.enforcment = this.OldEnforcment; + this.Class.formatting = this.OldFormatting; + + this.Class.hashValue = this.OldHashValue; + this.Class.saltValue = this.OldSaltValue; + this.Class.spinCount = this.OldSpinCount; + this.Class.algIdExt = this.OldAlgIdExt; + + this.Class.algIdExtSource = this.OldAlgIdExtSource; + this.Class.cryptAlgorithmClass = this.OldCryptAlgorithmClass; + this.Class.cryptAlgorithmSid = this.OldCryptAlgorithmSid; + this.Class.cryptAlgorithmType = this.OldCryptAlgorithmType; + + this.Class.cryptProvider = this.OldCryptProvider; + this.Class.cryptProviderType = this.OldCryptProviderType; + this.Class.cryptProviderTypeExt = this.OldCryptProviderTypeExt; + this.Class.cryptProviderTypeExtSource = this.OldCryptProviderTypeExtSource; +}; +CChangesDocumentProtection.prototype.Redo = function () { + if (!this.Class) { + return; + } + + if (this.NewAlgorithmName !== undefined) { + this.Class.algorithmName = this.NewAlgorithmName; + } + if (this.NewEdit !== undefined) { + this.Class.edit = this.NewEdit; + } + if (this.NewEnforcment !== undefined) { + this.Class.enforcment = this.NewEnforcment; + } + if (this.NewFormatting !== undefined) { + this.Class.formatting = this.NewFormatting; + } + + if (this.NewHashValue !== undefined) { + this.Class.hashValue = this.NewHashValue; + } + if (this.NewSaltValue !== undefined) { + this.Class.saltValue = this.NewSaltValue; + } + if (this.NewSpinCount !== undefined) { + this.Class.spinCount = this.NewSpinCount; + } + if (this.NewFormatting !== undefined) { + this.Class.formatting = this.NewFormatting; + } + + if (this.NewAlgIdExt !== undefined) { + this.Class.algIdExt = this.NewAlgIdExt; + } + if (this.NewAlgIdExtSource !== undefined) { + this.Class.algIdExtSource = this.NewAlgIdExtSource; + } + if (this.NewCryptAlgorithmClass !== undefined) { + this.Class.cryptAlgorithmClass = this.NewCryptAlgorithmClass; + } + if (this.NewCryptAlgorithmSid !== undefined) { + this.Class.cryptAlgorithmSid = this.NewCryptAlgorithmSid; + } + + if (this.NewCryptAlgorithmType !== undefined) { + this.Class.cryptAlgorithmType = this.NewCryptAlgorithmType; + } + if (this.NewCryptProvider !== undefined) { + this.Class.cryptProvider = this.NewCryptProvider; + } + if (this.NewCryptProviderType !== undefined) { + this.Class.cryptProviderType = this.NewCryptProviderType; + } + if (this.NewCryptProviderTypeExt !== undefined) { + this.Class.cryptProviderTypeExt = this.NewCryptProviderTypeExt; + } + if (this.NewCryptProviderTypeExtSource !== undefined) { + this.Class.cryptProviderTypeExtSource = this.NewCryptProviderTypeExtSource; + } +}; +CChangesDocumentProtection.prototype.WriteToBinary = function (Writer) { + if (undefined !== this.NewAlgorithmName) { + Writer.WriteBool(true); + Writer.WriteByte(this.NewAlgorithmName); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewEdit) { + Writer.WriteBool(true); + Writer.WriteByte(this.NewEdit); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewEnforcment) { + Writer.WriteBool(true); + Writer.WriteBool(this.NewEnforcment); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewFormatting) { + Writer.WriteBool(true); + Writer.WriteBool(this.NewFormatting); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewHashValue) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewHashValue); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewSaltValue) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewSaltValue); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewSpinCount) { + Writer.WriteBool(true); + Writer.WriteLong(this.NewSpinCount); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewAlgIdExt) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewAlgIdExt); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewAlgIdExt) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewAlgIdExt); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewAlgIdExtSource) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewAlgIdExtSource); + } else { + Writer.WriteBool(false); + } + + if (undefined !== this.NewCryptAlgorithmClass) { + Writer.WriteBool(true); + Writer.WriteByte(this.NewCryptAlgorithmClass); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewCryptAlgorithmSid) { + Writer.WriteBool(true); + Writer.WriteLong(this.NewCryptAlgorithmSid); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewCryptAlgorithmType) { + Writer.WriteBool(true); + Writer.WriteByte(this.NewCryptAlgorithmType); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewCryptProvider) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewCryptProvider); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewCryptProviderType) { + Writer.WriteBool(true); + Writer.WriteByte(this.NewCryptProviderType); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewCryptProviderTypeExt) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewCryptProviderTypeExt); + } else { + Writer.WriteBool(false); + } + if (undefined !== this.NewCryptProviderTypeExtSource) { + Writer.WriteBool(true); + Writer.WriteString2(this.NewCryptProviderTypeExtSource); + } else { + Writer.WriteBool(false); + } +}; + +CChangesDocumentProtection.prototype.ReadFromBinary = function (Reader) { + if (Reader.GetBool()) { + this.NewAlgorithmName = Reader.GetByte(); + } + if (Reader.GetBool()) { + this.NewEdit = Reader.GetByte(); + } + if (Reader.GetBool()) { + this.NewEnforcment = Reader.GetBool(); + } + if (Reader.GetBool()) { + this.NewFormatting = Reader.GetBool(); + } + if (Reader.GetBool()) { + this.NewHashValue = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewSaltValue = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewSpinCount = Reader.GetLong(); + } + if (Reader.GetBool()) { + this.NewAlgIdExt = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewAlgIdExt = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewAlgIdExtSource = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewCryptAlgorithmClass = Reader.GetByte(); + } + if (Reader.GetBool()) { + this.NewCryptAlgorithmSid = Reader.GetLong(); + } + if (Reader.GetBool()) { + this.NewCryptAlgorithmType = Reader.GetByte(); + } + if (Reader.GetBool()) { + this.NewCryptProvider = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewCryptProviderType = Reader.GetByte(); + } + if (Reader.GetBool()) { + this.NewCryptProviderTypeExt = Reader.GetString2(); + } + if (Reader.GetBool()) { + this.NewCryptProviderTypeExtSource = Reader.GetString2(); + } +}; +CChangesDocumentProtection.prototype.CreateReverseChange = function () { +}; + +AscDFH.changesFactory[AscDFH.historydescription_Document_DocumentProtection] = CChangesDocumentProtection; diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 44a9158e20..aa96e063ce 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -89,6 +89,9 @@ function CDocProtect() { // Добавляем данный класс в таблицу Id (обязательно в конце конструктора) AscCommon.g_oTableId.Add(this, this.Id); } +CDocProtect.prototype.Get_Id = function () { + return this.Id; +}; CDocProtect.prototype.isOnlyView = function () { return this.edit === Asc.c_oAscEDocProtect.ReadOnly; }; @@ -141,11 +144,55 @@ CDocProtect.prototype.isPassword = function () { return this.algorithmName != null || this.cryptAlgorithmSid != null; }; CDocProtect.prototype.setProps = function (oProps) { - History.Add(new CChangesCorePr(this, this, oProps, null)); - this.title = oProps.title; - this.creator = oProps.creator; - this.description = oProps.description; - this.subject = oProps.subject; + //TODO в дальнейшем отправилять копию CDocProtect в интерфейс и принимать эту измененную копию + отправлять её в историю. сейчас имитирую эти действия + var newDocProtect; + AscFormat.ExecuteNoHistory(function () { + newDocProtect = new CDocProtect(); + }, this, []); + //TODO compare + newDocProtect.setFromInterface(oProps) + History.Add(new CChangesDocumentProtection(this, this, newDocProtect)); + this.setFromInterface(oProps); +}; +CDocProtect.prototype.setFromInterface = function (oProps) { + this.edit = oProps.props; + this.saltValue = oProps.saltValue; + this.spinCount = oProps.spinCount; + this.cryptAlgorithmSid = oProps.alg; + this.hashValue = oProps.hashValue; +}; +CDocProtect.prototype.Refresh_RecalcData = function () { +}; +CDocProtect.prototype.copy = function () { + return AscFormat.ExecuteNoHistory(function () { + var oDocProtect = new CDocProtect(); + oDocProtect.algorithmName = this.algorithmName; + oDocProtect.edit = this.edit; + oDocProtect.enforcment = this.enforcment; + oDocProtect.formatting = this.formatting; + oDocProtect.hashValue = this.hashValue; + oDocProtect.saltValue = this.saltValue; + oDocProtect.spinCount = this.spinCount; + oDocProtect.algIdExt = this.algIdExt; + oDocProtect.algIdExtSource = this.algIdExtSource; + oDocProtect.cryptAlgorithmClass = this.cryptAlgorithmClass; + oDocProtect.cryptAlgorithmSid = this.cryptAlgorithmSid; + oDocProtect.cryptAlgorithmType = this.cryptAlgorithmType; + oDocProtect.cryptProvider = this.cryptProvider; + oDocProtect.cryptProviderType = this.cryptProviderType; + oDocProtect.cryptProviderTypeExt = this.cryptProviderTypeExt; + oDocProtect.cryptProviderTypeExtSource = this.cryptProviderTypeExtSource; + return oDocProtect; + }, this, []); +}; +CDocProtect.prototype.Write_ToBinary2 = function(Writer) +{ + Writer.WriteLong(AscDFH.historyitem_type_DocumentProtection); + Writer.WriteString2("" + this.Id); +}; +CDocProtect.prototype.Read_FromBinary2 = function(Reader) +{ + this.Id = Reader.GetString2(); }; @@ -166,3 +213,5 @@ function CWriteProtection() { this.cryptProviderTypeExt = null; this.cryptProviderTypeExtSource = null; } + +window['AscCommonWord'].CDocProtect = CDocProtect; diff --git a/word/api.js b/word/api.js index 79a9d28f26..f3d703a3d1 100644 --- a/word/api.js +++ b/word/api.js @@ -12824,41 +12824,9 @@ background-repeat: no-repeat;\ t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); if (res) { - - - - - - oDocument.StartAction(AscDFH.historydescription_Document_SetDocumentProtection); - - //this.SetCheckBoxChecked(false); - - oDocument.SetProtection(props, salt, spinCount, alg, calculatedHashValue); - - - /*if (props) { - //устанавливаем - oDocument.SetProtection(props, salt, spinCount, alg, calculatedHashValue); - documentProtection.hashValue = calculatedHashValue; - } else { - //снимаем - documentProtection.hashValue = null; - documentProtection.saltValue = null; - documentProtection.spinCount = null; - documentProtection.algorithmName = null; - }*/ - /*History.Create_NewPoint(); - History.StartTransaction(); - if (!t.wbModel.getWorksheet(i).setProtectedSheet(props, true)) { - t.handlers.trigger("asc_onError", c_oAscError.ID.LockedWorksheetRename, - c_oAscError.Level.NoCritical); - } else if (wsView) { - wsView.updateAfterChangeSheetProtection(); - } - t.handlers.trigger("asc_onChangeDocumentProtection"); - - History.EndTransaction();*/ - + oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); + oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); + oDocument.UpdateInterface(); oDocument.FinalizeAction(); } else { t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); From b27b0c446dcb900870ca8379b9154270e7c9b845 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Wed, 5 Oct 2022 23:31:28 +0300 Subject: [PATCH 10/26] [de] Protect --- common/editorscommon.js | 16 +++--- word/Editor/DocumentChanges.js | 84 ++++++++++++------------------- word/Editor/DocumentProtection.js | 5 ++ word/api.js | 7 +-- 4 files changed, 48 insertions(+), 64 deletions(-) diff --git a/common/editorscommon.js b/common/editorscommon.js index 7ade16afa0..74e2c81b01 100644 --- a/common/editorscommon.js +++ b/common/editorscommon.js @@ -12891,28 +12891,28 @@ case AscCommon.c_oSerCryptAlgorithmSid.MD5 : res = AscCommon.HashAlgs.MD5; break; - case AscCommon.c_oSerAlgorithmNameTypes.SHA_1 : + case AscCommon.c_oSerCryptAlgorithmSid.SHA_1 : res = AscCommon.HashAlgs.SHA1; break; - case AscCommon.c_oSerAlgorithmNameTypes.MAC : + case AscCommon.c_oSerCryptAlgorithmSid.MAC : //alg = AscCommon.HashAlgs.SHA1; break; - case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD : + case AscCommon.c_oSerCryptAlgorithmSid.RIPEMD : //alg = AscCommon.HashAlgs.SHA256; break; - case AscCommon.c_oSerAlgorithmNameTypes.RIPEMD_160 : + case AscCommon.c_oSerCryptAlgorithmSid.RIPEMD_160 : //alg = AscCommon.HashAlgs.SHA384; break; - case AscCommon.c_oSerAlgorithmNameTypes.HMAC : + case AscCommon.c_oSerCryptAlgorithmSid.HMAC : //alg = AscCommon.HashAlgs.SHA512; break; - case AscCommon.c_oSerAlgorithmNameTypes.SHA_256 : + case AscCommon.c_oSerCryptAlgorithmSid.SHA_256 : res = AscCommon.HashAlgs.SHA256; break; - case AscCommon.c_oSerAlgorithmNameTypes.SHA_384 : + case AscCommon.c_oSerCryptAlgorithmSid.SHA_384 : res = AscCommon.HashAlgs.SHA384; break; - case AscCommon.c_oSerAlgorithmNameTypes.SHA_512 : + case AscCommon.c_oSerCryptAlgorithmSid.SHA_512 : res = AscCommon.HashAlgs.SHA512; break; } diff --git a/word/Editor/DocumentChanges.js b/word/Editor/DocumentChanges.js index 81e0a1751c..c1d7727ae2 100644 --- a/word/Editor/DocumentChanges.js +++ b/word/Editor/DocumentChanges.js @@ -824,65 +824,43 @@ CChangesDocumentProtection.prototype.Undo = function () { this.Class.cryptProviderType = this.OldCryptProviderType; this.Class.cryptProviderTypeExt = this.OldCryptProviderTypeExt; this.Class.cryptProviderTypeExtSource = this.OldCryptProviderTypeExtSource; + + editor.sendEvent("asc_onChangeDocumentProtection"); }; CChangesDocumentProtection.prototype.Redo = function () { if (!this.Class) { return; } - if (this.NewAlgorithmName !== undefined) { - this.Class.algorithmName = this.NewAlgorithmName; - } - if (this.NewEdit !== undefined) { - this.Class.edit = this.NewEdit; - } - if (this.NewEnforcment !== undefined) { - this.Class.enforcment = this.NewEnforcment; - } - if (this.NewFormatting !== undefined) { - this.Class.formatting = this.NewFormatting; - } - - if (this.NewHashValue !== undefined) { - this.Class.hashValue = this.NewHashValue; - } - if (this.NewSaltValue !== undefined) { - this.Class.saltValue = this.NewSaltValue; - } - if (this.NewSpinCount !== undefined) { - this.Class.spinCount = this.NewSpinCount; - } - if (this.NewFormatting !== undefined) { - this.Class.formatting = this.NewFormatting; - } - - if (this.NewAlgIdExt !== undefined) { - this.Class.algIdExt = this.NewAlgIdExt; - } - if (this.NewAlgIdExtSource !== undefined) { - this.Class.algIdExtSource = this.NewAlgIdExtSource; - } - if (this.NewCryptAlgorithmClass !== undefined) { - this.Class.cryptAlgorithmClass = this.NewCryptAlgorithmClass; - } - if (this.NewCryptAlgorithmSid !== undefined) { - this.Class.cryptAlgorithmSid = this.NewCryptAlgorithmSid; - } - - if (this.NewCryptAlgorithmType !== undefined) { - this.Class.cryptAlgorithmType = this.NewCryptAlgorithmType; - } - if (this.NewCryptProvider !== undefined) { - this.Class.cryptProvider = this.NewCryptProvider; - } - if (this.NewCryptProviderType !== undefined) { - this.Class.cryptProviderType = this.NewCryptProviderType; - } - if (this.NewCryptProviderTypeExt !== undefined) { - this.Class.cryptProviderTypeExt = this.NewCryptProviderTypeExt; - } - if (this.NewCryptProviderTypeExtSource !== undefined) { - this.Class.cryptProviderTypeExtSource = this.NewCryptProviderTypeExtSource; + this.Class.algorithmName = this.NewAlgorithmName; + this.Class.edit = this.NewEdit; + this.Class.enforcment = this.NewEnforcment; + this.Class.formatting = this.NewFormatting; + this.Class.hashValue = this.NewHashValue; + this.Class.saltValue = this.NewSaltValue; + this.Class.spinCount = this.NewSpinCount; + this.Class.formatting = this.NewFormatting; + this.Class.algIdExt = this.NewAlgIdExt; + + this.Class.algIdExtSource = this.NewAlgIdExtSource; + this.Class.cryptAlgorithmClass = this.NewCryptAlgorithmClass; + this.Class.cryptAlgorithmSid = this.NewCryptAlgorithmSid; + this.Class.cryptAlgorithmType = this.NewCryptAlgorithmType; + this.Class.cryptProvider = this.NewCryptProvider; + this.Class.cryptProviderType = this.NewCryptProviderType; + this.Class.cryptProviderTypeExt = this.NewCryptProviderTypeExt; + this.Class.cryptProviderTypeExtSource = this.NewCryptProviderTypeExtSource; + + var api = Asc.editor || editor; + if (api) { + let oDocument = api.private_GetLogicDocument(); + if (oDocument && oDocument.Settings) { + var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; + if (!docProtection || this.Class !== docProtection) { + oDocument.Settings.DocumentProtection = this.Class; + } + } + api.sendEvent("asc_onChangeDocumentProtection"); } }; CChangesDocumentProtection.prototype.WriteToBinary = function (Writer) { diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index aa96e063ce..29c9e4f7c8 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -160,6 +160,11 @@ CDocProtect.prototype.setFromInterface = function (oProps) { this.spinCount = oProps.spinCount; this.cryptAlgorithmSid = oProps.alg; this.hashValue = oProps.hashValue; + + if (oProps.props && oProps.props !== Asc.c_oAscEDocProtect.None) { + this.enforcment = true; + } + }; CDocProtect.prototype.Refresh_RecalcData = function () { }; diff --git a/word/api.js b/word/api.js index f3d703a3d1..8753f05f30 100644 --- a/word/api.js +++ b/word/api.js @@ -12801,7 +12801,7 @@ background-repeat: no-repeat;\ var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; //TODO enforcment!!! - проверить данный флаг if (docProtection) { - return [docProtection.edit, docProtection.isPassword()]; + return [docProtection.edit, docProtection.enforcement ? docProtection.isPassword() : null]; } }; @@ -12825,9 +12825,10 @@ background-repeat: no-repeat;\ if (res) { oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); - oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); + oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); oDocument.UpdateInterface(); oDocument.FinalizeAction(); + t.sendEvent("asc_onChangeDocumentProtection"); } else { t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); } @@ -12851,7 +12852,7 @@ background-repeat: no-repeat;\ //t.collaborativeEditing.lock([lockInfo], callback); callback(true); } else { - if (props) { + if (props && props !== Asc.c_oAscEDocProtect.None) { //устанавливаем защиту calculatedHashValue = hash && hash[0] ? hash[0] : null; //t.collaborativeEditing.lock([lockInfo], callback); From fcfda37103bb969f99dfe821c3b4a0a40e582409 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 6 Oct 2022 11:34:46 +0300 Subject: [PATCH 11/26] [de] Fix --- word/Editor/DocumentProtection.js | 3 +++ word/api.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 29c9e4f7c8..559d689c46 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -88,6 +88,9 @@ function CDocProtect() { // Добавляем данный класс в таблицу Id (обязательно в конце конструктора) AscCommon.g_oTableId.Add(this, this.Id); + + this.Lock = new AscCommon.CLock(); + this.lockType = AscCommon.c_oAscLockTypes.kLockTypeNone; } CDocProtect.prototype.Get_Id = function () { return this.Id; diff --git a/word/api.js b/word/api.js index 8753f05f30..99e8346dcf 100644 --- a/word/api.js +++ b/word/api.js @@ -12801,7 +12801,7 @@ background-repeat: no-repeat;\ var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; //TODO enforcment!!! - проверить данный флаг if (docProtection) { - return [docProtection.edit, docProtection.enforcement ? docProtection.isPassword() : null]; + return [docProtection.edit, docProtection.enforcment ? docProtection.isPassword() : null]; } }; From 863851549a86019e9f0ffebb564403e678b8ead3 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 6 Oct 2022 14:42:11 +0300 Subject: [PATCH 12/26] [se] Fix --- word/api.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/word/api.js b/word/api.js index 99e8346dcf..b0a3814c10 100644 --- a/word/api.js +++ b/word/api.js @@ -12840,10 +12840,15 @@ background-repeat: no-repeat;\ salt = documentProtection.saltValue; spinCount = documentProtection.spinCount; alg = documentProtection.cryptAlgorithmSid; - } else { + } + + if (!salt || !spinCount) { var params = AscCommon.generateHashParams(); salt = params.saltValue; spinCount = params.spinCount; + } + + if (!alg) { alg = AscCommon.c_oSerCryptAlgorithmSid.SHA_512; } From 2602052a0b0738f11dde8501505b4b70acdbe4af Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 6 Oct 2022 17:04:28 +0300 Subject: [PATCH 13/26] [de] Fix --- common/commonDefines.js | 2 ++ word/Editor/Document.js | 7 ++++++ word/Editor/DocumentProtection.js | 5 +--- word/api.js | 42 ++++++++++++++++++------------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/common/commonDefines.js b/common/commonDefines.js index e7dca9f4fc..eea1b0597d 100644 --- a/common/commonDefines.js +++ b/common/commonDefines.js @@ -2336,6 +2336,7 @@ var lcid_haLatn = 0x7c68; // Hausa, Latin var changestype_CorePr = 76; var changestype_Document_Settings = 77; // Изменение общих настроек документа Document.Settings var changestype_Timing = 78; // Изменение общих настроек документа Document.Settings + var changestype_DocumentProtection = 79; var changestype_2_InlineObjectMove = 1; // Передвигаем объект в заданную позцию (проверяем место, в которое пытаемся передвинуть) var changestype_2_HdrFtr = 2; // Изменения с колонтитулом @@ -4327,6 +4328,7 @@ var lcid_haLatn = 0x7c68; // Hausa, Latin window["AscCommon"].changestype_CorePr = changestype_CorePr; window["AscCommon"].changestype_Document_Settings = changestype_Document_Settings; window["AscCommon"].changestype_Timing = changestype_Timing; + window["AscCommon"].changestype_DocumentProtection = changestype_DocumentProtection; window["AscCommon"].changestype_2_InlineObjectMove = changestype_2_InlineObjectMove; window["AscCommon"].changestype_2_HdrFtr = changestype_2_HdrFtr; diff --git a/word/Editor/Document.js b/word/Editor/Document.js index e3d86397ed..44631af88c 100644 --- a/word/Editor/Document.js +++ b/word/Editor/Document.js @@ -13364,6 +13364,13 @@ CDocument.prototype.private_DocumentIsSelectionLocked = function(CheckType) this.Core.Lock.Check(this.Core.Get_Id()); } } + else if (AscCommon.changestype_DocumentProtection === CheckType) + { + if (this.Core) + { + this.Core.Lock.Check(this.Core.Get_Id()); + } + } else if (AscCommon.changestype_Document_Settings === CheckType) { this.Lock.Check(this.GetId()); diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 559d689c46..cd93ac54c1 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -164,10 +164,7 @@ CDocProtect.prototype.setFromInterface = function (oProps) { this.cryptAlgorithmSid = oProps.alg; this.hashValue = oProps.hashValue; - if (oProps.props && oProps.props !== Asc.c_oAscEDocProtect.None) { - this.enforcment = true; - } - + this.enforcment = oProps.props && oProps.props !== Asc.c_oAscEDocProtect.None; }; CDocProtect.prototype.Refresh_RecalcData = function () { }; diff --git a/word/api.js b/word/api.js index b0a3814c10..3c94d29309 100644 --- a/word/api.js +++ b/word/api.js @@ -12824,11 +12824,15 @@ background-repeat: no-repeat;\ t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); if (res) { - oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); - oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); - oDocument.UpdateInterface(); - oDocument.FinalizeAction(); - t.sendEvent("asc_onChangeDocumentProtection"); + //if(false === oDocument.Document_Is_SelectionLocked(AscCommon.changestype_DocumentProtection, null)) + //{ + oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); + oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); + oDocument.UpdateInterface(); + oDocument.FinalizeAction(); + t.sendEvent("asc_onChangeDocumentProtection"); + //} + } else { t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); } @@ -12836,20 +12840,22 @@ background-repeat: no-repeat;\ var documentProtection = oDocument.Settings.DocumentProtection; var salt, alg, spinCount; - if (documentProtection) { - salt = documentProtection.saltValue; - spinCount = documentProtection.spinCount; - alg = documentProtection.cryptAlgorithmSid; - } + if (password !== "" && password != null) { + if (documentProtection) { + salt = documentProtection.saltValue; + spinCount = documentProtection.spinCount; + alg = documentProtection.cryptAlgorithmSid; + } - if (!salt || !spinCount) { - var params = AscCommon.generateHashParams(); - salt = params.saltValue; - spinCount = params.spinCount; - } + if (!salt || !spinCount) { + var params = AscCommon.generateHashParams(); + salt = params.saltValue; + spinCount = params.spinCount; + } - if (!alg) { - alg = AscCommon.c_oSerCryptAlgorithmSid.SHA_512; + if (!alg) { + alg = AscCommon.c_oSerCryptAlgorithmSid.SHA_512; + } } var checkPassword = function (hash, doNotCheckPassword) { @@ -12857,7 +12863,7 @@ background-repeat: no-repeat;\ //t.collaborativeEditing.lock([lockInfo], callback); callback(true); } else { - if (props && props !== Asc.c_oAscEDocProtect.None) { + if (props != null && props !== Asc.c_oAscEDocProtect.None) { //устанавливаем защиту calculatedHashValue = hash && hash[0] ? hash[0] : null; //t.collaborativeEditing.lock([lockInfo], callback); From e3d000cf20d694acc6c5c23a06b8532d815c4dba Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 6 Oct 2022 18:06:55 +0300 Subject: [PATCH 14/26] [de] Add lock event --- word/Editor/Document.js | 4 ++-- word/Editor/DocumentSettings.js | 2 +- word/api.js | 28 +++++++++++++++++++++------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/word/Editor/Document.js b/word/Editor/Document.js index 44631af88c..1e71df9e8b 100644 --- a/word/Editor/Document.js +++ b/word/Editor/Document.js @@ -13366,9 +13366,9 @@ CDocument.prototype.private_DocumentIsSelectionLocked = function(CheckType) } else if (AscCommon.changestype_DocumentProtection === CheckType) { - if (this.Core) + if (this.Settings.DocumentProtection) { - this.Core.Lock.Check(this.Core.Get_Id()); + this.Settings.DocumentProtection.Lock.Check(this.Settings.DocumentProtection.Get_Id()); } } else if (AscCommon.changestype_Document_Settings === CheckType) diff --git a/word/Editor/DocumentSettings.js b/word/Editor/DocumentSettings.js index 8493445d3b..3922beccde 100644 --- a/word/Editor/DocumentSettings.js +++ b/word/Editor/DocumentSettings.js @@ -46,7 +46,7 @@ this.SdtSettings = new CSdtGlobalSettings(); this.SpecialFormsSettings = new CSpecialFormsGlobalSettings(); this.WriteProtection = undefined; - this.DocumentProtection = undefined; + this.DocumentProtection = new AscCommonWord.CDocProtect(); this.ListSeparator = undefined; this.DecimalSymbol = undefined; diff --git a/word/api.js b/word/api.js index 3c94d29309..c687f676fb 100644 --- a/word/api.js +++ b/word/api.js @@ -1750,6 +1750,10 @@ background-repeat: no-repeat;\ { editor.sendEvent("asc_onLockCore", true); } + else if(Class instanceof AscCommonWord.CDocProtect) + { + editor.sendEvent("asc_onLockDocumentProtection", true); + } // Теперь обновлять состояние необходимо, чтобы обновить локи в режиме рецензирования. t.WordControl.m_oLogicDocument.UpdateInterface(undefined, true); } @@ -1859,6 +1863,17 @@ background-repeat: no-repeat;\ editor.sendEvent("asc_onLockCore", false); } } + else if(Class instanceof AscCommonWord.CDocProtect) + { + if (NewType !== locktype_Mine && NewType !== locktype_None) + { + editor.sendEvent("asc_onLockDocumentProtection", true); + } + else + { + editor.sendEvent("asc_onLockDocumentProtection", false); + } + } } } else @@ -12809,9 +12824,9 @@ background-repeat: no-repeat;\ //props -> c_oAscEDocProtect // Проверка глобального лока - /*if (this.collaborativeEditing.getGlobalLock() || !this.canEdit()) { - return false; - }*/ + if (AscCommon.CollaborativeEditing.Get_GlobalLock()) { + return; + } let oDocument = this.private_GetLogicDocument(); if (!oDocument) { @@ -12824,15 +12839,14 @@ background-repeat: no-repeat;\ t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); if (res) { - //if(false === oDocument.Document_Is_SelectionLocked(AscCommon.changestype_DocumentProtection, null)) - //{ + if(false === oDocument.Document_Is_SelectionLocked(AscCommon.changestype_DocumentProtection, null)) + { oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); oDocument.UpdateInterface(); oDocument.FinalizeAction(); t.sendEvent("asc_onChangeDocumentProtection"); - //} - + } } else { t.sendEvent("asc_onError", c_oAscError.ID.PasswordIsNotCorrect, c_oAscError.Level.NoCritical); } From 09b78bc9211eb5a7234a070dadcd9efd17bc01ce Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 6 Oct 2022 18:34:34 +0300 Subject: [PATCH 15/26] [de] Fix --- word/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/word/api.js b/word/api.js index c687f676fb..5b386c2b52 100644 --- a/word/api.js +++ b/word/api.js @@ -12839,7 +12839,7 @@ background-repeat: no-repeat;\ t.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction); if (res) { - if(false === oDocument.Document_Is_SelectionLocked(AscCommon.changestype_DocumentProtection, null)) + if(false === oDocument.Document_Is_SelectionLocked(AscCommon.changestype_DocumentProtection, null, null, true)) { oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); From 11ba74cbc196e42e621791d03ba6cf1b4de3186d Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 7 Oct 2022 00:14:17 +0300 Subject: [PATCH 16/26] [de] By locks --- word/Editor/CollaborativeEditing.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/word/Editor/CollaborativeEditing.js b/word/Editor/CollaborativeEditing.js index 6bebecbece..39cdf93cf9 100644 --- a/word/Editor/CollaborativeEditing.js +++ b/word/Editor/CollaborativeEditing.js @@ -204,6 +204,8 @@ CWordCollaborativeEditing.prototype.Release_Locks = function() editor.sync_UnLockDocumentSchema(); else if (this.m_aNeedUnlock[Index] instanceof AscCommon.CCore) editor.sendEvent("asc_onLockCore", false); + else if (this.m_aNeedUnlock[Index] instanceof AscCommonWord.CDocProtect) + editor.sendEvent("asc_onLockDocumentProtection", false); } else if (AscCommon.locktype_Other3 === CurLockType) { From 04681950146664993a63b7cce066a1a90fe46139 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 7 Oct 2022 11:04:29 +0300 Subject: [PATCH 17/26] [de] Change get/set protections --- word/Editor/Document.js | 5 ++-- word/Editor/DocumentProtection.js | 47 ++++++++++++++++++------------- word/api.js | 19 +++++++++---- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/word/Editor/Document.js b/word/Editor/Document.js index 1e71df9e8b..17fbff9e26 100644 --- a/word/Editor/Document.js +++ b/word/Editor/Document.js @@ -16327,10 +16327,9 @@ CDocument.prototype.IsSpecialFormsSettingsDefault = function() }; CDocument.prototype.SetProtection = function(props) { - if (!this.Settings.DocumentProtection) { - this.Settings.DocumentProtection = new CDocProtect(); + if (this.Settings.DocumentProtection) { + this.Settings.DocumentProtection.setProps(props); } - return this.Settings.DocumentProtection.setProps(props); }; CDocument.prototype.GetDocumentLayout = function() diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index cd93ac54c1..2b4544cfaf 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -61,13 +61,6 @@ var ECryptProv = { function CDocProtect() { this.Id = AscCommon.g_oIdCounter.Get_NewId(); - /*this.Lock = new AscCommon.CLock(); // Зажат ли комментарий другим пользователем - if (false === AscCommon.g_oIdCounter.m_bLoad) - { - this.Lock.Set_Type(AscCommon.locktype_Mine, false); - AscCommon.CollaborativeEditing.Add_Unlock2(this); - }*/ - this.algorithmName = null; this.edit = null; this.enforcment = null; @@ -91,6 +84,8 @@ function CDocProtect() { this.Lock = new AscCommon.CLock(); this.lockType = AscCommon.c_oAscLockTypes.kLockTypeNone; + + this.temporaryPassword = null; } CDocProtect.prototype.Get_Id = function () { return this.Id; @@ -147,28 +142,21 @@ CDocProtect.prototype.isPassword = function () { return this.algorithmName != null || this.cryptAlgorithmSid != null; }; CDocProtect.prototype.setProps = function (oProps) { - //TODO в дальнейшем отправилять копию CDocProtect в интерфейс и принимать эту измененную копию + отправлять её в историю. сейчас имитирую эти действия - var newDocProtect; - AscFormat.ExecuteNoHistory(function () { - newDocProtect = new CDocProtect(); - }, this, []); - //TODO compare - newDocProtect.setFromInterface(oProps) - History.Add(new CChangesDocumentProtection(this, this, newDocProtect)); + History.Add(new CChangesDocumentProtection(this, this, oProps)); this.setFromInterface(oProps); }; CDocProtect.prototype.setFromInterface = function (oProps) { - this.edit = oProps.props; + this.edit = oProps.edit; this.saltValue = oProps.saltValue; this.spinCount = oProps.spinCount; - this.cryptAlgorithmSid = oProps.alg; + this.cryptAlgorithmSid = oProps.cryptAlgorithmSid; this.hashValue = oProps.hashValue; - this.enforcment = oProps.props && oProps.props !== Asc.c_oAscEDocProtect.None; + this.enforcment = oProps.enforcment; }; CDocProtect.prototype.Refresh_RecalcData = function () { }; -CDocProtect.prototype.copy = function () { +CDocProtect.prototype.Copy = function () { return AscFormat.ExecuteNoHistory(function () { var oDocProtect = new CDocProtect(); oDocProtect.algorithmName = this.algorithmName; @@ -199,6 +187,22 @@ CDocProtect.prototype.Read_FromBinary2 = function(Reader) { this.Id = Reader.GetString2(); }; +CDocProtect.prototype.asc_getIsPassword = function() +{ + return this.enforcment ? this.isPassword() : null +}; +CDocProtect.prototype.asc_getEditType = function() +{ + return this.edit; +}; +CDocProtect.prototype.asc_setPassword = function(val) +{ + this.temporaryPassword = val; +}; +CDocProtect.prototype.asc_setEditType = function(val) +{ + this.edit = val; +}; function CWriteProtection() { @@ -220,3 +224,8 @@ function CWriteProtection() { } window['AscCommonWord'].CDocProtect = CDocProtect; +prot = CDocProtect.prototype; +prot["asc_getIsPassword"] = prot.asc_getTitle; +prot["asc_getEditType"] = prot.asc_getCreator; +prot["asc_setEditType"] = prot.asc_getLastModifiedBy; +prot["asc_setPassword"] = prot.asc_getRevision; diff --git a/word/api.js b/word/api.js index 5b386c2b52..cb4ddc7579 100644 --- a/word/api.js +++ b/word/api.js @@ -12816,12 +12816,12 @@ background-repeat: no-repeat;\ var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; //TODO enforcment!!! - проверить данный флаг if (docProtection) { - return [docProtection.edit, docProtection.enforcment ? docProtection.isPassword() : null]; + return docProtection.Copy(); } }; - asc_docs_api.prototype.asc_setDocumentProtection = function (props, password) { - //props -> c_oAscEDocProtect + asc_docs_api.prototype.asc_setDocumentProtection = function (props) { + //props -> CDocProtect // Проверка глобального лока if (AscCommon.CollaborativeEditing.Get_GlobalLock()) { @@ -12842,7 +12842,14 @@ background-repeat: no-repeat;\ if(false === oDocument.Document_Is_SelectionLocked(AscCommon.changestype_DocumentProtection, null, null, true)) { oDocument.StartAction(AscDFH.historydescription_Document_DocumentProtection); - oDocument.SetProtection({props: props, saltValue: salt, spinCount: spinCount, alg: alg, hashValue: calculatedHashValue}); + + props.saltValue = salt; + props.spinCount = spinCount; + props.cryptAlgorithmSid = alg; + props.hashValue = calculatedHashValue; + props.enforcment = props.edit && props.edit !== Asc.c_oAscEDocProtect.None; + + oDocument.SetProtection(props); oDocument.UpdateInterface(); oDocument.FinalizeAction(); t.sendEvent("asc_onChangeDocumentProtection"); @@ -12852,6 +12859,8 @@ background-repeat: no-repeat;\ } }; + var password = props.temporaryPassword; + props.temporaryPassword = null; var documentProtection = oDocument.Settings.DocumentProtection; var salt, alg, spinCount; if (password !== "" && password != null) { @@ -12877,7 +12886,7 @@ background-repeat: no-repeat;\ //t.collaborativeEditing.lock([lockInfo], callback); callback(true); } else { - if (props != null && props !== Asc.c_oAscEDocProtect.None) { + if (props != null && props.edit != null && props.edit !== Asc.c_oAscEDocProtect.None) { //устанавливаем защиту calculatedHashValue = hash && hash[0] ? hash[0] : null; //t.collaborativeEditing.lock([lockInfo], callback); From 98c957a304808fd9e97cce6677f8a1be183a1306 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 7 Oct 2022 11:06:11 +0300 Subject: [PATCH 18/26] [de] Fix --- word/Editor/DocumentProtection.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 2b4544cfaf..17940477c2 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -225,7 +225,7 @@ function CWriteProtection() { window['AscCommonWord'].CDocProtect = CDocProtect; prot = CDocProtect.prototype; -prot["asc_getIsPassword"] = prot.asc_getTitle; -prot["asc_getEditType"] = prot.asc_getCreator; -prot["asc_setEditType"] = prot.asc_getLastModifiedBy; -prot["asc_setPassword"] = prot.asc_getRevision; +prot["asc_getIsPassword"] = prot.asc_getIsPassword; +prot["asc_getEditType"] = prot.asc_getEditType; +prot["asc_setEditType"] = prot.asc_setEditType; +prot["asc_setPassword"] = prot.asc_setPassword; From 14ecee2ed51f9e51c9580faaa4a9ff05dc4fd31e Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Fri, 7 Oct 2022 15:09:04 +0300 Subject: [PATCH 19/26] [de] Fix --- common/HistoryCommon.js | 1 - common/TableId.js | 1 - word/Editor/CollaborativeEditing.js | 4 ++-- word/Editor/DocumentProtection.js | 4 ++-- word/api.js | 7 ------- 5 files changed, 4 insertions(+), 13 deletions(-) diff --git a/common/HistoryCommon.js b/common/HistoryCommon.js index 9a6184cbf9..91ec1b8570 100644 --- a/common/HistoryCommon.js +++ b/common/HistoryCommon.js @@ -1665,7 +1665,6 @@ window['AscDFH'].historyitem_type_VMLShapeType = 2109 << 16; window['AscDFH'].historyitem_type_VMLClientData = 2110 << 16; - window['AscDFH'].historyitem_type_DocumentProtection = 2111 << 16; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/common/TableId.js b/common/TableId.js index 77c90e852b..633c5673ef 100644 --- a/common/TableId.js +++ b/common/TableId.js @@ -381,7 +381,6 @@ this.m_oFactoryClass[AscDFH.historyitem_type_SmartArtNode ] = AscFormat.SmartArtNode; this.m_oFactoryClass[AscDFH.historyitem_type_SmartArtNodeData ] = AscFormat.SmartArtNodeData; this.m_oFactoryClass[AscDFH.historyitem_type_BuBlip ] = AscFormat.CBuBlip; - this.m_oFactoryClass[AscDFH.historyitem_type_DocumentProtection] = AscCommonWord.CDocProtect; if (window['AscCommonSlide']) { diff --git a/word/Editor/CollaborativeEditing.js b/word/Editor/CollaborativeEditing.js index 39cdf93cf9..97fb06a7d3 100644 --- a/word/Editor/CollaborativeEditing.js +++ b/word/Editor/CollaborativeEditing.js @@ -204,8 +204,8 @@ CWordCollaborativeEditing.prototype.Release_Locks = function() editor.sync_UnLockDocumentSchema(); else if (this.m_aNeedUnlock[Index] instanceof AscCommon.CCore) editor.sendEvent("asc_onLockCore", false); - else if (this.m_aNeedUnlock[Index] instanceof AscCommonWord.CDocProtect) - editor.sendEvent("asc_onLockDocumentProtection", false); + else if (this.m_aNeedUnlock[Index] instanceof AscCommonWord.CDocProtect) + editor.sendEvent("asc_onLockDocumentProtection", false); } else if (AscCommon.locktype_Other3 === CurLockType) { diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 17940477c2..3433a1ba6c 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -178,7 +178,7 @@ CDocProtect.prototype.Copy = function () { return oDocProtect; }, this, []); }; -CDocProtect.prototype.Write_ToBinary2 = function(Writer) +/*CDocProtect.prototype.Write_ToBinary2 = function(Writer) { Writer.WriteLong(AscDFH.historyitem_type_DocumentProtection); Writer.WriteString2("" + this.Id); @@ -186,7 +186,7 @@ CDocProtect.prototype.Write_ToBinary2 = function(Writer) CDocProtect.prototype.Read_FromBinary2 = function(Reader) { this.Id = Reader.GetString2(); -}; +};*/ CDocProtect.prototype.asc_getIsPassword = function() { return this.enforcment ? this.isPassword() : null diff --git a/word/api.js b/word/api.js index cb4ddc7579..dbd14bb1ac 100644 --- a/word/api.js +++ b/word/api.js @@ -12806,7 +12806,6 @@ background-repeat: no-repeat;\ return oDocument.PutImageToSelection(sImageSrc, nWidth, nHeight); }; - //asc_onChangeProtectWorkbook asc_docs_api.prototype.asc_getDocumentProtection = function () { let oDocument = this.private_GetLogicDocument(); if (!oDocument) { @@ -12814,7 +12813,6 @@ background-repeat: no-repeat;\ } var docProtection = oDocument.Settings && oDocument.Settings.DocumentProtection; - //TODO enforcment!!! - проверить данный флаг if (docProtection) { return docProtection.Copy(); } @@ -12883,18 +12881,15 @@ background-repeat: no-repeat;\ var checkPassword = function (hash, doNotCheckPassword) { if (doNotCheckPassword) { - //t.collaborativeEditing.lock([lockInfo], callback); callback(true); } else { if (props != null && props.edit != null && props.edit !== Asc.c_oAscEDocProtect.None) { //устанавливаем защиту calculatedHashValue = hash && hash[0] ? hash[0] : null; - //t.collaborativeEditing.lock([lockInfo], callback); callback(true); } else { //пробуем снять защиту if (documentProtection && hash && hash[0] === documentProtection.hashValue) { - //t.collaborativeEditing.lock([lockInfo], callback); callback(true); } else { //неверный пароль @@ -12907,8 +12902,6 @@ background-repeat: no-repeat;\ }; this.sync_StartAction(Asc.c_oAscAsyncActionType.BlockInteraction); - //props !== null && props !== Asc.c_oAscEDocProtect.None - if (password != null) { if (password === "") { checkPassword([""]); From 9092794e6745433392fe1fef146fae7a2b8e2822 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Wed, 12 Oct 2022 12:54:11 +0300 Subject: [PATCH 20/26] [de] Test --- common/hash/hash.js | 89 +++++++++++++++++++++++++++++++++++++++++++++ word/api.js | 2 +- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/common/hash/hash.js b/common/hash/hash.js index 84748a84db..b91d35623a 100644 --- a/common/hash/hash.js +++ b/common/hash/hash.js @@ -142,6 +142,95 @@ currentHashWorker = new CHashWorker(sendedData, callback); }; + window['AscCommon'].prepareWordPassword = function(password) { + + + let _highOrderWords = [[0xE1, 0xF0], [0x1D, 0x0F], [0xCC, 0x9C], [0x84, 0xC0], [0x11, 0x0C], [0x0E, 0x10], [0xF1, 0xCE], [0x31, 0x3E], [0x18, 0x72], [0xE1, 0x39], + [0xD4, 0x0F], [0x84, 0xF9], [0x28, 0x0C], [0xA9, 0x6A], [0x4E, 0xC3]]; + + let _encryptionMatrix = [[[0xAE, 0xFC], [0x4D, 0xD9], [0x9B, 0xB2], [0x27, 0x45], [0x4E, 0x8A], [0x9D, 0x14], [0x2A, 0x09]], + [[0x7B, 0x61], [0xF6, 0xC2], [0xFD, 0xA5], [0xEB, 0x6B], [0xC6, 0xF7], [0x9D, 0xCF], [0x2B, 0xBF]], + [[0x45, 0x63], [0x8A, 0xC6], [0x05, 0xAD], [0x0B, 0x5A], [0x16, 0xB4], [0x2D, 0x68], [0x5A, 0xD0]], + [[0x03, 0x75], [0x06, 0xEA], [0x0D, 0xD4], [0x1B, 0xA8], [0x37, 0x50], [0x6E, 0xA0], [0xDD, 0x40]], + [[0xD8, 0x49], [0xA0, 0xB3], [0x51, 0x47], [0xA2, 0x8E], [0x55, 0x3D], [0xAA, 0x7A], [0x44, 0xD5]], + [[0x6F, 0x45], [0xDE, 0x8A], [0xAD, 0x35], [0x4A, 0x4B], [0x94, 0x96], [0x39, 0x0D], [0x72, 0x1A]], + [[0xEB, 0x23], [0xC6, 0x67], [0x9C, 0xEF], [0x29, 0xFF], [0x53, 0xFE], [0xA7, 0xFC], [0x5F, 0xD9]], + [[0x47, 0xD3], [0x8F, 0xA6], [0x0F, 0x6D], [0x1E, 0xDA], [0x3D, 0xB4], [0x7B, 0x68], [0xF6, 0xD0]], + [[0xB8, 0x61], [0x60, 0xE3], [0xC1, 0xC6], [0x93, 0xAD], [0x37, 0x7B], [0x6E, 0xF6], [0xDD, 0xEC]], + [[0x45, 0xA0], [0x8B, 0x40], [0x06, 0xA1], [0x0D, 0x42], [0x1A, 0x84], [0x35, 0x08], [0x6A, 0x10]], + [[0xAA, 0x51], [0x44, 0x83], [0x89, 0x06], [0x02, 0x2D], [0x04, 0x5A], [0x08, 0xB4], [0x11, 0x68]], + [[0x76, 0xB4], [0xED, 0x68], [0xCA, 0xF1], [0x85, 0xC3], [0x1B, 0xA7], [0x37, 0x4E], [0x6E, 0x9C]], + [[0x37, 0x30], [0x6E, 0x60], [0xDC, 0xC0], [0xA9, 0xA1], [0x43, 0x63], [0x86, 0xC6], [0x1D, 0xAD]], + [[0x33, 0x31], [0x66, 0x62], [0xCC, 0xC4], [0x89, 0xA9], [0x03, 0x73], [0x06, 0xE6], [0x0D, 0xCC]], + [[0x10, 0x21], [0x20, 0x42], [0x40, 0x84], [0x81, 0x08], [0x12, 0x31], [0x24, 0x62], [0x48, 0xC4]]]; + + var byteToBits = function (byte) { + var bitsArr = []; + for (let i = 7; i >= 0; i--) { + let bit = byte & (1 << i) ? 1 : 0; + bitsArr.push(bit); + } + return bitsArr; + }; + var getBitsSequence = function (bytes) { + var bitsArr = []; + //TODO ++ ? + for (let j = 0; i >= bytes.length; j++) { + var byte = bytes[i]; + for (let i = 7; i >= 0; i--) { + let bit = byte & (1 << i) ? 1 : 0; + bitsArr.push(bit); + } + } + + return bitsArr; + }; + + if (password == null) { + return null; + } + + let textEncoder = new TextEncoder(); + let passwordBytes = textEncoder.encode(password); + + passwordBytes = passwordBytes.slice(0, 14); + let passwordLength = passwordBytes.length; + + + let highOrderWord = [0x00, 0x00]; + if (passwordLength > 0) { + highOrderWord = _highOrderWords[passwordLength - 1]; + } + + for (var i = 0; i < passwordLength; i++) { + var passwordByte = passwordBytes[i]; + var encryptionMatrixIndex = i + (_encryptionMatrix.length - passwordLength); + + let bitArray = byteToBits(passwordByte)//.ToBitArray(); + + for (let j = 0; j < _encryptionMatrix[0].length; j++) { + let isSet = bitArray[j]; + + if (isSet) { + for (let k = 0; k < _encryptionMatrix[0][0].length; k++) { + highOrderWord[k] = highOrderWord[k] ^ var encryptionMatrixIndex = i + (_encryptionMatrix.length - passwordLength); + [encryptionMatrixIndex][j][k]; + } + } + } + } + + let lowOrderWord = [0x00, 0x00]; + let lowOrderBitSequence = getBitsSequence(lowOrderWord); + let bitSequence1 = getBitsSequence([0x00, 0x01]); + let bitSequence7FFF = getBitsSequence([0x7F, 0xFF]); + + for (let i = passwordLength - 1; i >= 0; i--) { + let passwordByte = passwordBytes[i]; + lowOrderBitSequence = (((lowOrderBitSequence >> 14) & bitSequence1) | ((lowOrderBitSequence << 1) & bitSequence7FFF)) ^ getBitsSequence([0x00, passwordByte]); + } + }; + window['AscCommon'].HashAlgs = { MD2 : 0, MD4 : 1, diff --git a/word/api.js b/word/api.js index dbd14bb1ac..1ff489794c 100644 --- a/word/api.js +++ b/word/api.js @@ -12906,7 +12906,7 @@ background-repeat: no-repeat;\ if (password === "") { checkPassword([""]); } else { - var checkHash = {password: password, salt: salt, spinCount: spinCount, alg: AscCommon.fromModelCryptAlgorithmSid(alg)}; + var checkHash = {password: AscCommon.prepareWordPassword(password), salt: salt, spinCount: spinCount, alg: AscCommon.fromModelCryptAlgorithmSid(alg)}; AscCommon.calculateProtectHash([checkHash], checkPassword); } } else { From e347f4cde7951e91346704f6ff0dbad46edf0c94 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Wed, 12 Oct 2022 12:56:45 +0300 Subject: [PATCH 21/26] [de] Fix --- common/hash/hash.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/hash/hash.js b/common/hash/hash.js index b91d35623a..983816e250 100644 --- a/common/hash/hash.js +++ b/common/hash/hash.js @@ -213,8 +213,7 @@ if (isSet) { for (let k = 0; k < _encryptionMatrix[0][0].length; k++) { - highOrderWord[k] = highOrderWord[k] ^ var encryptionMatrixIndex = i + (_encryptionMatrix.length - passwordLength); - [encryptionMatrixIndex][j][k]; + highOrderWord[k] = highOrderWord[k] ^ _encryptionMatrix[encryptionMatrixIndex][j][k]; } } } From 6388bae656808f76a6803646df4a7db6738e4708 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 13 Oct 2022 13:52:39 +0300 Subject: [PATCH 22/26] [de] Test --- common/hash/hash.js | 66 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/common/hash/hash.js b/common/hash/hash.js index 983816e250..034421b52b 100644 --- a/common/hash/hash.js +++ b/common/hash/hash.js @@ -166,26 +166,49 @@ var byteToBits = function (byte) { var bitsArr = []; - for (let i = 7; i >= 0; i--) { + for (let i = 0; i < 8; i++) { let bit = byte & (1 << i) ? 1 : 0; bitsArr.push(bit); } return bitsArr; }; - var getBitsSequence = function (bytes) { - var bitsArr = []; + var getBitsSequence = function (bytes, getStr) { + var bitsArr = getStr ? "" : []; //TODO ++ ? - for (let j = 0; i >= bytes.length; j++) { - var byte = bytes[i]; + for (let j = 0; j < bytes.length; j++) { + var byte = bytes[j]; for (let i = 7; i >= 0; i--) { let bit = byte & (1 << i) ? 1 : 0; - bitsArr.push(bit); + if (getStr) { + bitsArr += bit; + } else { + bitsArr.push(bit); + } } } return bitsArr; }; + + + var getFull2Byte = function (str) { + var strLength = str.length; + var beforeStr = "0000000000000000".substring(0, 16 - strLength); + return beforeStr + str; + }; + + var toByteArray = function (str) { + /*var arr = []; + for (var i = 0; i < str; i++) { + + } + return arr;*/ + var byte1 = str.substring(0, 8); + var byte2 = str.substring(8, 16); + return [parseInt( byte1, 2), parseInt(byte2, 2)]; + }; + if (password == null) { return null; } @@ -220,14 +243,37 @@ } let lowOrderWord = [0x00, 0x00]; - let lowOrderBitSequence = getBitsSequence(lowOrderWord); - let bitSequence1 = getBitsSequence([0x00, 0x01]); - let bitSequence7FFF = getBitsSequence([0x7F, 0xFF]); + let lowOrderBitSequence = getBitsSequence(lowOrderWord, true); + let bitSequence1 = getBitsSequence([0x00, 0x01], true); + let bitSequence7FFF = getBitsSequence([0x7F, 0xFF], true); - for (let i = passwordLength - 1; i >= 0; i--) { + let lowOrderSequence = parseInt(lowOrderBitSequence,2); + let sequence1 = parseInt(bitSequence1,2); + let sequence7FFF = parseInt(bitSequence7FFF,2); + + /*for (let i = passwordLength - 1; i >= 0; i--) { let passwordByte = passwordBytes[i]; lowOrderBitSequence = (((lowOrderBitSequence >> 14) & bitSequence1) | ((lowOrderBitSequence << 1) & bitSequence7FFF)) ^ getBitsSequence([0x00, passwordByte]); + }*/ + for (let i = passwordLength - 1; i >= 0; i--) { + let passwordByte = passwordBytes[i]; + let passwordBits = parseInt(getBitsSequence([0x00, passwordByte], true), 2); + lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ passwordBits; } + + //lowOrderBitSequence = (lowOrderSequence).toString(2); + //lowOrderSequence = getFull2Byte(lowOrderBitSequence); + + let bitTest1 = getBitsSequence([0x00, passwordLength], true); + let test1 = parseInt(bitTest1, 2); + let bitTest2 = getBitsSequence([0xCE, 0x4B], true); + let test2 = parseInt(bitTest2, 2); + lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ test1 ^ test2; + + lowOrderWord = toByteArray((getFull2Byte((lowOrderSequence).toString(2)))); + var key = highOrderWord.concat(lowOrderWord).reverse(); + + }; window['AscCommon'].HashAlgs = { From 3f8b1648718175c6237a039ca37dc2078503c464 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 13 Oct 2022 15:22:28 +0300 Subject: [PATCH 23/26] [de] Test --- common/hash/hash.js | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/common/hash/hash.js b/common/hash/hash.js index 034421b52b..7b40f05f54 100644 --- a/common/hash/hash.js +++ b/common/hash/hash.js @@ -144,7 +144,6 @@ window['AscCommon'].prepareWordPassword = function(password) { - let _highOrderWords = [[0xE1, 0xF0], [0x1D, 0x0F], [0xCC, 0x9C], [0x84, 0xC0], [0x11, 0x0C], [0x0E, 0x10], [0xF1, 0xCE], [0x31, 0x3E], [0x18, 0x72], [0xE1, 0x39], [0xD4, 0x0F], [0x84, 0xF9], [0x28, 0x0C], [0xA9, 0x6A], [0x4E, 0xC3]]; @@ -165,7 +164,7 @@ [[0x10, 0x21], [0x20, 0x42], [0x40, 0x84], [0x81, 0x08], [0x12, 0x31], [0x24, 0x62], [0x48, 0xC4]]]; var byteToBits = function (byte) { - var bitsArr = []; + let bitsArr = []; for (let i = 0; i < 8; i++) { let bit = byte & (1 << i) ? 1 : 0; bitsArr.push(bit); @@ -173,10 +172,10 @@ return bitsArr; }; var getBitsSequence = function (bytes, getStr) { - var bitsArr = getStr ? "" : []; + let bitsArr = getStr ? "" : []; //TODO ++ ? for (let j = 0; j < bytes.length; j++) { - var byte = bytes[j]; + let byte = bytes[j]; for (let i = 7; i >= 0; i--) { let bit = byte & (1 << i) ? 1 : 0; if (getStr) { @@ -190,23 +189,24 @@ return bitsArr; }; - - var getFull2Byte = function (str) { - var strLength = str.length; - var beforeStr = "0000000000000000".substring(0, 16 - strLength); + let strLength = str.length; + let beforeStr = "0000000000000000".substring(0, 16 - strLength); return beforeStr + str; }; var toByteArray = function (str) { - /*var arr = []; - for (var i = 0; i < str; i++) { + let byte1 = str.substring(0, 8); + let byte2 = str.substring(8, 16); + return [parseInt( byte1, 2), parseInt(byte2, 2)]; + }; + var getHexStr = function (arr) { + let res = ""; + for (let i = 0; i < arr.length; i++) { + res += AscCommon.ByteToHex(arr[i]); } - return arr;*/ - var byte1 = str.substring(0, 8); - var byte2 = str.substring(8, 16); - return [parseInt( byte1, 2), parseInt(byte2, 2)]; + return res; }; if (password == null) { @@ -226,10 +226,10 @@ } for (var i = 0; i < passwordLength; i++) { - var passwordByte = passwordBytes[i]; - var encryptionMatrixIndex = i + (_encryptionMatrix.length - passwordLength); + let passwordByte = passwordBytes[i]; + let encryptionMatrixIndex = i + (_encryptionMatrix.length - passwordLength); - let bitArray = byteToBits(passwordByte)//.ToBitArray(); + let bitArray = byteToBits(passwordByte); for (let j = 0; j < _encryptionMatrix[0].length; j++) { let isSet = bitArray[j]; @@ -251,19 +251,12 @@ let sequence1 = parseInt(bitSequence1,2); let sequence7FFF = parseInt(bitSequence7FFF,2); - /*for (let i = passwordLength - 1; i >= 0; i--) { - let passwordByte = passwordBytes[i]; - lowOrderBitSequence = (((lowOrderBitSequence >> 14) & bitSequence1) | ((lowOrderBitSequence << 1) & bitSequence7FFF)) ^ getBitsSequence([0x00, passwordByte]); - }*/ for (let i = passwordLength - 1; i >= 0; i--) { let passwordByte = passwordBytes[i]; let passwordBits = parseInt(getBitsSequence([0x00, passwordByte], true), 2); lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ passwordBits; } - //lowOrderBitSequence = (lowOrderSequence).toString(2); - //lowOrderSequence = getFull2Byte(lowOrderBitSequence); - let bitTest1 = getBitsSequence([0x00, passwordLength], true); let test1 = parseInt(bitTest1, 2); let bitTest2 = getBitsSequence([0xCE, 0x4B], true); @@ -271,9 +264,7 @@ lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ test1 ^ test2; lowOrderWord = toByteArray((getFull2Byte((lowOrderSequence).toString(2)))); - var key = highOrderWord.concat(lowOrderWord).reverse(); - - + return getHexStr(highOrderWord.concat(lowOrderWord).reverse()); }; window['AscCommon'].HashAlgs = { From acca60579045d5c550e97e92f74d4b2656b53e40 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 13 Oct 2022 18:07:22 +0300 Subject: [PATCH 24/26] [de] Prepare password --- common/hash/hash.js | 81 ++++++++++++++++++--------------------------- word/api.js | 14 ++++++-- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/common/hash/hash.js b/common/hash/hash.js index 7b40f05f54..a66d7bead4 100644 --- a/common/hash/hash.js +++ b/common/hash/hash.js @@ -163,7 +163,7 @@ [[0x33, 0x31], [0x66, 0x62], [0xCC, 0xC4], [0x89, 0xA9], [0x03, 0x73], [0x06, 0xE6], [0x0D, 0xCC]], [[0x10, 0x21], [0x20, 0x42], [0x40, 0x84], [0x81, 0x08], [0x12, 0x31], [0x24, 0x62], [0x48, 0xC4]]]; - var byteToBits = function (byte) { + let byteToBits = function (byte) { let bitsArr = []; for (let i = 0; i < 8; i++) { let bit = byte & (1 << i) ? 1 : 0; @@ -171,37 +171,12 @@ } return bitsArr; }; - var getBitsSequence = function (bytes, getStr) { - let bitsArr = getStr ? "" : []; - //TODO ++ ? - for (let j = 0; j < bytes.length; j++) { - let byte = bytes[j]; - for (let i = 7; i >= 0; i--) { - let bit = byte & (1 << i) ? 1 : 0; - if (getStr) { - bitsArr += bit; - } else { - bitsArr.push(bit); - } - } - } - - return bitsArr; - }; - - var getFull2Byte = function (str) { - let strLength = str.length; - let beforeStr = "0000000000000000".substring(0, 16 - strLength); - return beforeStr + str; - }; - var toByteArray = function (str) { - let byte1 = str.substring(0, 8); - let byte2 = str.substring(8, 16); - return [parseInt( byte1, 2), parseInt(byte2, 2)]; + let splitByHighLowBytes = function (val) { + return [(val & 0xff00) >> 8, val & 0x00ff]; }; - var getHexStr = function (arr) { + let getHexStr = function (arr) { let res = ""; for (let i = 0; i < arr.length; i++) { res += AscCommon.ByteToHex(arr[i]); @@ -209,6 +184,10 @@ return res; }; + let getUnionBytes = function (byte1, byte2) { + return ((byte1 << 8) | byte2); + }; + if (password == null) { return null; } @@ -216,15 +195,15 @@ let textEncoder = new TextEncoder(); let passwordBytes = textEncoder.encode(password); - passwordBytes = passwordBytes.slice(0, 14); + let maxPasswordLength = 15; + passwordBytes = passwordBytes.slice(0, maxPasswordLength); let passwordLength = passwordBytes.length; - + //high word let highOrderWord = [0x00, 0x00]; if (passwordLength > 0) { highOrderWord = _highOrderWords[passwordLength - 1]; } - for (var i = 0; i < passwordLength; i++) { let passwordByte = passwordBytes[i]; let encryptionMatrixIndex = i + (_encryptionMatrix.length - passwordLength); @@ -242,29 +221,33 @@ } } - let lowOrderWord = [0x00, 0x00]; - let lowOrderBitSequence = getBitsSequence(lowOrderWord, true); - let bitSequence1 = getBitsSequence([0x00, 0x01], true); - let bitSequence7FFF = getBitsSequence([0x7F, 0xFF], true); - - let lowOrderSequence = parseInt(lowOrderBitSequence,2); - let sequence1 = parseInt(bitSequence1,2); - let sequence7FFF = parseInt(bitSequence7FFF,2); + if (!highOrderWord) { + return null; + } + //low word + let lowOrderSequence = 0x00; + let sequence1 = 0x01; + let sequence7FFF = 0x7FFF; for (let i = passwordLength - 1; i >= 0; i--) { let passwordByte = passwordBytes[i]; - let passwordBits = parseInt(getBitsSequence([0x00, passwordByte], true), 2); - lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ passwordBits; + lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ passwordByte; } + lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ passwordLength ^ getUnionBytes(0xCE, 0x4B); - let bitTest1 = getBitsSequence([0x00, passwordLength], true); - let test1 = parseInt(bitTest1, 2); - let bitTest2 = getBitsSequence([0xCE, 0x4B], true); - let test2 = parseInt(bitTest2, 2); - lowOrderSequence = (((lowOrderSequence >> 14) & sequence1) | ((lowOrderSequence << 1) & sequence7FFF)) ^ test1 ^ test2; + let lowOrderWord = splitByHighLowBytes(lowOrderSequence); + if (!lowOrderWord) { + return null; + } + + //full word + let fullWord = highOrderWord.concat(lowOrderWord); + fullWord = fullWord && fullWord.reverse(); + if (!fullWord) { + return null; + } - lowOrderWord = toByteArray((getFull2Byte((lowOrderSequence).toString(2)))); - return getHexStr(highOrderWord.concat(lowOrderWord).reverse()); + return getHexStr(fullWord); }; window['AscCommon'].HashAlgs = { diff --git a/word/api.js b/word/api.js index 1ff489794c..f985441c58 100644 --- a/word/api.js +++ b/word/api.js @@ -12889,7 +12889,7 @@ background-repeat: no-repeat;\ callback(true); } else { //пробуем снять защиту - if (documentProtection && hash && hash[0] === documentProtection.hashValue) { + if (documentProtection && hash && (hash[0] === documentProtection.hashValue || hash[1] === documentProtection.hashValue)) { callback(true); } else { //неверный пароль @@ -12906,8 +12906,16 @@ background-repeat: no-repeat;\ if (password === "") { checkPassword([""]); } else { - var checkHash = {password: AscCommon.prepareWordPassword(password), salt: salt, spinCount: spinCount, alg: AscCommon.fromModelCryptAlgorithmSid(alg)}; - AscCommon.calculateProtectHash([checkHash], checkPassword); + //перед тем, как сгенерировать хэш, мс предварительно преобразовывает пароль + //в мс подходит как преобразованные пароль, так и не преобразованный + //т.е. если сгенерировать вручную хэш из непреобразованного пароля и положить в xml, то документ можно будет разблокировать по первоначальному паролю + var hashPassword = AscCommon.prepareWordPassword(password); + var hashArr = []; + if (hashPassword) { + hashArr.push({password: hashPassword, salt: salt, spinCount: spinCount, alg: AscCommon.fromModelCryptAlgorithmSid(alg)}); + } + hashArr.push({password: password, salt: salt, spinCount: spinCount, alg: AscCommon.fromModelCryptAlgorithmSid(alg)}); + AscCommon.calculateProtectHash(hashArr, checkPassword); } } else { checkPassword(null, true); From a80ebc6005e13fd2546d4d405940999c053be797 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 13 Oct 2022 18:42:47 +0300 Subject: [PATCH 25/26] [de] Fix typo --- word/Editor/DocumentChanges.js | 18 +++++++++--------- word/Editor/DocumentProtection.js | 12 ++++++------ word/Editor/Serialize2.js | 18 +++++++++--------- word/api.js | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/word/Editor/DocumentChanges.js b/word/Editor/DocumentChanges.js index c1d7727ae2..336fe26d8c 100644 --- a/word/Editor/DocumentChanges.js +++ b/word/Editor/DocumentChanges.js @@ -729,7 +729,7 @@ function CChangesDocumentProtection(Class, Old, New) { if (Old && New) { this.OldAlgorithmName = Old.algorithmName; this.OldEdit = Old.edit; - this.OldEnforcment = Old.enforcment; + this.OldEnforcement = Old.enforcement; this.OldFormatting = Old.formatting; this.OldHashValue = Old.hashValue; this.OldSaltValue = Old.saltValue; @@ -746,7 +746,7 @@ function CChangesDocumentProtection(Class, Old, New) { this.NewAlgorithmName = New.algorithmName === Old.algorithmName ? undefined : New.algorithmName; this.NewEdit = New.edit === Old.edit ? undefined : New.edit; - this.NewEnforcment = New.enforcment === Old.enforcment ? undefined : New.enforcment; + this.NewEnforcement = New.enforcement === Old.enforcement ? undefined : New.enforcement; this.NewFormatting = New.formatting === Old.formatting ? undefined : New.formatting; this.NewHashValue = New.hashValue === Old.hashValue ? undefined : New.hashValue; this.NewSaltValue = New.saltValue === Old.saltValue ? undefined : New.saltValue; @@ -763,7 +763,7 @@ function CChangesDocumentProtection(Class, Old, New) { } else { this.OldAlgorithmName = undefined; this.OldEdit = undefined; - this.OldEnforcment = undefined; + this.OldEnforcement = undefined; this.OldFormatting = undefined; this.OldHashValue = undefined; this.OldSaltValue = undefined; @@ -780,7 +780,7 @@ function CChangesDocumentProtection(Class, Old, New) { this.NewAlgorithmName = undefined; this.NewEdit = undefined; - this.NewEnforcment = undefined; + this.NewEnforcement = undefined; this.NewFormatting = undefined; this.NewHashValue = undefined; this.NewSaltValue = undefined; @@ -807,7 +807,7 @@ CChangesDocumentProtection.prototype.Undo = function () { this.Class.algorithmName = this.OldAlgorithmName; this.Class.edit = this.OldEdit; - this.Class.enforcment = this.OldEnforcment; + this.Class.enforcement = this.OldEnforcement; this.Class.formatting = this.OldFormatting; this.Class.hashValue = this.OldHashValue; @@ -834,7 +834,7 @@ CChangesDocumentProtection.prototype.Redo = function () { this.Class.algorithmName = this.NewAlgorithmName; this.Class.edit = this.NewEdit; - this.Class.enforcment = this.NewEnforcment; + this.Class.enforcement = this.NewEnforcement; this.Class.formatting = this.NewFormatting; this.Class.hashValue = this.NewHashValue; this.Class.saltValue = this.NewSaltValue; @@ -876,9 +876,9 @@ CChangesDocumentProtection.prototype.WriteToBinary = function (Writer) { } else { Writer.WriteBool(false); } - if (undefined !== this.NewEnforcment) { + if (undefined !== this.NewEnforcement) { Writer.WriteBool(true); - Writer.WriteBool(this.NewEnforcment); + Writer.WriteBool(this.NewEnforcement); } else { Writer.WriteBool(false); } @@ -977,7 +977,7 @@ CChangesDocumentProtection.prototype.ReadFromBinary = function (Reader) { this.NewEdit = Reader.GetByte(); } if (Reader.GetBool()) { - this.NewEnforcment = Reader.GetBool(); + this.NewEnforcement = Reader.GetBool(); } if (Reader.GetBool()) { this.NewFormatting = Reader.GetBool(); diff --git a/word/Editor/DocumentProtection.js b/word/Editor/DocumentProtection.js index 3433a1ba6c..b963f21366 100644 --- a/word/Editor/DocumentProtection.js +++ b/word/Editor/DocumentProtection.js @@ -63,7 +63,7 @@ function CDocProtect() { this.algorithmName = null; this.edit = null; - this.enforcment = null; + this.enforcement = null; this.formatting = null; this.hashValue = null; this.saltValue = null; @@ -93,8 +93,8 @@ CDocProtect.prototype.Get_Id = function () { CDocProtect.prototype.isOnlyView = function () { return this.edit === Asc.c_oAscEDocProtect.ReadOnly; }; -CDocProtect.prototype.getEnforcment = function () { - return this.enforcment; +CDocProtect.prototype.getEnforcement = function () { + return this.enforcement; }; CDocProtect.prototype.getRestrictionType = function () { var res = null; @@ -152,7 +152,7 @@ CDocProtect.prototype.setFromInterface = function (oProps) { this.cryptAlgorithmSid = oProps.cryptAlgorithmSid; this.hashValue = oProps.hashValue; - this.enforcment = oProps.enforcment; + this.enforcement = oProps.enforcement; }; CDocProtect.prototype.Refresh_RecalcData = function () { }; @@ -161,7 +161,7 @@ CDocProtect.prototype.Copy = function () { var oDocProtect = new CDocProtect(); oDocProtect.algorithmName = this.algorithmName; oDocProtect.edit = this.edit; - oDocProtect.enforcment = this.enforcment; + oDocProtect.enforcement = this.enforcement; oDocProtect.formatting = this.formatting; oDocProtect.hashValue = this.hashValue; oDocProtect.saltValue = this.saltValue; @@ -189,7 +189,7 @@ CDocProtect.prototype.Read_FromBinary2 = function(Reader) };*/ CDocProtect.prototype.asc_getIsPassword = function() { - return this.enforcment ? this.isPassword() : null + return this.enforcement ? this.isPassword() : null }; CDocProtect.prototype.asc_getEditType = function() { diff --git a/word/Editor/Serialize2.js b/word/Editor/Serialize2.js index 43d76ac1f4..068552561e 100644 --- a/word/Editor/Serialize2.js +++ b/word/Editor/Serialize2.js @@ -722,7 +722,7 @@ var c_oSer_SettingsType = { var c_oDocProtect = { AlgorithmName: 0, Edit: 1, - Enforcment: 2, + Enforcement: 2, Formatting: 3, HashValue: 4, SaltValue: 5, @@ -7250,16 +7250,16 @@ function BinarySettingsTableWriter(memory, doc, saveParams) oThis.memory.WriteByte(oDocProtect.algorithmName); }); } - if (oDocProtect.edit) + if (null !== oDocProtect.edit) { this.bs.WriteItem(c_oDocProtect.Edit, function () { oThis.memory.WriteByte(oDocProtect.edit); }); } - if (null !== oDocProtect.enforcment) + if (null !== oDocProtect.enforcement) { - this.bs.WriteItem(c_oDocProtect.Enforcment, function () { - oThis.memory.WriteBool(oDocProtect.enforcment); + this.bs.WriteItem(c_oDocProtect.Enforcement, function () { + oThis.memory.WriteBool(oDocProtect.enforcement); }); } if (null !== oDocProtect.formatting) @@ -8382,9 +8382,9 @@ function BinaryFileReader(doc, openParams) if (docProtection) { //this.Document.applyProtection() var restrictionType = docProtection.getRestrictionType(); - if (/*docProtection.isOnlyView() && false !== docProtection.getEnforcment()*/restrictionType !== null) { + if (/*docProtection.isOnlyView() && false !== docProtection.getEnforcement()*/restrictionType !== null) { api && api.asc_addRestriction(restrictionType); - } else if (false !== docProtection.getEnforcment() ) { + } else if (false !== docProtection.getEnforcement() ) { //TODO ? api && api.asc_addRestriction(Asc.c_oAscRestrictionType.View); } @@ -16825,9 +16825,9 @@ function Binary_SettingsTableReader(doc, oReadResult, stream) { pDocProtect.edit = this.stream.GetUChar(); } - else if (c_oDocProtect.Enforcment == type) + else if (c_oDocProtect.Enforcement == type) { - pDocProtect.enforcment = this.stream.GetUChar() != 0; + pDocProtect.enforcement = this.stream.GetUChar() != 0; } else if (c_oDocProtect.Formatting == type) { diff --git a/word/api.js b/word/api.js index f985441c58..acca15d0b8 100644 --- a/word/api.js +++ b/word/api.js @@ -12845,7 +12845,7 @@ background-repeat: no-repeat;\ props.spinCount = spinCount; props.cryptAlgorithmSid = alg; props.hashValue = calculatedHashValue; - props.enforcment = props.edit && props.edit !== Asc.c_oAscEDocProtect.None; + props.enforcement = props.edit !== Asc.c_oAscEDocProtect.None; oDocument.SetProtection(props); oDocument.UpdateInterface(); From 033727040dfa6af02b2c5f9bcc929ae502d92ff0 Mon Sep 17 00:00:00 2001 From: GoshaZotov Date: Thu, 13 Oct 2022 18:59:01 +0300 Subject: [PATCH 26/26] [de] Fix merge --- common/commonDefines.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/commonDefines.js b/common/commonDefines.js index 2f3db1267e..e7af10f334 100644 --- a/common/commonDefines.js +++ b/common/commonDefines.js @@ -2347,7 +2347,7 @@ var lcid_haLatn = 0x7c68; // Hausa, Latin var changestype_Document_Settings = 77; // Изменение общих настроек документа Document.Settings var changestype_Timing = 78; var changestype_ViewPr = 79; - var changestype_DocumentProtection = 79; + var changestype_DocumentProtection = 80; var changestype_2_InlineObjectMove = 1; // Передвигаем объект в заданную позцию (проверяем место, в которое пытаемся передвинуть) var changestype_2_HdrFtr = 2; // Изменения с колонтитулом