fc9146c7bfec61e29b20f32f1b4882a135a6a7c8
[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 default slot,
82   // and returns it if it can be found.  Returns NULL otherwise.
83   // Caller takes ownership.
84   //
85   // TODO (ygorshenin@): this function is deprecated and should be
86   // removed, see crbug.com/372316.
87   virtual crypto::RSAPrivateKey* FindPrivateKey(
88       const std::vector<uint8>& key) = 0;
89
90   // Looks for the private key associated with |key| in the |slot|
91   // and returns it if it can be found.  Returns NULL otherwise.
92   // Caller takes ownership.
93   virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
94       const std::vector<uint8>& key,
95       PK11SlotInfo* slot) = 0;
96
97   // Checks whether the public key is present in the file system.
98   virtual bool IsPublicKeyPresent() = 0;
99
100  protected:
101   OwnerKeyUtil();
102   virtual ~OwnerKeyUtil();
103
104  private:
105   friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
106
107   FRIEND_TEST_ALL_PREFIXES(OwnerKeyUtilTest, ExportImportPublicKey);
108 };
109
110 // Implementation of OwnerKeyUtil that is used in production code.
111 class OwnerKeyUtilImpl : public OwnerKeyUtil {
112  public:
113   explicit OwnerKeyUtilImpl(const base::FilePath& public_key_file);
114
115   // OwnerKeyUtil:
116   virtual bool ImportPublicKey(std::vector<uint8>* output) OVERRIDE;
117   virtual crypto::RSAPrivateKey* FindPrivateKey(
118       const std::vector<uint8>& key) OVERRIDE;
119   virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
120       const std::vector<uint8>& key,
121       PK11SlotInfo* slot) OVERRIDE;
122   virtual bool IsPublicKeyPresent() OVERRIDE;
123
124  protected:
125   virtual ~OwnerKeyUtilImpl();
126
127  private:
128   // The file that holds the public key.
129   base::FilePath key_file_;
130
131   DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImpl);
132 };
133
134 }  // namespace chromeos
135
136 #endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_