Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 1 | // 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 Stewart | a9fbe5c | 2013-06-13 11:12:55 -0700 | [diff] [blame] | 9 | #include <vector> |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/file_path.h> |
| 12 | |
| 13 | namespace shill { |
| 14 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 15 | // 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. |
| 19 | class CertificateFile { |
| 20 | public: |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 21 | CertificateFile(); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 22 | virtual ~CertificateFile(); |
| 23 | |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 24 | // Write out a PEM file from an input vector of strings in PEM format. |
| 25 | // Returns an empty path on failure. |
Paul Stewart | a9fbe5c | 2013-06-13 11:12:55 -0700 | [diff] [blame] | 26 | virtual base::FilePath CreatePEMFromStrings( |
| 27 | const std::vector<std::string> &pem_contents); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 28 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 29 | // Setters. |
| 30 | void set_root_directory(const base::FilePath &root_directory) { |
| 31 | 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. |
| 49 | static std::string ExtractHexData(const std::string &pem_data); |
| 50 | |
| 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. |
| 53 | base::FilePath WriteFile(const std::string &output_data); |
| 54 | |
| 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 Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(CertificateFile); |
| 62 | }; |
| 63 | |
| 64 | } // namespace shill |
| 65 | |
| 66 | #endif // SHILL_CERTIFICATE_FILE_H_ |