pem.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2011 The Chromium 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. #ifndef NET_CERT_PEM_H_
  5. #define NET_CERT_PEM_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/strings/string_piece.h"
  10. #include "net/base/net_export.h"
  11. namespace net {
  12. // PEMTokenizer is a utility class for the parsing of data encapsulated
  13. // using RFC 1421, Privacy Enhancement for Internet Electronic Mail. It
  14. // does not implement the full specification, most notably it does not
  15. // support the Encapsulated Header Portion described in Section 4.4.
  16. class NET_EXPORT_PRIVATE PEMTokenizer {
  17. public:
  18. // Create a new PEMTokenizer that iterates through |str| searching for
  19. // instances of PEM encoded blocks that are of the |allowed_block_types|.
  20. // |str| must remain valid for the duration of the PEMTokenizer.
  21. PEMTokenizer(const base::StringPiece& str,
  22. const std::vector<std::string>& allowed_block_types);
  23. PEMTokenizer(const PEMTokenizer&) = delete;
  24. PEMTokenizer& operator=(const PEMTokenizer&) = delete;
  25. ~PEMTokenizer();
  26. // Attempts to decode the next PEM block in the string. Returns false if no
  27. // PEM blocks can be decoded. The decoded PEM block will be available via
  28. // data().
  29. bool GetNext();
  30. // Returns the PEM block type (eg: CERTIFICATE) of the last successfully
  31. // decoded PEM block.
  32. // GetNext() must have returned true before calling this method.
  33. const std::string& block_type() const { return block_type_; }
  34. // Returns the raw, Base64-decoded data of the last successfully decoded
  35. // PEM block.
  36. // GetNext() must have returned true before calling this method.
  37. const std::string& data() const { return data_; }
  38. private:
  39. void Init(const base::StringPiece& str,
  40. const std::vector<std::string>& allowed_block_types);
  41. // A simple cache of the allowed PEM header and footer for a given PEM
  42. // block type, so that it is only computed once.
  43. struct PEMType;
  44. // The string to search, which must remain valid for as long as this class
  45. // is around.
  46. base::StringPiece str_;
  47. // The current position within |str_| that searching should begin from,
  48. // or StringPiece::npos if iteration is complete
  49. base::StringPiece::size_type pos_;
  50. // The type of data that was encoded, as indicated in the PEM
  51. // Pre-Encapsulation Boundary (eg: CERTIFICATE, PKCS7, or
  52. // PRIVACY-ENHANCED MESSAGE).
  53. std::string block_type_;
  54. // The types of PEM blocks that are allowed. PEM blocks that are not of
  55. // one of these types will be skipped.
  56. std::vector<PEMType> block_types_;
  57. // The raw (Base64-decoded) data of the last successfully decoded block.
  58. std::string data_;
  59. };
  60. // Encodes |data| in the encapsulated message format described in RFC 1421,
  61. // with |type| as the PEM block type (eg: CERTIFICATE).
  62. NET_EXPORT_PRIVATE std::string PEMEncode(base::StringPiece data,
  63. const std::string& type);
  64. } // namespace net
  65. #endif // NET_CERT_PEM_H_