ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 1 | // Copyright 2014 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 COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ |
| 6 | #define COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ |
| 7 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/memory/ref_counted.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 15 | #include "components/ownership/ownership_export.h" |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 16 | #include "crypto/scoped_nss_types.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 17 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 18 | struct PK11SlotInfoStr; |
| 19 | typedef struct PK11SlotInfoStr PK11SlotInfo; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 20 | |
| 21 | namespace ownership { |
| 22 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 23 | // This class is a ref-counted wrapper around a plain public key. |
| 24 | class OWNERSHIP_EXPORT PublicKey |
| 25 | : public base::RefCountedThreadSafe<PublicKey> { |
| 26 | public: |
| 27 | PublicKey(); |
| 28 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 29 | std::vector<uint8_t>& data() { return data_; } |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 30 | |
| 31 | bool is_loaded() const { return !data_.empty(); } |
| 32 | |
| 33 | std::string as_string() { |
davidben | aa62f38 | 2015-11-20 22:10:01 | [diff] [blame] | 34 | return std::string(reinterpret_cast<const char*>(data_.data()), |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 35 | data_.size()); |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | friend class base::RefCountedThreadSafe<PublicKey>; |
| 40 | |
| 41 | virtual ~PublicKey(); |
| 42 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 43 | std::vector<uint8_t> data_; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 44 | |
| 45 | DISALLOW_COPY_AND_ASSIGN(PublicKey); |
| 46 | }; |
| 47 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 48 | // This class is a ref-counted wrapper around a SECKEYPrivateKey |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 49 | // instance. |
| 50 | class OWNERSHIP_EXPORT PrivateKey |
| 51 | : public base::RefCountedThreadSafe<PrivateKey> { |
| 52 | public: |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 53 | explicit PrivateKey(crypto::ScopedSECKEYPrivateKey key); |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 54 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 55 | SECKEYPrivateKey* key() { return key_.get(); } |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | friend class base::RefCountedThreadSafe<PrivateKey>; |
| 59 | |
| 60 | virtual ~PrivateKey(); |
| 61 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 62 | crypto::ScopedSECKEYPrivateKey key_; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(PrivateKey); |
| 65 | }; |
| 66 | |
| 67 | // This class is a helper class that allows to import public/private |
| 68 | // parts of the owner key. |
| 69 | class OWNERSHIP_EXPORT OwnerKeyUtil |
| 70 | : public base::RefCountedThreadSafe<OwnerKeyUtil> { |
| 71 | public: |
| 72 | // Attempts to read the public key from the file system. Upon success, |
| 73 | // returns true and populates |output|. False on failure. |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 74 | virtual bool ImportPublicKey(std::vector<uint8_t>* output) = 0; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 75 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 76 | // Looks for the private key associated with |key| in the |slot| |
| 77 | // and returns it if it can be found. Returns NULL otherwise. |
| 78 | // Caller takes ownership. |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 79 | virtual crypto::ScopedSECKEYPrivateKey FindPrivateKeyInSlot( |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 80 | const std::vector<uint8_t>& key, |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 81 | PK11SlotInfo* slot) = 0; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 82 | |
| 83 | // Checks whether the public key is present in the file system. |
| 84 | virtual bool IsPublicKeyPresent() = 0; |
| 85 | |
| 86 | protected: |
| 87 | virtual ~OwnerKeyUtil() {} |
| 88 | |
| 89 | private: |
| 90 | friend class base::RefCountedThreadSafe<OwnerKeyUtil>; |
| 91 | }; |
| 92 | |
| 93 | } // namespace ownership |
| 94 | |
| 95 | #endif // COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ |