2015-10-12 21:03:41 +00:00
|
|
|
/*
|
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
|
|
|
* PKI functions (code signing, etc.)
|
|
|
|
* Copyright © 2015 Pete Batard <pete@akeo.ie>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#include <wintrust.h>
|
|
|
|
|
|
|
|
#include "rufus.h"
|
|
|
|
#include "msapi_utf8.h"
|
2015-10-13 22:29:30 +00:00
|
|
|
#include "resource.h"
|
|
|
|
#include "localization.h"
|
2015-10-12 21:03:41 +00:00
|
|
|
|
|
|
|
#define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
|
|
|
|
|
2015-10-13 22:29:30 +00:00
|
|
|
// Signatures names we accept (may be suffixed, but the signature should start with one of those)
|
2015-10-12 21:03:41 +00:00
|
|
|
const char* valid_cert_names[] = { "Akeo Consulting", "Akeo Systems", "Pete Batard" };
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
LPWSTR lpszProgramName;
|
|
|
|
LPWSTR lpszPublisherLink;
|
|
|
|
LPWSTR lpszMoreInfoLink;
|
|
|
|
} SPROG_PUBLISHERINFO, *PSPROG_PUBLISHERINFO;
|
|
|
|
|
|
|
|
// Mostly from https://support.microsoft.com/en-us/kb/323809
|
2015-10-13 22:29:30 +00:00
|
|
|
static char* GetSignatureName(const char* path)
|
2015-10-12 21:03:41 +00:00
|
|
|
{
|
2015-10-13 22:29:30 +00:00
|
|
|
static char szSubjectName[128];
|
|
|
|
char* p = NULL;
|
|
|
|
BOOL r;
|
2015-10-12 21:03:41 +00:00
|
|
|
HCERTSTORE hStore = NULL;
|
|
|
|
HCRYPTMSG hMsg = NULL;
|
|
|
|
PCCERT_CONTEXT pCertContext = NULL;
|
|
|
|
DWORD dwEncoding, dwContentType, dwFormatType, dwSubjectSize;
|
|
|
|
PCMSG_SIGNER_INFO pSignerInfo = NULL;
|
|
|
|
PCMSG_SIGNER_INFO pCounterSignerInfo = NULL;
|
|
|
|
DWORD dwSignerInfo = 0;
|
|
|
|
CERT_INFO CertInfo = { 0 };
|
|
|
|
SPROG_PUBLISHERINFO ProgPubInfo = { 0 };
|
|
|
|
wchar_t *szFileName = utf8_to_wchar(path);
|
|
|
|
|
|
|
|
// Get message handle and store handle from the signed file.
|
|
|
|
r = 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 (!r) {
|
|
|
|
uprintf("PKI: Failed to get store handle for '%s': %s", path, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get signer information size.
|
|
|
|
r = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &dwSignerInfo);
|
|
|
|
if (!r) {
|
|
|
|
uprintf("PKI: Failed to get signer size: %s", WindowsErrorString);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate memory for signer information.
|
|
|
|
pSignerInfo = (PCMSG_SIGNER_INFO)calloc(dwSignerInfo, 1);
|
|
|
|
if (!pSignerInfo) {
|
|
|
|
uprintf("PKI: Could not allocate memory for signer information");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Signer Information.
|
|
|
|
r = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, (PVOID)pSignerInfo, &dwSignerInfo);
|
|
|
|
if (!r) {
|
|
|
|
uprintf("PKI: Failed to get signer information: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the signer certificate in the temporary certificate store.
|
|
|
|
CertInfo.Issuer = pSignerInfo->Issuer;
|
|
|
|
CertInfo.SerialNumber = pSignerInfo->SerialNumber;
|
|
|
|
|
|
|
|
pCertContext = CertFindCertificateInStore(hStore, ENCODING, 0, CERT_FIND_SUBJECT_CERT, (PVOID)&CertInfo, NULL);
|
|
|
|
if (!pCertContext) {
|
|
|
|
uprintf("PKI: Failed to locate signer certificate in temporary store: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Isolate the signing certificate subject name
|
|
|
|
dwSubjectSize = CertGetNameStringA(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
|
|
|
|
szSubjectName, sizeof(szSubjectName));
|
|
|
|
if (dwSubjectSize <= 1) {
|
|
|
|
uprintf("PKI: Failed to get Subject Name");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-10-13 22:29:30 +00:00
|
|
|
uprintf("Downloaded executable is signed by '%s'", szSubjectName);
|
|
|
|
p = szSubjectName;
|
2015-10-12 21:03:41 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
safe_free(szFileName);
|
|
|
|
safe_free(ProgPubInfo.lpszProgramName);
|
|
|
|
safe_free(ProgPubInfo.lpszPublisherLink);
|
|
|
|
safe_free(ProgPubInfo.lpszMoreInfoLink);
|
|
|
|
safe_free(pSignerInfo);
|
|
|
|
safe_free(pCounterSignerInfo);
|
|
|
|
if (pCertContext != NULL)
|
|
|
|
CertFreeCertificateContext(pCertContext);
|
|
|
|
if (hStore != NULL)
|
|
|
|
CertCloseStore(hStore, 0);
|
|
|
|
if (hMsg != NULL)
|
|
|
|
CryptMsgClose(hMsg);
|
2015-10-13 22:29:30 +00:00
|
|
|
return p;
|
2015-10-12 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// From https://msdn.microsoft.com/en-us/library/windows/desktop/aa382384.aspx
|
|
|
|
LONG ValidateSignature(HWND hDlg, const char* path)
|
|
|
|
{
|
|
|
|
LONG r;
|
|
|
|
WINTRUST_DATA trust_data = { 0 };
|
|
|
|
WINTRUST_FILE_INFO trust_file = { 0 };
|
|
|
|
GUID guid_generic_verify = // WINTRUST_ACTION_GENERIC_VERIFY_V2
|
|
|
|
{ 0xaac56b, 0xcd44, 0x11d0,{ 0x8c, 0xc2, 0x0, 0xc0, 0x4f, 0xc2, 0x95, 0xee } };
|
2015-10-13 22:29:30 +00:00
|
|
|
char *signature_name;
|
2015-10-15 22:51:06 +00:00
|
|
|
size_t i, len;
|
2015-10-13 22:29:30 +00:00
|
|
|
|
|
|
|
// Check the signature name. Make it specific enough (i.e. don't simply check for "Akeo")
|
|
|
|
// so that, besides hacking our server, it'll place an extra hurdle on any malicious entity
|
2015-10-18 20:31:47 +00:00
|
|
|
// into also fooling a C.A. to issue a certificate that passes our test.
|
2015-10-13 22:29:30 +00:00
|
|
|
signature_name = GetSignatureName(path);
|
|
|
|
if (signature_name == NULL) {
|
|
|
|
uprintf("PKI: Could not get signature name");
|
2015-10-18 20:31:47 +00:00
|
|
|
MessageBoxExU(hDlg, lmprintf(MSG_284), lmprintf(MSG_283), MB_OK | MB_ICONERROR | MB_IS_RTL, selected_langid);
|
2015-10-13 22:29:30 +00:00
|
|
|
return TRUST_E_NOSIGNATURE;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ARRAYSIZE(valid_cert_names); i++) {
|
2015-10-15 22:51:06 +00:00
|
|
|
len = strlen(valid_cert_names[i]);
|
|
|
|
if (strncmp(signature_name, valid_cert_names[i], len) == 0) {
|
|
|
|
// Test for whitespace after the part we match, for added safety
|
|
|
|
if ((len >= strlen(signature_name)) || isspace(signature_name[len]))
|
|
|
|
break;
|
|
|
|
}
|
2015-10-13 22:29:30 +00:00
|
|
|
}
|
|
|
|
if (i >= ARRAYSIZE(valid_cert_names)) {
|
2015-10-15 22:51:06 +00:00
|
|
|
uprintf("PKI: Signature '%s' is unexpected...", signature_name);
|
2015-10-18 20:31:47 +00:00
|
|
|
if (MessageBoxExU(hDlg, lmprintf(MSG_285, signature_name), lmprintf(MSG_283),
|
|
|
|
MB_YESNO | MB_ICONWARNING | MB_IS_RTL, selected_langid) != IDYES)
|
2015-10-13 22:29:30 +00:00
|
|
|
return TRUST_E_EXPLICIT_DISTRUST;
|
|
|
|
}
|
2015-10-12 21:03:41 +00:00
|
|
|
|
|
|
|
trust_file.cbStruct = sizeof(trust_file);
|
2015-10-13 22:29:30 +00:00
|
|
|
trust_file.pcwszFilePath = utf8_to_wchar(path);
|
|
|
|
if (trust_file.pcwszFilePath == NULL) {
|
|
|
|
uprintf("PKI: Unable to convert '%s' to UTF16", path);
|
|
|
|
return ERROR_SEVERITY_ERROR | FAC(FACILITY_CERT) | ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
}
|
2015-10-12 21:03:41 +00:00
|
|
|
|
|
|
|
trust_data.cbStruct = sizeof(trust_data);
|
|
|
|
trust_data.dwUIChoice = WTD_UI_ALL;
|
2015-10-13 22:29:30 +00:00
|
|
|
// We just downloaded from the Internet, so we should be able to check revocation
|
2015-10-12 21:03:41 +00:00
|
|
|
trust_data.fdwRevocationChecks = WTD_REVOKE_WHOLECHAIN;
|
|
|
|
// 0x400 = WTD_MOTW for Windows 8.1 or later
|
|
|
|
trust_data.dwProvFlags = WTD_REVOCATION_CHECK_CHAIN | 0x400;
|
2015-10-13 22:29:30 +00:00
|
|
|
trust_data.dwUnionChoice = WTD_CHOICE_FILE;
|
|
|
|
trust_data.pFile = &trust_file;
|
2015-10-12 21:03:41 +00:00
|
|
|
|
|
|
|
r = WinVerifyTrust(NULL, &guid_generic_verify, &trust_data);
|
2015-10-13 22:29:30 +00:00
|
|
|
safe_free(trust_file.pcwszFilePath);
|
|
|
|
|
2015-10-12 21:03:41 +00:00
|
|
|
return r;
|
|
|
|
}
|