public static Signature GetSignature(string fileName) { CheckArgForNullOrEmpty(fileName, "fileName"); CheckIfFileExists(fileName); System.Management.Automation.Signature signature; try { NativeStructs.WINTRUST_DATA wtData; uint winTrustData = SignatureHelper.GetWinTrustData(fileName, out wtData); //if ((int)winTrustData != 0) // SignatureHelper.tracer.WriteLine("GetWinTrustData failed: {0:x}", new object[1] // { // (object) winTrustData // }); signature = SignatureHelper.GetSignatureFromWintrustData(fileName, winTrustData, wtData); uint num = WINTRUST.DestroyWintrustDataStruct(wtData); //if ((int)num != 0) // SignatureHelper.tracer.WriteLine("DestroyWinTrustDataStruct failed: {0:x}", new object[1] //{ // (object) num //}); } catch { signature = SignatureProxy.GenerateSignature(fileName, 2148204800U); } return(signature); }
private static System.Management.Automation.Signature GetSignatureFromWintrustData(string filePath, uint error, NativeStructs.WINTRUST_DATA wtd) { System.Management.Automation.Signature signature = (System.Management.Automation.Signature)null; X509Certificate2 timestamper = (X509Certificate2)null; IntPtr pProvData = WINTRUST.WTHelperProvDataFromStateData(wtd.hWVTStateData); if (pProvData != IntPtr.Zero) { IntPtr provSignerFromChain = WINTRUST.WTHelperGetProvSignerFromChain(pProvData, 0U, 0U, 0U); if (provSignerFromChain != IntPtr.Zero) { X509Certificate2 certFromChain = SignatureHelper.GetCertFromChain(provSignerFromChain); if (certFromChain != null) { NativeStructs.CRYPT_PROVIDER_SGNR cryptProviderSgnr = (NativeStructs.CRYPT_PROVIDER_SGNR)Marshal.PtrToStructure(provSignerFromChain, typeof(NativeStructs.CRYPT_PROVIDER_SGNR)); if ((int)cryptProviderSgnr.csCounterSigners == 1) { timestamper = SignatureHelper.GetCertFromChain(cryptProviderSgnr.pasCounterSigners); } signature = timestamper == null?SignatureProxy.GenerateSignature(filePath, error, certFromChain) : SignatureProxy.GenerateSignature(filePath, error, certFromChain, timestamper); } } } if (signature == null && (int)error != 0) { signature = SignatureProxy.GenerateSignature(filePath, error); } return(signature); }
private static Signature SignFile(NativeStructs.SigningOption option, string fileName, X509Certificate2 certificate, string timeStampServerUrl, string hashAlgorithm) { System.Management.Automation.Signature signature = (System.Management.Automation.Signature)null; IntPtr num = IntPtr.Zero; uint error = 0U; string hashAlgorithm1 = (string)null; CheckArgForNullOrEmpty(fileName, "fileName"); CheckArgForNull((object)certificate, "certificate"); if (!string.IsNullOrEmpty(timeStampServerUrl) && (timeStampServerUrl.Length <= 7 || timeStampServerUrl.IndexOf("http://", StringComparison.OrdinalIgnoreCase) != 0)) { throw new ArgumentException("Time stamp server url required"); } if (!string.IsNullOrEmpty(hashAlgorithm)) { IntPtr oidInfo = CRYPT32.CryptFindOIDInfo(2U, Marshal.StringToHGlobalUni(hashAlgorithm), 0U); if (oidInfo == IntPtr.Zero) { throw new ArgumentException("Invalid hash algorithm"); } hashAlgorithm1 = ((NativeStructs.CRYPT_OID_INFO)Marshal.PtrToStructure(oidInfo, typeof(NativeStructs.CRYPT_OID_INFO))).pszOID; } if (!CertIsGoodForSigning(certificate)) { throw new ArgumentException("Supplied certificate cannot be used to sign files."); } CheckIfFileExists(fileName); try { string timeStampServerUrl1 = (string)null; if (!string.IsNullOrEmpty(timeStampServerUrl)) { timeStampServerUrl1 = timeStampServerUrl; } NativeStructs.CRYPTUI_WIZ_DIGITAL_SIGN_INFO wizDigitalSignInfo = NativeStructs.InitSignInfoStruct(fileName, certificate, timeStampServerUrl1, hashAlgorithm1, option); num = Marshal.AllocCoTaskMem(Marshal.SizeOf((object)wizDigitalSignInfo)); Marshal.StructureToPtr((object)wizDigitalSignInfo, num, false); bool flag = CRYPTUI.CryptUIWizDigitalSign(1U, IntPtr.Zero, IntPtr.Zero, num, IntPtr.Zero); Marshal.DestroyStructure(wizDigitalSignInfo.pSignExtInfo, typeof(NativeStructs.CRYPTUI_WIZ_DIGITAL_SIGN_EXTENDED_INFO)); Marshal.FreeCoTaskMem(wizDigitalSignInfo.pSignExtInfo); if (!flag) { error = SignatureHelper.GetLastWin32Error(); switch (error) { case 2147500037U: case 2147942401U: case 2147954407U: flag = true; break; case 2148073480U: throw new ArgumentException("InvalidHashAlgorithm"); default: throw new ArgumentException(string.Format("CryptUIWizDigitalSign: failed: {0:x}", new object[1] { (object)error })); } } signature = !flag?SignatureProxy.GenerateSignature(fileName, error) : SignatureHelper.GetSignature(fileName); } finally { Marshal.DestroyStructure(num, typeof(NativeStructs.CRYPTUI_WIZ_DIGITAL_SIGN_INFO)); Marshal.FreeCoTaskMem(num); } return(signature); }