protected override void UnprotectDocument()
 {
     if (this.ProtectionType != Word.WdProtectionType.wdNoProtection)
     {
         protectionTypeValue = this.ProtectionType;
         this.Unprotect(ref securelyStoredPassword);
     }
 }
Beispiel #2
0
        public int TurnOffProtection(string password)
        {
            if (_document == null || _document.ProtectionType == WordOM.WdProtectionType.wdNoProtection)
            {
                return((int)WordOM.WdProtectionType.wdNoProtection);
            }

            WordOM.WdProtectionType oldType = _document.ProtectionType;
            _document.Unprotect(password);
            return((int)oldType);
        }
Beispiel #3
0
 public int TurnOffProtection(string password)
 {
     WordOM.WdProtectionType oldType = _document.ProtectionType;
     _document.Unprotect(password);
     return((int)oldType);
 }
Beispiel #4
0
		private DecryptResult OpenDocument(IContentEncryptionUi ui, string sourceFile, out object doc, WordApplicationDecryptWrapper decryptWrapper)
		{
			bool bUseDummyModifyPassword = false;
			doc = null;
			DecryptResult result = DecryptResult.Skip;

			// TODO - Fix the handling of Modify passwords to correctly validate them.
			// Currently any Modify password is stripped from the document when it is saved.
			// This is due to the fact that we don't correctly verify the Modify password when opening
			// the document and as a result it might be possible for the user to enter an incorrect password.
			// When saving, the incorrect password would still be applied causing problems for the receiver.

			string extension = System.IO.Path.GetExtension(sourceFile);

			switch (extension.ToLower())
			{
				case ".doc":
				case ".docx":
				case ".docm":
				case ".dotx":
				case ".dotm":
					result = this.GetAndCheckPasswords(ui);
					break;
				case ".rtf":
					result = this.GetAndCheckModifyPassword(ui);
					break;
				default:
					result = this.GetAndCheckOpenPassword(ui);
					break;
			}
		  
			if (result == DecryptResult.Ok)
			{
                // Don't use the cache to decrypt because password errors leave word in a state where
				// subsequent calls return RPC_UNAVAIABLE.
			    try
				{
                    doc = decryptWrapper.Decrypt(sourceFile, false, string.IsNullOrEmpty(OpenPassword) ? DUMMY_PASSWORD : OpenPassword, string.IsNullOrEmpty(ModifyPassword) ? DUMMY_PASSWORD : ModifyPassword, AttachmentFileType);

					// If the user typed a Modify or Open password for a document that didn't need one
					// remove it here, otherwise it will be added to the saved document.

                    if (!decryptWrapper.ShouldHandleOpenPassword(doc))
                    {
                        OpenPassword = "";
                    }

                    if (!decryptWrapper.ShouldHandleWritePassword(doc))
					{
						ModifyPassword = "";
					}
					// Bug fix for VE: 97
					// In OfficeXP, casting Microsoft.Office.Interop.Word.DocumentClass to Microsoft.Office.Interop.Word.Document fails at 
					// runtime. Use _Document instead.
					Word12Interop._Document wordDoc = doc as Word12Interop._Document;
                    DisableReadingLayout(wordDoc);
					Logger.LogDebug("Document Protection Type: " + wordDoc.ProtectionType);

					if( wordDoc.ProtectionType != Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection )
					{
						m_typeProtect = wordDoc.ProtectionType;


						if (!CanUnprotectDocument(wordDoc))
						{
							result = this.GetDocumentProtectionPassword(ui);

							if (result == DecryptResult.Ok)
							{
								if (string.IsNullOrEmpty(DocumentProtectionPassword))
								{
									m_typeProtect = Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection;
								}
								else if (m_typeProtect != Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection)
								{									
									object password = DocumentProtectionPassword;
									wordDoc.Unprotect(ref password);

									Logger.LogDebug(string.Format("WordEncryption.OpenDocument: Successfully unprotected document \"{0}\"", sourceFile));								
								}
							}
						}
					}
				}
                catch(UnauthorizedAccessException)
                {
                    Logger.LogInfo("Incorrect password. Exit word, and retry");
					DocumentProtectionPassword = string.Empty;
                    decryptWrapper.ForceQuit();
                    throw;
                }
				catch (COMException ex)
				{                   
					Logger.LogError("Office Open Exception");
					Logger.LogError(ex);

                    // handle exceptions for incorrect encryption passwords and the document preotection password 
                    if( ex.ErrorCode == unchecked((int)0x800A03EC) || ex.ErrorCode == unchecked((int)0x800A156D) )
                    {								
						Logger.LogInfo("Incorrect password. Exit word, and retry");
						DocumentProtectionPassword = string.Empty;
                        decryptWrapper.ForceQuit();
						throw new UnauthorizedAccessException("Failed to open encrypted file - incorrect password");
					}

					throw;
				}
				finally
				{
					// Reset any dummy password which may have been used so that the
					// document doesn't incorrectly get saved with it.
					if (bUseDummyModifyPassword)
					{
						ModifyPassword = "";
					}
				}
			}

			return result;
		}