blob: e3faec124d3ceeb28985a8b62ed34a7699844e41 [file] [log] [blame]
Paul Stewart5baebb72013-03-14 11:43:29 -07001// Copyright (c) 2013 The Chromium OS 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#ifndef SHILL_CERTIFICATE_FILE_H_
6#define SHILL_CERTIFICATE_FILE_H_
7
8#include <string>
Paul Stewarta9fbe5c2013-06-13 11:12:55 -07009#include <vector>
Paul Stewart5baebb72013-03-14 11:43:29 -070010
Ben Chana0ddf462014-02-06 11:32:42 -080011#include <base/files/file_path.h>
Paul Stewart5baebb72013-03-14 11:43:29 -070012
13namespace shill {
14
Paul Stewart5baebb72013-03-14 11:43:29 -070015// Creates a scoped temporary file containing the DER or PEM
16// equivalent of an input PEM-format certificate. When this object
17// is destroyed (or a different file is created from the same object)
18// the previous temporary file is destroyed.
19class CertificateFile {
20 public:
Paul Stewarteb713e82013-06-28 14:51:54 -070021 CertificateFile();
Paul Stewart5baebb72013-03-14 11:43:29 -070022 virtual ~CertificateFile();
23
Paul Stewart0f9c9302013-06-14 15:41:28 -070024 // Write out a PEM file from an input vector of strings in PEM format.
25 // Returns an empty path on failure.
Paul Stewarta9fbe5c2013-06-13 11:12:55 -070026 virtual base::FilePath CreatePEMFromStrings(
Paul Stewarta794cd62015-06-16 13:13:10 -070027 const std::vector<std::string>& pem_contents);
Paul Stewart5baebb72013-03-14 11:43:29 -070028
Paul Stewart5baebb72013-03-14 11:43:29 -070029 // Setters.
Paul Stewarta794cd62015-06-16 13:13:10 -070030 void set_root_directory(const base::FilePath& root_directory) {
Paul Stewart5baebb72013-03-14 11:43:29 -070031 root_directory_ = root_directory;
32 }
33
34 private:
35 friend class CertificateFileTest;
36
37 // Default root directory to create output files.
38 static const char kDefaultRootDirectory[];
39
40 // Start and end strings for a PEM certificate.
41 static const char kPEMHeader[];
42 static const char kPEMFooter[];
43
44 // Removes the non-empty lines betweeen the PEM header and footer lines
45 // in |pem_data|, removing all leading and trailing whitespace. If
46 // neither a header nor a footer appears, assume they were not provided
47 // by the caller and return all non-empty lines. Returns the resulting
48 // inner portion on success, or an empty string otherwise.
Paul Stewarta794cd62015-06-16 13:13:10 -070049 static std::string ExtractHexData(const std::string& pem_data);
Paul Stewart5baebb72013-03-14 11:43:29 -070050
51 // Creates a temporary output file with |output_data| in it. Returns the
52 // path the output data on success or an empty FilePath otherwise.
Paul Stewarta794cd62015-06-16 13:13:10 -070053 base::FilePath WriteFile(const std::string& output_data);
Paul Stewart5baebb72013-03-14 11:43:29 -070054
55 // Root directory in which output new files will be created.
56 base::FilePath root_directory_;
57
58 // File path for the created temporary file.
59 base::FilePath output_file_;
60
Paul Stewart5baebb72013-03-14 11:43:29 -070061 DISALLOW_COPY_AND_ASSIGN(CertificateFile);
62};
63
64} // namespace shill
65
66#endif // SHILL_CERTIFICATE_FILE_H_