Mono.Net.Security.MonoTlsStream.CreateStream C# (CSharp) Method

CreateStream() private method

private CreateStream ( byte buffer ) : Stream
buffer byte
return Stream
		internal Stream CreateStream (byte[] buffer)
		{
			sslStream = provider.CreateSslStream (networkStream, false, settings);

			try {
				sslStream.AuthenticateAsClient (
					request.Host, request.ClientCertificates,
					(SslProtocols)ServicePointManager.SecurityProtocol,
					ServicePointManager.CheckCertificateRevocationList);

				status = WebExceptionStatus.Success;
			} catch (Exception) {
				status = WebExceptionStatus.SecureChannelFailure;
				throw;
			} finally {
				if (CertificateValidationFailed)
					status = WebExceptionStatus.TrustFailure;

				if (status == WebExceptionStatus.Success)
					request.ServicePoint.UpdateClientCertificate (sslStream.InternalLocalCertificate);
				else {
					request.ServicePoint.UpdateClientCertificate (null);
					sslStream = null;
				}
			}

			try {
				if (buffer != null)
					sslStream.Write (buffer, 0, buffer.Length);
			} catch {
				status = WebExceptionStatus.SendFailure;
				sslStream = null;
				throw;
			}

			return sslStream.AuthenticatedStream;
		}
#endif

Usage Example

Example #1
0
		bool CreateStream (HttpWebRequest request)
		{
			try {
				NetworkStream serverStream = new NetworkStream (socket, false);

				if (request.Address.Scheme == Uri.UriSchemeHttps) {
#if SECURITY_DEP
					if (!reused || nstream == null || tlsStream == null) {
						byte [] buffer = null;
						if (sPoint.UseConnect) {
							bool ok = CreateTunnel (request, sPoint.Address, serverStream, out buffer);
							if (!ok)
								return false;
						}
						tlsStream = new MonoTlsStream (request, serverStream);
						nstream = tlsStream.CreateStream (buffer);
					}
					// we also need to set ServicePoint.Certificate 
					// and ServicePoint.ClientCertificate but this can
					// only be done later (after handshake - which is
					// done only after a read operation).
#else
					throw new NotSupportedException ();
#endif
				} else {
					nstream = serverStream;
				}
			} catch (Exception ex) {
				if (tlsStream != null)
					status = tlsStream.ExceptionStatus;
				else if (!request.Aborted)
					status = WebExceptionStatus.ConnectFailure;
				connect_exception = ex;
				return false;
			}

			return true;
		}