File: pkcs12_validator_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (228 lines) | stat: -rw-r--r-- 8,919 bytes parent folder | download | duplicates (7)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chromeos/ash/components/kcer/helpers/pkcs12_validator.h"

#include "base/containers/span.h"
#include "base/numerics/safe_conversions.h"
#include "chromeos/ash/components/kcer/kcer_nss/test_utils.h"
#include "net/cert/x509_certificate.h"
#include "net/test/cert_builder.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace kcer::internal {
namespace {

std::string GetPassword(const std::string& file_name) {
  if (file_name == "client.p12") {
    return "12345";
  }
  if (file_name == "client_with_ec_key.p12") {
    return "123456";
  }
  ADD_FAILURE() << "GetPassword() is called with an unexpected file name";
  return "";
}

scoped_refptr<const Cert> MakeKcerCert(
    const std::string& nickname,
    scoped_refptr<net::X509Certificate> cert) {
  // CertCache only cares about the `cert`, other fields are can be anything.
  return base::MakeRefCounted<Cert>(Token::kUser, Pkcs11Id(), nickname,
                                    std::move(cert));
}

scoped_refptr<const Cert> MakeKcerCertFromBsslCert(const std::string& nickname,
                                                   X509* cert) {
  int cert_der_size = 0;
  bssl::UniquePtr<uint8_t> cert_der;
  Pkcs12Reader pkcs12_reader;
  Pkcs12ReaderStatusCode get_cert_der_result =
      pkcs12_reader.GetDerEncodedCert(cert, cert_der, cert_der_size);
  if (get_cert_der_result != Pkcs12ReaderStatusCode::kSuccess) {
    ADD_FAILURE() << "GetDerEncodedCert failed";
    return nullptr;
  }

  scoped_refptr<net::X509Certificate> x509_cert =
      net::X509Certificate::CreateFromBytes(base::span(
          cert_der.get(), base::checked_cast<size_t>(cert_der_size)));
  return MakeKcerCert("name", x509_cert);
}

class KcerPkcs12ValidatorTest : public ::testing::Test {
 public:
  void GetData(const char* file_name,
               KeyData& key_data,
               bssl::UniquePtr<STACK_OF(X509)>& certs) {
    Pkcs12Blob pkcs12_blob(ReadTestFile(file_name));
    std::string password(GetPassword(file_name));

    Pkcs12ReaderStatusCode get_key_and_cert_status =
        pkcs12_reader_.GetPkcs12KeyAndCerts(pkcs12_blob.value(), password,
                                            key_data.key, certs);
    if (get_key_and_cert_status != Pkcs12ReaderStatusCode::kSuccess) {
      ADD_FAILURE() << "GetPkcs12KeyAndCerts failed";
      return;
    }

    if (pkcs12_reader_.EnrichKeyData(key_data) !=
        Pkcs12ReaderStatusCode::kSuccess) {
      ADD_FAILURE() << "EnrichKeyData failed";
      return;
    }
  }

  Pkcs12Reader pkcs12_reader_;
  CertCache cert_cache_;
};

// Test that ValidateAndPrepareCertData() returns success on validating a
// correct RSA PKCS#12 data and chooses the correct nickname.
TEST_F(KcerPkcs12ValidatorTest, RsaSuccess) {
  KeyData key_data;
  bssl::UniquePtr<STACK_OF(X509)> certs;
  ASSERT_NO_FATAL_FAILURE(GetData("client.p12", key_data, certs));

  std::vector<CertData> certs_data;
  Pkcs12ReaderStatusCode prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs), key_data, certs_data);
  EXPECT_EQ(prepare_certs_status, Pkcs12ReaderStatusCode::kSuccess);
  ASSERT_EQ(certs_data.size(), 1u);
  // The file doesn't have the nickname set and there are no other certs to copy
  // it from, so the subject name should be used.
  EXPECT_EQ(certs_data[0].nickname, "testusercert");
}

// Test that ValidateAndPrepareCertData() returns success on validating a
// correct EC PKCS#12 data and chooses the correct nickname.
TEST_F(KcerPkcs12ValidatorTest, EcSuccess) {
  KeyData key_data;
  bssl::UniquePtr<STACK_OF(X509)> certs;
  ASSERT_NO_FATAL_FAILURE(GetData("client_with_ec_key.p12", key_data, certs));

  std::vector<CertData> certs_data;
  Pkcs12ReaderStatusCode prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs), key_data, certs_data);
  EXPECT_EQ(prepare_certs_status, Pkcs12ReaderStatusCode::kSuccess);
  ASSERT_EQ(certs_data.size(), 1u);
  // There are no other certs to copy the nickname from, the file has the
  // nickname set and it should be used.
  EXPECT_EQ(certs_data[0].nickname, "serverkey");
}

// Test that ValidateAndPrepareCertData() correctly fails when the key and the
// certs are not related.
TEST_F(KcerPkcs12ValidatorTest, UnrelatedCert) {
  KeyData key_data_1;
  bssl::UniquePtr<STACK_OF(X509)> certs_1;
  ASSERT_NO_FATAL_FAILURE(GetData("client.p12", key_data_1, certs_1));

  KeyData key_data_2;
  bssl::UniquePtr<STACK_OF(X509)> certs_2;
  ASSERT_NO_FATAL_FAILURE(
      GetData("client_with_ec_key.p12", key_data_2, certs_2));

  std::vector<CertData> certs_data;
  Pkcs12ReaderStatusCode prepare_certs_status;

  // `certs_1` and `key_data_2` don't match and should fail.
  prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs_1), key_data_2, certs_data);
  EXPECT_EQ(prepare_certs_status,
            Pkcs12ReaderStatusCode::kPkcs12NoValidCertificatesFound);

  // `certs_2` and `key_data_1` don't match and should fail.
  prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs_2), key_data_1, certs_data);
  EXPECT_EQ(prepare_certs_status,
            Pkcs12ReaderStatusCode::kPkcs12NoValidCertificatesFound);
}

// Test that ValidateAndPrepareCertData() skips already imported keys and
// correctly fails if there's nothing new to import.
TEST_F(KcerPkcs12ValidatorTest, CertExists) {
  KeyData key_data;
  bssl::UniquePtr<STACK_OF(X509)> certs;
  ASSERT_NO_FATAL_FAILURE(GetData("client.p12", key_data, certs));

  X509* cert = sk_X509_value(certs.get(), 0);
  ASSERT_TRUE(cert);

  scoped_refptr<const Cert> kcer_cert = MakeKcerCertFromBsslCert("name", cert);
  cert_cache_ = CertCache(base::span_from_ref(kcer_cert));

  std::vector<CertData> certs_data;
  Pkcs12ReaderStatusCode prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs), key_data, certs_data);
  EXPECT_EQ(prepare_certs_status, Pkcs12ReaderStatusCode::kAlreadyExists);
}

// Test that ValidateAndPrepareCertData() takes the nickname from an existing
// cert when it has the same subject name as the one for import. (This is
// implemented for backwards compatibility with NSS, potentially doesn't have to
// stay this way long term.)
TEST_F(KcerPkcs12ValidatorTest, NicknameFromExistingCert) {
  KeyData key_data;
  bssl::UniquePtr<STACK_OF(X509)> certs;
  ASSERT_NO_FATAL_FAILURE(GetData("client.p12", key_data, certs));

  X509* cert = sk_X509_value(certs.get(), 0);
  ASSERT_TRUE(cert);

  base::span<const uint8_t> subject_der;
  Pkcs12ReaderStatusCode get_subject_der_result =
      pkcs12_reader_.GetSubjectNameDer(cert, subject_der);
  ASSERT_EQ(get_subject_der_result, Pkcs12ReaderStatusCode::kSuccess);

  std::vector<std::unique_ptr<net::CertBuilder>> cert_builders =
      net::CertBuilder::CreateSimpleChain(/*chain_length=*/1);
  cert_builders[0]->SetSubjectTLV(subject_der);

  const char kNickname[] = "nickname123";
  scoped_refptr<const Cert> kcer_cert =
      MakeKcerCert(kNickname, cert_builders[0]->GetX509Certificate());
  cert_cache_ = CertCache(base::span_from_ref(kcer_cert));

  std::vector<CertData> certs_data;
  Pkcs12ReaderStatusCode prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs), key_data, certs_data);
  EXPECT_EQ(prepare_certs_status, Pkcs12ReaderStatusCode::kSuccess);
  ASSERT_EQ(certs_data.size(), 1u);
  EXPECT_EQ(certs_data[0].nickname, kNickname);
}

// Test that ValidateAndPrepareCertData() will generate a unique nickname when
// the default option is already taken.
TEST_F(KcerPkcs12ValidatorTest, UniqueNickname) {
  KeyData key_data;
  bssl::UniquePtr<STACK_OF(X509)> certs;
  ASSERT_NO_FATAL_FAILURE(GetData("client.p12", key_data, certs));

  X509* cert = sk_X509_value(certs.get(), 0);
  ASSERT_TRUE(cert);

  std::vector<std::unique_ptr<net::CertBuilder>> cert_builders =
      net::CertBuilder::CreateSimpleChain(/*chain_length=*/1);

  const char kNickname[] = "testusercert";
  scoped_refptr<const Cert> kcer_cert =
      MakeKcerCert(kNickname, cert_builders[0]->GetX509Certificate());
  cert_cache_ = CertCache(base::span_from_ref(kcer_cert));

  std::vector<CertData> certs_data;
  Pkcs12ReaderStatusCode prepare_certs_status = ValidateAndPrepareCertData(
      cert_cache_, pkcs12_reader_, std::move(certs), key_data, certs_data);
  EXPECT_EQ(prepare_certs_status, Pkcs12ReaderStatusCode::kSuccess);
  ASSERT_EQ(certs_data.size(), 1u);
  EXPECT_EQ(certs_data[0].nickname, std::string(kNickname) + " 1");
}

}  // namespace
}  // namespace kcer::internal