C#从存储为字节数组的exe文件中获取X509Certificate
C# Getting a X509Certificate from exe file stored as byte array
我目前正在我的自动更新程序项目中进行一些代码优化(并试图阻止安全软件阻止生成的 tempFile.exe)。基本上 auto-updater.exe 正在下载最新版本的 program.exe 并将其保存到 C:\Temp\,检查 md5 和 exe 签名以进行安全验证,然后如果全部通过我们覆盖 program.exe 位于 C:\Program File\directory.
我优化了项目以将 program.exe 下载到字节 [] 中(以避免在 C:\temp 中创建临时文件)。我也可以检查 byte[] 的 md5,但是尝试检查 exe 的签名是我遇到困难的地方。
当我在 C:\temp 中创建临时文件时,我能够使用 X509Certificate.CreateFromSignedFile("C:\temp\program.exe") 方法创建 X509Certificate,但是没有方法接受 byte[] 而不是文件路径。
X509Certificate2 theCertificate;
try
{
X509Certificate theSigner = X509Certificate.CreateFromSignedFile(pathToFile);
theCertificate = new X509Certificate2(theSigner);
}
catch (Exception ex)
{
Console.WriteLine("No digital signature found in: " + pathToFile);
Console.WriteLine("Signature check ex: " + ex);
return false;
}
// This section will check that the certificate is from a trusted authority IE not self-signed
var theCertificateChain = new X509Chain();
theCertificateChain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
theCertificateChain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
theCertificateChain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
theCertificateChain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
bool chainIsValid = theCertificateChain.Build(theCertificate);
if (chainIsValid)
{
// Valid signature...
}
您有什么建议可以使用 byte[] 来验证下载的 program.exe 的签名吗?
我不确定这是不是你关心的问题,但你当然可以使用构造函数从字节数组创建证书实例
new X509Certificate2(byte[])
https://msdn.microsoft.com/en-us/library/ms148413(v=vs.110).aspx
我目前正在我的自动更新程序项目中进行一些代码优化(并试图阻止安全软件阻止生成的 tempFile.exe)。基本上 auto-updater.exe 正在下载最新版本的 program.exe 并将其保存到 C:\Temp\,检查 md5 和 exe 签名以进行安全验证,然后如果全部通过我们覆盖 program.exe 位于 C:\Program File\directory.
我优化了项目以将 program.exe 下载到字节 [] 中(以避免在 C:\temp 中创建临时文件)。我也可以检查 byte[] 的 md5,但是尝试检查 exe 的签名是我遇到困难的地方。
当我在 C:\temp 中创建临时文件时,我能够使用 X509Certificate.CreateFromSignedFile("C:\temp\program.exe") 方法创建 X509Certificate,但是没有方法接受 byte[] 而不是文件路径。
X509Certificate2 theCertificate;
try
{
X509Certificate theSigner = X509Certificate.CreateFromSignedFile(pathToFile);
theCertificate = new X509Certificate2(theSigner);
}
catch (Exception ex)
{
Console.WriteLine("No digital signature found in: " + pathToFile);
Console.WriteLine("Signature check ex: " + ex);
return false;
}
// This section will check that the certificate is from a trusted authority IE not self-signed
var theCertificateChain = new X509Chain();
theCertificateChain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
theCertificateChain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
theCertificateChain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
theCertificateChain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
bool chainIsValid = theCertificateChain.Build(theCertificate);
if (chainIsValid)
{
// Valid signature...
}
您有什么建议可以使用 byte[] 来验证下载的 program.exe 的签名吗?
我不确定这是不是你关心的问题,但你当然可以使用构造函数从字节数组创建证书实例
new X509Certificate2(byte[])
https://msdn.microsoft.com/en-us/library/ms148413(v=vs.110).aspx