Translate to C#

D

David Anton

(via C++ to C# Converter - some adjustments will certainly be required - the
header files wincrypt.h and wintrust.h are not factored into the conversion,
but if you download the demo and know the location of these files you can
include them in the conversion):

using System;
public static class GlobalMembers
{

public static int GetProgAndPublisherInfo(PCMSG_SIGNER_INFO pSignerInfo,
PSPROG_PUBLISHERINFO Info)
{
int fReturn = 0;
PSPC_SP_OPUS_INFO OpusInfo = null;
uint dwData;
int fResult;

try
{
// Loop through authenticated attributes and find
// SPC_SP_OPUS_INFO_OBJID OID.
for (uint n = 0; n < pSignerInfo.AuthAttrs.cAttr; n++)
{
if (lstrcmpA(SPC_SP_OPUS_INFO_OBJID,
pSignerInfo.AuthAttrs.rgAttr[n].pszObjId) == 0)
{
// Get Size of SPC_SP_OPUS_INFO structure.
fResult = CryptDecodeObject((X509_ASN_ENCODING | PKCS_7_ASN_ENCODING),
SPC_SP_OPUS_INFO_OBJID, pSignerInfo.AuthAttrs.rgAttr[n].rgValue[0].pbData,
pSignerInfo.AuthAttrs.rgAttr[n].rgValue[0].cbData, 0, null, dwData);
if (fResult == 0)
{
_tprintf(_T("CryptDecodeObject failed with %x\n"), GetLastError());
__leave;
}

// Allocate memory for SPC_SP_OPUS_INFO structure.
OpusInfo = (PSPC_SP_OPUS_INFO)LocalAlloc(LPTR, dwData);
if (OpusInfo == null)
{
_tprintf(_T("Unable to allocate memory for Publisher Info.\n"));
__leave;
}

// Decode and get SPC_SP_OPUS_INFO structure.
fResult = CryptDecodeObject((X509_ASN_ENCODING | PKCS_7_ASN_ENCODING),
SPC_SP_OPUS_INFO_OBJID, pSignerInfo.AuthAttrs.rgAttr[n].rgValue[0].pbData,
pSignerInfo.AuthAttrs.rgAttr[n].rgValue[0].cbData, 0, OpusInfo, dwData);
if (fResult == 0)
{
_tprintf(_T("CryptDecodeObject failed with %x\n"), GetLastError());
__leave;
}

// Fill in Program Name if present.
if (OpusInfo.pwszProgramName)
{
Info.lpszProgramName =
AllocateAndCopyWideString(OpusInfo.pwszProgramName);
}
else
Info.lpszProgramName = null;

// Fill in Publisher Information if present.
if (OpusInfo.pPublisherInfo)
{

switch (OpusInfo.pPublisherInfo.dwLinkChoice)
{
case SPC_URL_LINK_CHOICE:
Info.lpszPublisherLink =
AllocateAndCopyWideString(OpusInfo.pPublisherInfo.pwszUrl);
break;

case SPC_FILE_LINK_CHOICE:
Info.lpszPublisherLink =
AllocateAndCopyWideString(OpusInfo.pPublisherInfo.pwszFile);
break;

default:
Info.lpszPublisherLink = null;
break;
}
}
else
{
Info.lpszPublisherLink = null;
}

// Fill in More Info if present.
if (OpusInfo.pMoreInfo)
{
switch (OpusInfo.pMoreInfo.dwLinkChoice)
{
case SPC_URL_LINK_CHOICE:
Info.lpszMoreInfoLink =
AllocateAndCopyWideString(OpusInfo.pMoreInfo.pwszUrl);
break;

case SPC_FILE_LINK_CHOICE:
Info.lpszMoreInfoLink =
AllocateAndCopyWideString(OpusInfo.pMoreInfo.pwszFile);
break;

default:
Info.lpszMoreInfoLink = null;
break;
}
}
else
{
Info.lpszMoreInfoLink = null;
}

fReturn = 1;

break; // Break from for loop.
} // lstrcmp SPC_SP_OPUS_INFO_OBJID
} // for
}
finally
{
if (OpusInfo != null)
LocalFree(OpusInfo);
}

return fReturn;
}
public static int GetDateOfTimeStamp(PCMSG_SIGNER_INFO pSignerInfo, ref
SYSTEMTIME st)
{
int fResult;
FILETIME lft;
FILETIME ft;
uint dwData;
int fReturn = 0;

// Loop through authenticated attributes and find
// szOID_RSA_signingTime OID.
for (uint n = 0; n < pSignerInfo.AuthAttrs.cAttr; n++)
{
if (lstrcmpA(szOID_RSA_signingTime,
pSignerInfo.AuthAttrs.rgAttr[n].pszObjId) == 0)
{
// Decode and get FILETIME structure.
dwData = sizeof(ft);
fResult = CryptDecodeObject((X509_ASN_ENCODING | PKCS_7_ASN_ENCODING),
szOID_RSA_signingTime, pSignerInfo.AuthAttrs.rgAttr[n].rgValue[0].pbData,
pSignerInfo.AuthAttrs.rgAttr[n].rgValue[0].cbData, 0, (IntPtr) ft, dwData);
if (fResult == 0)
{
_tprintf(_T("CryptDecodeObject failed with %x\n"), GetLastError());
break;
}

// Convert to local time.
FileTimeToLocalFileTime(ft, lft);
FileTimeToSystemTime(lft, st);

fReturn = 1;

break; // Break from for loop.

} //lstrcmp szOID_RSA_signingTime
} // for

return fReturn;
}
public static int PrintCertificateInfo(PCCERT_CONTEXT pCertContext)
{
int fReturn = 0;
string szName = null;
uint dwData;

try
{
// Print Serial Number.
_tprintf(_T("Serial Number: "));
dwData = pCertContext.pCertInfo.SerialNumber.cbData;
for (uint n = 0; n < dwData; n++)
{
_tprintf(_T("%02x "), pCertContext.pCertInfo.SerialNumber.pbData[dwData
- (n + 1)]);
}
_tprintf(_T("\n"));

// Get Issuer name size.
if (!(dwData = CertGetNameString(pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, null, null, 0)))
{
_tprintf(_T("CertGetNameString failed.\n"));
__leave;
}

// Allocate memory for Issuer name.
szName = (string)LocalAlloc(LPTR, dwData * sizeof(char));
if (szName == null)
{
_tprintf(_T("Unable to allocate memory for issuer name.\n"));
__leave;
}

// Get Issuer name.
if (!(CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE,
CERT_NAME_ISSUER_FLAG, null, szName, dwData)))
{
_tprintf(_T("CertGetNameString failed.\n"));
__leave;
}

// print Issuer name.
_tprintf(_T("Issuer Name: %s\n"), szName);
LocalFree(szName);
szName = null;

// Get Subject name size.
if (!(dwData = CertGetNameString(pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, null, null, 0)))
{
_tprintf(_T("CertGetNameString failed.\n"));
__leave;
}

// Allocate memory for subject name.
szName = (string)LocalAlloc(LPTR, dwData * sizeof(char));
if (szName == null)
{
_tprintf(_T("Unable to allocate memory for subject name.\n"));
__leave;
}

// Get subject name.
if (!(CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
null, szName, dwData)))
{
_tprintf(_T("CertGetNameString failed.\n"));
__leave;
}

// Print Subject Name.
_tprintf(_T("Subject Name: %s\n"), szName);

fReturn = 1;
}
finally
{
if (szName != null)
LocalFree(szName);
}

return fReturn;
}
public static int GetTimeStampSignerInfo(PCMSG_SIGNER_INFO pSignerInfo, ref
PCMSG_SIGNER_INFO pCounterSignerInfo)
{
PCCERT_CONTEXT pCertContext = null;
int fReturn = 0;
int fResult;
uint dwSize;

try
{
pCounterSignerInfo = null;

// Loop through unathenticated attributes for
// szOID_RSA_counterSign OID.
for (uint n = 0; n < pSignerInfo.UnauthAttrs.cAttr; n++)
{
if (lstrcmpA(pSignerInfo.UnauthAttrs.rgAttr[n].pszObjId,
szOID_RSA_counterSign) == 0)
{
// Get size of CMSG_SIGNER_INFO structure.
fResult = CryptDecodeObject((X509_ASN_ENCODING | PKCS_7_ASN_ENCODING),
PKCS7_SIGNER_INFO, pSignerInfo.UnauthAttrs.rgAttr[n].rgValue[0].pbData,
pSignerInfo.UnauthAttrs.rgAttr[n].rgValue[0].cbData, 0, null, dwSize);
if (fResult == 0)
{
_tprintf(_T("CryptDecodeObject failed with %x\n"), GetLastError());
__leave;
}

// Allocate memory for CMSG_SIGNER_INFO.
pCounterSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSize);
if (pCounterSignerInfo == null)
{
_tprintf(_T("Unable to allocate memory for timestamp info.\n"));
__leave;
}

// Decode and get CMSG_SIGNER_INFO structure
// for timestamp certificate.
fResult = CryptDecodeObject((X509_ASN_ENCODING | PKCS_7_ASN_ENCODING),
PKCS7_SIGNER_INFO, pSignerInfo.UnauthAttrs.rgAttr[n].rgValue[0].pbData,
pSignerInfo.UnauthAttrs.rgAttr[n].rgValue[0].cbData, 0, (IntPtr)
pCounterSignerInfo, dwSize);
if (fResult == 0)
{
_tprintf(_T("CryptDecodeObject failed with %x\n"), GetLastError());
__leave;
}

fReturn = 1;

break; // Break from for loop.
}
}
}
finally
{
// Clean up.
if (pCertContext != null)
CertFreeCertificateContext(pCertContext);
}

return fReturn;
}

public static int _tmain(int argc, ref string[] argv)
{
string szFileName = new string(new char[MAX_PATH]);
HCERTSTORE hStore = null;
HCRYPTMSG hMsg = null;
PCCERT_CONTEXT pCertContext = null;
int fResult;
uint dwEncoding;
uint dwContentType;
uint dwFormatType;
PCMSG_SIGNER_INFO pSignerInfo = null;
PCMSG_SIGNER_INFO pCounterSignerInfo = null;
uint dwSignerInfo;
CERT_INFO CertInfo;
SPROG_PUBLISHERINFO ProgPubInfo = new SPROG_PUBLISHERINFO();
SYSTEMTIME st;

ZeroMemory(ProgPubInfo, sizeof(ProgPubInfo));
try
{
if (argc != 2)
{
_tprintf(_T("Usage: SignedFileInfo <filename>\n"));
return 0;
}

#if UNICODE
lstrcpynW(szFileName, argv[1], MAX_PATH);
#else
if (mbstowcs(szFileName, argv[1], MAX_PATH) == -1)
{
Console.Write("Unable to convert to unicode.\n");
__leave;
}
#endif

// Get message handle and store handle from the signed file.
fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE, szFileName,
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED, CERT_QUERY_FORMAT_FLAG_BINARY, 0,
dwEncoding, dwContentType, dwFormatType, hStore, hMsg, null);
if (fResult == 0)
{
_tprintf(_T("CryptQueryObject failed with %x\n"), GetLastError());
__leave;
}

// Get signer information size.
fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, null,
dwSignerInfo);
if (fResult == 0)
{
_tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());
__leave;
}

// Allocate memory for signer information.
pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);
if (pSignerInfo == null)
{
_tprintf(_T("Unable to allocate memory for Signer Info.\n"));
__leave;
}

// Get Signer Information.
fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0,
(IntPtr)pSignerInfo, dwSignerInfo);
if (fResult == 0)
{
_tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());
__leave;
}

// Get program name and publisher information from
// signer info structure.
if (GetProgAndPublisherInfo(pSignerInfo, ProgPubInfo))
{
if (ProgPubInfo.lpszProgramName != null)
{
Console.Write("Program Name : {0}\n", ProgPubInfo.lpszProgramName);
}

if (ProgPubInfo.lpszPublisherLink != null)
{
Console.Write("Publisher Link : {0}\n", ProgPubInfo.lpszPublisherLink);
}

if (ProgPubInfo.lpszMoreInfoLink != null)
{
Console.Write("MoreInfo Link : {0}\n", ProgPubInfo.lpszMoreInfoLink);
}
}

_tprintf(_T("\n"));

// Search for the signer certificate in the temporary
// certificate store.
CertInfo.Issuer = pSignerInfo.Issuer;
CertInfo.SerialNumber = pSignerInfo.SerialNumber;

pCertContext = CertFindCertificateInStore(hStore, (X509_ASN_ENCODING |
PKCS_7_ASN_ENCODING), 0, CERT_FIND_SUBJECT_CERT, (IntPtr) CertInfo, null);
if (pCertContext == null)
{
_tprintf(_T("CertFindCertificateInStore failed with %x\n"),
GetLastError());
__leave;
}

// Print Signer certificate information.
_tprintf(_T("Signer Certificate:\n\n"));
PrintCertificateInfo(pCertContext);
_tprintf(_T("\n"));

// Get the timestamp certificate signerinfo structure.
if (GetTimeStampSignerInfo(pSignerInfo, pCounterSignerInfo))
{
// Search for Timestamp certificate in the temporary
// certificate store.
CertInfo.Issuer = pCounterSignerInfo.Issuer;
CertInfo.SerialNumber = pCounterSignerInfo.SerialNumber;

pCertContext = CertFindCertificateInStore(hStore, (X509_ASN_ENCODING |
PKCS_7_ASN_ENCODING), 0, CERT_FIND_SUBJECT_CERT, (IntPtr) CertInfo, null);
if (pCertContext == null)
{
_tprintf(_T("CertFindCertificateInStore failed with %x\n"),
GetLastError());
__leave;
}

// Print timestamp certificate information.
_tprintf(_T("TimeStamp Certificate:\n\n"));
PrintCertificateInfo(pCertContext);
_tprintf(_T("\n"));

// Find Date of timestamp.
if (GetDateOfTimeStamp(pCounterSignerInfo, st))
{
_tprintf(_T("Date of TimeStamp : %02d/%02d/%04d %02d:%02d\n"),
st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute);
}
_tprintf(_T("\n"));
}
}
finally
{
// Clean up.
if (ProgPubInfo.lpszProgramName != null)
LocalFree(ProgPubInfo.lpszProgramName);
if (ProgPubInfo.lpszPublisherLink != null)
LocalFree(ProgPubInfo.lpszPublisherLink);
if (ProgPubInfo.lpszMoreInfoLink != null)
LocalFree(ProgPubInfo.lpszMoreInfoLink);

if (pSignerInfo != null)
LocalFree(pSignerInfo);
if (pCounterSignerInfo != null)
LocalFree(pCounterSignerInfo);
if (pCertContext != null)
CertFreeCertificateContext(pCertContext);
if (hStore != null)
CertCloseStore(hStore, 0);
if (hMsg != null)
CryptMsgClose(hMsg);
}
return 0;
}

public static string AllocateAndCopyWideString(string inputString)
{
string outputString = null;

outputString = (string)LocalAlloc(LPTR, (inputString.Length + 1) *
sizeof(char));
if (outputString != null)
{
lstrcpyW(outputString, inputString);
}
return outputString;
}
}

//C++ TO C# CONVERTER TODO TASK: There is no equivalent to most C++ 'pragma'
directives in C#:
//#pragma comment(lib, "crypt32.lib")

//C++ TO C# CONVERTER NOTE: The following #define macro was replaced in-line:
//ORIGINAL LINE: #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
#define ENCODING

public class SPROG_PUBLISHERINFO
{
public string lpszProgramName;
public string lpszPublisherLink;
public string lpszMoreInfoLink;
}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Java to C#
Java to VB
Instant C#: convert VB to C#
Instant VB: convert C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top