| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/lib/util/dynamic_encrypted_message_maker.h" | |
| #include <algorithm> | |
| #include <chrono> | |
| #include <cstdint> | |
| #include <memory> | |
| #include <shared_mutex> | |
| #include <string> | |
| #include <string_view> | |
| #include <utility> | |
| #include "src/lib/util/datetime_util.h" | |
| #include "src/lib/util/hybrid_tink_encrypted_message_maker.h" | |
| #include "src/logging.h" | |
| #include "src/pb/cobalt_public_keys.pb.h" | |
| #include "src/public/lib/status.h" | |
| #include "src/public/lib/status_codes.h" | |
| namespace cobalt::util { | |
| namespace { | |
| Status ValidateCobaltPublicKey(const CobaltPublicKey& key, std::string_view key_label) { | |
| if (key.public_tink_keyset().empty()) { | |
| return Status(StatusCode::FAILED_PRECONDITION, | |
| std::string("ParsePublicKeys: Empty public_tink_keyset for ").append(key_label)); | |
| } | |
| if (key.key_index() == 0) { | |
| return Status(StatusCode::INVALID_ARGUMENT, | |
| std::string("ParsePublicKeys: Invalid key_index 0 for ").append(key_label)); | |
| } | |
| if (key.expiration_timestamp_secs() <= 0) { | |
| return Status( | |
| StatusCode::INVALID_ARGUMENT, | |
| std::string("ParsePublicKeys: Invalid expiration_timestamp_secs for ").append(key_label)); | |
| } | |
| return Status::OkStatus(); | |
| } | |
| } // namespace | |
| lib::statusor::StatusOr<ParsedKeyset> ParsePublicKeys(std::string_view pb_bytes, | |
| system_data::Environment env) { | |
| if (pb_bytes.empty()) { | |
| return Status(StatusCode::INVALID_ARGUMENT, "ParsePublicKeys: Empty protobuf bytes."); | |
| } | |
| CobaltPublicKeys public_keys; | |
| if (!public_keys.ParseFromString(pb_bytes)) { | |
| return Status(StatusCode::INVALID_ARGUMENT, | |
| "ParsePublicKeys: Failed to parse CobaltPublicKeys proto."); | |
| } | |
| const CobaltPublicKey* shuffler_key = nullptr; | |
| const CobaltPublicKey* analyzer_key = nullptr; | |
| switch (env) { | |
| case system_data::Environment::PROD: | |
| shuffler_key = &public_keys.prod_shuffler_key(); | |
| analyzer_key = &public_keys.prod_analyzer_key(); | |
| break; | |
| case system_data::Environment::DEVEL: | |
| shuffler_key = &public_keys.dev_shuffler_key(); | |
| analyzer_key = &public_keys.dev_analyzer_key(); | |
| break; | |
| default: | |
| return Status(StatusCode::INVALID_ARGUMENT, | |
| "ParsePublicKeys: Unsupported environment for dynamic public keys."); | |
| } | |
| Status shuffler_status = ValidateCobaltPublicKey(*shuffler_key, "shuffler"); | |
| if (!shuffler_status.ok()) { | |
| return shuffler_status; | |
| } | |
| Status analyzer_status = ValidateCobaltPublicKey(*analyzer_key, "analyzer"); | |
| if (!analyzer_status.ok()) { | |
| return analyzer_status; | |
| } | |
| lib::statusor::StatusOr<NotNullUniquePtr<EncryptedMessageMaker>> shuffler_maker = | |
| MakeHybridTinkEncryptedMessageMaker(shuffler_key->public_tink_keyset(), kShufflerContextInfo, | |
| shuffler_key->key_index()); | |
| if (!shuffler_maker.ok()) { | |
| return Status(StatusCode::FAILED_PRECONDITION, shuffler_maker.status().error_message()); | |
| } | |
| lib::statusor::StatusOr<NotNullUniquePtr<EncryptedMessageMaker>> analyzer_maker = | |
| MakeHybridTinkEncryptedMessageMaker(analyzer_key->public_tink_keyset(), kAnalyzerContextInfo, | |
| analyzer_key->key_index()); | |
| if (!analyzer_maker.ok()) { | |
| return Status(StatusCode::FAILED_PRECONDITION, analyzer_maker.status().error_message()); | |
| } | |
| const int64_t effective_expiration_secs = std::min(shuffler_key->expiration_timestamp_secs(), | |
| analyzer_key->expiration_timestamp_secs()); | |
| return ParsedKeyset( | |
| ParsedKey(std::move(*shuffler_maker), shuffler_key->key_index(), effective_expiration_secs), | |
| ParsedKey(std::move(*analyzer_maker), analyzer_key->key_index(), effective_expiration_secs)); | |
| } | |
| DynamicEncryptedMessageMaker::DynamicEncryptedMessageMaker(SystemClockInterface* clock) | |
| : clock_(clock) { | |
| CHECK(clock_ != nullptr); | |
| } | |
| DynamicEncryptedMessageMaker::DynamicEncryptedMessageMaker(ParsedKey initial_key, | |
| SystemClockInterface* clock) | |
| : current_maker_(initial_key.TakeMaker().Unwrap()), | |
| key_index_(initial_key.key_index()), | |
| expiration_timestamp_secs_(initial_key.expiration_timestamp_secs()), | |
| clock_(clock) { | |
| CHECK(clock_ != nullptr); | |
| } | |
| bool DynamicEncryptedMessageMaker::Encrypt(const google::protobuf::MessageLite& message, | |
| EncryptedMessage* encrypted_message) const { | |
| if (encrypted_message == nullptr) { | |
| return false; | |
| } | |
| std::shared_lock<std::shared_mutex> lock(mu_); | |
| if (current_maker_ == nullptr) { | |
| if (uninitialized_log_limiter_.ShouldLog()) { | |
| LOG(ERROR) << "DynamicEncryptedMessageMaker is uninitialized; refusing encryption."; | |
| } | |
| return false; | |
| } | |
| // Refuse encryption if the key has expired. | |
| const int64_t now_secs = ToUnixSeconds(clock_->now()); | |
| if (now_secs >= expiration_timestamp_secs_) { | |
| if (expired_log_limiter_.ShouldLog()) { | |
| LOG(ERROR) << "Key index " << key_index_ << " has expired (now: " << now_secs | |
| << ", exp: " << expiration_timestamp_secs_ << ")!"; | |
| } | |
| return false; | |
| } | |
| return current_maker_->Encrypt(message, encrypted_message); | |
| } | |
| void DynamicEncryptedMessageMaker::UpdateKey(ParsedKey new_key) { | |
| std::unique_lock<std::shared_mutex> lock(mu_); | |
| current_maker_ = new_key.TakeMaker().Unwrap(); | |
| key_index_ = new_key.key_index(); | |
| expiration_timestamp_secs_ = new_key.expiration_timestamp_secs(); | |
| expired_log_limiter_.Reset(); | |
| } | |
| bool DynamicEncryptedMessageMaker::IsExpired() const { | |
| std::shared_lock<std::shared_mutex> lock(mu_); | |
| if (current_maker_ == nullptr) { | |
| return false; | |
| } | |
| return ToUnixSeconds(clock_->now()) >= expiration_timestamp_secs_; | |
| } | |
| } // namespace cobalt::util |