2014-10-13 4 views
10

Ich versuche, die beste Methode für die Code-Signierung einer ausführbaren Datei mit Bouncy Castle, verwalteten Code oder nicht verwalteten Code aus C# zu ermitteln. Da CAPICOM nun veraltet ist, stelle ich mir vor, dass eine der SignerSign-Methoden aus msign32.dll der beste Weg ist, wenn sie unmanaged ausgeführt werden muss.Programmatisch Code-Signieren einer ausführbaren Datei mit einem PFX (Hüpfburg oder sonst)

Diese Antwort (https://stackoverflow.com/a/3952235/722078) scheint nahe, aber es erzeugt eine .p7m Datei, die, während sie erscheint, die richtige Größe zu sein, nicht richtig ausgeführt wird (natürlich vor am Laufen .exe umbenannt).

Die Lösung der Frage-Fragesteller, die hier gegeben (API/Library to replace signtool.exe) schien vielversprechend und verwaltet werden, aber wie Tom Canham unten in den Kommentaren erwähnt, „es scheint, dass dies für die Unterzeichnung Nachrichten hüllt ist Authenticode -. Die Code-Signierung, dass signtool ist - ist anders, weshalb die EXE nach dem Signieren nicht läuft. " Ich erhalte den gleichen Fehler wie Tom, wenn ich unter Verwendung der Frage-Fragesteller-Lösung oder der zuvor erwähnten Bouncy-Castle-Lösung signiere.

Die einzige Option, die ich noch nicht versucht habe, ist hier (https://stackoverflow.com/a/6429860/722078), und obwohl es vielversprechend aussieht, bin ich nicht sicher, dass es "authenticode" Code-Signierung als Gegensatz "Code-Signierung" umhüllte Nachricht verwendet. Diese Antwort hat auch den Vorteil, dass die CAPICOM-Interop-Methoden, die jetzt veraltet sind, nicht mehr verwendet werden. Daher stelle ich mir vor, dass ich heute mit dieser Methode über meine Ergebnisse berichten werde. Wenn dies die beste Option ist, könnte jemand da draußen die Unterschiede zwischen SignerSign, SignerSignEx und SignerSignEx2 Funktionen sprechen, die aus mssign32.dll exportiert werden? Ich habe gelesen, dass SignerSignEx2 sollte mit Windows 8 und höher ...

Lange Rede kurzer Sinn verwendet werden, ich möchte die Fähigkeit von signtool.exe replizieren eine ausführbare Datei zu unterzeichnen eine EXE-Datei, PFX-Datei angegeben, und Passwort wie folgt:

signtool sign /f cert.pfx /p password application.exe 

ich für die beste Option Suche programmatisch Code signieren eine ausführbare Datei (PE, wenn es darauf ankommt) authenticode Unterzeichnung mit, und ich würde es vorziehen, Hüpfburg oder verwaltetem Code, wenn möglich zu verwenden, obwohl Ich werde etwas nicht verwaltet verwenden, wenn es funktioniert und derzeit nicht veraltet ist.

Danke!

+0

Nur ein Lurker aber es sieht aus wie http://stackoverflow.com/a/6429860/722078 das anwendbare Beispiel. FYI, in diesen Funktionen bedeutet das Wort "SIP" [Subject Interface Package] (http://msdn.microsoft.com/en-us/library/windows/desktop/ms721625 (v = vs.85) .aspx # _security_subject_interface_package_gly) –

+0

Da lehne ich mich auch, obwohl ich hoffe, dass jemand die Unterschiede zwischen der historischen Verfügbarkeit der SignerSign-Methoden klären kann. Ich werde sehen, ob ich die Referenz ausgraben kann, die ausdrücklich SignerSignEx2 mit Windows 8 verwendet werden soll, obwohl es keine zusätzlichen Details zur Verfügung stellt. –

+0

Dieser Windows 8-Kommentar ist ein Kommentar zu dieser Antwort: (http://Stackoverflow.com/a/9925866/722078). SignerSignEx2 wurde anscheinend zum Signieren von Apps im Windows App Store eingeführt, was für mich nicht relevant ist. –

Antwort

11

Soweit ich das beurteilen kann, sind SignSigner und SignSignerEx ab Windows XP verfügbar, welches das älteste Betriebssystem ist, das ich unterstützen möchte. Da ich mich nicht um die Veröffentlichung im Windows App Store kümmern muss, ist diese Antwort auf SignSigner und SignSignerEx beschränkt, obwohl der Import für SignSignerEx2 SignSignerEx sehr ähnlich ist, und ich würde nicht erwarten, dass dies zu Problemen führen wird.

Die folgende Klasse ermöglicht es Ihnen, eine ausführbare Datei mit einem PFX zu unterzeichnen durch den Aufruf:

SignWithCert(string appPath, string certPath, string certPassword, string timestampUrl); 

Es erlaubt Ihnen auch eine ausführbare Datei mit einem Zertifikat von einem Schlüsselspeicher zu unterzeichnen durch den Aufruf:

SignWithThumbPrint(string appPath, string thumbprint, string timestampUrl); 

Wenn Sie mit einem in einem Keystore installierten Cert signieren möchten, müssen Sie möglicherweise FindCertByThumbPrint (string thumbPrint) aktualisieren, um weitere Schlüsselspeicher zu überprüfen, die ich überprüfen möchte. 99,5% der Zeit signieren unsere Kunden mit einer .pfx und nicht mit einem Daumenabdruck.

Aus Gründen der Veranschaulichung verwendet SignWithCert() SignerSignEx und SignerTimeStampEx, während SignWithThumbPrint() SignerSign und SignerTimeStamp verwendet.

Sie sind leicht austauschbar.SignerSignEx und SignerTimeStampEx geben Ihnen einen Zeiger SIGNER_CONTEXT zurück und ermöglichen es Ihnen, das Verhalten der Funktionen mit dem Argument DwFlags zu ändern (wenn Sie eine portable ausführbare Datei signieren). Gültige Flag-Optionen sind unter here aufgelistet. Wenn Sie 0x0 als dwFlags an SignerSignEx übergeben, ist die Ausgabe identisch mit der Verwendung von SignerSign. In meinem Fall stelle ich mir vor, dass ich SignerSign verwenden werde, weil ich glaube, dass ich aus irgendeinem erdenklichen Grund keinen Hinweis auf den Signer-Kontext brauche.

Wie auch immer, hier ist die Klasse. Ich schreibe hier zum ersten Mal Code, also hoffe ich, dass ich ihn nicht in der Formatierung gebrochen habe.

Der Code funktioniert wie erwartet, und die ausführbare Datei läuft gut und signiert, ABER die binäre Ausgabe des Signaturblocks unterscheidet sich geringfügig von der binären Ausgabe von signtool.exe (in diesem Test wurde ein Zeitstempel von keinem der beiden Tools verwendet). Ich schreibe dies der Tatsache zu, dass signtool.exe scheint CAPICOM zum Signieren zu verwenden, und dies verwendet Mssign32.dll, aber alles in allem bin ich ziemlich zufrieden damit in der ersten Reihe von Tests.

Die Fehlerbehandlung muss natürlich Verbesserung.

Dank GregS und jeder gibt, die vorher Codebeispiele geschrieben wird.

Hier ist die relevanten Sachen. Ich werde diesen Block mit Kommentaren und Verbesserungen aktualisieren, wenn ich die Möglichkeit dazu habe.

Update 1: Etwas bessere Fehlerbehandlung und Kommentare hinzugefügt, zusammen mit einigen Neuformatierung des Fingerabdrucks in FindCertByThumbprint (String thumbprint), um das Cert unter Windows 8 und Windows 10 (öffentliche Vorschau) zu finden. Diese Betriebssysteme geben keine Übereinstimmung zurück, wenn Leerzeichen im Fingerabdruck verblieben sind. Daher behebe ich sie jetzt vor der Suche.

using System; 
using System.Runtime.InteropServices; 
using System.Security.Cryptography; 
using System.Security.Cryptography.X509Certificates; 

namespace Utilities 
{ 
    internal static class SignTool 
    { 
     #region Structures 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_SUBJECT_INFO 
     { 
      public uint cbSize; 
      public IntPtr pdwIndex; 
      public uint dwSubjectChoice; 
      public SubjectChoiceUnion Union1; 
      [StructLayoutAttribute(LayoutKind.Explicit)] 
      internal struct SubjectChoiceUnion 
      { 
       [FieldOffsetAttribute(0)] 
       public System.IntPtr pSignerFileInfo; 
       [FieldOffsetAttribute(0)] 
       public System.IntPtr pSignerBlobInfo; 
      }; 
     } 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_CERT 
     { 
      public uint cbSize; 
      public uint dwCertChoice; 
      public SignerCertUnion Union1; 
      [StructLayoutAttribute(LayoutKind.Explicit)] 
      internal struct SignerCertUnion 
      { 
       [FieldOffsetAttribute(0)] 
       public IntPtr pwszSpcFile; 
       [FieldOffsetAttribute(0)] 
       public IntPtr pCertStoreInfo; 
       [FieldOffsetAttribute(0)] 
       public IntPtr pSpcChainInfo; 
      }; 
      public IntPtr hwnd; 
     } 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_SIGNATURE_INFO 
     { 
      public uint cbSize; 
      public uint algidHash; // ALG_ID 
      public uint dwAttrChoice; 
      public IntPtr pAttrAuthCode; 
      public IntPtr psAuthenticated; // PCRYPT_ATTRIBUTES 
      public IntPtr psUnauthenticated; // PCRYPT_ATTRIBUTES 
     } 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_FILE_INFO 
     { 
      public uint cbSize; 
      public IntPtr pwszFileName; 
      public IntPtr hFile; 
     } 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_CERT_STORE_INFO 
     { 
      public uint cbSize; 
      public IntPtr pSigningCert; // CERT_CONTEXT 
      public uint dwCertPolicy; 
      public IntPtr hCertStore; 
     } 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_CONTEXT 
     { 
      public uint cbSize; 
      public uint cbBlob; 
      public IntPtr pbBlob; 
     } 

     [StructLayoutAttribute(LayoutKind.Sequential)] 
     struct SIGNER_PROVIDER_INFO 
     { 
      public uint cbSize; 
      public IntPtr pwszProviderName; 
      public uint dwProviderType; 
      public uint dwKeySpec; 
      public uint dwPvkChoice; 
      public SignerProviderUnion Union1; 
      [StructLayoutAttribute(LayoutKind.Explicit)] 
      internal struct SignerProviderUnion 
      { 
       [FieldOffsetAttribute(0)] 
       public IntPtr pwszPvkFileName; 
       [FieldOffsetAttribute(0)] 
       public IntPtr pwszKeyContainer; 
      }; 
     } 

     #endregion 

     #region Imports 

     [DllImport("Mssign32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     private static extern int SignerSign(
      IntPtr pSubjectInfo,  // SIGNER_SUBJECT_INFO 
      IntPtr pSignerCert,   // SIGNER_CERT 
      IntPtr pSignatureInfo,  // SIGNER_SIGNATURE_INFO 
      IntPtr pProviderInfo,  // SIGNER_PROVIDER_INFO 
      string pwszHttpTimeStamp, // LPCWSTR 
      IntPtr psRequest,   // PCRYPT_ATTRIBUTES 
      IntPtr pSipData    // LPVOID 
      ); 

     [DllImport("Mssign32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     private static extern int SignerSignEx(
      uint dwFlags,    // DWORD 
      IntPtr pSubjectInfo,  // SIGNER_SUBJECT_INFO 
      IntPtr pSignerCert,   // SIGNER_CERT 
      IntPtr pSignatureInfo,  // SIGNER_SIGNATURE_INFO 
      IntPtr pProviderInfo,  // SIGNER_PROVIDER_INFO 
      string pwszHttpTimeStamp, // LPCWSTR 
      IntPtr psRequest,   // PCRYPT_ATTRIBUTES 
      IntPtr pSipData,   // LPVOID 
      out SIGNER_CONTEXT ppSignerContext // SIGNER_CONTEXT 
      ); 

     [DllImport("Mssign32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     private static extern int SignerTimeStamp(
      IntPtr pSubjectInfo,  // SIGNER_SUBJECT_INFO 
      string pwszHttpTimeStamp, // LPCWSTR 
      IntPtr psRequest,   // PCRYPT_ATTRIBUTES 
      IntPtr pSipData    // LPVOID 
      ); 

     [DllImport("Mssign32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     private static extern int SignerTimeStampEx(
      uint dwFlags,    // DWORD 
      IntPtr pSubjectInfo,  // SIGNER_SUBJECT_INFO 
      string pwszHttpTimeStamp, // LPCWSTR 
      IntPtr psRequest,   // PCRYPT_ATTRIBUTES 
      IntPtr pSipData,   // LPVOID 
      out SIGNER_CONTEXT ppSignerContext // SIGNER_CONTEXT 
      ); 

     [DllImport("Crypt32.dll", EntryPoint = "CertCreateCertificateContext", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] 
     private static extern IntPtr CertCreateCertificateContext(
      int dwCertEncodingType, 
      byte[] pbCertEncoded, 
      int cbCertEncoded); 

     #endregion 

     #region public methods 

     // Call SignerSignEx and SignerTimeStampEx for a given .pfx 
     public static void SignWithCert(string appPath, string certPath, string certPassword, string timestampUrl) 
     { 
      IntPtr pSignerCert = IntPtr.Zero; 
      IntPtr pSubjectInfo = IntPtr.Zero; 
      IntPtr pSignatureInfo = IntPtr.Zero; 
      IntPtr pProviderInfo = IntPtr.Zero; 

      try 
      { 
       // Grab the X509Certificate from the .pfx file. 
       X509Certificate2 cert = new X509Certificate2(certPath, certPassword); 

       pSignerCert = CreateSignerCert(cert); 
       pSubjectInfo = CreateSignerSubjectInfo(appPath); 
       pSignatureInfo = CreateSignerSignatureInfo(); 
       pProviderInfo = GetProviderInfo(cert); 

       SIGNER_CONTEXT signerContext; 

       SignCode(0x0, pSubjectInfo, pSignerCert, pSignatureInfo, pProviderInfo, out signerContext); 

       // Only attempt to timestamp if we've got a timestampUrl. 
       if (!string.IsNullOrEmpty(timestampUrl)) 
       { 
        TimeStampSignedCode(0x0, pSubjectInfo, timestampUrl, out signerContext); 
       } 
      } 
      catch (CryptographicException ce) 
      { 
       string exception; 

       // do anything with this useful information? 
       switch (Marshal.GetHRForException(ce)) 
       { 
        case -2146885623: 
         exception = string.Format(@"An error occurred while attempting to load the signing certificate. ""{0}"" does not appear to contain a valid certificate.", certPath); 
         break; 
        case -2147024810: 
         exception = string.Format(@"An error occurred while attempting to load the signing certificate. The specified password was incorrect."); 
         break; 
        default: 
         exception = string.Format(@"An error occurred while attempting to load the signing certificate. {0}", ce.Message); 
         break; 
       } 
      } 
      catch (Exception e) 
      { 
       // do anything with this useful information? 
       string exception = e.Message; 
      } 
      finally 
      { 
       if (pSignerCert != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSignerCert, typeof(SIGNER_CERT)); 
       } 
       if (pSubjectInfo != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSubjectInfo, typeof(SIGNER_SUBJECT_INFO)); 
       } 
       if (pSignatureInfo != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSignatureInfo, typeof(SIGNER_SIGNATURE_INFO)); 
       } 
       if (pProviderInfo != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSignatureInfo, typeof(SIGNER_PROVIDER_INFO)); 
       } 
      } 
     } 

     // Call SignerSign and SignerTimeStamp for a given thumbprint. 
     public static void SignWithThumbprint(string appPath, string thumbprint, string timestampUrl) 
     { 
      IntPtr pSignerCert = IntPtr.Zero; 
      IntPtr pSubjectInfo = IntPtr.Zero; 
      IntPtr pSignatureInfo = IntPtr.Zero; 
      IntPtr pProviderInfo = IntPtr.Zero; 

      try 
      { 
       pSignerCert = CreateSignerCert(thumbprint); 
       pSubjectInfo = CreateSignerSubjectInfo(appPath); 
       pSignatureInfo = CreateSignerSignatureInfo(); 

       SignCode(pSubjectInfo, pSignerCert, pSignatureInfo, pProviderInfo); 

       // Only attempt to timestamp if we've got a timestampUrl. 
       if (!string.IsNullOrEmpty(timestampUrl)) 
       { 
        TimeStampSignedCode(pSubjectInfo, timestampUrl); 
       } 
      } 
      catch (CryptographicException ce) 
      { 
       // do anything with this useful information? 
       string exception = string.Format(@"An error occurred while attempting to load the signing certificate. {0}", ce.Message); 
      } 
      catch (Exception e) 
      { 
       // do anything with this useful information? 
       string exception = e.Message; 
      } 
      finally 
      { 
       if (pSignerCert != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSignerCert, typeof(SIGNER_CERT)); 
       } 
       if (pSubjectInfo != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSubjectInfo, typeof(SIGNER_SUBJECT_INFO)); 
       } 
       if (pSignatureInfo != IntPtr.Zero) 
       { 
        Marshal.DestroyStructure(pSignatureInfo, typeof(SIGNER_SIGNATURE_INFO)); 
       } 
      } 
     } 

     #endregion 

     #region private methods 

     private static IntPtr CreateSignerSubjectInfo(string pathToAssembly) 
     { 
      SIGNER_SUBJECT_INFO info = new SIGNER_SUBJECT_INFO 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_SUBJECT_INFO)), 
       pdwIndex = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint))) 
      }; 
      var index = 0; 
      Marshal.StructureToPtr(index, info.pdwIndex, false); 

      info.dwSubjectChoice = 0x1; //SIGNER_SUBJECT_FILE 
      IntPtr assemblyFilePtr = Marshal.StringToHGlobalUni(pathToAssembly); 

      SIGNER_FILE_INFO fileInfo = new SIGNER_FILE_INFO 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_FILE_INFO)), 
       pwszFileName = assemblyFilePtr, 
       hFile = IntPtr.Zero 
      }; 

      info.Union1 = new SIGNER_SUBJECT_INFO.SubjectChoiceUnion 
      { 
       pSignerFileInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SIGNER_FILE_INFO))) 
      }; 

      Marshal.StructureToPtr(fileInfo, info.Union1.pSignerFileInfo, false); 

      IntPtr pSubjectInfo = Marshal.AllocHGlobal(Marshal.SizeOf(info)); 
      Marshal.StructureToPtr(info, pSubjectInfo, false); 

      return pSubjectInfo; 
     } 

     private static X509Certificate2 FindCertByThumbprint(string thumbprint) 
     { 
      try 
      { 
       // Remove spaces convert to upper. Windows 10 (preview) and Windows 8 will not return a cert 
       // unless it is a perfect match with no spaces and all uppercase characters. 
       string thumbprintFixed = thumbprint.Replace(" ", string.Empty).ToUpperInvariant(); 

       // Check common store locations for the corresponding code-signing cert. 
       X509Store[] stores = new X509Store[4] { new X509Store(StoreName.My, StoreLocation.CurrentUser), 
                 new X509Store(StoreName.My, StoreLocation.LocalMachine), 
                 new X509Store(StoreName.TrustedPublisher, StoreLocation.CurrentUser), 
                 new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine) }; 

       foreach (X509Store store in stores) 
       { 
        store.Open(OpenFlags.ReadOnly); 

        // Find the cert! 
        X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprintFixed, false); 

        store.Close(); 

        // If we didn't find the cert, try the next store. 
        if (certs.Count < 1) 
        { 
         continue; 
        } 

        // Return the cert (first one if there is more than one identical cert in the collection). 
        return certs[0]; 
       } 

       // No cert was found. Return null. 
       throw new Exception(string.Format(@"A certificate matching the thumbprint: ""{0}"" could not be found. Make sure that a valid certificate matching the provided thumbprint is installed.", thumbprint)); 
      } 
      catch (Exception e) 
      { 
       throw new Exception(string.Format("{0}", e.Message)); 
      } 
     } 

     private static IntPtr CreateSignerCert(X509Certificate2 cert) 
     { 
      SIGNER_CERT signerCert = new SIGNER_CERT 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_CERT)), 
       dwCertChoice = 0x2, 
       Union1 = new SIGNER_CERT.SignerCertUnion 
       { 
        pCertStoreInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SIGNER_CERT_STORE_INFO))) 
       }, 
       hwnd = IntPtr.Zero 
      }; 

      const int X509_ASN_ENCODING = 0x00000001; 
      const int PKCS_7_ASN_ENCODING = 0x00010000; 

      IntPtr pCertContext = CertCreateCertificateContext(
       X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 
       cert.GetRawCertData(), 
       cert.GetRawCertData().Length); 

      SIGNER_CERT_STORE_INFO certStoreInfo = new SIGNER_CERT_STORE_INFO 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_CERT_STORE_INFO)), 
       pSigningCert = pCertContext, 
       dwCertPolicy = 0x2, // SIGNER_CERT_POLICY_CHAIN 
       hCertStore = IntPtr.Zero 
      }; 

      Marshal.StructureToPtr(certStoreInfo, signerCert.Union1.pCertStoreInfo, false); 

      IntPtr pSignerCert = Marshal.AllocHGlobal(Marshal.SizeOf(signerCert)); 
      Marshal.StructureToPtr(signerCert, pSignerCert, false); 

      return pSignerCert; 
     } 

     private static IntPtr CreateSignerCert(string thumbprint) 
     { 
      SIGNER_CERT signerCert = new SIGNER_CERT 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_CERT)), 
       dwCertChoice = 0x2, 
       Union1 = new SIGNER_CERT.SignerCertUnion 
       { 
        pCertStoreInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SIGNER_CERT_STORE_INFO))) 
       }, 
       hwnd = IntPtr.Zero 
      }; 

      const int X509_ASN_ENCODING = 0x00000001; 
      const int PKCS_7_ASN_ENCODING = 0x00010000; 

      X509Certificate2 cert = FindCertByThumbprint(thumbprint); 

      IntPtr pCertContext = CertCreateCertificateContext(
       X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 
       cert.GetRawCertData(), 
       cert.GetRawCertData().Length); 

      SIGNER_CERT_STORE_INFO certStoreInfo = new SIGNER_CERT_STORE_INFO 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_CERT_STORE_INFO)), 
       pSigningCert = pCertContext, 
       dwCertPolicy = 0x2, // SIGNER_CERT_POLICY_CHAIN 
       hCertStore = IntPtr.Zero 
      }; 

      Marshal.StructureToPtr(certStoreInfo, signerCert.Union1.pCertStoreInfo, false); 

      IntPtr pSignerCert = Marshal.AllocHGlobal(Marshal.SizeOf(signerCert)); 
      Marshal.StructureToPtr(signerCert, pSignerCert, false); 

      return pSignerCert; 
     } 

     private static IntPtr CreateSignerSignatureInfo() 
     { 
      SIGNER_SIGNATURE_INFO signatureInfo = new SIGNER_SIGNATURE_INFO 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_SIGNATURE_INFO)), 
       algidHash = 0x00008004, // CALG_SHA1 
       dwAttrChoice = 0x0, // SIGNER_NO_ATTR 
       pAttrAuthCode = IntPtr.Zero, 
       psAuthenticated = IntPtr.Zero, 
       psUnauthenticated = IntPtr.Zero 
      }; 

      IntPtr pSignatureInfo = Marshal.AllocHGlobal(Marshal.SizeOf(signatureInfo)); 
      Marshal.StructureToPtr(signatureInfo, pSignatureInfo, false); 

      return pSignatureInfo; 
     } 

     private static IntPtr GetProviderInfo(X509Certificate2 cert) 
     { 
      if (cert == null || !cert.HasPrivateKey) 
      { 
       return IntPtr.Zero; 
      } 

      ICspAsymmetricAlgorithm key = (ICspAsymmetricAlgorithm)cert.PrivateKey; 
      const int PVK_TYPE_KEYCONTAINER = 2; 

      if (key == null) 
      { 
       return IntPtr.Zero; 
      } 

      SIGNER_PROVIDER_INFO providerInfo = new SIGNER_PROVIDER_INFO 
      { 
       cbSize = (uint)Marshal.SizeOf(typeof(SIGNER_PROVIDER_INFO)), 
       pwszProviderName = Marshal.StringToHGlobalUni(key.CspKeyContainerInfo.ProviderName), 
       dwProviderType = (uint)key.CspKeyContainerInfo.ProviderType, 
       dwPvkChoice = PVK_TYPE_KEYCONTAINER, 
       Union1 = new SIGNER_PROVIDER_INFO.SignerProviderUnion 
       { 
        pwszKeyContainer = Marshal.StringToHGlobalUni(key.CspKeyContainerInfo.KeyContainerName) 
       }, 
      }; 

      IntPtr pProviderInfo = Marshal.AllocHGlobal(Marshal.SizeOf(providerInfo)); 
      Marshal.StructureToPtr(providerInfo, pProviderInfo, false); 

      return pProviderInfo; 
     } 

     // Use SignerSign 
     private static void SignCode(IntPtr pSubjectInfo, IntPtr pSignerCert, IntPtr pSignatureInfo, IntPtr pProviderInfo) 
     { 
      int hResult = SignerSign(
       pSubjectInfo, 
       pSignerCert, 
       pSignatureInfo, 
       pProviderInfo, 
       null, 
       IntPtr.Zero, 
       IntPtr.Zero 
       ); 

      if (hResult != 0) 
      { 
       // See if we can get anything useful. Jury's still out on this one. 
       Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); 
      } 
     } 

     // Use SignerSignEx 
     private static void SignCode(uint dwFlags, IntPtr pSubjectInfo, IntPtr pSignerCert, IntPtr pSignatureInfo, IntPtr pProviderInfo, out SIGNER_CONTEXT signerContext) 
     { 

      int hResult = SignerSignEx(
       dwFlags, 
       pSubjectInfo, 
       pSignerCert, 
       pSignatureInfo, 
       pProviderInfo, 
       null, 
       IntPtr.Zero, 
       IntPtr.Zero, 
       out signerContext 
       ); 

      if (hResult != 0) 
      { 
       // See if we can get anything useful. Jury's still out on this one. 
       Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); 
      } 
     } 

     // Use SignerTimeStamp 
     private static void TimeStampSignedCode(IntPtr pSubjectInfo, string timestampUrl) 
     { 
      int hResult = SignerTimeStamp(
       pSubjectInfo, 
       timestampUrl, 
       IntPtr.Zero, 
       IntPtr.Zero 
       ); 

      if (hResult != 0) 
      { 
       // We can't get anything useful from GetHRForLastWin32Error, so let's throw our own. 
       //Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); 
       throw new Exception(string.Format(@"""{0}"" could not be used at this time. If necessary, check the timestampUrl, internet connection, and try again.", timestampUrl)); 
      } 
     } 

     // Use SignerTimeStampEx 
     private static void TimeStampSignedCode(uint dwFlags, IntPtr pSubjectInfo, string timestampUrl, out SIGNER_CONTEXT signerContext) 
     { 
      int hResult = SignerTimeStampEx(
       dwFlags, 
       pSubjectInfo, 
       timestampUrl, 
       IntPtr.Zero, 
       IntPtr.Zero, 
       out signerContext 
       ); 

      if (hResult != 0) 
      { 
       // We can't get anything useful from GetHRForLastWin32Error, so let's throw our own. 
       //Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); 
       throw new Exception(string.Format(@"""{0}"" could not be used at this time. If necessary, check the timestampUrl, internet connection, and try again.", timestampUrl)); 
      } 
     } 

     #endregion 

    } 
} 
+0

Beeindruckend. Schön gemacht! –

+0

Was passiert, wenn ich eine ausführbare Datei signieren muss, die sich bereits im Speicher befindet? Angenommen, ich habe es mit 'byte [] executable = File.ReadAllBytes (filename)' geladen. Ich sollte dann SIGNER_BLOB_INFO Struktur verwenden, oder? Wenn ja, brauche ich GUID für SIP, die ich nicht kenne? Bedeutet das, dass ich eine GUID brauche, die sagt "dieser Blob, den ich hier habe, ist ausführbar"? – clzola

+0

Ich kann mit diesem Code keine andere Datei als eine ausführbare Datei signieren. Können Sie mir bitte mitteilen, ob es andere Möglichkeiten gibt, andere Binärdateien als ausführbare Dateien zu signieren? –

Verwandte Themen