PcapPlusPlus  24.09
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()
133  {}
134 
139 
143  uint16_t getTypeAsInt() const;
144 
148  uint16_t getLength() const;
149 
153  uint16_t getTotalLength() const;
154 
158  uint8_t* getData() const;
159 
160  protected:
166  {
168  uint16_t extensionType;
172  uint8_t extensionData[];
173  };
174 
175  uint8_t* m_RawData;
176 
177  SSLExtensionStruct* getExtensionStruct() const
178  {
179  return (SSLExtensionStruct*)m_RawData;
180  }
181  };
182 
189  {
190  public:
195  explicit SSLServerNameIndicationExtension(uint8_t* data) : SSLExtension(data)
196  {}
197 
201  std::string getHostName() const;
202  };
203 
210  {
211  public:
216  explicit SSLSupportedVersionsExtension(uint8_t* data) : SSLExtension(data)
217  {}
218 
222  std::vector<SSLVersion> getSupportedVersions() const;
223  };
224 
231  {
232  public:
237  explicit TLSSupportedGroupsExtension(uint8_t* data) : SSLExtension(data)
238  {}
239 
243  std::vector<uint16_t> getSupportedGroups() const;
244  };
245 
252  {
253  public:
258  explicit TLSECPointFormatExtension(uint8_t* data) : SSLExtension(data)
259  {}
260 
264  std::vector<uint8_t> getECPointFormatList() const;
265  };
266 
275  {
276  public:
285  SSLx509Certificate(uint8_t* data, size_t dataLen, bool allDataExists)
286  : m_Data(data), m_DataLen(dataLen), m_AllDataExists(allDataExists)
287  {}
288 
292  uint8_t* getData() const
293  {
294  return m_Data;
295  }
296 
300  size_t getDataLength() const
301  {
302  return m_DataLen;
303  }
304 
310 
316  bool allDataExists() const
317  {
318  return m_AllDataExists;
319  }
320 
321  private:
322  std::unique_ptr<Asn1Record> m_Asn1Record;
323  uint8_t* m_Data;
324  size_t m_DataLen;
325  bool m_AllDataExists;
326  };
327 
328  class SSLHandshakeLayer;
329 
339  {
340  public:
341  virtual ~SSLHandshakeMessage()
342  {}
343 
351  static SSLHandshakeMessage* createHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
352 
357 
362  virtual size_t getMessageLength() const;
363 
370  virtual bool isMessageComplete() const;
371 
376  {
377  return m_Container;
378  }
379 
383  virtual std::string toString() const = 0;
384 
385  protected:
386  SSLHandshakeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
387 
388  uint8_t* m_Data;
389  size_t m_DataLen;
390  SSLHandshakeLayer* m_Container;
391  };
392 
399  {
400  public:
410  {
412  uint16_t tlsVersion;
414  std::vector<uint16_t> cipherSuites;
416  std::vector<uint16_t> extensions;
418  std::vector<uint16_t> supportedGroups;
420  std::vector<uint8_t> ecPointFormats;
421 
433  std::string toString();
434 
438  std::string toMD5();
439 
444  std::pair<std::string, std::string> toStringAndMD5();
445  };
446 
454  SSLClientHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
455 
456  virtual ~SSLClientHelloMessage()
457  {}
458 
464  {
465  return (ssl_tls_client_server_hello*)m_Data;
466  }
467 
474 
478  uint8_t getSessionIDLength() const;
479 
483  uint8_t* getSessionID() const;
484 
488  int getCipherSuiteCount() const;
489 
498  SSLCipherSuite* getCipherSuite(int index) const;
499 
510  uint16_t getCipherSuiteID(int index, bool& isValid) const;
511 
515  uint8_t getCompressionMethodsValue() const;
516 
520  int getExtensionCount() const;
521 
526  uint16_t getExtensionsLength() const;
527 
535  SSLExtension* getExtension(int index) const;
536 
544  SSLExtension* getExtensionOfType(uint16_t type) const;
545 
552 
559  template <class TExtension> TExtension* getExtensionOfType() const;
560 
572 
573  // implement abstract methods
574 
575  std::string toString() const;
576 
577  private:
578  PointerVector<SSLExtension> m_ExtensionList;
579  };
580 
587  {
588  public:
597  {
599  uint16_t tlsVersion;
601  uint16_t cipherSuite;
603  std::vector<uint16_t> extensions;
604 
612  std::string toString();
613 
617  std::string toMD5();
618 
623  std::pair<std::string, std::string> toStringAndMD5();
624  };
625 
633  SSLServerHelloMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
634 
635  virtual ~SSLServerHelloMessage()
636  {}
637 
643  {
644  return (ssl_tls_client_server_hello*)m_Data;
645  }
646 
657 
661  uint8_t getSessionIDLength() const;
662 
666  uint8_t* getSessionID() const;
667 
675 
684  uint16_t getCipherSuiteID(bool& isValid) const;
685 
689  uint8_t getCompressionMethodsValue() const;
690 
694  int getExtensionCount() const;
695 
700  uint16_t getExtensionsLength() const;
701 
709  SSLExtension* getExtension(int index) const;
710 
718  SSLExtension* getExtensionOfType(uint16_t type) const;
719 
726 
733  template <class TExtension> TExtension* getExtensionOfType() const;
734 
747 
748  // implement abstract methods
749 
750  std::string toString() const;
751 
752  private:
753  PointerVector<SSLExtension> m_ExtensionList;
754  };
755 
765  {
766  public:
774  SSLCertificateMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
775 
776  virtual ~SSLCertificateMessage()
777  {}
778 
786  int getNumOfCertificates() const;
787 
795 
796  // implement abstract methods
797 
798  std::string toString() const;
799 
800  private:
801  PointerVector<SSLx509Certificate> m_CertificateList;
802  };
803 
810  {
811  public:
819  SSLHelloRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
820  : SSLHandshakeMessage(data, dataLen, container)
821  {}
822 
823  virtual ~SSLHelloRequestMessage()
824  {}
825 
826  // implement abstract methods
827 
828  std::string toString() const;
829  };
830 
838  {
839  public:
847  SSLServerKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
848  : SSLHandshakeMessage(data, dataLen, container)
849  {}
850 
852  {}
853 
859  uint8_t* getServerKeyExchangeParams() const;
860 
868 
869  // implement abstract methods
870 
871  std::string toString() const;
872  };
873 
881  {
882  public:
890  SSLClientKeyExchangeMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
891  : SSLHandshakeMessage(data, dataLen, container)
892  {}
893 
895  {}
896 
902  uint8_t* getClientKeyExchangeParams() const;
903 
911 
912  // implement abstract methods
913 
914  std::string toString() const;
915  };
916 
923  {
924  public:
932  SSLCertificateRequestMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container);
933 
935  {}
936 
940  std::vector<SSLClientCertificateType>& getCertificateTypes();
941 
947  uint8_t* getCertificateAuthorityData() const;
948 
956 
957  // implement abstract methods
958 
959  std::string toString() const;
960 
961  private:
962  std::vector<SSLClientCertificateType> m_ClientCertificateTypes;
963  };
964 
971  {
972  public:
980  SSLServerHelloDoneMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
981  : SSLHandshakeMessage(data, dataLen, container)
982  {}
983 
984  virtual ~SSLServerHelloDoneMessage()
985  {}
986 
987  // implement abstract methods
988 
989  std::string toString() const;
990  };
991 
999  {
1000  public:
1008  SSLCertificateVerifyMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1009  : SSLHandshakeMessage(data, dataLen, container)
1010  {}
1011 
1012  virtual ~SSLCertificateVerifyMessage()
1013  {}
1014 
1020  uint8_t* getSignedHash() const;
1021 
1028  size_t getSignedHashLength() const;
1029 
1030  // implement abstract methods
1031 
1032  std::string toString() const;
1033  };
1034 
1042  {
1043  public:
1051  SSLFinishedMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1052  : SSLHandshakeMessage(data, dataLen, container)
1053  {}
1054 
1055  virtual ~SSLFinishedMessage()
1056  {}
1057 
1063  uint8_t* getSignedHash() const;
1064 
1071  size_t getSignedHashLength() const;
1072 
1073  // implement abstract methods
1074 
1075  std::string toString() const;
1076  };
1077 
1084  {
1085  public:
1093  SSLNewSessionTicketMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1094  : SSLHandshakeMessage(data, dataLen, container)
1095  {}
1096 
1097  virtual ~SSLNewSessionTicketMessage()
1098  {}
1099 
1105  uint8_t* getSessionTicketData() const;
1106 
1114 
1115  // implement abstract methods
1116 
1117  std::string toString() const;
1118  };
1119 
1127  {
1128  public:
1136  SSLUnknownMessage(uint8_t* data, size_t dataLen, SSLHandshakeLayer* container)
1137  : SSLHandshakeMessage(data, dataLen, container)
1138  {}
1139 
1140  virtual ~SSLUnknownMessage()
1141  {}
1142 
1143  // implement virtual and abstract methods
1144 
1149 
1155  size_t getMessageLength() const;
1156 
1157  std::string toString() const;
1158  };
1159 
1160  template <class TExtension> TExtension* SSLClientHelloMessage::getExtensionOfType() const
1161  {
1162  size_t vecSize = m_ExtensionList.size();
1163  for (size_t i = 0; i < vecSize; i++)
1164  {
1165  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1166  if (dynamic_cast<TExtension*>(curElem) != nullptr)
1167  return (TExtension*)curElem;
1168  }
1169 
1170  return nullptr;
1171  }
1172 
1173  template <class TExtension> TExtension* SSLServerHelloMessage::getExtensionOfType() const
1174  {
1175  size_t vecSize = m_ExtensionList.size();
1176  for (size_t i = 0; i < vecSize; i++)
1177  {
1178  SSLExtension* curElem = const_cast<SSLExtension*>(m_ExtensionList.at(i));
1179  if (dynamic_cast<TExtension*>(curElem) != nullptr)
1180  return (TExtension*)curElem;
1181  }
1182 
1183  return nullptr;
1184  }
1185 
1186 } // namespace pcpp
Definition: Asn1Codec.h:366
Definition: PointerVector.h:29
Definition: SSLHandshake.h:765
std::string toString() const
SSLCertificateMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
SSLx509Certificate * getCertificate(int index) const
Definition: SSLHandshake.h:923
size_t getCertificateAuthorityLength() const
std::vector< SSLClientCertificateType > & getCertificateTypes()
uint8_t * getCertificateAuthorityData() const
SSLCertificateRequestMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:999
SSLCertificateVerifyMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1008
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:399
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)
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1160
uint8_t getSessionIDLength() const
SSLCipherSuite * getCipherSuite(int index) const
ssl_tls_client_server_hello * getClientHelloHeader() const
Definition: SSLHandshake.h:463
uint8_t * getSessionID() const
uint8_t getCompressionMethodsValue() const
SSLExtension * getExtension(int index) const
ClientHelloTLSFingerprint generateTLSFingerprint() const
SSLVersion getHandshakeVersion() const
std::string toString() const
Definition: SSLHandshake.h:881
SSLClientKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:890
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:1042
uint8_t * getSignedHash() const
SSLFinishedMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1051
size_t getSignedHashLength() const
std::string toString() const
Definition: SSLLayer.h:319
Definition: SSLHandshake.h:339
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:375
virtual bool isMessageComplete() const
Definition: SSLHandshake.h:810
SSLHelloRequestMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:819
std::string toString() const
Definition: SSLHandshake.h:1084
size_t getSessionTicketDataLength() const
std::string toString() const
SSLNewSessionTicketMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1093
uint8_t * getSessionTicketData() const
Definition: SSLHandshake.h:971
std::string toString() const
SSLServerHelloDoneMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:980
Definition: SSLHandshake.h:587
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
ssl_tls_client_server_hello * getServerHelloHeader() const
Definition: SSLHandshake.h:642
SSLVersion getHandshakeVersion() const
uint16_t getCipherSuiteID(bool &isValid) const
uint8_t getCompressionMethodsValue() const
SSLExtension * getExtension(int index) const
SSLCipherSuite * getCipherSuite() const
std::string toString() const
TExtension * getExtensionOfType() const
Definition: SSLHandshake.h:1173
ServerHelloTLSFingerprint generateTLSFingerprint() const
uint8_t * getSessionID() const
Definition: SSLHandshake.h:838
SSLServerKeyExchangeMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:847
uint8_t * getServerKeyExchangeParams() const
size_t getServerKeyExchangeParamsLength() const
Definition: SSLHandshake.h:189
SSLServerNameIndicationExtension(uint8_t *data)
Definition: SSLHandshake.h:195
Definition: SSLHandshake.h:210
std::vector< SSLVersion > getSupportedVersions() const
SSLSupportedVersionsExtension(uint8_t *data)
Definition: SSLHandshake.h:216
Definition: SSLHandshake.h:1127
std::string toString() const
size_t getMessageLength() const
SSLHandshakeType getHandshakeType() const
SSLUnknownMessage(uint8_t *data, size_t dataLen, SSLHandshakeLayer *container)
Definition: SSLHandshake.h:1136
Definition: SSLCommon.h:112
Definition: SSLHandshake.h:275
SSLx509Certificate(uint8_t *data, size_t dataLen, bool allDataExists)
Definition: SSLHandshake.h:285
Asn1SequenceRecord * getRootAsn1Record()
size_t getDataLength() const
Definition: SSLHandshake.h:300
bool allDataExists() const
Definition: SSLHandshake.h:316
uint8_t * getData() const
Definition: SSLHandshake.h:292
Definition: SSLHandshake.h:252
TLSECPointFormatExtension(uint8_t *data)
Definition: SSLHandshake.h:258
std::vector< uint8_t > getECPointFormatList() const
Definition: SSLHandshake.h:231
std::vector< uint16_t > getSupportedGroups() const
TLSSupportedGroupsExtension(uint8_t *data)
Definition: SSLHandshake.h:237
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:420
std::pair< std::string, std::string > toStringAndMD5()
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:416
std::vector< uint16_t > cipherSuites
Definition: SSLHandshake.h:414
std::vector< uint16_t > supportedGroups
Definition: SSLHandshake.h:418
uint16_t tlsVersion
Definition: SSLHandshake.h:412
Definition: SSLHandshake.h:166
uint8_t extensionData[]
Definition: SSLHandshake.h:172
uint16_t extensionType
Definition: SSLHandshake.h:168
uint16_t extensionDataLength
Definition: SSLHandshake.h:170
uint16_t tlsVersion
Definition: SSLHandshake.h:599
uint16_t cipherSuite
Definition: SSLHandshake.h:601
std::vector< uint16_t > extensions
Definition: SSLHandshake.h:603
std::pair< std::string, std::string > toStringAndMD5()
Definition: SSLCommon.h:56