// 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.

#include "chrome/browser/webauthn/fake_recovery_key_store.h"

#include <algorithm>

#include "base/base64.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "components/trusted_vault/proto/recovery_key_store.pb.h"
#include "components/trusted_vault/proto/vault.pb.h"
#include "crypto/evp.h"
#include "crypto/keypair.h"
#include "crypto/sha2.h"
#include "crypto/sign.h"
#include "crypto/subtle_passkey.h"
#include "device/fido/enclave/constants.h"
#include "device/fido/enclave/enclave_authenticator.h"
#include "net/cert/asn1_util.h"
#include "net/cert/x509_util.h"
#include "services/network/public/cpp/data_element.h"
#include "services/network/public/cpp/resource_request.h"
#include "third_party/boringssl/src/include/openssl/bn.h"
#include "third_party/boringssl/src/include/openssl/ec.h"
#include "third_party/boringssl/src/include/openssl/evp.h"

namespace {

// A 2048-bit RSA private key. It matches up with the testing root certificate
// that is embedded in `recovery_key_store.rs`.
static const uint8_t kRootRSAPrivateKey[] = {
    0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
    0xc7, 0x95, 0x62, 0x74, 0xaf, 0x3f, 0x50, 0xb9, 0xa8, 0x4f, 0x55, 0x12,
    0x0e, 0x5d, 0x1f, 0x89, 0x67, 0xdf, 0x55, 0x8c, 0x16, 0x75, 0x06, 0x1c,
    0x33, 0x01, 0xa0, 0xdb, 0xa4, 0x88, 0x39, 0xdc, 0xca, 0x57, 0xf2, 0x6b,
    0xd3, 0x0b, 0x22, 0x0d, 0x79, 0x2c, 0xc2, 0x4c, 0x1d, 0xf6, 0x0c, 0x32,
    0xf6, 0x66, 0xca, 0x07, 0x89, 0x12, 0x17, 0x1e, 0x43, 0xbb, 0x16, 0xda,
    0xe8, 0x2a, 0xec, 0x89, 0x25, 0x84, 0x88, 0xa5, 0x5d, 0xc1, 0x80, 0xb1,
    0x9c, 0xab, 0xed, 0x00, 0x7d, 0xec, 0x9d, 0x4a, 0x49, 0xa4, 0x61, 0x48,
    0x9e, 0xab, 0xb3, 0x78, 0x35, 0xce, 0x72, 0x80, 0x40, 0x20, 0x58, 0xca,
    0x90, 0xbc, 0x24, 0x41, 0x01, 0x48, 0xaf, 0xc7, 0x69, 0x9d, 0x04, 0xa0,
    0xa5, 0x20, 0xe8, 0xca, 0x16, 0xe5, 0x96, 0x6a, 0x36, 0x82, 0x2f, 0xc6,
    0xe5, 0x0b, 0x53, 0x72, 0x3d, 0x89, 0x78, 0xc8, 0x27, 0xf7, 0xda, 0xe1,
    0x6e, 0x07, 0xd6, 0xc1, 0xad, 0xaf, 0xa7, 0x27, 0xbb, 0x4b, 0x1f, 0x09,
    0xdc, 0x41, 0x2d, 0x68, 0x21, 0xda, 0x85, 0x0b, 0xe1, 0x6f, 0x7b, 0x34,
    0xa3, 0x45, 0x80, 0x43, 0x4e, 0x2a, 0xe7, 0x17, 0xd1, 0xde, 0xd7, 0xc0,
    0xde, 0xf6, 0x0c, 0xa2, 0x50, 0x41, 0xd2, 0xf7, 0x7b, 0xb6, 0x81, 0x29,
    0xb3, 0x80, 0xcc, 0xbf, 0x1e, 0x5a, 0xeb, 0xf3, 0x3c, 0xd2, 0xe6, 0x8d,
    0xfc, 0x9a, 0x41, 0x23, 0xdc, 0xcb, 0x68, 0xff, 0x9e, 0x34, 0xe9, 0x25,
    0xd7, 0xa7, 0x27, 0xc4, 0x68, 0x72, 0x38, 0x0e, 0xcc, 0x5b, 0x48, 0x40,
    0xbb, 0x90, 0xa0, 0xfd, 0xd4, 0xc4, 0xea, 0xe9, 0x11, 0xd5, 0x6e, 0xda,
    0x34, 0xea, 0x40, 0x2b, 0xcd, 0x21, 0x9b, 0x77, 0xdc, 0x05, 0xc2, 0xcf,
    0x00, 0x05, 0xbc, 0x28, 0x5c, 0x59, 0xcf, 0xd9, 0x67, 0xcb, 0x30, 0x12,
    0x77, 0x5e, 0xb6, 0xe9, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
    0x00, 0x38, 0xfd, 0xa5, 0x41, 0xa0, 0x91, 0x39, 0xe1, 0xe9, 0xf7, 0xcd,
    0x58, 0xfc, 0x95, 0x06, 0xde, 0x5b, 0xb4, 0xe0, 0x5e, 0xc2, 0x42, 0xe4,
    0x5d, 0xc5, 0x0a, 0xdf, 0xfe, 0x18, 0x3b, 0x76, 0x1a, 0x60, 0x05, 0x41,
    0x0a, 0x1d, 0x17, 0x7e, 0xbf, 0xce, 0x2c, 0x43, 0xc7, 0x2c, 0x7f, 0x07,
    0xf8, 0xbd, 0x77, 0x98, 0x95, 0x12, 0x45, 0x02, 0x28, 0xf8, 0x3a, 0x42,
    0x4a, 0x7c, 0x25, 0x55, 0xa9, 0xcb, 0xf6, 0xce, 0x76, 0x3a, 0x11, 0x79,
    0x48, 0x3e, 0x6e, 0xb1, 0x9c, 0x84, 0x58, 0xbe, 0x89, 0x6a, 0x01, 0xf0,
    0x6d, 0xa6, 0x31, 0x6d, 0x6c, 0x14, 0x21, 0x41, 0x7c, 0xf1, 0xd2, 0xca,
    0xc7, 0xfd, 0x10, 0x5c, 0x61, 0xdb, 0x95, 0xff, 0xeb, 0xb7, 0x72, 0xc8,
    0xd1, 0x7e, 0x95, 0xa9, 0x23, 0x9b, 0xea, 0x2f, 0x4b, 0x61, 0x09, 0x47,
    0xcf, 0x49, 0xb4, 0x34, 0x3d, 0xd8, 0xe8, 0x9a, 0x7e, 0x3b, 0xdb, 0x13,
    0x41, 0x45, 0xd3, 0xac, 0x16, 0x87, 0x41, 0x0b, 0xac, 0x64, 0xae, 0x0c,
    0x43, 0x82, 0x60, 0x37, 0x78, 0xf0, 0xba, 0xdc, 0x2e, 0x29, 0xa8, 0xdd,
    0x03, 0xfb, 0xe8, 0xe1, 0x6e, 0x43, 0xc0, 0x4b, 0x1e, 0x5a, 0x5c, 0x85,
    0x1a, 0x1a, 0xc3, 0xf7, 0xd8, 0x19, 0x4d, 0xcc, 0xf9, 0xea, 0xaa, 0x97,
    0x15, 0x73, 0xfe, 0xcf, 0x55, 0x9c, 0xb9, 0xdc, 0xc4, 0x67, 0x7b, 0xe0,
    0x1f, 0x5f, 0xf0, 0x33, 0xe1, 0x76, 0x03, 0x18, 0xbe, 0xef, 0xcb, 0xc0,
    0x10, 0x5e, 0xc9, 0xc5, 0x98, 0x19, 0xd8, 0x8a, 0xb6, 0x42, 0x84, 0xd1,
    0x0a, 0x61, 0x13, 0x4f, 0x28, 0xe3, 0xbf, 0xcd, 0xba, 0x4d, 0x15, 0x19,
    0x14, 0x91, 0x41, 0xf2, 0x62, 0x25, 0xd2, 0xc6, 0x7b, 0xf5, 0xfd, 0x05,
    0xc4, 0xf4, 0xca, 0xae, 0xea, 0x56, 0x86, 0x4a, 0x69, 0x29, 0xfa, 0x9d,
    0xa1, 0xcf, 0x2f, 0xe9, 0x01, 0x02, 0x81, 0x81, 0x00, 0xf1, 0x8f, 0x1d,
    0xa4, 0xe7, 0x47, 0xbe, 0xee, 0x93, 0x2f, 0x49, 0xe1, 0x4f, 0x26, 0x1c,
    0xd3, 0x4c, 0x26, 0x00, 0x91, 0x50, 0x4b, 0x5f, 0x42, 0x92, 0x2a, 0xca,
    0x15, 0xac, 0x15, 0x67, 0xaf, 0x10, 0x52, 0x28, 0xce, 0xad, 0x06, 0xea,
    0xbe, 0x94, 0x45, 0x81, 0x51, 0xde, 0x87, 0x56, 0x68, 0x05, 0x97, 0xe0,
    0xe9, 0xba, 0x25, 0x57, 0x30, 0x8b, 0xb4, 0xb0, 0xb3, 0x04, 0x80, 0xff,
    0xde, 0x23, 0x5c, 0x4c, 0xae, 0x89, 0x04, 0x6b, 0xb4, 0xe1, 0x4d, 0x57,
    0xe3, 0xd6, 0x9a, 0x5f, 0xe3, 0x9c, 0x28, 0x30, 0x37, 0xa0, 0xdc, 0xa4,
    0xbf, 0x66, 0x67, 0xa0, 0xb8, 0x32, 0x7f, 0xf4, 0x60, 0x6d, 0xc6, 0x03,
    0x18, 0xb1, 0x57, 0x8a, 0xab, 0x8d, 0xb5, 0x9a, 0xbb, 0x91, 0xd5, 0xe3,
    0x31, 0x3f, 0x0d, 0xbf, 0xfd, 0xe3, 0x1c, 0x37, 0xc2, 0xf0, 0xad, 0x1b,
    0x60, 0x8f, 0x57, 0xdc, 0xb9, 0x02, 0x81, 0x81, 0x00, 0xd3, 0x83, 0xdd,
    0x44, 0xf0, 0x2e, 0x06, 0x02, 0xd3, 0xb3, 0x9f, 0x6f, 0xf2, 0xe8, 0xf1,
    0x3a, 0xa8, 0x75, 0xa7, 0x72, 0x7d, 0x59, 0xac, 0xdf, 0x41, 0x00, 0x09,
    0xd5, 0xd2, 0x60, 0xbf, 0xeb, 0x6b, 0x4f, 0x5c, 0x62, 0xbb, 0x5c, 0xa0,
    0x10, 0xfc, 0x46, 0xf4, 0xeb, 0x79, 0x1d, 0xe7, 0xf2, 0xf8, 0xc0, 0xbb,
    0x8d, 0xc4, 0xd2, 0xf2, 0x2c, 0x1b, 0x34, 0xce, 0xa1, 0x96, 0x83, 0x89,
    0x3c, 0xf7, 0x6c, 0xcb, 0xdc, 0xa3, 0x15, 0x7f, 0xfc, 0x82, 0xb3, 0xc0,
    0x42, 0x60, 0x1f, 0x06, 0x2d, 0x3f, 0x4c, 0x67, 0x3b, 0x26, 0xa3, 0x2b,
    0x53, 0xc7, 0x2b, 0x91, 0xd1, 0xf9, 0x5f, 0x20, 0x25, 0x49, 0xdd, 0x7c,
    0xf8, 0x2c, 0x4a, 0x4b, 0x4a, 0x90, 0xf0, 0x8d, 0xd1, 0x1a, 0x8a, 0x8b,
    0x91, 0xae, 0x42, 0x8a, 0xf7, 0x89, 0xd6, 0xca, 0xf4, 0xb9, 0xe0, 0x80,
    0x27, 0xf3, 0x38, 0x73, 0xb1, 0x02, 0x81, 0x81, 0x00, 0xa8, 0x74, 0x3c,
    0xa1, 0x0e, 0x24, 0xcb, 0x72, 0xc6, 0xb3, 0xb8, 0x9a, 0xe6, 0x3e, 0x56,
    0xe5, 0xc7, 0x79, 0xcc, 0xf5, 0x89, 0x80, 0x36, 0x7b, 0x4f, 0x41, 0x85,
    0xd2, 0x1d, 0x42, 0x84, 0x1e, 0xe2, 0x8b, 0xd9, 0x27, 0x60, 0x24, 0xad,
    0xb9, 0x9f, 0xde, 0xe4, 0xb3, 0x3a, 0x33, 0x39, 0x3f, 0xab, 0x7e, 0xe1,
    0xfb, 0x3c, 0xb3, 0xe3, 0x9c, 0xd8, 0x20, 0x77, 0x05, 0x30, 0x83, 0xe1,
    0x6b, 0x81, 0x4f, 0x51, 0xae, 0x79, 0xa0, 0xda, 0x97, 0xb6, 0x72, 0xe0,
    0x72, 0xe3, 0xea, 0x9e, 0x2a, 0x35, 0x71, 0x15, 0x47, 0x05, 0xe8, 0x38,
    0xff, 0xbd, 0x23, 0x51, 0x42, 0x82, 0xfd, 0x16, 0x67, 0xd4, 0xc5, 0xbc,
    0xcd, 0x10, 0x87, 0x53, 0x0e, 0x15, 0xf1, 0xea, 0x69, 0x19, 0xe7, 0x85,
    0xdb, 0x86, 0xf4, 0x48, 0xca, 0x7e, 0x01, 0xe4, 0xa5, 0x0e, 0x80, 0x39,
    0xd2, 0x59, 0xc4, 0x55, 0x09, 0x02, 0x81, 0x80, 0x77, 0xde, 0x92, 0x46,
    0xa0, 0x6b, 0x24, 0xc2, 0xf3, 0x61, 0x27, 0xa9, 0x55, 0x1b, 0x88, 0xa2,
    0x7a, 0x97, 0xfa, 0xe6, 0x9a, 0x7d, 0x78, 0xc8, 0x29, 0x59, 0x31, 0x39,
    0xb9, 0x79, 0x55, 0x2a, 0xc0, 0xec, 0xff, 0xe9, 0x80, 0x4f, 0x54, 0xf3,
    0x6b, 0x55, 0x77, 0x35, 0xff, 0xb4, 0xe0, 0x1f, 0xc5, 0xb3, 0xcc, 0x56,
    0xe2, 0xc5, 0xbd, 0xb3, 0x1b, 0x54, 0x09, 0x0a, 0x70, 0x77, 0x69, 0x0e,
    0x61, 0x16, 0x8a, 0x4e, 0x27, 0x2a, 0x67, 0x17, 0x1e, 0xa7, 0x69, 0xfc,
    0xf3, 0xe6, 0x81, 0x24, 0xa1, 0x6b, 0xd6, 0x51, 0x22, 0x6b, 0x74, 0x81,
    0x8d, 0x2d, 0x76, 0xfd, 0x6d, 0x7a, 0xe8, 0x87, 0xd9, 0x5d, 0xa9, 0x96,
    0x27, 0xdb, 0x8e, 0xe1, 0xda, 0x54, 0x49, 0xba, 0xc8, 0x74, 0xdd, 0xf6,
    0x18, 0xb0, 0xe3, 0x7a, 0xb7, 0xe6, 0x3f, 0xad, 0x03, 0xae, 0x64, 0x52,
    0xbf, 0xb7, 0x2d, 0x71, 0x02, 0x81, 0x80, 0x77, 0xb6, 0x35, 0x51, 0x62,
    0xe1, 0x44, 0x55, 0x78, 0x87, 0x58, 0x38, 0xfd, 0xf1, 0xac, 0xb7, 0xaf,
    0xc0, 0xd0, 0x8f, 0x17, 0xdd, 0x9d, 0xec, 0x05, 0x9e, 0x7c, 0xfb, 0xab,
    0x5e, 0xa5, 0x63, 0xa9, 0x03, 0x8c, 0x55, 0x3e, 0x88, 0x89, 0x29, 0xba,
    0x7e, 0x9b, 0x98, 0x58, 0x74, 0x9d, 0x0f, 0xdf, 0xc1, 0xb9, 0xad, 0xcb,
    0x27, 0x4e, 0xec, 0xb9, 0x5f, 0x6c, 0xc9, 0x46, 0x0e, 0xc3, 0x6c, 0x32,
    0x5e, 0xe9, 0x42, 0xbf, 0x6b, 0xab, 0xd2, 0x8f, 0x78, 0x78, 0x7d, 0x28,
    0x63, 0xbe, 0x2c, 0xba, 0x32, 0x58, 0x42, 0xd6, 0xe7, 0x7d, 0xf4, 0xd7,
    0x52, 0x24, 0xf4, 0xfa, 0x98, 0x00, 0x40, 0x95, 0x55, 0x80, 0x36, 0xc2,
    0x42, 0x3e, 0x4a, 0xed, 0x46, 0xe7, 0xf6, 0x46, 0xb7, 0xc2, 0x15, 0xba,
    0x65, 0x2e, 0x12, 0x9e, 0x8b, 0x73, 0x08, 0xb0, 0xae, 0xd0, 0x6d, 0x9b,
    0x9d, 0x40, 0x5d,
};

// This is a 2048-bit RSA private key that exists just to avoiding doing
// slow RSA key generation during tests. It can be changed at any time.
static const uint8_t kSigRSAPrivateKey[] = {
    0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
    0xc2, 0xce, 0x06, 0x23, 0xc3, 0x04, 0x5d, 0xe4, 0x6d, 0x03, 0x82, 0x2c,
    0x3a, 0x7f, 0x8d, 0xef, 0x94, 0xa9, 0x26, 0xca, 0x0e, 0x5f, 0xca, 0xbf,
    0x04, 0x2a, 0x22, 0x35, 0x74, 0xf2, 0xd0, 0x54, 0x04, 0x3e, 0x32, 0x15,
    0x2a, 0xfd, 0x43, 0x1c, 0x71, 0x6b, 0x7d, 0xd3, 0x9b, 0x93, 0xac, 0x72,
    0xfa, 0x72, 0x07, 0x11, 0x37, 0xe0, 0xd7, 0xc6, 0xfc, 0xf8, 0xa6, 0x6f,
    0xa9, 0x03, 0x94, 0xed, 0xdc, 0xb0, 0x53, 0xfb, 0xc9, 0xc1, 0xd8, 0x57,
    0x03, 0x3f, 0x45, 0x04, 0x3a, 0x67, 0x5c, 0x79, 0xdc, 0x0a, 0x11, 0x3e,
    0x7a, 0xb5, 0xd5, 0xf2, 0xc3, 0xe0, 0x82, 0x4a, 0x89, 0xe9, 0x03, 0xe9,
    0x7e, 0x4d, 0xd1, 0xd0, 0x75, 0x5c, 0xe1, 0x2c, 0x22, 0x23, 0x3a, 0x8b,
    0x49, 0x0e, 0xb9, 0x4e, 0xd1, 0xd2, 0xb0, 0x2f, 0xa8, 0x95, 0xcb, 0x49,
    0xd1, 0x03, 0x46, 0x9a, 0xf7, 0xca, 0x76, 0x59, 0x69, 0x74, 0xe4, 0xf6,
    0xbe, 0x6d, 0x3c, 0x7f, 0xd0, 0x73, 0xa9, 0x94, 0x1e, 0xf6, 0xab, 0xb0,
    0x67, 0x11, 0x3f, 0xa5, 0x4d, 0x67, 0xa9, 0x3a, 0xaa, 0xf2, 0xb0, 0xbe,
    0x86, 0x24, 0x04, 0xab, 0x07, 0xbf, 0x42, 0x4b, 0xf5, 0x9a, 0x96, 0xd4,
    0x9a, 0xd6, 0xc9, 0xfa, 0x81, 0xfe, 0x0b, 0x80, 0x98, 0x66, 0x19, 0x87,
    0x28, 0xad, 0x2f, 0xfb, 0x7a, 0xa7, 0xbd, 0x4f, 0x82, 0x31, 0xe5, 0xaf,
    0xbe, 0xba, 0xd8, 0x7c, 0xfc, 0x14, 0x6c, 0x1c, 0x93, 0x92, 0x2e, 0x67,
    0x53, 0x76, 0x3e, 0x25, 0xa8, 0x4f, 0x1f, 0x2b, 0x1f, 0xb8, 0xe7, 0xb2,
    0xf7, 0x08, 0x1d, 0xc4, 0xf3, 0x04, 0x6b, 0xc5, 0x7f, 0x30, 0xeb, 0x82,
    0xed, 0x29, 0x91, 0x23, 0xf8, 0x26, 0xc5, 0x4e, 0x47, 0xb9, 0xc5, 0x92,
    0x99, 0xfd, 0x8a, 0x04, 0xf0, 0x92, 0x4c, 0x10, 0x47, 0x77, 0xfa, 0x36,
    0xb1, 0x4a, 0x2b, 0x91, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
    0x00, 0x0d, 0xcd, 0xd3, 0x99, 0x8d, 0x24, 0x2e, 0x13, 0x15, 0xb6, 0x7d,
    0xee, 0xec, 0x66, 0x45, 0x84, 0x88, 0xa0, 0x43, 0xe3, 0x65, 0x89, 0x5c,
    0x30, 0x94, 0xf6, 0xf7, 0x5d, 0x9a, 0x5f, 0xea, 0x6b, 0x49, 0xb1, 0xae,
    0x92, 0xd9, 0x6d, 0x00, 0xe8, 0x6f, 0x11, 0x12, 0xe5, 0x7f, 0xcc, 0xc3,
    0x17, 0x92, 0xaf, 0xa9, 0xd8, 0x8a, 0x81, 0x20, 0x49, 0x35, 0x7b, 0xd9,
    0x2f, 0x68, 0x66, 0x9c, 0x93, 0x0e, 0x64, 0x60, 0x4f, 0x23, 0x48, 0xdd,
    0x2f, 0xad, 0x47, 0x3a, 0x78, 0xc6, 0x3a, 0xb4, 0xce, 0xdd, 0xcc, 0xc8,
    0xd4, 0x08, 0x96, 0x02, 0x07, 0x25, 0xe2, 0x2e, 0xd9, 0x61, 0x54, 0x65,
    0xb6, 0x92, 0x00, 0xa9, 0xb1, 0x7f, 0xdf, 0x89, 0x05, 0x3b, 0x96, 0x0b,
    0x7b, 0xfb, 0x3d, 0x53, 0xdc, 0x45, 0xfa, 0x0c, 0x0a, 0x14, 0x84, 0x28,
    0xd7, 0x9e, 0x3c, 0x01, 0x30, 0xd2, 0x06, 0x42, 0xc4, 0x8b, 0x8a, 0xb7,
    0x55, 0x70, 0xfc, 0xf2, 0xbf, 0xa2, 0x4c, 0xe6, 0x34, 0x27, 0xf4, 0x49,
    0x21, 0x87, 0x48, 0x27, 0x75, 0x06, 0x7f, 0xe3, 0x14, 0x12, 0x2f, 0x68,
    0xb3, 0x1d, 0x34, 0xff, 0xcd, 0x9a, 0x9b, 0x66, 0x99, 0x56, 0x6d, 0x6b,
    0xf4, 0xfd, 0xf4, 0x75, 0xfb, 0x4b, 0x3f, 0x31, 0xed, 0xde, 0x30, 0x09,
    0x0d, 0xd5, 0x68, 0xf4, 0xda, 0x7d, 0x49, 0x8b, 0x5d, 0xe3, 0xcc, 0x7c,
    0xd1, 0x5b, 0xd1, 0x2b, 0x7b, 0x16, 0x1f, 0xd6, 0x6b, 0xd9, 0xe7, 0x5f,
    0x6f, 0x2e, 0xf7, 0xcb, 0x01, 0x9b, 0x8d, 0x58, 0xc9, 0x89, 0xb3, 0xb0,
    0xdc, 0xd2, 0xd4, 0x08, 0xec, 0x63, 0x83, 0x56, 0x43, 0x52, 0xfe, 0xdf,
    0x28, 0x01, 0x03, 0xd3, 0x89, 0x25, 0x70, 0xa3, 0x4a, 0x3b, 0xab, 0x43,
    0xa4, 0x80, 0x01, 0xd9, 0xe9, 0xb2, 0xad, 0x54, 0x6a, 0x68, 0x47, 0x01,
    0xd4, 0x54, 0x76, 0xa9, 0x51, 0x02, 0x81, 0x81, 0x00, 0xe9, 0x34, 0x21,
    0x30, 0x40, 0xae, 0xd8, 0x55, 0xb2, 0xd4, 0x16, 0x07, 0xf1, 0xd4, 0x83,
    0x1f, 0xf5, 0xef, 0x36, 0x7f, 0x93, 0x8a, 0x53, 0x8e, 0x04, 0x1a, 0x22,
    0x2e, 0x79, 0x36, 0x8c, 0x1f, 0xad, 0x72, 0xd9, 0x2a, 0x70, 0x2d, 0xba,
    0xff, 0x6e, 0x16, 0x58, 0xb5, 0x13, 0xa5, 0x2a, 0x0f, 0x44, 0x9b, 0x38,
    0x43, 0xae, 0x7b, 0x01, 0x12, 0x0c, 0x98, 0x7e, 0x1d, 0xd2, 0x9e, 0x14,
    0x46, 0x9b, 0xa0, 0x9f, 0xbc, 0xe7, 0x35, 0x89, 0xbe, 0x56, 0x56, 0x50,
    0xe8, 0x01, 0xfe, 0x2e, 0x01, 0xb8, 0xe4, 0x41, 0x1f, 0x5d, 0x69, 0xff,
    0x92, 0xa3, 0x00, 0x16, 0x08, 0x87, 0xde, 0x9f, 0xbf, 0xf9, 0x07, 0xf2,
    0x42, 0xf9, 0x0d, 0xf3, 0xca, 0x67, 0xac, 0x4c, 0x72, 0x99, 0x8c, 0x56,
    0x16, 0xe4, 0x68, 0xb2, 0x3e, 0xc5, 0xd1, 0x32, 0x82, 0x6f, 0x09, 0x39,
    0xcc, 0xa9, 0xc7, 0x35, 0xfb, 0x02, 0x81, 0x81, 0x00, 0xd5, 0xd8, 0xf8,
    0xaf, 0xc3, 0x11, 0xc1, 0x2e, 0xe8, 0xac, 0x8e, 0xdb, 0xe0, 0xd0, 0xf4,
    0x6d, 0x23, 0xa5, 0x14, 0xfd, 0x44, 0x49, 0xbb, 0xc4, 0x9d, 0xd5, 0x88,
    0x07, 0x74, 0xc6, 0xd3, 0xe8, 0x7d, 0xbe, 0x57, 0xab, 0xb7, 0xc4, 0x7c,
    0x71, 0x9f, 0xc5, 0xbc, 0xea, 0x84, 0x7a, 0xfe, 0x45, 0x00, 0x1e, 0x45,
    0x44, 0x71, 0xa9, 0xca, 0x2c, 0x90, 0x74, 0xa3, 0xfe, 0x5a, 0xfe, 0xb9,
    0xef, 0x9b, 0x5b, 0x16, 0x56, 0x98, 0x20, 0x90, 0x23, 0x91, 0x81, 0xe9,
    0x0a, 0xfb, 0x87, 0xd4, 0x87, 0x3d, 0x4f, 0xba, 0x2a, 0x7d, 0xe2, 0xb8,
    0x78, 0x53, 0x1a, 0x1f, 0xf7, 0xb5, 0x78, 0x9b, 0x5d, 0x6f, 0x1b, 0x67,
    0x8f, 0xc3, 0xd3, 0xec, 0x03, 0xbc, 0x7a, 0xdf, 0x73, 0xe1, 0xe9, 0x8f,
    0x3f, 0x88, 0x3f, 0xd8, 0x3d, 0x6c, 0xbc, 0xbd, 0x66, 0x56, 0x7a, 0xfb,
    0xc3, 0xdd, 0xe4, 0x8a, 0xe3, 0x02, 0x81, 0x81, 0x00, 0xc3, 0x96, 0x9b,
    0x37, 0x53, 0x28, 0xd1, 0xfc, 0x9e, 0x6d, 0xce, 0xc4, 0x7f, 0x09, 0x13,
    0xa8, 0xdc, 0xa5, 0xf2, 0xf9, 0x71, 0x0f, 0x15, 0x21, 0x09, 0x43, 0xb9,
    0xe3, 0x6c, 0x7a, 0x59, 0x6f, 0xc0, 0x13, 0x91, 0x1c, 0x4c, 0xd5, 0xad,
    0xfe, 0xc9, 0xa0, 0x28, 0x6a, 0x46, 0xbd, 0xc1, 0xd6, 0xe8, 0x5d, 0x9f,
    0x04, 0xf4, 0xf3, 0xe9, 0x63, 0xef, 0xf8, 0x7d, 0xde, 0x19, 0x65, 0xc7,
    0xef, 0xa8, 0x5b, 0x41, 0x5e, 0x38, 0x34, 0xe9, 0x00, 0x20, 0x7d, 0x68,
    0x82, 0x0e, 0x19, 0xd7, 0x10, 0x4e, 0xb7, 0xf1, 0xfe, 0x92, 0x64, 0x72,
    0x9b, 0xd3, 0xe6, 0x20, 0x94, 0xab, 0x10, 0x06, 0xc0, 0x05, 0xe7, 0xaa,
    0x42, 0x8d, 0x8c, 0x2c, 0x2a, 0x58, 0x65, 0x64, 0x00, 0xc1, 0xd3, 0xb7,
    0xe8, 0x9f, 0x4d, 0x48, 0x21, 0xe9, 0xdb, 0x99, 0x17, 0x58, 0x75, 0x4e,
    0x6a, 0xdf, 0x02, 0x5b, 0xbd, 0x02, 0x81, 0x80, 0x52, 0xd2, 0x53, 0x8e,
    0x8e, 0xf8, 0x43, 0x5f, 0x8d, 0x91, 0xb7, 0x4a, 0x85, 0x49, 0xa8, 0x40,
    0xd4, 0xbb, 0x4c, 0xe3, 0xc2, 0x1c, 0xe4, 0x72, 0x03, 0xc6, 0xfd, 0x86,
    0x15, 0xbe, 0x23, 0xb8, 0x39, 0x6b, 0x0c, 0xbd, 0x5e, 0xd0, 0xe4, 0x79,
    0x85, 0x2d, 0xaa, 0x3b, 0x11, 0xd0, 0x28, 0xc6, 0x44, 0x83, 0x74, 0xc4,
    0x4b, 0x59, 0x3d, 0x0a, 0x01, 0x70, 0x8b, 0x4b, 0x29, 0xa6, 0xda, 0x10,
    0x68, 0x94, 0xe0, 0x93, 0xed, 0x1b, 0x3f, 0x77, 0xe7, 0x14, 0xef, 0x52,
    0xe7, 0x93, 0xf5, 0xc5, 0xe3, 0x03, 0x0b, 0xd1, 0x05, 0x2c, 0x57, 0xd1,
    0x92, 0x2d, 0xde, 0x0f, 0xab, 0xe2, 0xe8, 0x19, 0x49, 0xbc, 0xcd, 0x61,
    0x2a, 0xb0, 0xdb, 0x09, 0x9f, 0x67, 0xdf, 0x3f, 0x1c, 0x51, 0xbe, 0xbf,
    0x16, 0x5f, 0xd3, 0xba, 0x06, 0x71, 0x1a, 0xd1, 0x95, 0xd0, 0xcf, 0xcd,
    0x4e, 0x15, 0x03, 0x8d, 0x02, 0x81, 0x80, 0x7a, 0x3c, 0xf3, 0x75, 0x25,
    0x70, 0x9a, 0xb7, 0xa7, 0x46, 0x6c, 0x7f, 0xd8, 0xb4, 0x50, 0x2c, 0xda,
    0x25, 0xec, 0xde, 0x2e, 0x4d, 0x68, 0x66, 0xf3, 0x39, 0x96, 0xdc, 0x68,
    0x67, 0xe8, 0x66, 0x72, 0xc4, 0xec, 0x68, 0x57, 0x2c, 0x13, 0xea, 0x7e,
    0xc5, 0xb3, 0x29, 0x97, 0xa2, 0xab, 0xcd, 0x06, 0x9d, 0xc7, 0xec, 0x41,
    0x44, 0x86, 0x61, 0xdb, 0x63, 0x8f, 0xd0, 0x8c, 0xee, 0xde, 0xc7, 0x91,
    0xc5, 0x3a, 0xa3, 0xf7, 0x83, 0x23, 0x6e, 0x8d, 0xc7, 0xe7, 0x43, 0x8f,
    0xf0, 0xda, 0x64, 0xc8, 0x5c, 0x36, 0x9c, 0xf5, 0x17, 0x48, 0x4b, 0x7c,
    0x50, 0xe1, 0x1c, 0xed, 0x23, 0x85, 0xdc, 0x88, 0xdd, 0x60, 0x77, 0x27,
    0xc9, 0x5e, 0x4c, 0xb4, 0x07, 0x9d, 0x18, 0xab, 0x8f, 0x52, 0x38, 0x37,
    0x71, 0xf7, 0x7a, 0x85, 0x93, 0x5f, 0xe1, 0xe9, 0x44, 0x5a, 0xf9, 0xc1,
    0x45, 0xc2, 0xc4,
};

// kTestTime is the fake time at which the test enclave service runs at.
// This value also appears in
// //third_party/cloud_authenticator/test/local_service/src/main.rs.
constexpr uint32_t kTestTime = 1707344402;

// Load an RSA private key from one of the embedded bytestrings, above.
crypto::keypair::PrivateKey GetRSAKey(base::span<const uint8_t> der) {
  std::optional<crypto::keypair::PrivateKey> key =
      crypto::keypair::PrivateKey::FromRSAPrivateKey(der);
  CHECK(key);
  return *key;
}

// Generate a random P-256 key.
crypto::keypair::PrivateKey NewECKey() {
  return crypto::keypair::PrivateKey::GenerateEcP256();
}

std::vector<uint8_t> GetPublicKeyBytes(const crypto::keypair::PrivateKey& key) {
  return crypto::keypair::PublicKey::FromPrivateKey(key)
      .ToUncompressedX962Point();
}

class FakeRecoveryKeyStoreImpl : public FakeRecoveryKeyStore {
 public:
  FakeRecoveryKeyStoreImpl()
      : root_key_(GetRSAKey(kRootRSAPrivateKey)),
        sig_key_(GetRSAKey(kSigRSAPrivateKey)) {
    RegenerateKeys();
  }

  ~FakeRecoveryKeyStoreImpl() override = default;

  base::RepeatingCallback<MaybeResponse(const network::ResourceRequest&)>
  GetCallback() override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    return base::BindRepeating(
        [](base::WeakPtr<FakeRecoveryKeyStoreImpl> impl,
           const network::ResourceRequest& request) -> MaybeResponse {
          // Passing a WeakPtr into BindRepeating is only supported if the
          // function returns void. Thus the WeakPtr is handled directly here.
          if (!impl) {
            return std::nullopt;
          }
          return impl->OnRequest(request);
        },
        weak_ptr_factory_.GetWeakPtr());
  }

  base::span<const trusted_vault_pb::Vault> vaults() const override {
    return vaults_;
  }

  void set_cert_xml_url(std::string url) override {
    cert_xml_url_override_ = std::move(url);
  }

  void set_sig_xml_url(std::string url) override {
    sig_xml_url_override_ = std::move(url);
  }

  void break_cert_xml_file() override { break_cert_xml_file_ = true; }

  void break_sig_xml_file() override { break_sig_xml_file_ = true; }

  void DowngradeCohort() override {
    --recovery_key_store_serial_number_;
    RegenerateKeys();
  }

  void UpgradeCohort() override {
    ++recovery_key_store_serial_number_;
    RegenerateKeys();
  }

  int recovery_key_store_serial_number() override {
    return recovery_key_store_serial_number_;
  }

  std::array<uint8_t, 32> EndpointPrivateKeyFor(
      base::span<const uint8_t> public_key_bytes) const override {
    for (const auto& endpoint_key : endpoint_keys_) {
      if (GetPublicKeyBytes(endpoint_key) != public_key_bytes) {
        continue;
      }
      std::array<uint8_t, 32> result;
      CHECK(BN_bn2bin_padded(
          result.data(), result.size(),
          EC_KEY_get0_private_key(EVP_PKEY_get0_EC_KEY(endpoint_key.key()))));
      return result;
    }
    NOTREACHED() << "Could not find public key";
  }

  std::vector<uint8_t> CurrentEndpointPublicKeyBytes() const override {
    return GetPublicKeyBytes(endpoint_keys_.back());
  }

 private:
  void RegenerateKeys() {
    endpoint_keys_.push_back(NewECKey());

    // To avoid expiration issues, the test enclave runs at a fixed time
    // (`kTestTime`). Certificates are generated to be valid for a week before
    // and after that point.
    const base::Time not_before = base::Time::FromTimeT(kTestTime - 7 * 86400);
    const base::Time not_after = base::Time::FromTimeT(kTestTime + 7 * 86400);

    // This code was used to generate the root certificate that is included
    // in `recovery_key_store.rs`.
#if 0
    static const uint8_t kBasicConstraints[] = {0x55, 0x1d, 0x13};
    static const uint8_t kIsCA[] = {0x30, 0x03, 0x01, 0x01, 0xff};
    std::string root_cert;
    CHECK(net::x509_util::CreateSelfSignedCert(
        endpoint_keys_.back().key(), net::x509_util::DIGEST_SHA256, "CN=Root",
        /*serial_number=*/1, not_before, not_after,
        {net::x509_util::Extension(kBasicConstraints, /*critical=*/true,
                                   kIsCA)},
        &root_cert));
    const std::string root_cert_hex = base::HexEncode(root_cert);
    write(1, root_cert_hex.data(), root_cert_hex.size());
    write(1, "\n", 1);
#endif

    CHECK(net::x509_util::CreateCert(
        sig_key_.key(), net::x509_util::DIGEST_SHA256, "CN=Sig",
        /*serial_number=*/2, not_before, not_after, /*extension_specs=*/{},
        "CN=Root", root_key_.key(), &sig_cert_der_));
    CHECK(net::x509_util::CreateCert(
        endpoint_keys_.back().key(), net::x509_util::DIGEST_SHA256,
        "CN=Endpoint",
        /*serial_number=*/3, not_before, not_after, /*extension_specs=*/{},
        "CN=Root", root_key_.key(), &endpoint_cert_der_));

    certs_xml_ = base::ReplaceStringPlaceholders(
        R"(<?xml version="1.0" encoding="UTF-8"?>
<certificate>
  <metadata>
    <serial>$3</serial>
    <creation-time>$1</creation-time>
    <refresh-interval>2592000</refresh-interval>
    <previous>
      <serial>1</serial>
      <hash>TQudrujnu1I9bdoDaYxGQYuRN/8SwTLjdk6vzYTOkIU=</hash>
    </previous>
  </metadata>
  <intermediates>
  </intermediates>
  <endpoints>
    <cert>$2</cert>
  </endpoints>
</certificate>
)",
        {base::NumberToString(kTestTime),
         base::Base64Encode(endpoint_cert_der_),
         base::NumberToString(recovery_key_store_serial_number_)},
        /*offsets=*/nullptr);

    const std::vector<uint8_t> sig =
        crypto::sign::Sign(crypto::sign::RSA_PKCS1_SHA256, sig_key_,
                           base::as_byte_span(certs_xml_));

    sig_xml_ = base::ReplaceStringPlaceholders(
        R"(<?xml version="1.0" encoding="UTF-8"?>
<signature>
  <chromium-test>true</chromium-test>
  <intermediates>
  </intermediates>
  <certificate>$1</certificate>
  <value>$2</value>
</signature>
)",
        {base::Base64Encode(sig_cert_der_), base::Base64Encode(sig)},
        /*offsets=*/nullptr);
  }

  MaybeResponse OnRequest(const network::ResourceRequest& request) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    std::string cert_xml_url = cert_xml_url_override_;
    if (cert_xml_url.empty()) {
      cert_xml_url = device::enclave::kCertXmlUrlFeature.Get();
    }
    if (request.url == cert_xml_url) {
      if (break_cert_xml_file_) {
        return std::make_pair(net::HTTP_NOT_FOUND, "");
      }
      return std::make_pair(net::HTTP_OK, certs_xml_);
    }

    std::string sig_xml_url = sig_xml_url_override_;
    if (sig_xml_url.empty()) {
      sig_xml_url = device::enclave::kSigXmlUrlFeature.Get();
    }
    if (request.url == sig_xml_url) {
      if (break_sig_xml_file_) {
        return std::make_pair(net::HTTP_NOT_FOUND, "");
      }
      return std::make_pair(net::HTTP_OK, sig_xml_);
    }

    if (request.url ==
        std::string(device::enclave::kRecoveryKeyStoreURL) + "?alt=proto") {
      return ProcessVaultUpdate(request);
    }

    return std::nullopt;
  }

  MaybeResponse ProcessVaultUpdate(
      const network::ResourceRequest& resource_request) {
    const std::vector<network::DataElement>* const elements =
        resource_request.request_body->elements();
    CHECK_EQ(elements->size(), 1u);
    CHECK_EQ(elements->at(0).type(), network::DataElement::Tag::kBytes);
    const std::string_view body =
        elements->at(0).As<network::DataElementBytes>().AsStringPiece();

    trusted_vault_pb::Vault request;
    CHECK(request.ParseFromArray(body.data(), body.size()));
    CHECK(!request.vault_parameters().vault_handle().empty());
    CHECK_EQ(request.vault_parameters().vault_handle()[0], 3 /* GPM PIN */);
    auto existing = std::ranges::find_if(
        vaults_, [&request](const auto& candidate) -> bool {
          const auto& candidate_params = candidate.vault_parameters();
          const auto& request_params = request.vault_parameters();
          return candidate_params.counter_id() == request_params.counter_id() &&
                 candidate_params.vault_handle() ==
                     request_params.vault_handle();
        });
    if (existing != vaults_.end()) {
      *existing = std::move(request);
    } else {
      vaults_.emplace_back(std::move(request));
    }

    trusted_vault_pb::Vault response;
    response.mutable_vault_parameters()->set_vault_handle("test vault handle");
    return std::make_pair(net::HTTP_OK, response.SerializeAsString());
  }

  crypto::keypair::PrivateKey root_key_;
  crypto::keypair::PrivateKey sig_key_;
  std::vector<crypto::keypair::PrivateKey> endpoint_keys_;
  std::string sig_cert_der_;
  std::string endpoint_cert_der_;
  std::string certs_xml_;
  std::string sig_xml_;
  std::vector<trusted_vault_pb::Vault> vaults_;
  std::string cert_xml_url_override_;
  std::string sig_xml_url_override_;
  bool break_cert_xml_file_ = false;
  bool break_sig_xml_file_ = false;
  int recovery_key_store_serial_number_ = 1;
  SEQUENCE_CHECKER(sequence_checker_);
  base::WeakPtrFactory<FakeRecoveryKeyStoreImpl> weak_ptr_factory_{this};
};

}  // namespace

FakeRecoveryKeyStore::~FakeRecoveryKeyStore() = default;

// static
std::unique_ptr<FakeRecoveryKeyStore> FakeRecoveryKeyStore::New() {
  return std::make_unique<FakeRecoveryKeyStoreImpl>();
}
