public HelloExtension(HelloExtensionType type, byte[] data) { if (data == null) { throw new AlertException(AlertDescription.InternalError, "Trying to create HandshakeExtension with null data"); } Type = type; Data = data; }
protected override void DecodeDataBytes(ProtocolVersion ver, byte[] data) { MemoryStream memStream = new MemoryStream(data); HandshakeStream stream = new HandshakeStream(memStream); byte major = stream.ReadUInt8(); byte minor = stream.ReadUInt8(); ServerVersion = new ProtocolVersion(major, minor); byte[] randomBytes = stream.ReadBytes(32); Random = new HandshakeRandom(randomBytes); int idLength = (int)stream.ReadUInt8(); SessionID = stream.ReadBytes(idLength); Trace.WriteLine($"SessionId: {SessionID}"); CipherSuite = (CipherSuiteId)stream.ReadUInt16(); Trace.WriteLine($"Cipher Suite Id: {CipherSuite} refer to list at http://www.thesprawl.org/research/tls-and-ssl-cipher-suites/"); CompressionMethod = stream.ReadUInt8(); byte[] extensionList = new byte[0]; if (!stream.EndOfStream && ServerVersion.HasExtensions) { UInt16 extensionListLength = stream.ReadUInt16(); extensionList = stream.ReadBytes(extensionListLength); } stream.ConfirmEndOfStream(); int pos = 0; while (pos + 4 <= extensionList.Length) { HelloExtensionType extensionType = (HelloExtensionType)(UInt16)((extensionList[pos] << 8) | extensionList[pos + 1]); Trace.WriteLine($"Extension Type { extensionType }"); UInt16 extensionDataLength = (UInt16)((extensionList[pos + 2] << 8) | extensionList[pos + 3]); pos += 4; if (pos + extensionDataLength > extensionList.Length) { throw new AlertException(AlertDescription.IllegalParameter, "ServerHello extension data length too large: " + extensionDataLength); } byte[] extensionData = new byte[extensionDataLength]; Buffer.BlockCopy(extensionList, pos, extensionData, 0, extensionData.Length); pos += extensionData.Length; Extensions.Add(new HelloExtension(extensionType, extensionData)); } }
protected override byte[] EncodeDataBytes(ProtocolVersion ver) { MemoryStream memStream = new MemoryStream(); HandshakeStream stream = new HandshakeStream(memStream); stream.WriteUInt8(ServerVersion.Major); stream.WriteUInt8(ServerVersion.Minor); stream.WriteBytes(Random.GetBytes()); stream.WriteUInt8((Byte)SessionID.Length); stream.WriteBytes(SessionID); stream.WriteUInt16((ushort)CipherSuite); stream.WriteUInt8(CompressionMethod); if (Extensions.Count > 0) { int length = 0; foreach (HelloExtension ext in Extensions) { if (!ext.SupportsProtocolVersion(ServerVersion)) { continue; } length += 4 + ext.Data.Length; } stream.WriteUInt16((UInt16)length); foreach (HelloExtension ext in Extensions) { if (!ext.SupportsProtocolVersion(ServerVersion)) { continue; } HelloExtensionType type = ext.Type; byte[] data = ext.Data; stream.WriteUInt16((ushort)type); stream.WriteUInt16((UInt16)data.Length); stream.WriteBytes(data); } } return(memStream.ToArray()); }
protected HelloEccExtension(HelloExtensionType type) : base(type) { this.SupportedEllipticCurves = new List<EccNamedCurve>(); }
protected HelloExtension(HelloExtensionType type) : this((UInt16)type) { }
protected HelloEccPointFormatsExtension(HelloExtensionType type) : base(type) { }
protected HelloExtension(HelloExtensionType type) : this((UInt16) type) { }
public HelloExtension(HelloExtensionType type) { Type = type; _data = new byte[0]; }
protected override byte[] EncodeDataBytes(ProtocolVersion ver) { if (CipherSuites.Count == 0 || CompressionMethods.Count == 0) { throw new Exception("No cipher suites or compression methods defined"); } MemoryStream memStream = new MemoryStream(); HandshakeStream stream = new HandshakeStream(memStream); stream.WriteUInt8(ClientVersion.Major); stream.WriteUInt8(ClientVersion.Minor); stream.WriteBytes(Random.GetBytes()); stream.WriteUInt8((Byte)SessionID.Length); stream.WriteBytes(SessionID); if (ClientVersion.IsUsingDatagrams) { stream.WriteUInt8((Byte)Cookie.Length); stream.WriteBytes(Cookie); } stream.WriteUInt16((UInt16)(2 * CipherSuites.Count)); foreach (UInt16 cipher in CipherSuites) { stream.WriteUInt16(cipher); } stream.WriteUInt8((Byte)CompressionMethods.Count); foreach (Byte compression in CompressionMethods) { stream.WriteUInt8(compression); } if (Extensions.Count > 0) { int length = 0; foreach (HelloExtension ext in Extensions) { if (!ext.SupportsProtocolVersion(ClientVersion)) { continue; } length += 4 + ext.Data.Length; } stream.WriteUInt16((UInt16)length); foreach (HelloExtension ext in Extensions) { if (!ext.SupportsProtocolVersion(ClientVersion)) { continue; } HelloExtensionType type = ext.Type; byte[] data = ext.Data; stream.WriteUInt16((ushort)type); stream.WriteUInt16((UInt16)data.Length); stream.WriteBytes(data); } } return(memStream.ToArray()); }
protected override void DecodeDataBytes(ProtocolVersion ver, byte[] data) { CipherSuites.Clear(); CompressionMethods.Clear(); MemoryStream memStream = new MemoryStream(data); HandshakeStream stream = new HandshakeStream(memStream); byte major = stream.ReadUInt8(); byte minor = stream.ReadUInt8(); ClientVersion = new ProtocolVersion(major, minor); byte[] randomBytes = stream.ReadBytes(32); Random = new HandshakeRandom(randomBytes); int idLength = (int)stream.ReadUInt8(); SessionID = stream.ReadBytes(idLength); if (ClientVersion.IsUsingDatagrams) { int cookieLength = (int)stream.ReadUInt8(); Cookie = stream.ReadBytes(cookieLength); } int cipherLength = (int)stream.ReadUInt16(); for (int i = 0; i < cipherLength; i += 2) { CipherSuites.Add((CipherSuiteId)stream.ReadUInt16()); } int compressionLength = (int)stream.ReadUInt8(); for (int i = 0; i < compressionLength; i++) { CompressionMethods.Add(stream.ReadUInt8()); } byte[] extensionList = new byte[0]; if (!stream.EndOfStream && ClientVersion.HasExtensions) { UInt16 extensionListLength = stream.ReadUInt16(); extensionList = stream.ReadBytes(extensionListLength); } stream.ConfirmEndOfStream(); int pos = 0; while (pos + 4 <= extensionList.Length) { HelloExtensionType extensionType = (HelloExtensionType)((extensionList[pos] << 8) | extensionList[pos + 1]); UInt16 extensionDataLength = (UInt16)((extensionList[pos + 2] << 8) | extensionList[pos + 3]); pos += 4; if (pos + extensionDataLength > extensionList.Length) { throw new AlertException(AlertDescription.IllegalParameter, "ClientHello extension data length too large: " + extensionDataLength); } byte[] extensionData = new byte[extensionDataLength]; Buffer.BlockCopy(extensionList, pos, extensionData, 0, extensionData.Length); pos += extensionData.Length; Extensions.Add(new HelloExtension(extensionType, extensionData)); } }
protected HelloEccExtension(HelloExtensionType type) : base(type) { this.SupportedEllipticCurves = new List <EccNamedCurve>(); }