// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/cloud/test/policy_builder.h"

#include <vector>

#include "base/base64.h"
#include "base/containers/span.h"
#include "base/notreached.h"
#include "base/strings/string_view_util.h"
#include "build/build_config.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "crypto/keypair.h"
#include "crypto/sign.h"
#include "google_apis/gaia/gaia_id.h"

namespace em = enterprise_management;

namespace policy {

namespace {

// Signing key test data in DER-encoded PKCS8 format.
constexpr auto kSigningKey = std::to_array<uint8_t>({
    0x30, 0x82, 0x01, 0x55, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
    0x01, 0x3f, 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
    0xd9, 0xcd, 0xca, 0xcd, 0xc3, 0xea, 0xbe, 0x72, 0x79, 0x1c, 0x29, 0x37,
    0x39, 0x99, 0x1f, 0xd4, 0xb3, 0x0e, 0xf0, 0x7b, 0x78, 0x77, 0x0e, 0x05,
    0x3b, 0x65, 0x34, 0x12, 0x62, 0xaf, 0xa6, 0x8d, 0x33, 0xce, 0x78, 0xf8,
    0x47, 0x05, 0x1d, 0x98, 0xaa, 0x1b, 0x1f, 0x50, 0x05, 0x5b, 0x3c, 0x19,
    0x3f, 0x80, 0x83, 0x63, 0x63, 0x3a, 0xec, 0xcb, 0x2e, 0x90, 0x4f, 0xf5,
    0x26, 0x76, 0xf1, 0xd5, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x64,
    0x29, 0xc2, 0xd9, 0x6b, 0xfe, 0xf9, 0x84, 0x75, 0x73, 0xe0, 0xf4, 0x77,
    0xb5, 0x96, 0xb0, 0xdf, 0x83, 0xc0, 0x4e, 0x57, 0xf1, 0x10, 0x6e, 0x91,
    0x89, 0x12, 0x30, 0x5e, 0x57, 0xff, 0x14, 0x59, 0x5f, 0x18, 0x86, 0x4e,
    0x4b, 0x17, 0x56, 0xfc, 0x8d, 0x40, 0xdd, 0x74, 0x65, 0xd3, 0xff, 0x67,
    0x64, 0xcb, 0x9c, 0xb4, 0x14, 0x8a, 0x06, 0xb7, 0x13, 0x45, 0x94, 0x16,
    0x7d, 0x3f, 0xe1, 0x02, 0x21, 0x00, 0xf6, 0x0f, 0x31, 0x6d, 0x06, 0xcc,
    0x3b, 0xa0, 0x44, 0x1f, 0xf5, 0xc2, 0x45, 0x2b, 0x10, 0x6c, 0xf9, 0x6f,
    0x8f, 0x87, 0x3d, 0xc0, 0x3b, 0x55, 0x13, 0x37, 0x80, 0xcd, 0x9f, 0xe1,
    0xb7, 0xd9, 0x02, 0x21, 0x00, 0xe2, 0x9a, 0x5f, 0xbf, 0x95, 0x74, 0xb5,
    0x7a, 0x6a, 0xa6, 0x97, 0xbd, 0x75, 0x8c, 0x97, 0x18, 0x24, 0xd6, 0x09,
    0xcd, 0xdc, 0xb5, 0x94, 0xbf, 0xe2, 0x78, 0xaa, 0x20, 0x47, 0x9f, 0x68,
    0x5d, 0x02, 0x21, 0x00, 0xaf, 0x8f, 0x97, 0x8c, 0x5a, 0xd5, 0x4d, 0x95,
    0xc4, 0x05, 0xa9, 0xab, 0xba, 0xfe, 0x46, 0xf1, 0xf9, 0xe7, 0x07, 0x59,
    0x4f, 0x4d, 0xe1, 0x07, 0x8a, 0x76, 0x87, 0x88, 0x2f, 0x13, 0x35, 0xc1,
    0x02, 0x20, 0x24, 0xc3, 0xd9, 0x2f, 0x13, 0x47, 0x99, 0x3e, 0x20, 0x59,
    0xa1, 0x1a, 0xeb, 0x1c, 0x81, 0x53, 0x38, 0x7e, 0xc5, 0x9e, 0x71, 0xe5,
    0xc0, 0x19, 0x95, 0xdb, 0xef, 0xf6, 0x46, 0xc8, 0x95, 0x3d, 0x02, 0x21,
    0x00, 0xaa, 0xb1, 0xff, 0x8a, 0xa2, 0xb2, 0x2b, 0xef, 0x9a, 0x83, 0x3f,
    0xc5, 0xbc, 0xd4, 0x6a, 0x07, 0xe8, 0xc7, 0x0b, 0x2e, 0xd4, 0x0f, 0xf8,
    0x98, 0x68, 0xe1, 0x04, 0xa8, 0x92, 0xd0, 0x10, 0xaa,
});

// SHA256 signature of kSigningKey for "example.com" domain.
constexpr auto kSigningKeySignature = std::to_array<uint8_t>(
    {0x54, 0x20, 0x93, 0x27, 0x2e, 0x7e, 0xae, 0xd5, 0xe5, 0x0f, 0x22, 0x56,
     0x09, 0x0b, 0x38, 0x40, 0x05, 0x0f, 0x25, 0x80, 0xbe, 0xd8, 0xe4, 0x9e,
     0xf0, 0x54, 0xca, 0xc8, 0x04, 0x72, 0x66, 0xcd, 0x1d, 0x6a, 0xb1, 0x04,
     0x00, 0x61, 0x19, 0xc4, 0xee, 0x2a, 0x59, 0xca, 0x65, 0xd3, 0x05, 0xd9,
     0x62, 0x87, 0x98, 0xa9, 0xb2, 0x2f, 0x06, 0xf7, 0x25, 0xcb, 0x66, 0xf4,
     0x17, 0xf6, 0x6f, 0x33, 0xfc, 0x0c, 0xd9, 0x4a, 0x06, 0xff, 0x82, 0xa4,
     0x44, 0x27, 0xba, 0xd0, 0x8a, 0xd0, 0x88, 0x06, 0x63, 0x15, 0x99, 0xdc,
     0x13, 0xb1, 0xf7, 0x86, 0x4a, 0xa7, 0xfb, 0x76, 0xd5, 0x27, 0xb9, 0x09,
     0x11, 0x6b, 0xe5, 0x09, 0x81, 0x33, 0x2f, 0x44, 0x14, 0x11, 0xfc, 0x24,
     0x42, 0xd4, 0x42, 0x50, 0x9b, 0x73, 0xd3, 0xa2, 0x1e, 0xba, 0xb2, 0x94,
     0x0f, 0xc7, 0x3e, 0xac, 0x9c, 0x34, 0xfa, 0x2b, 0x60, 0x22, 0xe3, 0x46,
     0xd7, 0x7c, 0x14, 0x9c, 0xd3, 0x08, 0xd5, 0x24, 0x5f, 0x72, 0x0a, 0xec,
     0x82, 0x10, 0x22, 0x58, 0xbd, 0x6c, 0x8f, 0x45, 0xae, 0xf9, 0x33, 0x68,
     0xe1, 0x16, 0x6e, 0x3a, 0x41, 0x3d, 0x84, 0x0a, 0xc0, 0xa0, 0xb4, 0x06,
     0x3a, 0xb5, 0x64, 0x16, 0x1f, 0x41, 0x19, 0x3b, 0x02, 0x28, 0xf7, 0x21,
     0x6c, 0x1e, 0x64, 0x9e, 0xa3, 0xa9, 0xf5, 0x33, 0x1c, 0x05, 0x28, 0xfc,
     0x6f, 0x2e, 0xb0, 0x70, 0xcd, 0xf8, 0x36, 0x33, 0xa3, 0xc6, 0x06, 0x7b,
     0x82, 0x56, 0xe4, 0x75, 0x3e, 0x1c, 0xb1, 0xf3, 0x60, 0xfa, 0x19, 0x66,
     0x8c, 0x78, 0xe8, 0x11, 0xa3, 0xc2, 0x30, 0xf4, 0x9b, 0x42, 0x90, 0xe1,
     0xc5, 0x0c, 0x4a, 0x97, 0xf2, 0xce, 0xc9, 0x08, 0xa1, 0xf9, 0xb2, 0x3c,
     0xf1, 0x30, 0x9c, 0x36, 0xa1, 0x00, 0xd7, 0xf3, 0xee, 0x42, 0x40, 0xbd,
     0xe4, 0x87, 0x95, 0x5d});

// SHA256 signature of kSigningKey for "gmail.com" domain when the testing
// verification key is used.
constexpr auto kSigningKeySignatureForChild = std::to_array<uint8_t>(
    {0x09, 0xb9, 0x96, 0x69, 0x79, 0xa6, 0xd7, 0x3c, 0x27, 0xdc, 0x47, 0x2b,
     0x75, 0x85, 0x01, 0x2f, 0x75, 0xa5, 0x4d, 0xed, 0x1d, 0xde, 0x9e, 0xae,
     0xfd, 0x45, 0xbf, 0x5e, 0x25, 0xca, 0xe2, 0x0c, 0x77, 0x10, 0x7d, 0x51,
     0xa3, 0xea, 0x87, 0x78, 0x48, 0x14, 0xeb, 0xe3, 0x83, 0x0a, 0xce, 0xb3,
     0x1b, 0xfb, 0x96, 0xfa, 0x36, 0x11, 0xdf, 0x65, 0xa9, 0xd6, 0xaa, 0x00,
     0x54, 0x7c, 0x99, 0x28, 0x70, 0x7c, 0xf7, 0xdf, 0xa3, 0xb3, 0xc9, 0x0c,
     0xcf, 0x48, 0x3b, 0xac, 0x0b, 0x46, 0x7e, 0xd8, 0x3d, 0x6d, 0xe1, 0x65,
     0xc5, 0x67, 0x9a, 0xd8, 0xaf, 0x0c, 0x4b, 0x12, 0x65, 0x73, 0xff, 0xa6,
     0x85, 0xdb, 0xea, 0xe1, 0x37, 0x41, 0xb5, 0xc0, 0x21, 0x09, 0x6c, 0xf2,
     0x56, 0x0d, 0x39, 0x58, 0x54, 0x15, 0xd4, 0x38, 0xa5, 0xe7, 0x01, 0x2c,
     0x31, 0xf8, 0x95, 0x4e, 0xf5, 0x9a, 0xa2, 0x14, 0xbf, 0x06, 0xb1, 0x28,
     0xa2, 0x7a, 0x62, 0x2f, 0xd9, 0x60, 0xd3, 0x19, 0x50, 0xb2, 0x90, 0x4d,
     0xe2, 0x11, 0x19, 0x53, 0xb5, 0x55, 0xc1, 0xaf, 0x12, 0x23, 0x06, 0x6c,
     0x55, 0x24, 0x85, 0x20, 0x24, 0xe2, 0xf7, 0xb8, 0xac, 0x7e, 0x69, 0x47,
     0x1c, 0x0e, 0xc4, 0x3a, 0xb5, 0xc1, 0x89, 0x6c, 0x32, 0xcb, 0xca, 0x1d,
     0x97, 0x4b, 0x64, 0x69, 0x5f, 0x6c, 0xd0, 0xb0, 0xa8, 0x77, 0x00, 0xc6,
     0x6c, 0xdd, 0x93, 0x01, 0x2c, 0x57, 0xf3, 0xcf, 0xd6, 0xa1, 0xf5, 0xbe,
     0x95, 0x84, 0x8d, 0x44, 0xa2, 0xa6, 0x25, 0xdb, 0x5a, 0x49, 0x05, 0x18,
     0x59, 0x6c, 0x95, 0x99, 0x4f, 0xbb, 0x42, 0x7c, 0x42, 0x20, 0x32, 0xc1,
     0x11, 0x3e, 0xaa, 0x85, 0xe8, 0x78, 0x43, 0xe0, 0x88, 0xdd, 0x8b, 0x45,
     0xdc, 0xc7, 0xf7, 0xbb, 0x09, 0x3a, 0x54, 0xc0, 0x32, 0x79, 0x69, 0x2a,
     0x05, 0xcd, 0x42, 0x85});

// New signing key test data in DER-encoded PKCS8 format.
constexpr auto kNewSigningKey = std::to_array<uint8_t>({
    0x30, 0x82, 0x01, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
    0x01, 0x3e, 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
    0x99, 0x98, 0x6b, 0x79, 0x5d, 0x38, 0x33, 0x79, 0x27, 0x0a, 0x2e, 0xb0,
    0x89, 0xba, 0xf8, 0xf6, 0x80, 0xde, 0xb0, 0x79, 0xf2, 0xd4, 0x6d, 0xf7,
    0x3c, 0xa3, 0x97, 0xf6, 0x4a, 0x3c, 0xa5, 0xcc, 0x40, 0x8a, 0xef, 0x59,
    0xaa, 0xc2, 0x82, 0x8f, 0xbc, 0x0d, 0x5b, 0x63, 0xc6, 0xaa, 0x72, 0xe2,
    0xf3, 0x57, 0xdd, 0x74, 0x00, 0xb0, 0x42, 0xd6, 0x27, 0xe7, 0x17, 0x61,
    0x0a, 0xdc, 0xc1, 0xf7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x34,
    0xcf, 0xc9, 0xb4, 0x73, 0x2f, 0x0d, 0xd3, 0xcc, 0x6e, 0x9d, 0xdb, 0x29,
    0xa0, 0x56, 0x56, 0x3b, 0xbd, 0x56, 0x24, 0xb8, 0x2f, 0xfe, 0x97, 0x92,
    0x0c, 0x16, 0x06, 0x23, 0x44, 0x73, 0x25, 0x1d, 0x65, 0xf4, 0xda, 0x77,
    0xe7, 0x91, 0x2e, 0x91, 0x05, 0x10, 0xc1, 0x1b, 0x39, 0x5e, 0xb2, 0xf7,
    0xbd, 0x14, 0x19, 0xcb, 0x6b, 0xc3, 0xa9, 0xe8, 0x91, 0xf7, 0xa7, 0xa9,
    0x90, 0x08, 0x51, 0x02, 0x21, 0x00, 0xcc, 0x9e, 0x03, 0x54, 0x8f, 0x24,
    0xde, 0x90, 0x25, 0xec, 0x21, 0xaf, 0xe6, 0x27, 0x2a, 0x16, 0x42, 0x74,
    0xda, 0xf8, 0x84, 0xc4, 0x8c, 0x1e, 0x86, 0x12, 0x04, 0x5c, 0x17, 0x01,
    0xea, 0x9d, 0x02, 0x21, 0x00, 0xc0, 0x2a, 0x6c, 0xe9, 0xa1, 0x1a, 0x41,
    0x11, 0x94, 0x50, 0xf7, 0x1a, 0xd3, 0xbc, 0xf3, 0xa2, 0xf8, 0x46, 0xbc,
    0x26, 0x77, 0x78, 0xef, 0xc0, 0x54, 0xec, 0x22, 0x3f, 0x2c, 0x57, 0xe0,
    0xa3, 0x02, 0x20, 0x31, 0xf2, 0xc8, 0xa1, 0x55, 0xa8, 0x0c, 0x64, 0x67,
    0xbd, 0x72, 0xa3, 0xbb, 0xad, 0x07, 0xcb, 0x13, 0x41, 0xef, 0x4a, 0x07,
    0x2e, 0xeb, 0x7d, 0x70, 0x00, 0xe9, 0xeb, 0x88, 0xfa, 0x40, 0xc9, 0x02,
    0x20, 0x3a, 0xe0, 0xc4, 0xde, 0x10, 0x6e, 0x6a, 0xe1, 0x68, 0x00, 0x26,
    0xb6, 0x21, 0x8a, 0x13, 0x5c, 0x2b, 0x96, 0x00, 0xb0, 0x08, 0x8b, 0x15,
    0x6a, 0x68, 0x9a, 0xb1, 0x23, 0x8a, 0x02, 0xa2, 0xe1, 0x02, 0x21, 0x00,
    0xa3, 0xf2, 0x2d, 0x55, 0xc1, 0x6d, 0x40, 0xfa, 0x1d, 0xf7, 0xba, 0x86,
    0xef, 0x50, 0x98, 0xfc, 0xee, 0x09, 0xcc, 0xe7, 0x22, 0xb9, 0x4e, 0x80,
    0x32, 0x1a, 0x6b, 0xb3, 0x5f, 0x35, 0xbd, 0xf3,
});

// SHA256 signature of kNewSigningKey for "example.com" domain.
constexpr auto kNewSigningKeySignature = std::to_array<uint8_t>(
    {0x66, 0xc0, 0x12, 0xf5, 0x80, 0xe8, 0x6a, 0xe1, 0x94, 0x37, 0xed, 0x9b,
     0xf7, 0x8b, 0x35, 0x05, 0x36, 0xad, 0xc8, 0x18, 0x2e, 0x0a, 0x28, 0x13,
     0x32, 0x6b, 0x97, 0xd7, 0x9b, 0x1b, 0xda, 0x28, 0xa0, 0x3b, 0x17, 0xd9,
     0x05, 0xe2, 0xd1, 0x02, 0x5f, 0x69, 0xc4, 0xf9, 0xb8, 0xeb, 0x0b, 0xc5,
     0xea, 0x5a, 0x67, 0x2d, 0xc0, 0x68, 0x12, 0x7f, 0xa6, 0x0b, 0xf3, 0x5d,
     0x39, 0x84, 0x3a, 0x47, 0xa5, 0x16, 0x0e, 0x74, 0x35, 0x36, 0xdb, 0xf9,
     0xef, 0xd1, 0x40, 0xb6, 0xd8, 0x89, 0xe9, 0x4c, 0xab, 0x26, 0xa3, 0x94,
     0x68, 0x7f, 0xc0, 0x0f, 0xf3, 0xe6, 0xd2, 0x55, 0x01, 0x3f, 0x45, 0x5f,
     0x48, 0x68, 0x49, 0xcb, 0x95, 0x4c, 0x00, 0x6c, 0x38, 0x95, 0x80, 0x89,
     0x29, 0xd5, 0xdc, 0x0b, 0xfc, 0x52, 0x25, 0x82, 0xb1, 0xa8, 0xe3, 0x36,
     0xd8, 0x0a, 0x82, 0xd7, 0x73, 0xd0, 0xcf, 0x4f, 0x29, 0xcc, 0x5f, 0x58,
     0x6a, 0x0c, 0x33, 0xb5, 0x55, 0x65, 0xd4, 0xbb, 0x30, 0x18, 0xc5, 0x53,
     0x6f, 0xf2, 0xa3, 0xd1, 0xff, 0x23, 0xb8, 0x5e, 0x3f, 0xe9, 0xc7, 0xe5,
     0x67, 0x4a, 0xfe, 0x94, 0x5a, 0xe8, 0x76, 0x81, 0xef, 0x4f, 0xed, 0xdf,
     0x3d, 0xdf, 0xd4, 0x52, 0x1e, 0x14, 0x29, 0xc6, 0x29, 0xe7, 0x6e, 0xd8,
     0xe4, 0xcb, 0xd1, 0xf1, 0x26, 0x6e, 0x43, 0xa1, 0x02, 0x26, 0xc5, 0x25,
     0x43, 0x9e, 0x7b, 0x5d, 0x72, 0x44, 0xd6, 0xe3, 0x36, 0x67, 0xfe, 0xfa,
     0x12, 0xb9, 0xc2, 0xe3, 0x37, 0x3a, 0x34, 0x87, 0xd3, 0xf1, 0x24, 0x60,
     0x11, 0xb7, 0xb1, 0x68, 0xcf, 0xa3, 0xfd, 0x0c, 0xce, 0x07, 0x9b, 0xf7,
     0xee, 0x37, 0xa2, 0xaf, 0x41, 0x5b, 0xa7, 0x71, 0x8a, 0xd1, 0x7d, 0xf6,
     0xd6, 0x58, 0xff, 0x48, 0xf3, 0xcd, 0x67, 0x2c, 0x3b, 0xbb, 0x7d, 0xef,
     0x6b, 0xa2, 0x5b, 0xf3});

// The public part of the key to be used by CloudPolicyValidator for
// checking the new public key data signature. This is replacement
// for baked in the code |verification_key_| used by the validator.
// Since we have the private part of this key, in kVerificationPrivateKey
// new signatures can be generated that will pass the checks from
// CloudPolicyValidator.
constexpr auto kVerificationPublicKey = std::to_array<uint8_t>(
    {0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
     0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
     0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb2, 0x68, 0xc7,
     0xcd, 0xd1, 0xd5, 0xac, 0xe1, 0xb9, 0xfc, 0x97, 0xbd, 0x94, 0x5e, 0xef,
     0x1b, 0xf6, 0x0d, 0xea, 0x47, 0xc8, 0xb0, 0x8f, 0xa2, 0xb2, 0x17, 0xa3,
     0xd5, 0x3f, 0x1d, 0x40, 0x0c, 0x26, 0xa6, 0xf3, 0xc4, 0xb9, 0xc6, 0xbe,
     0x8c, 0x79, 0xe1, 0x92, 0x26, 0xd9, 0xae, 0x2d, 0xdb, 0x3f, 0xbb, 0x62,
     0xda, 0x1b, 0x28, 0x70, 0x1d, 0x60, 0x65, 0xb9, 0x70, 0x21, 0x0e, 0x57,
     0xad, 0x28, 0x52, 0x17, 0x74, 0xf2, 0x8d, 0x86, 0x86, 0x47, 0xad, 0xd5,
     0x8c, 0xfe, 0xb3, 0x71, 0x81, 0x9f, 0x74, 0x6b, 0x66, 0x4c, 0x32, 0x8d,
     0xb4, 0x45, 0x5a, 0xe1, 0x06, 0xc2, 0x45, 0xc1, 0xa5, 0xcd, 0x6d, 0x2d,
     0xb2, 0x43, 0x63, 0xd8, 0xaa, 0xb9, 0x1b, 0xa5, 0x53, 0x9b, 0xe7, 0x75,
     0xc0, 0x0d, 0x7c, 0xc8, 0xdc, 0x54, 0x4b, 0xde, 0xb6, 0x7b, 0x6e, 0x8f,
     0xfc, 0xfc, 0x39, 0xbd, 0x06, 0x32, 0xfc, 0x6f, 0x47, 0xab, 0x68, 0xab,
     0x66, 0x98, 0x50, 0xe0, 0x94, 0xfb, 0x7c, 0x15, 0x03, 0xf2, 0xa5, 0xd6,
     0x61, 0xa7, 0x70, 0x57, 0x49, 0x8f, 0x26, 0x75, 0x30, 0x99, 0xa3, 0xd7,
     0xa3, 0xd0, 0xb8, 0x26, 0xd9, 0xeb, 0x7f, 0xff, 0xc6, 0xa0, 0x62, 0x0b,
     0xc5, 0x38, 0xd6, 0x34, 0xc5, 0xaf, 0xb3, 0x93, 0x09, 0xaf, 0x37, 0xfc,
     0x98, 0x80, 0xf2, 0xf2, 0x5b, 0x5d, 0xce, 0xa5, 0x0e, 0xc7, 0x44, 0x31,
     0x87, 0x24, 0x00, 0xf7, 0xcd, 0xdd, 0xe1, 0xc8, 0xfe, 0xde, 0x46, 0x68,
     0x93, 0xe9, 0x3c, 0x1f, 0xad, 0x8a, 0xa6, 0xf5, 0x5f, 0x1f, 0x0c, 0x6a,
     0xa1, 0x1c, 0x39, 0xe1, 0xb5, 0x71, 0xa3, 0x4f, 0x2f, 0x62, 0x48, 0xcd,
     0x52, 0x65, 0x58, 0x7b, 0x84, 0x40, 0x31, 0x14, 0x4e, 0x01, 0x58, 0x4e,
     0x89, 0x9b, 0x3a, 0x04, 0x3e, 0x0f, 0x9d, 0x66, 0xf0, 0x97, 0x66, 0xcd,
     0x8f, 0x02, 0x03, 0x01, 0x00, 0x01});

// The signature for the new public key verification data corresponding
// to public_key = policy_signing_key, domain = kFakeDomain and
// new_public_key_version = kNewPublicKeyVersion blob that is signed with
// kVerificationPublicKey.
constexpr auto kPublicKeyVerificationDataSignature = std::to_array<uint8_t>(
    {0x92, 0x70, 0xb0, 0x64, 0x44, 0x88, 0x9c, 0x3d, 0x51, 0x32, 0xc1, 0x62,
     0x1f, 0x74, 0xff, 0x3f, 0x6f, 0x1a, 0x6d, 0x56, 0x33, 0xbe, 0x4e, 0xd6,
     0x4e, 0xf0, 0x14, 0xd8, 0x21, 0xf5, 0x77, 0x89, 0x54, 0x56, 0x49, 0xbd,
     0xaf, 0x18, 0xc1, 0x0b, 0xd3, 0xbd, 0xe9, 0x2a, 0x01, 0xdf, 0x94, 0x87,
     0xab, 0x6b, 0xb8, 0xdb, 0x1e, 0x28, 0xaa, 0x62, 0xa6, 0x3e, 0x0e, 0x7b,
     0x9d, 0xc8, 0xe4, 0x54, 0x1f, 0x0e, 0xa8, 0xc6, 0xc6, 0x35, 0xd4, 0x13,
     0x78, 0xdd, 0x7e, 0xa3, 0xbf, 0xbb, 0x68, 0x4c, 0xaf, 0x36, 0x9e, 0x51,
     0x4e, 0xa8, 0x0e, 0xf7, 0x81, 0xad, 0x8f, 0x5d, 0xaf, 0x18, 0xe6, 0x5c,
     0x77, 0xdc, 0xda, 0xa7, 0x9a, 0x7f, 0x10, 0x60, 0x7a, 0x23, 0xba, 0xf8,
     0x0f, 0xc6, 0x12, 0xa6, 0xea, 0x82, 0xa1, 0x7b, 0x6a, 0xe0, 0x79, 0x4e,
     0x95, 0x40, 0xd5, 0x7f, 0x98, 0x3b, 0xc5, 0xa3, 0x15, 0xd6, 0xba, 0x74,
     0x8f, 0x22, 0x6a, 0x2b, 0x32, 0x7a, 0x8f, 0x3a, 0x25, 0xc4, 0x40, 0x7b,
     0x34, 0xe4, 0x40, 0xd5, 0x4a, 0xa5, 0x5f, 0xc2, 0x85, 0x53, 0x06, 0x9f,
     0xe7, 0x95, 0x95, 0x48, 0x0c, 0xff, 0xf7, 0x3f, 0x1c, 0xce, 0xd4, 0xcd,
     0x7a, 0xc7, 0x7d, 0x81, 0x19, 0x55, 0x1f, 0x26, 0x59, 0xf1, 0xa7, 0xea,
     0x20, 0x8d, 0x62, 0x51, 0xa6, 0x69, 0x79, 0xbe, 0xd7, 0x75, 0x52, 0xd9,
     0x83, 0xe3, 0xd8, 0x77, 0x69, 0x04, 0x27, 0xc0, 0xf5, 0x70, 0x8c, 0xf9,
     0xc7, 0x56, 0x66, 0xcc, 0x98, 0x43, 0x3f, 0x5e, 0xc6, 0x62, 0x37, 0x22,
     0xcc, 0x11, 0x96, 0x5c, 0xe8, 0x1f, 0x18, 0x69, 0x4f, 0x8f, 0xf1, 0x1d,
     0xbe, 0x35, 0x4f, 0xd8, 0x66, 0x30, 0x36, 0x83, 0xa3, 0x05, 0x53, 0xa0,
     0xe6, 0x8e, 0x42, 0xf1, 0xd9, 0x26, 0x1f, 0x90, 0x0e, 0xc8, 0x81, 0x75,
     0x23, 0x23, 0xde, 0xe1});

// The version of new public key for verification data
const int32_t kNewPublicKeyVersion = 1;

const char user_affiliation_id1[] = "id1";
const char user_affiliation_id2[] = "id2";

std::string ConvertPublicKeyToString(const std::vector<uint8_t>& public_key) {
  return std::string(reinterpret_cast<const char*>(public_key.data()),
                     public_key.size());
}

// Produces |key|'s signature over |data| and stores it in |signature|.
void SignData(const std::string& data,
              const crypto::keypair::PrivateKey& key,
              std::string* const signature,
              em::PolicyFetchRequest::SignatureType signature_type) {
  crypto::sign::SignatureKind kind;
  switch (signature_type) {
    case em::PolicyFetchRequest::SHA1_RSA:
      kind = crypto::sign::SignatureKind::RSA_PKCS1_SHA1;
      break;
    case em::PolicyFetchRequest::SHA256_RSA:
      kind = crypto::sign::SignatureKind::RSA_PKCS1_SHA256;
      break;
    default:
      // `em::PolicyFetchRequest::NONE` indicates unsigned blobs.
      // Crash is OK here.
      NOTREACHED();
  }

  std::vector<uint8_t> signature_bytes =
      crypto::sign::Sign(kind, key, base::as_byte_span(data));
  signature->assign(base::as_string_view(signature_bytes));
}

}  // namespace

// Constants used as dummy data for filling the PolicyData protobuf.
// LINT.IfChange
const GaiaId::Literal PolicyBuilder::kFakeGaiaId("0000111111");
const char PolicyBuilder::kFakeUsername[] = "username@example.com";
// LINT.ThenChange(/chrome/test/base/fake_gaia_mixin.cc)

const char PolicyBuilder::kFakeDeviceId[] = "device-id";
const char PolicyBuilder::kFakeDomain[] = "example.com";
const char PolicyBuilder::kFakeMachineName[] = "machine-name";
const char PolicyBuilder::kFakePolicyType[] = "policy type";
const int PolicyBuilder::kFakePublicKeyVersion = 17;
const int64_t PolicyBuilder::kFakeTimestamp = 365LL * 24 * 60 * 60 * 1000;
const char PolicyBuilder::kFakeToken[] = "token";
const char PolicyBuilder::kFakeServiceAccountIdentity[] = "robot4test@g.com";

PolicyBuilder::PolicyBuilder() {
  SetDefaultSigningKey();
  CreatePolicyData();
  policy_data_->set_policy_type(kFakePolicyType);
  policy_data_->set_timestamp(kFakeTimestamp);
  policy_data_->set_gaia_id(kFakeGaiaId.ToString());
  policy_data_->set_request_token(kFakeToken);
  policy_data_->set_machine_name(kFakeMachineName);
  policy_data_->set_public_key_version(kFakePublicKeyVersion);
  policy_data_->set_username(kFakeUsername);
  policy_data_->set_device_id(kFakeDeviceId);
  policy_data_->set_state(em::PolicyData::ACTIVE);
  policy_data_->set_service_account_identity(kFakeServiceAccountIdentity);
  policy_data_->set_cec_enabled(true);
  policy_data_->set_command_invalidation_topic("fake-topic");
  policy_data_->add_user_affiliation_ids(user_affiliation_id1);
  policy_data_->add_user_affiliation_ids(user_affiliation_id2);
}

PolicyBuilder::~PolicyBuilder() = default;

std::optional<crypto::keypair::PrivateKey> PolicyBuilder::GetSigningKey()
    const {
  if (raw_signing_key_.empty())
    return std::nullopt;
  return crypto::keypair::PrivateKey::FromPrivateKeyInfo(raw_signing_key_);
}

void PolicyBuilder::SetSigningKey(const crypto::keypair::PrivateKey& key) {
  raw_signing_key_ = key.ToPrivateKeyInfo();
}

void PolicyBuilder::SetDefaultSigningKey() {
  raw_signing_key_.assign(std::begin(kSigningKey), std::end(kSigningKey));
}

void PolicyBuilder::UnsetSigningKey() {
  raw_signing_key_.clear();
}

std::optional<crypto::keypair::PrivateKey> PolicyBuilder::GetNewSigningKey()
    const {
  if (raw_new_signing_key_.empty())
    return std::nullopt;
  return crypto::keypair::PrivateKey::FromPrivateKeyInfo(raw_new_signing_key_);
}

void PolicyBuilder::SetDefaultNewSigningKey() {
  raw_new_signing_key_.assign(std::begin(kNewSigningKey),
                              std::end(kNewSigningKey));
  raw_new_signing_key_signature_ = GetTestOtherSigningKeySignature();
}

void PolicyBuilder::UnsetNewSigningKey() {
  raw_new_signing_key_.clear();
  raw_new_signing_key_signature_.clear();
}

void PolicyBuilder::SetDefaultInitialSigningKey() {
  raw_new_signing_key_.assign(std::begin(kSigningKey), std::end(kSigningKey));
  raw_new_signing_key_signature_ = GetTestSigningKeySignature();
  UnsetSigningKey();
}

void PolicyBuilder::Build() {
  // Start with a clean state.
  policy_.clear_policy_data();
  policy_.clear_policy_data_signature();

  if (!policy_data_) {
    return;
  }

  // Serialize the policy data.
  CHECK(policy_data_->SerializeToString(policy_.mutable_policy_data()));

  // Signature type of `NONE` indicates an unsigned blob.
  if (signature_type_ == em::PolicyFetchRequest::NONE) {
    return;
  }

  // Rotate/install keys.
  const std::optional<crypto::keypair::PrivateKey> new_signing_key =
      GetNewSigningKey();
  const std::optional<crypto::keypair::PrivateKey> current_signing_key =
      GetSigningKey();
  std::optional<crypto::keypair::PrivateKey> key_to_sign_policy_data_with;
  if (new_signing_key.has_value()) {
    // Add the new public key.
    policy_.set_new_public_key(
        base::as_string_view(new_signing_key->ToSubjectPublicKeyInfo()));
    policy_.set_new_public_key_verification_signature_deprecated(
        raw_new_signing_key_signature_);

    // Add the new public key verification data.
    em::PublicKeyVerificationData new_signing_key_verification_data;

    // Need to set as public key the key that will be used to validate the
    // policy. So, it's the new public key.
    new_signing_key_verification_data.set_new_public_key(
        base::as_string_view(new_signing_key->ToSubjectPublicKeyInfo()));
    new_signing_key_verification_data.set_domain(kFakeDomain);
    new_signing_key_verification_data.set_new_public_key_version(
        kNewPublicKeyVersion);
    std::string new_signing_key_verification_data_as_string;
    CHECK(new_signing_key_verification_data.SerializeToString(
        &new_signing_key_verification_data_as_string));

    // Note that protobuf serialization doesn't guarantee the same output,
    // but the alternative of putting a hard-coded string here will make
    // the code less clear to read. So, we prefer to use this option with
    // the risk that in the future the signature given by this file will
    // fail.
    policy_.set_new_public_key_verification_data(
        new_signing_key_verification_data_as_string);
    std::string new_signing_key_verification_data_signature =
        GetPublicKeyVerificationDataSignature();
    policy_.set_new_public_key_verification_data_signature(
        new_signing_key_verification_data_signature);

    // The new public key must be signed by the current key in the event of key
    // rotation.
    if (current_signing_key) {
      SignData(policy_.new_public_key(), *current_signing_key,
               policy_.mutable_new_public_key_signature(), signature_type_);
    }

    key_to_sign_policy_data_with = new_signing_key;
  } else {
    // No new signing key, so clear the old public key (this allows us to
    // reuse the same PolicyBuilder to build multiple policy blobs).
    policy_.clear_new_public_key();
    policy_.clear_new_public_key_verification_signature_deprecated();
    policy_.clear_new_public_key_signature();
    policy_.clear_new_public_key_verification_data();
    policy_.clear_new_public_key_verification_data_signature();

    if (current_signing_key.has_value()) {
      key_to_sign_policy_data_with = current_signing_key;
    }
  }

  // Sign if possible.
  if (key_to_sign_policy_data_with.has_value()) {
    SignData(policy_.policy_data(), *key_to_sign_policy_data_with,
             policy_.mutable_policy_data_signature(), signature_type_);

    // Generate signatures if applicable.
    policy_.set_policy_data_signature_type(signature_type_);
  } else {
    policy_data_->clear_public_key_version();
  }

  return;
}

std::string PolicyBuilder::GetBlob() const {
  return policy_.SerializeAsString();
}

std::unique_ptr<em::PolicyFetchResponse> PolicyBuilder::GetCopy() const {
  return std::make_unique<em::PolicyFetchResponse>(policy_);
}

// static
crypto::keypair::PrivateKey PolicyBuilder::CreateTestSigningKey() {
  return *crypto::keypair::PrivateKey::FromPrivateKeyInfo(kSigningKey);
}

// static
crypto::keypair::PrivateKey PolicyBuilder::CreateTestOtherSigningKey() {
  return *crypto::keypair::PrivateKey::FromPrivateKeyInfo(kNewSigningKey);
}

// static
std::string PolicyBuilder::GetEncodedPolicyVerificationKey() {
  return base::Base64Encode(kVerificationPublicKey);
}

// static
std::string PolicyBuilder::GetPublicKeyVerificationDataSignature() {
  return std::string(base::as_string_view(kPublicKeyVerificationDataSignature));
}

// static
std::string PolicyBuilder::GetTestSigningKeySignature() {
  return std::string(base::as_string_view(kSigningKeySignature));
}

std::string PolicyBuilder::GetTestSigningKeySignatureForChild() {
  return std::string(base::as_string_view(kSigningKeySignatureForChild));
}

// static
std::string PolicyBuilder::GetTestOtherSigningKeySignature() {
  return std::string(base::as_string_view(kNewSigningKeySignature));
}

std::vector<uint8_t> PolicyBuilder::GetPublicSigningKey() const {
  const auto key = GetSigningKey();
  return key ? key->ToSubjectPublicKeyInfo() : std::vector<uint8_t>();
}

std::vector<uint8_t> PolicyBuilder::GetPublicNewSigningKey() const {
  auto key = GetNewSigningKey();
  return key ? key->ToSubjectPublicKeyInfo() : std::vector<uint8_t>();
}

// static
std::vector<uint8_t> PolicyBuilder::GetPublicTestKey() {
  return CreateTestSigningKey().ToSubjectPublicKeyInfo();
}

// static
std::vector<uint8_t> PolicyBuilder::GetPublicTestOtherKey() {
  return CreateTestOtherSigningKey().ToSubjectPublicKeyInfo();
}

std::string PolicyBuilder::GetPublicSigningKeyAsString() const {
  return std::string(base::as_string_view(GetPublicSigningKey()));
}

std::string PolicyBuilder::GetPublicNewSigningKeyAsString() const {
  return std::string(base::as_string_view(GetPublicNewSigningKey()));
}

// static
std::string PolicyBuilder::GetPublicTestKeyAsString() {
  return ConvertPublicKeyToString(GetPublicTestKey());
}

// static
std::string PolicyBuilder::GetPublicTestOtherKeyAsString() {
  return ConvertPublicKeyToString(GetPublicTestOtherKey());
}

// static
std::vector<std::string> PolicyBuilder::GetUserAffiliationIds() {
  return {user_affiliation_id1, user_affiliation_id2};
}

// static
AccountId PolicyBuilder::GetFakeAccountIdForTesting() {
  return AccountId::FromUserEmailGaiaId(kFakeUsername, kFakeGaiaId);
}

void PolicyBuilder::SetSignatureType(
    em::PolicyFetchRequest::SignatureType signature_type) {
  signature_type_ = signature_type;
}

template <>
TypedPolicyBuilder<em::CloudPolicySettings>::TypedPolicyBuilder()
    : payload_(new em::CloudPolicySettings()) {
  policy_data().set_policy_type(dm_protocol::GetChromeUserPolicyType());
}

// Have the instantiation compiled into the module.
template class TypedPolicyBuilder<em::CloudPolicySettings>;

template <>
TypedPolicyBuilder<em::ExtensionInstallPolicies>::TypedPolicyBuilder() {
  CreatePayload();
  policy_data().set_policy_type(
      dm_protocol::kChromeExtensionInstallUserCloudPolicyType);
}

template class TypedPolicyBuilder<em::ExtensionInstallPolicies>;

#if (!BUILDFLAG(IS_ANDROID) || BUILDFLAG(ENABLE_EXTENSIONS_CORE)) && !BUILDFLAG(IS_IOS)
template <>
TypedPolicyBuilder<em::ExternalPolicyData>::TypedPolicyBuilder() {
  CreatePayload();
  policy_data().set_policy_type(dm_protocol::kChromeExtensionPolicyType);
}

template class TypedPolicyBuilder<em::ExternalPolicyData>;
#endif

#if BUILDFLAG(IS_CHROMEOS)
StringPolicyBuilder::StringPolicyBuilder() = default;

void StringPolicyBuilder::Build() {
  policy_data().set_policy_value(payload_);
  PolicyBuilder::Build();
}
#endif

}  // namespace policy
