File: userdataauth_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (211 lines) | stat: -rw-r--r-- 9,325 bytes parent folder | download | duplicates (8)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/cryptohome/userdataauth_util.h"

#include "base/logging.h"
#include "base/notreached.h"
#include "chromeos/ash/components/dbus/cryptohome/UserDataAuth.pb.h"
#include "components/device_event_log/device_event_log.h"

namespace user_data_auth {

namespace {

using ::google::protobuf::RepeatedPtrField;

template <typename ReplyType>
bool IsEmpty(const std::optional<ReplyType>& reply) {
  if (!reply.has_value()) {
    LOGIN_LOG(ERROR) << "Cryptohome call failed with empty reply.";
    return true;
  }
  return false;
}

}  // namespace

template <typename ReplyType>
cryptohome::MountError ReplyToMountError(
    const std::optional<ReplyType>& reply) {
  if (IsEmpty(reply)) {
    return cryptohome::MOUNT_ERROR_FATAL;
  }

  return CryptohomeErrorToMountError(reply->error());
}

template <typename ReplyType>
cryptohome::ErrorWrapper ReplyToCryptohomeError(
    const std::optional<ReplyType>& reply) {
  if (IsEmpty(reply)) {
    return cryptohome::ErrorWrapper::CreateFromErrorCodeOnly(
        CRYPTOHOME_ERROR_MOUNT_FATAL);
  }
  return cryptohome::ErrorWrapper::CreateFrom(reply->error(),
                                              reply->error_info());
}

// Instantiate ReplyToMountError and export them for types actually used.
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::MountError ReplyToMountError(const std::optional<RemoveReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::MountError
    ReplyToMountError(const std::optional<UnmountReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<StartAuthSessionReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<AuthenticateAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<AddAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<UnmountReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<RemoveReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<CreatePersistentUserReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<PrepareGuestVaultReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<PrepareEphemeralVaultReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<PreparePersistentVaultReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<PrepareVaultForMigrationReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<ListAuthFactorsReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<RemoveAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<UpdateAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<UpdateAuthFactorMetadataReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<ReplaceAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<GetAuthSessionStatusReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<ExtendAuthSessionReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<InvalidateAuthSessionReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<PrepareAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<TerminateAuthFactorReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<StartMigrateToDircryptoReply>&);
template COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    cryptohome::ErrorWrapper
    ReplyToCryptohomeError(const std::optional<LockFactorUntilRebootReply>&);

int64_t AccountDiskUsageReplyToUsageSize(
    const std::optional<GetAccountDiskUsageReply>& reply) {
  if (IsEmpty(reply)) {
    return -1;
  }

  if (reply->error() != CRYPTOHOME_ERROR_NOT_SET) {
    LOGIN_LOG(ERROR) << "GetAccountDiskUsage failed with error: "
                     << reply->error();
    return -1;
  }

  return reply->size();
}

// TODO(crbug.com/40556176): Finish testing this method.
cryptohome::MountError CryptohomeErrorToMountError(CryptohomeErrorCode code) {
  switch (code) {
    case CRYPTOHOME_ERROR_NOT_SET:
      return cryptohome::MOUNT_ERROR_NONE;
    case CRYPTOHOME_ERROR_ACCOUNT_NOT_FOUND:
      return cryptohome::MOUNT_ERROR_USER_DOES_NOT_EXIST;
    case CRYPTOHOME_ERROR_NOT_IMPLEMENTED:
    case CRYPTOHOME_ERROR_MOUNT_FATAL:
    case CRYPTOHOME_ERROR_KEY_QUOTA_EXCEEDED:
    case CRYPTOHOME_ERROR_BACKING_STORE_FAILURE:
    case CRYPTOHOME_ERROR_INSTALL_ATTRIBUTES_FINALIZE_FAILED:
    case CRYPTOHOME_ERROR_INSTALL_ATTRIBUTES_GET_FAILED:
    case CRYPTOHOME_ERROR_INSTALL_ATTRIBUTES_SET_FAILED:
    case CRYPTOHOME_ERROR_INVALID_ARGUMENT:
    case CRYPTOHOME_ERROR_FINGERPRINT_ERROR_INTERNAL:
    case CRYPTOHOME_ERROR_FINGERPRINT_RETRY_REQUIRED:
    case CRYPTOHOME_ERROR_FINGERPRINT_DENIED:
      return cryptohome::MOUNT_ERROR_FATAL;
    case CRYPTOHOME_ERROR_AUTHORIZATION_KEY_NOT_FOUND:
    case CRYPTOHOME_ERROR_KEY_NOT_FOUND:
    case CRYPTOHOME_ERROR_MIGRATE_KEY_FAILED:
    case CRYPTOHOME_ERROR_AUTHORIZATION_KEY_FAILED:
      return cryptohome::MOUNT_ERROR_KEY_FAILURE;
    case CRYPTOHOME_ERROR_TPM_COMM_ERROR:
      return cryptohome::MOUNT_ERROR_TPM_COMM_ERROR;
    case CRYPTOHOME_ERROR_TPM_DEFEND_LOCK:
      return cryptohome::MOUNT_ERROR_TPM_DEFEND_LOCK;
    case CRYPTOHOME_ERROR_MOUNT_MOUNT_POINT_BUSY:
      return cryptohome::MOUNT_ERROR_MOUNT_POINT_BUSY;
    case CRYPTOHOME_ERROR_TPM_NEEDS_REBOOT:
      return cryptohome::MOUNT_ERROR_TPM_NEEDS_REBOOT;
    case CRYPTOHOME_ERROR_AUTHORIZATION_KEY_DENIED:
    case CRYPTOHOME_ERROR_KEY_LABEL_EXISTS:
    case CRYPTOHOME_ERROR_UPDATE_SIGNATURE_INVALID:
      return cryptohome::MOUNT_ERROR_KEY_FAILURE;
    case CRYPTOHOME_ERROR_MOUNT_OLD_ENCRYPTION:
      return cryptohome::MOUNT_ERROR_OLD_ENCRYPTION;
    case CRYPTOHOME_ERROR_MOUNT_PREVIOUS_MIGRATION_INCOMPLETE:
      return cryptohome::MOUNT_ERROR_PREVIOUS_MIGRATION_INCOMPLETE;
    case CRYPTOHOME_ERROR_REMOVE_FAILED:
      return cryptohome::MOUNT_ERROR_REMOVE_FAILED;
    case CRYPTOHOME_ERROR_TPM_UPDATE_REQUIRED:
      return cryptohome::MOUNT_ERROR_TPM_UPDATE_REQUIRED;
    case CRYPTOHOME_ERROR_VAULT_UNRECOVERABLE:
      return cryptohome::MOUNT_ERROR_VAULT_UNRECOVERABLE;
    // TODO(crbug.com/797563): Split the error space and/or handle everything.
    case CRYPTOHOME_ERROR_LOCKBOX_SIGNATURE_INVALID:
    case CRYPTOHOME_ERROR_LOCKBOX_CANNOT_SIGN:
    case CRYPTOHOME_ERROR_BOOT_ATTRIBUTE_NOT_FOUND:
    case CRYPTOHOME_ERROR_BOOT_ATTRIBUTES_CANNOT_SIGN:
    case CRYPTOHOME_ERROR_TPM_EK_NOT_AVAILABLE:
    case CRYPTOHOME_ERROR_ATTESTATION_NOT_READY:
    case CRYPTOHOME_ERROR_CANNOT_CONNECT_TO_CA:
    case CRYPTOHOME_ERROR_CA_REFUSED_ENROLLMENT:
    case CRYPTOHOME_ERROR_CA_REFUSED_CERTIFICATE:
    case CRYPTOHOME_ERROR_INTERNAL_ATTESTATION_ERROR:
    case CRYPTOHOME_ERROR_FIRMWARE_MANAGEMENT_PARAMETERS_INVALID:
    case CRYPTOHOME_ERROR_FIRMWARE_MANAGEMENT_PARAMETERS_CANNOT_STORE:
    case CRYPTOHOME_ERROR_FIRMWARE_MANAGEMENT_PARAMETERS_CANNOT_REMOVE:
    case CRYPTOHOME_ERROR_UPDATE_USER_ACTIVITY_TIMESTAMP_FAILED:
    case CRYPTOHOME_ERROR_FAILED_TO_EXTEND_PCR:
    case CRYPTOHOME_ERROR_FAILED_TO_READ_PCR:
    case CRYPTOHOME_ERROR_PCR_ALREADY_EXTENDED:
      NOTREACHED();
    // TODO(dlunev): remove this temporary case after rolling up system api
    // change and adding proper handling for the new enum value in
    // https://chromium-review.googlesource.com/c/chromium/src/+/2518524
    default:
      NOTREACHED();
  }
}

}  // namespace user_data_auth