/// <summary> /// Initializes a new instance of the <see cref="NoiseSocket"/> class. /// </summary> /// <param name="stream">The stream for reading and writing encoded protocol messages.</param> /// <param name="leaveOpen"> /// True to leave the stream open after the /// <see cref="NoiseSocket"/> object is disposed, false otherwise. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="stream"/> is null. /// </exception> public NoiseSocket(Stream stream, bool leaveOpen = false) { ThrowIfNull(stream, nameof(stream)); this.stream = stream; this.leaveOpen = leaveOpen; savedMessages = new List <Memory <byte> >(); isNextMessageEncrypted = protocol != null && IsInitialMessageEncrypted(protocol); lastOperation = HandshakeOperation.WriteHandshakeMessage; }
private static HandshakeOperation Next(HandshakeOperation operation) { switch (operation) { case HandshakeOperation.ReadNegotiationData: return(HandshakeOperation.ReadHandshakeMessage); case HandshakeOperation.WriteNegotiationData: return(HandshakeOperation.WriteHandshakeMessage); case HandshakeOperation.ReadHandshakeMessage: return(HandshakeOperation.WriteNegotiationData); case HandshakeOperation.WriteHandshakeMessage: return(HandshakeOperation.ReadNegotiationData); default: throw new InvalidOperationException("Unknown handshake operation."); } }
private void ProcessMessage(HandshakeOperation operation, Memory <byte> data, bool copy = true) { var next = Next(lastOperation); if (next != operation) { throw new InvalidOperationException($"Expected the call to {next}, but {operation} was called instead."); } lastOperation = operation; if (savedMessages != null && savedMessages.Count < MaxSavedMessagesCount) { savedMessages.Add(copy ? data.ToArray() : data); } else { savedMessages = null; } }
/// <summary> /// Initializes a new instance of the <see cref="NoiseSocket"/> class. /// </summary> /// <param name="protocol">A concrete Noise protocol (e.g. Noise_XX_25519_AESGCM_BLAKE2b).</param> /// <param name="config"> /// A set of parameters used to instantiate an initial <see cref="HandshakeState"/>. /// </param> /// <param name="stream">The stream for reading and writing encoded protocol messages.</param> /// <param name="leaveOpen"> /// True to leave the stream open after the /// <see cref="NoiseSocket"/> object is disposed, false otherwise. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown if either <paramref name="protocol"/>, /// <paramref name="config"/>, or <paramref name="stream"/> is null. /// </exception> /// <exception cref="ArgumentException"> /// Thrown if the selected handshake pattern was a one-way pattern. /// </exception> public NoiseSocket(Protocol protocol, ProtocolConfig config, Stream stream, bool leaveOpen = false) { ThrowIfNull(protocol, nameof(protocol)); ThrowIfNull(config, nameof(config)); ThrowIfNull(stream, nameof(stream)); if (protocol.HandshakePattern.Patterns.Count() == 1) { throw new ArgumentException("One-way patterns are not yet supported."); } this.client = config.Initiator; this.protocol = protocol; this.config = config; this.stream = stream; this.leaveOpen = leaveOpen; savedMessages = new List <Memory <byte> >(); isNextMessageEncrypted = protocol != null && IsInitialMessageEncrypted(protocol); lastOperation = config.Initiator ? HandshakeOperation.ReadHandshakeMessage : HandshakeOperation.WriteHandshakeMessage; }
public handshaker(HandshakeOperation operationId, uint identifier = 1, uint version = 1) { this.identifier = identifier; this.version = version; this.operationId = operationId; }