nx二次开发 编写dll批量签名代码
时间: 2024-02-24 12:54:25 浏览: 125
签名 DLL 批量签名的代码示例,基于 Windows 操作系统和 Microsoft Visual Studio 编写。
```c++
#include <Windows.h>
#include <WinTrust.h>
#include <SoftPub.h>
#pragma comment(lib, "wintrust")
BOOL SignFile(LPCWSTR lpszFilePath, LPCWSTR lpszCertSubjectName);
BOOL SignFile(LPCWSTR lpszFilePath, LPCWSTR lpszCertSubjectName)
{
BOOL bResult = FALSE;
GUID guidAction = WINTRUST_ACTION_GENERIC_VERIFY_V2;
WINTRUST_FILE_INFO fileInfo;
WINTRUST_DATA trustData;
memset(&fileInfo, 0, sizeof(WINTRUST_FILE_INFO));
memset(&trustData, 0, sizeof(WINTRUST_DATA));
fileInfo.cbStruct = sizeof(WINTRUST_FILE_INFO);
fileInfo.pcwszFilePath = lpszFilePath;
fileInfo.hFile = NULL;
fileInfo.pgKnownSubject = NULL;
memset(&trustData, 0, sizeof(WINTRUST_DATA));
trustData.cbStruct = sizeof(WINTRUST_DATA);
trustData.pPolicyCallbackData = NULL;
trustData.pSIPClientData = NULL;
trustData.dwUIChoice = WTD_UI_NONE;
trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
trustData.dwUnionChoice = WTD_CHOICE_FILE;
trustData.pFile = &fileInfo;
trustData.dwStateAction = WTD_STATEACTION_VERIFY;
trustData.hWVTStateData = NULL;
trustData.pwszURLReference = NULL;
trustData.dwProvFlags = WTD_SAFER_FLAG | WTD_HASH_ONLY_FLAG;
trustData.dwUIContext = 0;
trustData.pSignatureSettings = NULL;
if (WinVerifyTrust(NULL, &guidAction, &trustData) == ERROR_SUCCESS)
{
HCRYPTPROV hProv = NULL;
HCRYPTHASH hHash = NULL;
HANDLE hFile = NULL;
DWORD dwFileSize = 0;
LPBYTE lpFileBuffer = NULL;
DWORD dwBytesRead = 0;
if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
hFile = CreateFile(lpszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
dwFileSize = GetFileSize(hFile, NULL);
lpFileBuffer = (LPBYTE)malloc(dwFileSize);
if (lpFileBuffer)
{
if (ReadFile(hFile, lpFileBuffer, dwFileSize, &dwBytesRead, NULL))
{
if (CryptHashData(hHash, lpFileBuffer, dwBytesRead, 0))
{
DWORD dwSignerInfoSize = 0;
CryptSignAndEncodeCertificate(NULL, X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED, lpFileBuffer, dwBytesRead, NULL, NULL, NULL, &dwSignerInfoSize);
if (dwSignerInfoSize > 0)
{
BYTE* lpSignerInfo = (BYTE*)malloc(dwSignerInfoSize);
if (lpSignerInfo)
{
if (CryptSignAndEncodeCertificate(hProv, X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED, lpFileBuffer, dwBytesRead, NULL, NULL, lpSignerInfo, &dwSignerInfoSize))
{
HCERTSTORE hStore = CertOpenSystemStore(NULL, L"MY");
if (hStore)
{
PCCERT_CONTEXT pCertContext = CertFindCertificateInStore(hStore, X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_STR, lpszCertSubjectName, NULL);
if (pCertContext)
{
CRYPT_SIGN_MESSAGE_PARA signParams;
memset(&signParams, 0, sizeof(CRYPT_SIGN_MESSAGE_PARA));
signParams.cbSize = sizeof(CRYPT_SIGN_MESSAGE_PARA);
signParams.dwMsgEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
signParams.pSigningCert = pCertContext;
signParams.HashAlgorithm.pszObjId = szOID_RSA_SHA1RSA;
signParams.cMsgCert = 1;
signParams.rgpMsgCert = &pCertContext;
signParams.cAuthAttr = 0;
signParams.dwInnerContentType = 0;
signParams.cMsgCrl = 0;
signParams.cUnauthAttr = 0;
DWORD dwSignedMessageSize = 0;
CryptSignMessage(&signParams, FALSE, 1, &lpSignerInfo, &dwSignerInfoSize, NULL, &dwSignedMessageSize);
if (dwSignedMessageSize > 0)
{
BYTE* lpSignedMessage = (BYTE*)malloc(dwSignedMessageSize);
if (lpSignedMessage)
{
if (CryptSignMessage(&signParams, FALSE, 1, &lpSignerInfo, &dwSignerInfoSize, lpSignedMessage, &dwSignedMessageSize))
{
DWORD dwNewFileSize = dwFileSize + dwSignedMessageSize;
HANDLE hNewFile = CreateFile(lpszFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hNewFile != INVALID_HANDLE_VALUE)
{
DWORD dwBytesWritten = 0;
WriteFile(hNewFile, lpSignedMessage, dwSignedMessageSize, &dwBytesWritten, NULL);
WriteFile(hNewFile, lpFileBuffer, dwFileSize, &dwBytesWritten, NULL);
CloseHandle(hNewFile);
bResult = TRUE;
}
}
free(lpSignedMessage);
}
}
CertFreeCertificateContext(pCertContext);
}
CertCloseStore(hStore, 0);
}
}
free(lpSignerInfo);
}
}
}
}
free(lpFileBuffer);
}
CloseHandle(hFile);
}
CryptDestroyHash(hHash);
}
CryptReleaseContext(hProv, 0);
}
}
return bResult;
}
```
此代码使用 WinVerifyTrust 函数验证要签名的文件,并使用 CryptSignAndEncodeCertificate 和 CryptSignMessage 函数签名文件。要使用此代码,您需要将证书安装到 Windows 证书存储中,并指定证书的主题名称。请注意,此代码仅用于参考目的,您需要根据自己的需求进行修改和测试。
阅读全文