PcapPlusPlus  22.05
SSLHandshake.h
Go to the documentation of this file.
1 #ifndef PACKETPP_SSL_HANDSHAKE_MESSAGE
2 #define PACKETPP_SSL_HANDSHAKE_MESSAGE
3 
4 #include <utility>
5 #include "SSLCommon.h"
6 #include "PointerVector.h"
7 
8 #ifndef PCPP_DEPRECATED
9 #if defined(__GNUC__) || defined(__clang__)
10 #define PCPP_DEPRECATED __attribute__((deprecated))
11 #elif defined(_MSC_VER)
12 #define PCPP_DEPRECATED __declspec(deprecated)
13 #else
14 #pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
15 #define PCPP_DEPRECATED
16 #endif
17 #endif
18 
28 namespace pcpp
29 {
30 
31 
42 {
43 public:
53  SSLCipherSuite(uint16_t id, SSLKeyExchangeAlgorithm keyExAlg,
56  SSLHashingAlgorithm MACAlg,
57  const char* name)
58  : m_Id(id), m_KeyExAlg(keyExAlg), m_AuthAlg(authAlg), m_SymKeyAlg(symKeyAlg), m_MACAlg(MACAlg), m_Name(name) {}
59 
63  uint16_t getID() const { return m_Id; }
64 
68  std::string asString() const { return m_Name; }
69 
73  SSLKeyExchangeAlgorithm getKeyExchangeAlg() const { return m_KeyExAlg; }
74 
78  SSLAuthenticationAlgorithm getAuthAlg() const { return m_AuthAlg; }
79 
83  SSLSymetricEncryptionAlgorithm getSymKeyAlg() const { return m_SymKeyAlg; }
84 
88  SSLHashingAlgorithm getMACAlg() const { return m_MACAlg; }
89 
95  static SSLCipherSuite* getCipherSuiteByID(uint16_t id);
96 
102  static SSLCipherSuite* getCipherSuiteByName(std::string name);
103 
104 private:
105  uint16_t m_Id;
106  SSLKeyExchangeAlgorithm m_KeyExAlg;
107  SSLAuthenticationAlgorithm m_AuthAlg;
108  SSLSymetricEncryptionAlgorithm m_SymKeyAlg;
109  SSLHashingAlgorithm m_MACAlg;
110  std::string m_Name;
111 };
112 
113 
121 {
122 public:
127  SSLExtension(uint8_t* data);
128 
129  virtual ~SSLExtension() { }
130 
134  SSLExtensionType getType() const;
135 
139  uint16_t getTypeAsInt() const;
140 
144  uint16_t getLength() const;
145 
149  uint16_t getTotalLength() const;
150 
154  uint8_t* getData() const;
155 
156 protected:
157 
163  {
165  uint16_t extensionType;
169  uint8_t extensionData[];
170  };
171 
172  uint8_t* m_RawData;
173 
174  SSLExtensionStruct* getExtensionStruct() const { return (SSLExtensionStruct*)m_RawData; }
175 };
176 
177 
184 {
185 public:
191 
195  std::string getHostName() const;
196 };
197 
198 
205 {
206 public:
211  SSLSupportedVersionsExtension(uint8_t* data) : SSLExtension(data) {}
212 
216  std::vector<SSLVersion> getSupportedVersions() const;
217 };
218 
219 
226 {
227  public:
232  TLSSupportedGroupsExtension(uint8_t* data) : SSLExtension(data) {}
233 
237  std::vector<uint16_t> getSupportedGroups() const;
238 };
239 
240 
247 {
248  public:
253  TLSECPointFormatExtension(uint8_t* data) : SSLExtension(data) {}
254 
258  std::vector<uint8_t> getECPointFormatList() const;
259 };
260 
261 
269 {
270 public:
271 
280  SSLx509Certificate(uint8_t* data, size_t dataLen, bool allDataExists)
281  : m_Data(data), m_DataLen(dataLen), m_AllDataExists(allDataExists) {}
282 
286  uint8_t* getData() const { return m_Data; }
287 
291  size_t getDataLength() const { return m_DataLen; }
292 
298  bool allDataExists() const { return m_AllDataExists; }
299 
300 private:
301  uint8_t* m_Data;
302  size_t m_DataLen;
303  bool m_AllDataExists;
304 };
305 
306 
307 class SSLHandshakeLayer;
308 
309 
319 {
320 public:
321 
322  virtual ~SSLHandshakeMessage() {}
323 
327  PCPP_DEPRECATED static SSLHandshakeMessage* createHandhakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) { return createHandshakeMessage(data, dataLen, container);}
328 
336  static SSLHandshakeMessage* createHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
337 
341  virtual SSLHandshakeType getHandshakeType() const;
342 
347  virtual size_t getMessageLength() const;
348 
355  virtual bool isMessageComplete() const;
356 
360  SSLHandshakeLayer* getContainingLayer() const { return m_Container; }
361 
365  virtual std::string toString() const = 0;
366 
367 protected:
368 
369  SSLHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
370 
371  uint8_t* m_Data;
372  size_t m_DataLen;
373  SSLHandshakeLayer* m_Container;
374 
375 };
376 
377 
384 {
385 public:
386 
396  {
398  uint16_t tlsVersion;
400  std::vector<uint16_t> cipherSuites;
402  std::vector<uint16_t> extensions;
404  std::vector<uint16_t> supportedGroups;
406  std::vector<uint8_t> ecPointFormats;
407 
418  std::string toString();
419 
423  std::string toMD5();
424 
429  std::pair<std::string, std::string> toStringAndMD5();
430  };
431 
439  SSLClientHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
440 
441  virtual ~SSLClientHelloMessage() {}
442 
448 
453  SSLVersion getHandshakeVersion() const;
454 
458  uint8_t getSessionIDLength() const;
459 
463  uint8_t* getSessionID() const;
464 
468  int getCipherSuiteCount() const;
469 
478  SSLCipherSuite* getCipherSuite(int index) const;
479 
489  uint16_t getCipherSuiteID(int index, bool& isValid) const;
490 
494  uint8_t getCompressionMethodsValue() const;
495 
499  int getExtensionCount() const;
500 
504  PCPP_DEPRECATED uint16_t getExtensionsLenth() const { return getExtensionsLength(); };
505 
509  uint16_t getExtensionsLength() const;
510 
518  SSLExtension* getExtension(int index) const;
519 
527  SSLExtension* getExtensionOfType(uint16_t type) const;
528 
534  SSLExtension* getExtensionOfType(SSLExtensionType type) const;
535 
542  template<class TExtension>
543  TExtension* getExtensionOfType() const;
544 
555  ClientHelloTLSFingerprint generateTLSFingerprint() const;
556 
557  // implement abstract methods
558 
559  std::string toString() const;
560 
561 private:
562  PointerVector<SSLExtension> m_ExtensionList;
563 
564 };
565 
566 
573 {
574 public:
575 
584  {
586  uint16_t tlsVersion;
588  uint16_t cipherSuite;
590  std::vector<uint16_t> extensions;
591 
599  std::string toString();
600 
604  std::string toMD5();
605 
610  std::pair<std::string, std::string> toStringAndMD5();
611  };
612 
620  SSLServerHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
621 
622  virtual ~SSLServerHelloMessage() {}
623 
629 
638  SSLVersion getHandshakeVersion() const;
639 
643  uint8_t getSessionIDLength() const;
644 
648  uint8_t* getSessionID() const;
649 
656  SSLCipherSuite* getCipherSuite() const;
657 
665  uint16_t getCipherSuiteID(bool& isValid) const;
666 
670  uint8_t getCompressionMethodsValue() const;
671 
675  int getExtensionCount() const;
676 
680  PCPP_DEPRECATED uint16_t getExtensionsLenth() const { return getExtensionsLength(); };
681 
685  uint16_t getExtensionsLength() const;
686 
694  SSLExtension* getExtension(int index) const;
695 
703  SSLExtension* getExtensionOfType(uint16_t type) const;
704 
710  SSLExtension* getExtensionOfType(SSLExtensionType type) const;
711 
718  template<class TExtension>
719  TExtension* getExtensionOfType() const;
720 
732  ServerHelloTLSFingerprint generateTLSFingerprint() const;
733 
734  // implement abstract methods
735 
736  std::string toString() const;
737 
738 private:
739  PointerVector<SSLExtension> m_ExtensionList;
740 };
741 
742 
752 {
753 public:
754 
762  SSLCertificateMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
763 
764  virtual ~SSLCertificateMessage() {}
765 
773  int getNumOfCertificates() const;
774 
781  SSLx509Certificate* getCertificate(int index) const;
782 
783  // implement abstract methods
784 
785  std::string toString() const;
786 
787 private:
788  PointerVector<SSLx509Certificate> m_CertificateList;
789 };
790 
791 
798 {
799 public:
800 
808  SSLHelloRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
809 
810  virtual ~SSLHelloRequestMessage() {}
811 
812  // implement abstract methods
813 
814  std::string toString() const;
815 };
816 
817 
825 {
826 public:
827 
835  SSLServerKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
836 
838 
844  uint8_t* getServerKeyExchangeParams() const;
845 
852  size_t getServerKeyExchangeParamsLength() const;
853 
854  // implement abstract methods
855 
856  std::string toString() const;
857 };
858 
859 
867 {
868 public:
869 
877  SSLClientKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
878 
880 
886  uint8_t* getClientKeyExchangeParams() const;
887 
894  size_t getClientKeyExchangeParamsLength() const;
895 
896  // implement abstract methods
897 
898  std::string toString() const;
899 };
900 
901 
908 {
909 public:
910 
918  SSLCertificateRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
919 
921 
925  std::vector<SSLClientCertificateType>& getCertificateTypes();
926 
932  uint8_t* getCertificateAuthorityData() const;
933 
940  size_t getCertificateAuthorityLength() const;
941 
942  // implement abstract methods
943 
944  std::string toString() const;
945 
946 private:
947  std::vector<SSLClientCertificateType> m_ClientCertificateTypes;
948 };
949 
950 
957 {
958 public:
959 
967  SSLServerHelloDoneMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
968 
969  virtual ~SSLServerHelloDoneMessage() {}
970 
971  // implement abstract methods
972 
973  std::string toString() const;
974 };
975 
976 
984 {
985 public:
986 
994  SSLCertificateVerifyMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
995 
996  virtual ~SSLCertificateVerifyMessage() {}
997 
1003  uint8_t* getSignedHash() const;
1004 
1011  size_t getSignedHashLength() const;
1012 
1013  // implement abstract methods
1014 
1015  std::string toString() const;
1016 };
1017 
1018 
1026 {
1027 public:
1028 
1036  SSLFinishedMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
1037 
1038  virtual ~SSLFinishedMessage() {}
1039 
1045  uint8_t* getSignedHash() const;
1046 
1053  size_t getSignedHashLength() const;
1054 
1055  // implement abstract methods
1056 
1057  std::string toString() const;
1058 };
1059 
1060 
1067 {
1068 public:
1069 
1077  SSLNewSessionTicketMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
1078 
1079  virtual ~SSLNewSessionTicketMessage() {}
1080 
1086  uint8_t* getSessionTicketData() const;
1087 
1094  size_t getSessionTicketDataLength() const;
1095 
1096  // implement abstract methods
1097 
1098  std::string toString() const;
1099 };
1100 
1101 
1109 {
1110 public:
1111 
1119  SSLUnknownMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
1120 
1121  virtual ~SSLUnknownMessage() {}
1122 
1123  // implement virtual and abstract methods
1124 
1128  SSLHandshakeType getHandshakeType() const;
1129 
1135  size_t getMessageLength() const;
1136 
1137  std::string toString() const;
1138 };
1139 
1140 template<class TExtension>
1142 {
1143  size_t vecSize = m_ExtensionList.size();
1144  for (size_t i = 0; i < vecSize; i++)
1145  {
1146  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1147  if (dynamic_cast<TExtension*>(curElem) != NULL)
1148  return (TExtension*)curElem;
1149  }
1150 
1151  return NULL;
1152 }
1153 
1154 template<class TExtension>
1156 {
1157  size_t vecSize = m_ExtensionList.size();
1158  for (size_t i = 0; i < vecSize; i++)
1159  {
1160  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1161  if (dynamic_cast<TExtension*>(curElem) != NULL)
1162  return (TExtension*)curElem;
1163  }
1164 
1165  return NULL;
1166 }
1167 
1168 } // namespace pcpp
1169 
1170 #endif /* PACKETPP_SSL_HANDSHAKE_MESSAGE */
The main namespace for the PcapPlusPlus lib.
static SSLCipherSuite * getCipherSuiteByID(uint16_t id)
Definition: SSLHandshake.h:268
bool allDataExists() const
Definition: SSLHandshake.h:298
uint16_t extensionDataLength
Definition: SSLHandshake.h:167
SSLSymetricEncryptionAlgorithm getSymKeyAlg() const
Definition: SSLHandshake.h:83
SSLHandshakeLayer * getContainingLayer() const
Definition: SSLHandshake.h:360
SSLKeyExchangeAlgorithm
Definition: SSLCommon.h:325
uint16_t getID() const
Definition: SSLHandshake.h:63
SSLUnknownMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1119
Definition: PointerVector.h:24
Definition: SSLLayer.h:306
Definition: SSLHandshake.h:183
uint16_t cipherSuite
Definition: SSLHandshake.h:588
Definition: SSLHandshake.h:956
SSLExtensionType
Definition: SSLCommon.h:501
SSLServerNameIndicationExtension(uint8_t *data)
Definition: SSLHandshake.h:190
SSLAuthenticationAlgorithm
Definition: SSLCommon.h:358
size_t getDataLength() const
Definition: SSLHandshake.h:291
SSLFinishedMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1036
Definition: SSLHandshake.h:120
Definition: SSLHandshake.h:1025
SSLClientKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:877
std::vector< uint16_t > cipherSuites
Definition: SSLHandshake.h:400
ssl_tls_client_server_hello * getClientHelloHeader() const
Definition: SSLHandshake.h:447
uint16_t tlsVersion
Definition: SSLHandshake.h:586
SSLHandshakeType
Definition: SSLCommon.h:216
Definition: SSLHandshake.h:751
std::vector< uint8_t > ecPointFormats
Definition: SSLHandshake.h:406
Definition: SSLHandshake.h:983
Definition: SSLHandshake.h:824
Definition: SSLHandshake.h:572
Definition: SSLHandshake.h:246
SSLSymetricEncryptionAlgorithm
Definition: SSLCommon.h:391
TLSECPointFormatExtension(uint8_t *data)
Definition: SSLHandshake.h:253
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1141
static SSLCipherSuite * getCipherSuiteByName(std::string name)
Definition: SSLHandshake.h:797
Definition: SSLHandshake.h:204
SSLHelloRequestMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:808
SSLx509Certificate(uint8_t *data, size_t dataLen, bool allDataExists)
Definition: SSLHandshake.h:280
Definition: SSLHandshake.h:318
ssl_tls_client_server_hello * getServerHelloHeader() const
Definition: SSLHandshake.h:628
SSLNewSessionTicketMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1077
Definition: SSLHandshake.h:866
SSLHashingAlgorithm
Definition: SSLCommon.h:474
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:402
SSLHashingAlgorithm getMACAlg() const
Definition: SSLHandshake.h:88
Definition: SSLCommon.h:118
SSLCipherSuite(uint16_t id, SSLKeyExchangeAlgorithm keyExAlg, SSLAuthenticationAlgorithm authAlg, SSLSymetricEncryptionAlgorithm symKeyAlg, SSLHashingAlgorithm MACAlg, const char *name)
Definition: SSLHandshake.h:53
SSLKeyExchangeAlgorithm getKeyExchangeAlg() const
Definition: SSLHandshake.h:73
Definition: SSLHandshake.h:1108
Definition: SSLHandshake.h:162
uint8_t * getData() const
Definition: SSLHandshake.h:286
Definition: SSLCommon.h:58
SSLServerHelloDoneMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:967
SSLServerKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:835
uint16_t extensionType
Definition: SSLHandshake.h:165
SSLCertificateVerifyMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:994
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:590
Definition: SSLHandshake.h:41
std::vector< uint16_t > supportedGroups
Definition: SSLHandshake.h:404
SSLSupportedVersionsExtension(uint8_t *data)
Definition: SSLHandshake.h:211
Definition: SSLHandshake.h:383
Definition: SSLHandshake.h:907
Definition: SSLHandshake.h:225
uint16_t tlsVersion
Definition: SSLHandshake.h:398
TLSSupportedGroupsExtension(uint8_t *data)
Definition: SSLHandshake.h:232
std::string asString() const
Definition: SSLHandshake.h:68
SSLAuthenticationAlgorithm getAuthAlg() const
Definition: SSLHandshake.h:78
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1155
Definition: SSLHandshake.h:1066