private void SaveButton_Click(object sender, EventArgs e) { UpdateList lst = new UpdateList(); List <Update> updates = new List <Update>(); foreach (Update u in listBox1.Items) { updates.Add(u); } lst.Updates = updates.ToArray(); lst.SignedHash = ""; System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(); rsa.FromXmlString(m_privateKey); System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(UpdateList)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { sr.Serialize(ms, lst); ms.Position = 0; lst.SignedHash = Convert.ToBase64String(rsa.SignData(ms, System.Security.Cryptography.SHA1.Create())); } using (System.IO.FileStream fs = new System.IO.FileStream(UpdateFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)) sr.Serialize(fs, lst); }
private void SaveButton_Click(object sender, EventArgs e) { UpdateList lst = new UpdateList(); List<Update> updates = new List<Update>(); foreach (Update u in listBox1.Items) updates.Add(u); lst.Updates = updates.ToArray(); lst.SignedHash = ""; System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(); rsa.FromXmlString(m_privateKey); System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(UpdateList)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { sr.Serialize(ms, lst); ms.Position = 0; lst.SignedHash = Convert.ToBase64String(rsa.SignData(ms, System.Security.Cryptography.SHA1.Create())); } using (System.IO.FileStream fs = new System.IO.FileStream(UpdateFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)) sr.Serialize(fs, lst); }
private void UpdateAdministration_Load(object sender, EventArgs e) { if (!System.IO.File.Exists("releasekey.xml")) { if (MessageBox.Show("Signature key does not exist, create it?", Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes) { Application.Exit(); return; } System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(); using (System.IO.StreamWriter sw = new System.IO.StreamWriter("releasekey.xml", false)) sw.Write(rsa.ToXmlString(true)); } using (System.IO.StreamReader rd = new System.IO.StreamReader("releasekey.xml")) m_privateKey = rd.ReadToEnd(); List <Update> updates = new List <Update>(); System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(UpdateList)); UpdateList lst = null; if (System.IO.File.Exists(UpdateFile)) { using (System.IO.FileStream fs = new System.IO.FileStream(UpdateFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) lst = (UpdateList)sr.Deserialize(fs); } SortedList <string, string> applicationNames = new SortedList <string, string>(); SortedList <string, string> architectures = new SortedList <string, string>(); if (lst != null && lst.Updates != null) { foreach (Update u in lst.Updates) { listBox1.Items.Add(u); if (!string.IsNullOrEmpty(u.ApplicationName)) { applicationNames[u.ApplicationName.ToLower().Trim()] = u.ApplicationName; } if (!string.IsNullOrEmpty(u.Architecture)) { architectures[u.Architecture.ToLower().Trim()] = u.Architecture; } } } foreach (string s in architectures.Values) { if (UpdateArchitecture.FindString(s) < 0) { UpdateArchitecture.Items.Add(s); } } foreach (string s in applicationNames.Values) { if (UpdateApplication.FindString(s) < 0) { UpdateApplication.Items.Add(s); } } }
/// <summary> /// Initiates an syncronous check for updates /// </summary> /// <param name="force">A value indicating if the duration and user-enabled check should be bypassed</param> public void CheckForUpdates(bool force) { try { if (m_config == null) { System.Xml.Serialization.XmlSerializer src = new System.Xml.Serialization.XmlSerializer(typeof(Config)); using (System.IO.FileStream fs = new System.IO.FileStream(m_configFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) m_config = (Config)src.Deserialize(fs); } //This throws an exception if somethings broken m_config.CheckValid(); if (!m_config.Enabled && !force) { return; } if (m_lastCheck == null) { string file = m_config.ApplicationName + ".xml"; foreach (char c in System.IO.Path.GetInvalidFileNameChars()) { file = file.Replace(c, '-'); } file = System.IO.Path.Combine(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FreshKeeper"), file); if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(file))) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(file)); } if (System.IO.File.Exists(file)) { System.Xml.Serialization.XmlSerializer srl = new System.Xml.Serialization.XmlSerializer(typeof(LastCheck)); using (System.IO.FileStream fs = new System.IO.FileStream(m_configFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) m_lastCheck = (LastCheck)srl.Deserialize(fs); } else { m_lastCheck = new LastCheck(); } } if (Duplicati.Library.Core.Timeparser.ParseTimeInterval(m_config.CheckInterval, m_lastCheck.Time) > DateTime.Now) { return; } Random r = new Random(); string url = m_config.Urls[r.Next(0, m_config.Urls.Length)]; System.Net.WebClient wc = new System.Net.WebClient(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.PreserveWhitespace = true; //Make sure we don't alter the document using (System.IO.MemoryStream ms = new System.IO.MemoryStream(wc.DownloadData(url))) doc.Load(ms); string hash = doc["UpdateList"].Attributes["SignedHash"].Value; doc["UpdateList"].Attributes["SignedHash"].Value = ""; System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(); rsa.FromXmlString(m_config.PublicKey); UpdateList lst = null; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { doc.Save(ms); if (!rsa.VerifyData(ms.ToArray(), System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA1"), Convert.FromBase64String(hash))) { throw new Exception("Failed to verify signature"); } ms.Position = 0; System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(UpdateList)); lst = (UpdateList)sr.Deserialize(ms); lst.SignedHash = hash; } if (lst == null || lst.Updates == null || lst.Updates.Length == 0) { return; } Update newest = lst.Updates[0]; foreach (Update u in lst.Updates) { if (u.Version > newest.Version && (!u.BugfixUpdate || (u.BugfixUpdate && m_config.NotifyOnRevisionChange))) { newest = u; } } if (newest.Version > m_config.LocalVersion) { if (Updateavailable != null) { Updateavailable(this, newest); } } } catch (Exception ex) { RaiseErrorEvent(ex); return; } }