MyDirect .NET allows you to establish secure network connections. You can do it using SSL and SSH protocols. This article describes basic concepts for these technologies and how to use it in MyDirect .NET.
The article consists of following sections:
When MySQL client communicates with MySQL server, all communication (with the exception of the user password) is done in plain text. This means that anyone who gets between your client and the server can have full access to all information transmitted, and even change the data while it is in transit. In order to protect your information you need to encrypt communications between the MySQL server and the client application.
The two following alternatives are available to you:
The Secure Sockets Layer protocol enables two parties to negotiate a 'secure' communications channel, ensuring the privacy, authenticity, and integrity of message data. The negotiation mechanism follows:
To check whether your MySQL server supports SSL you can examine the value of the have_openssl system variable:
SHOW VARIABLES LIKE 'have_openssl'
If server returns YES, you can go on to setup client. If the response is NO, or if something refuses to work in the existing configuration, please visit http://dev.mysql.com/doc/mysql/en/secure-requirements.html for instructions on how to start up required server from scratch.Once server is ready to use SSL connections you only have to setup client properly. You must have 3 additional files reachable: authority certificate (used to verify identity of client and server), client certificate and private key (used to encrypt and decrypt data during connection). Please refer to OpenSSL site for information about these files. The later part assumes you have valid certificates and private key.
When you create MySqlConnection object you have to add following parameter to connection string:
Protocol=SSL
MySqlConnection.SslOptions property points to object that holds all information necessary to establish SSL connection. Here is brief explanation on what you have to specify in this object:
| Property | Meaning |
|---|---|
| SslOptions.CACert | Location of authority certificate |
| SslOptions.Cert | Location of client certificate |
| SslOptions.Key | Location of client's private key |
| SslOptions.CipherList | List of allowed ciphers separated by colons. |
Locations of certificates can be specified in three ways:
The property SslOptions.CipherList by default is empty, which means that client agrees to use any of available ciphers. The following ciphers are allowed in MyDirect .NET:
The sample code below illustrates establishment of SSL connection.
[C#]
MySqlConnection myConn = new MySqlConnection("host=server;protocol=SSL;user=root;password=root;database=test");
myConn.SslOptions.CACert = "file://D:\\Temp\\CA-cert.pem";
myConn.SslOptions.Cert = "file://D:\\Temp\\SSL-client-cert.pem";
myConn.SslOptions.Key = "file://D:\\Temp\\SSL-client-key.pem";
MySqlCommand myCommand = new MySqlCommand("select count(*) from dept",myConn);
myConn.Open();
Int64 count = Convert.ToInt64(myCommand.ExecuteScalar());
Console.WriteLine(count);
myConn.Close();
Console.ReadLine();
[Visual Basic]
Dim myConn As MySqlConnection = New MySqlConnection("host=server;protocol=SSL;user=root;password=root;database=test")
myConn.SslOptions.CACert = "file://D:\\Temp\\CA-cert.pem"
myConn.SslOptions.Cert = "file://D:\\Temp\\SSL-client-cert.pem"
myConn.SslOptions.Key = "file://D:\\Temp\\SSL-client-key.pem"
Dim myCommand As MySqlCommand = New MySqlCommand("select count(*) from dept", myConn)
myConn.Open()
Dim count As Int64 = Convert.ToInt64(myCommand.ExecuteScalar())
Console.WriteLine(count)
myConn.Close()
Console.ReadLine()
SSH connection is established between client and SSH server. SSH server in turn communicates with MySQL server in an unencrypted mode. This is called SSH tunneling. A benefit of SSH tunneling is that it allows you to connect to a MySQL server from behind a firewall when the MySQL server port is blocked. MySQL server does not need to be attuned for this type of connection and functions as usual. To connect to MySQL server a client must first be authorized on SSH server.
You can download free SSH server at www.openssh.org. The server can run either on the same machine with MySQL server or on a different one. Note that you have to create a user on SSH server to be authorized.
When you create MySqlConnection object you have to add following parameter to connection string:
Protocol=SSH
MySqlConnection.SshOptions property points to object that holds all information necessary to connect to SSH server. Here is brief explanation on what you have to specify in this object:
| Property | Meaning |
|---|---|
| SshOptions.Host | Name or ip address of SSH server |
| SshOptions.Password | User password on SSH server |
| SshOptions.Port | Number of port on SSH server to connect |
| SshOptions.User | User id on SSH server |
MySQL server address that you specify in connection string is the address for SSH server to refer. For instance, if both servers are running on the same machine you have to specify "host=localhost" in the connection string.
[C#]
MySqlConnection myConn = new MySqlConnection("host=server;protocol=SSH;user=root;password=root;database=test");
myConn.SshOptions.User = "sshUser";
myConn.SshOptions.Password = "sshPassword";
myConn.SshOptions.Host = "sshServer";
MySqlCommand myCommand = new MySqlCommand("select count(*) from dept",myConn);
myConn.Open();
Int64 count = Convert.ToInt64(myCommand.ExecuteScalar());
Console.WriteLine(count);
myConn.Close();
[Visual Basic]
Dim myConn As MySqlConnection = New MySqlConnection("host=server;protocol=SSH;user=root;password=root;database=test")
myConn.SshOptions.User = "sshUser"
myConn.SshOptions.Password = "sshPassword"
myConn.SshOptions.Host = "sshServer"
Dim myCommand As MySqlCommand = New MySqlCommand("select count(*) from dept", myConn)
myConn.Open()
Dim count As Int64 = Convert.ToInt64(myCommand.ExecuteScalar())
Console.WriteLine(count)
myConn.Close()
MySqlConnection Class | SshOptions Class | SslOptions Class
© 2002-2008 Devart. All rights reserved.