| 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 #ifndef CHROME_BROWSER_POLICY_POLICY_BUILDER_H_ | |
| 6 #define CHROME_BROWSER_POLICY_POLICY_BUILDER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | |
| 14 #include "chrome/browser/policy/proto/device_management_local.pb.h" | |
| 15 #include "crypto/rsa_private_key.h" | |
| 16 | |
| 17 namespace enterprise_management { | |
| 18 class ChromeDeviceSettingsProto; | |
| 19 } // namespace enterprise_management | |
| 20 | |
| 21 namespace policy { | |
| 22 | |
| 23 // A helper class for testing that provides a straightforward interface for | |
| 24 // constructing policy blobs for use in testing. NB: This uses fake data and | |
| 25 // hard-coded signing keys by default, so should not be used in production code. | |
| 26 class PolicyBuilder { | |
| 27 public: | |
| 28 // Constants used as dummy data for filling the PolicyData protobuf. | |
| 29 static const char kFakeDeviceId[]; | |
| 30 static const char kFakeDomain[]; | |
| 31 static const char kFakeMachineName[]; | |
| 32 static const char kFakePolicyType[]; | |
| 33 static const int kFakePublicKeyVersion; | |
| 34 static const int64 kFakeTimestamp; | |
| 35 static const char kFakeToken[]; | |
| 36 static const char kFakeUsername[]; | |
| 37 | |
| 38 // Creates a policy builder. The builder will have all PolicyData fields | |
| 39 // initialized to dummy values and use the test signing keys. | |
| 40 PolicyBuilder(); | |
| 41 virtual ~PolicyBuilder(); | |
| 42 | |
| 43 // Use this member to access the PolicyData protobuf. | |
| 44 enterprise_management::PolicyData& policy_data() { | |
| 45 if (!policy_data_.get()) | |
| 46 policy_data_.reset(new enterprise_management::PolicyData()); | |
| 47 return *policy_data_; | |
| 48 } | |
| 49 void clear_policy_data() { | |
| 50 policy_data_.reset(); | |
| 51 } | |
| 52 | |
| 53 enterprise_management::PolicyFetchResponse& policy() { | |
| 54 return policy_; | |
| 55 } | |
| 56 | |
| 57 crypto::RSAPrivateKey* signing_key() { | |
| 58 return signing_key_.get(); | |
| 59 } | |
| 60 void set_signing_key(scoped_ptr<crypto::RSAPrivateKey> signing_key) { | |
| 61 signing_key_ = signing_key.Pass(); | |
| 62 } | |
| 63 | |
| 64 crypto::RSAPrivateKey* new_signing_key() { | |
| 65 return new_signing_key_.get(); | |
| 66 } | |
| 67 void set_new_signing_key(scoped_ptr<crypto::RSAPrivateKey> new_signing_key) { | |
| 68 new_signing_key_ = new_signing_key.Pass(); | |
| 69 } | |
| 70 | |
| 71 // Assembles the policy components. The resulting policy protobuf is available | |
| 72 // through policy() after this call. | |
| 73 virtual void Build(); | |
| 74 | |
| 75 // Returns a copy of policy(). | |
| 76 scoped_ptr<enterprise_management::PolicyFetchResponse> GetCopy(); | |
| 77 | |
| 78 // Returns a binary policy blob, i.e. an encoded PolicyFetchResponse. | |
| 79 std::string GetBlob(); | |
| 80 | |
| 81 // These return hard-coded testing keys. Don't use in production! | |
| 82 static scoped_ptr<crypto::RSAPrivateKey> CreateTestSigningKey(); | |
| 83 static scoped_ptr<crypto::RSAPrivateKey> CreateTestNewSigningKey(); | |
| 84 | |
| 85 private: | |
| 86 // Produces |key|'s signature over |data| and stores it in |signature|. | |
| 87 void SignData(const std::string& data, | |
| 88 crypto::RSAPrivateKey* key, | |
| 89 std::string* signature); | |
| 90 | |
| 91 enterprise_management::PolicyFetchResponse policy_; | |
| 92 scoped_ptr<enterprise_management::PolicyData> policy_data_; | |
| 93 std::string payload_data_; | |
| 94 | |
| 95 scoped_ptr<crypto::RSAPrivateKey> signing_key_; | |
| 96 scoped_ptr<crypto::RSAPrivateKey> new_signing_key_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(PolicyBuilder); | |
| 99 }; | |
| 100 | |
| 101 // Type-parameterized PolicyBuilder extension that allows for building policy | |
| 102 // blobs carrying protobuf payloads. | |
| 103 template<typename PayloadProto> | |
| 104 class TypedPolicyBuilder : public PolicyBuilder { | |
| 105 public: | |
| 106 TypedPolicyBuilder(); | |
| 107 virtual ~TypedPolicyBuilder(); | |
| 108 | |
| 109 // Returns a reference to the payload protobuf being built. | |
| 110 PayloadProto& payload() { | |
| 111 if (!payload_.get()) | |
| 112 payload_.reset(new PayloadProto()); | |
| 113 return *payload_; | |
| 114 } | |
| 115 void clear_payload() { | |
| 116 payload_.reset(); | |
| 117 } | |
| 118 | |
| 119 // PolicyBuilder: | |
| 120 virtual void Build() OVERRIDE; | |
| 121 | |
| 122 private: | |
| 123 scoped_ptr<PayloadProto> payload_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(TypedPolicyBuilder); | |
| 126 }; | |
| 127 | |
| 128 typedef TypedPolicyBuilder<enterprise_management::CloudPolicySettings> | |
| 129 UserPolicyBuilder; | |
| 130 typedef TypedPolicyBuilder<enterprise_management::ChromeDeviceSettingsProto> | |
| 131 DevicePolicyBuilder; | |
| 132 | |
| 133 } // namespace policy | |
| 134 | |
| 135 #endif // CHROME_BROWSER_POLICY_POLICY_BUILDER_H_ | |
| OLD | NEW |