Change the EOL

This commit is contained in:
Don HO 2018-02-27 01:18:00 +01:00
parent d6daac2434
commit abcbec181a

View File

@ -994,7 +994,7 @@ generic_string GetLastErrorAsString(DWORD errorCode)
LocalFree(messageBuffer); LocalFree(messageBuffer);
return errorMsg; return errorMsg;
} }
HWND CreateToolTip(int toolID, HWND hDlg, HINSTANCE hInst, const PTSTR pszText) HWND CreateToolTip(int toolID, HWND hDlg, HINSTANCE hInst, const PTSTR pszText)
{ {
@ -1037,119 +1037,119 @@ HWND CreateToolTip(int toolID, HWND hDlg, HINSTANCE hInst, const PTSTR pszText)
} }
return hwndTip; return hwndTip;
} }
bool isCertificateValidated(const generic_string & fullFilePath, const generic_string & subjectName2check) bool isCertificateValidated(const generic_string & fullFilePath, const generic_string & subjectName2check)
{ {
bool isOK = false; bool isOK = false;
HCERTSTORE hStore = NULL; HCERTSTORE hStore = NULL;
HCRYPTMSG hMsg = NULL; HCRYPTMSG hMsg = NULL;
PCCERT_CONTEXT pCertContext = NULL; PCCERT_CONTEXT pCertContext = NULL;
BOOL result; BOOL result;
DWORD dwEncoding, dwContentType, dwFormatType; DWORD dwEncoding, dwContentType, dwFormatType;
PCMSG_SIGNER_INFO pSignerInfo = NULL; PCMSG_SIGNER_INFO pSignerInfo = NULL;
DWORD dwSignerInfo; DWORD dwSignerInfo;
CERT_INFO CertInfo; CERT_INFO CertInfo;
LPTSTR szName = NULL; LPTSTR szName = NULL;
generic_string subjectName; generic_string subjectName;
try { try {
// Get message handle and store handle from the signed file. // Get message handle and store handle from the signed file.
result = CryptQueryObject(CERT_QUERY_OBJECT_FILE, result = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
fullFilePath.c_str(), fullFilePath.c_str(),
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED, CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
CERT_QUERY_FORMAT_FLAG_BINARY, CERT_QUERY_FORMAT_FLAG_BINARY,
0, 0,
&dwEncoding, &dwEncoding,
&dwContentType, &dwContentType,
&dwFormatType, &dwFormatType,
&hStore, &hStore,
&hMsg, &hMsg,
NULL); NULL);
if (!result) if (!result)
{ {
generic_string errorMessage = TEXT("Check certificate of ") + fullFilePath + TEXT(" : "); generic_string errorMessage = TEXT("Check certificate of ") + fullFilePath + TEXT(" : ");
errorMessage += GetLastErrorAsString(GetLastError()); errorMessage += GetLastErrorAsString(GetLastError());
throw errorMessage; throw errorMessage;
} }
// Get signer information size. // Get signer information size.
result = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &dwSignerInfo); result = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &dwSignerInfo);
if (!result) if (!result)
{ {
generic_string errorMessage = TEXT("CryptMsgGetParam first call: "); generic_string errorMessage = TEXT("CryptMsgGetParam first call: ");
errorMessage += GetLastErrorAsString(GetLastError()); errorMessage += GetLastErrorAsString(GetLastError());
throw errorMessage; throw errorMessage;
} }
// Allocate memory for signer information. // Allocate memory for signer information.
pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo); pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);
if (!pSignerInfo) if (!pSignerInfo)
{ {
generic_string errorMessage = TEXT("CryptMsgGetParam memory allocation problem: "); generic_string errorMessage = TEXT("CryptMsgGetParam memory allocation problem: ");
errorMessage += GetLastErrorAsString(GetLastError()); errorMessage += GetLastErrorAsString(GetLastError());
throw errorMessage; throw errorMessage;
} }
// Get Signer Information. // Get Signer Information.
result = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, (PVOID)pSignerInfo, &dwSignerInfo); result = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, (PVOID)pSignerInfo, &dwSignerInfo);
if (!result) if (!result)
{ {
generic_string errorMessage = TEXT("CryptMsgGetParam: "); generic_string errorMessage = TEXT("CryptMsgGetParam: ");
errorMessage += GetLastErrorAsString(GetLastError()); errorMessage += GetLastErrorAsString(GetLastError());
throw errorMessage; throw errorMessage;
} }
// Search for the signer certificate in the temporary // Search for the signer certificate in the temporary
// certificate store. // certificate store.
CertInfo.Issuer = pSignerInfo->Issuer; CertInfo.Issuer = pSignerInfo->Issuer;
CertInfo.SerialNumber = pSignerInfo->SerialNumber; CertInfo.SerialNumber = pSignerInfo->SerialNumber;
pCertContext = CertFindCertificateInStore(hStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, (PVOID)&CertInfo, NULL); pCertContext = CertFindCertificateInStore(hStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, (PVOID)&CertInfo, NULL);
if (not pCertContext) if (not pCertContext)
{ {
generic_string errorMessage = TEXT("Certificate context: "); generic_string errorMessage = TEXT("Certificate context: ");
errorMessage += GetLastErrorAsString(GetLastError()); errorMessage += GetLastErrorAsString(GetLastError());
throw errorMessage; throw errorMessage;
} }
DWORD dwData; DWORD dwData;
// Get Subject name size. // Get Subject name size.
dwData = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0); dwData = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
if (dwData <= 1) if (dwData <= 1)
{ {
throw generic_string(TEXT("Certificate checking error: getting data size problem.")); throw generic_string(TEXT("Certificate checking error: getting data size problem."));
} }
// Allocate memory for subject name. // Allocate memory for subject name.
szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR)); szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));
if (!szName) if (!szName)
{ {
throw generic_string(TEXT("Certificate checking error: memory allocation problem.")); throw generic_string(TEXT("Certificate checking error: memory allocation problem."));
} }
// Get subject name. // Get subject name.
if (CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, szName, dwData) <= 1) if (CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, szName, dwData) <= 1)
{ {
throw generic_string(TEXT("Cannot get certificate info.")); throw generic_string(TEXT("Cannot get certificate info."));
} }
// check Subject name. // check Subject name.
subjectName = szName; subjectName = szName;
if (subjectName != subjectName2check) if (subjectName != subjectName2check)
{ {
throw generic_string(TEXT("Certificate checking error: the certificate is not matched.")); throw generic_string(TEXT("Certificate checking error: the certificate is not matched."));
} }
isOK = true; isOK = true;
} }
catch (generic_string s) catch (generic_string s)
{ {
// display error message // display error message
MessageBox(NULL, s.c_str(), TEXT("Certificate checking"), MB_OK); MessageBox(NULL, s.c_str(), TEXT("Certificate checking"), MB_OK);
} }
catch (...) catch (...)
{ {
@ -1158,38 +1158,38 @@ bool isCertificateValidated(const generic_string & fullFilePath, const generic_s
errorMessage += GetLastErrorAsString(GetLastError()); errorMessage += GetLastErrorAsString(GetLastError());
MessageBox(NULL, errorMessage.c_str(), TEXT("Certificate checking"), MB_OK); MessageBox(NULL, errorMessage.c_str(), TEXT("Certificate checking"), MB_OK);
} }
// Clean up. // Clean up.
if (pSignerInfo != NULL) LocalFree(pSignerInfo); if (pSignerInfo != NULL) LocalFree(pSignerInfo);
if (pCertContext != NULL) CertFreeCertificateContext(pCertContext); if (pCertContext != NULL) CertFreeCertificateContext(pCertContext);
if (hStore != NULL) CertCloseStore(hStore, 0); if (hStore != NULL) CertCloseStore(hStore, 0);
if (hMsg != NULL) CryptMsgClose(hMsg); if (hMsg != NULL) CryptMsgClose(hMsg);
if (szName != NULL) LocalFree(szName); if (szName != NULL) LocalFree(szName);
return isOK; return isOK;
} }
bool isAssoCommandExisting(LPCTSTR FullPathName) bool isAssoCommandExisting(LPCTSTR FullPathName)
{ {
bool isAssoCommandExisting = false; bool isAssoCommandExisting = false;
bool isFileExisting = PathFileExists(FullPathName) != FALSE; bool isFileExisting = PathFileExists(FullPathName) != FALSE;
if (isFileExisting) if (isFileExisting)
{ {
PTSTR ext = PathFindExtension(FullPathName); PTSTR ext = PathFindExtension(FullPathName);
HRESULT hres; HRESULT hres;
wchar_t buffer[MAX_PATH] = TEXT(""); wchar_t buffer[MAX_PATH] = TEXT("");
DWORD bufferLen = MAX_PATH; DWORD bufferLen = MAX_PATH;
// check if association exist // check if association exist
hres = AssocQueryString(ASSOCF_VERIFY|ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_COMMAND, ext, NULL, buffer, &bufferLen); hres = AssocQueryString(ASSOCF_VERIFY|ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_COMMAND, ext, NULL, buffer, &bufferLen);
isAssoCommandExisting = (hres == S_OK) // check if association exist and no error isAssoCommandExisting = (hres == S_OK) // check if association exist and no error
&& (buffer != NULL) // check if buffer is not NULL && (buffer != NULL) // check if buffer is not NULL
&& (wcsstr(buffer, TEXT("notepad++.exe")) == NULL); // check association with notepad++ && (wcsstr(buffer, TEXT("notepad++.exe")) == NULL); // check association with notepad++
} }
return isAssoCommandExisting; return isAssoCommandExisting;
} }