Example #1
0
 public void Close()
 {
     OpenSslNative.SSL_shutdown(_ssl);
     if (_socket.Connected)
     {
         _socket.Close();
     }
 }
Example #2
0
        private static IntPtr GetMethodFromProtocol(OpenSslProtocol protocol, bool server)
        {
            IntPtr method = IntPtr.Zero;

            switch (protocol)
            {
            case OpenSslProtocol.AutoDetect:
                if (server)
                {
                    method = OpenSslNative.SSLv23_server_method();
                }
                else
                {
                    method = OpenSslNative.SSLv23_client_method();
                }
                break;

            case OpenSslProtocol.SSLv2:
                if (server)
                {
                    method = OpenSslNative.SSLv2_server_method();
                }
                else
                {
                    method = OpenSslNative.SSLv2_client_method();
                }
                break;

            case OpenSslProtocol.SSLv3:
                if (server)
                {
                    method = OpenSslNative.SSLv3_server_method();
                }
                else
                {
                    method = OpenSslNative.SSLv3_client_method();
                }
                break;

            case OpenSslProtocol.TLSv1:
                if (server)
                {
                    method = OpenSslNative.TLSv1_server_method();
                }
                else
                {
                    method = OpenSslNative.TLSv1_client_method();
                }
                break;

            default:
                break;
            }

            return(OpenSslUtility.AssertNotNull(method));
        }
Example #3
0
 public static string GetErrorMessage(uint err)
 {
     if (err == 0)
     {
         return("Unknown OpenSSL Error");
     }
     byte[] buf = new byte[1024];
     OpenSslNative.ERR_error_string_n(err, buf, buf.Length);
     return(Encoding.ASCII.GetString(buf));
 }
Example #4
0
        public static string GetErrorMessages()
        {
            StringBuilder sb = new StringBuilder();

            for (; ;)
            {
                uint err = OpenSslNative.ERR_get_error();
                if (err == 0)
                {
                    break;
                }
                sb.AppendLine(GetErrorMessage(err));
            }
            return(sb.ToString().TrimEnd());
        }
Example #5
0
 public void Write(byte[] buffer, int offset, int count)
 {
     if (buffer.Length < (count + offset))
     {
         count = buffer.Length - offset;
     }
     if (offset == 0)
     {
         OpenSslNative.SSL_write(_ssl, buffer, count);
     }
     else if (count > 0)
     {
         byte[] temp = new byte[count];
         Buffer.BlockCopy(buffer, offset, temp, 0, count);
         OpenSslNative.SSL_write(_ssl, temp, (int)count);
     }
 }
Example #6
0
 public int Read(byte[] buffer, int offset, int count)
 {
     if (buffer.Length < (count + offset))
     {
         count = buffer.Length - offset;
     }
     if (offset == 0)
     {
         return(OpenSslNative.SSL_read(_ssl, buffer, count));
     }
     else
     {
         byte[] temp = new byte[count];
         int    res  = OpenSslNative.SSL_read(_ssl, temp, count);
         if (res > 0)
         {
             Buffer.BlockCopy(temp, 0, buffer, offset, res);
         }
         return(res);
     }
 }
Example #7
0
 public void Dispose()
 {
     OpenSslNative.BIO_free(_bio);
 }
Example #8
0
 public static OpenSslBIO FromFile(string file)
 {
     return(new OpenSslBIO(OpenSslUtility.AssertNotNull(OpenSslNative.BIO_new_file(file, "r"))));
 }
Example #9
0
 public OpenSslCertificate(OpenSslBIO bio)
 {
     _x509 = OpenSslUtility.AssertNotNull(OpenSslNative.PEM_read_bio_X509(bio.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero));
 }
Example #10
0
 public void Dispose()
 {
     OpenSslNative.SSL_free(_ssl);
 }
Example #11
0
 public OpenSslContext(OpenSslProtocol protocol, bool server)
 {
     _ctx = OpenSslUtility.AssertNotNull(OpenSslNative.SSL_CTX_new(GetMethodFromProtocol(protocol, server)));
 }
Example #12
0
 public void Connect(Socket socket)
 {
     InitializeSocket(socket);
     OpenSslUtility.AssertSuccess(OpenSslNative.SSL_connect(_ssl));
 }
Example #13
0
 public OpenSslConnection(OpenSslContext ctx)
 {
     _context = ctx;
     _ssl     = OpenSslUtility.AssertNotNull(OpenSslNative.SSL_new(_context.Handle));
 }
Example #14
0
 public void Dispose()
 {
     OpenSslNative.SSL_CTX_free(_ctx);
 }
Example #15
0
 public void Dispose()
 {
     OpenSslNative.X509_free(_x509);
 }
Example #16
0
 private void InitializeSocket(Socket socket)
 {
     OpenSslUtility.AssertSuccess(OpenSslNative.SSL_set_fd(_ssl, socket.Handle.ToInt32()));
     _socket = socket;
 }
Example #17
0
 public OpenSslPrivateKey(OpenSslBIO bio)
 {
     _pkey = OpenSslUtility.AssertNotNull(OpenSslNative.PEM_read_bio_PrivateKey(bio.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero));
 }
Example #18
0
 public void Dispose()
 {
     OpenSslNative.EVP_PKEY_free(_pkey);
 }