PcapPlusPlus  24.09
Asn1Codec.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <memory>
5 #include <typeinfo>
6 #include <stdexcept>
7 #include "PointerVector.h"
8 
10 
15 namespace pcpp
16 {
20  enum class Asn1TagClass : uint8_t
21  {
23  Universal = 0,
25  Application = 1,
27  ContextSpecific = 2,
29  Private = 3,
30  };
31 
35  enum class Asn1UniversalTagType : uint8_t
36  {
38  EndOfContent = 0,
40  Boolean = 1,
42  Integer = 2,
44  BitString = 3,
46  OctetString = 4,
48  Null = 5,
50  ObjectIdentifier = 6,
52  ObjectDescriptor = 7,
54  External = 8,
56  Real = 9,
58  Enumerated = 10,
60  EmbeddedPDV = 11,
62  UTF8String = 12,
66  Time = 14,
68  Reserved = 15,
70  Sequence = 16,
72  Set = 17,
74  NumericString = 18,
76  PrintableString = 19,
78  T61String = 20,
80  VideotexString = 21,
82  IA5String = 22,
84  UTCTime = 23,
86  GeneralizedTime = 24,
88  GraphicString = 25,
90  VisibleString = 26,
92  GeneralString = 27,
94  UniversalString = 28,
96  CharacterString = 29,
98  BMPString = 30,
100  Date = 31,
102  TimeOfDay = 32,
104  DateTime = 33,
106  Duration = 34,
108  ObjectIdentifierIRI = 35,
112  NotApplicable = 255
113  };
114 
123  {
124  public:
134  static std::unique_ptr<Asn1Record> decode(const uint8_t* data, size_t dataLen, bool lazy = true);
135 
140  std::vector<uint8_t> encode();
141 
146  {
147  return m_TagClass;
148  }
149 
153  bool isConstructed() const
154  {
155  return m_IsConstructed;
156  }
157 
163 
167  uint8_t getTagType() const
168  {
169  return m_TagType;
170  }
171 
175  size_t getValueLength() const
176  {
177  return m_ValueLength;
178  }
179 
183  size_t getTotalLength() const
184  {
185  return m_TotalLength;
186  }
187 
191  std::string toString();
192 
199  template <class Asn1RecordType> Asn1RecordType* castAs()
200  {
201  auto result = dynamic_cast<Asn1RecordType*>(this);
202  if (result == nullptr)
203  {
204  throw std::bad_cast();
205  }
206  return result;
207  }
208 
209  virtual ~Asn1Record() = default;
210 
211  protected:
213  bool m_IsConstructed = false;
214  uint8_t m_TagType = 0;
215 
216  size_t m_ValueLength = 0;
217  size_t m_TotalLength = 0;
218 
219  uint8_t* m_EncodedValue = nullptr;
220 
221  Asn1Record() = default;
222 
223  static Asn1Record* decodeInternal(const uint8_t* data, size_t dataLen, bool lazy);
224 
225  virtual void decodeValue(uint8_t* data, bool lazy) = 0;
226  virtual std::vector<uint8_t> encodeValue() const = 0;
227 
228  static Asn1Record* decodeTagAndCreateRecord(const uint8_t* data, size_t dataLen, int& tagLen);
229  int decodeLength(const uint8_t* data, size_t dataLen);
230  void decodeValueIfNeeded();
231 
232  uint8_t encodeTag();
233  std::vector<uint8_t> encodeLength() const;
234 
235  virtual std::vector<std::string> toStringList();
236 
237  friend class Asn1ConstructedRecord;
238  };
239 
246  {
247  friend class Asn1Record;
248 
249  public:
258  Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t* value,
259  size_t valueLen);
260 
268  Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const std::string& value);
269 
270  ~Asn1GenericRecord() override;
271 
275  const uint8_t* getValue()
276  {
277  decodeValueIfNeeded();
278  return m_Value;
279  }
280 
281  protected:
282  Asn1GenericRecord() = default;
283 
284  void decodeValue(uint8_t* data, bool lazy) override;
285  std::vector<uint8_t> encodeValue() const override;
286 
287  private:
288  uint8_t* m_Value = nullptr;
289 
290  void init(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t* value, size_t valueLen);
291  };
292 
298  {
299  friend class Asn1Record;
300 
301  public:
308  explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType,
309  const std::vector<Asn1Record*>& subRecords);
310 
317  explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType,
318  const PointerVector<Asn1Record>& subRecords);
319 
325  {
326  decodeValueIfNeeded();
327  return m_SubRecords;
328  };
329 
330  protected:
331  Asn1ConstructedRecord() = default;
332 
333  void decodeValue(uint8_t* data, bool lazy) override;
334  std::vector<uint8_t> encodeValue() const override;
335 
336  std::vector<std::string> toStringList() override;
337 
338  template <typename Iterator> void init(Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
339  {
340  m_TagType = tagType;
341  m_TagClass = tagClass;
342  m_IsConstructed = true;
343 
344  size_t recordValueLength = 0;
345  for (Iterator recordIter = begin; recordIter != end; ++recordIter)
346  {
347  auto encodedRecord = (*recordIter)->encode();
348  auto copyRecord = Asn1Record::decode(encodedRecord.data(), encodedRecord.size(), false);
349  m_SubRecords.pushBack(std::move(copyRecord));
350  recordValueLength += encodedRecord.size();
351  }
352 
353  m_ValueLength = recordValueLength;
354  m_TotalLength = recordValueLength + 1 + (m_ValueLength < 128 ? 1 : 2);
355  }
356 
357  private:
358  PointerVector<Asn1Record> m_SubRecords;
359  };
360 
366  {
367  friend class Asn1Record;
368 
369  public:
374  explicit Asn1SequenceRecord(const std::vector<Asn1Record*>& subRecords);
375 
380  explicit Asn1SequenceRecord(const PointerVector<Asn1Record>& subRecords);
381 
382  private:
383  Asn1SequenceRecord() = default;
384  };
385 
391  {
392  friend class Asn1Record;
393 
394  public:
399  explicit Asn1SetRecord(const std::vector<Asn1Record*>& subRecords);
400 
405  explicit Asn1SetRecord(const PointerVector<Asn1Record>& subRecords);
406 
407  private:
408  Asn1SetRecord() = default;
409  };
410 
417  {
418  friend class Asn1Record;
419 
420  protected:
421  Asn1PrimitiveRecord() = default;
422  explicit Asn1PrimitiveRecord(Asn1UniversalTagType tagType);
423  };
424 
430  {
431  friend class Asn1Record;
432 
433  public:
438  explicit Asn1IntegerRecord(uint32_t value);
439 
443  uint32_t getValue()
444  {
445  decodeValueIfNeeded();
446  return m_Value;
447  }
448 
449  protected:
450  Asn1IntegerRecord() = default;
451 
452  void decodeValue(uint8_t* data, bool lazy) override;
453  std::vector<uint8_t> encodeValue() const override;
454 
455  std::vector<std::string> toStringList() override;
456 
457  private:
458  uint32_t m_Value = 0;
459  };
460 
466  {
467  friend class Asn1Record;
468 
469  public:
474  explicit Asn1EnumeratedRecord(uint32_t value);
475 
476  private:
477  Asn1EnumeratedRecord() = default;
478  };
479 
485  {
486  friend class Asn1Record;
487 
488  public:
493  explicit Asn1OctetStringRecord(const std::string& value);
494 
500  explicit Asn1OctetStringRecord(const uint8_t* value, size_t valueLength);
501 
505  std::string getValue()
506  {
507  decodeValueIfNeeded();
508  return m_Value;
509  };
510 
511  protected:
512  void decodeValue(uint8_t* data, bool lazy) override;
513  std::vector<uint8_t> encodeValue() const override;
514 
515  std::vector<std::string> toStringList() override;
516 
517  private:
518  std::string m_Value;
519  bool m_IsPrintable = true;
520 
521  Asn1OctetStringRecord() = default;
522  };
523 
529  {
530  friend class Asn1Record;
531 
532  public:
537  explicit Asn1BooleanRecord(bool value);
538 
542  bool getValue()
543  {
544  decodeValueIfNeeded();
545  return m_Value;
546  };
547 
548  protected:
549  void decodeValue(uint8_t* data, bool lazy) override;
550  std::vector<uint8_t> encodeValue() const override;
551 
552  std::vector<std::string> toStringList() override;
553 
554  private:
555  Asn1BooleanRecord() = default;
556 
557  bool m_Value = false;
558  };
559 
565  {
566  friend class Asn1Record;
567 
568  public:
573 
574  protected:
575  void decodeValue(uint8_t* data, bool lazy) override
576  {}
577  std::vector<uint8_t> encodeValue() const override
578  {
579  return {};
580  }
581  };
582 } // namespace pcpp
Definition: Asn1Codec.h:529
Asn1BooleanRecord(bool value)
bool getValue()
Definition: Asn1Codec.h:542
Definition: Asn1Codec.h:298
PointerVector< Asn1Record > & getSubRecords()
Definition: Asn1Codec.h:324
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const PointerVector< Asn1Record > &subRecords)
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:466
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:246
const uint8_t * getValue()
Definition: Asn1Codec.h:275
Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t *value, size_t valueLen)
Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const std::string &value)
Definition: Asn1Codec.h:430
Asn1IntegerRecord(uint32_t value)
uint32_t getValue()
Definition: Asn1Codec.h:443
Definition: Asn1Codec.h:565
Definition: Asn1Codec.h:485
Asn1OctetStringRecord(const std::string &value)
std::string getValue()
Definition: Asn1Codec.h:505
Asn1OctetStringRecord(const uint8_t *value, size_t valueLength)
Definition: Asn1Codec.h:417
Definition: Asn1Codec.h:123
uint8_t getTagType() const
Definition: Asn1Codec.h:167
std::vector< uint8_t > encode()
bool isConstructed() const
Definition: Asn1Codec.h:153
Asn1UniversalTagType getUniversalTagType() const
Asn1TagClass getTagClass() const
Definition: Asn1Codec.h:145
std::string toString()
size_t getTotalLength() const
Definition: Asn1Codec.h:183
Asn1RecordType * castAs()
Definition: Asn1Codec.h:199
size_t getValueLength() const
Definition: Asn1Codec.h:175
static std::unique_ptr< Asn1Record > decode(const uint8_t *data, size_t dataLen, bool lazy=true)
Definition: Asn1Codec.h:366
Asn1SequenceRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SequenceRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:391
Asn1SetRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SetRecord(const std::vector< Asn1Record * > &subRecords)
Definition: PointerVector.h:29
The main namespace for the PcapPlusPlus lib.
Asn1TagClass
Definition: Asn1Codec.h:21
Asn1UniversalTagType
Definition: Asn1Codec.h:36