Class SslStream
Provides a stream used for client-server communication that uses the Secure Socket Layer (SSL) security protocol to authenticate the server and optionally the client.
public class SslStream : NetworkStream, IDisposable
- Inheritance
-
SslStream
- Implements
- Inherited Members
- Extension Methods
Constructors
SslStream(Socket)
Initializes a new instance of the SslStream class using the specified Socket.
public SslStream(Socket socket)
Parameters
socketSocketA valid socket that currently has a TCP connection.
Remarks
The SslStream maintains the lifetime of the socket. When the SslStream object is disposed, the underlying TCP socket will be closed.
Properties
DataAvailable
Gets a value the indicates whether data is available in the stream. (Overrides NetworkStream. . :: . .DataAvailable.)
public override bool DataAvailable { get; }
Property Value
IsServer
Gets a value that indicates whether the local side of the connection used by this SslStream was authenticated as the server.
public bool IsServer { get; }
Property Value
Length
Gets the length of the stream. (Overrides NetworkStream. . :: . .Length.)
public override long Length { get; }
Property Value
SslVerification
Option for SSL verification. The default behaviour is CertificateRequired.
public SslVerification SslVerification { get; set; }
Property Value
UseStoredDeviceCertificate
Option to use the certificate stored in the device as client or server certificate. The default option is false.
public bool UseStoredDeviceCertificate { get; set; }
Property Value
Remarks
This property is exclusive of .NET nanoFramework. In case there is no device certificate stored, the authentication will use whatever is provided (or not) in the parameter of the method being called.
Methods
AuthenticateAsClient(string, SslProtocols)
Called by clients to authenticate the server and optionally the client in a client-server connection. The authentication process uses the specified SSL protocols.
public void AuthenticateAsClient(string targetHost, SslProtocols enabledSslProtocols)
Parameters
targetHoststringThe name of the server that will share this SslStream.
enabledSslProtocolsSslProtocolsThe SslProtocols value that represents the protocol used for authentication.
Exceptions
- InvalidOperationException
Authentication has already been performed on this stream, or all native SSL context slots are in.
- OutOfMemoryException
A memory allocation failed while setting up the SSL context. The device may be low on heap.
- CryptographicException
SSL context initialisation failed. The ErrorCode property contains the corresponding SslError value: DrbgSeedFailed - entropy source could not be initialised; ConfigDefaultsFailed - internal mbedTLS configuration error; UnsupportedProtocolVersion - the requested TLS version is not supported on this device; SetupFailed - final SSL context setup failed.
- InvalidOperationException
Thrown during the TLS handshake when: HandshakeBadContext - the SSL context was not valid; HandshakeSetHostname - setting the server hostname for SNI failed.
- CryptographicException
Thrown during the TLS handshake. The ErrorCode property contains: for HandshakeCertVerifyFailed - the bitmask of
MBEDTLS_X509_BADCERT_*verification flags; for HandshakeFailed - the raw negative mbedTLS error code.
AuthenticateAsClient(string, X509Certificate, SslProtocols)
Called by clients to authenticate the server and optionally the client in a client-server connection. The authentication process uses the specified certificate collections and SSL protocols.
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, SslProtocols enabledSslProtocols)
Parameters
targetHoststringThe name of the server that will share this SslStream.
clientCertificateX509CertificateThe client certificate.
enabledSslProtocolsSslProtocolsThe SslProtocols value that represents the protocol used for authentication.
Remarks
Instead of providing the client certificate in the clientCertificate parameter the UseStoredDeviceCertificate property can be used to use the certificate stored in the device.
Exceptions
- InvalidOperationException
Authentication has already been performed on this stream, or all native SSL context slots are in.
- OutOfMemoryException
A memory allocation failed while setting up the SSL context. The device may be low on heap.
- CryptographicException
SSL context initialisation failed. The ErrorCode property contains the corresponding SslError value: DrbgSeedFailed - entropy source could not be initialised; ConfigDefaultsFailed - internal mbedTLS configuration error; UnsupportedProtocolVersion - the requested TLS version is not supported on this device; CertificateParseFailed - the client certificate could not be parsed; PrivateKeyParseFailed - the client private key could not be parsed; OwnCertConfigFailed - configuring the certificate/key pair on the SSL context failed; SetupFailed - final SSL context setup failed.
- InvalidOperationException
Thrown during the TLS handshake when: HandshakeBadContext - the SSL context was not valid; HandshakeSetHostname - setting the server hostname for SNI failed.
- CryptographicException
Thrown during the TLS handshake. The ErrorCode property contains: for HandshakeCertVerifyFailed - the bitmask of
MBEDTLS_X509_BADCERT_*verification flags; for HandshakeFailed - the raw negative mbedTLS error code.
AuthenticateAsClient(string, X509Certificate, X509Certificate, SslProtocols)
Called by clients to authenticate the server and optionally the client in a client-server connection. The authentication process uses the specified certificate collections and SSL protocols.
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, X509Certificate ca, SslProtocols enabledSslProtocols)
Parameters
targetHoststringThe name of the server that will share this SslStream.
clientCertificateX509CertificateThe client certificate.
caX509CertificateCertificate Authority certificate to use for authentication with the server.
enabledSslProtocolsSslProtocolsThe SslProtocols value that represents the protocol used for authentication.
Remarks
Instead of providing the client certificate in the clientCertificate parameter the UseStoredDeviceCertificate property can be used to use the certificate stored in the device.
Exceptions
- InvalidOperationException
Authentication has already been performed on this stream, or all native SSL context slots are in.
- OutOfMemoryException
A memory allocation failed while setting up the SSL context. The device may be low on heap.
- CryptographicException
SSL context initialisation failed. The ErrorCode property contains the corresponding SslError value: DrbgSeedFailed - entropy source could not be initialised; ConfigDefaultsFailed - internal mbedTLS configuration error; UnsupportedProtocolVersion - the requested TLS version is not supported on this device; CertificateParseFailed - the client or CA certificate could not be parsed; PrivateKeyParseFailed - the client private key could not be parsed; OwnCertConfigFailed - configuring the certificate/key pair on the SSL context failed; SetupFailed - final SSL context setup failed.
- InvalidOperationException
Thrown during the TLS handshake when: HandshakeBadContext - the SSL context was not valid; HandshakeSetHostname - setting the server hostname for SNI failed.
- CryptographicException
Thrown during the TLS handshake. The ErrorCode property contains: for HandshakeCertVerifyFailed - the bitmask of
MBEDTLS_X509_BADCERT_*verification flags; for HandshakeFailed - the raw negative mbedTLS error code.
AuthenticateAsServer(X509Certificate, bool, SslProtocols)
Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificates, requirements and security protocol.
public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols)
Parameters
serverCertificateX509CertificateThe X509Certificate used to authenticate the server.
clientCertificateRequiredboolA bool value that specifies whether the client is asked for a certificate for authentication. Note that this is only a request — if no certificate is provided, the server still accepts the connection request.
enabledSslProtocolsSslProtocolsThe protocols that may be used for authentication.
Remarks
Instead of providing the server certificate in the serverCertificate parameter the UseStoredDeviceCertificate property can be used to use the certificate stored in the device.
Exceptions
- InvalidOperationException
Authentication has already been performed on this stream, or all native SSL context slots are in.
- OutOfMemoryException
A memory allocation failed while setting up the SSL context. The device may be low on heap.
- CryptographicException
SSL context initialisation failed. The ErrorCode property contains the corresponding SslError value: DrbgSeedFailed - entropy source could not be initialised; ConfigDefaultsFailed - internal mbedTLS configuration error; UnsupportedProtocolVersion - the requested TLS version is not supported on this device; CertificateParseFailed - the server certificate could not be parsed; PrivateKeyParseFailed - the server private key could not be parsed; OwnCertConfigFailed - configuring the certificate/key pair on the SSL context failed; SetupFailed - final SSL context setup failed.
- InvalidOperationException
Thrown during the TLS handshake when HandshakeBadContext - the SSL context was not valid.
- CryptographicException
Thrown during the TLS handshake. The ErrorCode property contains: for HandshakeCertVerifyFailed - the bitmask of
MBEDTLS_X509_BADCERT_*verification flags (when client certificate verification is enabled); for HandshakeFailed - the raw negative mbedTLS error code.
AuthenticateAsServer(X509Certificate, SslProtocols)
Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificate, verification requirements and security protocol.
public void AuthenticateAsServer(X509Certificate serverCertificate, SslProtocols enabledSslProtocols)
Parameters
serverCertificateX509CertificateThe certificate used to authenticate the server.
enabledSslProtocolsSslProtocolsThe protocols that may be used for authentication.
Remarks
Instead of providing the server certificate in the serverCertificate parameter the UseStoredDeviceCertificate property can be used to use the certificate stored in the device.
Exceptions
- InvalidOperationException
Authentication has already been performed on this stream, or all native SSL context slots are in.
- OutOfMemoryException
A memory allocation failed while setting up the SSL context. The device may be low on heap.
- CryptographicException
SSL context initialisation failed. The ErrorCode property contains the corresponding SslError value: DrbgSeedFailed - entropy source could not be initialised; ConfigDefaultsFailed - internal mbedTLS configuration error; UnsupportedProtocolVersion - the requested TLS version is not supported on this device; CertificateParseFailed - the server certificate could not be parsed; PrivateKeyParseFailed - the server private key could not be parsed; OwnCertConfigFailed - configuring the certificate/key pair on the SSL context failed; SetupFailed - final SSL context setup failed.
- InvalidOperationException
Thrown during the TLS handshake when HandshakeBadContext - the SSL context was not valid.
- CryptographicException
Thrown during the TLS handshake. The ErrorCode property contains: for HandshakeCertVerifyFailed - the bitmask of
MBEDTLS_X509_BADCERT_*verification flags (when client certificate verification is enabled); for HandshakeFailed - the raw negative mbedTLS error code.
Dispose(bool)
Releases the unmanaged resources used by the SslStream and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposingbooltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
Finalize()
protected override void Finalize()
Read(byte[], int, int)
Reads data from this stream and stores it in the specified array.
public override int Read(byte[] buffer, int offset, int size)
Parameters
bufferbyte[]An array that receives the bytes read from this stream.
offsetintAn integer that contains the zero-based location in buffer at which to begin storing the data read from this stream.
sizeintThe maximum number of bytes to read from this stream.
Returns
Write(byte[], int, int)
Write the specified number of bytes to the underlying stream using the specified buffer and offset.
public override void Write(byte[] buffer, int offset, int size)
Parameters
bufferbyte[]An array that supplies the bytes written to the stream.
offsetinthe zero-based location in buffer at which to begin reading bytes to be written to the stream.
sizeintThe number of bytes to read from buffer.
Exceptions
- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorsizeis less than zero-or-
offsetis greater than the length ofbuffer.-or-
offset+sizeis greater than the length ofbuffer.- ObjectDisposedException
The stream has been disposed.
- IOException
The write operation failed.