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_
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"
20 class DictionaryValue
;
25 class X509Certificate
;
31 struct OncValueSignature
;
33 // A valid but empty (no networks and no certificates) and unencrypted
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
{
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;
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 // Creates a copy of |onc_object| with all values of sensitive fields replaced
87 // by |mask|. To find sensitive fields, signature and field name are checked
88 // with the function FieldIsCredential().
89 CHROMEOS_EXPORT scoped_ptr
<base::DictionaryValue
> MaskCredentialsInOncObject(
90 const OncValueSignature
& signature
,
91 const base::DictionaryValue
& onc_object
,
92 const std::string
& mask
);
94 // Decrypts |onc_blob| with |passphrase| if necessary. Clears |network_configs|,
95 // |global_network_config| and |certificates| and fills them with the validated
96 // NetworkConfigurations, GlobalNetworkConfiguration and Certificates of
97 // |onc_blob|. Returns false if any validation errors or warnings occurred.
98 // Still, some configuration might be added to the output arguments and should
99 // be further processed by the caller.
100 CHROMEOS_EXPORT
bool ParseAndValidateOncForImport(
101 const std::string
& onc_blob
,
102 ::onc::ONCSource onc_source
,
103 const std::string
& passphrase
,
104 base::ListValue
* network_configs
,
105 base::DictionaryValue
* global_network_config
,
106 base::ListValue
* certificates
);
108 // Parse the given PEM encoded certificate |pem_encoded| and create a
109 // X509Certificate from it.
110 CHROMEOS_EXPORT scoped_refptr
<net::X509Certificate
> DecodePEMCertificate(
111 const std::string
& pem_encoded
);
113 // Replaces all references by GUID to Server or CA certs by their PEM
114 // encoding. Returns true if all references could be resolved. Otherwise returns
115 // false and network configurations with unresolveable references are removed
116 // from |network_configs|. |network_configs| must be a list of ONC
117 // NetworkConfiguration dictionaries.
118 CHROMEOS_EXPORT
bool ResolveServerCertRefsInNetworks(
119 const CertPEMsByGUIDMap
& certs_by_guid
,
120 base::ListValue
* network_configs
);
122 // Replaces all references by GUID to Server or CA certs by their PEM
123 // encoding. Returns true if all references could be resolved. |network_config|
124 // must be a ONC NetworkConfiguration.
125 CHROMEOS_EXPORT
bool ResolveServerCertRefsInNetwork(
126 const CertPEMsByGUIDMap
& certs_by_guid
,
127 base::DictionaryValue
* network_config
);
129 // Returns a network type pattern for matching the ONC type string.
130 CHROMEOS_EXPORT NetworkTypePattern
NetworkTypePatternFromOncType(
131 const std::string
& type
);
133 // Returns true if |property_key| is a recommended value in the ONC dictionary.
134 CHROMEOS_EXPORT
bool IsRecommendedValue(const base::DictionaryValue
* onc
,
135 const std::string
& property_key
);
138 } // namespace chromeos
140 #endif // CHROMEOS_NETWORK_ONC_ONC_UTILS_H_