| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #ifndef BSSL_PKI_PEM_H_ |
| 16 | #define BSSL_PKI_PEM_H_ |
| 17 |
| 18 | #include <stddef.h> |
| 19 |
| 20 | #include <string> |
| 21 | #include <string_view> |
| 22 | #include <vector> |
| 23 |
| 24 | #include <openssl/base.h> |
| 25 |
| 26 | BSSL_NAMESPACE_BEGIN |
| 27 |
| 28 | // PEMTokenizer is a utility class for the parsing of data encapsulated |
| 29 | // using RFC 1421, Privacy Enhancement for Internet Electronic Mail. It |
| 30 | // does not implement the full specification, most notably it does not |
| 31 | // support the Encapsulated Header Portion described in Section 4.4. |
| 32 | class OPENSSL_EXPORT PEMTokenizer { |
| 33 | public: |
| 34 | // Create a new PEMTokenizer that iterates through |str| searching for |
| 35 | // instances of PEM encoded blocks that are of the |allowed_block_types|. |
| 36 | // |str| must remain valid for the duration of the PEMTokenizer. |
| 37 | PEMTokenizer(std::string_view str, |
| 38 | const std::vector<std::string> &allowed_block_types); |
| 39 |
| 40 | PEMTokenizer(const PEMTokenizer &) = delete; |
| 41 | PEMTokenizer &operator=(const PEMTokenizer &) = delete; |
| 42 |
| 43 | ~PEMTokenizer(); |
| 44 |
| 45 | // Attempts to decode the next PEM block in the string. Returns false if no |
| 46 | // PEM blocks can be decoded. The decoded PEM block will be available via |
| 47 | // data(). |
| 48 | bool GetNext(); |
| 49 |
| 50 | // Returns the PEM block type (eg: CERTIFICATE) of the last successfully |
| 51 | // decoded PEM block. |
| 52 | // GetNext() must have returned true before calling this method. |
| 53 | const std::string &block_type() const { return block_type_; } |
| 54 |
| 55 | // Returns the raw, Base64-decoded data of the last successfully decoded |
| 56 | // PEM block. |
| 57 | // GetNext() must have returned true before calling this method. |
| 58 | const std::string &data() const { return data_; } |
| 59 |
| 60 | private: |
| 61 | void Init(std::string_view str, |
| 62 | const std::vector<std::string> &allowed_block_types); |
| 63 |
| 64 | // A simple cache of the allowed PEM header and footer for a given PEM |
| 65 | // block type, so that it is only computed once. |
| 66 | struct PEMType; |
| 67 |
| 68 | // The string to search, which must remain valid for as long as this class |
| 69 | // is around. |
| 70 | std::string_view str_; |
| 71 |
| 72 | // The current position within |str_| that searching should begin from, |
| 73 | // or std::string_view::npos if iteration is complete |
| 74 | std::string_view::size_type pos_; |
| 75 |
| 76 | // The type of data that was encoded, as indicated in the PEM |
| 77 | // Pre-Encapsulation Boundary (eg: CERTIFICATE, PKCS7, or |
| 78 | // PRIVACY-ENHANCED MESSAGE). |
| 79 | std::string block_type_; |
| 80 |
| 81 | // The types of PEM blocks that are allowed. PEM blocks that are not of |
| 82 | // one of these types will be skipped. |
| 83 | std::vector<PEMType> block_types_; |
| 84 |
| 85 | // The raw (Base64-decoded) data of the last successfully decoded block. |
| 86 | std::string data_; |
| 87 | }; |
| 88 |
| 89 | // Encodes |data| in the encapsulated message format described in RFC 1421, |
| 90 | // with |type| as the PEM block type (eg: CERTIFICATE). |
| 91 | OPENSSL_EXPORT std::string PEMEncode(std::string_view data, |
| 92 | const std::string &type); |
| 93 |
| 94 | BSSL_NAMESPACE_END |
| 95 |
| 96 | #endif // BSSL_PKI_PEM_H_ |
| 97 |