File: mock_owner_key_util.cc

package info (click to toggle)
chromium 138.0.7204.92-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,576 kB
  • sloc: cpp: 34,933,512; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,956; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 3,310 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ownership/mock_owner_key_util.h"

#include <pk11pub.h>

#include "base/check.h"
#include "base/files/file_path.h"
#include "crypto/nss_key_util.h"
#include "crypto/nss_util.h"
#include "crypto/rsa_private_key.h"

namespace ownership {

static const uint16_t kKeySizeInBits = 2048;

MockOwnerKeyUtil::MockOwnerKeyUtil() = default;

MockOwnerKeyUtil::~MockOwnerKeyUtil() = default;

scoped_refptr<PublicKey> MockOwnerKeyUtil::ImportPublicKey() {
  return public_key_.empty() ? nullptr
                             : base::MakeRefCounted<ownership::PublicKey>(
                                   /*is_persisted=*/true, /*data=*/public_key_);
}

crypto::ScopedSECKEYPrivateKey MockOwnerKeyUtil::GenerateKeyPair(
    PK11SlotInfo* slot) {
  if (generate_key_fail_times_ > 0) {
    --generate_key_fail_times_;
    return nullptr;
  }

  PK11RSAGenParams param;
  param.keySizeInBits = kKeySizeInBits;
  param.pe = 65537L;
  SECKEYPublicKey* public_key_ptr = nullptr;

  crypto::ScopedSECKEYPrivateKey key(PK11_GenerateKeyPair(
      slot, CKM_RSA_PKCS_KEY_PAIR_GEN, &param, &public_key_ptr,
      PR_TRUE /* permanent */, PR_TRUE /* sensitive */, nullptr));
  crypto::ScopedSECKEYPublicKey public_key(public_key_ptr);
  return key;
}

crypto::ScopedSECKEYPrivateKey MockOwnerKeyUtil::FindPrivateKeyInSlot(
    const std::vector<uint8_t>& key,
    PK11SlotInfo* slot) {
  if (!private_key_ || !slot) {
    return nullptr;
  }

  if (private_key_slot_id_.has_value() &&
      (private_key_slot_id_.value() != PK11_GetSlotID(slot))) {
    return nullptr;
  }

  return crypto::ScopedSECKEYPrivateKey(
      SECKEY_CopyPrivateKey(private_key_.get()));
}

bool MockOwnerKeyUtil::IsPublicKeyPresent() {
  return !public_key_.empty();
}

void MockOwnerKeyUtil::Clear() {
  public_key_.clear();
  private_key_.reset();
}

void MockOwnerKeyUtil::SetPublicKey(const std::vector<uint8_t>& key) {
  public_key_ = key;
}

void MockOwnerKeyUtil::SetPublicKeyFromPrivateKey(
    const crypto::RSAPrivateKey& key) {
  CHECK(key.ExportPublicKey(&public_key_));
}

void MockOwnerKeyUtil::ImportPrivateKeyAndSetPublicKey(
    std::unique_ptr<crypto::RSAPrivateKey> key) {
  crypto::EnsureNSSInit();

  crypto::ScopedPK11Slot slot(PK11_GetInternalSlot());
  CHECK(slot);
  ImportPrivateKeyAndSetPublicKeyImpl(std::move(key), slot.get());
}

void MockOwnerKeyUtil::ImportPrivateKeyAndSetPublicKeyImpl(
    std::unique_ptr<crypto::RSAPrivateKey> key,
    PK11SlotInfo* slot) {
  CHECK(slot);
  crypto::EnsureNSSInit();

  CHECK(key->ExportPublicKey(&public_key_));

  std::vector<uint8_t> key_exported;
  CHECK(key->ExportPrivateKey(&key_exported));

  private_key_ = crypto::ImportNSSKeyFromPrivateKeyInfo(
      slot, key_exported, false /* not permanent */);
  CHECK(private_key_);
}

void MockOwnerKeyUtil::ImportPrivateKeyInSlotAndSetPublicKey(
    std::unique_ptr<crypto::RSAPrivateKey> key,
    PK11SlotInfo* slot) {
  private_key_slot_id_ = PK11_GetSlotID(slot);
  ImportPrivateKeyAndSetPublicKeyImpl(std::move(key), slot);
}

void MockOwnerKeyUtil::SimulateGenerateKeyFailure(int fail_times) {
  generate_key_fail_times_ = fail_times;
}

}  // namespace ownership