1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cast/common/certificate/testing/test_helpers.h"
6
7 #include <openssl/bytestring.h>
8 #include <openssl/pem.h>
9 #include <openssl/rsa.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "absl/strings/match.h"
14 #include "util/osp_logging.h"
15
16 namespace openscreen {
17 namespace cast {
18 namespace testing {
19
SignatureTestData()20 SignatureTestData::SignatureTestData()
21 : message{nullptr, 0}, sha1{nullptr, 0}, sha256{nullptr, 0} {}
22
~SignatureTestData()23 SignatureTestData::~SignatureTestData() {
24 OPENSSL_free(const_cast<uint8_t*>(message.data));
25 OPENSSL_free(const_cast<uint8_t*>(sha1.data));
26 OPENSSL_free(const_cast<uint8_t*>(sha256.data));
27 }
28
ReadSignatureTestData(absl::string_view filename)29 SignatureTestData ReadSignatureTestData(absl::string_view filename) {
30 FILE* fp = fopen(filename.data(), "r");
31 OSP_DCHECK(fp);
32 SignatureTestData result = {};
33 char* name;
34 char* header;
35 unsigned char* data;
36 long length; // NOLINT
37 while (PEM_read(fp, &name, &header, &data, &length) == 1) {
38 if (strcmp(name, "MESSAGE") == 0) {
39 OSP_DCHECK(!result.message.data);
40 result.message.data = data;
41 result.message.length = length;
42 } else if (strcmp(name, "SIGNATURE SHA1") == 0) {
43 OSP_DCHECK(!result.sha1.data);
44 result.sha1.data = data;
45 result.sha1.length = length;
46 } else if (strcmp(name, "SIGNATURE SHA256") == 0) {
47 OSP_DCHECK(!result.sha256.data);
48 result.sha256.data = data;
49 result.sha256.length = length;
50 } else {
51 OPENSSL_free(data);
52 }
53 OPENSSL_free(name);
54 OPENSSL_free(header);
55 }
56 OSP_DCHECK(result.message.data);
57 OSP_DCHECK(result.sha1.data);
58 OSP_DCHECK(result.sha256.data);
59
60 return result;
61 }
62
63 } // namespace testing
64 } // namespace cast
65 } // namespace openscreen
66