File size: 24,130 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Runspaces.Internal;
using System.Threading;

using Dbg = System.Management.Automation.Diagnostics;

namespace System.Management.Automation.Remoting
{
    /// <summary>
    /// This class is designed to contains the pertinent information about a Remote Connection,
    /// such as remote computer name, remote user name etc.
    /// It is also used to access remote connection capability and configuration information.
    /// Currently the session is identified by the InstanceId of the runspacePool associated with it
    /// This can change in future if we start supporting multiple runspacePools per session.
    /// </summary>
    internal class ClientRemoteSessionContext
    {
        #region properties

        /// <summary>
        /// Remote computer address in URI format.
        /// </summary>
        internal Uri RemoteAddress { get; set; }

        /// <summary>
        /// User credential to be used on the remote computer.
        /// </summary>
        internal PSCredential UserCredential { get; set; }

        /// <summary>
        /// Capability information for the client side.
        /// </summary>
        internal RemoteSessionCapability ClientCapability { get; set; }

        /// <summary>
        /// Capability information received from the server side.
        /// </summary>
        internal RemoteSessionCapability ServerCapability { get; set; }

        /// <summary>
        /// This is the shellName which identifies the PowerShell configuration to launch
        /// on remote machine.
        /// </summary>
        internal string ShellName { get; set; }

        #endregion Public_Properties
    }

    /// <summary>
    /// This abstract class defines the client view of the remote connection.
    /// </summary>
    internal abstract class ClientRemoteSession : RemoteSession
    {
        [TraceSource("CRSession", "ClientRemoteSession")]
        private static readonly PSTraceSource s_trace = PSTraceSource.GetTracer("CRSession", "ClientRemoteSession");

        #region Public_Method_API

        /// <summary>
        /// Client side user calls this function to create a new remote session.
        /// User needs to register event handler to ConnectionEstablished and ConnectionClosed to
        /// monitor the actual connection state.
        /// </summary>
        public abstract void CreateAsync();

        /// <summary>
        /// This event handler is raised when the state of session changes.
        /// </summary>
        public abstract event EventHandler<RemoteSessionStateEventArgs> StateChanged;

        /// <summary>
        /// Close the connection to the remote computer in an asynchronous manner.
        /// Client side user can register an event handler with ConnectionClosed to monitor
        /// the connection state.
        /// </summary>
        public abstract void CloseAsync();

        /// <summary>
        /// Disconnects the remote session in an asynchronous manner.
        /// </summary>
        public abstract void DisconnectAsync();

        /// <summary>
        /// Reconnects the remote session in an asynchronous manner.
        /// </summary>
        public abstract void ReconnectAsync();

        /// <summary>
        /// Connects to an existing remote session
        /// User needs to register event handler to ConnectionEstablished and ConnectionClosed to
        /// monitor the actual connection state.
        /// </summary>
        public abstract void ConnectAsync();

        #endregion Public_Method_API

        #region Public_Properties

        internal ClientRemoteSessionContext Context { get; } = new ClientRemoteSessionContext();

        #endregion Public_Properties

        #region URI Redirection

        /// <summary>
        /// Delegate used to report connection URI redirections to the application.
        /// </summary>
        /// <param name="newURI">
        /// New URI to which the connection is being redirected to.
        /// </param>
        internal delegate void URIDirectionReported(Uri newURI);

        #endregion

        /// <summary>
        /// ServerRemoteSessionDataStructureHandler instance for this session.
        /// </summary>
        internal ClientRemoteSessionDataStructureHandler SessionDataStructureHandler { get; set; }

        protected Version _serverProtocolVersion;
        /// <summary>
        /// Protocol version negotiated by the server.
        /// </summary>
        internal Version ServerProtocolVersion
        {
            get
            {
                return _serverProtocolVersion;
            }
        }

        private RemoteRunspacePoolInternal _remoteRunspacePool;

        /// <summary>
        /// Remote runspace pool if used, for this session.
        /// </summary>
        internal RemoteRunspacePoolInternal RemoteRunspacePoolInternal
        {
            get
            {
                return _remoteRunspacePool;
            }

            set
            {
                Dbg.Assert(_remoteRunspacePool == null, @"RunspacePool should be
                        attached only once to the session");
                _remoteRunspacePool = value;
            }
        }

        /// <summary>
        /// Get the runspace pool with the matching id.
        /// </summary>
        /// <param name="clientRunspacePoolId">
        /// Id of the runspace to get
        /// </param>
        /// <returns></returns>
        internal RemoteRunspacePoolInternal GetRunspacePool(Guid clientRunspacePoolId)
        {
            if (_remoteRunspacePool != null)
            {
                if (_remoteRunspacePool.InstanceId.Equals(clientRunspacePoolId))
                    return _remoteRunspacePool;
            }

            return null;
        }
    }

    /// <summary>
    /// Remote Session Implementation.
    /// </summary>
    internal class ClientRemoteSessionImpl : ClientRemoteSession, IDisposable
    {
        [TraceSource("CRSessionImpl", "ClientRemoteSessionImpl")]
        private static readonly PSTraceSource s_trace = PSTraceSource.GetTracer("CRSessionImpl", "ClientRemoteSessionImpl");

        private PSRemotingCryptoHelperClient _cryptoHelper = null;

        #region Constructors

        /// <summary>
        /// Creates a new instance of ClientRemoteSessionImpl.
        /// </summary>
        /// <param name="rsPool">
        /// The RunspacePool object this session should map to.
        /// </param>
        /// <param name="uriRedirectionHandler">
        /// </param>
        internal ClientRemoteSessionImpl(RemoteRunspacePoolInternal rsPool,
                                       URIDirectionReported uriRedirectionHandler)
        {
            Dbg.Assert(rsPool != null, "RunspacePool cannot be null");
            base.RemoteRunspacePoolInternal = rsPool;
            Context.RemoteAddress = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<Uri>(rsPool.ConnectionInfo,
                "ConnectionUri", null);
            _cryptoHelper = new PSRemotingCryptoHelperClient();
            _cryptoHelper.Session = this;
            Context.ClientCapability = RemoteSessionCapability.CreateClientCapability();
            Context.UserCredential = rsPool.ConnectionInfo.Credential;

            // shellName validation is not performed on the client side.
            // This is recommended by the WinRS team: for the reason that the rules may change in the future.
            Context.ShellName = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<string>(rsPool.ConnectionInfo,
                "ShellUri", string.Empty);

            MySelf = RemotingDestination.Client;
            // Create session data structure handler for this session
            SessionDataStructureHandler = new ClientRemoteSessionDSHandlerImpl(this,
                _cryptoHelper,
                rsPool.ConnectionInfo,
                uriRedirectionHandler);
            BaseSessionDataStructureHandler = SessionDataStructureHandler;
            _waitHandleForConfigurationReceived = new ManualResetEvent(false);

            // Register handlers for various ClientSessiondata structure handler events
            SessionDataStructureHandler.NegotiationReceived += HandleNegotiationReceived;
            SessionDataStructureHandler.ConnectionStateChanged += HandleConnectionStateChanged;
            SessionDataStructureHandler.EncryptedSessionKeyReceived += HandleEncryptedSessionKeyReceived;
            SessionDataStructureHandler.PublicKeyRequestReceived += HandlePublicKeyRequestReceived;
        }

        #endregion Constructors

        #region connect/close

        /// <summary>
        /// Creates a Remote Session Asynchronously.
        /// </summary>
        public override void CreateAsync()
        {
            // Raise a CreateSession event in StateMachine. This start the process of connection and negotiation to a new remote session
            RemoteSessionStateMachineEventArgs startArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.CreateSession);
            SessionDataStructureHandler.StateMachine.RaiseEvent(startArg);
        }

        /// <summary>
        /// Connects to a existing Remote Session Asynchronously by executing a Connect negotiation algorithm.
        /// </summary>
        public override void ConnectAsync()
        {
            // Raise the connectsession event in statemachine. This start the process of connection and negotiation to an existing remote session
            RemoteSessionStateMachineEventArgs startArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.ConnectSession);
            SessionDataStructureHandler.StateMachine.RaiseEvent(startArg);
        }

        /// <summary>
        /// Closes Session Connection Asynchronously.
        /// </summary>
        /// <remarks>
        /// Caller should register for ConnectionClosed event to get notified
        /// </remarks>
        public override void CloseAsync()
        {
            RemoteSessionStateMachineEventArgs closeArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close);
            SessionDataStructureHandler.StateMachine.RaiseEvent(closeArg);
        }

        /// <summary>
        /// Temporarily suspends connection to a connected remote session.
        /// </summary>
        public override void DisconnectAsync()
        {
            RemoteSessionStateMachineEventArgs startDisconnectArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.DisconnectStart);
            SessionDataStructureHandler.StateMachine.RaiseEvent(startDisconnectArg);
        }

        /// <summary>
        /// Restores connection to a disconnected remote session. Negotiation has already been performed before.
        /// </summary>
        public override void ReconnectAsync()
        {
            RemoteSessionStateMachineEventArgs startReconnectArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.ReconnectStart);
            SessionDataStructureHandler.StateMachine.RaiseEvent(startReconnectArg);
        }

        /// <summary>
        /// This event handler is raised when the state of session changes.
        /// </summary>
        public override event EventHandler<RemoteSessionStateEventArgs> StateChanged;

        /// <summary>
        /// Handles changes in data structure handler state.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arg">
        /// Event argument which contains the new state
        /// </param>
        private void HandleConnectionStateChanged(object sender, RemoteSessionStateEventArgs arg)
        {
            using (s_trace.TraceEventHandlers())
            {
                if (arg == null)
                {
                    throw PSTraceSource.NewArgumentNullException(nameof(arg));
                }

                if (arg.SessionStateInfo.State == RemoteSessionState.EstablishedAndKeyReceived) // TODO - Client session would never get into this state... to be removed
                {
                    // send the public key
                    StartKeyExchange();
                }

                if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
                {
                    // when the connection is being closed we need to
                    // complete the key exchange process to release
                    // the lock under which the key exchange is happening
                    // if we fail to release the lock, then when
                    // transport manager is closing it will try to
                    // acquire the lock again leading to a deadlock
                    CompleteKeyExchange();
                }

                StateChanged.SafeInvoke(this, arg);
            }
        }

        #endregion connect/closed

        #region KeyExchange

        /// <summary>
        /// Start the key exchange process.
        /// </summary>
        internal override void StartKeyExchange()
        {
            if (SessionDataStructureHandler.StateMachine.State == RemoteSessionState.Established ||
                SessionDataStructureHandler.StateMachine.State == RemoteSessionState.EstablishedAndKeyRequested)
            {
                // Start the key sending process
                string localPublicKey = null;
                bool ret = false;
                RemoteSessionStateMachineEventArgs eventArgs = null;
                Exception exception = null;

                try
                {
                    ret = _cryptoHelper.ExportLocalPublicKey(out localPublicKey);
                }
                catch (PSCryptoException cryptoException)
                {
                    ret = false;
                    exception = cryptoException;
                }

                if (!ret)
                {
                    // we need to complete the key exchange
                    // since the crypto helper will be waiting on it
                    CompleteKeyExchange();

                    // exporting local public key failed
                    // set state to Closed
                    eventArgs = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.KeySendFailed,
                                        exception);

                    SessionDataStructureHandler.StateMachine.RaiseEvent(eventArgs);
                }
                else
                {
                    // send using data structure handler
                    eventArgs = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.KeySent);
                    SessionDataStructureHandler.StateMachine.RaiseEvent(eventArgs);

                    SessionDataStructureHandler.SendPublicKeyAsync(localPublicKey);
                }
            }
        }

        /// <summary>
        /// Complete the key exchange process.
        /// </summary>
        internal override void CompleteKeyExchange()
        {
            _cryptoHelper.CompleteKeyExchange();
        }

        /// <summary>
        /// Handles an encrypted session key received from the other side.
        /// </summary>
        /// <param name="sender">Sender of this event.</param>
        /// <param name="eventArgs">arguments that contain the remote
        /// public key</param>
        private void HandleEncryptedSessionKeyReceived(object sender, RemoteDataEventArgs<string> eventArgs)
        {
            if (SessionDataStructureHandler.StateMachine.State == RemoteSessionState.EstablishedAndKeySent)
            {
                string encryptedSessionKey = eventArgs.Data;

                bool ret = _cryptoHelper.ImportEncryptedSessionKey(encryptedSessionKey);

                RemoteSessionStateMachineEventArgs args = null;
                if (!ret)
                {
                    // importing remote public key failed
                    // set state to closed
                    args = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.KeyReceiveFailed);

                    SessionDataStructureHandler.StateMachine.RaiseEvent(args);
                }

                // complete the key exchange process
                CompleteKeyExchange();

                args = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.KeyReceived);
                SessionDataStructureHandler.StateMachine.RaiseEvent(args);
            }
        }

        /// <summary>
        /// Handles a request for public key from the server.
        /// </summary>
        /// <param name="sender">Send of this event, unused.</param>
        /// <param name="eventArgs">Arguments describing this event, unused.</param>
        private void HandlePublicKeyRequestReceived(object sender, RemoteDataEventArgs<string> eventArgs)
        {
            if (SessionDataStructureHandler.StateMachine.State == RemoteSessionState.Established)
            {
                RemoteSessionStateMachineEventArgs args =
                    new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.KeyRequested);
                SessionDataStructureHandler.StateMachine.RaiseEvent(args);

                StartKeyExchange();
            }
        }

        #endregion KeyExchange

        // TODO:Review Configuration Story
        #region configuration

        private ManualResetEvent _waitHandleForConfigurationReceived;

        #endregion configuration

        #region negotiation

        /// <summary>
        /// Examines the negotiation packet received from the server.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arg"></param>
        private void HandleNegotiationReceived(object sender, RemoteSessionNegotiationEventArgs arg)
        {
            using (s_trace.TraceEventHandlers())
            {
                if (arg == null)
                {
                    throw PSTraceSource.NewArgumentNullException(nameof(arg));
                }

                if (arg.RemoteSessionCapability == null)
                {
                    throw PSTraceSource.NewArgumentException(nameof(arg));
                }

                Context.ServerCapability = arg.RemoteSessionCapability;

                try
                {
                    // This will throw if there is an error running the algorithm
                    RunClientNegotiationAlgorithm(Context.ServerCapability);

                    RemoteSessionStateMachineEventArgs negotiationCompletedArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationCompleted);
                    SessionDataStructureHandler.StateMachine.RaiseEvent(negotiationCompletedArg);
                }
                catch (PSRemotingDataStructureException dse)
                {
                    RemoteSessionStateMachineEventArgs negotiationFailedArg =
                        new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationFailed,
                            dse);
                    SessionDataStructureHandler.StateMachine.RaiseEvent(negotiationFailedArg);
                }
            }
        }

        /// <summary>
        /// Verifies the negotiation packet received from the server.
        /// </summary>
        /// <param name="serverRemoteSessionCapability">
        /// Capabilities of remote session
        /// </param>
        /// <returns>
        /// The method returns true if the capability negotiation is successful.
        /// Otherwise, it returns false.
        /// </returns>
        /// <exception cref="PSRemotingDataStructureException">
        /// 1. PowerShell client does not support the PSVersion {1} negotiated by the server.
        ///    Make sure the server is compatible with the build {2} of PowerShell.
        /// 2. PowerShell client does not support the SerializationVersion {1} negotiated by the server.
        ///    Make sure the server is compatible with the build {2} of PowerShell.
        /// </exception>
        private bool RunClientNegotiationAlgorithm(RemoteSessionCapability serverRemoteSessionCapability)
        {
            Dbg.Assert(serverRemoteSessionCapability != null, "server capability cache must be non-null");

            // ProtocolVersion check
            Version serverProtocolVersion = serverRemoteSessionCapability.ProtocolVersion;
            _serverProtocolVersion = serverProtocolVersion;
            Version clientProtocolVersion = Context.ClientCapability.ProtocolVersion;

            if (clientProtocolVersion == serverProtocolVersion ||
                serverProtocolVersion == RemotingConstants.ProtocolVersion_2_0 ||
                serverProtocolVersion == RemotingConstants.ProtocolVersion_2_1 ||
                serverProtocolVersion == RemotingConstants.ProtocolVersion_2_2 ||
                serverProtocolVersion == RemotingConstants.ProtocolVersion_2_3)
            {
                // passed negotiation check
            }
            else
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                        RemoteDataNameStrings.PS_STARTUP_PROTOCOL_VERSION_NAME,
                        serverProtocolVersion,
                        PSVersionInfo.GitCommitId,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // PSVersion check
            Version serverPSVersion = serverRemoteSessionCapability.PSVersion;
            Version clientPSVersion = Context.ClientCapability.PSVersion;
            if (!clientPSVersion.Equals(serverPSVersion))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                        RemoteDataNameStrings.PSVersion,
                        serverPSVersion.ToString(),
                        PSVersionInfo.GitCommitId,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // Serialization Version check
            Version serverSerVersion = serverRemoteSessionCapability.SerializationVersion;
            Version clientSerVersion = Context.ClientCapability.SerializationVersion;
            if (!clientSerVersion.Equals(serverSerVersion))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                        RemoteDataNameStrings.SerializationVersion,
                        serverSerVersion.ToString(),
                        PSVersionInfo.GitCommitId,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            return true;
        }

        #endregion negotiation

        internal override RemotingDestination MySelf { get; }

        #region IDisposable

        /// <summary>
        /// Public method for dispose.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Release all resources.
        /// </summary>
        /// <param name="disposing">If true, release all managed resources.</param>
        public void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_waitHandleForConfigurationReceived != null)
                {
                    _waitHandleForConfigurationReceived.Dispose();
                    _waitHandleForConfigurationReceived = null;
                }

                ((ClientRemoteSessionDSHandlerImpl)SessionDataStructureHandler).Dispose();
                SessionDataStructureHandler = null;
                _cryptoHelper.Dispose();
                _cryptoHelper = null;
            }
        }

        #endregion IDisposable
    }
}