1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_CERTIFICATE_FILE_H_
18 #define SHILL_CERTIFICATE_FILE_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <base/files/file_path.h>
24 
25 namespace shill {
26 
27 // Creates a scoped temporary file containing the DER or PEM
28 // equivalent of an input PEM-format certificate.  When this object
29 // is destroyed (or a different file is created from the same object)
30 // the previous temporary file is destroyed.
31 class CertificateFile {
32  public:
33   CertificateFile();
34   virtual ~CertificateFile();
35 
36   // Write out a PEM file from an input vector of strings in PEM format.
37   // Returns an empty path on failure.
38   virtual base::FilePath CreatePEMFromStrings(
39       const std::vector<std::string>& pem_contents);
40 
41   // Setters.
set_root_directory(const base::FilePath & root_directory)42   void set_root_directory(const base::FilePath& root_directory) {
43     root_directory_ = root_directory;
44   }
45 
46  private:
47   friend class CertificateFileTest;
48 
49   // Default root directory to create output files.
50   static const char kDefaultRootDirectory[];
51 
52   // Start and end strings for a PEM certificate.
53   static const char kPEMHeader[];
54   static const char kPEMFooter[];
55 
56   // Removes the non-empty lines betweeen the PEM header and footer lines
57   // in |pem_data|, removing all leading and trailing whitespace.  If
58   // neither a header nor a footer appears, assume they were not provided
59   // by the caller and return all non-empty lines.  Returns the resulting
60   // inner portion on success, or an empty string otherwise.
61   static std::string ExtractHexData(const std::string& pem_data);
62 
63   // Creates a temporary output file with |output_data| in it.  Returns the
64   // path the output data on success or an empty FilePath otherwise.
65   base::FilePath WriteFile(const std::string& output_data);
66 
67   // Root directory in which output new files will be created.
68   base::FilePath root_directory_;
69 
70   // File path for the created temporary file.
71   base::FilePath output_file_;
72 
73   DISALLOW_COPY_AND_ASSIGN(CertificateFile);
74 };
75 
76 }  // namespace shill
77 
78 #endif  // SHILL_CERTIFICATE_FILE_H_
79