| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/policy_builder.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "chrome/browser/policy/cloud_policy_constants.h" | |
| 12 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
| 13 #include "chrome/browser/policy/proto/chrome_extension_policy.pb.h" | |
| 14 #include "crypto/signature_creator.h" | |
| 15 | |
| 16 namespace em = enterprise_management; | |
| 17 | |
| 18 namespace policy { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Signing key test data in DER-encoded PKCS8 format. | |
| 23 const uint8 kSigningKey[] = { | |
| 24 0x30, 0x82, 0x01, 0x55, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, | |
| 25 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, | |
| 26 0x01, 0x3f, 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, | |
| 27 0xd9, 0xcd, 0xca, 0xcd, 0xc3, 0xea, 0xbe, 0x72, 0x79, 0x1c, 0x29, 0x37, | |
| 28 0x39, 0x99, 0x1f, 0xd4, 0xb3, 0x0e, 0xf0, 0x7b, 0x78, 0x77, 0x0e, 0x05, | |
| 29 0x3b, 0x65, 0x34, 0x12, 0x62, 0xaf, 0xa6, 0x8d, 0x33, 0xce, 0x78, 0xf8, | |
| 30 0x47, 0x05, 0x1d, 0x98, 0xaa, 0x1b, 0x1f, 0x50, 0x05, 0x5b, 0x3c, 0x19, | |
| 31 0x3f, 0x80, 0x83, 0x63, 0x63, 0x3a, 0xec, 0xcb, 0x2e, 0x90, 0x4f, 0xf5, | |
| 32 0x26, 0x76, 0xf1, 0xd5, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x64, | |
| 33 0x29, 0xc2, 0xd9, 0x6b, 0xfe, 0xf9, 0x84, 0x75, 0x73, 0xe0, 0xf4, 0x77, | |
| 34 0xb5, 0x96, 0xb0, 0xdf, 0x83, 0xc0, 0x4e, 0x57, 0xf1, 0x10, 0x6e, 0x91, | |
| 35 0x89, 0x12, 0x30, 0x5e, 0x57, 0xff, 0x14, 0x59, 0x5f, 0x18, 0x86, 0x4e, | |
| 36 0x4b, 0x17, 0x56, 0xfc, 0x8d, 0x40, 0xdd, 0x74, 0x65, 0xd3, 0xff, 0x67, | |
| 37 0x64, 0xcb, 0x9c, 0xb4, 0x14, 0x8a, 0x06, 0xb7, 0x13, 0x45, 0x94, 0x16, | |
| 38 0x7d, 0x3f, 0xe1, 0x02, 0x21, 0x00, 0xf6, 0x0f, 0x31, 0x6d, 0x06, 0xcc, | |
| 39 0x3b, 0xa0, 0x44, 0x1f, 0xf5, 0xc2, 0x45, 0x2b, 0x10, 0x6c, 0xf9, 0x6f, | |
| 40 0x8f, 0x87, 0x3d, 0xc0, 0x3b, 0x55, 0x13, 0x37, 0x80, 0xcd, 0x9f, 0xe1, | |
| 41 0xb7, 0xd9, 0x02, 0x21, 0x00, 0xe2, 0x9a, 0x5f, 0xbf, 0x95, 0x74, 0xb5, | |
| 42 0x7a, 0x6a, 0xa6, 0x97, 0xbd, 0x75, 0x8c, 0x97, 0x18, 0x24, 0xd6, 0x09, | |
| 43 0xcd, 0xdc, 0xb5, 0x94, 0xbf, 0xe2, 0x78, 0xaa, 0x20, 0x47, 0x9f, 0x68, | |
| 44 0x5d, 0x02, 0x21, 0x00, 0xaf, 0x8f, 0x97, 0x8c, 0x5a, 0xd5, 0x4d, 0x95, | |
| 45 0xc4, 0x05, 0xa9, 0xab, 0xba, 0xfe, 0x46, 0xf1, 0xf9, 0xe7, 0x07, 0x59, | |
| 46 0x4f, 0x4d, 0xe1, 0x07, 0x8a, 0x76, 0x87, 0x88, 0x2f, 0x13, 0x35, 0xc1, | |
| 47 0x02, 0x20, 0x24, 0xc3, 0xd9, 0x2f, 0x13, 0x47, 0x99, 0x3e, 0x20, 0x59, | |
| 48 0xa1, 0x1a, 0xeb, 0x1c, 0x81, 0x53, 0x38, 0x7e, 0xc5, 0x9e, 0x71, 0xe5, | |
| 49 0xc0, 0x19, 0x95, 0xdb, 0xef, 0xf6, 0x46, 0xc8, 0x95, 0x3d, 0x02, 0x21, | |
| 50 0x00, 0xaa, 0xb1, 0xff, 0x8a, 0xa2, 0xb2, 0x2b, 0xef, 0x9a, 0x83, 0x3f, | |
| 51 0xc5, 0xbc, 0xd4, 0x6a, 0x07, 0xe8, 0xc7, 0x0b, 0x2e, 0xd4, 0x0f, 0xf8, | |
| 52 0x98, 0x68, 0xe1, 0x04, 0xa8, 0x92, 0xd0, 0x10, 0xaa, | |
| 53 }; | |
| 54 | |
| 55 // New signing key test data in DER-encoded PKCS8 format. | |
| 56 const uint8 kNewSigningKey[] = { | |
| 57 0x30, 0x82, 0x01, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, | |
| 58 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, | |
| 59 0x01, 0x3e, 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, | |
| 60 0x99, 0x98, 0x6b, 0x79, 0x5d, 0x38, 0x33, 0x79, 0x27, 0x0a, 0x2e, 0xb0, | |
| 61 0x89, 0xba, 0xf8, 0xf6, 0x80, 0xde, 0xb0, 0x79, 0xf2, 0xd4, 0x6d, 0xf7, | |
| 62 0x3c, 0xa3, 0x97, 0xf6, 0x4a, 0x3c, 0xa5, 0xcc, 0x40, 0x8a, 0xef, 0x59, | |
| 63 0xaa, 0xc2, 0x82, 0x8f, 0xbc, 0x0d, 0x5b, 0x63, 0xc6, 0xaa, 0x72, 0xe2, | |
| 64 0xf3, 0x57, 0xdd, 0x74, 0x00, 0xb0, 0x42, 0xd6, 0x27, 0xe7, 0x17, 0x61, | |
| 65 0x0a, 0xdc, 0xc1, 0xf7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x34, | |
| 66 0xcf, 0xc9, 0xb4, 0x73, 0x2f, 0x0d, 0xd3, 0xcc, 0x6e, 0x9d, 0xdb, 0x29, | |
| 67 0xa0, 0x56, 0x56, 0x3b, 0xbd, 0x56, 0x24, 0xb8, 0x2f, 0xfe, 0x97, 0x92, | |
| 68 0x0c, 0x16, 0x06, 0x23, 0x44, 0x73, 0x25, 0x1d, 0x65, 0xf4, 0xda, 0x77, | |
| 69 0xe7, 0x91, 0x2e, 0x91, 0x05, 0x10, 0xc1, 0x1b, 0x39, 0x5e, 0xb2, 0xf7, | |
| 70 0xbd, 0x14, 0x19, 0xcb, 0x6b, 0xc3, 0xa9, 0xe8, 0x91, 0xf7, 0xa7, 0xa9, | |
| 71 0x90, 0x08, 0x51, 0x02, 0x21, 0x00, 0xcc, 0x9e, 0x03, 0x54, 0x8f, 0x24, | |
| 72 0xde, 0x90, 0x25, 0xec, 0x21, 0xaf, 0xe6, 0x27, 0x2a, 0x16, 0x42, 0x74, | |
| 73 0xda, 0xf8, 0x84, 0xc4, 0x8c, 0x1e, 0x86, 0x12, 0x04, 0x5c, 0x17, 0x01, | |
| 74 0xea, 0x9d, 0x02, 0x21, 0x00, 0xc0, 0x2a, 0x6c, 0xe9, 0xa1, 0x1a, 0x41, | |
| 75 0x11, 0x94, 0x50, 0xf7, 0x1a, 0xd3, 0xbc, 0xf3, 0xa2, 0xf8, 0x46, 0xbc, | |
| 76 0x26, 0x77, 0x78, 0xef, 0xc0, 0x54, 0xec, 0x22, 0x3f, 0x2c, 0x57, 0xe0, | |
| 77 0xa3, 0x02, 0x20, 0x31, 0xf2, 0xc8, 0xa1, 0x55, 0xa8, 0x0c, 0x64, 0x67, | |
| 78 0xbd, 0x72, 0xa3, 0xbb, 0xad, 0x07, 0xcb, 0x13, 0x41, 0xef, 0x4a, 0x07, | |
| 79 0x2e, 0xeb, 0x7d, 0x70, 0x00, 0xe9, 0xeb, 0x88, 0xfa, 0x40, 0xc9, 0x02, | |
| 80 0x20, 0x3a, 0xe0, 0xc4, 0xde, 0x10, 0x6e, 0x6a, 0xe1, 0x68, 0x00, 0x26, | |
| 81 0xb6, 0x21, 0x8a, 0x13, 0x5c, 0x2b, 0x96, 0x00, 0xb0, 0x08, 0x8b, 0x15, | |
| 82 0x6a, 0x68, 0x9a, 0xb1, 0x23, 0x8a, 0x02, 0xa2, 0xe1, 0x02, 0x21, 0x00, | |
| 83 0xa3, 0xf2, 0x2d, 0x55, 0xc1, 0x6d, 0x40, 0xfa, 0x1d, 0xf7, 0xba, 0x86, | |
| 84 0xef, 0x50, 0x98, 0xfc, 0xee, 0x09, 0xcc, 0xe7, 0x22, 0xb9, 0x4e, 0x80, | |
| 85 0x32, 0x1a, 0x6b, 0xb3, 0x5f, 0x35, 0xbd, 0xf3, | |
| 86 }; | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 // Constants used as dummy data for filling the PolicyData protobuf. | |
| 91 const char PolicyBuilder::kFakeDeviceId[] = "device-id"; | |
| 92 const char PolicyBuilder::kFakeDomain[] = "example.com"; | |
| 93 const char PolicyBuilder::kFakeMachineName[] = "machine-name"; | |
| 94 const char PolicyBuilder::kFakePolicyType[] = "policy type"; | |
| 95 const int PolicyBuilder::kFakePublicKeyVersion = 17; | |
| 96 const int64 PolicyBuilder::kFakeTimestamp = 365LL * 24 * 60 * 60 * 1000; | |
| 97 const char PolicyBuilder::kFakeToken[] = "token"; | |
| 98 const char PolicyBuilder::kFakeUsername[] = "username@example.com"; | |
| 99 | |
| 100 PolicyBuilder::PolicyBuilder() | |
| 101 : policy_data_(new em::PolicyData()), | |
| 102 signing_key_(CreateTestSigningKey()) { | |
| 103 policy_data_->set_policy_type(kFakePolicyType); | |
| 104 policy_data_->set_timestamp(kFakeTimestamp); | |
| 105 policy_data_->set_request_token(kFakeToken); | |
| 106 policy_data_->set_machine_name(kFakeMachineName); | |
| 107 policy_data_->set_public_key_version(kFakePublicKeyVersion); | |
| 108 policy_data_->set_username(kFakeUsername); | |
| 109 policy_data_->set_device_id(kFakeDeviceId); | |
| 110 policy_data_->set_state(em::PolicyData::ACTIVE); | |
| 111 } | |
| 112 | |
| 113 PolicyBuilder::~PolicyBuilder() {} | |
| 114 | |
| 115 void PolicyBuilder::Build() { | |
| 116 if (policy_data_.get()) | |
| 117 CHECK(policy_data_->SerializeToString(policy_.mutable_policy_data())); | |
| 118 | |
| 119 // Generate signatures if applicable. | |
| 120 crypto::RSAPrivateKey* policy_signing_key = signing_key_.get(); | |
| 121 if (new_signing_key_.get()) { | |
| 122 policy_signing_key = new_signing_key_.get(); | |
| 123 | |
| 124 // Add the new public key. | |
| 125 std::vector<uint8> raw_new_public_signing_key; | |
| 126 CHECK(new_signing_key_->ExportPublicKey(&raw_new_public_signing_key)); | |
| 127 policy_.set_new_public_key(vector_as_array(&raw_new_public_signing_key), | |
| 128 raw_new_public_signing_key.size()); | |
| 129 | |
| 130 // New public key signature. | |
| 131 if (signing_key_.get()) { | |
| 132 SignData(policy_.new_public_key(), signing_key_.get(), | |
| 133 policy_.mutable_new_public_key_signature()); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // PolicyData signature. | |
| 138 if (policy_signing_key) { | |
| 139 SignData(policy_.policy_data(), policy_signing_key, | |
| 140 policy_.mutable_policy_data_signature()); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 std::string PolicyBuilder::GetBlob() { | |
| 145 return policy_.SerializeAsString(); | |
| 146 } | |
| 147 | |
| 148 scoped_ptr<em::PolicyFetchResponse> PolicyBuilder::GetCopy() { | |
| 149 scoped_ptr<em::PolicyFetchResponse> result(new em::PolicyFetchResponse()); | |
| 150 result->CopyFrom(policy_); | |
| 151 return result.Pass(); | |
| 152 } | |
| 153 | |
| 154 // static | |
| 155 scoped_ptr<crypto::RSAPrivateKey> PolicyBuilder::CreateTestSigningKey() { | |
| 156 std::vector<uint8> raw_signing_key( | |
| 157 kSigningKey, kSigningKey + arraysize(kSigningKey)); | |
| 158 return scoped_ptr<crypto::RSAPrivateKey>( | |
| 159 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_signing_key)); | |
| 160 } | |
| 161 | |
| 162 // static | |
| 163 scoped_ptr<crypto::RSAPrivateKey> PolicyBuilder::CreateTestNewSigningKey() { | |
| 164 std::vector<uint8> raw_new_signing_key( | |
| 165 kNewSigningKey, kNewSigningKey + arraysize(kNewSigningKey)); | |
| 166 return scoped_ptr<crypto::RSAPrivateKey>( | |
| 167 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_new_signing_key)); | |
| 168 } | |
| 169 | |
| 170 void PolicyBuilder::SignData(const std::string& data, | |
| 171 crypto::RSAPrivateKey* key, | |
| 172 std::string* signature) { | |
| 173 scoped_ptr<crypto::SignatureCreator> signature_creator( | |
| 174 crypto::SignatureCreator::Create(key)); | |
| 175 signature_creator->Update(reinterpret_cast<const uint8*>(data.c_str()), | |
| 176 data.size()); | |
| 177 std::vector<uint8> signature_bytes; | |
| 178 CHECK(signature_creator->Final(&signature_bytes)); | |
| 179 signature->assign( | |
| 180 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), | |
| 181 signature_bytes.size()); | |
| 182 } | |
| 183 | |
| 184 template<typename PayloadProto> | |
| 185 TypedPolicyBuilder<PayloadProto>::TypedPolicyBuilder() | |
| 186 : payload_(new PayloadProto()) {} | |
| 187 | |
| 188 template<> | |
| 189 TypedPolicyBuilder<em::CloudPolicySettings>::TypedPolicyBuilder() | |
| 190 : payload_(new em::CloudPolicySettings()) { | |
| 191 policy_data().set_policy_type(dm_protocol::kChromeUserPolicyType); | |
| 192 } | |
| 193 | |
| 194 template<> | |
| 195 TypedPolicyBuilder<em::ChromeDeviceSettingsProto>::TypedPolicyBuilder() | |
| 196 : payload_(new em::ChromeDeviceSettingsProto()) { | |
| 197 policy_data().set_policy_type(dm_protocol::kChromeDevicePolicyType); | |
| 198 } | |
| 199 | |
| 200 template<typename PayloadProto> | |
| 201 TypedPolicyBuilder<PayloadProto>::~TypedPolicyBuilder() {} | |
| 202 | |
| 203 template<typename PayloadProto> | |
| 204 void TypedPolicyBuilder<PayloadProto>::Build() { | |
| 205 if (payload_.get()) | |
| 206 CHECK(payload_->SerializeToString(policy_data().mutable_policy_value())); | |
| 207 | |
| 208 PolicyBuilder::Build(); | |
| 209 } | |
| 210 | |
| 211 // Have the instantiations compiled into the module. | |
| 212 template class TypedPolicyBuilder<em::CloudPolicySettings>; | |
| 213 template class TypedPolicyBuilder<em::ChromeDeviceSettingsProto>; | |
| 214 template class TypedPolicyBuilder<em::ExternalPolicyData>; | |
| 215 | |
| 216 } // namespace policy | |
| OLD | NEW |