public Stream PerformHandshake(Object socket) { lock (this) { // Validate the parameter and state. if (socket == null) { throw new ArgumentNullException("socket"); } int fd = Utils.GetSocketFd(socket); if (fd == -1) { throw new ArgumentException(); } if (stream != null) { throw new InvalidOperationException(); } if (ctx == IntPtr.Zero) { throw new ObjectDisposedException("session"); } // Create the SSL session control object. IntPtr ssl = SSL_new(ctx); if (ssl == IntPtr.Zero) { throw new NotSupportedException(); } // Create a socket BIO object and set it. IntPtr bio = BIO_new_socket((Int)fd, (Int)0); if (bio == IntPtr.Zero) { SSL_free(ssl); throw new NotSupportedException(); } SSL_set_bio(ssl, bio, bio); // Attempt to connect or accept. int result; if (isClient) { result = (int)SSL_connect(ssl); } else { result = (int)SSL_accept(ssl); } if (result != 1) { SSL_free(ssl); throw new SecurityException(); } // Get the remote certificate and record it. IntPtr x509 = SSL_get_peer_certificate(ssl); if (x509 != IntPtr.Zero) { int length = (int)i2d_X509(x509, IntPtr.Zero); if (length > 0) { IntPtr data = Marshal.AllocHGlobal(length); if (data != IntPtr.Zero) { IntPtr temp = data; i2d_X509(x509, ref temp); remoteCertificate = new byte [length]; Marshal.Copy(data, remoteCertificate, 0, length); Marshal.FreeHGlobal(data); } } X509_free(x509); } // Create the stream object and return it. stream = new OpenSSLStream(ssl); return(stream); } }
public Stream PerformHandshake(Object socket) { lock(this) { // Validate the parameter and state. if(socket == null) { throw new ArgumentNullException("socket"); } int fd = Utils.GetSocketFd(socket); if(fd == -1) { throw new ArgumentException(); } if(stream != null) { throw new InvalidOperationException(); } if(ctx == IntPtr.Zero) { throw new ObjectDisposedException("session"); } // Create the SSL session control object. IntPtr ssl = SSL_new(ctx); if(ssl == IntPtr.Zero) { throw new NotSupportedException(); } // Create a socket BIO object and set it. IntPtr bio = BIO_new_socket((Int)fd, (Int)0); if(bio == IntPtr.Zero) { SSL_free(ssl); throw new NotSupportedException(); } SSL_set_bio(ssl, bio, bio); // Attempt to connect or accept. int result; if(isClient) { result = (int)SSL_connect(ssl); } else { result = (int)SSL_accept(ssl); } if(result != 1) { SSL_free(ssl); throw new SecurityException(); } // Get the remote certificate and record it. IntPtr x509 = SSL_get_peer_certificate(ssl); if(x509 != IntPtr.Zero) { int length = (int)i2d_X509(x509, IntPtr.Zero); if(length > 0) { IntPtr data = Marshal.AllocHGlobal(length); if(data != IntPtr.Zero) { IntPtr temp = data; i2d_X509(x509, ref temp); remoteCertificate = new byte [length]; Marshal.Copy(data, remoteCertificate, 0, length); Marshal.FreeHGlobal(data); } } X509_free(x509); } // Create the stream object and return it. stream = new OpenSSLStream(ssl); return stream; } }