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