Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: components/policy/core/common/cloud/policy_builder.cc

Issue 1902633006: Convert //components/policy from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments and use namespace alias Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/policy/core/common/cloud/policy_builder.h" 5 #include "components/policy/core/common/cloud/policy_builder.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 policy_data_->set_machine_name(kFakeMachineName); 157 policy_data_->set_machine_name(kFakeMachineName);
158 policy_data_->set_public_key_version(kFakePublicKeyVersion); 158 policy_data_->set_public_key_version(kFakePublicKeyVersion);
159 policy_data_->set_username(kFakeUsername); 159 policy_data_->set_username(kFakeUsername);
160 policy_data_->set_device_id(kFakeDeviceId); 160 policy_data_->set_device_id(kFakeDeviceId);
161 policy_data_->set_state(em::PolicyData::ACTIVE); 161 policy_data_->set_state(em::PolicyData::ACTIVE);
162 policy_data_->set_service_account_identity(kFakeServiceAccountIdentity); 162 policy_data_->set_service_account_identity(kFakeServiceAccountIdentity);
163 } 163 }
164 164
165 PolicyBuilder::~PolicyBuilder() {} 165 PolicyBuilder::~PolicyBuilder() {}
166 166
167 scoped_ptr<crypto::RSAPrivateKey> PolicyBuilder::GetSigningKey() { 167 std::unique_ptr<crypto::RSAPrivateKey> PolicyBuilder::GetSigningKey() {
168 if (raw_signing_key_.empty()) 168 if (raw_signing_key_.empty())
169 return scoped_ptr<crypto::RSAPrivateKey>(); 169 return std::unique_ptr<crypto::RSAPrivateKey>();
170 return scoped_ptr<crypto::RSAPrivateKey>( 170 return std::unique_ptr<crypto::RSAPrivateKey>(
171 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_signing_key_)); 171 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_signing_key_));
172 } 172 }
173 173
174 void PolicyBuilder::SetSigningKey(const crypto::RSAPrivateKey& key) { 174 void PolicyBuilder::SetSigningKey(const crypto::RSAPrivateKey& key) {
175 key.ExportPrivateKey(&raw_signing_key_); 175 key.ExportPrivateKey(&raw_signing_key_);
176 } 176 }
177 177
178 void PolicyBuilder::SetDefaultSigningKey() { 178 void PolicyBuilder::SetDefaultSigningKey() {
179 std::vector<uint8_t> key(kSigningKey, kSigningKey + arraysize(kSigningKey)); 179 std::vector<uint8_t> key(kSigningKey, kSigningKey + arraysize(kSigningKey));
180 raw_signing_key_.swap(key); 180 raw_signing_key_.swap(key);
181 } 181 }
182 182
183 void PolicyBuilder::UnsetSigningKey() { 183 void PolicyBuilder::UnsetSigningKey() {
184 raw_signing_key_.clear(); 184 raw_signing_key_.clear();
185 } 185 }
186 186
187 scoped_ptr<crypto::RSAPrivateKey> PolicyBuilder::GetNewSigningKey() { 187 std::unique_ptr<crypto::RSAPrivateKey> PolicyBuilder::GetNewSigningKey() {
188 if (raw_new_signing_key_.empty()) 188 if (raw_new_signing_key_.empty())
189 return scoped_ptr<crypto::RSAPrivateKey>(); 189 return std::unique_ptr<crypto::RSAPrivateKey>();
190 return scoped_ptr<crypto::RSAPrivateKey>( 190 return std::unique_ptr<crypto::RSAPrivateKey>(
191 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_new_signing_key_)); 191 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_new_signing_key_));
192 } 192 }
193 193
194 void PolicyBuilder::SetDefaultNewSigningKey() { 194 void PolicyBuilder::SetDefaultNewSigningKey() {
195 std::vector<uint8_t> key(kNewSigningKey, 195 std::vector<uint8_t> key(kNewSigningKey,
196 kNewSigningKey + arraysize(kNewSigningKey)); 196 kNewSigningKey + arraysize(kNewSigningKey));
197 raw_new_signing_key_.swap(key); 197 raw_new_signing_key_.swap(key);
198 raw_new_signing_key_signature_ = GetTestOtherSigningKeySignature(); 198 raw_new_signing_key_signature_ = GetTestOtherSigningKeySignature();
199 } 199 }
200 200
201 void PolicyBuilder::SetDefaultInitialSigningKey() { 201 void PolicyBuilder::SetDefaultInitialSigningKey() {
202 std::vector<uint8_t> key(kSigningKey, kSigningKey + arraysize(kSigningKey)); 202 std::vector<uint8_t> key(kSigningKey, kSigningKey + arraysize(kSigningKey));
203 raw_new_signing_key_.swap(key); 203 raw_new_signing_key_.swap(key);
204 raw_new_signing_key_signature_ = GetTestSigningKeySignature(); 204 raw_new_signing_key_signature_ = GetTestSigningKeySignature();
205 UnsetSigningKey(); 205 UnsetSigningKey();
206 } 206 }
207 207
208 void PolicyBuilder::UnsetNewSigningKey() { 208 void PolicyBuilder::UnsetNewSigningKey() {
209 raw_new_signing_key_.clear(); 209 raw_new_signing_key_.clear();
210 raw_new_signing_key_signature_.clear(); 210 raw_new_signing_key_signature_.clear();
211 } 211 }
212 212
213 void PolicyBuilder::Build() { 213 void PolicyBuilder::Build() {
214 // Generate signatures if applicable. 214 // Generate signatures if applicable.
215 scoped_ptr<crypto::RSAPrivateKey> policy_signing_key = GetNewSigningKey(); 215 std::unique_ptr<crypto::RSAPrivateKey> policy_signing_key =
216 GetNewSigningKey();
216 if (policy_signing_key) { 217 if (policy_signing_key) {
217 // Add the new public key. 218 // Add the new public key.
218 std::vector<uint8_t> raw_new_public_signing_key; 219 std::vector<uint8_t> raw_new_public_signing_key;
219 CHECK(policy_signing_key->ExportPublicKey(&raw_new_public_signing_key)); 220 CHECK(policy_signing_key->ExportPublicKey(&raw_new_public_signing_key));
220 policy_.set_new_public_key(raw_new_public_signing_key.data(), 221 policy_.set_new_public_key(raw_new_public_signing_key.data(),
221 raw_new_public_signing_key.size()); 222 raw_new_public_signing_key.size());
222 223
223 policy_.set_new_public_key_verification_signature( 224 policy_.set_new_public_key_verification_signature(
224 raw_new_signing_key_signature_); 225 raw_new_signing_key_signature_);
225 226
226 // The new public key must be signed by the old key. 227 // The new public key must be signed by the old key.
227 scoped_ptr<crypto::RSAPrivateKey> old_signing_key = GetSigningKey(); 228 std::unique_ptr<crypto::RSAPrivateKey> old_signing_key = GetSigningKey();
228 if (old_signing_key) { 229 if (old_signing_key) {
229 SignData(policy_.new_public_key(), 230 SignData(policy_.new_public_key(),
230 old_signing_key.get(), 231 old_signing_key.get(),
231 policy_.mutable_new_public_key_signature()); 232 policy_.mutable_new_public_key_signature());
232 } 233 }
233 } else { 234 } else {
234 // No new signing key, so clear the old public key (this allows us to 235 // No new signing key, so clear the old public key (this allows us to
235 // reuse the same PolicyBuilder to build multiple policy blobs). 236 // reuse the same PolicyBuilder to build multiple policy blobs).
236 policy_.clear_new_public_key(); 237 policy_.clear_new_public_key();
237 policy_.clear_new_public_key_signature(); 238 policy_.clear_new_public_key_signature();
(...skipping 12 matching lines...) Expand all
250 if (policy_signing_key) { 251 if (policy_signing_key) {
251 SignData(policy_.policy_data(), policy_signing_key.get(), 252 SignData(policy_.policy_data(), policy_signing_key.get(),
252 policy_.mutable_policy_data_signature()); 253 policy_.mutable_policy_data_signature());
253 } 254 }
254 } 255 }
255 256
256 std::string PolicyBuilder::GetBlob() { 257 std::string PolicyBuilder::GetBlob() {
257 return policy_.SerializeAsString(); 258 return policy_.SerializeAsString();
258 } 259 }
259 260
260 scoped_ptr<em::PolicyFetchResponse> PolicyBuilder::GetCopy() { 261 std::unique_ptr<em::PolicyFetchResponse> PolicyBuilder::GetCopy() {
261 scoped_ptr<em::PolicyFetchResponse> result(new em::PolicyFetchResponse()); 262 std::unique_ptr<em::PolicyFetchResponse> result(
263 new em::PolicyFetchResponse());
262 result->CopyFrom(policy_); 264 result->CopyFrom(policy_);
263 return result; 265 return result;
264 } 266 }
265 267
266 // static 268 // static
267 scoped_ptr<crypto::RSAPrivateKey> PolicyBuilder::CreateTestSigningKey() { 269 std::unique_ptr<crypto::RSAPrivateKey> PolicyBuilder::CreateTestSigningKey() {
268 std::vector<uint8_t> raw_signing_key(kSigningKey, 270 std::vector<uint8_t> raw_signing_key(kSigningKey,
269 kSigningKey + arraysize(kSigningKey)); 271 kSigningKey + arraysize(kSigningKey));
270 return scoped_ptr<crypto::RSAPrivateKey>( 272 return std::unique_ptr<crypto::RSAPrivateKey>(
271 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_signing_key)); 273 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_signing_key));
272 } 274 }
273 275
274 // static 276 // static
275 scoped_ptr<crypto::RSAPrivateKey> PolicyBuilder::CreateTestOtherSigningKey() { 277 std::unique_ptr<crypto::RSAPrivateKey>
278 PolicyBuilder::CreateTestOtherSigningKey() {
276 std::vector<uint8_t> raw_new_signing_key( 279 std::vector<uint8_t> raw_new_signing_key(
277 kNewSigningKey, kNewSigningKey + arraysize(kNewSigningKey)); 280 kNewSigningKey, kNewSigningKey + arraysize(kNewSigningKey));
278 return scoped_ptr<crypto::RSAPrivateKey>( 281 return std::unique_ptr<crypto::RSAPrivateKey>(
279 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_new_signing_key)); 282 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(raw_new_signing_key));
280 } 283 }
281 284
282 // static 285 // static
283 std::string PolicyBuilder::GetTestSigningKeySignature() { 286 std::string PolicyBuilder::GetTestSigningKeySignature() {
284 return std::string(reinterpret_cast<const char*>(kSigningKeySignature), 287 return std::string(reinterpret_cast<const char*>(kSigningKeySignature),
285 sizeof(kSigningKeySignature)); 288 sizeof(kSigningKeySignature));
286 } 289 }
287 290
288 // static 291 // static
289 std::string PolicyBuilder::GetTestOtherSigningKeySignature() { 292 std::string PolicyBuilder::GetTestOtherSigningKeySignature() {
290 return std::string(reinterpret_cast<const char*>(kNewSigningKeySignature), 293 return std::string(reinterpret_cast<const char*>(kNewSigningKeySignature),
291 sizeof(kNewSigningKeySignature)); 294 sizeof(kNewSigningKeySignature));
292 } 295 }
293 296
294 void PolicyBuilder::SignData(const std::string& data, 297 void PolicyBuilder::SignData(const std::string& data,
295 crypto::RSAPrivateKey* key, 298 crypto::RSAPrivateKey* key,
296 std::string* signature) { 299 std::string* signature) {
297 scoped_ptr<crypto::SignatureCreator> signature_creator( 300 std::unique_ptr<crypto::SignatureCreator> signature_creator(
298 crypto::SignatureCreator::Create(key, 301 crypto::SignatureCreator::Create(key, crypto::SignatureCreator::SHA1));
299 crypto::SignatureCreator::SHA1));
300 signature_creator->Update(reinterpret_cast<const uint8_t*>(data.c_str()), 302 signature_creator->Update(reinterpret_cast<const uint8_t*>(data.c_str()),
301 data.size()); 303 data.size());
302 std::vector<uint8_t> signature_bytes; 304 std::vector<uint8_t> signature_bytes;
303 CHECK(signature_creator->Final(&signature_bytes)); 305 CHECK(signature_creator->Final(&signature_bytes));
304 signature->assign(reinterpret_cast<const char*>(signature_bytes.data()), 306 signature->assign(reinterpret_cast<const char*>(signature_bytes.data()),
305 signature_bytes.size()); 307 signature_bytes.size());
306 } 308 }
307 309
308 template<> 310 template<>
309 TypedPolicyBuilder<em::CloudPolicySettings>::TypedPolicyBuilder() 311 TypedPolicyBuilder<em::CloudPolicySettings>::TypedPolicyBuilder()
310 : payload_(new em::CloudPolicySettings()) { 312 : payload_(new em::CloudPolicySettings()) {
311 policy_data().set_policy_type(dm_protocol::kChromeUserPolicyType); 313 policy_data().set_policy_type(dm_protocol::kChromeUserPolicyType);
312 } 314 }
313 315
314 // Have the instantiation compiled into the module. 316 // Have the instantiation compiled into the module.
315 template class TypedPolicyBuilder<em::CloudPolicySettings>; 317 template class TypedPolicyBuilder<em::CloudPolicySettings>;
316 318
317 #if !defined(OS_ANDROID) && !defined(OS_IOS) 319 #if !defined(OS_ANDROID) && !defined(OS_IOS)
318 template<> 320 template<>
319 TypedPolicyBuilder<em::ExternalPolicyData>::TypedPolicyBuilder() 321 TypedPolicyBuilder<em::ExternalPolicyData>::TypedPolicyBuilder()
320 : payload_(new em::ExternalPolicyData()) { 322 : payload_(new em::ExternalPolicyData()) {
321 policy_data().set_policy_type(dm_protocol::kChromeExtensionPolicyType); 323 policy_data().set_policy_type(dm_protocol::kChromeExtensionPolicyType);
322 } 324 }
323 325
324 template class TypedPolicyBuilder<em::ExternalPolicyData>; 326 template class TypedPolicyBuilder<em::ExternalPolicyData>;
325 #endif 327 #endif
326 328
327 } // namespace policy 329 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698