PcapPlusPlus  Next
SSLHandshake.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include "SSLCommon.h"
5 #include "PointerVector.h"
6 #include "Asn1Codec.h"
7 
17 namespace pcpp
18 {
19 
30  {
31  public:
42  SSLSymetricEncryptionAlgorithm symKeyAlg, SSLHashingAlgorithm MACAlg, const char* name)
43  : m_Id(id), m_KeyExAlg(keyExAlg), m_AuthAlg(authAlg), m_SymKeyAlg(symKeyAlg), m_MACAlg(MACAlg), m_Name(name)
44  {}
45 
49  uint16_t getID() const
50  {
51  return m_Id;
52  }
53 
57  std::string asString() const
58  {
59  return m_Name;
60  }
61 
66  {
67  return m_KeyExAlg;
68  }
69 
74  {
75  return m_AuthAlg;
76  }
77 
82  {
83  return m_SymKeyAlg;
84  }
85 
90  {
91  return m_MACAlg;
92  }
93 
99  static SSLCipherSuite* getCipherSuiteByID(uint16_t id);
100 
106  static SSLCipherSuite* getCipherSuiteByName(std::string name);
107 
108  private:
109  uint16_t m_Id;
110  SSLKeyExchangeAlgorithm m_KeyExAlg;
111  SSLAuthenticationAlgorithm m_AuthAlg;
112  SSLSymetricEncryptionAlgorithm m_SymKeyAlg;
113  SSLHashingAlgorithm m_MACAlg;
114  std::string m_Name;
115  };
116 
124  {
125  public:
130  explicit SSLExtension(uint8_t* data);
131 
132  virtual ~SSLExtension() = default;
133 
138 
142  uint16_t getTypeAsInt() const;
143 
147  uint16_t getLength() const;
148 
152  uint16_t getTotalLength() const;
153 
157  uint8_t* getData() const;
158 
159  protected:
165  {
167  uint16_t extensionType;
171  uint8_t extensionData[];
172  };
173 
174  uint8_t* m_RawData;
175 
176  SSLExtensionStruct* getExtensionStruct() const
177  {
178  return reinterpret_cast<SSLExtensionStruct*>(m_RawData);
179  }
180  };
181 
188  {
189  public:
194  explicit SSLServerNameIndicationExtension(uint8_t* data) : SSLExtension(data)
195  {}
196 
200  std::string getHostName() const;
201  };
202 
209  {
210  public:
215  explicit SSLSupportedVersionsExtension(uint8_t* data) : SSLExtension(data)
216  {}
217 
221  std::vector<SSLVersion> getSupportedVersions() const;
222  };
223 
230  {
231  public:
236  explicit TLSSupportedGroupsExtension(uint8_t* data) : SSLExtension(data)
237  {}
238 
242  std::vector<uint16_t> getSupportedGroups() const;
243  };
244 
251  {
252  public:
257  explicit TLSECPointFormatExtension(uint8_t* data) : SSLExtension(data)
258  {}
259 
263  std::vector<uint8_t> getECPointFormatList() const;
264  };
265 
274  {
275  public:
284  SSLx509Certificate(uint8_t* data, size_t dataLen, bool allDataExists)
285  : m_Data(data), m_DataLen(dataLen), m_AllDataExists(allDataExists)
286  {}
287 
291  uint8_t* getData() const
292  {
293  return m_Data;
294  }
295 
299  size_t getDataLength() const
300  {
301  return m_DataLen;
302  }
303 
309 
315  bool allDataExists() const
316  {
317  return m_AllDataExists;
318  }
319 
320  private:
321  std::unique_ptr<Asn1Record> m_Asn1Record;
322  uint8_t* m_Data;
323  size_t m_DataLen;
324  bool m_AllDataExists;
325  };
326 
327  class SSLHandshakeLayer;
328 
338  {
339  public:
340  virtual ~SSLHandshakeMessage() = default;
341 
349  static SSLHandshakeMessage* createHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
350 
355 
360  virtual size_t getMessageLength() const;
361 
368  virtual bool isMessageComplete() const;
369 
374  {
375  return m_Container;
376  }
377 
381  virtual std::string toString() const = 0;
382 
383  protected:
384  SSLHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
385 
386  uint8_t* m_Data;
387  size_t m_DataLen;
388  SSLHandshakeLayer* m_Container;
389  };
390 
397  {
398  public:
408  {
410  uint16_t tlsVersion;
412  std::vector<uint16_t> cipherSuites;
414  std::vector<uint16_t> extensions;
416  std::vector<uint16_t> supportedGroups;
418  std::vector<uint8_t> ecPointFormats;
419 
431  std::string toString();
432 
436  std::string toMD5();
437 
442  std::pair<std::string, std::string> toStringAndMD5();
443  };
444 
452  SSLClientHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
453 
454  ~SSLClientHelloMessage() override = default;
455 
461  {
462  return reinterpret_cast<ssl_tls_client_server_hello*>(m_Data);
463  }
464 
471 
475  uint8_t getSessionIDLength() const;
476 
480  uint8_t* getSessionID() const;
481 
485  int getCipherSuiteCount() const;
486 
495  SSLCipherSuite* getCipherSuite(int index) const;
496 
507  uint16_t getCipherSuiteID(int index, bool& isValid) const;
508 
512  uint8_t getCompressionMethodsValue() const;
513 
517  int getExtensionCount() const;
518 
523  uint16_t getExtensionsLength() const;
524 
532  SSLExtension* getExtension(int index) const;
533 
541  SSLExtension* getExtensionOfType(uint16_t type) const;
542 
549 
556  template <class TExtension> TExtension* getExtensionOfType() const;
557 
569 
570  // implement abstract methods
571 
572  std::string toString() const override;
573 
574  private:
575  PointerVector<SSLExtension> m_ExtensionList;
576  };
577 
584  {
585  public:
594  {
596  uint16_t tlsVersion;
598  uint16_t cipherSuite;
600  std::vector<uint16_t> extensions;
601 
609  std::string toString();
610 
614  std::string toMD5();
615 
620  std::pair<std::string, std::string> toStringAndMD5();
621  };
622 
630  SSLServerHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
631 
632  ~SSLServerHelloMessage() override = default;
633 
639  {
640  return reinterpret_cast<ssl_tls_client_server_hello*>(m_Data);
641  }
642 
653 
657  uint8_t getSessionIDLength() const;
658 
662  uint8_t* getSessionID() const;
663 
671 
680  uint16_t getCipherSuiteID(bool& isValid) const;
681 
685  uint8_t getCompressionMethodsValue() const;
686 
690  int getExtensionCount() const;
691 
696  uint16_t getExtensionsLength() const;
697 
705  SSLExtension* getExtension(int index) const;
706 
714  SSLExtension* getExtensionOfType(uint16_t type) const;
715 
722 
729  template <class TExtension> TExtension* getExtensionOfType() const;
730 
743 
744  // implement abstract methods
745 
746  std::string toString() const override;
747 
748  private:
749  PointerVector<SSLExtension> m_ExtensionList;
750  };
751 
761  {
762  public:
770  SSLCertificateMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
771 
772  ~SSLCertificateMessage() override = default;
773 
781  int getNumOfCertificates() const;
782 
790 
791  // implement abstract methods
792 
793  std::string toString() const override;
794 
795  private:
796  PointerVector<SSLx509Certificate> m_CertificateList;
797  };
798 
805  {
806  public:
814  SSLHelloRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
815  : SSLHandshakeMessage(data, dataLen, container)
816  {}
817 
818  ~SSLHelloRequestMessage() override = default;
819 
820  // implement abstract methods
821 
822  std::string toString() const override;
823  };
824 
832  {
833  public:
841  SSLServerKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
842  : SSLHandshakeMessage(data, dataLen, container)
843  {}
844 
845  ~SSLServerKeyExchangeMessage() override = default;
846 
852  uint8_t* getServerKeyExchangeParams() const;
853 
861 
862  // implement abstract methods
863 
864  std::string toString() const override;
865  };
866 
874  {
875  public:
883  SSLClientKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
884  : SSLHandshakeMessage(data, dataLen, container)
885  {}
886 
887  ~SSLClientKeyExchangeMessage() override = default;
888 
894  uint8_t* getClientKeyExchangeParams() const;
895 
903 
904  // implement abstract methods
905 
906  std::string toString() const override;
907  };
908 
915  {
916  public:
924  SSLCertificateRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
925 
926  ~SSLCertificateRequestMessage() override = default;
927 
931  std::vector<SSLClientCertificateType>& getCertificateTypes();
932 
938  uint8_t* getCertificateAuthorityData() const;
939 
947 
948  // implement abstract methods
949 
950  std::string toString() const override;
951 
952  private:
953  std::vector<SSLClientCertificateType> m_ClientCertificateTypes;
954  };
955 
962  {
963  public:
971  SSLServerHelloDoneMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
972  : SSLHandshakeMessage(data, dataLen, container)
973  {}
974 
975  ~SSLServerHelloDoneMessage() override = default;
976 
977  // implement abstract methods
978 
979  std::string toString() const override;
980  };
981 
989  {
990  public:
998  SSLCertificateVerifyMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
999  : SSLHandshakeMessage(data, dataLen, container)
1000  {}
1001 
1002  ~SSLCertificateVerifyMessage() override = default;
1003 
1009  uint8_t* getSignedHash() const;
1010 
1017  size_t getSignedHashLength() const;
1018 
1019  // implement abstract methods
1020 
1021  std::string toString() const override;
1022  };
1023 
1031  {
1032  public:
1040  SSLFinishedMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1041  : SSLHandshakeMessage(data, dataLen, container)
1042  {}
1043 
1044  ~SSLFinishedMessage() override = default;
1045 
1051  uint8_t* getSignedHash() const;
1052 
1059  size_t getSignedHashLength() const;
1060 
1061  // implement abstract methods
1062 
1063  std::string toString() const override;
1064  };
1065 
1072  {
1073  public:
1081  SSLNewSessionTicketMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1082  : SSLHandshakeMessage(data, dataLen, container)
1083  {}
1084 
1085  ~SSLNewSessionTicketMessage() override = default;
1086 
1092  uint8_t* getSessionTicketData() const;
1093 
1101 
1102  // implement abstract methods
1103 
1104  std::string toString() const override;
1105  };
1106 
1114  {
1115  public:
1123  SSLUnknownMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1124  : SSLHandshakeMessage(data, dataLen, container)
1125  {}
1126 
1127  ~SSLUnknownMessage() override = default;
1128 
1129  // implement virtual and abstract methods
1130 
1135 
1141  size_t getMessageLength() const override;
1142 
1143  std::string toString() const override;
1144  };
1145 
1146  template <class TExtension> TExtension* SSLClientHelloMessage::getExtensionOfType() const
1147  {
1148  size_t vecSize = m_ExtensionList.size();
1149  for (size_t i = 0; i < vecSize; i++)
1150  {
1151  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1152  if (dynamic_cast<TExtension*>(curElem) != nullptr)
1153  return static_cast<TExtension*>(curElem);
1154  }
1155 
1156  return nullptr;
1157  }
1158 
1159  template <class TExtension> TExtension* SSLServerHelloMessage::getExtensionOfType() const
1160  {
1161  size_t vecSize = m_ExtensionList.size();
1162  for (size_t i = 0; i < vecSize; i++)
1163  {
1164  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1165  if (dynamic_cast<TExtension*>(curElem) != nullptr)
1166  return static_cast<TExtension*>(curElem);
1167  }
1168 
1169  return nullptr;
1170  }
1171 
1172 } // namespace pcpp
Definition: Asn1Codec.h:366
Definition: PointerVector.h:58
Definition: SSLHandshake.h:761
std::string toString() const override
SSLCertificateMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
SSLx509Certificate * getCertificate(int index) const
Definition: SSLHandshake.h:915
size_t getCertificateAuthorityLength() const
std::vector< SSLClientCertificateType > & getCertificateTypes()
uint8_t * getCertificateAuthorityData() const
std::string toString() const override
SSLCertificateRequestMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:989
SSLCertificateVerifyMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:998
std::string toString() const override
Definition: SSLHandshake.h:30
SSLAuthenticationAlgorithm getAuthAlg() const
Definition: SSLHandshake.h:73
SSLHashingAlgorithm getMACAlg() const
Definition: SSLHandshake.h:89
uint16_t getID() const
Definition: SSLHandshake.h:49
SSLCipherSuite(uint16_t id, SSLKeyExchangeAlgorithm keyExAlg, SSLAuthenticationAlgorithm authAlg, SSLSymetricEncryptionAlgorithm symKeyAlg, SSLHashingAlgorithm MACAlg, const char *name)
Definition: SSLHandshake.h:41
SSLSymetricEncryptionAlgorithm getSymKeyAlg() const
Definition: SSLHandshake.h:81
SSLKeyExchangeAlgorithm getKeyExchangeAlg() const
Definition: SSLHandshake.h:65
static SSLCipherSuite * getCipherSuiteByName(std::string name)
std::string asString() const
Definition: SSLHandshake.h:57
static SSLCipherSuite * getCipherSuiteByID(uint16_t id)
Definition: SSLHandshake.h:397
uint16_t getCipherSuiteID(int index, bool &isValid) const
uint16_t getExtensionsLength() const
SSLExtension * getExtensionOfType(uint16_t type) const
SSLExtension * getExtensionOfType(SSLExtensionType type) const
SSLClientHelloMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
std::string toString() const override
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1146
uint8_t getSessionIDLength() const
SSLCipherSuite * getCipherSuite(int index) const
ssl_tls_client_server_hello * getClientHelloHeader() const
Definition: SSLHandshake.h:460
uint8_t * getSessionID() const
uint8_t getCompressionMethodsValue() const
SSLExtension * getExtension(int index) const
ClientHelloTLSFingerprint generateTLSFingerprint() const
SSLVersion getHandshakeVersion() const
Definition: SSLHandshake.h:874
SSLClientKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:883
std::string toString() const override
uint8_t * getClientKeyExchangeParams() const
size_t getClientKeyExchangeParamsLength() const
Definition: SSLHandshake.h:124
uint16_t getLength() const
SSLExtension(uint8_t *data)
uint16_t getTotalLength() const
uint16_t getTypeAsInt() const
SSLExtensionType getType() const
uint8_t * getData() const
Definition: SSLHandshake.h:1031
uint8_t * getSignedHash() const
std::string toString() const override
SSLFinishedMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1040
size_t getSignedHashLength() const
Definition: SSLLayer.h:319
Definition: SSLHandshake.h:338
static SSLHandshakeMessage * createHandshakeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
virtual size_t getMessageLength() const
virtual SSLHandshakeType getHandshakeType() const
virtual std::string toString() const =0
SSLHandshakeLayer * getContainingLayer() const
Definition: SSLHandshake.h:373
virtual bool isMessageComplete() const
Definition: SSLHandshake.h:805
SSLHelloRequestMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:814
std::string toString() const override
Definition: SSLHandshake.h:1072
size_t getSessionTicketDataLength() const
SSLNewSessionTicketMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1081
std::string toString() const override
uint8_t * getSessionTicketData() const
Definition: SSLHandshake.h:962
std::string toString() const override
SSLServerHelloDoneMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:971
Definition: SSLHandshake.h:584
uint8_t getSessionIDLength() const
SSLExtension * getExtensionOfType(uint16_t type) const
uint16_t getExtensionsLength() const
SSLServerHelloMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
SSLExtension * getExtensionOfType(SSLExtensionType type) const
std::string toString() const override
ssl_tls_client_server_hello * getServerHelloHeader() const
Definition: SSLHandshake.h:638
SSLVersion getHandshakeVersion() const
uint16_t getCipherSuiteID(bool &isValid) const
uint8_t getCompressionMethodsValue() const
SSLExtension * getExtension(int index) const
SSLCipherSuite * getCipherSuite() const
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1159
ServerHelloTLSFingerprint generateTLSFingerprint() const
uint8_t * getSessionID() const
Definition: SSLHandshake.h:832
SSLServerKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:841
std::string toString() const override
uint8_t * getServerKeyExchangeParams() const
size_t getServerKeyExchangeParamsLength() const
Definition: SSLHandshake.h:188
SSLServerNameIndicationExtension(uint8_t *data)
Definition: SSLHandshake.h:194
Definition: SSLHandshake.h:209
std::vector< SSLVersion > getSupportedVersions() const
SSLSupportedVersionsExtension(uint8_t *data)
Definition: SSLHandshake.h:215
Definition: SSLHandshake.h:1114
SSLHandshakeType getHandshakeType() const override
std::string toString() const override
SSLUnknownMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1123
size_t getMessageLength() const override
Definition: SSLCommon.h:112
Definition: SSLHandshake.h:274
SSLx509Certificate(uint8_t *data, size_t dataLen, bool allDataExists)
Definition: SSLHandshake.h:284
Asn1SequenceRecord * getRootAsn1Record()
size_t getDataLength() const
Definition: SSLHandshake.h:299
bool allDataExists() const
Definition: SSLHandshake.h:315
uint8_t * getData() const
Definition: SSLHandshake.h:291
Definition: SSLHandshake.h:251
TLSECPointFormatExtension(uint8_t *data)
Definition: SSLHandshake.h:257
std::vector< uint8_t > getECPointFormatList() const
Definition: SSLHandshake.h:230
std::vector< uint16_t > getSupportedGroups() const
TLSSupportedGroupsExtension(uint8_t *data)
Definition: SSLHandshake.h:236
The main namespace for the PcapPlusPlus lib.
SSLKeyExchangeAlgorithm
Definition: SSLCommon.h:325
SSLExtensionType
Definition: SSLCommon.h:501
SSLSymetricEncryptionAlgorithm
Definition: SSLCommon.h:391
SSLAuthenticationAlgorithm
Definition: SSLCommon.h:358
SSLHandshakeType
Definition: SSLCommon.h:216
SSLHashingAlgorithm
Definition: SSLCommon.h:474
std::vector< uint8_t > ecPointFormats
Definition: SSLHandshake.h:418
std::pair< std::string, std::string > toStringAndMD5()
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:414
std::vector< uint16_t > cipherSuites
Definition: SSLHandshake.h:412
std::vector< uint16_t > supportedGroups
Definition: SSLHandshake.h:416
uint16_t tlsVersion
Definition: SSLHandshake.h:410
Definition: SSLHandshake.h:165
uint8_t extensionData[]
Definition: SSLHandshake.h:171
uint16_t extensionType
Definition: SSLHandshake.h:167
uint16_t extensionDataLength
Definition: SSLHandshake.h:169
uint16_t tlsVersion
Definition: SSLHandshake.h:596
uint16_t cipherSuite
Definition: SSLHandshake.h:598
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:600
std::pair< std::string, std::string > toStringAndMD5()
Definition: SSLCommon.h:56