blob: 9095fd5dcc7b447c76030ae956b1399b9aa24c34 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2012 The Chromium Authors
[email protected]3798a56a2012-08-30 09:03:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
ygorshenin39e36782014-08-29 13:09:515#include "components/ownership/mock_owner_key_util.h"
[email protected]3798a56a2012-08-30 09:03:466
davidbenee92e382015-05-26 20:25:457#include <pk11pub.h>
8
Hans Wennborgdf87046c2020-04-28 11:06:249#include "base/check.h"
ygorshenin39e36782014-08-29 13:09:5110#include "base/files/file_path.h"
davidbenee92e382015-05-26 20:25:4511#include "crypto/nss_key_util.h"
davidben97c06a02015-07-02 13:36:0112#include "crypto/nss_util.h"
[email protected]3798a56a2012-08-30 09:03:4613#include "crypto/rsa_private_key.h"
14
ygorshenin39e36782014-08-29 13:09:5115namespace ownership {
[email protected]3798a56a2012-08-30 09:03:4616
Michael Ershovc15bd582022-10-04 13:57:2217static const uint16_t kKeySizeInBits = 2048;
18
Michael Ershov30976292022-09-09 09:09:1019MockOwnerKeyUtil::MockOwnerKeyUtil() = default;
[email protected]3798a56a2012-08-30 09:03:4620
Michael Ershov30976292022-09-09 09:09:1021MockOwnerKeyUtil::~MockOwnerKeyUtil() = default;
[email protected]3798a56a2012-08-30 09:03:4622
Michael Ershov3edb5ea2022-10-04 09:24:4523scoped_refptr<PublicKey> MockOwnerKeyUtil::ImportPublicKey() {
24 return public_key_.empty() ? nullptr
25 : base::MakeRefCounted<ownership::PublicKey>(
26 /*is_persisted=*/true, /*data=*/public_key_);
[email protected]3798a56a2012-08-30 09:03:4627}
28
Michael Ershovc15bd582022-10-04 13:57:2229crypto::ScopedSECKEYPrivateKey MockOwnerKeyUtil::GenerateKeyPair(
30 PK11SlotInfo* slot) {
31 if (generate_key_fail_times_ > 0) {
32 --generate_key_fail_times_;
33 return nullptr;
34 }
35
36 PK11RSAGenParams param;
37 param.keySizeInBits = kKeySizeInBits;
38 param.pe = 65537L;
39 SECKEYPublicKey* public_key_ptr = nullptr;
40
41 crypto::ScopedSECKEYPrivateKey key(PK11_GenerateKeyPair(
42 slot, CKM_RSA_PKCS_KEY_PAIR_GEN, &param, &public_key_ptr,
43 PR_TRUE /* permanent */, PR_TRUE /* sensitive */, nullptr));
44 crypto::ScopedSECKEYPublicKey public_key(public_key_ptr);
45 return key;
46}
47
davidbenee92e382015-05-26 20:25:4548crypto::ScopedSECKEYPrivateKey MockOwnerKeyUtil::FindPrivateKeyInSlot(
avif57136c12015-12-25 23:27:4549 const std::vector<uint8_t>& key,
[email protected]196e53e82014-05-21 01:50:2550 PK11SlotInfo* slot) {
Michael Ershov7e8e65b2023-11-16 17:46:0451 if (!private_key_ || !slot) {
davidbenee92e382015-05-26 20:25:4552 return nullptr;
Michael Ershov7e8e65b2023-11-16 17:46:0453 }
54
55 if (private_key_slot_id_.has_value() &&
56 (private_key_slot_id_.value() != PK11_GetSlotID(slot))) {
57 return nullptr;
58 }
59
davidbenee92e382015-05-26 20:25:4560 return crypto::ScopedSECKEYPrivateKey(
61 SECKEY_CopyPrivateKey(private_key_.get()));
[email protected]196e53e82014-05-21 01:50:2562}
63
[email protected]3798a56a2012-08-30 09:03:4664bool MockOwnerKeyUtil::IsPublicKeyPresent() {
65 return !public_key_.empty();
66}
67
[email protected]7452c1c2012-11-23 18:44:5968void MockOwnerKeyUtil::Clear() {
69 public_key_.clear();
70 private_key_.reset();
71}
72
avif57136c12015-12-25 23:27:4573void MockOwnerKeyUtil::SetPublicKey(const std::vector<uint8_t>& key) {
[email protected]3798a56a2012-08-30 09:03:4674 public_key_ = key;
75}
76
[email protected]4e8c49a2013-08-05 22:02:0777void MockOwnerKeyUtil::SetPublicKeyFromPrivateKey(
78 const crypto::RSAPrivateKey& key) {
davidbenee92e382015-05-26 20:25:4579 CHECK(key.ExportPublicKey(&public_key_));
[email protected]3798a56a2012-08-30 09:03:4680}
81
Michael Ershov30976292022-09-09 09:09:1082void MockOwnerKeyUtil::ImportPrivateKeyAndSetPublicKey(
dcheng82beb4f2016-04-26 00:35:0283 std::unique_ptr<crypto::RSAPrivateKey> key) {
davidben97c06a02015-07-02 13:36:0184 crypto::EnsureNSSInit();
85
Michael Ershov7e8e65b2023-11-16 17:46:0486 crypto::ScopedPK11Slot slot(PK11_GetInternalSlot());
87 CHECK(slot);
88 ImportPrivateKeyAndSetPublicKeyImpl(std::move(key), slot.get());
89}
90
91void MockOwnerKeyUtil::ImportPrivateKeyAndSetPublicKeyImpl(
92 std::unique_ptr<crypto::RSAPrivateKey> key,
93 PK11SlotInfo* slot) {
94 CHECK(slot);
95 crypto::EnsureNSSInit();
96
davidbenee92e382015-05-26 20:25:4597 CHECK(key->ExportPublicKey(&public_key_));
98
99 std::vector<uint8_t> key_exported;
100 CHECK(key->ExportPrivateKey(&key_exported));
101
davidbenee92e382015-05-26 20:25:45102 private_key_ = crypto::ImportNSSKeyFromPrivateKeyInfo(
Michael Ershov7e8e65b2023-11-16 17:46:04103 slot, key_exported, false /* not permanent */);
davidbenee92e382015-05-26 20:25:45104 CHECK(private_key_);
[email protected]3798a56a2012-08-30 09:03:46105}
106
Michael Ershov7e8e65b2023-11-16 17:46:04107void MockOwnerKeyUtil::ImportPrivateKeyInSlotAndSetPublicKey(
108 std::unique_ptr<crypto::RSAPrivateKey> key,
109 PK11SlotInfo* slot) {
110 private_key_slot_id_ = PK11_GetSlotID(slot);
111 ImportPrivateKeyAndSetPublicKeyImpl(std::move(key), slot);
112}
113
Michael Ershovc15bd582022-10-04 13:57:22114void MockOwnerKeyUtil::SimulateGenerateKeyFailure(int fail_times) {
115 generate_key_fail_times_ = fail_times;
116}
117
ygorshenin39e36782014-08-29 13:09:51118} // namespace ownership