[CODE]
//作用:获取软件的数字签名
//参数:
// v_pwszFilePath --- 程序的全路径
// v_pwszSign --- 用于返回数字签名的缓冲区,如果为NULL,
// 那么将会需要的缓冲区大小
// v_iBufSize --- v_pwszSign缓冲区的大小
//返回值:
// -1 --- 失败
// 0 --- 成功
// 其它值 --- 需要的缓冲区大小
//
//*****************************************************//
LONG GetSoftSign(PWCHAR v_pszFilePath, char* v_pszSign, int v_iBufSize)
{
//首先判断参数是否正确
if (v_pszFilePath == NULL) return -1;
HCERTSTORE hStore = NULL;
HCRYPTMSG hMsg = NULL;
PCCERT_CONTEXT pCertContext = NULL;
BOOL bResult;
DWORD dwEncoding, dwContentType, dwFormatType;
PCMSG_SIGNER_INFO pSignerInfo = NULL;
PCMSG_SIGNER_INFO pCounterSignerInfo = NULL;
DWORD dwSignerInfo = NULL;
CERT_INFO CertInfo;
LONG lRet;
DWORD dwDataSize = 0;
char chTemp[MAX_PATH] = { 0 };
do
{
//从签名文件中获取存储句柄
bResult = CryptQueryObject(
CERT_QUERY_OBJECT_FILE,
v_pszFilePath,
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
CERT_QUERY_FORMAT_FLAG_BINARY,
0,
&dwEncoding,
&dwContentType,
&dwFormatType,
&hStore,
&hMsg,
NULL
);
if (!bResult)
{
lRet = -1;
break;
}
//获取签名信息所需的缓冲区大小
bResult = CryptMsgGetParam(
hMsg,
CMSG_SIGNER_INFO_PARAM,
0,
NULL,
&dwSignerInfo
);
if (!bResult)
{
lRet = -1;
break;
}
//分配缓冲区
pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);
if (pSignerInfo == NULL)
{
lRet = -1;
break;
}
//获取签名信息
bResult = CryptMsgGetParam(
hMsg,
CMSG_SIGNER_INFO_PARAM,
0,
pSignerInfo,
&dwSignerInfo
);
if (!bResult)
{
lRet = -1;
break;
}
CertInfo.Issuer = pSignerInfo->Issuer;
CertInfo.SerialNumber = pSignerInfo->SerialNumber;
pCertContext = CertFindCertificateInStore(
hStore,
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
0,
CERT_FIND_SUBJECT_CERT,
(PVOID)&CertInfo,
NULL
);
if (pCertContext == NULL)
{
lRet = -1;
break;
}
//获取数字键名
//没有给定缓冲区,那么说明只要获取下需要的长度
if (v_pszSign == NULL)
{
dwDataSize = CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
NULL,
0
);
if (dwDataSize != 0)
{
lRet = dwDataSize;
}
else
{
lRet = -1;
}
break;
}
if (!(CertGetNameStringA(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
v_pszSign,
v_iBufSize
)
)
)
{
lRet = -1;
break;
}
lRet = 0;
} while (FALSE);
if (pSignerInfo != NULL)
{
LocalFree((HLOCAL)pSignerInfo);
}
return lRet;
}
[/CODE]
最后于 2020-9-17 21:26
被KomiMoe_编辑
,原因: