PcapPlusPlus  Next
CryptoDataReader.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include "GeneralUtils.h"
6 #include "PemCodec.h"
7 #include <string>
8 #include <memory>
9 #include <fstream>
10 
13 namespace pcpp
14 {
15  namespace internal
16  {
22  template <typename CryptoDecoder> class CryptoDataReader
23  {
24  public:
32  static std::unique_ptr<CryptoDecoder> fromDER(uint8_t* derData, size_t derDataLen, bool ownDerData = false)
33  {
34  return std::unique_ptr<CryptoDecoder>(new CryptoDecoder(derData, derDataLen, ownDerData));
35  }
36 
41  static std::unique_ptr<CryptoDecoder> fromDER(const std::string& derData)
42  {
43  size_t derDataBufferLen = derData.length() / 2;
44  auto derDataBuffer = std::make_unique<uint8_t[]>(derDataBufferLen);
45  hexStringToByteArray(derData, derDataBuffer.get(), derDataBufferLen);
46  return std::unique_ptr<CryptoDecoder>(new CryptoDecoder(std::move(derDataBuffer), derDataBufferLen));
47  }
48 
53  static std::unique_ptr<CryptoDecoder> fromDERFile(const std::string& derFileName)
54  {
55  std::ifstream derFile(derFileName, std::ios::binary);
56  if (!derFile.good())
57  {
58  throw std::runtime_error("DER file doesn't exist or cannot be opened");
59  }
60 
61  derFile.seekg(0, std::ios::end);
62  std::streamsize derDataLen = derFile.tellg();
63  if (derDataLen < 0)
64  {
65  throw std::runtime_error("Failed to determine DER file size");
66  }
67  derFile.seekg(0, std::ios::beg);
68 
69  auto derData = std::make_unique<uint8_t[]>(derDataLen);
70 
71  if (!derFile.read(reinterpret_cast<char*>(derData.get()), derDataLen))
72  {
73  throw std::runtime_error("Failed to read DER file");
74  }
75  return std::unique_ptr<CryptoDecoder>(new CryptoDecoder(std::move(derData), derDataLen));
76  }
77 
82  static std::unique_ptr<CryptoDecoder> fromPEM(const std::string& pemData)
83  {
84  auto derData = PemCodec::decode(pemData, CryptoDecoder::pemLabel);
85  auto derDataBuffer = std::make_unique<uint8_t[]>(derData.size());
86  std::copy(derData.begin(), derData.end(), derDataBuffer.get());
87  return std::unique_ptr<CryptoDecoder>(new CryptoDecoder(std::move(derDataBuffer), derData.size()));
88  }
89 
95  static std::unique_ptr<CryptoDecoder> fromPEMFile(const std::string& pemFileName)
96  {
97  std::ifstream pemFile(pemFileName, std::ios::in | std::ios::binary);
98  if (!pemFile.good())
99  {
100  throw std::runtime_error("PEM file doesn't exist or cannot be opened");
101  }
102 
103  pemFile.seekg(0, std::ios::end);
104  std::streamsize pemContentLen = pemFile.tellg();
105  if (pemContentLen < 0)
106  {
107  throw std::runtime_error("Failed to determine PEM file size");
108  }
109  pemFile.seekg(0, std::ios::beg);
110 
111  std::string pemContent;
112  pemContent.resize(static_cast<std::size_t>(pemContentLen));
113  if (!pemFile.read(&pemContent[0], pemContentLen))
114  {
115  throw std::runtime_error("Failed to read PEM file");
116  }
117 
118  return fromPEM(pemContent);
119  }
120 
121  protected:
122  ~CryptoDataReader() = default;
123  };
124  } // namespace internal
125 } // namespace pcpp
static std::vector< uint8_t > decode(const std::string &pemData, const std::string &expectedLabel="")
A template helper class for reading and decoding cryptographic data in different formats (DER/PEM)
Definition: CryptoDataReader.h:23
static std::unique_ptr< CryptoDecoder > fromDER(uint8_t *derData, size_t derDataLen, bool ownDerData=false)
Definition: CryptoDataReader.h:32
static std::unique_ptr< CryptoDecoder > fromPEM(const std::string &pemData)
Definition: CryptoDataReader.h:82
static std::unique_ptr< CryptoDecoder > fromPEMFile(const std::string &pemFileName)
Definition: CryptoDataReader.h:95
static std::unique_ptr< CryptoDecoder > fromDERFile(const std::string &derFileName)
Definition: CryptoDataReader.h:53
static std::unique_ptr< CryptoDecoder > fromDER(const std::string &derData)
Definition: CryptoDataReader.h:41
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
size_t hexStringToByteArray(const std::string &hexString, uint8_t *resultByteArr, size_t resultByteArrSize)