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 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/macros.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/stl_util.h" 16 #include "components/ownership/ownership_export.h" 17 18 #if defined(USE_NSS) 19 struct PK11SlotInfoStr; 20 typedef struct PK11SlotInfoStr PK11SlotInfo; 21 #endif // defined(USE_NSS) 22 23 namespace crypto { 24 class RSAPrivateKey; 25 } 26 27 namespace ownership { 28 29 class OwnerKeyUtilTest; 30 31 // This class is a ref-counted wrapper around a plain public key. 32 class OWNERSHIP_EXPORT PublicKey 33 : public base::RefCountedThreadSafe<PublicKey> { 34 public: 35 PublicKey(); 36 data()37 std::vector<uint8>& data() { return data_; } 38 is_loaded()39 bool is_loaded() const { return !data_.empty(); } 40 as_string()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 // This class is a ref-counted wrapper around a crypto::RSAPrivateKey 57 // instance. 58 class OWNERSHIP_EXPORT PrivateKey 59 : public base::RefCountedThreadSafe<PrivateKey> { 60 public: 61 explicit PrivateKey(crypto::RSAPrivateKey* key); 62 key()63 crypto::RSAPrivateKey* key() { return key_.get(); } 64 65 private: 66 friend class base::RefCountedThreadSafe<PrivateKey>; 67 68 virtual ~PrivateKey(); 69 70 scoped_ptr<crypto::RSAPrivateKey> key_; 71 72 DISALLOW_COPY_AND_ASSIGN(PrivateKey); 73 }; 74 75 // This class is a helper class that allows to import public/private 76 // parts of the owner key. 77 class OWNERSHIP_EXPORT OwnerKeyUtil 78 : public base::RefCountedThreadSafe<OwnerKeyUtil> { 79 public: 80 // Attempts to read the public key from the file system. Upon success, 81 // returns true and populates |output|. False on failure. 82 virtual bool ImportPublicKey(std::vector<uint8>* output) = 0; 83 84 #if defined(USE_NSS) 85 // Looks for the private key associated with |key| in the |slot| 86 // and returns it if it can be found. Returns NULL otherwise. 87 // Caller takes ownership. 88 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot( 89 const std::vector<uint8>& key, 90 PK11SlotInfo* slot) = 0; 91 #endif // defined(USE_NSS) 92 93 // Checks whether the public key is present in the file system. 94 virtual bool IsPublicKeyPresent() = 0; 95 96 protected: ~OwnerKeyUtil()97 virtual ~OwnerKeyUtil() {} 98 99 private: 100 friend class base::RefCountedThreadSafe<OwnerKeyUtil>; 101 }; 102 103 } // namespace ownership 104 105 #endif // COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ 106