/// <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>
        private void AuthenticateAsServer(object cert)
        {
            if (cert is X509Certificate)
            {
                dtlsServerContext = new DtlsServerSecurityContext(
                    SecurityPackageType.Schannel,
                    new CertificateCredential((X509Certificate)cert),
                    null,
                    ServerSecurityContextAttribute.ReplayDetect | ServerSecurityContextAttribute.SequenceDetect |
                    ServerSecurityContextAttribute.Confidentiality | ServerSecurityContextAttribute.ExtendedError |
                    ServerSecurityContextAttribute.AllocMemory | ServerSecurityContextAttribute.Datagram,
                    SecurityTargetDataRepresentation.SecurityNativeDrep);

                try
                {
                    // 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
                {
                    // Don't throw exception in ThreadPool thread
                }
            }
        }
Пример #2
0
        /// <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;
            }
        }