SebWindowsClient.ConfigurationUtils.SEBConfigFileManager.DecryptSEBSettings C# (CSharp) Method

DecryptSEBSettings() public static method

Decrypt and deserialize SEB settings When forEditing = true, then the decrypting password the user entered and/or certificate reference found in the .seb file is returned
public static DecryptSEBSettings ( byte sebData, bool forEditing, string &sebFilePassword, bool &passwordIsHash, X509Certificate2 &sebFileCertificateRef ) : Dictionary,System.Collections.Generic
sebData byte
forEditing bool
sebFilePassword string
passwordIsHash bool
sebFileCertificateRef System.Security.Cryptography.X509Certificates.X509Certificate2
return Dictionary,System.Collections.Generic
        public static DictObj DecryptSEBSettings(byte[] sebData, bool forEditing, ref string sebFilePassword, ref bool passwordIsHash, ref X509Certificate2 sebFileCertificateRef)
        {
            // Ungzip the .seb (according to specification >= v14) source data
            byte[] unzippedSebData = GZipByte.Decompress(sebData);

            // if unzipped data is not null, then unzipping worked, we use unzipped data
            // if unzipped data is null, then the source data may be an uncompressed .seb file, we proceed with it
            if (unzippedSebData != null) sebData = unzippedSebData;

            string prefixString;

            // save the data including the first 4 bytes for the case that it's acutally an unencrypted XML plist
            byte[] sebDataUnencrypted = sebData.Clone() as byte[];

            // Get 4-char prefix
            prefixString = GetPrefixStringFromData(ref sebData);

            //// Check prefix identifying encryption modes

            // Prefix = pkhs ("Public Key Hash") ?

            if (prefixString.CompareTo(PUBLIC_KEY_HASH_MODE) == 0)
            {

                // Decrypt with cryptographic identity/private key
                sebData = DecryptDataWithPublicKeyHashPrefix(sebData, forEditing, ref sebFileCertificateRef);
                if (sebData == null)
                {
                    return null;
                }

                // Get 4-char prefix again
                // and remaining data without prefix, which is either plain or still encoded with password
                prefixString = GetPrefixStringFromData(ref sebData);
            }

            // Prefix = pswd ("Password") ?

            if (prefixString.CompareTo(PASSWORD_MODE) == 0)
            {

                // Decrypt with password
                // if the user enters the right one
                byte[] sebDataDecrypted = null;
                string password;
                // Allow up to 5 attempts for entering decoding password
                string enterPasswordString = SEBUIStrings.enterPassword;
                int i = 5;
                do
                {
                    i--;
                    // Prompt for password
                    password = ThreadedDialog.ShowPasswordDialogForm(SEBUIStrings.loadingSettings, enterPasswordString);
                    if (password == null) return null;
                    //error = nil;
                    sebDataDecrypted = SEBProtectionController.DecryptDataWithPassword(sebData, password);
                    enterPasswordString = SEBUIStrings.enterPasswordAgain;
                    // in case we get an error we allow the user to try it again
                } while ((sebDataDecrypted == null) && i > 0);
                if (sebDataDecrypted == null)
                {
                    //wrong password entered in 5th try: stop reading .seb file
                    SEBMessageBox.Show(SEBUIStrings.decryptingSettingsFailed, SEBUIStrings.decryptingSettingsFailedReason, MessageBoxIcon.Error, MessageBoxButtons.OK, neverShowTouchOptimized: forEditing);
                    return null;
                }
                sebData = sebDataDecrypted;
                // If these settings are being decrypted for editing, we return the decryption password
                if (forEditing) sebFilePassword = password;
            }
            else
            {

                // Prefix = pwcc ("Password Configuring Client") ?

                if (prefixString.CompareTo(PASSWORD_CONFIGURING_CLIENT_MODE) == 0)
                {

                    // Decrypt with password and configure local client settings
                    // and quit afterwards, returning if reading the .seb file was successfull
                    DictObj sebSettings = DecryptDataWithPasswordForConfiguringClient(sebData, forEditing, ref sebFilePassword, ref passwordIsHash);
                    return sebSettings;

                }
                else
                {

                    // Prefix = plnd ("Plain Data") ?

                    if (prefixString.CompareTo(PLAIN_DATA_MODE) != 0)
                    {
                        // No valid 4-char prefix was found in the .seb file
                        // Check if .seb file is unencrypted
                        if (prefixString.CompareTo(UNENCRYPTED_MODE) == 0)
                        {
                            // .seb file seems to be an unencrypted XML plist
                            // get the original data including the first 4 bytes
                            sebData = sebDataUnencrypted;
                        }
                        else
                        {
                            // No valid prefix and no unencrypted file with valid header
                            // cancel reading .seb file
                            SEBMessageBox.Show(SEBUIStrings.settingsNotUsable, SEBUIStrings.settingsNotUsableReason, MessageBoxIcon.Error, MessageBoxButtons.OK, neverShowTouchOptimized: forEditing);
                            return null;
                        }
                    }
                }
            }
            // If we don't deal with an unencrypted seb file
            // ungzip the .seb (according to specification >= v14) decrypted serialized XML plist data
            if (prefixString.CompareTo(UNENCRYPTED_MODE) != 0)
            {
                sebData = GZipByte.Decompress(sebData);
            }

            // Get preferences dictionary from decrypted data
            DictObj sebPreferencesDict = GetPreferencesDictFromConfigData(sebData, forEditing);
            // If we didn't get a preferences dict back, we abort reading settings
            if (sebPreferencesDict == null) return null;

            // We need to set the right value for the key sebConfigPurpose to know later where to store the new settings
            sebPreferencesDict[SEBSettings.KeySebConfigPurpose] = (int)SEBSettings.sebConfigPurposes.sebConfigPurposeStartingExam;

            // Reading preferences was successful!
            return sebPreferencesDict;
        }

Usage Example

        public static bool StoreDecryptedSEBSettings(byte[] sebData)
        {
            string                      sebFilePassword       = (string)null;
            bool                        passwordIsHash        = false;
            X509Certificate2            sebFileCertificateRef = (X509Certificate2)null;
            Dictionary <string, object> settingsDict          = SEBConfigFileManager.DecryptSEBSettings(sebData, false, ref sebFilePassword, ref passwordIsHash, ref sebFileCertificateRef);

            if (settingsDict == null)
            {
                return(false);
            }
            Logger.AddInformation("Reconfiguring", (object)null, (Exception)null, (string)null);
            SEBClientInfo.SebWindowsClientForm.closeSebClient = false;
            Logger.AddInformation("Attempting to CloseSEBForm for reconfiguration", (object)null, (Exception)null, (string)null);
            SEBClientInfo.SebWindowsClientForm.CloseSEBForm();
            Logger.AddInformation("Succesfully CloseSEBForm for reconfiguration", (object)null, (Exception)null, (string)null);
            SEBClientInfo.SebWindowsClientForm.closeSebClient = true;
            SEBClientInfo.CreateNewDesktopOldValue            = (bool)SEBSettings.valueForDictionaryKey(SEBSettings.settingsCurrent, "createNewDesktop");
            if ((int)settingsDict["sebConfigPurpose"] == 0)
            {
                Logger.AddInformation("Reconfiguring to start an exam", (object)null, (Exception)null, (string)null);
                Logger.AddInformation("Attempting to StoreSebClientSettings", (object)null, (Exception)null, (string)null);
                SEBSettings.StoreSebClientSettings(settingsDict);
                Logger.AddInformation("Successfully StoreSebClientSettings", (object)null, (Exception)null, (string)null);
                SEBClientInfo.examMode = true;
                SEBClientInfo.InitializeLogger();
                if (SEBClientInfo.CreateNewDesktopOldValue != (bool)SEBSettings.valueForDictionaryKey(SEBSettings.settingsCurrent, "createNewDesktop"))
                {
                    if (!SEBClientInfo.CreateNewDesktopOldValue)
                    {
                        int num1 = (int)SEBMessageBox.Show(SEBUIStrings.settingsRequireNewDesktop, SEBUIStrings.settingsRequireNewDesktopReason, MessageBoxIcon.Hand, MessageBoxButtons.OK, false);
                    }
                    else
                    {
                        int num2 = (int)SEBMessageBox.Show(SEBUIStrings.settingsRequireNotNewDesktop, SEBUIStrings.settingsRequireNotNewDesktopReason, MessageBoxIcon.Hand, MessageBoxButtons.OK, false);
                    }
                    SEBClientInfo.SebWindowsClientForm.ExitApplication(true);
                }
                Logger.AddInformation("Attemting to InitSEBDesktop for reconfiguration", (object)null, (Exception)null, (string)null);
                if (!SebWindowsClientMain.InitSEBDesktop())
                {
                    return(false);
                }
                Logger.AddInformation("Sucessfully InitSEBDesktop for reconfiguration", (object)null, (Exception)null, (string)null);
                Logger.AddInformation("Attempting to OpenSEBForm for reconfiguration", (object)null, (Exception)null, (string)null);
                int num = SEBClientInfo.SebWindowsClientForm.OpenSEBForm() ? 1 : 0;
                Logger.AddInformation("Successfully OpenSEBForm for reconfiguration", (object)null, (Exception)null, (string)null);
                return(num != 0);
            }
            Logger.AddInformation("Reconfiguring to configure a client", (object)null, (Exception)null, (string)null);
            List <object> objectList = (List <object>)settingsDict["embeddedCertificates"];

            for (int index = objectList.Count - 1; index >= 0; --index)
            {
                Dictionary <string, object> dictionary = (Dictionary <string, object>)objectList[index];
                if ((int)dictionary["type"] == 1)
                {
                    SEBProtectionController.StoreCertificateIntoStore((byte[])dictionary["certificateData"]);
                }
                objectList.RemoveAt(index);
            }
            SEBSettings.StoreSebClientSettings(settingsDict);
            SEBClientInfo.InitializeLogger();
            SEBSettings.WriteSebConfigurationFile(SEBClientInfo.SebClientSettingsAppDataFile, "", false, (X509Certificate2)null, SEBSettings.sebConfigPurposes.sebConfigPurposeConfiguringClient, false);
            if (!SebWindowsClientMain.InitSEBDesktop() || !SEBClientInfo.SebWindowsClientForm.OpenSEBForm())
            {
                return(false);
            }
            if (SEBClientInfo.CreateNewDesktopOldValue != (bool)SEBSettings.valueForDictionaryKey(SEBSettings.settingsCurrent, "createNewDesktop"))
            {
                int num = (int)SEBMessageBox.Show(SEBUIStrings.sebReconfiguredRestartNeeded, SEBUIStrings.sebReconfiguredRestartNeededReason, MessageBoxIcon.Exclamation, MessageBoxButtons.OK, false);
                SEBClientInfo.SebWindowsClientForm.ExitApplication(true);
            }
            if (SEBMessageBox.Show(SEBUIStrings.sebReconfigured, SEBUIStrings.sebReconfiguredQuestion, MessageBoxIcon.Question, MessageBoxButtons.YesNo, false) == DialogResult.No)
            {
                SEBClientInfo.SebWindowsClientForm.ExitApplication(true);
            }
            return(true);
        }