PcapPlusPlus  Next
SSLLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "PointerVector.h"
4 #include "Layer.h"
5 #include "SSLCommon.h"
6 #include "SSLHandshake.h"
7 
164 namespace pcpp
165 {
166 
174  class SSLLayer : public Layer
175  {
176  public:
181  static inline bool isSSLPort(uint16_t port);
182 
210  static bool IsSSLMessage(uint16_t srcPort, uint16_t dstPort, uint8_t* data, size_t dataLen,
211  bool ignorePorts = false);
212 
224  static SSLLayer* createSSLMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
225 
232  {
233  return reinterpret_cast<ssl_tls_record_layer*>(m_Data);
234  }
235 
240 
245 
246  // implement abstract methods
247 
251  size_t getHeaderLen() const override;
252 
257  void parseNextLayer() override;
258 
260  {
262  }
263 
264  protected:
265  SSLLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
266  : Layer(data, dataLen, prevLayer, packet, SSL)
267  {}
268 
269  }; // class SSLLayer
270 
271  // The graph below will break the code formatting, so it's disabled.
272  // clang-format off
317  // clang-format on
319  {
320  public:
328  SSLHandshakeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
329 
334  {
335  return m_MessageList.size();
336  }
337 
346 
352  template <class THandshakeMessage> THandshakeMessage* getHandshakeMessageOfType() const;
353 
363  template <class THandshakeMessage>
364  THandshakeMessage* getNextHandshakeMessageOfType(const SSLHandshakeMessage* after) const;
365 
366  // implement abstract methods
367 
368  std::string toString() const override;
369 
373  void computeCalculateFields() override
374  {}
375 
376  private:
378  }; // class SSLHandshakeLayer
379 
386  {
387  public:
395  SSLChangeCipherSpecLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
396  : SSLLayer(data, dataLen, prevLayer, packet)
397  {}
398 
399  ~SSLChangeCipherSpecLayer() override = default;
400 
401  // implement abstract methods
402 
403  std::string toString() const override;
404 
408  void computeCalculateFields() override
409  {}
410  }; // class SSLChangeCipherSpecLayer
411 
417  class SSLAlertLayer : public SSLLayer
418  {
419  public:
427  SSLAlertLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
428  : SSLLayer(data, dataLen, prevLayer, packet)
429  {}
430 
431  ~SSLAlertLayer() override = default;
432 
437 
442 
443  // implement abstract methods
444 
445  std::string toString() const override;
446 
450  void computeCalculateFields() override
451  {}
452  }; // class SSLAlertLayer
453 
460  {
461  public:
469  SSLApplicationDataLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
470  : SSLLayer(data, dataLen, prevLayer, packet)
471  {}
472 
473  ~SSLApplicationDataLayer() override = default;
474 
479  uint8_t* getEncryptedData() const;
480 
484  size_t getEncryptedDataLen() const;
485 
486  // implement abstract methods
487 
488  std::string toString() const override;
489 
493  void computeCalculateFields() override
494  {}
495  }; // class SSLApplicationDataLayer
496 
497  template <class THandshakeMessage> THandshakeMessage* SSLHandshakeLayer::getHandshakeMessageOfType() const
498  {
499  size_t vecSize = m_MessageList.size();
500  for (size_t i = 0; i < vecSize; i++)
501  {
502  SSLHandshakeMessage* curElem = const_cast<SSLHandshakeMessage*>(m_MessageList.at(i));
503  if (dynamic_cast<THandshakeMessage*>(curElem) != nullptr)
504  return (THandshakeMessage*)curElem;
505  }
506 
507  // element not found
508  return nullptr;
509  } // getHandshakeMessageOfType
510 
511  template <class THandshakeMessage>
513  {
514  size_t vecSize = m_MessageList.size();
515  size_t afterIndex;
516 
517  // find the index of "after"
518  for (afterIndex = 0; afterIndex < vecSize; afterIndex++)
519  {
520  SSLHandshakeMessage* curElem = const_cast<SSLHandshakeMessage*>(m_MessageList.at(afterIndex));
521  if (curElem == after)
522  break;
523  }
524 
525  // "after" not found
526  if (afterIndex == vecSize)
527  return nullptr;
528 
529  for (size_t i = afterIndex + 1; i < vecSize; i++)
530  {
531  SSLHandshakeMessage* curElem = const_cast<SSLHandshakeMessage*>(m_MessageList.at(i));
532  if (dynamic_cast<THandshakeMessage*>(curElem) != nullptr)
533  return (THandshakeMessage*)curElem;
534  }
535 
536  // element not found
537  return nullptr;
538  } // getNextHandshakeMessageOfType
539 
540  // implementation of inline methods
541 
542  bool SSLLayer::isSSLPort(uint16_t port)
543  {
544  if (port == 443) // HTTPS, this is likely case
545  return true;
546 
547  switch (port)
548  {
549  case 261: // NSIIOPS
550  case 448: // DDM-SSL
551  case 465: // SMTPS
552  case 563: // NNTPS
553  case 614: // SSHELL
554  case 636: // LDAPS
555  case 989: // FTPS - data
556  case 990: // FTPS - control
557  case 992: // Telnet over TLS/SSL
558  case 993: // IMAPS
559  case 994: // IRCS
560  case 995: // POP3S
561  return true;
562  default:
563  return false;
564  }
565  } // isSSLPort
566 
567 } // namespace pcpp
Definition: Layer.h:69
Definition: Packet.h:27
Definition: PointerVector.h:58
Definition: SSLLayer.h:418
void computeCalculateFields() override
Definition: SSLLayer.h:450
SSLAlertLevel getAlertLevel() const
std::string toString() const override
SSLAlertDescription getAlertDescription()
SSLAlertLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SSLLayer.h:427
Definition: SSLLayer.h:460
uint8_t * getEncryptedData() const
std::string toString() const override
void computeCalculateFields() override
Definition: SSLLayer.h:493
size_t getEncryptedDataLen() const
SSLApplicationDataLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SSLLayer.h:469
Definition: SSLLayer.h:386
SSLChangeCipherSpecLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SSLLayer.h:395
std::string toString() const override
void computeCalculateFields() override
Definition: SSLLayer.h:408
Definition: SSLLayer.h:319
SSLHandshakeLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
std::string toString() const override
size_t getHandshakeMessagesCount() const
Definition: SSLLayer.h:333
THandshakeMessage * getHandshakeMessageOfType() const
Definition: SSLLayer.h:497
void computeCalculateFields() override
Definition: SSLLayer.h:373
THandshakeMessage * getNextHandshakeMessageOfType(const SSLHandshakeMessage *after) const
Definition: SSLLayer.h:512
SSLHandshakeMessage * getHandshakeMessageAt(int index) const
Definition: SSLHandshake.h:338
Definition: SSLLayer.h:175
SSLVersion getRecordVersion() const
size_t getHeaderLen() const override
void parseNextLayer() override
OsiModelLayer getOsiModelLayer() const override
Definition: SSLLayer.h:259
static bool isSSLPort(uint16_t port)
Definition: SSLLayer.h:542
static bool IsSSLMessage(uint16_t srcPort, uint16_t dstPort, uint8_t *data, size_t dataLen, bool ignorePorts=false)
static SSLLayer * createSSLMessage(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
ssl_tls_record_layer * getRecordLayer() const
Definition: SSLLayer.h:231
SSLRecordType getRecordType() const
Definition: SSLCommon.h:112
The main namespace for the PcapPlusPlus lib.
SSLAlertDescription
Definition: SSLCommon.h:266
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelPresentationLayer
Definition: ProtocolType.h:376
const ProtocolType SSL
Definition: ProtocolType.h:138
SSLRecordType
Definition: SSLCommon.h:94
SSLAlertLevel
Definition: SSLCommon.h:253
Definition: SSLCommon.h:24