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
|
// Copyright 2023 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/auth_factor_conversions.h"
#include <memory>
#include <optional>
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chromeos/ash/components/cryptohome/auth_factor.h"
#include "chromeos/ash/components/cryptohome/common_types.h"
#include "chromeos/ash/components/dbus/cryptohome/UserDataAuth.pb.h"
#include "chromeos/ash/components/dbus/cryptohome/auth_factor.pb.h"
#include "chromeos/ash/components/dbus/cryptohome/recoverable_key_store.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cryptohome {
namespace {
class AuthFactorConversionsTest : public testing::Test {
protected:
AuthFactorConversionsTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
base::test::SingleThreadTaskEnvironment task_environment_;
};
// Makes sure that `SafeConvertFactorTypeFromProto` and
// `ConvertFactorTypeFromProto` return corresponding values.
TEST_F(AuthFactorConversionsTest, FactorTypeProtoToChrome) {
for (user_data_auth::AuthFactorType type = user_data_auth::AuthFactorType_MIN;
type <= user_data_auth::AuthFactorType_MAX;
type = static_cast<user_data_auth::AuthFactorType>(type + 1)) {
std::optional<AuthFactorType> result = SafeConvertFactorTypeFromProto(type);
SCOPED_TRACE("For user_data_auth::AuthFactorType " +
base::NumberToString(type));
if (result.has_value()) {
EXPECT_EQ(result.value(), ConvertFactorTypeFromProto(type));
} else {
EXPECT_DEATH(ConvertFactorTypeFromProto(type), "FATAL");
}
}
}
// Makes sure that `SerializeAuthFactor` and `DeserializeAuthFactor` convert
// factor-specific metadata correctly.
TEST_F(AuthFactorConversionsTest, MetadataConversion) {
constexpr char kHashInfoSalt[] = "fake_salt";
{
// Test password metadata conversion.
const AuthFactor password(
AuthFactorRef(AuthFactorType::kPassword, KeyLabel("password")),
AuthFactorCommonMetadata(),
PasswordMetadata::CreateForLocalPassword(SystemSalt(kHashInfoSalt)));
user_data_auth::AuthFactor password_proto;
SerializeAuthFactor(password, &password_proto);
ASSERT_TRUE(password_proto.has_password_metadata());
ASSERT_TRUE(password_proto.password_metadata().has_hash_info());
user_data_auth::KnowledgeFactorHashInfo password_hash_info_proto =
password_proto.password_metadata().hash_info();
EXPECT_EQ(password_hash_info_proto.algorithm(),
KnowledgeFactorHashAlgorithm::HASH_TYPE_SHA256_TOP_HALF);
EXPECT_EQ(password_hash_info_proto.salt(), kHashInfoSalt);
EXPECT_TRUE(password_hash_info_proto.should_generate_key_store());
user_data_auth::AuthFactorWithStatus factor_with_status;
*factor_with_status.mutable_auth_factor() = password_proto;
const AuthFactor reconstructed_password =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPassword);
const std::optional<KnowledgeFactorHashInfo>& password_hash_info =
reconstructed_password.GetPasswordMetadata().hash_info();
ASSERT_TRUE(password_hash_info.has_value());
EXPECT_EQ(password_hash_info->algorithm,
KnowledgeFactorHashAlgorithmWrapper::kSha256TopHalf);
EXPECT_EQ(password_hash_info->salt, kHashInfoSalt);
EXPECT_TRUE(password_hash_info->should_generate_key_store);
}
{
// Test password metadata conversion without generating key store.
const AuthFactor password(
AuthFactorRef(AuthFactorType::kPassword, KeyLabel("password2")),
AuthFactorCommonMetadata(),
PasswordMetadata::CreateForOnlinePassword(SystemSalt(kHashInfoSalt)));
user_data_auth::AuthFactor password_proto;
SerializeAuthFactor(password, &password_proto);
ASSERT_TRUE(password_proto.has_password_metadata());
ASSERT_TRUE(password_proto.password_metadata().has_hash_info());
user_data_auth::KnowledgeFactorHashInfo password_hash_info_proto =
password_proto.password_metadata().hash_info();
EXPECT_EQ(password_hash_info_proto.algorithm(),
KnowledgeFactorHashAlgorithm::HASH_TYPE_SHA256_TOP_HALF);
EXPECT_EQ(password_hash_info_proto.salt(), kHashInfoSalt);
EXPECT_FALSE(password_hash_info_proto.should_generate_key_store());
user_data_auth::AuthFactorWithStatus factor_with_status;
*factor_with_status.mutable_auth_factor() = password_proto;
const AuthFactor reconstructed_password =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPassword);
const std::optional<KnowledgeFactorHashInfo>& password_hash_info =
reconstructed_password.GetPasswordMetadata().hash_info();
ASSERT_TRUE(password_hash_info.has_value());
EXPECT_EQ(password_hash_info->algorithm,
KnowledgeFactorHashAlgorithmWrapper::kSha256TopHalf);
EXPECT_EQ(password_hash_info->salt, kHashInfoSalt);
EXPECT_FALSE(password_hash_info->should_generate_key_store);
}
{
// Test password metadata conversion without salt.
const AuthFactor password(
AuthFactorRef(AuthFactorType::kPassword, KeyLabel("password")),
AuthFactorCommonMetadata(), PasswordMetadata::CreateWithoutSalt());
user_data_auth::AuthFactor password_proto;
SerializeAuthFactor(password, &password_proto);
ASSERT_TRUE(password_proto.has_password_metadata());
EXPECT_FALSE(password_proto.password_metadata().has_hash_info());
user_data_auth::AuthFactorWithStatus factor_with_status;
*factor_with_status.mutable_auth_factor() = password_proto;
const AuthFactor reconstructed_password =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPassword);
EXPECT_EQ(reconstructed_password.GetPasswordMetadata().hash_info(),
std::nullopt);
}
{
// Test PIN metadata conversion.
const AuthFactor pin(AuthFactorRef(AuthFactorType::kPin, KeyLabel("pin")),
AuthFactorCommonMetadata(),
PinMetadata::Create(PinSalt(kHashInfoSalt)));
user_data_auth::AuthFactor pin_proto;
SerializeAuthFactor(pin, &pin_proto);
ASSERT_TRUE(pin_proto.has_pin_metadata());
ASSERT_TRUE(pin_proto.pin_metadata().has_hash_info());
user_data_auth::KnowledgeFactorHashInfo pin_hash_info_proto =
pin_proto.pin_metadata().hash_info();
EXPECT_EQ(pin_hash_info_proto.algorithm(),
KnowledgeFactorHashAlgorithm::HASH_TYPE_PBKDF2_AES256_1234);
EXPECT_EQ(pin_hash_info_proto.salt(), kHashInfoSalt);
user_data_auth::AuthFactorWithStatus factor_with_status;
*factor_with_status.mutable_auth_factor() = pin_proto;
const AuthFactor reconstructed_pin =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPin);
const std::optional<KnowledgeFactorHashInfo>& pin_hash_info =
reconstructed_pin.GetPinMetadata().hash_info();
ASSERT_TRUE(pin_hash_info.has_value());
EXPECT_EQ(pin_hash_info->algorithm,
KnowledgeFactorHashAlgorithmWrapper::kPbkdf2Aes2561234);
EXPECT_EQ(pin_hash_info->salt, kHashInfoSalt);
}
{
// Test recovery metadata conversion.
cryptohome::AuthFactor recovery(
AuthFactorRef(cryptohome::AuthFactorType::kRecovery,
KeyLabel{"fake-recovery-label"}),
AuthFactorCommonMetadata(),
CryptohomeRecoveryMetadata{"hsm-public-key"});
user_data_auth::AuthFactor recovery_proto;
cryptohome::SerializeAuthFactor(recovery, &recovery_proto);
user_data_auth::AuthFactorWithStatus factor_with_status;
*factor_with_status.mutable_auth_factor() = recovery_proto;
auto deserialized_recovery = cryptohome::DeserializeAuthFactor(
factor_with_status,
/*fallback_type=*/cryptohome::AuthFactorType::kPassword);
EXPECT_EQ(deserialized_recovery.ref(), recovery.ref());
EXPECT_EQ(
deserialized_recovery.GetCryptohomeRecoveryMetadata().mediator_pub_key,
recovery.GetCryptohomeRecoveryMetadata().mediator_pub_key);
}
}
// Makes sure that various status bits are handled correctly.
TEST_F(AuthFactorConversionsTest, PinFactorStatusConversion) {
constexpr char kHashInfoSalt[] = "fake_salt";
const base::TimeDelta in_a_while = base::Seconds(10);
// PinFactor with no StatusInfo field.
user_data_auth::AuthFactorWithStatus factor_with_status;
auto* factor_proto = factor_with_status.mutable_auth_factor();
factor_proto->set_type(user_data_auth::AUTH_FACTOR_TYPE_PIN);
factor_proto->set_label("pin");
factor_proto->mutable_common_metadata();
factor_proto->mutable_pin_metadata()->mutable_hash_info()->set_algorithm(
KnowledgeFactorHashAlgorithm::HASH_TYPE_PBKDF2_AES256_1234);
factor_proto->mutable_pin_metadata()->mutable_hash_info()->set_salt(
kHashInfoSalt);
factor_proto->mutable_pin_metadata()
->mutable_hash_info()
->set_should_generate_key_store(true);
// By default, PinFactor is available.
{
AuthFactor parsed =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPin);
EXPECT_FALSE(parsed.GetPinStatus().IsLockedFactor());
EXPECT_EQ(base::Time::Now(), parsed.GetPinStatus().AvailableAt());
}
// PinFactor indefinitely locked.
{
factor_with_status.clear_status_info();
auto* status_info = factor_with_status.mutable_status_info();
status_info->set_time_available_in(std::numeric_limits<uint64_t>::max());
AuthFactor parsed =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPin);
EXPECT_TRUE(parsed.GetPinStatus().IsLockedFactor());
EXPECT_EQ(base::Time::Max(), parsed.GetPinStatus().AvailableAt());
}
// PinFactor temporary locked with a timeout.
{
factor_with_status.clear_status_info();
auto* status_info = factor_with_status.mutable_status_info();
status_info->set_time_available_in(in_a_while.InMilliseconds());
AuthFactor parsed =
DeserializeAuthFactor(factor_with_status,
/*fallback_type=*/AuthFactorType::kPin);
EXPECT_TRUE(parsed.GetPinStatus().IsLockedFactor());
EXPECT_EQ(base::Time::Now() + in_a_while,
parsed.GetPinStatus().AvailableAt());
}
}
} // namespace
} // namespace cryptohome
|