/// <summary> /// Called by servers to authenticate the server and optionally the client in /// a client-server connection using the specified certificate. /// </summary> /// <param name="cert">The certificate used to authenticate the server.</param> public void AuthenticateAsServer(X509Certificate cert) { var data = new AuthenticateAsServerData { Certificate = cert, }; // Using thread in threadpool to manage the authentication process ThreadPool.QueueUserWorkItem(AuthenticateAsServerTask, data, false); DateTime endTime = DateTime.Now + timeout; if (rdpeudpSocket.AutoHandle) { while (!IsAuthenticated && DateTime.Now < endTime) { Thread.Sleep(waitInterval); } if (!IsAuthenticated) { if (data.Exception != null) { throw data.Exception; } throw new TimeoutException("Time out when Authenticate as Server!"); } } }
/// <summary> /// Called by servers to authenticate the server and optionally the client in /// a client-server connection using the specified certificate. /// </summary> /// <param name="data">The authenticate as server data.</param> private void AuthenticateAsServerTask(AuthenticateAsServerData data) { try { var cert = data.Certificate; dtlsServerContext = new DtlsServerSecurityContext( SecurityPackageType.Schannel, new CertificateCredential((X509Certificate)cert), null, ServerSecurityContextAttribute.ReplayDetect | ServerSecurityContextAttribute.SequenceDetect | ServerSecurityContextAttribute.Confidentiality | ServerSecurityContextAttribute.ExtendedError | ServerSecurityContextAttribute.Datagram, SecurityTargetDataRepresentation.SecurityNativeDrep); // First accept. byte[] clientToken = this.GetReceivedData(this.timeout); dtlsServerContext.Accept(clientToken); this.SendData(dtlsServerContext.Token); while (dtlsServerContext.NeedContinueProcessing) { if (dtlsServerContext.HasMoreFragments) { dtlsServerContext.Accept(null); } else { clientToken = this.GetReceivedData(this.timeout); dtlsServerContext.Accept(clientToken); } if (dtlsServerContext.Token != null) { this.SendData(dtlsServerContext.Token); } } isAuthenticated = true; dtlsStreamSizes = dtlsServerContext.StreamSizes; } catch (Exception ex) { data.Exception = ex; } }