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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/os_crypt/async/browser/dpapi_key_provider.h"
#include <windows.h>
#include <wincrypt.h>
#include "base/base64.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/types/expected.h"
#include "base/win/scoped_localalloc.h"
#include "components/os_crypt/async/common/algorithm.mojom.h"
#include "components/prefs/pref_service.h"
namespace os_crypt_async {
namespace {
// Legacy (OSCrypt) random key encrypted with DPAPI imported by this code.
// This should match the pref name defined in os_crypt_win.cc until sync is
// deprecated and the pref registration can be moved here.
constexpr char kOsCryptEncryptedKeyPrefName[] = "os_crypt.encrypted_key";
// Data prefix for data encrypted with DPAPI. This must match
// kEncryptionVersionPrefix in os_crypt_win.cc to ensure data is compatible.
constexpr char kKeyTag[] = "v10";
// Key prefix for a key encrypted with DPAPI. This must match kDPAPIKeyPrefix in
// os_crypt_win.cc to ensure the same key can be decrypted successfully.
constexpr uint8_t kDPAPIKeyPrefix[] = {'D', 'P', 'A', 'P', 'I'};
std::optional<std::vector<uint8_t>> DecryptKeyWithDPAPI(
base::span<const uint8_t> ciphertext) {
DATA_BLOB input = {};
input.pbData = const_cast<BYTE*>(ciphertext.data());
input.cbData = static_cast<DWORD>(ciphertext.size());
BOOL result = FALSE;
DATA_BLOB output;
result = ::CryptUnprotectData(&input, nullptr, nullptr, nullptr, nullptr, 0,
&output);
if (!result) {
return std::nullopt;
}
auto local_alloc = base::win::TakeLocalAlloc(output.pbData);
return std::vector<uint8_t>(local_alloc.get(),
local_alloc.get() + output.cbData);
}
} // namespace
DPAPIKeyProvider::DPAPIKeyProvider(PrefService* local_state)
: local_state_(local_state) {}
DPAPIKeyProvider::~DPAPIKeyProvider() = default;
base::expected<Encryptor::Key, DPAPIKeyProvider::KeyStatus>
DPAPIKeyProvider::GetKeyInternal() {
if (!local_state_->HasPrefPath(kOsCryptEncryptedKeyPrefName)) {
return base::unexpected(KeyStatus::kKeyNotFound);
}
const std::string base64_encrypted_key =
local_state_->GetString(kOsCryptEncryptedKeyPrefName);
std::optional<std::vector<uint8_t>> decoded =
base::Base64Decode(base64_encrypted_key);
if (!decoded) {
return base::unexpected(KeyStatus::kKeyDecodeFailure);
}
if (decoded->size() < std::size(kDPAPIKeyPrefix)) {
return base::unexpected(KeyStatus::kKeyTooShort);
}
if (!std::equal(decoded->begin(),
decoded->begin() + std::size(kDPAPIKeyPrefix),
kDPAPIKeyPrefix)) {
return base::unexpected(KeyStatus::kInvalidKeyHeader);
}
auto encrypted_key_data = std::vector<uint8_t>(
decoded->cbegin() + std::size(kDPAPIKeyPrefix), decoded->cend());
auto decrypted_key = DecryptKeyWithDPAPI(encrypted_key_data);
if (!decrypted_key) {
return base::unexpected(KeyStatus::kDPAPIDecryptFailure);
}
if (decrypted_key->size() != Encryptor::Key::kAES256GCMKeySize) {
return base::unexpected(KeyStatus::kInvalidKeyLength);
}
return Encryptor::Key(*decrypted_key, mojom::Algorithm::kAES256GCM);
}
void DPAPIKeyProvider::GetKey(KeyCallback callback) {
auto result = GetKeyInternal();
base::UmaHistogramEnumeration("OSCrypt.DPAPIProvider.Status",
result.error_or(KeyStatus::kSuccess));
if (result.has_value()) {
std::move(callback).Run(kKeyTag, std::move(result.value()));
return;
}
KeyError status;
switch (result.error()) {
case KeyStatus::kSuccess:
NOTREACHED();
case KeyStatus::kKeyNotFound:
status = KeyError::kPermanentlyUnavailable;
break;
case KeyStatus::kKeyDecodeFailure:
status = KeyError::kPermanentlyUnavailable;
break;
case KeyStatus::kKeyTooShort:
status = KeyError::kPermanentlyUnavailable;
break;
case KeyStatus::kInvalidKeyHeader:
status = KeyError::kPermanentlyUnavailable;
break;
case KeyStatus::kDPAPIDecryptFailure:
status = KeyError::kTemporarilyUnavailable;
break;
case KeyStatus::kInvalidKeyLength:
status = KeyError::kPermanentlyUnavailable;
break;
}
std::move(callback).Run(kKeyTag, base::unexpected(status));
}
bool DPAPIKeyProvider::UseForEncryption() {
return true;
}
bool DPAPIKeyProvider::IsCompatibleWithOsCryptSync() {
return true;
}
} // namespace os_crypt_async
|