Пример #1
0
        internal static Stream Decrypt(Stream aStream, TEncryptionData Encryption)
        {
            using (TOle2File DataStream = new TOle2File(aStream, false))
            {
                DataStream.SelectStream(XlsxConsts.EncryptionInfoString);
                byte[] RecordHeader = new byte[4 * 2];
                DataStream.Read(RecordHeader, RecordHeader.Length);
                int vMajor = BitOps.GetWord(RecordHeader, 0);
                int vMinor = BitOps.GetWord(RecordHeader, 2);


                if ((vMajor == 0x03 || vMajor == 0x04) && vMinor == 0x02)
                {
                    long Flags = BitOps.GetCardinal(RecordHeader, 4);
                    if (Flags == 0x10)
                    {
                        XlsMessages.ThrowException(XlsErr.ErrNotSupportedEncryption);                //external encryption
                    }
                    return(ReadStandardEncryptionInfo(DataStream, Encryption));
                }
                else if (vMajor == 4 && vMinor == 4 && BitOps.GetCardinal(RecordHeader, 4) == 0x040)
                {
                    return(ReadAgileEncryptionInfo(DataStream, Encryption));
                }

                XlsMessages.ThrowException(XlsErr.ErrNotSupportedEncryption);
                return(null);
            }
        }
Пример #2
0
 internal virtual long TotalSize(TEncryptionData Encryption, bool Repeatable)
 {
     if ((sEOF == null) || (sBOF == null))
     {
         XlsMessages.ThrowException(XlsErr.ErrSectionNotLoaded);
     }
     return(sEOF.TotalSize() + sBOF.TotalSize());
 }
Пример #3
0
 internal TXlsRecordLoader(TOle2File aDataStream, TBiff8XFMap aXFMap, TSST aSST, IFlexCelFontList aFontList, TBorderList aBorderList,
                           TPatternList aPatternList, TEncryptionData aEncryption, TXlsBiffVersion aXlsBiffVersion, TNameRecordList aNames, TVirtualReader VirtualReader)
     : base(aSST, aFontList, aEncryption, aXlsBiffVersion, aXFMap, aNames, VirtualReader)
 {
     DataStream  = aDataStream;
     BorderList  = aBorderList;
     PatternList = aPatternList;
 }
Пример #4
0
 protected TBaseRecordLoader(TSST aSST, IFlexCelFontList aFontList, TEncryptionData aEncryption, TXlsBiffVersion aXlsBiffVersion,
                             TBiff8XFMap aXFMap, TNameRecordList aNames, TVirtualReader aVirtualReader)
 {
     RecordHeader   = new TRecordHeader();
     SST            = aSST;
     FontList       = aFontList;
     Encryption     = aEncryption;
     XlsBiffVersion = aXlsBiffVersion;
     XFMap          = aXFMap;
     Names          = aNames;
     VirtualReader  = aVirtualReader;
 }
Пример #5
0
        internal TPxlRecordLoader(Stream aDataStream, TExternSheetList aExternSheetList,
                                  TEncryptionData aEncryption, TSST aSST, IFlexCelFontList aFontList, TBorderList aBorderList,
                                  TPatternList aPatternList, ExcelFile aWorkbook, TBiff8XFMap aXFMap, int aMainBookXFCount, TNameRecordList aNames, TVirtualReader VirtualReader)
            : base(aSST, aFontList, aEncryption, aWorkbook.XlsBiffVersion, aXFMap, aNames, VirtualReader)
        {
            DataStream         = aDataStream;
            FExternSheetList   = aExternSheetList;
            FormatId           = 233;
            FWorkbook          = aWorkbook;
            BorderList         = aBorderList;
            PatternList        = aPatternList;
            DataStreamLength   = DataStream.Length;
            DataStreamPosition = DataStream.Position;  //cached for performance.

            MainBookXFCount = aMainBookXFCount;
        }
Пример #6
0
        /// <summary>
        /// Fixes the offset for the sheets on the global section. If offsets are wrong, Excel will crash, even when FlexCel
        /// will be able to read the file ok. (FlexCel does not use sheet offsets).
        /// </summary>
        private void FixBoundSheetsOffset(TEncryptionData Encryption, bool Repeatable)
        {
            Globals.SST.FixRefs();
            long TotalOfs = Globals.TotalSize(Encryption, Repeatable);  //Includes the EOF on workbook Globals

            if (Globals.SheetCount != Sheets.Count)
            {
                XlsMessages.ThrowException(XlsErr.ErrExcelInvalid);
            }

            for (int i = 0; i < Globals.SheetCount; i++)
            {
                Globals.SheetSetOffset(i, TotalOfs);
                TotalOfs += (Sheets[i].TotalSize(Encryption, Repeatable));
            }
        }
Пример #7
0
        private static Stream ReadAgileEncryptionInfo(TOle2File DataStream, TEncryptionData Encryption)
        {
            byte[] Enc = new byte[DataStream.Length - DataStream.Position];
            DataStream.Read(Enc, Enc.Length);

            TEncryptionParameters DataEncParams = new TEncryptionParameters();
            TAgileEncryptionKey   DataKey       = new TAgileEncryptionKey();

            TAgileEncryptionVerifier KeyVerifier  = new TAgileEncryptionVerifier();
            TEncryptionParameters    KeyEncParams = new TEncryptionParameters();
            TAgileEncryptionKey      KeyKey       = new TAgileEncryptionKey();

            using (MemoryStream ms = new MemoryStream(Enc))
            {
                using (XmlReader xml = XmlReader.Create(ms))
                {
                    xml.ReadStartElement("encryption"); //goes to keyData

                    ReadAgileCipherParams(xml, DataEncParams, DataKey);

                    xml.ReadStartElement("keyData"); //goes to dataIntegrity

                    //We are not checking data integrity at the moment.
                    //DataIntegrity.EncryptedHMacKey = Convert.FromBase64String(xml.GetAttribute("encryptedHmacKey"));
                    //DataIntegrity.EncryptedHmacValue = Convert.FromBase64String(xml.GetAttribute("encryptedHmacValue"));

                    xml.ReadStartElement("dataIntegrity"); //goes to keyEncryptors
                    xml.ReadStartElement("keyEncryptors"); //goes to keyEncryptor
                    xml.ReadStartElement("keyEncryptor");  //goes to encryptedKey
                    KeyKey.SpinCount = Convert.ToInt32(xml.GetAttribute("spinCount"), CultureInfo.InvariantCulture);
                    ReadAgileCipherParams(xml, KeyEncParams, KeyKey);
                    KeyVerifier.EncryptedVerifierHashInput = Convert.FromBase64String(xml.GetAttribute("encryptedVerifierHashInput"));
                    KeyVerifier.EncryptedVerifierHashValue = Convert.FromBase64String(xml.GetAttribute("encryptedVerifierHashValue"));
                    KeyVerifier.EncryptedKeyValue          = Convert.FromBase64String(xml.GetAttribute("encryptedKeyValue"));
                }
            }

            CheckPassword(Encryption, KeyVerifier, KeyEncParams, KeyKey);
            DataKey.Key      = KeyKey.Key;
            DataKey.Password = KeyKey.Password;
            DataKey.CalcDataIV(0);
            return(DecryptStream(DataStream, DataEncParams, DataKey));
        }
Пример #8
0
        private void EndLoading(TProtection Protection, TEncryptionData Encryption, TSST SST)
        {
            // References from LABELSST to SST have been loaded, we can sort
            // Globals.SST.Sort(); Not needed here, we are using a hash and entries are automatically sorted.
            SST.ClearIndexData(); //All labelSST records have been loaded. We can go on...

            //now we can safely sort, all BSEs are pointers, no integers
            if (Globals.DrawingGroup.RecordCache.BStore != null)
            {
                Globals.DrawingGroup.RecordCache.BStore.ContainedRecords.Sort();
            }

            FixRows();
            if (Encryption != null)
            {
                Protection.OpenPassword = Encryption.ReadPassword;                     //The read password might have been modified if the event was used.
            }
            Loaded = true;
        }
Пример #9
0
        private static void CheckPassword(TEncryptionData Encryption, TEncryptionVerifier Verifier, TEncryptionParameters EncParams, TEncryptionKey Key)
        {
            if (Verifier.VerifyPass(XlsConsts.EmptyExcelPassword, EncParams, Key))
            {
                return; //workbook password protected
            }

            string Password = Encryption.ReadPassword;

            if (Encryption.OnPassword != null)
            {
                OnPasswordEventArgs ea = new OnPasswordEventArgs(Encryption.Xls);
                Encryption.OnPassword(ea);
                Encryption.ReadPassword = ea.Password;
                Password = ea.Password;
            }

            if (!Verifier.VerifyPass(Password, EncParams, Key))
            {
                XlsMessages.ThrowException(XlsErr.ErrInvalidPassword);
            }
        }
Пример #10
0
        private static Stream ReadStandardEncryptionInfo(TOle2File DataStream, TEncryptionData Encryption)
        {
            byte[] RecordHeaderLen = new byte[4];
            DataStream.Read(RecordHeaderLen, RecordHeaderLen.Length);

            long EncryptionHeaderSize = BitOps.GetCardinal(RecordHeaderLen, 0);

            byte[] EncryptionHeader = new byte[EncryptionHeaderSize];
            DataStream.Read(EncryptionHeader, EncryptionHeader.Length);
            long AlgId   = BitOps.GetCardinal(EncryptionHeader, 8);
            long KeyBits = BitOps.GetCardinal(EncryptionHeader, 16);

            TEncryptionParameters EncParams = TEncryptionParameters.CreateStandard(GetStandardEncAlg(AlgId));

            byte[] VerifierBytes = new byte[DataStream.Length - DataStream.Position];
            DataStream.Read(VerifierBytes, VerifierBytes.Length);
            TStandardEncryptionVerifier Verifier = ReadStandardVerifier(VerifierBytes);
            TEncryptionKey Key = new TStandardEncryptionKey(ReadStandardSalt(VerifierBytes), (int)KeyBits / 8);

            CheckPassword(Encryption, Verifier, EncParams, Key);

            return(DecryptStream(DataStream, EncParams, Key));
        }
Пример #11
0
 internal override long TotalRangeSize(int SheetIndex, TXlsCellRange CellRange, TEncryptionData Encryption, bool Repeatable)
 {
     return(base.TotalRangeSize(SheetIndex, CellRange, Encryption, Repeatable) +
            TotalSize(Encryption, Repeatable, SheetIndex));
 }
Пример #12
0
 internal override long TotalSize(TEncryptionData Encryption, bool Repeatable)
 {
     return(base.TotalSize(Encryption, Repeatable) +
            TotalSize(Encryption, Repeatable, -1));
 }
Пример #13
0
        internal long TotalSize(TEncryptionData Encryption, bool Repeatable, int SheetIndex)
        {
            return
                (FileEncryption.TotalSize() +
                 Encryption.TotalSize() +

                 TTemplateRecord.GetSize(FIsXltTemplate) +
                 ts(CodePage) +

                 FLel.TotalSize +
                 TDSFRecord.StandardSize() +
                 ts(Excel9File) +

                 (SheetIndex < 0 ? FBoundSheets.TabIdsTotalSize() : 0)
                 +
                 ts(GetMacroRec(ObjProj)) +
                 ts(GetMacroRec(ObNoMacros)) +
                 ts(CodeNameRecord) +

                 FFnGroups.TotalSize +
                 ts(OleObjectSize) +
                 FWorkbookProtection.TotalSize() +
                 ts(Window1) +
                 TBackupRecord.StandardSize() +
                 THideObjRecord.StandardSize() +
                 T1904Record.StandardSize() +

                 TPrecisionRecord.StandardSize() +
                 TRefreshAllRecord.StandardSize() +

                 ts(BookBool) +
                 Fonts.TotalSize +
                 Formats.TotalSize +
                 StyleXF.SizeWithXFExt(this, CellXF) +
                 DXF.TotalSize +
                 Styles.TotalSize +
                 TableStyles.TotalSize +
                 ts(Palette) +
                 ts(ClrtClient) +

                 PivotCache.TotalSize +
                 DocRoute.TotalSize +

                 UserBView.TotalSize +
                 TUsesELFsRecord.StandardSize() +

                 (SheetIndex < 0 ?
                  FBoundSheets.TotalSize() +
                  MetaData.TotalSize
                :
                  FBoundSheets.TotalRangeSize(SheetIndex)
                 )

                 +

                 ts(MTRSettings) +
                 ts(ForceFullCalculation) +
                 ts(Country) +

                 FReferences.TotalSize() +
                 FNames.TotalSize +
                 RealTimeData.TotalSize +
                 ts(RecalcId) +

                 (SheetIndex < 0 ?
                  FHeaderImages.TotalSize() +
                  FDrawingGroup.TotalSize()
                :
                  0)
                 +

                 FSST.TotalSize(Repeatable) +
                 WebPub.TotalSize +
                 ts(WOpt) +
                 //CrErr is ignored
                 ts(BookExt) +
                 FeatHdr.TotalSize +
                 DConn.TotalSize +
                 ts(ThemeRecord) +
                 ts(CompressPictures) +
                 ts(Compat12) +
                 ts(GUIDTypeLib) +
                 FFutureRecords.TotalSize);
        }
Пример #14
0
 protected TBinRecordLoader(TSST aSST, IFlexCelFontList aFontList, TEncryptionData aEncryption,
                            TXlsBiffVersion aXlsBiffVersion, TBiff8XFMap aXFMap, TNameRecordList aNames, TVirtualReader aVirtualReader)
     : base(aSST, aFontList, aEncryption, aXlsBiffVersion, aXFMap, aNames, aVirtualReader)
 {
 }
Пример #15
0
        private void FixRangeBoundSheetsOffset(int SheetIndex, TXlsCellRange CellRange, TEncryptionData Encryption, bool Repeatable)
        {
            Globals.SST.FixRefs();
            long TotalOfs = Globals.TotalRangeSize(SheetIndex, CellRange, Encryption, Repeatable);  //Includes the EOF on workbook Globals

            if (Globals.SheetCount != Sheets.Count)
            {
                XlsMessages.ThrowException(XlsErr.ErrExcelInvalid);
            }

            Globals.SheetSetOffset(SheetIndex, TotalOfs);
        }