aboutsummaryrefslogtreecommitdiff
path: root/cast/common/certificate/test_helpers.cc
blob: 41aef74feef8eadd819bfcee2634cf1a6649daa6 (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
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
// 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/common/certificate/test_helpers.h"

#include <openssl/pem.h>
#include <stdio.h>
#include <string.h>

#include "util/logging.h"

namespace cast {
namespace certificate {
namespace testing {

std::string ReadEntireFileToString(const std::string& filename) {
  FILE* file = fopen(filename.c_str(), "r");
  if (file == nullptr) {
    return {};
  }
  fseek(file, 0, SEEK_END);
  long file_size = ftell(file);
  fseek(file, 0, SEEK_SET);
  std::string contents(file_size, 0);
  int bytes_read = 0;
  while (bytes_read < file_size) {
    size_t ret = fread(&contents[bytes_read], 1, file_size - bytes_read, file);
    if (ret == 0 && ferror(file)) {
      return {};
    } else {
      bytes_read += ret;
    }
  }
  fclose(file);

  return contents;
}

std::vector<std::string> ReadCertificatesFromPemFile(
    const std::string& filename) {
  FILE* fp = fopen(filename.c_str(), "r");
  if (!fp) {
    return {};
  }
  std::vector<std::string> certs;
#define STRCMP_LITERAL(s, l) strncmp(s, l, sizeof(l))
  for (;;) {
    char* name;
    char* header;
    unsigned char* data;
    long length;
    if (PEM_read(fp, &name, &header, &data, &length) == 1) {
      if (STRCMP_LITERAL(name, "CERTIFICATE") == 0) {
        certs.emplace_back((char*)data, length);
      }
      OPENSSL_free(name);
      OPENSSL_free(header);
      OPENSSL_free(data);
    } else {
      break;
    }
  }
  fclose(fp);
  return certs;
}

SignatureTestData::SignatureTestData()
    : message{nullptr, 0}, sha1{nullptr, 0}, sha256{nullptr, 0} {}

SignatureTestData::~SignatureTestData() {
  OPENSSL_free(const_cast<uint8_t*>(message.data));
  OPENSSL_free(const_cast<uint8_t*>(sha1.data));
  OPENSSL_free(const_cast<uint8_t*>(sha256.data));
}

SignatureTestData ReadSignatureTestData(const std::string& filename) {
  FILE* fp = fopen(filename.c_str(), "r");
  OSP_DCHECK(fp);
  SignatureTestData result = {};
  for (;;) {
    char* name;
    char* header;
    unsigned char* data;
    long length;
    if (PEM_read(fp, &name, &header, &data, &length) == 1) {
      if (strcmp(name, "MESSAGE") == 0) {
        OSP_DCHECK(!result.message.data);
        result.message.data = data;
        result.message.length = length;
      } else if (strcmp(name, "SIGNATURE SHA1") == 0) {
        OSP_DCHECK(!result.sha1.data);
        result.sha1.data = data;
        result.sha1.length = length;
      } else if (strcmp(name, "SIGNATURE SHA256") == 0) {
        OSP_DCHECK(!result.sha256.data);
        result.sha256.data = data;
        result.sha256.length = length;
      } else {
        OPENSSL_free(data);
      }
      OPENSSL_free(name);
      OPENSSL_free(header);
    } else {
      break;
    }
  }
  OSP_DCHECK(result.message.data);
  OSP_DCHECK(result.sha1.data);
  OSP_DCHECK(result.sha256.data);

  return result;
}

std::unique_ptr<TrustStore> CreateTrustStoreFromPemFile(
    const std::string& filename) {
  std::unique_ptr<TrustStore> store = std::make_unique<TrustStore>();

  std::vector<std::string> certs =
      testing::ReadCertificatesFromPemFile(filename);
  for (const auto& der_cert : certs) {
    const uint8_t* data = (const uint8_t*)der_cert.data();
    store->certs.emplace_back(d2i_X509(nullptr, &data, der_cert.size()));
  }
  return store;
}

}  // namespace testing
}  // namespace certificate
}  // namespace cast