File: trust_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,883 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/win/trust_util.h"

#include <windows.h>

#include <softpub.h>

#include "base/logging.h"
#include "base/win/wintrust_shim.h"

namespace remoting {

bool IsBinaryTrusted(const base::FilePath& binary_path) {
#if defined(OFFICIAL_BUILD)
  // This function uses the WinVerifyTrust function to validate the signature
  // for the provided |binary_path|. More information on the structures and
  // function used here can be found at:
  // https://docs.microsoft.com/en-us/windows/win32/api/wintrust/nf-wintrust-winverifytrust

  WINTRUST_FILE_INFO file_info = {0};
  file_info.cbStruct = sizeof(file_info);
  file_info.pcwszFilePath = binary_path.value().c_str();
  file_info.hFile = NULL;
  file_info.pgKnownSubject = NULL;

  WINTRUST_DATA wintrust_data = {0};
  wintrust_data.cbStruct = sizeof(wintrust_data);
  wintrust_data.pPolicyCallbackData = NULL;
  wintrust_data.pSIPClientData = NULL;
  wintrust_data.dwUIChoice = WTD_UI_NONE;
  wintrust_data.fdwRevocationChecks = WTD_REVOKE_NONE;
  wintrust_data.dwUnionChoice = WTD_CHOICE_FILE;
  wintrust_data.dwStateAction = WTD_STATEACTION_VERIFY;
  wintrust_data.hWVTStateData = NULL;
  wintrust_data.pwszURLReference = NULL;
  wintrust_data.dwUIContext = WTD_UICONTEXT_EXECUTE;
  wintrust_data.dwProvFlags = WTD_CACHE_ONLY_URL_RETRIEVAL;
  wintrust_data.pFile = &file_info;

  GUID policy_guid = WINTRUST_ACTION_GENERIC_VERIFY_V2;

  LONG trust_status = WinVerifyTrust(static_cast<HWND>(INVALID_HANDLE_VALUE),
                                     &policy_guid, &wintrust_data);

  DWORD dwLastError;
  switch (trust_status) {
    case ERROR_SUCCESS:
      // Indicates that the binary is trusted:
      //   - The hash that represents the subject is trusted
      //   - The publisher is trusted
      //   - No verification or time stamp chain errors
      LOG(INFO) << "Signature verified for " << binary_path.value();
      return true;

    case TRUST_E_NOSIGNATURE:
      // The file was not signed or had a signature that was not valid.
      // The reason for this status is retrieved via GetLastError(). Note that
      // the last error is a DWORD but the expected values set by this function
      // are HRESULTS so we need to cast.
      dwLastError = GetLastError();
      switch (static_cast<HRESULT>(dwLastError)) {
        case TRUST_E_NOSIGNATURE:
          LOG(ERROR) << "No signature found for " << binary_path.value()
                     << ". ErrorReason: 0x" << std::hex << dwLastError;
          break;
        case TRUST_E_SUBJECT_FORM_UNKNOWN:
          LOG(ERROR) << "The trust provider does not support the form "
                     << "specified for the subject for " << binary_path.value()
                     << ". ErrorReason: 0x" << std::hex << dwLastError;
          break;
        case TRUST_E_PROVIDER_UNKNOWN:
          LOG(ERROR) << "The trust provider is not recognized on this system "
                     << "for " << binary_path.value() << ". ErrorReason: 0x"
                     << std::hex << dwLastError;
          break;
        default:
          // The signature was not valid or there was an error opening the file.
          LOG(ERROR) << "Could not verify signature for " << binary_path.value()
                     << ". ErrorReason: " << dwLastError << ", 0x" << std::hex
                     << dwLastError;
          break;
      }
      return false;

    case TRUST_E_EXPLICIT_DISTRUST:
      // The hash that represents the subject or the publisher is not allowed by
      // the admin or user.
      LOG(ERROR) << "Signature for " << binary_path.value() << " is present, "
                 << "but is explicitly distrusted.";
      return false;

    case TRUST_E_SUBJECT_NOT_TRUSTED:
      LOG(ERROR) << "Signature for " << binary_path.value() << " is present, "
                 << "but not trusted.";
      return false;

    case CRYPT_E_SECURITY_SETTINGS:
      LOG(ERROR) << "Verification failed for " << binary_path.value() << ". "
                 << "The hash representing the subject or the publisher wasn't "
                 << "explicitly trusted by the admin and admin policy has "
                 << "disabled user trust. No signature, publisher or timestamp "
                 << "errors.";
      return false;

    default:
      LOG(ERROR) << "Signature verification error for " << binary_path.value()
                 << ": 0x" << std::hex << trust_status;
      return false;
  }
#else
  // Binaries are only signed in official builds so running the code above for
  // local builds won't work w/o setting up a test certificate and signing the
  // binaries using it. To simplify local development, bypass the signature
  // checks.
  return true;
#endif
}

}  // namespace remoting