I've had a design where one Socket
has two NetworkStream
opened: one for reads and one for writes.
When updating the code to start using SslStreams
a call to sslStream.AuthenticateAsClientAsync
yields an error for the second stream (order is not relevant).
Win32Exception: The token supplied to the function is invalid
Q: Is there a way to have two active and authenticated SslStream
against one Socket
?
Sample code:
NetworkStream readStream = socket.CreateReadWriteStream();
NetworkStream writeStream = socket.CreateReadWriteStream();
var sslR = new SslStream(readStream, false, RemoteCertificateValidationCallback, null, EncryptionPolicy.RequireEncryption);
var sslW = new SslStream(writeStream, false, RemoteCertificateValidationCallback, null, EncryptionPolicy.RequireEncryption);
var clientAuthOptions = new SslClientAuthenticationOptions
{
RemoteCertificateValidationCallback = RemoteCertificateValidationCallback,
AllowRenegotiation = true,
CertificateRevocationCheckMode = X509RevocationMode.Online,
ClientCertificates = connectionInfo.ClientCertificates,
EnabledSslProtocols = SslProtocols.Tls12,
EncryptionPolicy = EncryptionPolicy.RequireEncryption,
TargetHost = host.Address
};
//Suceeds
await sslR.AuthenticateAsClientAsync(clientAuthOptions, cancellationToken).ConfigureAwait(false);
//Fails
await sslW.AuthenticateAsClientAsync(clientAuthOptions, cancellationToken).ConfigureAwait(false);