aboutsummaryrefslogtreecommitdiff
path: root/cast/receiver/channel/testing/device_auth_test_helpers.cc
blob: 51d7ebaa32acdd13a36002dc8867fb81f9ccd692 (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cast/receiver/channel/testing/device_auth_test_helpers.h"

#include "gtest/gtest.h"

namespace openscreen {
namespace cast {

void InitStaticCredentialsFromFiles(StaticCredentialsProvider* creds,
                                    bssl::UniquePtr<X509>* parsed_cert,
                                    TrustStore* fake_trust_store,
                                    absl::string_view privkey_filename,
                                    absl::string_view chain_filename,
                                    absl::string_view tls_filename) {
  auto private_key = testing::ReadKeyFromPemFile(privkey_filename);
  ASSERT_TRUE(private_key);
  std::vector<std::string> certs =
      testing::ReadCertificatesFromPemFile(chain_filename);
  ASSERT_GT(certs.size(), 1u);

  // Use the root of the chain as the trust store for the test.
  auto* data = reinterpret_cast<const uint8_t*>(certs.back().data());
  auto fake_root =
      bssl::UniquePtr<X509>(d2i_X509(nullptr, &data, certs.back().size()));
  ASSERT_TRUE(fake_root);
  certs.pop_back();
  if (fake_trust_store) {
    fake_trust_store->certs.emplace_back(fake_root.release());
  }

  creds->device_creds = DeviceCredentials{
      std::move(certs), std::move(private_key), std::string()};

  const std::vector<std::string> tls_cert =
      testing::ReadCertificatesFromPemFile(tls_filename);
  ASSERT_EQ(tls_cert.size(), 1u);
  data = reinterpret_cast<const uint8_t*>(tls_cert[0].data());
  if (parsed_cert) {
    *parsed_cert =
        bssl::UniquePtr<X509>(d2i_X509(nullptr, &data, tls_cert[0].size()));
    ASSERT_TRUE(*parsed_cert);
  }
  const auto* begin = reinterpret_cast<const uint8_t*>(tls_cert[0].data());
  creds->tls_cert_der.assign(begin, begin + tls_cert[0].size());
}

}  // namespace cast
}  // namespace openscreen