Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / network / onc / onc_utils.h
blob6a56e96406382210c7ff0c8aa0a344d3ad7e7d2e
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.
5 #ifndef CHROMEOS_NETWORK_ONC_ONC_UTILS_H_
6 #define CHROMEOS_NETWORK_ONC_ONC_UTILS_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "chromeos/chromeos_export.h"
16 #include "chromeos/network/network_type_pattern.h"
17 #include "components/onc/onc_constants.h"
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
24 namespace net {
25 class X509Certificate;
28 namespace chromeos {
29 namespace onc {
31 struct OncValueSignature;
33 // A valid but empty (no networks and no certificates) and unencrypted
34 // configuration.
35 CHROMEOS_EXPORT extern const char kEmptyUnencryptedConfiguration[];
37 typedef std::map<std::string, std::string> CertPEMsByGUIDMap;
39 // Parses |json| according to the JSON format. If |json| is a JSON formatted
40 // dictionary, the function returns the dictionary as a DictionaryValue.
41 // Otherwise returns NULL.
42 CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson(
43 const std::string& json);
45 // Decrypts the given EncryptedConfiguration |onc| (see the ONC specification)
46 // using |passphrase|. The resulting UnencryptedConfiguration is returned. If an
47 // error occurs, returns NULL.
48 CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> Decrypt(
49 const std::string& passphrase,
50 const base::DictionaryValue& onc);
52 // For logging only: strings not user facing.
53 CHROMEOS_EXPORT std::string GetSourceAsString(::onc::ONCSource source);
55 // Used for string expansion with function ExpandStringInOncObject(...).
56 class CHROMEOS_EXPORT StringSubstitution {
57 public:
58 StringSubstitution() {}
59 virtual ~StringSubstitution() {}
61 // Returns the replacement string for |placeholder| in
62 // |substitute|. Currently, substitutes::kLoginIDField and
63 // substitutes::kEmailField are supported.
64 virtual bool GetSubstitute(const std::string& placeholder,
65 std::string* substitute) const = 0;
67 private:
68 DISALLOW_COPY_AND_ASSIGN(StringSubstitution);
71 // Replaces all expandable fields that are mentioned in the ONC
72 // specification. The object of |onc_object| is modified in place. Currently
73 // substitutes::kLoginIDField and substitutes::kEmailField are expanded. The
74 // replacement strings are obtained from |substitution|.
75 CHROMEOS_EXPORT void ExpandStringsInOncObject(
76 const OncValueSignature& signature,
77 const StringSubstitution& substitution,
78 base::DictionaryValue* onc_object);
80 // Replaces expandable fields in the networks of |network_configs|, which must
81 // be a list of ONC NetworkConfigurations. See ExpandStringsInOncObject above.
82 CHROMEOS_EXPORT void ExpandStringsInNetworks(
83 const StringSubstitution& substitution,
84 base::ListValue* network_configs);
86 // Fills in all missing HexSSID fields that are mentioned in the ONC
87 // specification. The object of |onc_object| is modified in place.
88 CHROMEOS_EXPORT void FillInHexSSIDFieldsInOncObject(
89 const OncValueSignature& signature,
90 base::DictionaryValue* onc_object);
92 // If the SSID field is set, but HexSSID is not, converts the contents of the
93 // SSID field to UTF-8 encoding, creates the hex representation and assigns the
94 // result to HexSSID.
95 CHROMEOS_EXPORT void FillInHexSSIDField(base::DictionaryValue* wifi_fields);
97 // Creates a copy of |onc_object| with all values of sensitive fields replaced
98 // by |mask|. To find sensitive fields, signature and field name are checked
99 // with the function FieldIsCredential().
100 CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> MaskCredentialsInOncObject(
101 const OncValueSignature& signature,
102 const base::DictionaryValue& onc_object,
103 const std::string& mask);
105 // Decrypts |onc_blob| with |passphrase| if necessary. Clears |network_configs|,
106 // |global_network_config| and |certificates| and fills them with the validated
107 // NetworkConfigurations, GlobalNetworkConfiguration and Certificates of
108 // |onc_blob|. Returns false if any validation errors or warnings occurred.
109 // Still, some configuration might be added to the output arguments and should
110 // be further processed by the caller.
111 CHROMEOS_EXPORT bool ParseAndValidateOncForImport(
112 const std::string& onc_blob,
113 ::onc::ONCSource onc_source,
114 const std::string& passphrase,
115 base::ListValue* network_configs,
116 base::DictionaryValue* global_network_config,
117 base::ListValue* certificates);
119 // Parse the given PEM encoded certificate |pem_encoded| and create a
120 // X509Certificate from it.
121 CHROMEOS_EXPORT scoped_refptr<net::X509Certificate> DecodePEMCertificate(
122 const std::string& pem_encoded);
124 // Replaces all references by GUID to Server or CA certs by their PEM
125 // encoding. Returns true if all references could be resolved. Otherwise returns
126 // false and network configurations with unresolveable references are removed
127 // from |network_configs|. |network_configs| must be a list of ONC
128 // NetworkConfiguration dictionaries.
129 CHROMEOS_EXPORT bool ResolveServerCertRefsInNetworks(
130 const CertPEMsByGUIDMap& certs_by_guid,
131 base::ListValue* network_configs);
133 // Replaces all references by GUID to Server or CA certs by their PEM
134 // encoding. Returns true if all references could be resolved. |network_config|
135 // must be a ONC NetworkConfiguration.
136 CHROMEOS_EXPORT bool ResolveServerCertRefsInNetwork(
137 const CertPEMsByGUIDMap& certs_by_guid,
138 base::DictionaryValue* network_config);
140 // Returns a network type pattern for matching the ONC type string.
141 CHROMEOS_EXPORT NetworkTypePattern NetworkTypePatternFromOncType(
142 const std::string& type);
144 // Returns true if |property_key| is a recommended value in the ONC dictionary.
145 CHROMEOS_EXPORT bool IsRecommendedValue(const base::DictionaryValue* onc,
146 const std::string& property_key);
148 // Translates |onc_proxy_settings|, which must be a valid ONC ProxySettings
149 // dictionary, to a ProxyConfig dictionary (see proxy_config_dictionary.h).
150 CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue>
151 ConvertOncProxySettingsToProxyConfig(
152 const base::DictionaryValue& onc_proxy_settings);
154 // Translates |proxy_config_value|, which must be a valid ProxyConfig dictionary
155 // (see proxy_config_dictionary.h) to an ONC ProxySettings dictionary.
156 CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue>
157 ConvertProxyConfigToOncProxySettings(
158 const base::DictionaryValue& proxy_config_value);
160 } // namespace onc
161 } // namespace chromeos
163 #endif // CHROMEOS_NETWORK_ONC_ONC_UTILS_H_