http://blogs.msdn.com/b/alejacma/archive/2008/12/11/how-to-sign-exe-files-with-an-authenticode-certificate-part-2.aspx?CommentPosted=true
谢谢!!!!!!!
解决方法 我得到它的工作如果有人有兴趣,这里是代码 – 它可能需要一些更多的工作,使其生产准备,但它适用于我:)using System;using System.Runtime.InteropServices;using System.Security.Cryptography.X509Certificates;namespace FingerPrinting.PatchUploader{ internal static class Signer { #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; } #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",SetLastError = true)] private static extern int SignerTimeStamp( IntPtr pSubjectInfo,// SIGNER_SUBJECT_INFO string pwszhttpTimeStamp,// PCRYPT_ATTRIBUTES IntPtr pSipData // LPVOID ); [Dllimport("Crypt32.DLL",EntryPoint = "CertCreateCertificateContext",SetLastError = true,ExactSpelling = false,CallingConvention = CallingConvention.StdCall)] private static extern IntPtr CertCreateCertificateContext( int DWCertEnCodingType,byte[] pbCertEncoded,int cbCertEncoded); #endregion public static voID Sign(string appPath,string thumbnail,string tsaServer) { var pSignerCert = IntPtr.Zero; var pSubjectInfo = IntPtr.Zero; var pSignatureInfo = IntPtr.Zero; try { pSignerCert = CreateSignerCert(thumbnail); pSubjectInfo = CreateSignerSubjectInfo(appPath); pSignatureInfo = CreateSignerSignatureInfo(); SignCode(pSubjectInfo,pSignerCert,pSignatureInfo); if (tsaServer != null) { TimeStampSignedCode(pSubjectInfo,tsaServer); } } 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)); } } } private static IntPtr CreateSignerSubjectInfo(string pathToAssembly) { var 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 var assemblyfilePtr = Marshal.StringToHGlobalUni(pathToAssembly); var 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 X509Certificate FindCertBythumbnail(string thumbnail) { try { var store = new X509Store(Storename.TrustedPublisher,StoreLocation.LocalMachine); store.Open(OpenFlags.Readonly); var certs = store.Certificates.Find(X509FindType.FindByThumbprint,thumbnail,false); if (certs.Count == 0) { throw new Exception(string.Format("Unable to find certificate with thumbnail '{0}'",thumbnail)); } if (certs.Count > 1) // Can this happen? { throw new Exception(string.Format("More than one certificate with thumbnail '{0}'",thumbnail)); } store.Close(); return certs[0]; } catch (Exception e) { throw new Exception(string.Format("Error locating certificate",e)); } } private static IntPtr CreateSignerCert(string thumbnail) { var 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; var cert = FindCertBythumbnail(thumbnail); var pCertContext = CertCreateCertificateContext( X509_ASN_ENCoding | PKCS_7_ASN_ENCoding,cert.GetRawCertData(),cert.GetRawCertData().Length); var 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,false); return pSignerCert; } private static IntPtr CreateSignerSignatureInfo() { var 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 voID TimeStampSignedCode(IntPtr pSubjectInfo,string tsaServer) { var hResult = SignerTimeStamp( pSubjectInfo,tsaServer,IntPtr.Zero,IntPtr.Zero ); if (hResult != 0) { throw new Exception(string.Format("Error timestamPing signed installer - Error code 0x{0:X}",hResult)); } } private static voID SignCode(IntPtr pSubjectInfo,IntPtr pSignerCert,IntPtr pSignatureInfo) { var hResult = SignerSign( pSubjectInfo,null,hResult)); } } }}总结
以上是内存溢出为你收集整理的有没有人有任何代码从C#调用SignerSignEx?全部内容,希望文章能够帮你解决有没有人有任何代码从C#调用SignerSignEx?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)