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.
5 #ifndef COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_
6 #define COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_
13 #include "base/memory/ref_counted.h"
14 #include "components/ownership/ownership_export.h"
15 #include "crypto/scoped_nss_types.h"
17 struct PK11SlotInfoStr;
18 typedef struct PK11SlotInfoStr PK11SlotInfo;
22 // This class is a ref-counted wrapper around a plain public key.
23 class OWNERSHIP_EXPORT PublicKey
24 : public base::RefCountedThreadSafe<PublicKey> {
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
29 PublicKey(bool is_persisted, std::vector<uint8_t> data);
31 PublicKey(const PublicKey&) = delete;
32 PublicKey& operator=(const PublicKey&) = delete;
34 scoped_refptr<PublicKey> clone();
36 std::vector<uint8_t>& data() { return data_; }
38 bool is_empty() const { return data_.empty(); }
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_; }
45 // Marks that the key was saved on disk.
46 void mark_persisted() { is_persisted_ = true; }
48 std::string as_string() {
49 return std::string(reinterpret_cast<const char*>(data_.data()),
54 friend class base::RefCountedThreadSafe<PublicKey>;
58 bool is_persisted_ = false;
59 std::vector<uint8_t> data_;
62 // This class is a ref-counted wrapper around a SECKEYPrivateKey
64 class OWNERSHIP_EXPORT PrivateKey
65 : public base::RefCountedThreadSafe<PrivateKey> {
67 explicit PrivateKey(crypto::ScopedSECKEYPrivateKey key);
69 PrivateKey(const PrivateKey&) = delete;
70 PrivateKey& operator=(const PrivateKey&) = delete;
72 SECKEYPrivateKey* key() { return key_.get(); }
75 friend class base::RefCountedThreadSafe<PrivateKey>;
77 virtual ~PrivateKey();
79 crypto::ScopedSECKEYPrivateKey key_;
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> {
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;
91 // Generates a new key pair in the `slot`.
92 virtual crypto::ScopedSECKEYPrivateKey GenerateKeyPair(
93 PK11SlotInfo* slot) = 0;
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;
102 // Checks whether the public key is present in the file system.
103 virtual bool IsPublicKeyPresent() = 0;
106 virtual ~OwnerKeyUtil() {}
109 friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
112 } // namespace ownership
114 #endif // COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_