onc_utils.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 CHROMEOS_NETWORK_ONC_ONC_UTILS_H_ 6#define CHROMEOS_NETWORK_ONC_ONC_UTILS_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/memory/scoped_ptr.h" 12#include "chromeos/chromeos_export.h" 13#include "chromeos/network/onc/onc_constants.h" 14 15namespace base { 16class DictionaryValue; 17class ListValue; 18} 19 20namespace chromeos { 21namespace onc { 22 23struct OncValueSignature; 24 25// A valid but empty (no networks and no certificates) and unencrypted 26// configuration. 27CHROMEOS_EXPORT extern const char kEmptyUnencryptedConfiguration[]; 28 29// Parses |json| according to the JSON format. If |json| is a JSON formatted 30// dictionary, the function returns the dictionary as a DictionaryValue. 31// Otherwise returns NULL. 32CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson( 33 const std::string& json); 34 35// Decrypts the given EncryptedConfiguration |onc| (see the ONC specification) 36// using |passphrase|. The resulting UnencryptedConfiguration is returned. If an 37// error occurs, returns NULL. 38CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> Decrypt( 39 const std::string& passphrase, 40 const base::DictionaryValue& onc); 41 42// For logging only: strings not user facing. 43CHROMEOS_EXPORT std::string GetSourceAsString(ONCSource source); 44 45// Used for string expansion with function ExpandStringInOncObject(...). 46class CHROMEOS_EXPORT StringSubstitution { 47 public: 48 StringSubstitution() {} 49 virtual ~StringSubstitution() {} 50 51 // Returns the replacement string for |placeholder| in 52 // |substitute|. Currently, onc::substitutes::kLoginIDField and 53 // onc::substitutes::kEmailField are supported. 54 virtual bool GetSubstitute(std::string placeholder, 55 std::string* substitute) const = 0; 56 private: 57 DISALLOW_COPY_AND_ASSIGN(StringSubstitution); 58}; 59 60// Replaces all expandable fields that are mentioned in the ONC 61// specification. The object of |onc_object| is modified in place. Currently 62// onc::substitutes::kLoginIDField and onc::substitutes::kEmailField are 63// expanded. The replacement strings are obtained from |substitution|. 64CHROMEOS_EXPORT void ExpandStringsInOncObject( 65 const OncValueSignature& signature, 66 const StringSubstitution& substitution, 67 base::DictionaryValue* onc_object); 68 69// Creates a copy of |onc_object| with all values of sensitive fields replaced 70// by |mask|. To find sensitive fields, signature and field name are checked 71// with the function FieldIsCredential(). 72CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> MaskCredentialsInOncObject( 73 const OncValueSignature& signature, 74 const base::DictionaryValue& onc_object, 75 const std::string& mask); 76 77// Decrypts |onc_blob| with |passphrase| if necessary. Clears |network_configs| 78// and |certificates| and fills them with the validated NetworkConfigurations 79// and Certificates of |onc_blob|. Returns false if any validation errors or 80// warnings occurred. Still, some networks or certificates might be added to the 81// output lists and should be further processed by the caller. 82CHROMEOS_EXPORT bool ParseAndValidateOncForImport( 83 const std::string& onc_blob, 84 chromeos::onc::ONCSource onc_source, 85 const std::string& passphrase, 86 base::ListValue* network_configs, 87 base::ListValue* certificates); 88 89} // namespace onc 90} // namespace chromeos 91 92#endif // CHROMEOS_NETWORK_ONC_ONC_UTILS_H_ 93