Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / settings / owner_key_util.h
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 #include "base/memory/scoped_ptr.h"
17 #include "base/stl_util.h"
18 #include "crypto/rsa_private_key.h"
19 #include "net/cert/x509_util_nss.h"
20
21 namespace base {
22 class FilePath;
23 }
24
25 namespace crypto {
26 class RSAPrivateKey;
27 }
28
29 namespace chromeos {
30
31 class OwnerKeyUtilTest;
32
33 class PublicKey : public base::RefCountedThreadSafe<PublicKey> {
34  public:
35   PublicKey();
36
37   std::vector<uint8>& data() { return data_; }
38
39   bool is_loaded() const { return !data_.empty(); }
40
41   std::string as_string() {
42     return std::string(reinterpret_cast<const char*>(vector_as_array(&data_)),
43                        data_.size());
44   }
45
46  private:
47   friend class base::RefCountedThreadSafe<PublicKey>;
48
49   virtual ~PublicKey();
50
51   std::vector<uint8> data_;
52
53   DISALLOW_COPY_AND_ASSIGN(PublicKey);
54 };
55
56 class PrivateKey : public base::RefCountedThreadSafe<PrivateKey> {
57  public:
58   explicit PrivateKey(crypto::RSAPrivateKey* key);
59
60   crypto::RSAPrivateKey* key() { return key_.get(); }
61
62  private:
63   friend class base::RefCountedThreadSafe<PrivateKey>;
64
65   virtual ~PrivateKey();
66
67   scoped_ptr<crypto::RSAPrivateKey> key_;
68
69   DISALLOW_COPY_AND_ASSIGN(PrivateKey);
70 };
71
72 class OwnerKeyUtil : public base::RefCountedThreadSafe<OwnerKeyUtil> {
73  public:
74   // Creates an OwnerKeyUtil instance.
75   static OwnerKeyUtil* Create();
76
77   // Attempts to read the public key from the file system.
78   // Upon success, returns true and populates |output|.  False on failure.
79   virtual bool ImportPublicKey(std::vector<uint8>* output) = 0;
80
81   // Looks for the private key associated with |key| in the |slot|
82   // and returns it if it can be found.  Returns NULL otherwise.
83   // Caller takes ownership.
84   virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
85       const std::vector<uint8>& key,
86       PK11SlotInfo* slot) = 0;
87
88   // Checks whether the public key is present in the file system.
89   virtual bool IsPublicKeyPresent() = 0;
90
91  protected:
92   OwnerKeyUtil();
93   virtual ~OwnerKeyUtil();
94
95  private:
96   friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
97
98   FRIEND_TEST_ALL_PREFIXES(OwnerKeyUtilTest, ExportImportPublicKey);
99 };
100
101 // Implementation of OwnerKeyUtil that is used in production code.
102 class OwnerKeyUtilImpl : public OwnerKeyUtil {
103  public:
104   explicit OwnerKeyUtilImpl(const base::FilePath& public_key_file);
105
106   // OwnerKeyUtil:
107   virtual bool ImportPublicKey(std::vector<uint8>* output) OVERRIDE;
108   virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
109       const std::vector<uint8>& key,
110       PK11SlotInfo* slot) OVERRIDE;
111   virtual bool IsPublicKeyPresent() OVERRIDE;
112
113  protected:
114   virtual ~OwnerKeyUtilImpl();
115
116  private:
117   // The file that holds the public key.
118   base::FilePath key_file_;
119
120   DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImpl);
121 };
122
123 }  // namespace chromeos
124
125 #endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_