1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <softpub.h>
#include <wintrust.h>

#include "certificatecheck.hxx"
#include "servicebase.hxx"

#pragma comment(lib, "wintrust.lib")
#pragma comment(lib, "crypt32.lib")

static const int ENCODING = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;

/**
 * Checks to see if a file stored at filePath matches the specified info.
 *
 * @param  filePath    The PE file path to check
 * @param  infoToMatch The acceptable information to match
 * @return ERROR_SUCCESS if successful, ERROR_NOT_FOUND if the info
 *         does not match, or the last error otherwise.
 */
DWORD
CheckCertificateForPEFile(LPCWSTR filePath,
                          CertificateCheckInfo &infoToMatch)
{
    HCERTSTORE certStore = nullptr;
    HCRYPTMSG cryptMsg = nullptr;
    PCCERT_CONTEXT certContext = nullptr;
    PCMSG_SIGNER_INFO signerInfo = nullptr;
    DWORD lastError = ERROR_SUCCESS;

    // Get the HCERTSTORE and HCRYPTMSG from the signed file.
    DWORD encoding, contentType, formatType;
    BOOL result = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
                                   filePath,
                                   CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
                                   CERT_QUERY_CONTENT_FLAG_ALL,
                                   0, &encoding, &contentType,
                                   &formatType, &certStore, &cryptMsg, nullptr);
    if (!result)
    {
        lastError = GetLastError();
        LOG_WARN(("CryptQueryObject failed.  (%d)", lastError));
        goto cleanup;
    }

    // Pass in nullptr to get the needed signer information size.
    DWORD signerInfoSize;
    result = CryptMsgGetParam(cryptMsg, CMSG_SIGNER_INFO_PARAM, 0,
                              nullptr, &signerInfoSize);
    if (!result)
    {
        lastError = GetLastError();
        LOG_WARN(("CryptMsgGetParam failed.  (%d)", lastError));
        goto cleanup;
    }

    // Allocate the needed size for the signer information.
    signerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, signerInfoSize);
    if (!signerInfo)
    {
        lastError = GetLastError();
        LOG_WARN(("Unable to allocate memory for Signer Info.  (%d)", lastError));
        goto cleanup;
    }

    // Get the signer information (PCMSG_SIGNER_INFO).
    // In particular we want the issuer and serial number.
    result = CryptMsgGetParam(cryptMsg, CMSG_SIGNER_INFO_PARAM, 0,
                              (PVOID)signerInfo, &signerInfoSize);
    if (!result)
    {
        lastError = GetLastError();
        LOG_WARN(("CryptMsgGetParam failed.  (%d)", lastError));
        goto cleanup;
    }

    // Search for the signer certificate in the certificate store.
    CERT_INFO certInfo;
    certInfo.Issuer = signerInfo->Issuer;
    certInfo.SerialNumber = signerInfo->SerialNumber;
    certContext = CertFindCertificateInStore(certStore, ENCODING, 0,
                  CERT_FIND_SUBJECT_CERT,
                  (PVOID)&certInfo, nullptr);
    if (!certContext)
    {
        lastError = GetLastError();
        LOG_WARN(("CertFindCertificateInStore failed.  (%d)", lastError));
        goto cleanup;
    }

    if (!DoCertificateAttributesMatch(certContext, infoToMatch))
    {
        lastError = ERROR_NOT_FOUND;
        LOG_WARN(("Certificate did not match issuer or name.  (%d)", lastError));
        goto cleanup;
    }

cleanup:
    if (signerInfo)
    {
        LocalFree(signerInfo);
    }
    if (certContext)
    {
        CertFreeCertificateContext(certContext);
    }
    if (certStore)
    {
        CertCloseStore(certStore, 0);
    }
    if (cryptMsg)
    {
        CryptMsgClose(cryptMsg);
    }
    return lastError;
}

/**
 * Checks to see if a file stored at filePath matches the specified info.
 *
 * @param  certContext  The certificate context of the file
 * @param  infoToMatch  The acceptable information to match
 * @return FALSE if the info does not match or if any error occurs in the check
 */
BOOL
DoCertificateAttributesMatch(PCCERT_CONTEXT certContext,
                             CertificateCheckInfo &infoToMatch)<--- Parameter 'infoToMatch' can be declared with const
{
    DWORD dwData;
    LPTSTR szName = nullptr;<--- Shadowed declaration

    if (infoToMatch.issuer)
    {
        // Pass in nullptr to get the needed size of the issuer buffer.
        dwData = CertGetNameString(certContext,
                                   CERT_NAME_SIMPLE_DISPLAY_TYPE,
                                   CERT_NAME_ISSUER_FLAG, nullptr,
                                   nullptr, 0);

        if (!dwData)
        {
            LOG_WARN(("CertGetNameString failed.  (%d)", GetLastError()));
            return FALSE;
        }

        // Allocate memory for Issuer name buffer.
        LPTSTR szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(WCHAR));<--- Shadow variable
        if (!szName)
        {
            LOG_WARN(("Unable to allocate memory for issuer name.  (%d)",
                      GetLastError()));
            return FALSE;
        }

        // Get Issuer name.
        if (!CertGetNameString(certContext, CERT_NAME_SIMPLE_DISPLAY_TYPE,
                               CERT_NAME_ISSUER_FLAG, nullptr, szName, dwData))
        {
            LOG_WARN(("CertGetNameString failed.  (%d)", GetLastError()));
            LocalFree(szName);
            return FALSE;
        }

        // If the issuer does not match, return a failure.
        if (!infoToMatch.issuer ||
                wcscmp(szName, infoToMatch.issuer))
        {
            LocalFree(szName);
            return FALSE;
        }

        LocalFree(szName);
        szName = nullptr;
    }

    if (infoToMatch.name)
    {
        // Pass in nullptr to get the needed size of the name buffer.
        dwData = CertGetNameString(certContext, CERT_NAME_SIMPLE_DISPLAY_TYPE,
                                   0, nullptr, nullptr, 0);
        if (!dwData)
        {
            LOG_WARN(("CertGetNameString failed.  (%d)", GetLastError()));
            return FALSE;
        }

        // Allocate memory for the name buffer.
        szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(WCHAR));
        if (!szName)
        {
            LOG_WARN(("Unable to allocate memory for subject name.  (%d)",
                      GetLastError()));
            return FALSE;
        }

        // Obtain the name.
        if (!(CertGetNameString(certContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
                                nullptr, szName, dwData)))
        {
            LOG_WARN(("CertGetNameString failed.  (%d)", GetLastError()));
            LocalFree(szName);
            return FALSE;
        }

        // If the issuer does not match, return a failure.
        if (!infoToMatch.name ||
                wcscmp(szName, infoToMatch.name))
        {
            LocalFree(szName);
            return FALSE;
        }

        // We have a match!
        LocalFree(szName);
    }

    // If there were any errors we would have aborted by now.
    return TRUE;
}

/**
 * Duplicates the specified string
 *
 * @param  inputString The string to duplicate
 * @return The duplicated string which should be freed by the caller.
 */
LPWSTR
AllocateAndCopyWideString(LPCWSTR inputString)<--- The function 'AllocateAndCopyWideString' is never used.
{
    LPWSTR outputString =
        (LPWSTR)LocalAlloc(LPTR, (wcslen(inputString) + 1) * sizeof(WCHAR));
    if (outputString)
    {
        lstrcpyW(outputString, inputString);
    }
    return outputString;
}

/**
 * Verifies the trust of the specified file path.
 *
 * @param  filePath  The file path to check.
 * @return ERROR_SUCCESS if successful, or the last error code otherwise.
 */
DWORD
VerifyCertificateTrustForFile(LPCWSTR filePath)
{
    // Setup the file to check.
    WINTRUST_FILE_INFO fileToCheck;
    ZeroMemory(&fileToCheck, sizeof(fileToCheck));
    fileToCheck.cbStruct = sizeof(WINTRUST_FILE_INFO);
    fileToCheck.pcwszFilePath = filePath;

    // Setup what to check, we want to check it is signed and trusted.
    WINTRUST_DATA trustData;
    ZeroMemory(&trustData, sizeof(trustData));
    trustData.cbStruct = sizeof(trustData);
    trustData.pPolicyCallbackData = nullptr;
    trustData.pSIPClientData = nullptr;
    trustData.dwUIChoice = WTD_UI_NONE;
    trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
    trustData.dwUnionChoice = WTD_CHOICE_FILE;
    trustData.dwStateAction = 0;
    trustData.hWVTStateData = nullptr;
    trustData.pwszURLReference = nullptr;
    // no UI
    trustData.dwUIContext = 0;
    trustData.pFile = &fileToCheck;

    GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
    // Check if the file is signed by something that is trusted.
    LONG ret = WinVerifyTrust(nullptr, &policyGUID, &trustData);
    if (ERROR_SUCCESS == ret)
    {
        // The hash that represents the subject is trusted and there were no
        // verification errors.  No publisher nor time stamp chain errors.
        LOG(("The file \"%ls\" is signed and the signature was verified.",
             filePath));
        return ERROR_SUCCESS;
    }

    DWORD lastError = GetLastError();
    LOG_WARN(("There was an error validating trust of the certificate for file"
              " \"%ls\". Returned: %d.  (%d)", filePath, ret, lastError));
    return ret;
}