[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / ownership / owner_key_util.h
1 // Copyright 2014 The Chromium Authors
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 COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_
6 #define COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_
7
8 #include <stdint.h>
9
10 #include <string>
11 #include <vector>
12
13 #include "base/memory/ref_counted.h"
14 #include "components/ownership/ownership_export.h"
15 #include "crypto/scoped_nss_types.h"
16
17 struct PK11SlotInfoStr;
18 typedef struct PK11SlotInfoStr PK11SlotInfo;
19
20 namespace ownership {
21
22 // This class is a ref-counted wrapper around a plain public key.
23 class OWNERSHIP_EXPORT PublicKey
24     : public base::RefCountedThreadSafe<PublicKey> {
25  public:
26   // `is_persisted` should be true for keys that are loaded from disk and false
27   // for newly generated ones. `data` is the binary representation of the key
28   // itself.
29   PublicKey(bool is_persisted, std::vector<uint8_t> data);
30
31   PublicKey(const PublicKey&) = delete;
32   PublicKey& operator=(const PublicKey&) = delete;
33
34   scoped_refptr<PublicKey> clone();
35
36   std::vector<uint8_t>& data() { return data_; }
37
38   bool is_empty() const { return data_.empty(); }
39
40   // Returns true if the key was read from the filesystem or it was already
41   // saved on disk. Returns false for recently generated keys that still need
42   // to be sent to session_manager for saving on disk.
43   bool is_persisted() { return is_persisted_; }
44
45   // Marks that the key was saved on disk.
46   void mark_persisted() { is_persisted_ = true; }
47
48   std::string as_string() {
49     return std::string(reinterpret_cast<const char*>(data_.data()),
50                        data_.size());
51   }
52
53  private:
54   friend class base::RefCountedThreadSafe<PublicKey>;
55
56   virtual ~PublicKey();
57
58   bool is_persisted_ = false;
59   std::vector<uint8_t> data_;
60 };
61
62 // This class is a ref-counted wrapper around a SECKEYPrivateKey
63 // instance.
64 class OWNERSHIP_EXPORT PrivateKey
65     : public base::RefCountedThreadSafe<PrivateKey> {
66  public:
67   explicit PrivateKey(crypto::ScopedSECKEYPrivateKey key);
68
69   PrivateKey(const PrivateKey&) = delete;
70   PrivateKey& operator=(const PrivateKey&) = delete;
71
72   SECKEYPrivateKey* key() { return key_.get(); }
73
74  private:
75   friend class base::RefCountedThreadSafe<PrivateKey>;
76
77   virtual ~PrivateKey();
78
79   crypto::ScopedSECKEYPrivateKey key_;
80 };
81
82 // This class is a helper class that allows to import public/private
83 // parts of the owner key.
84 class OWNERSHIP_EXPORT OwnerKeyUtil
85     : public base::RefCountedThreadSafe<OwnerKeyUtil> {
86  public:
87   // Attempts to read the public key from the file system. Returns nullptr on
88   // failure and a populated key on success.
89   virtual scoped_refptr<PublicKey> ImportPublicKey() = 0;
90
91   // Generates a new key pair in the `slot`.
92   virtual crypto::ScopedSECKEYPrivateKey GenerateKeyPair(
93       PK11SlotInfo* slot) = 0;
94
95   // Looks for the private key associated with |key| in the |slot|
96   // and returns it if it can be found.  Returns NULL otherwise.
97   // Caller takes ownership.
98   virtual crypto::ScopedSECKEYPrivateKey FindPrivateKeyInSlot(
99       const std::vector<uint8_t>& key,
100       PK11SlotInfo* slot) = 0;
101
102   // Checks whether the public key is present in the file system.
103   virtual bool IsPublicKeyPresent() = 0;
104
105  protected:
106   virtual ~OwnerKeyUtil() {}
107
108  private:
109   friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
110 };
111
112 }  // namespace ownership
113
114 #endif  // COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_