支持net8版本的签名检查

This commit is contained in:
ZouJin 2024-03-13 18:57:16 +08:00
parent c6528e1676
commit ead7115ac9
2 changed files with 226 additions and 280 deletions

View File

@ -10,6 +10,7 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Security.Cryptography.X509Certificates;
using System.Windows;
using System.Windows.Media;
using System.Xml.Linq;
@ -77,8 +78,10 @@ namespace VPet_Simulator.Windows
}
public CoreMOD(DirectoryInfo directory, MainWindow mw)
{
#if !DEBUG
try
{
#endif
Path = directory;
LpsDocument modlps = new LpsDocument(File.ReadAllText(directory.FullName + @"\info.lps"));
Name = modlps.FindLine("vupmod").Info;
@ -278,14 +281,15 @@ namespace VPet_Simulator.Windows
continue;
}
#endif
try
{
var path = tmpfi.Name;
if (LoadedDLL.Contains(path))
continue;
LoadedDLL.Add(path);
Assembly dll = Assembly.LoadFrom(tmpfi.FullName);
var certificate = Win32.GetCertificateFromSignedFile(path);
X509Certificate2 certificate = new X509Certificate2(tmpfi.FullName);
if (certificate != null)
{
if (certificate.Subject == "CN=\"Shenzhen Lingban Computer Technology Co., Ltd.\", O=\"Shenzhen Lingban Computer Technology Co., Ltd.\", L=Shenzhen, S=Guangdong Province, C=CN, SERIALNUMBER=91440300MA5H8REU3K, OID.2.5.4.15=Private Organization, OID.1.3.6.1.4.1.311.60.2.1.1=Shenzhen, OID.1.3.6.1.4.1.311.60.2.1.2=Guangdong Province, OID.1.3.6.1.4.1.311.60.2.1.3=CN"
@ -315,6 +319,7 @@ namespace VPet_Simulator.Windows
continue;
}
}
Assembly dll = Assembly.LoadFrom(tmpfi.FullName);
var v = dll.GetExportedTypes();
foreach (Type exportedType in v)
{
@ -323,6 +328,7 @@ namespace VPet_Simulator.Windows
mw.Plugins.Add((MainPlugin)Activator.CreateInstance(exportedType, mw));
}
}
}
catch (Exception e)
{
@ -335,6 +341,7 @@ namespace VPet_Simulator.Windows
break;
}
}
#if !DEBUG
}
catch (Exception e)
{
@ -342,6 +349,7 @@ namespace VPet_Simulator.Windows
Tag.Add("该模组已损坏");
SuccessLoad = false;
}
#endif
}
public bool IsOnMOD(MainWindow mw) => mw.Set.IsOnMod(Name);
public bool IsPassMOD(MainWindow mw) => mw.Set.IsPassMOD(Name);

View File

@ -11,68 +11,6 @@ namespace VPet_Simulator.Windows
{
static partial class Win32
{
[DllImport("Crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CryptQueryObject(
int dwObjectType,
string pvObject,
int dwExpectedContentTypeFlags,
int dwExpectedFormatTypeFlags,
int dwFlags,
IntPtr pdwMsgAndCertEncodingType,
IntPtr pdwContentType,
IntPtr pdwFormatType,
IntPtr phStore,
IntPtr phMsg,
ref IntPtr ppvContext
);
private const int CERT_QUERY_OBJECT_FILE = 0x00000001;
private const int CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED = 0x00004000;
private const int CERT_QUERY_FORMAT_FLAG_BINARY = 0x00000002;
public static X509Certificate2? GetCertificateFromSignedFile(string filePath)
{
IntPtr certificateContext = IntPtr.Zero;
if (!CryptQueryObject(
CERT_QUERY_OBJECT_FILE,
filePath,
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
CERT_QUERY_FORMAT_FLAG_BINARY,
0,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
ref certificateContext))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
try
{
return new X509Certificate2(certificateContext);
}
catch
{
return null;
}
//finally
//{
// if (certificateContext != IntPtr.Zero)
// {
// X509Certificate2Collection certificates = new X509Certificate2Collection();
// certificates.ImportFromPemFile(filePath);
// foreach (var certificate in certificates)
// {
// Console.WriteLine($"Subject: {certificate.Subject}");
// Console.WriteLine($"Issuer: {certificate.Issuer}");
// }
// }
//}
}
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink