PcapPlusPlus  Next
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 
13 namespace pcpp
14 {
16  enum class Asn1TagClass : uint8_t
17  {
19  Universal = 0,
21  Application = 1,
23  ContextSpecific = 2,
25  Private = 3,
26  };
27 
29  enum class Asn1UniversalTagType : uint8_t
30  {
32  EndOfContent = 0,
34  Boolean = 1,
36  Integer = 2,
38  BitString = 3,
40  OctetString = 4,
42  Null = 5,
44  ObjectIdentifier = 6,
46  ObjectDescriptor = 7,
48  External = 8,
50  Real = 9,
52  Enumerated = 10,
54  EmbeddedPDV = 11,
56  UTF8String = 12,
60  Time = 14,
62  Reserved = 15,
64  Sequence = 16,
66  Set = 17,
68  NumericString = 18,
70  PrintableString = 19,
72  T61String = 20,
74  VideotexString = 21,
76  IA5String = 22,
78  UTCTime = 23,
80  GeneralizedTime = 24,
82  GraphicString = 25,
84  VisibleString = 26,
86  GeneralString = 27,
88  UniversalString = 28,
90  CharacterString = 29,
92  BMPString = 30,
94  Date = 31,
96  TimeOfDay = 32,
98  DateTime = 33,
100  Duration = 34,
102  ObjectIdentifierIRI = 35,
106  NotApplicable = 255
107  };
108 
114  {
115  public:
123  static std::unique_ptr<Asn1Record> decode(const uint8_t* data, size_t dataLen, bool lazy = true);
124 
127  std::vector<uint8_t> encode();
128 
131  {
132  return m_TagClass;
133  }
134 
136  bool isConstructed() const
137  {
138  return m_IsConstructed;
139  }
140 
144 
146  uint8_t getTagType() const
147  {
148  return m_TagType;
149  }
150 
152  size_t getValueLength() const
153  {
154  return m_ValueLength;
155  }
156 
158  size_t getTotalLength() const
159  {
160  return m_TotalLength;
161  }
162 
164  std::string toString();
165 
170  template <class Asn1RecordType> Asn1RecordType* castAs()
171  {
172  auto result = dynamic_cast<Asn1RecordType*>(this);
173  if (result == nullptr)
174  {
175  throw std::bad_cast();
176  }
177  return result;
178  }
179 
180  virtual ~Asn1Record() = default;
181 
182  protected:
184  bool m_IsConstructed = false;
185  uint8_t m_TagType = 0;
186 
187  size_t m_ValueLength = 0;
188  size_t m_TotalLength = 0;
189 
190  uint8_t* m_EncodedValue = nullptr;
191 
192  Asn1Record() = default;
193 
194  static Asn1Record* decodeInternal(const uint8_t* data, size_t dataLen, bool lazy);
195 
196  virtual void decodeValue(uint8_t* data, bool lazy) = 0;
197  virtual std::vector<uint8_t> encodeValue() const = 0;
198 
199  static Asn1Record* decodeTagAndCreateRecord(const uint8_t* data, size_t dataLen, uint8_t& tagLen);
200  uint8_t decodeLength(const uint8_t* data, size_t dataLen);
201  void decodeValueIfNeeded();
202 
203  uint8_t encodeTag();
204  std::vector<uint8_t> encodeLength() const;
205 
206  virtual std::vector<std::string> toStringList();
207 
208  friend class Asn1ConstructedRecord;
209  };
210 
215  {
216  friend class Asn1Record;
217 
218  public:
225  Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t* value,
226  size_t valueLen);
227 
233  Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const std::string& value);
234 
235  ~Asn1GenericRecord() override;
236 
238  const uint8_t* getValue()
239  {
240  decodeValueIfNeeded();
241  return m_Value;
242  }
243 
244  protected:
245  Asn1GenericRecord() = default;
246 
247  void decodeValue(uint8_t* data, bool lazy) override;
248  std::vector<uint8_t> encodeValue() const override;
249 
250  private:
251  uint8_t* m_Value = nullptr;
252 
253  void init(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t* value, size_t valueLen);
254  };
255 
259  {
260  friend class Asn1Record;
261 
262  public:
267  explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType,
268  const std::vector<Asn1Record*>& subRecords);
269 
274  explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType,
275  const PointerVector<Asn1Record>& subRecords);
276 
280  {
281  decodeValueIfNeeded();
282  return m_SubRecords;
283  };
284 
285  protected:
286  Asn1ConstructedRecord() = default;
287 
288  void decodeValue(uint8_t* data, bool lazy) override;
289  std::vector<uint8_t> encodeValue() const override;
290 
291  std::vector<std::string> toStringList() override;
292 
293  template <typename Iterator> void init(Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
294  {
295  m_TagType = tagType;
296  m_TagClass = tagClass;
297  m_IsConstructed = true;
298 
299  size_t recordValueLength = 0;
300  for (Iterator recordIter = begin; recordIter != end; ++recordIter)
301  {
302  auto encodedRecord = (*recordIter)->encode();
303  auto copyRecord = Asn1Record::decode(encodedRecord.data(), encodedRecord.size(), false);
304  m_SubRecords.pushBack(std::move(copyRecord));
305  recordValueLength += encodedRecord.size();
306  }
307 
308  m_ValueLength = recordValueLength;
309  m_TotalLength = recordValueLength + 1 + (m_ValueLength < 128 ? 1 : 2);
310  }
311 
312  private:
313  PointerVector<Asn1Record> m_SubRecords;
314  };
315 
319  {
320  friend class Asn1Record;
321 
322  public:
325  explicit Asn1SequenceRecord(const std::vector<Asn1Record*>& subRecords);
326 
329  explicit Asn1SequenceRecord(const PointerVector<Asn1Record>& subRecords);
330 
331  private:
332  Asn1SequenceRecord() = default;
333  };
334 
338  {
339  friend class Asn1Record;
340 
341  public:
344  explicit Asn1SetRecord(const std::vector<Asn1Record*>& subRecords);
345 
348  explicit Asn1SetRecord(const PointerVector<Asn1Record>& subRecords);
349 
350  private:
351  Asn1SetRecord() = default;
352  };
353 
358  {
359  friend class Asn1Record;
360 
361  protected:
362  Asn1PrimitiveRecord() = default;
363  explicit Asn1PrimitiveRecord(Asn1UniversalTagType tagType);
364  };
365 
369  {
370  friend class Asn1Record;
371 
372  public:
375  explicit Asn1IntegerRecord(uint32_t value);
376 
378  uint32_t getValue()
379  {
380  decodeValueIfNeeded();
381  return m_Value;
382  }
383 
384  protected:
385  Asn1IntegerRecord() = default;
386 
387  void decodeValue(uint8_t* data, bool lazy) override;
388  std::vector<uint8_t> encodeValue() const override;
389 
390  std::vector<std::string> toStringList() override;
391 
392  private:
393  uint32_t m_Value = 0;
394  };
395 
399  {
400  friend class Asn1Record;
401 
402  public:
405  explicit Asn1EnumeratedRecord(uint32_t value);
406 
407  private:
408  Asn1EnumeratedRecord() = default;
409  };
410 
414  {
415  friend class Asn1Record;
416 
417  public:
420  explicit Asn1OctetStringRecord(const std::string& value);
421 
425  explicit Asn1OctetStringRecord(const uint8_t* value, size_t valueLength);
426 
428  std::string getValue()
429  {
430  decodeValueIfNeeded();
431  return m_Value;
432  };
433 
434  protected:
435  void decodeValue(uint8_t* data, bool lazy) override;
436  std::vector<uint8_t> encodeValue() const override;
437 
438  std::vector<std::string> toStringList() override;
439 
440  private:
441  std::string m_Value;
442  bool m_IsPrintable = true;
443 
444  Asn1OctetStringRecord() = default;
445  };
446 
450  {
451  friend class Asn1Record;
452 
453  public:
456  explicit Asn1BooleanRecord(bool value);
457 
459  bool getValue()
460  {
461  decodeValueIfNeeded();
462  return m_Value;
463  };
464 
465  protected:
466  void decodeValue(uint8_t* data, bool lazy) override;
467  std::vector<uint8_t> encodeValue() const override;
468 
469  std::vector<std::string> toStringList() override;
470 
471  private:
472  Asn1BooleanRecord() = default;
473 
474  bool m_Value = false;
475  };
476 
480  {
481  friend class Asn1Record;
482 
483  public:
486 
487  protected:
488  void decodeValue(uint8_t* data, bool lazy) override
489  {}
490  std::vector<uint8_t> encodeValue() const override
491  {
492  return {};
493  }
494  };
495 } // namespace pcpp
Definition: Asn1Codec.h:450
Asn1BooleanRecord(bool value)
bool getValue()
Definition: Asn1Codec.h:459
Definition: Asn1Codec.h:259
PointerVector< Asn1Record > & getSubRecords()
Definition: Asn1Codec.h:279
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const PointerVector< Asn1Record > &subRecords)
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:399
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:215
const uint8_t * getValue()
Definition: Asn1Codec.h:238
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:369
Asn1IntegerRecord(uint32_t value)
uint32_t getValue()
Definition: Asn1Codec.h:378
Definition: Asn1Codec.h:480
Asn1NullRecord()
A constructor to create a record of type Null.
Definition: Asn1Codec.h:414
Asn1OctetStringRecord(const std::string &value)
std::string getValue()
Definition: Asn1Codec.h:428
Asn1OctetStringRecord(const uint8_t *value, size_t valueLength)
Definition: Asn1Codec.h:358
Definition: Asn1Codec.h:114
uint8_t getTagType() const
Definition: Asn1Codec.h:146
std::vector< uint8_t > encode()
bool isConstructed() const
Definition: Asn1Codec.h:136
Asn1UniversalTagType getUniversalTagType() const
Asn1TagClass getTagClass() const
Definition: Asn1Codec.h:130
std::string toString()
size_t getTotalLength() const
Definition: Asn1Codec.h:158
Asn1RecordType * castAs()
Definition: Asn1Codec.h:170
size_t getValueLength() const
Definition: Asn1Codec.h:152
static std::unique_ptr< Asn1Record > decode(const uint8_t *data, size_t dataLen, bool lazy=true)
Definition: Asn1Codec.h:319
Asn1SequenceRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SequenceRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:338
Asn1SetRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SetRecord(const std::vector< Asn1Record * > &subRecords)
Definition: PointerVector.h:50
The main namespace for the PcapPlusPlus lib.
Asn1TagClass
An enum for representing ASN.1 tag class.
Definition: Asn1Codec.h:17
@ ContextSpecific
The Context-Specific tag class.
@ Private
The Private tag class.
@ Universal
The Universal tag class.
@ Application
The Application tag class.
Asn1UniversalTagType
An enum for representing ASN.1 Universal tag types.
Definition: Asn1Codec.h:30
@ BitString
The universal tag type for Bit String.
@ Enumerated
The universal tag type for Enumerated.
@ ObjectIdentifierIRI
The universal tag type for Object Identifier Internationalized Resource Identifier (IRI)
@ ObjectDescriptor
The universal tag type for Object Descriptor.
@ TimeOfDay
The universal tag type for Time of Day.
@ GraphicString
The universal tag type for GraphicString.
@ UTCTime
The universal tag type for UTC time.
@ Boolean
The universal tag type for Boolean.
@ EmbeddedPDV
The universal tag type for Embedded-PDV.
@ OctetString
The universal tag type for Octet String.
@ UniversalString
The universal tag type for UniversalString.
@ Sequence
The universal tag type Sequence.
@ Date
The universal tag type for Date.
@ RelativeObjectIdentifierIRI
The universal tag type for Relative Object Identifier Internationalized Resource Identifier (IRI)
@ PrintableString
The universal tag type for Printable String.
@ CharacterString
The universal tag type for CharacterString.
@ RelativeObjectIdentifier
The universal tag type for Relative Object Identifier.
@ Set
The universal tag type for Set.
@ ObjectIdentifier
The universal tag type for Object Identifier.
@ IA5String
The universal tag type for IA5String.
@ VideotexString
The universal tag type for Videotex String.
@ NotApplicable
A non-applicable value.
@ Real
The universal tag type for Real.
@ DateTime
The universal tag type for Date-Time.
@ EndOfContent
The reserved identifier for the End-of-Contents marker in an indefinite length encoding.
@ Reserved
A reserved value.
@ Integer
The universal tag type for Integer.
@ Time
The universal tag type for Time.
@ External
The universal tag type for External.
@ BMPString
The universal tag type for BMPString.
@ UTF8String
The universal tag type for UTF8 String.
@ Null
The universal tag type for Null.
@ GeneralizedTime
The universal tag type for Generalized time.
@ T61String
The universal tag type for T61String.
@ Duration
The universal tag type for Duration.
@ VisibleString
The universal tag type for VisibleString.
@ GeneralString
The universal tag type for GeneralString.
@ NumericString
The universal tag type for Numeric String.