指定pwszTimestampURL时CryptUIWizDigitalSign失败

时间:2012-07-13 22:30:11

标签: windows pinvoke code-signing authenticode

我在调用CryptUIWizDigitalSign以编程方式使用我们的公共代码签名证书签署可执行文件时遇到问题,而没有显示任何UI。该证书是Comodo代码签名证书。

当timestamp URL参数设置为null时,它工作正常,但每当我传递除null之外的任何内容时,调用将失败(返回零)。

问题是如果没有时间戳,就没有会签,因此签名有效性问题就会越来越严重。

环境是Windows 7 x64。有一个工作标准的互联网连接。通过嗅探网络流量,CryptUIWizDigitalSign没有尝试联系时间戳服务器。

我通过PInvoke从.NET中调用它,但我怀疑这会有什么不同。

关于这个功能的网上不是很多......

Dim cert As X509Certificate2 = New X509Certificate2("mycert.pfx", "password")
Dim pSigningCertContext As IntPtr = cert.Handle

Dim digitalSignInfo As CRYPTUI_WIZ_DIGITAL_SIGN_INFO 
    = New CRYPTUI_WIZ_DIGITAL_SIGN_INFO
digitalSignInfo.dwSize = Marshal.SizeOf(digitalSignInfo)
digitalSignInfo.dwSubjectChoice = CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE
digitalSignInfo.pwszFileName = "C:\temp\installer.exe"
digitalSignInfo.dwSigningCertChoice = CRYPTUI_WIZ_DIGITAL_SIGN_CERT
digitalSignInfo.pSigningCertContext = pSigningCertContext
digitalSignInfo.pwszTimestampURL = "http://timestamp.comodoca.com/authenticode"
digitalSignInfo.dwAdditionalCertChoice = 0
digitalSignInfo.pSignExtInfo = IntPtr.Zero

If (Not CryptUIWizDigitalSign(CRYPTUI_WIZ_NO_UI, IntPtr.Zero, vbNullString, 
                              digitalSignInfo, pSignContext)) Then
    Throw New Win32Exception(Marshal.GetLastWin32Error(), 
        "CryptUIWizDigitalSign")
End If

CRYPTUI_WIZ_DIGITAL_SIGN_INFO类型定义为:

<StructLayout(LayoutKind.Sequential)> _
Public Structure CRYPTUI_WIZ_DIGITAL_SIGN_INFO
    Public dwSize As Int32
    Public dwSubjectChoice As Int32
    <MarshalAs(UnmanagedType.LPWStr)> Public pwszFileName As String
    Public dwSigningCertChoice As Int32
    Public pSigningCertContext As IntPtr
    Public pwszTimestampURL As String
    Public dwAdditionalCertChoice As Int32
    Public pSignExtInfo As IntPtr
End Structure

Public Const CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE As Int32 = 1
Public Const CRYPTUI_WIZ_DIGITAL_SIGN_CERT As Int32 = 1
Public Const CRYPTUI_WIZ_NO_UI As Int32 = 1

2 个答案:

答案 0 :(得分:2)

您已在MarshalAs但未pwszFileName上应用pwszTimestampURL属性,是否有任何理由?它们在documentation for CRYPTUI_WIZ_DIGITAL_SIGN_INFO中的描述相同:

pwszFileName:

  

指向以null结尾的Unicode字符串的指针,该字符串包含要签名的文件的路径和文件名。如果为dwSubjectChoice成员指定了CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE,则使用此成员。

pwszTimestampURL:

  

指向以null结尾的Unicode字符串的指针,该字符串包含时间戳的URL。

答案 1 :(得分:0)

已修复 - 仅在32位进程内工作。