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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "Authenticode.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/DynamicallyLinkedFunctionPtr.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/UniquePtr.h"
#include "nsWindowsHelpers.h"
#include <windows.h>
#include <softpub.h>
#include <wincrypt.h>
#include <wintrust.h>
#include <mscat.h>
#include <string.h>
// mingw build doesn't have this defined (in other builds
// it gets pulled in from wintrust.h)
#if !defined(szOID_NESTED_SIGNATURE)
# define szOID_NESTED_SIGNATURE "1.3.6.1.4.1.311.2.4.1"
#endif // !defined(szOID_NESTED_SIGNATURE)
namespace {
struct CertStoreDeleter {
typedef HCERTSTORE pointer;
void operator()(pointer aStore) { ::CertCloseStore(aStore, 0); }
};
struct CryptMsgDeleter {
typedef HCRYPTMSG pointer;
void operator()(pointer aMsg) { ::CryptMsgClose(aMsg); }
};
struct CertContextDeleter {
void operator()(PCCERT_CONTEXT aCertContext) {
::CertFreeCertificateContext(aCertContext);
}
};
struct CATAdminContextDeleter {
typedef HCATADMIN pointer;
void operator()(pointer aCtx) {
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATAdminReleaseContext)>
pCryptCATAdminReleaseContext(L"wintrust.dll",
"CryptCATAdminReleaseContext");
MOZ_ASSERT(!!pCryptCATAdminReleaseContext);
if (!pCryptCATAdminReleaseContext) {
return;
}
pCryptCATAdminReleaseContext(aCtx, 0);
}
};
typedef mozilla::UniquePtr<HCERTSTORE, CertStoreDeleter> CertStoreUniquePtr;
typedef mozilla::UniquePtr<HCRYPTMSG, CryptMsgDeleter> CryptMsgUniquePtr;
typedef mozilla::UniquePtr<const CERT_CONTEXT, CertContextDeleter>
CertContextUniquePtr;
typedef mozilla::UniquePtr<HCATADMIN, CATAdminContextDeleter>
CATAdminContextUniquePtr;
static const DWORD kEncodingTypes = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
class SignedBinary final {
public:
SignedBinary(const wchar_t* aFilePath, mozilla::AuthenticodeFlags aFlags);
explicit operator bool() const { return mCertStore && mCryptMsg && mCertCtx; }
bool HasNestedMicrosoftSignature() const;
mozilla::UniquePtr<wchar_t[]> GetOrgName();
SignedBinary(const SignedBinary&) = delete;
SignedBinary(SignedBinary&&) = delete;
SignedBinary& operator=(const SignedBinary&) = delete;
SignedBinary& operator=(SignedBinary&&) = delete;
private:
bool VerifySignature(const wchar_t* aFilePath);
bool QueryObject(const wchar_t* aFilePath);
static bool VerifySignatureInternal(WINTRUST_DATA& aTrustData);
private:
enum class TrustSource { eNone, eEmbedded, eCatalog };
private:
const mozilla::AuthenticodeFlags mFlags;
TrustSource mTrustSource;
CertStoreUniquePtr mCertStore;
CryptMsgUniquePtr mCryptMsg;
CertContextUniquePtr mCertCtx;
};
static CertContextUniquePtr GetCertificateFromCryptMsg(
const CryptMsgUniquePtr& aCryptMsg, const CertStoreUniquePtr& aCertStore) {
DWORD certInfoLen = 0;
BOOL ok = CryptMsgGetParam(aCryptMsg.get(), CMSG_SIGNER_CERT_INFO_PARAM, 0,
nullptr, &certInfoLen);
if (!ok) {
return nullptr;
}
auto certInfoBuf = mozilla::MakeUnique<char[]>(certInfoLen);
ok = CryptMsgGetParam(aCryptMsg.get(), CMSG_SIGNER_CERT_INFO_PARAM, 0,
certInfoBuf.get(), &certInfoLen);
if (!ok) {
return nullptr;
}
auto certInfo = reinterpret_cast<CERT_INFO*>(certInfoBuf.get());
PCCERT_CONTEXT certCtx =
CertFindCertificateInStore(aCertStore.get(), kEncodingTypes, 0,
CERT_FIND_SUBJECT_CERT, certInfo, nullptr);
return CertContextUniquePtr(certCtx);
}
static mozilla::UniquePtr<wchar_t[]> GetSignerOrganizationFromCertificate(
const CertContextUniquePtr& certCtx) {
DWORD charCount = CertGetNameStringW(
certCtx.get(), CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr, nullptr, 0);
if (charCount <= 1) {
// Not found
return nullptr;
}
auto result = mozilla::MakeUnique<wchar_t[]>(charCount);
charCount = CertGetNameStringW(certCtx.get(), CERT_NAME_SIMPLE_DISPLAY_TYPE,
0, nullptr, result.get(), charCount);
MOZ_ASSERT(charCount > 1);
return result;
}
SignedBinary::SignedBinary(const wchar_t* aFilePath,
mozilla::AuthenticodeFlags aFlags)
: mFlags(aFlags), mTrustSource(TrustSource::eNone) {
if (!VerifySignature(aFilePath)) {
return;
}
CertContextUniquePtr certCtx =
GetCertificateFromCryptMsg(mCryptMsg, mCertStore);
if (!certCtx) {
return;
}
mCertCtx.swap(certCtx);
}
bool SignedBinary::HasNestedMicrosoftSignature() const {
// Look for nested certificates - see bug 1817026
DWORD attributesLen = 0;
BOOL ok = CryptMsgGetParam(mCryptMsg.get(), CMSG_SIGNER_UNAUTH_ATTR_PARAM, 0,
nullptr, &attributesLen);
if (!ok) {
return false;
}
auto attributesBuf = mozilla::MakeUnique<char[]>(attributesLen);
ok = CryptMsgGetParam(mCryptMsg.get(), CMSG_SIGNER_UNAUTH_ATTR_PARAM, 0,
attributesBuf.get(), &attributesLen);
if (!ok) {
return false;
}
auto attributesInfo =
reinterpret_cast<CRYPT_ATTRIBUTES*>(attributesBuf.get());
for (DWORD i = 0; i < attributesInfo->cAttr; ++i) {
PCRYPT_ATTRIBUTE cur = &attributesInfo->rgAttr[i];
if (!strcmp(cur->pszObjId, szOID_NESTED_SIGNATURE)) {
CryptMsgUniquePtr nestedMsg(
CryptMsgOpenToDecode(kEncodingTypes, 0, 0, NULL, NULL, NULL));
if (!nestedMsg) {
continue;
}
ok = CryptMsgUpdate(nestedMsg.get(), cur->rgValue->pbData,
cur->rgValue->cbData, TRUE);
if (!ok) {
continue;
}
CertStoreUniquePtr nestedCertStore(
CertOpenStore(CERT_STORE_PROV_MSG, kEncodingTypes, NULL,
CERT_STORE_OPEN_EXISTING_FLAG, nestedMsg.get()));
if (!nestedCertStore) {
continue;
}
CertContextUniquePtr nestedCertCtx =
GetCertificateFromCryptMsg(nestedMsg, nestedCertStore);
if (!nestedCertCtx) {
continue;
}
mozilla::UniquePtr<wchar_t[]> nestedSigner =
GetSignerOrganizationFromCertificate(nestedCertCtx);
if (!nestedSigner) {
continue;
}
if (!wcscmp(nestedSigner.get(), L"Microsoft Windows") ||
!wcscmp(nestedSigner.get(), L"Microsoft Corporation")) {
return true;
}
}
}
return false;
}
bool SignedBinary::QueryObject(const wchar_t* aFilePath) {
DWORD encodingType, contentType, formatType;
HCERTSTORE rawCertStore;
HCRYPTMSG rawCryptMsg;
BOOL result = ::CryptQueryObject(CERT_QUERY_OBJECT_FILE, aFilePath,
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
CERT_QUERY_FORMAT_FLAG_BINARY, 0,
&encodingType, &contentType, &formatType,
&rawCertStore, &rawCryptMsg, nullptr);
if (!result) {
return false;
}
mCertStore.reset(rawCertStore);
mCryptMsg.reset(rawCryptMsg);
return true;
}
/**
* @param aTrustData must be a WINTRUST_DATA structure that has been zeroed out
* and then populated at least with its |cbStruct|,
* |dwUnionChoice|, and appropriate union field. This function
* will then populate the remaining fields as appropriate.
*/
/* static */
bool SignedBinary::VerifySignatureInternal(WINTRUST_DATA& aTrustData) {
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::WinVerifyTrust)>
pWinVerifyTrust(L"wintrust.dll", "WinVerifyTrust");
if (!pWinVerifyTrust) {
return false;
}
aTrustData.dwUIChoice = WTD_UI_NONE;
aTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
aTrustData.dwStateAction = WTD_STATEACTION_VERIFY;
aTrustData.dwProvFlags = WTD_CACHE_ONLY_URL_RETRIEVAL;
const HWND hwnd = (HWND)INVALID_HANDLE_VALUE;
GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
LONG result = pWinVerifyTrust(hwnd, &policyGUID, &aTrustData);
aTrustData.dwStateAction = WTD_STATEACTION_CLOSE;
pWinVerifyTrust(hwnd, &policyGUID, &aTrustData);
return result == ERROR_SUCCESS;
}
bool SignedBinary::VerifySignature(const wchar_t* aFilePath) {
// First, try the binary itself
if (QueryObject(aFilePath)) {
mTrustSource = TrustSource::eEmbedded;
if (mFlags & mozilla::AuthenticodeFlags::SkipTrustVerification) {
return true;
}
WINTRUST_FILE_INFO fileInfo = {sizeof(fileInfo)};
fileInfo.pcwszFilePath = aFilePath;
WINTRUST_DATA trustData = {sizeof(trustData)};
trustData.dwUnionChoice = WTD_CHOICE_FILE;
trustData.pFile = &fileInfo;
return VerifySignatureInternal(trustData);
}
// We didn't find anything in the binary, so now try a catalog file.
// First, we open a catalog admin context.
HCATADMIN rawCatAdmin;
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATAdminAcquireContext2)>
pCryptCATAdminAcquireContext2(L"wintrust.dll",
"CryptCATAdminAcquireContext2");
if (!pCryptCATAdminAcquireContext2) {
return false;
}
CERT_STRONG_SIGN_PARA policy = {sizeof(policy)};
policy.dwInfoChoice = CERT_STRONG_SIGN_OID_INFO_CHOICE;
policy.pszOID = const_cast<char*>(
szOID_CERT_STRONG_SIGN_OS_CURRENT); // -Wwritable-strings
if (!pCryptCATAdminAcquireContext2(&rawCatAdmin, nullptr,
BCRYPT_SHA256_ALGORITHM, &policy, 0)) {
return false;
}
CATAdminContextUniquePtr catAdmin(rawCatAdmin);
// Now we need to hash the file at aFilePath.
// Since we're hashing this file, let's open it with a sequential scan hint.
HANDLE rawFile =
::CreateFileW(aFilePath, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
if (rawFile == INVALID_HANDLE_VALUE) {
return false;
}
nsAutoHandle file(rawFile);
DWORD hashLen = 0;
mozilla::UniquePtr<BYTE[]> hashBuf;
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATAdminCalcHashFromFileHandle2)>
pCryptCATAdminCalcHashFromFileHandle2(
L"wintrust.dll", "CryptCATAdminCalcHashFromFileHandle2");
if (pCryptCATAdminCalcHashFromFileHandle2) {
if (!pCryptCATAdminCalcHashFromFileHandle2(rawCatAdmin, rawFile, &hashLen,
nullptr, 0) &&
::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return false;
}
hashBuf = mozilla::MakeUnique<BYTE[]>(hashLen);
if (!pCryptCATAdminCalcHashFromFileHandle2(rawCatAdmin, rawFile, &hashLen,
hashBuf.get(), 0)) {
return false;
}
} else {
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATAdminCalcHashFromFileHandle)>
pCryptCATAdminCalcHashFromFileHandle(
L"wintrust.dll", "CryptCATAdminCalcHashFromFileHandle");
if (!pCryptCATAdminCalcHashFromFileHandle) {
return false;
}
if (!pCryptCATAdminCalcHashFromFileHandle(rawFile, &hashLen, nullptr, 0) &&
::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return false;
}
hashBuf = mozilla::MakeUnique<BYTE[]>(hashLen);
if (!pCryptCATAdminCalcHashFromFileHandle(rawFile, &hashLen, hashBuf.get(),
0)) {
return false;
}
}
// Now that we've hashed the file, query the catalog system to see if any
// catalogs reference a binary with our hash.
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATAdminEnumCatalogFromHash)>
pCryptCATAdminEnumCatalogFromHash(L"wintrust.dll",
"CryptCATAdminEnumCatalogFromHash");
if (!pCryptCATAdminEnumCatalogFromHash) {
return false;
}
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATAdminReleaseCatalogContext)>
pCryptCATAdminReleaseCatalogContext(L"wintrust.dll",
"CryptCATAdminReleaseCatalogContext");
if (!pCryptCATAdminReleaseCatalogContext) {
return false;
}
HCATINFO catInfoHdl = pCryptCATAdminEnumCatalogFromHash(
rawCatAdmin, hashBuf.get(), hashLen, 0, nullptr);
if (!catInfoHdl) {
return false;
}
// We can't use UniquePtr for this because the deleter function requires two
// parameters.
auto cleanCatInfoHdl =
mozilla::MakeScopeExit([rawCatAdmin, catInfoHdl]() -> void {
pCryptCATAdminReleaseCatalogContext(rawCatAdmin, catInfoHdl, 0);
});
// We found a catalog! Now query for the path to the catalog file.
static const mozilla::StaticDynamicallyLinkedFunctionPtr<
decltype(&::CryptCATCatalogInfoFromContext)>
pCryptCATCatalogInfoFromContext(L"wintrust.dll",
"CryptCATCatalogInfoFromContext");
if (!pCryptCATCatalogInfoFromContext) {
return false;
}
CATALOG_INFO_ catInfo = {sizeof(catInfo)};
if (!pCryptCATCatalogInfoFromContext(catInfoHdl, &catInfo, 0)) {
return false;
}
if (!QueryObject(catInfo.wszCatalogFile)) {
return false;
}
mTrustSource = TrustSource::eCatalog;
if (mFlags & mozilla::AuthenticodeFlags::SkipTrustVerification) {
return true;
}
// WINTRUST_CATALOG_INFO::pcwszMemberTag is commonly set to the string
// representation of the file hash, so we build that here.
DWORD strHashBufLen = (hashLen * 2) + 1;
auto strHashBuf = mozilla::MakeUnique<wchar_t[]>(strHashBufLen);
if (!::CryptBinaryToStringW(hashBuf.get(), hashLen,
CRYPT_STRING_HEXRAW | CRYPT_STRING_NOCRLF,
strHashBuf.get(), &strHashBufLen)) {
return false;
}
// Ensure that the tag is uppercase for WinVerifyTrust
// NB: CryptBinaryToStringW overwrites strHashBufLen with the length excluding
// the null terminator, so we need to add it back for this call.
if (_wcsupr_s(strHashBuf.get(), strHashBufLen + 1)) {
return false;
}
// Now, given the path to the catalog, and the path to the member (ie, the
// binary whose hash we are validating), we may now validate. If the
// validation is successful, we then QueryObject on the *catalog file*
// instead of the binary.
WINTRUST_CATALOG_INFO wtCatInfo = {sizeof(wtCatInfo)};
wtCatInfo.pcwszCatalogFilePath = catInfo.wszCatalogFile;
wtCatInfo.pcwszMemberTag = strHashBuf.get();
wtCatInfo.pcwszMemberFilePath = aFilePath;
wtCatInfo.hMemberFile = rawFile;
wtCatInfo.hCatAdmin = rawCatAdmin;
WINTRUST_DATA trustData = {sizeof(trustData)};
trustData.dwUnionChoice = WTD_CHOICE_CATALOG;
trustData.pCatalog = &wtCatInfo;
return VerifySignatureInternal(trustData);
}
mozilla::UniquePtr<wchar_t[]> SignedBinary::GetOrgName() {
return GetSignerOrganizationFromCertificate(mCertCtx);
}
} // anonymous namespace
namespace mozilla {
class AuthenticodeImpl : public Authenticode {
public:
virtual UniquePtr<wchar_t[]> GetBinaryOrgName(
const wchar_t* aFilePath, bool* aHasNestedMicrosoftSignature = nullptr,
AuthenticodeFlags aFlags = AuthenticodeFlags::Default) override;
};
UniquePtr<wchar_t[]> AuthenticodeImpl::GetBinaryOrgName(
const wchar_t* aFilePath, bool* aHasNestedMicrosoftSignature,
AuthenticodeFlags aFlags) {
if (aHasNestedMicrosoftSignature) {
*aHasNestedMicrosoftSignature = false;
}
SignedBinary bin(aFilePath, aFlags);
if (!bin) {
return nullptr;
}
if (aHasNestedMicrosoftSignature) {
*aHasNestedMicrosoftSignature = bin.HasNestedMicrosoftSignature();
}
return bin.GetOrgName();
}
static AuthenticodeImpl sAuthenticodeImpl;
Authenticode* GetAuthenticode() { return &sAuthenticodeImpl; }
} // namespace mozilla
|