PcapPlusPlus  24.09
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 (ssl_tls_record_layer*)m_Data;
234  }
235 
240 
245 
246  // implement abstract methods
247 
251  size_t getHeaderLen() const;
252 
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;
369 
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 
400  {}
401 
402  // implement abstract methods
403 
404  std::string toString() const;
405 
410  {}
411  }; // class SSLChangeCipherSpecLayer
412 
418  class SSLAlertLayer : public SSLLayer
419  {
420  public:
428  SSLAlertLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
429  : SSLLayer(data, dataLen, prevLayer, packet)
430  {}
431 
432  ~SSLAlertLayer()
433  {}
434 
439 
444 
445  // implement abstract methods
446 
447  std::string toString() const;
448 
453  {}
454  }; // class SSLAlertLayer
455 
462  {
463  public:
471  SSLApplicationDataLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
472  : SSLLayer(data, dataLen, prevLayer, packet)
473  {}
474 
476  {}
477 
482  uint8_t* getEncryptedData() const;
483 
487  size_t getEncryptedDataLen() const;
488 
489  // implement abstract methods
490 
491  std::string toString() const;
492 
497  {}
498  }; // class SSLApplicationDataLayer
499 
500  template <class THandshakeMessage> THandshakeMessage* SSLHandshakeLayer::getHandshakeMessageOfType() const
501  {
502  size_t vecSize = m_MessageList.size();
503  for (size_t i = 0; i < vecSize; i++)
504  {
505  SSLHandshakeMessage* curElem = const_cast<SSLHandshakeMessage*>(m_MessageList.at(i));
506  if (dynamic_cast<THandshakeMessage*>(curElem) != nullptr)
507  return (THandshakeMessage*)curElem;
508  }
509 
510  // element not found
511  return nullptr;
512  } // getHandshakeMessageOfType
513 
514  template <class THandshakeMessage>
516  {
517  size_t vecSize = m_MessageList.size();
518  size_t afterIndex;
519 
520  // find the index of "after"
521  for (afterIndex = 0; afterIndex < vecSize; afterIndex++)
522  {
523  SSLHandshakeMessage* curElem = const_cast<SSLHandshakeMessage*>(m_MessageList.at(afterIndex));
524  if (curElem == after)
525  break;
526  }
527 
528  // "after" not found
529  if (afterIndex == vecSize)
530  return nullptr;
531 
532  for (size_t i = afterIndex + 1; i < vecSize; i++)
533  {
534  SSLHandshakeMessage* curElem = const_cast<SSLHandshakeMessage*>(m_MessageList.at(i));
535  if (dynamic_cast<THandshakeMessage*>(curElem) != nullptr)
536  return (THandshakeMessage*)curElem;
537  }
538 
539  // element not found
540  return nullptr;
541  } // getNextHandshakeMessageOfType
542 
543  // implementation of inline methods
544 
545  bool SSLLayer::isSSLPort(uint16_t port)
546  {
547  if (port == 443) // HTTPS, this is likely case
548  return true;
549 
550  switch (port)
551  {
552  case 261: // NSIIOPS
553  case 448: // DDM-SSL
554  case 465: // SMTPS
555  case 563: // NNTPS
556  case 614: // SSHELL
557  case 636: // LDAPS
558  case 989: // FTPS - data
559  case 990: // FTPS - control
560  case 992: // Telnet over TLS/SSL
561  case 993: // IMAPS
562  case 994: // IRCS
563  case 995: // POP3S
564  return true;
565  default:
566  return false;
567  }
568  } // isSSLPort
569 
570 } // namespace pcpp
Definition: Layer.h:70
Definition: Packet.h:27
Definition: PointerVector.h:29
Definition: SSLLayer.h:419
SSLAlertLevel getAlertLevel() const
std::string toString() const
SSLAlertDescription getAlertDescription()
void computeCalculateFields()
Definition: SSLLayer.h:452
SSLAlertLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SSLLayer.h:428
Definition: SSLLayer.h:462
void computeCalculateFields()
Definition: SSLLayer.h:496
uint8_t * getEncryptedData() const
std::string toString() const
size_t getEncryptedDataLen() const
SSLApplicationDataLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SSLLayer.h:471
Definition: SSLLayer.h:386
void computeCalculateFields()
Definition: SSLLayer.h:409
std::string toString() const
SSLChangeCipherSpecLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SSLLayer.h:395
Definition: SSLLayer.h:319
SSLHandshakeLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
size_t getHandshakeMessagesCount() const
Definition: SSLLayer.h:333
THandshakeMessage * getHandshakeMessageOfType() const
Definition: SSLLayer.h:500
std::string toString() const
THandshakeMessage * getNextHandshakeMessageOfType(const SSLHandshakeMessage *after) const
Definition: SSLLayer.h:515
void computeCalculateFields()
Definition: SSLLayer.h:373
SSLHandshakeMessage * getHandshakeMessageAt(int index) const
Definition: SSLHandshake.h:339
Definition: SSLLayer.h:175
SSLVersion getRecordVersion() const
OsiModelLayer getOsiModelLayer() const
Definition: SSLLayer.h:259
size_t getHeaderLen() const
static bool isSSLPort(uint16_t port)
Definition: SSLLayer.h:545
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
void parseNextLayer()
SSLRecordType getRecordType() const
Definition: SSLCommon.h:112
The main namespace for the PcapPlusPlus lib.
SSLAlertDescription
Definition: SSLCommon.h:266
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelPresentationLayer
Definition: ProtocolType.h:366
const ProtocolType SSL
Definition: ProtocolType.h:138
SSLRecordType
Definition: SSLCommon.h:94
SSLAlertLevel
Definition: SSLCommon.h:253
Definition: SSLCommon.h:24