Home | History | Annotate | Download | only in settings
      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_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_
      6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/files/file_path.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/ref_counted.h"
     16 
     17 namespace base {
     18 class FilePath;
     19 }
     20 
     21 namespace crypto {
     22 class RSAPrivateKey;
     23 }
     24 
     25 namespace chromeos {
     26 
     27 class OwnerKeyUtilTest;
     28 
     29 class OwnerKeyUtil : public base::RefCountedThreadSafe<OwnerKeyUtil> {
     30  public:
     31   // Creates an OwnerKeyUtil instance.
     32   static OwnerKeyUtil* Create();
     33 
     34   // Attempts to read the public key from the file system.
     35   // Upon success, returns true and populates |output|.  False on failure.
     36   virtual bool ImportPublicKey(std::vector<uint8>* output) = 0;
     37 
     38   // Looks for the private key associated with |key| in the default slot,
     39   // and returns it if it can be found.  Returns NULL otherwise.
     40   // Caller takes ownership.
     41   virtual crypto::RSAPrivateKey* FindPrivateKey(
     42       const std::vector<uint8>& key) = 0;
     43 
     44   // Checks whether the public key is present in the file system.
     45   virtual bool IsPublicKeyPresent() = 0;
     46 
     47  protected:
     48   OwnerKeyUtil();
     49   virtual ~OwnerKeyUtil();
     50 
     51  private:
     52   friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
     53 
     54   FRIEND_TEST_ALL_PREFIXES(OwnerKeyUtilTest, ExportImportPublicKey);
     55 };
     56 
     57 // Implementation of OwnerKeyUtil that is used in production code.
     58 class OwnerKeyUtilImpl : public OwnerKeyUtil {
     59  public:
     60   explicit OwnerKeyUtilImpl(const base::FilePath& public_key_file);
     61 
     62   // OwnerKeyUtil:
     63   virtual bool ImportPublicKey(std::vector<uint8>* output) OVERRIDE;
     64   virtual crypto::RSAPrivateKey* FindPrivateKey(
     65       const std::vector<uint8>& key) OVERRIDE;
     66   virtual bool IsPublicKeyPresent() OVERRIDE;
     67 
     68  protected:
     69   virtual ~OwnerKeyUtilImpl();
     70 
     71  private:
     72   // The file that holds the public key.
     73   base::FilePath key_file_;
     74 
     75   DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImpl);
     76 };
     77 
     78 }  // namespace chromeos
     79 
     80 #endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_
     81