Home > Software engineering >  Why always fail CertGetNameString function
Why always fail CertGetNameString function

Time:09-19


Bool GetCertSubject (IN PCCERT_CONTEXT pCertContext, OUT wstring & amp; StrSubjectName)
{
BOOL fReturn=FALSE;
LPTSTR szName=NULL;
DWORD dwData=https://bbs.csdn.net/topics/0;

Do
{
//Get the Subject name size.
if (! (dwData=https://bbs.csdn.net/topics/CertGetNameString (pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
NULL,
0)))
{
Break;
}
//the Allocate memory for the subject name.
SzName=(LPTSTR) LocalAlloc (LPTR, dwData * sizeof (TCHAR));
if (! SzName)
{
break;
}
//Get the subject name.
if (! (CertGetNameString (pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
SzName,
DwData)))
{
break;
}

StrSubjectName=szName (PWCHAR);
FReturn=TRUE;

} while (FALSE);

If (szName!=NULL)
{
LocalFree (szName);
}

Return fReturn;
}


Bool GetProcCertSubject (IN wstring strFileName, OUT wstring & amp; StrSubjectName)
{
BOOL fResult=FALSE;
HCERTSTORE hStore=NULL;
HCRYPTMSG hMsg=NULL;
PCCERT_CONTEXT pCertContext=NULL;
DWORD dwEncoding, dwContentType dwFormatType;
PCMSG_SIGNER_INFO pSignerInfo=NULL;
DWORD dwSignerInfo;
CERT_INFO CertInfo;

If (strFileName. Length ()==0)
{
return FALSE;
}
Do
{
//Get the message the handle and store the handle from the signed file.
CERT_QUERY_OBJECT_BLOB fResult=CryptQueryObject (CERT_QUERY_OBJECT_FILE,/* and */
StrFileName. C_str (),
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
CERT_QUERY_FORMAT_FLAG_BINARY,
0,
& DwEncoding,
& DwContentType,
& DwFormatType,
& HStore.
& HMsg,
NULL);
if (! FResult)
{
break;
}
//Get signer information size.
FResult=CryptMsgGetParam (hMsg,
CMSG_SIGNER_INFO_PARAM,
0,
NULL,
& DwSignerInfo);
if (! FResult)
{
break;
}
//the Allocate memory for signer information.
PSignerInfo=(PCMSG_SIGNER_INFO) LocalAlloc (LPTR dwSignerInfo);
if (! PSignerInfo)
{
FResult=FALSE;
break;
}
//Get Signer Information.
FResult=CryptMsgGetParam (hMsg,
CMSG_SIGNER_INFO_PARAM,
0,
(PVOID) pSignerInfo,
& DwSignerInfo);
if (! FResult)
{
break;
}

CertInfo. Issuer=pSignerInfo - & gt; The Issuer;
CertInfo. SerialNumber=pSignerInfo - & gt; SerialNumber;
PCertContext=CertFindCertificateInStore (hStore.
ENCODING,
0,
CERT_FIND_SUBJECT_CERT,
(PVOID) & amp; CertInfo,
NULL);
if (! PCertContext)
{
FResult=FALSE;
break;
}
FResult=GetCertSubject (pCertContext strSubjectName);

} while (FALSE);

If (pSignerInfo) LocalFree (pSignerInfo);
If (pCertContext) CertFreeCertificateContext (pCertContext);
If (hStore) CertCloseStore (hStore, 0);
If (hMsg) CryptMsgClose (hMsg);
Return fResult;
}

CodePudding user response:

#include
#include
# pragma comment (lib, "psapi. Lib")
# pragma comment (lib, "crypt32. Lib")
# define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
Header files are included all no problem

CodePudding user response:

reference 1st floor jixiang1983 response:
# include & lt; Windows. H>
#include
# pragma comment (lib, "psapi. Lib")
# pragma comment (lib, "crypt32. Lib")
# define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
Header files are included all no problem

1) GetLastError () returns? Or you posted the full call parameters,
2) GetCertSubject, this function is where come of?
  • Related