Exemple #1
0
        static public bool InstallCertificate(byte[] data)
        {
            if (ContainsLoadedCert(data) == true)
            {
                return(true);
            }

            string CertFolder             = SystemInfos.ProgramData + "Certificates\\";
            FilesystemCertificateData cer = new FilesystemCertificateData();

            for (int i = 1; i < 100; i++)
            {
                if (File.Exists(CertFolder + "Certificate" + i.ToString("00") + ".cer") == false)
                {
                    File.WriteAllBytes(CertFolder + "Certificate" + i.ToString("00") + ".cer", data);
                    byte[] d = ApplicationCertificate.Sign(data);
                    if (d != null)
                    {
                        File.WriteAllBytes(CertFolder + "Certificate" + i.ToString("00") + ".sign", d);
                    }
                    FoxEventLog.WriteEventLog("Certificate " + Certificates.GetCN(data) + " installed as ID=" + i.ToString("00"), System.Diagnostics.EventLogEntryType.Information);
                    cer.FSFilename  = "Certificate" + i.ToString("00") + ".cer";
                    cer.Certificate = data;
                    break;
                }
            }

            LoadedCertificates.Add(cer);
            FoxEventLog.WriteEventLog("Certificate " + Certificates.GetCN(data) + " loaded", System.Diagnostics.EventLogEntryType.Information);

            return(true);
        }
Exemple #2
0
        public static void WritePackageList()
        {
            string PackagesFolder = SystemInfos.ProgramData + "Packages\\";

            if (Directory.Exists(PackagesFolder) == false)
            {
                Directory.CreateDirectory(PackagesFolder);
            }
#if DEBUG
            Formatting frm = Formatting.Indented;
#else
            Formatting frm = Formatting.None;
#endif
            byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(LocalPackages, frm));
            File.WriteAllBytes(PackagesFolder + "List.json", data);
            byte[] sign = ApplicationCertificate.Sign(data);
            if (sign == null)
            {
                FoxEventLog.WriteEventLog("Cannot sign package list for saving", System.Diagnostics.EventLogEntryType.Error);
                return;
            }
            else
            {
                File.WriteAllBytes(PackagesFolder + "List.sign", sign);
            }
        }
Exemple #3
0
        public static void WriteEventLogList()
        {
            if (Directory.Exists(SystemInfos.ProgramData) == false)
            {
                Directory.CreateDirectory(SystemInfos.ProgramData);
            }
#if DEBUG
            Formatting frm = Formatting.Indented;
#else
            Formatting frm = Formatting.None;
#endif
            byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(SyncedEventLog, frm));
            File.WriteAllBytes(SystemInfos.ProgramData + "SyncedEventLog.json", data);
            byte[] sign = ApplicationCertificate.Sign(data);
            if (sign == null)
            {
                FoxEventLog.WriteEventLog("Cannot sign synced Event Log for saving", System.Diagnostics.EventLogEntryType.Error);
                return;
            }
            else
            {
                File.WriteAllBytes(SystemInfos.ProgramData + "SyncedEventLog.sign", sign);
            }
        }
Exemple #4
0
        static public bool InstallPolicy(PolicyObject data, Int64 Order)
        {
            if (data == null)
            {
                return(false);
            }
            data.Order = Order;
            string PoliciesFolder = SystemInfos.ProgramData + "Policies\\";
            string Filename       = data.ID.ToString("X8") + "-" + Guid.NewGuid().ToString();

            byte[] pol  = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
            byte[] sign = ApplicationCertificate.Sign(pol);
            if (sign == null)
            {
                FoxEventLog.WriteEventLog("Cannot sign policy for saving", System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            //do we have that policy loaded?... - overwrite that (and store it to an Update Dict)
            if (LoadedPolicyObjects == null)
            {
                LoadedPolicyObjects = new List <LoadedPolicyObject>();
            }

            foreach (LoadedPolicyObject loadedpol in LoadedPolicyObjects)
            {
                if (loadedpol.PolicyObject.ID != data.ID)
                {
                    continue;
                }

                if (UpdatePolicies == null)
                {
                    UpdatePolicies = new Dictionary <LoadedPolicyObject, LoadedPolicyObject>();
                }
                UpdatePolicies.Add(
                    new LoadedPolicyObject()
                {
                    Filename     = loadedpol.Filename,
                    SignFilename = loadedpol.SignFilename,
                    PolicyObject = new PolicyObject()
                    {
                        Condition      = loadedpol.PolicyObject.Condition,
                        DataAddtions1  = loadedpol.PolicyObject.DataAddtions1,
                        DataAddtions2  = loadedpol.PolicyObject.DataAddtions2,
                        DataAddtions3  = loadedpol.PolicyObject.DataAddtions3,
                        DataAddtions4  = loadedpol.PolicyObject.DataAddtions4,
                        DataAddtions5  = loadedpol.PolicyObject.DataAddtions5,
                        Data           = loadedpol.PolicyObject.Data,
                        DT             = loadedpol.PolicyObject.DT,
                        Enabled        = loadedpol.PolicyObject.Enabled,
                        Grouping       = loadedpol.PolicyObject.Grouping,
                        ID             = loadedpol.PolicyObject.ID,
                        MachineID      = loadedpol.PolicyObject.MachineID,
                        Name           = loadedpol.PolicyObject.Name,
                        Order          = loadedpol.PolicyObject.Order,
                        TimeStampCheck = loadedpol.PolicyObject.TimeStampCheck,
                        Type           = loadedpol.PolicyObject.Type,
                        Version        = loadedpol.PolicyObject.Version
                    }
                }
                    ,
                    new LoadedPolicyObject()
                {
                    PolicyObject = data,
                    Filename     = loadedpol.Filename,
                    SignFilename = loadedpol.SignFilename
                });

                loadedpol.PolicyObject = data;

                try
                {
                    File.WriteAllBytes(loadedpol.Filename, pol);
                }
                catch
                {
                    FoxEventLog.WriteEventLog("Cannot save policy", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }

                try
                {
                    File.WriteAllBytes(loadedpol.SignFilename, sign);
                }
                catch
                {
                    try
                    {
                        File.Delete(loadedpol.Filename);
                    }
                    catch
                    {
                    }
                    FoxEventLog.WriteEventLog("Cannot save policy signature", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }

                return(true);
            }

            //no? create the file

            LoadedPolicyObject lobj = new LoadedPolicyObject();

            lobj.PolicyObject = data;
            lobj.Filename     = PoliciesFolder + Filename + ".pol";
            lobj.SignFilename = PoliciesFolder + Filename + ".sign";

            try
            {
                File.WriteAllBytes(lobj.Filename, pol);
            }
            catch
            {
                FoxEventLog.WriteEventLog("Cannot save policy", System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            try
            {
                File.WriteAllBytes(lobj.SignFilename, sign);
            }
            catch
            {
                try
                {
                    File.Delete(lobj.Filename);
                }
                catch
                {
                }
                FoxEventLog.WriteEventLog("Cannot save policy signature", System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            LoadedPolicyObjects.Add(lobj);

            return(true);
        }
Exemple #5
0
        static public bool UpdatePolicyOrder(PolicyObject obj, Int64 Order)
        {
            if (LoadedPolicyObjects == null)
            {
                LoadedPolicyObjects = new List <LoadedPolicyObject>();
            }

            foreach (LoadedPolicyObject pol in LoadedPolicyObjects)
            {
                if (pol.PolicyObject.ID != obj.ID)
                {
                    continue;
                }
                if (pol.PolicyObject.Name != obj.Name)
                {
                    continue;
                }
                if (pol.PolicyObject.Type != obj.Type)
                {
                    continue;
                }
                if (pol.PolicyObject.Version != obj.Version)
                {
                    continue;
                }
                if (pol.PolicyObject.DT != obj.DT)
                {
                    continue;
                }
                pol.PolicyObject.Order = Order;

                byte[] ppol = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pol.PolicyObject));
                byte[] sign = ApplicationCertificate.Sign(ppol);
                if (sign == null)
                {
                    FoxEventLog.WriteEventLog("Cannot sign policy for saving", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }


                try
                {
                    File.WriteAllBytes(pol.Filename, ppol);
                }
                catch
                {
                    FoxEventLog.WriteEventLog("Cannot save policy", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }

                try
                {
                    File.WriteAllBytes(pol.SignFilename, sign);
                }
                catch
                {
                    try
                    {
                        File.Delete(pol.Filename);
                    }
                    catch
                    {
                    }
                    FoxEventLog.WriteEventLog("Cannot save policy signature", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }

                break;
            }
            return(true);
        }