blob: be9abd7a5aab9df0c944d331602d488a5bd83026 [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>
9
10#include <base/file_path.h>
11
12namespace shill {
13
14class GLib;
15
16// Creates a scoped temporary file containing the DER or PEM
17// equivalent of an input PEM-format certificate. When this object
18// is destroyed (or a different file is created from the same object)
19// the previous temporary file is destroyed.
20class CertificateFile {
21 public:
22 explicit CertificateFile(GLib *glib);
23 virtual ~CertificateFile();
24
25 // Write out a PEM file from an input string in PEM format.
26 // Returns an empty path on failure.
27 virtual base::FilePath CreatePEMFromString(const std::string &pem_contents);
28
29 // Write out a DER file from an input string in PEM format.
30 // Returns an empty path on failure.
31 virtual base::FilePath CreateDERFromString(const std::string &pem_contents);
32
33 // Setters.
34 void set_root_directory(const base::FilePath &root_directory) {
35 root_directory_ = root_directory;
36 }
37
38 private:
39 friend class CertificateFileTest;
40
41 // Default root directory to create output files.
42 static const char kDefaultRootDirectory[];
43
44 // Start and end strings for a PEM certificate.
45 static const char kPEMHeader[];
46 static const char kPEMFooter[];
47
48 // Removes the non-empty lines betweeen the PEM header and footer lines
49 // in |pem_data|, removing all leading and trailing whitespace. If
50 // neither a header nor a footer appears, assume they were not provided
51 // by the caller and return all non-empty lines. Returns the resulting
52 // inner portion on success, or an empty string otherwise.
53 static std::string ExtractHexData(const std::string &pem_data);
54
55 // Creates a temporary output file with |output_data| in it. Returns the
56 // path the output data on success or an empty FilePath otherwise.
57 base::FilePath WriteFile(const std::string &output_data);
58
59 // Root directory in which output new files will be created.
60 base::FilePath root_directory_;
61
62 // File path for the created temporary file.
63 base::FilePath output_file_;
64
65 // GLib instance.
66 GLib *glib_;
67
68 DISALLOW_COPY_AND_ASSIGN(CertificateFile);
69};
70
71} // namespace shill
72
73#endif // SHILL_CERTIFICATE_FILE_H_