Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 5 | #include "components/ownership/mock_owner_key_util.h" |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 6 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 7 | #include <pk11pub.h> |
| 8 | |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 9 | #include "base/check.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 11 | #include "crypto/nss_key_util.h" |
davidben | 97c06a0 | 2015-07-02 13:36:01 | [diff] [blame] | 12 | #include "crypto/nss_util.h" |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 13 | #include "crypto/rsa_private_key.h" |
| 14 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 15 | namespace ownership { |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 16 | |
Michael Ershov | c15bd58 | 2022-10-04 13:57:22 | [diff] [blame] | 17 | static const uint16_t kKeySizeInBits = 2048; |
| 18 | |
Michael Ershov | 3097629 | 2022-09-09 09:09:10 | [diff] [blame] | 19 | MockOwnerKeyUtil::MockOwnerKeyUtil() = default; |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 20 | |
Michael Ershov | 3097629 | 2022-09-09 09:09:10 | [diff] [blame] | 21 | MockOwnerKeyUtil::~MockOwnerKeyUtil() = default; |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 22 | |
Michael Ershov | 3edb5ea | 2022-10-04 09:24:45 | [diff] [blame] | 23 | scoped_refptr<PublicKey> MockOwnerKeyUtil::ImportPublicKey() { |
| 24 | return public_key_.empty() ? nullptr |
| 25 | : base::MakeRefCounted<ownership::PublicKey>( |
| 26 | /*is_persisted=*/true, /*data=*/public_key_); |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 27 | } |
| 28 | |
Michael Ershov | c15bd58 | 2022-10-04 13:57:22 | [diff] [blame] | 29 | crypto::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, ¶m, &public_key_ptr, |
| 43 | PR_TRUE /* permanent */, PR_TRUE /* sensitive */, nullptr)); |
| 44 | crypto::ScopedSECKEYPublicKey public_key(public_key_ptr); |
| 45 | return key; |
| 46 | } |
| 47 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 48 | crypto::ScopedSECKEYPrivateKey MockOwnerKeyUtil::FindPrivateKeyInSlot( |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 49 | const std::vector<uint8_t>& key, |
[email protected] | 196e53e8 | 2014-05-21 01:50:25 | [diff] [blame] | 50 | PK11SlotInfo* slot) { |
Michael Ershov | 7e8e65b | 2023-11-16 17:46:04 | [diff] [blame] | 51 | if (!private_key_ || !slot) { |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 52 | return nullptr; |
Michael Ershov | 7e8e65b | 2023-11-16 17:46:04 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | if (private_key_slot_id_.has_value() && |
| 56 | (private_key_slot_id_.value() != PK11_GetSlotID(slot))) { |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 60 | return crypto::ScopedSECKEYPrivateKey( |
| 61 | SECKEY_CopyPrivateKey(private_key_.get())); |
[email protected] | 196e53e8 | 2014-05-21 01:50:25 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 64 | bool MockOwnerKeyUtil::IsPublicKeyPresent() { |
| 65 | return !public_key_.empty(); |
| 66 | } |
| 67 | |
[email protected] | 7452c1c | 2012-11-23 18:44:59 | [diff] [blame] | 68 | void MockOwnerKeyUtil::Clear() { |
| 69 | public_key_.clear(); |
| 70 | private_key_.reset(); |
| 71 | } |
| 72 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 73 | void MockOwnerKeyUtil::SetPublicKey(const std::vector<uint8_t>& key) { |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 74 | public_key_ = key; |
| 75 | } |
| 76 | |
[email protected] | 4e8c49a | 2013-08-05 22:02:07 | [diff] [blame] | 77 | void MockOwnerKeyUtil::SetPublicKeyFromPrivateKey( |
| 78 | const crypto::RSAPrivateKey& key) { |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 79 | CHECK(key.ExportPublicKey(&public_key_)); |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 80 | } |
| 81 | |
Michael Ershov | 3097629 | 2022-09-09 09:09:10 | [diff] [blame] | 82 | void MockOwnerKeyUtil::ImportPrivateKeyAndSetPublicKey( |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 83 | std::unique_ptr<crypto::RSAPrivateKey> key) { |
davidben | 97c06a0 | 2015-07-02 13:36:01 | [diff] [blame] | 84 | crypto::EnsureNSSInit(); |
| 85 | |
Michael Ershov | 7e8e65b | 2023-11-16 17:46:04 | [diff] [blame] | 86 | crypto::ScopedPK11Slot slot(PK11_GetInternalSlot()); |
| 87 | CHECK(slot); |
| 88 | ImportPrivateKeyAndSetPublicKeyImpl(std::move(key), slot.get()); |
| 89 | } |
| 90 | |
| 91 | void MockOwnerKeyUtil::ImportPrivateKeyAndSetPublicKeyImpl( |
| 92 | std::unique_ptr<crypto::RSAPrivateKey> key, |
| 93 | PK11SlotInfo* slot) { |
| 94 | CHECK(slot); |
| 95 | crypto::EnsureNSSInit(); |
| 96 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 97 | CHECK(key->ExportPublicKey(&public_key_)); |
| 98 | |
| 99 | std::vector<uint8_t> key_exported; |
| 100 | CHECK(key->ExportPrivateKey(&key_exported)); |
| 101 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 102 | private_key_ = crypto::ImportNSSKeyFromPrivateKeyInfo( |
Michael Ershov | 7e8e65b | 2023-11-16 17:46:04 | [diff] [blame] | 103 | slot, key_exported, false /* not permanent */); |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 104 | CHECK(private_key_); |
[email protected] | 3798a56a | 2012-08-30 09:03:46 | [diff] [blame] | 105 | } |
| 106 | |
Michael Ershov | 7e8e65b | 2023-11-16 17:46:04 | [diff] [blame] | 107 | void 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 Ershov | c15bd58 | 2022-10-04 13:57:22 | [diff] [blame] | 114 | void MockOwnerKeyUtil::SimulateGenerateKeyFailure(int fail_times) { |
| 115 | generate_key_fail_times_ = fail_times; |
| 116 | } |
| 117 | |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 118 | } // namespace ownership |