| | 1 | | namespace NLightning.Infrastructure.Transport.Services; |
| | 2 | |
|
| | 3 | | using Common.Utils; |
| | 4 | | using Crypto.Functions; |
| | 5 | | using Domain.Transport; |
| | 6 | | using Handshake.States; |
| | 7 | | using Interfaces; |
| | 8 | | using Protocol.Constants; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Initializes a new instance of the <see cref="HandshakeService"/> class. |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="isInitiator">If we are initiating the connection</param> |
| | 14 | | /// <param name="localStaticPrivateKey">Our local Private Key</param> |
| | 15 | | /// <param name="staticPublicKey">If we are initiating, the remote Public Key, else our local Public Key</param> |
| 24 | 16 | | internal sealed class HandshakeService(bool isInitiator, ReadOnlySpan<byte> localStaticPrivateKey, |
| 24 | 17 | | ReadOnlySpan<byte> staticPublicKey, IHandshakeState? handshakeState = null) |
| | 18 | | : IHandshakeService |
| | 19 | | { |
| 24 | 20 | | private readonly IHandshakeState _handshakeState = handshakeState |
| 24 | 21 | | ?? new HandshakeState(isInitiator, localStaticPrivateKey, |
| 24 | 22 | | staticPublicKey, new Ecdh()); |
| | 23 | |
|
| 24 | 24 | | private byte _steps = 2; |
| | 25 | | private bool _disposed; |
| | 26 | |
|
| | 27 | | /// <inheritdoc/> |
| 40 | 28 | | public bool IsInitiator => isInitiator; |
| | 29 | |
|
| 0 | 30 | | public NBitcoin.PubKey? RemoteStaticPublicKey => _handshakeState.RemoteStaticPublicKey; |
| | 31 | |
|
| | 32 | | /// <inheritdoc/> |
| | 33 | | /// <exception cref="InvalidOperationException">Thrown when there's no more steps to complete</exception> |
| | 34 | | public int PerformStep(ReadOnlySpan<byte> inMessage, Span<byte> outMessage, out ITransport? transport) |
| | 35 | | { |
| 48 | 36 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(HandshakeService)); |
| | 37 | |
|
| 48 | 38 | | switch (_steps) |
| | 39 | | { |
| | 40 | | case 2: |
| 24 | 41 | | _steps--; |
| 24 | 42 | | transport = null; |
| 24 | 43 | | return IsInitiator |
| 24 | 44 | | ? InitiatorWriteActOne(outMessage) |
| 24 | 45 | | : ResponderReadActOneAndWriteActTwo(inMessage, outMessage); |
| | 46 | | case 1: |
| 16 | 47 | | _steps--; |
| 16 | 48 | | return IsInitiator |
| 16 | 49 | | ? InitiatorReadActTwoAndWriteActThree(inMessage, outMessage, out transport) |
| 16 | 50 | | : ResponderReadActThree(inMessage, out transport); |
| | 51 | | default: |
| 8 | 52 | | throw new InvalidOperationException("There's no more steps to complete"); |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| | 56 | | #region Initiator Methods |
| | 57 | | /// <summary> |
| | 58 | | /// Initiator writes act one |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="outMessage">The buffer to write the message to</param> |
| | 61 | | /// <returns>Number of bytes written to outMessage</returns> |
| | 62 | | private int InitiatorWriteActOne(Span<byte> outMessage) |
| | 63 | | { |
| | 64 | | // Write act one |
| 12 | 65 | | return _handshakeState.WriteMessage(ProtocolConstants.EMPTY_MESSAGE, outMessage).Item1; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Initiator reads act two and writes act three |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="actTwoMessage">Byte[] representation of Act Two Message</param> |
| | 72 | | /// <param name="outMessage">The buffer to write the message to</param> |
| | 73 | | /// <param name="transport"> The Transport that is going to be returned by the handshake after all steps has been co |
| | 74 | | /// <returns>Number of bytes written to outMessage</returns> |
| | 75 | | private int InitiatorReadActTwoAndWriteActThree(ReadOnlySpan<byte> actTwoMessage, Span<byte> outMessage, |
| | 76 | | out ITransport? transport) |
| | 77 | | { |
| | 78 | | // Read act two |
| 8 | 79 | | _ = _handshakeState.ReadMessage(actTwoMessage, outMessage); |
| | 80 | |
|
| | 81 | | // Write act three |
| 8 | 82 | | (var messageSize, _, transport) = _handshakeState.WriteMessage(ProtocolConstants.EMPTY_MESSAGE, outMessage); |
| | 83 | |
|
| 8 | 84 | | return messageSize; |
| | 85 | | } |
| | 86 | | #endregion |
| | 87 | |
|
| | 88 | | #region Responder Methods |
| | 89 | | /// <summary> |
| | 90 | | /// Responder reads act one and writes act two |
| | 91 | | /// </summary> |
| | 92 | | /// <param name="actOneMessage">Byte[] representation of Act One Message</param> |
| | 93 | | /// <param name="outMessage">The buffer to write the message to</param> |
| | 94 | | /// <returns>Number of bytes written to outMessage</returns> |
| | 95 | | private int ResponderReadActOneAndWriteActTwo(ReadOnlySpan<byte> actOneMessage, Span<byte> outMessage) |
| | 96 | | { |
| | 97 | | // Read act one |
| 12 | 98 | | var (responderMessageSize, _, _) = _handshakeState.ReadMessage(actOneMessage, outMessage); |
| | 99 | |
|
| | 100 | | // Write act two |
| 12 | 101 | | return _handshakeState.WriteMessage(outMessage[..responderMessageSize], outMessage).Item1; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Responder reads act three |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="actThreeMessage">Byte[] representation of Act Three Message</param> |
| | 108 | | /// <param name="transport"> The Transport that is going to be returned by the handshake after all steps has been co |
| | 109 | | /// <returns>Number of bytes read from actThreeMessage</returns> |
| | 110 | | private int ResponderReadActThree(ReadOnlySpan<byte> actThreeMessage, out ITransport? transport) |
| | 111 | | { |
| | 112 | | // Read act three |
| 8 | 113 | | var messageBuffer = new byte[ProtocolConstants.MAX_MESSAGE_LENGTH]; |
| 8 | 114 | | (var messageSize, _, transport) = _handshakeState.ReadMessage(actThreeMessage, messageBuffer); |
| | 115 | |
|
| 8 | 116 | | return messageSize; |
| | 117 | | } |
| | 118 | | #endregion |
| | 119 | |
|
| | 120 | | public void Dispose() |
| | 121 | | { |
| 0 | 122 | | if (_disposed) |
| | 123 | | { |
| 0 | 124 | | return; |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | _handshakeState.Dispose(); |
| | 128 | |
|
| 0 | 129 | | _disposed = true; |
| 0 | 130 | | } |
| | 131 | | } |