blob: 94d0e1aed78aea088927041d89368dff8b9b6e69 [file] [log] [blame]
ygorshenin39e36782014-08-29 13:09:511// 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
avif57136c12015-12-25 23:27:458#include <stdint.h>
9
ygorshenin39e36782014-08-29 13:09:5110#include <string>
11#include <vector>
12
ygorshenin39e36782014-08-29 13:09:5113#include "base/macros.h"
14#include "base/memory/ref_counted.h"
ygorshenin39e36782014-08-29 13:09:5115#include "components/ownership/ownership_export.h"
davidbenee92e382015-05-26 20:25:4516#include "crypto/scoped_nss_types.h"
ygorshenin39e36782014-08-29 13:09:5117
ygorshenin39e36782014-08-29 13:09:5118struct PK11SlotInfoStr;
19typedef struct PK11SlotInfoStr PK11SlotInfo;
ygorshenin39e36782014-08-29 13:09:5120
21namespace ownership {
22
ygorshenin39e36782014-08-29 13:09:5123// This class is a ref-counted wrapper around a plain public key.
24class OWNERSHIP_EXPORT PublicKey
25 : public base::RefCountedThreadSafe<PublicKey> {
26 public:
27 PublicKey();
28
avif57136c12015-12-25 23:27:4529 std::vector<uint8_t>& data() { return data_; }
ygorshenin39e36782014-08-29 13:09:5130
31 bool is_loaded() const { return !data_.empty(); }
32
33 std::string as_string() {
davidbenaa62f382015-11-20 22:10:0134 return std::string(reinterpret_cast<const char*>(data_.data()),
ygorshenin39e36782014-08-29 13:09:5135 data_.size());
36 }
37
38 private:
39 friend class base::RefCountedThreadSafe<PublicKey>;
40
41 virtual ~PublicKey();
42
avif57136c12015-12-25 23:27:4543 std::vector<uint8_t> data_;
ygorshenin39e36782014-08-29 13:09:5144
45 DISALLOW_COPY_AND_ASSIGN(PublicKey);
46};
47
davidbenee92e382015-05-26 20:25:4548// This class is a ref-counted wrapper around a SECKEYPrivateKey
ygorshenin39e36782014-08-29 13:09:5149// instance.
50class OWNERSHIP_EXPORT PrivateKey
51 : public base::RefCountedThreadSafe<PrivateKey> {
52 public:
davidbenee92e382015-05-26 20:25:4553 explicit PrivateKey(crypto::ScopedSECKEYPrivateKey key);
ygorshenin39e36782014-08-29 13:09:5154
davidbenee92e382015-05-26 20:25:4555 SECKEYPrivateKey* key() { return key_.get(); }
ygorshenin39e36782014-08-29 13:09:5156
57 private:
58 friend class base::RefCountedThreadSafe<PrivateKey>;
59
60 virtual ~PrivateKey();
61
davidbenee92e382015-05-26 20:25:4562 crypto::ScopedSECKEYPrivateKey key_;
ygorshenin39e36782014-08-29 13:09:5163
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.
69class 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.
avif57136c12015-12-25 23:27:4574 virtual bool ImportPublicKey(std::vector<uint8_t>* output) = 0;
ygorshenin39e36782014-08-29 13:09:5175
ygorshenin39e36782014-08-29 13:09:5176 // 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.
davidbenee92e382015-05-26 20:25:4579 virtual crypto::ScopedSECKEYPrivateKey FindPrivateKeyInSlot(
avif57136c12015-12-25 23:27:4580 const std::vector<uint8_t>& key,
ygorshenin39e36782014-08-29 13:09:5181 PK11SlotInfo* slot) = 0;
ygorshenin39e36782014-08-29 13:09:5182
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_