-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
LinuxPlatformServiceImpl.Certificate.cs
207 lines (185 loc) · 7.48 KB
/
LinuxPlatformServiceImpl.Certificate.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#if LINUX
// ReSharper disable once CheckNamespace
using System.Collections;
namespace BD.WTTS.Services.Implementation
{
partial class LinuxPlatformServiceImpl
{
public bool IsCertificateInstalled(byte[] certificate2)
{
var certificate2_ = Serializable.DMP2<X509CertificatePackable>(certificate2);
return IsCertificateInstalledCore(certificate2_);
}
public bool IsCertificateInstalledCore(X509CertificatePackable certificate2)
{
try
{
X509Certificate2? cert = certificate2;
if (cert == null)
return false;
//退出码 404 500 被使用
var shellStr = $"CERT_NAME=\"{CertificateConstants.CertificateName}\"; CERT_RESULT=$(certutil -L -d sql:$HOME/.pki/nssdb | grep \"$CERT_NAME\"); if [ -n \"$CERT_RESULT\" ]; then echo \"证书 '$CERT_NAME' 存在。\"; exit 200; else echo \"证书 '$CERT_NAME' 不存在。\"; exit 10; fi";
var p = Process.Start(Process2.BinBash, new string[] { "-c", shellStr });
p.WaitForExit();
return p.ExitCode == 200;
}
catch (Exception e)
{
Toast.Show(ToastIcon.Error, $"检测证书安装错误:{e}");
return false;
}
}
public bool? TrustRootCertificateAsync(string cerPath)
{
// 使用 Certutil NSS 工具 添加到 sql:$HOME/.pki/nssdb Chrome 信任此储存区
Process.Start(Process2.BinBash, new string[] {
"-c",
$"{Certutil} -A -d sql:$HOME/.pki/nssdb -n \"{CertificateConstants.CertificateName}\" -t C,, -i \"{cerPath}\""
}).WaitForExit();
return RunRootCommand(PkexecPath, new string[] { GetAppHostPath(), "-ceri", CertificateConstants.AppDataDirectory }) == 0;
}
public void RemoveCertificate(byte[] certificate2)
{
// 使用 Certutil NSS 工具 从 sql:$HOME/.pki/nssdb 中删除证书
Process.Start(Process2.BinBash, new string[] {
"-c",
$"{Certutil} -D -d sql:$HOME/.pki/nssdb -n \"{CertificateConstants.CertificateName}\""
}).WaitForExit();
RunRootCommand(PkexecPath, new string[] { GetAppHostPath(), "-cerd", CertificateConstants.AppDataDirectory });
}
public static int RunRootCommand(string path, string[] args)
{
var psi = new ProcessStartInfo
{
FileName = path,
UseShellExecute = false,
CreateNoWindow = false
};
args.ForEach(psi.ArgumentList.Add);
DotNetRuntimeHelper.AddEnvironment(psi);
var p = Process.Start(psi);
p.WaitForExit();
return p.ExitCode;
}
//public void ShowRootCommandError(int code, string msg)
//{
// if (code == 0)
// return;
// if (Enum.TryParse(code.ToString(), out AuthorizationStatus authStatus))
// {
// switch (authStatus)
// {
// case AuthorizationStatus.Canceled:
// Toast.Show(ToastIcon.Warning, msg.Format("授权被取消!"));
// return;
// case AuthorizationStatus.ToolExecuteFailure:
// default:
// Toast.Show(ToastIcon.Error, msg.Format("执行命令失败!"));
// return;
// }
// }
//}
public static bool? TrustRootCertificateCore(string cerPath)
{
if (!File.Exists(cerPath))
return false;
try
{
// 如果存在 /bin/trust 则直接用该命令执行
if (File.Exists(TrustPath))
{
Process.Start(TrustPath, new string[] {
"anchor",
"--store",
cerPath
}).WaitForExit();
return true;
}
else
{
var path = GetCertStore();
var destCertFilePath = Path.Combine(path.CaCertStorePath, CertificateConstants.CerFileName);
if (File.Exists(destCertFilePath) && File.ReadAllBytes(cerPath).SequenceEqual(File.ReadAllBytes(destCertFilePath)))
{
return true;
}
if (!Directory.Exists(path.CaCertStorePath))
return null;
File.Copy(cerPath, destCertFilePath, overwrite: true);
// 刷新系统证书
Process.Start(path.CaCertUpdatePath).WaitForExit();
return true;
}
}
catch (Exception e)
{
Toast.Show(ToastIcon.Error, $"安装证书错误:{e}");
return false;
}
}
public static void RemoveCertificate(string cerPath)
{
// 不存在证书则直接跳过
if (!File.Exists(cerPath))
return;
try
{
// 如果存在 /bin/trust 则直接用该命令执行
if (File.Exists(TrustPath))
{
Process.Start(TrustPath, new string[] {
"anchor",
"--remove",
cerPath
}).WaitForExit();
return;
}
else
{
var path = GetCertStore();
var destCertFilePath = Path.Combine(path.CaCertStorePath, CertificateConstants.CerFileName);
if (File.Exists(destCertFilePath) && File.ReadAllBytes(cerPath).SequenceEqual(File.ReadAllBytes(destCertFilePath)))
{
File.Delete(destCertFilePath);
}
// 刷新系统证书
Process.Start(path.CaCertUpdatePath).WaitForExit();
return;
}
}
catch (Exception e)
{
Toast.Show(ToastIcon.Error, $"删除证书错误:{e}");
return;
}
}
public static (string CaCertUpdatePath, string CaCertStorePath) GetCertStore()
{
if (File.Exists(DebianCaCertUpdatePath))
{
return (DebianCaCertUpdatePath, DebianCaCertStorePath);
}
else
{
return (RedHatCaCertUpdatePath, RedHatCaCertStorePath);
}
}
/// <summary>
/// 使用安装脚本 会要求前置必须安装 该依赖
/// </summary>
const string Certutil = "certutil";
const string PkexecPath = "pkexec";
#region trust
const string TrustPath = "/usr/bin/trust";
#endregion
#region Debian
const string DebianCaCertUpdatePath = "/usr/sbin/update-ca-certificates";
const string DebianCaCertStorePath = "/usr/local/share/ca-certificates";
#endregion
#region RedHat
const string RedHatCaCertUpdatePath = "/usr/bin/update-ca-trust";
const string RedHatCaCertStorePath = "/etc/pki/ca-trust/source/anchors";
#endregion
}
}
#endif