forked from Ylianst/MeshCentralRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateForm.cs
169 lines (149 loc) · 7.32 KB
/
UpdateForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright 2009-2022 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Net.Security;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace MeshCentralRouter
{
public partial class UpdateForm : Form
{
private string url = null;
private string hash = null;
private int size = 0;
private string[] args = null;
private string serverTlsCertHash = null;
public UpdateForm(string url, string hash, int size, string[] args, string serverhash)
{
InitializeComponent();
Translate.TranslateControl(this);
this.url = url;
this.hash = hash;
this.size = size;
this.args = args;
this.serverTlsCertHash = serverhash;
try
{
FileInfo selfExe = new FileInfo(Assembly.GetExecutingAssembly().Location);
string[] lines = File.ReadAllLines(Path.Combine(selfExe.Directory.FullName, @"customization\customize.txt"));
if (lines[4] != "") { this.Text = lines[4]; }
if (lines[5] != "") { mainLabel.Text = lines[5]; }
}
catch (Exception) { }
}
private void okButton_Click(object sender, EventArgs e)
{
DownloadUpdate();
}
private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
updateProgressBar.Value = e.ProgressPercentage;
}
private void DownloadUpdate()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
Uri x = webRequest.RequestUri;
webRequest.Method = "GET";
webRequest.Timeout = 10000;
webRequest.BeginGetResponse(new AsyncCallback(DownloadUpdateRespone), webRequest);
webRequest.ServerCertificateValidationCallback += RemoteCertificateValidationCallback;
updateProgressBar.Visible = true;
}
public bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// Check MeshCentral server's TLS certificate. This is our first security layer.
if ((serverTlsCertHash != null) && (serverTlsCertHash != certificate.GetCertHashString().ToLower()) && (serverTlsCertHash != GetMeshKeyHash(certificate).ToLower()) && (serverTlsCertHash != GetMeshCertHash(certificate).ToLower())) return false;
return true;
}
public delegate void updateProgressHandler(int ptr, int total);
public void updateProgress(int ptr, int total)
{
if (this.InvokeRequired) { this.Invoke(new updateProgressHandler(updateProgress), ptr, total); return; }
updateProgressBar.Visible = true;
updateProgressBar.Maximum = total;
if (ptr <= total) { updateProgressBar.Value = ptr; } else { updateProgressBar.Value = total; }
}
public delegate void updateMessageHandler(string msg, int buttons);
public void updateMessage(string msg, int buttons)
{
if (this.InvokeRequired) { this.Invoke(new updateMessageHandler(updateMessage), msg, buttons); return; }
mainLabel.Text = msg;
okButton.Enabled = ((buttons & 1) != 0);
cancelButton.Enabled = ((buttons & 2) != 0);
updateProgressBar.Visible = ((buttons & 4) != 0);
}
private void DownloadUpdateRespone(IAsyncResult asyncResult)
{
long received = 0;
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
try
{
// Hash our own executable
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult))
{
byte[] buffer = new byte[4096];
FileStream fileStream = File.OpenWrite(System.Reflection.Assembly.GetEntryAssembly().Location + ".update.exe");
using (Stream input = webResponse.GetResponseStream())
{
int size = input.Read(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
received += size;
updateProgress((int)received, (int)size);
size = input.Read(buffer, 0, buffer.Length);
}
}
fileStream.Flush();
fileStream.Close();
// Hash the resulting file
byte[] downloadHash;
using (var sha384 = SHA384Managed.Create()) { using (var stream = File.OpenRead(System.Reflection.Assembly.GetEntryAssembly().Location + ".update.exe")) { downloadHash = sha384.ComputeHash(stream); } }
string downloadHashHex = BitConverter.ToString(downloadHash).Replace("-", string.Empty).ToLower();
if (downloadHashHex != hash) {
updateMessage(Translate.T(Properties.Resources.InvalidDownload), 2);
File.Delete(System.Reflection.Assembly.GetEntryAssembly().Location + ".update.exe");
} else {
updateMessage(Translate.T(Properties.Resources.Updating), 0);
Process.Start(System.Reflection.Assembly.GetEntryAssembly().Location + ".update.exe", "-update:" + System.Reflection.Assembly.GetEntryAssembly().Location + " " + string.Join(" ", args));
Application.Exit();
}
}
}
catch (Exception ex) { updateMessage("Error: " + ex.ToString(), 2); }
}
// Return a modified base64 SHA384 hash string of the certificate public key
public static string GetMeshKeyHash(X509Certificate cert)
{
return ByteArrayToHexString(new SHA384Managed().ComputeHash(cert.GetPublicKey()));
}
// Return a modified base64 SHA384 hash string of the certificate
public static string GetMeshCertHash(X509Certificate cert)
{
return ByteArrayToHexString(new SHA384Managed().ComputeHash(cert.GetRawCertData()));
}
public static string ByteArrayToHexString(byte[] Bytes)
{
StringBuilder Result = new StringBuilder(Bytes.Length * 2);
string HexAlphabet = "0123456789ABCDEF";
foreach (byte B in Bytes) { Result.Append(HexAlphabet[(int)(B >> 4)]); Result.Append(HexAlphabet[(int)(B & 0xF)]); }
return Result.ToString();
}
}
}