public Task <UpdateResult> UpdateAsync() { return(Task.Run <UpdateResult>(() => { try { WaitForInitializationToComplete(); var ext = Path.GetExtension(_installerLocation); var tempFile = Path.ChangeExtension(Path.GetTempFileName(), ext); if (!CopyFile(_installerLocation, tempFile)) { return UpdateResult.DownloadFailed; } using (var trustVerifier = new TrustVerifier(tempFile)) { if (!trustVerifier.IsVerified) { return UpdateResult.VerificationFailed; } UpdateMethods.BeginMSIInstall(tempFile); } return UpdateResult.Success; } catch (Exception e) { e.ReportException(); return UpdateResult.Unknown; } })); }
/// <summary> /// Validate a local file for proper signing /// </summary> /// <param name="localFile">The full path to the local file</param> /// <returns>An initialized TrustVerifier object</returns> private static TrustVerifier ValidateLocalFile(string localFile) { TrustVerifier verifier = new TrustVerifier(localFile); if (!verifier.IsVerified) { // TODO : Better error messaging throw new ArgumentException("Untrusted file!", nameof(localFile)); } EventLogger.WriteInformationalMessage("Successfully validated local file: {0}", localFile); return(verifier); }
/// <summary> /// Synchronously update (gets wrapped into a tag) /// </summary> /// <returns>The result of the upgrade operation</returns> private UpdateResult Update() { string tempFile = Path.ChangeExtension(Path.GetTempFileName(), "msi"); // Reset here; in case anything goes wrong in the Interim, the value will reflect that. _installerVerificationStopwatch.Reset(); try { WaitForInitializationToComplete(); if (!TryDownloadInstaller(tempFile)) { return(UpdateResult.DownloadFailed); } // The verification wraps the beginning of the installation to preserve // the integrity of the file by holding an open handle. _installerVerificationStopwatch.Start(); using (var trustVerifier = new TrustVerifier(tempFile)) { if (!trustVerifier.IsVerified) { return(UpdateResult.VerificationFailed); } _installerVerificationStopwatch.Stop(); UpdateMethods.BeginMSIInstall(tempFile); return(UpdateResult.Success); } } catch (Exception e) { e.ReportException(); if (File.Exists(tempFile)) { File.Delete(tempFile); } } finally { _installerVerificationStopwatch.Stop(); } return(UpdateResult.Unknown); }
private void UdpRecieveLoop(IAsyncResult result) { try { _udpClient.BeginReceive(UdpRecieveLoop, null); } catch { //On disposed return; } try { IPEndPoint remote = null; var data = _udpClient.EndReceive(result, ref remote); if (remote == null || data == null || data.Length == 0) { return; } using (var mem = new MemoryStream(data)) { var bin = new BinaryReader(mem); var add = bin.ReadBoolean(); var pubKey = bin.ReadBytes(bin.ReadInt32()); if (pubKey.Compare(PublicKey) || !TrustVerifier.VerifyTrust(pubKey)) { return; } var peer = new PhonemePeer(this, pubKey, new IPEndPoint(remote.Address, NetworkPort)); if (add) { if (!_knownPeersSet.Contains(peer)) { _knownPeersSet.Add(peer); Application.Current.Dispatcher.Invoke(() => KnownPeers.Add(peer)); PeerJoin?.Invoke(peer); } peer.PerformTcpHandshake(); } else { if (!_knownPeersSet.Remove(peer)) { return; } var realPeer = KnownPeers.First(x => x.Equals(peer)); Application.Current.Dispatcher.Invoke(() => KnownPeers.Remove(realPeer)); PeerLeave?.Invoke(realPeer); } } } catch (Exception e) { //Some kind of noise? Ignore this. Debug.WriteLine(e); } }