PcapPlusPlus  20.08+
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 
17 namespace pcpp
18 {
19 
20 
31 {
32 public:
42  SSLCipherSuite(uint16_t id, SSLKeyExchangeAlgorithm keyExAlg,
45  SSLHashingAlgorithm MACAlg,
46  const char* name)
47  : m_Id(id), m_KeyExAlg(keyExAlg), m_AuthAlg(authAlg), m_SymKeyAlg(symKeyAlg), m_MACAlg(MACAlg), m_Name(name) {}
48 
52  uint16_t getID() const { return m_Id; }
53 
57  std::string asString() const { return m_Name; }
58 
62  SSLKeyExchangeAlgorithm getKeyExchangeAlg() const { return m_KeyExAlg; }
63 
67  SSLAuthenticationAlgorithm getAuthAlg() const { return m_AuthAlg; }
68 
72  SSLSymetricEncryptionAlgorithm getSymKeyAlg() const { return m_SymKeyAlg; }
73 
77  SSLHashingAlgorithm getMACAlg() const { return m_MACAlg; }
78 
84  static SSLCipherSuite* getCipherSuiteByID(uint16_t id);
85 
91  static SSLCipherSuite* getCipherSuiteByName(std::string name);
92 
93 private:
94  uint16_t m_Id;
95  SSLKeyExchangeAlgorithm m_KeyExAlg;
98  SSLHashingAlgorithm m_MACAlg;
99  std::string m_Name;
100 };
101 
102 
110 {
111 public:
116  SSLExtension(uint8_t* data);
117 
118  virtual ~SSLExtension() { }
119 
123  SSLExtensionType getType() const;
124 
128  uint16_t getTypeAsInt() const;
129 
133  uint16_t getLength() const;
134 
138  uint16_t getTotalLength() const;
139 
143  uint8_t* getData() const;
144 
145 protected:
146 
152  {
154  uint16_t extensionType;
158  uint8_t extensionData[];
159  };
160 
161  uint8_t* m_RawData;
162 
163  SSLExtensionStruct* getExtensionStruct() const { return (SSLExtensionStruct*)m_RawData; }
164 };
165 
166 
173 {
174 public:
180 
184  std::string getHostName() const;
185 };
186 
187 
194 {
195 public:
200  SSLSupportedVersionsExtension(uint8_t* data) : SSLExtension(data) {}
201 
205  std::vector<SSLVersion> getSupportedVersions() const;
206 };
207 
208 
215 {
216  public:
221  TLSSupportedGroupsExtension(uint8_t* data) : SSLExtension(data) {}
222 
226  std::vector<uint16_t> getSupportedGroups() const;
227 };
228 
229 
236 {
237  public:
242  TLSECPointFormatExtension(uint8_t* data) : SSLExtension(data) {}
243 
247  std::vector<uint8_t> getECPointFormatList() const;
248 };
249 
250 
258 {
259 public:
260 
269  SSLx509Certificate(uint8_t* data, size_t dataLen, bool allDataExists)
270  : m_Data(data), m_DataLen(dataLen), m_AllDataExists(allDataExists) {}
271 
275  uint8_t* getData() const { return m_Data; }
276 
280  size_t getDataLength() const { return m_DataLen; }
281 
287  bool allDataExists() const { return m_AllDataExists; }
288 
289 private:
290  uint8_t* m_Data;
291  size_t m_DataLen;
292  bool m_AllDataExists;
293 };
294 
295 
296 class SSLHandshakeLayer;
297 
298 
308 {
309 public:
310 
311  virtual ~SSLHandshakeMessage() {}
312 
320  static SSLHandshakeMessage* createHandhakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
321 
325  virtual SSLHandshakeType getHandshakeType() const;
326 
331  virtual size_t getMessageLength() const;
332 
339  virtual bool isMessageComplete() const;
340 
344  SSLHandshakeLayer* getContainingLayer() const { return m_Container; }
345 
349  virtual std::string toString() const = 0;
350 
351 protected:
352 
353  SSLHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
354 
355  uint8_t* m_Data;
356  size_t m_DataLen;
357  SSLHandshakeLayer* m_Container;
358 
359 };
360 
361 
368 {
369 public:
370 
380  {
382  uint16_t tlsVersion;
384  std::vector<uint16_t> cipherSuites;
386  std::vector<uint16_t> extensions;
388  std::vector<uint16_t> supportedGroups;
390  std::vector<uint8_t> ecPointFormats;
391 
402  std::string toString();
403 
407  std::string toMD5();
408 
413  std::pair<std::string, std::string> toStringAndMD5();
414  };
415 
423  SSLClientHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
424 
425  virtual ~SSLClientHelloMessage() {}
426 
432 
437  SSLVersion getHandshakeVersion() const;
438 
442  uint8_t getSessionIDLength() const;
443 
447  uint8_t* getSessionID() const;
448 
452  int getCipherSuiteCount() const;
453 
462  SSLCipherSuite* getCipherSuite(int index) const;
463 
473  uint16_t getCipherSuiteID(int index, bool& isValid) const;
474 
478  uint8_t getCompressionMethodsValue() const;
479 
483  int getExtensionCount() const;
484 
488  uint16_t getExtensionsLenth() const;
489 
497  SSLExtension* getExtension(int index) const;
498 
506  SSLExtension* getExtensionOfType(uint16_t type) const;
507 
513  SSLExtension* getExtensionOfType(SSLExtensionType type) const;
514 
521  template<class TExtension>
522  TExtension* getExtensionOfType() const;
523 
534  ClientHelloTLSFingerprint generateTLSFingerprint() const;
535 
536  // implement abstract methods
537 
538  std::string toString() const;
539 
540 private:
541  PointerVector<SSLExtension> m_ExtensionList;
542 
543 };
544 
545 
552 {
553 public:
554 
563  {
565  uint16_t tlsVersion;
567  uint16_t cipherSuite;
569  std::vector<uint16_t> extensions;
570 
578  std::string toString();
579 
583  std::string toMD5();
584 
589  std::pair<std::string, std::string> toStringAndMD5();
590  };
591 
599  SSLServerHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
600 
601  virtual ~SSLServerHelloMessage() {}
602 
608 
617  SSLVersion getHandshakeVersion() const;
618 
622  uint8_t getSessionIDLength() const;
623 
627  uint8_t* getSessionID() const;
628 
635  SSLCipherSuite* getCipherSuite() const;
636 
644  uint16_t getCipherSuiteID(bool& isValid) const;
645 
649  uint8_t getCompressionMethodsValue() const;
650 
654  int getExtensionCount() const;
655 
659  uint16_t getExtensionsLenth() const;
660 
668  SSLExtension* getExtension(int index) const;
669 
677  SSLExtension* getExtensionOfType(uint16_t type) const;
678 
684  SSLExtension* getExtensionOfType(SSLExtensionType type) const;
685 
692  template<class TExtension>
693  TExtension* getExtensionOfType() const;
694 
706  ServerHelloTLSFingerprint generateTLSFingerprint() const;
707 
708  // implement abstract methods
709 
710  std::string toString() const;
711 
712 private:
713  PointerVector<SSLExtension> m_ExtensionList;
714 };
715 
716 
726 {
727 public:
728 
736  SSLCertificateMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
737 
738  virtual ~SSLCertificateMessage() {}
739 
747  int getNumOfCertificates() const;
748 
755  SSLx509Certificate* getCertificate(int index) const;
756 
757  // implement abstract methods
758 
759  std::string toString() const;
760 
761 private:
762  PointerVector<SSLx509Certificate> m_CertificateList;
763 };
764 
765 
772 {
773 public:
774 
782  SSLHelloRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
783 
784  virtual ~SSLHelloRequestMessage() {}
785 
786  // implement abstract methods
787 
788  std::string toString() const;
789 };
790 
791 
799 {
800 public:
801 
809  SSLServerKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
810 
812 
818  uint8_t* getServerKeyExchangeParams() const;
819 
826  size_t getServerKeyExchangeParamsLength() const;
827 
828  // implement abstract methods
829 
830  std::string toString() const;
831 };
832 
833 
841 {
842 public:
843 
851  SSLClientKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
852 
854 
860  uint8_t* getClientKeyExchangeParams() const;
861 
868  size_t getClientKeyExchangeParamsLength() const;
869 
870  // implement abstract methods
871 
872  std::string toString() const;
873 };
874 
875 
882 {
883 public:
884 
892  SSLCertificateRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
893 
895 
899  std::vector<SSLClientCertificateType>& getCertificateTypes();
900 
906  uint8_t* getCertificateAuthorityData() const;
907 
914  size_t getCertificateAuthorityLength() const;
915 
916  // implement abstract methods
917 
918  std::string toString() const;
919 
920 private:
921  std::vector<SSLClientCertificateType> m_ClientCertificateTypes;
922 };
923 
924 
931 {
932 public:
933 
941  SSLServerHelloDoneMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
942 
943  virtual ~SSLServerHelloDoneMessage() {}
944 
945  // implement abstract methods
946 
947  std::string toString() const;
948 };
949 
950 
958 {
959 public:
960 
968  SSLCertificateVerifyMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
969 
970  virtual ~SSLCertificateVerifyMessage() {}
971 
977  uint8_t* getSignedHash() const;
978 
985  size_t getSignedHashLength() const;
986 
987  // implement abstract methods
988 
989  std::string toString() const;
990 };
991 
992 
1000 {
1001 public:
1002 
1010  SSLFinishedMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
1011 
1012  virtual ~SSLFinishedMessage() {}
1013 
1019  uint8_t* getSignedHash() const;
1020 
1027  size_t getSignedHashLength() const;
1028 
1029  // implement abstract methods
1030 
1031  std::string toString() const;
1032 };
1033 
1034 
1041 {
1042 public:
1043 
1051  SSLNewSessionTicketMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
1052 
1053  virtual ~SSLNewSessionTicketMessage() {}
1054 
1060  uint8_t* getSessionTicketData() const;
1061 
1068  size_t getSessionTicketDataLength() const;
1069 
1070  // implement abstract methods
1071 
1072  std::string toString() const;
1073 };
1074 
1075 
1083 {
1084 public:
1085 
1093  SSLUnknownMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container) : SSLHandshakeMessage(data, dataLen, container) {}
1094 
1095  virtual ~SSLUnknownMessage() {}
1096 
1097  // implement virtual and abstract methods
1098 
1102  SSLHandshakeType getHandshakeType() const;
1103 
1109  size_t getMessageLength() const;
1110 
1111  std::string toString() const;
1112 };
1113 
1114 template<class TExtension>
1116 {
1117  size_t vecSize = m_ExtensionList.size();
1118  for (size_t i = 0; i < vecSize; i++)
1119  {
1120  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1121  if (dynamic_cast<TExtension*>(curElem) != NULL)
1122  return (TExtension*)curElem;
1123  }
1124 
1125  return NULL;
1126 }
1127 
1128 template<class TExtension>
1130 {
1131  size_t vecSize = m_ExtensionList.size();
1132  for (size_t i = 0; i < vecSize; i++)
1133  {
1134  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1135  if (dynamic_cast<TExtension*>(curElem) != NULL)
1136  return (TExtension*)curElem;
1137  }
1138 
1139  return NULL;
1140 }
1141 
1142 } // namespace pcpp
1143 
1144 #endif /* PACKETPP_SSL_HANDSHAKE_MESSAGE */
The main namespace for the PcapPlusPlus lib.
static SSLCipherSuite * getCipherSuiteByID(uint16_t id)
Definition: SSLHandshake.h:257
bool allDataExists() const
Definition: SSLHandshake.h:287
uint16_t extensionDataLength
Definition: SSLHandshake.h:156
SSLSymetricEncryptionAlgorithm getSymKeyAlg() const
Definition: SSLHandshake.h:72
SSLHandshakeLayer * getContainingLayer() const
Definition: SSLHandshake.h:344
SSLKeyExchangeAlgorithm
Definition: SSLCommon.h:325
uint16_t getID() const
Definition: SSLHandshake.h:52
SSLUnknownMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1093
Definition: PointerVector.h:24
Definition: SSLLayer.h:306
Definition: SSLHandshake.h:172
uint16_t cipherSuite
Definition: SSLHandshake.h:567
Definition: SSLHandshake.h:930
SSLExtensionType
Definition: SSLCommon.h:501
SSLServerNameIndicationExtension(uint8_t *data)
Definition: SSLHandshake.h:179
SSLAuthenticationAlgorithm
Definition: SSLCommon.h:358
size_t getDataLength() const
Definition: SSLHandshake.h:280
SSLFinishedMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1010
Definition: SSLHandshake.h:109
Definition: SSLHandshake.h:999
SSLClientKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:851
std::vector< uint16_t > cipherSuites
Definition: SSLHandshake.h:384
ssl_tls_client_server_hello * getClientHelloHeader() const
Definition: SSLHandshake.h:431
uint16_t tlsVersion
Definition: SSLHandshake.h:565
SSLHandshakeType
Definition: SSLCommon.h:216
Definition: SSLHandshake.h:725
std::vector< uint8_t > ecPointFormats
Definition: SSLHandshake.h:390
Definition: SSLHandshake.h:957
Definition: SSLHandshake.h:798
Definition: SSLHandshake.h:551
Definition: SSLHandshake.h:235
SSLSymetricEncryptionAlgorithm
Definition: SSLCommon.h:391
TLSECPointFormatExtension(uint8_t *data)
Definition: SSLHandshake.h:242
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1115
static SSLCipherSuite * getCipherSuiteByName(std::string name)
Definition: SSLHandshake.h:771
Definition: SSLHandshake.h:193
SSLHelloRequestMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:782
SSLx509Certificate(uint8_t *data, size_t dataLen, bool allDataExists)
Definition: SSLHandshake.h:269
Definition: SSLHandshake.h:307
ssl_tls_client_server_hello * getServerHelloHeader() const
Definition: SSLHandshake.h:607
SSLNewSessionTicketMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1051
Definition: SSLHandshake.h:840
SSLHashingAlgorithm
Definition: SSLCommon.h:474
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:386
SSLHashingAlgorithm getMACAlg() const
Definition: SSLHandshake.h:77
Definition: SSLCommon.h:118
SSLCipherSuite(uint16_t id, SSLKeyExchangeAlgorithm keyExAlg, SSLAuthenticationAlgorithm authAlg, SSLSymetricEncryptionAlgorithm symKeyAlg, SSLHashingAlgorithm MACAlg, const char *name)
Definition: SSLHandshake.h:42
SSLKeyExchangeAlgorithm getKeyExchangeAlg() const
Definition: SSLHandshake.h:62
Definition: SSLHandshake.h:1082
Definition: SSLHandshake.h:151
uint8_t * getData() const
Definition: SSLHandshake.h:275
Definition: SSLCommon.h:58
SSLServerHelloDoneMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:941
SSLServerKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:809
uint16_t extensionType
Definition: SSLHandshake.h:154
SSLCertificateVerifyMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:968
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:569
Definition: SSLHandshake.h:30
std::vector< uint16_t > supportedGroups
Definition: SSLHandshake.h:388
SSLSupportedVersionsExtension(uint8_t *data)
Definition: SSLHandshake.h:200
Definition: SSLHandshake.h:367
Definition: SSLHandshake.h:881
Definition: SSLHandshake.h:214
uint16_t tlsVersion
Definition: SSLHandshake.h:382
TLSSupportedGroupsExtension(uint8_t *data)
Definition: SSLHandshake.h:221
std::string asString() const
Definition: SSLHandshake.h:57
SSLAuthenticationAlgorithm getAuthAlg() const
Definition: SSLHandshake.h:67
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1129
Definition: SSLHandshake.h:1040