public void Close() { OpenSslNative.SSL_shutdown(_ssl); if (_socket.Connected) { _socket.Close(); } }
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)); }
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)); }
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()); }
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); } }
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); } }
public void Dispose() { OpenSslNative.BIO_free(_bio); }
public static OpenSslBIO FromFile(string file) { return(new OpenSslBIO(OpenSslUtility.AssertNotNull(OpenSslNative.BIO_new_file(file, "r")))); }
public OpenSslCertificate(OpenSslBIO bio) { _x509 = OpenSslUtility.AssertNotNull(OpenSslNative.PEM_read_bio_X509(bio.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)); }
public void Dispose() { OpenSslNative.SSL_free(_ssl); }
public OpenSslContext(OpenSslProtocol protocol, bool server) { _ctx = OpenSslUtility.AssertNotNull(OpenSslNative.SSL_CTX_new(GetMethodFromProtocol(protocol, server))); }
public void Connect(Socket socket) { InitializeSocket(socket); OpenSslUtility.AssertSuccess(OpenSslNative.SSL_connect(_ssl)); }
public OpenSslConnection(OpenSslContext ctx) { _context = ctx; _ssl = OpenSslUtility.AssertNotNull(OpenSslNative.SSL_new(_context.Handle)); }
public void Dispose() { OpenSslNative.SSL_CTX_free(_ctx); }
public void Dispose() { OpenSslNative.X509_free(_x509); }
private void InitializeSocket(Socket socket) { OpenSslUtility.AssertSuccess(OpenSslNative.SSL_set_fd(_ssl, socket.Handle.ToInt32())); _socket = socket; }
public OpenSslPrivateKey(OpenSslBIO bio) { _pkey = OpenSslUtility.AssertNotNull(OpenSslNative.PEM_read_bio_PrivateKey(bio.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)); }
public void Dispose() { OpenSslNative.EVP_PKEY_free(_pkey); }