繁体   English   中英

C#从以字节数组存储的exe文件中获取X509证书

[英]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 \\目录。

我优化了该项目,将program.exe下载到byte []中(以避免在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/zh-CN/library/ms148413(v=vs.110).aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM