2009-02-04 19 views
11

को बदलने के लिए विंडोज एसडीके जहाजों को एक टूल के साथ भेजता है जिसे signtool.exe कहा जाता है जो आपको एक प्रमाणपत्र के साथ फाइल पर हस्ताक्षर करने देता है। मुझे एक ही चीज़ करने की ज़रूरत है, लेकिन पृष्ठभूमि सेवा में, इसलिए मैं एक ही काम करने के लिए लाइब्रेरी (अधिमानतः प्रबंधित कोड, लेकिन COM करूंगा) की तलाश में हूं। कोई विचार?एपीआई/लाइब्रेरी signtool.exe

उत्तर मिला। यहाँ कैसे .NET में एक फाइल पर हस्ताक्षर करने के एक X.509 प्रमाणपत्र का उपयोग करने के तरीके:

CmsSigner signer = new CmsSigner(); 
signer.Certificate = new X509Certificate2(certificate); 

SignedCms content = new SignedCms(new ContentInfo(File.ReadAllBytes(fileToSign))); 
content.ComputeSignature(signer, true); 
byte[] signedFile = content.Encode(); 

string signedFileName = fileToSign + ".signed"; 
File.WriteAllBytes(signedFileName, signedFile); 

Console.WriteLine("Signed file: " + signedFileName); 

यहाँ, प्रमाण पत्र .pfx प्रमाण पत्र और fileToSign युक्त फ़ाइल का पथ है पर हस्ताक्षर करने के फ़ाइल है।

उत्तर

3

साइनटूल कैपिकॉम का उपयोग कर रहा है जो क्रिप्टो एपीआई के लिए COM wrapper है। आप या तो एक का उपयोग कर सकते हैं। यदि आप कैपिकॉम के साथ जा रहे हैं, तो आप जानकारी here देख सकते हैं।

+0

मेरा जवाब मिला। यहां हस्ताक्षर करने के लिए CAPICOM लाइब्रेरी का उपयोग करने पर दस्तावेज़: http://msdn.microsoft.com/en-us/library/aa387760(VS.85).aspx चूंकि कैपिकॉम एपीआई को बहिष्कृत किया गया है, यह पृष्ठ दिखाता है कि यह पृष्ठ क्या दिखाता है .NET प्रतिस्थापन हैं: http://msdn.microsoft.com/en-us/library/cc778518(VS.85).aspx – Arun

0

आप बस इसके आसपास अपना रास्ता स्क्रिप्ट नहीं कर सकते? एक साधारण बैच फ़ाइल लिखें जो इसे सही तर्क और इनपुट देता है? जब हम यूनिक्स सर्वर पर इस समस्या को देखते हैं तो हम कम से कम क्या करते हैं।

+0

शायद क्योंकि वह यह नहीं मान सकता कि यह लक्ष्य कंप्यूटर पर तैनात है और लाइसेंस शायद पुनर्वितरण प्रतिबंधित करता है। –

0

मुझे सेबेस्टियन के समान समस्या मिल रही है। एपीआई में देखकर, ऐसा लगता है कि यह लिफाफे संदेशों पर हस्ताक्षर करने के लिए है। प्रमाणीकरण - कोड-हस्ताक्षर जो साइनटोल करता है - अलग है, यही कारण है कि EXE हस्ताक्षर के बाद नहीं चलती है।

मैं अभी भी विकल्पों की तलाश में हूं।

0

गूगल-यात्री यहां पहुंचने के लिए:

This MSDN forum thread कहते हैं खिड़कियों में एक CryptUIWizDigitalSign एपीआई है। यह अलेजांद्रो कैम्पोस मैगेनियो द्वारा blog article को भी इंगित करता है जो वीबी.नेट में नमूना कार्यान्वयन दिखाता है।

चूंकि एक सी # संस्करण गायब प्रतीत होता है, इसलिए मैंने अलेजांद्रो कोड को सी # में परिवर्तित कर दिया। ध्यान दें कि निम्न कोड केवल फाइलों के साथ काम करता है (अभी तक)।

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

namespace ConsoleApp1 
{ 
    /// <summary> 
    /// Provides code signing functionality via Windows COM Cryptui.dll. 
    /// </summary> 
    class Signer 
    { 

     public const Int32 CRYPTUI_WIZ_NO_UI = 1; 
     public const Int32 CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE = 1; 
     public const Int32 CRYPTUI_WIZ_DIGITAL_SIGN_CERT = 1; 

     public struct CRYPTUI_WIZ_DIGITAL_SIGN_INFO 
     { 
      public Int32 dwSize; 
      public Int32 dwSubjectChoice; 
      [MarshalAs(UnmanagedType.LPWStr)] 
      public string pwszFileName; 
      public Int32 dwSigningCertChoice; 
      public IntPtr pSigningCertContext; 
      public string pwszTimestampURL; 
      public Int32 dwAdditionalCertChoice; 
      public IntPtr pSignExtInfo; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     public struct CRYPTUI_WIZ_DIGITAL_SIGN_CONTEXT 
     { 
      public Int32 dwSize; 
      public Int32 cbBlob; 
      public IntPtr pbBlob; 
     } 

     [DllImport("Cryptui.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     public static extern bool CryptUIWizDigitalSign(Int32 dwFlags, IntPtr hwndParent, string pwszWizardTitle, ref CRYPTUI_WIZ_DIGITAL_SIGN_INFO pDigitalSignInfo, ref IntPtr ppSignContext); 

     [DllImport("Cryptui.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     public static extern bool CryptUIWizFreeDigitalSignContext(IntPtr pSignContext); 

     /// <summary> 
     /// Signs the executable at the given path with the given code signing certificate. 
     /// </summary> 
     /// <example> 
     /// string certPath = @"C:\certs\CodeSigningTestCert.pfx"; 
     /// string exePath = @"C:\temp\ConsoleApp2ToBeSigned.exe"; 
     /// string certPwd = "myGreatSecurePassword"; 
     ///  
     /// try 
     /// { 
     ///  string resultingSignature = Signer.SignExecutable(certPath, exePath, certPwd); 
     /// } 
     /// catch (Win32Exception ex) 
     /// { 
     ///  Console.WriteLine(ex.Message + ", Native error code: " + ex.NativeErrorCode.ToString()); 
     /// } 
     /// catch (Exception ex) 
     /// { 
     ///  // Any unexpected errors? 
     ///  Console.WriteLine(ex.Message); 
     /// } 
     /// 
     /// </example> 
     /// <param name="certPath">The absolute path to the PFX file to be used for signing the exe file.</param> 
     /// <param name="exePath">The absolute path to the executable to be signed.</param> 
     /// <param name="certPwd">The password for the PFX file.</param> 
     public static string SignExecutable(string certPath, string exePath, string certPwd) 
     { 
      X509Certificate2 cert = default(X509Certificate2); 

      CRYPTUI_WIZ_DIGITAL_SIGN_INFO digitalSignInfo = default(CRYPTUI_WIZ_DIGITAL_SIGN_INFO); 
      CRYPTUI_WIZ_DIGITAL_SIGN_CONTEXT signContext = default(CRYPTUI_WIZ_DIGITAL_SIGN_CONTEXT); 

      IntPtr pSignContext = default(IntPtr); 
      IntPtr pSigningCertContext = default(IntPtr); 

      // Get certificate context 
      cert = new X509Certificate2(certPath, certPwd); 
      pSigningCertContext = cert.Handle; 

      // Prepare signing info: exe and cert 
      digitalSignInfo = new CRYPTUI_WIZ_DIGITAL_SIGN_INFO(); 
      digitalSignInfo.dwSize = Marshal.SizeOf(digitalSignInfo); 
      digitalSignInfo.dwSubjectChoice = CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE; 
      digitalSignInfo.pwszFileName = exePath; 
      digitalSignInfo.dwSigningCertChoice = CRYPTUI_WIZ_DIGITAL_SIGN_CERT; 
      digitalSignInfo.pSigningCertContext = pSigningCertContext; 
      digitalSignInfo.pwszTimestampURL = null; 
      digitalSignInfo.dwAdditionalCertChoice = 0; 
      digitalSignInfo.pSignExtInfo = IntPtr.Zero; 

      // Sign exe 
      if ((!CryptUIWizDigitalSign(CRYPTUI_WIZ_NO_UI, IntPtr.Zero, null, ref digitalSignInfo, ref pSignContext))) 
       throw new Win32Exception(Marshal.GetLastWin32Error(), "CryptUIWizDigitalSign"); 

      // Get the blob with the signature 
      signContext = (CRYPTUI_WIZ_DIGITAL_SIGN_CONTEXT)Marshal.PtrToStructure(pSignContext, typeof(CRYPTUI_WIZ_DIGITAL_SIGN_CONTEXT)); 
      byte[] blob = new byte[signContext.cbBlob + 1]; 
      Marshal.Copy(signContext.pbBlob, blob, 0, signContext.cbBlob); 

      // Free blob memory 
      if ((!CryptUIWizFreeDigitalSignContext(pSignContext))) 
       throw new Win32Exception(Marshal.GetLastWin32Error(), "CryptUIWizFreeDigitalSignContext"); 

      return System.Text.Encoding.Default.GetString(blob); 
     } 
    } 
} 

उम्मीद है कि यह मदद करता है!

संबंधित मुद्दे