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

namespace cast {
namespace certificate {
namespace testing {

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;
}

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