Skip to main content

Fixed: "The specified file cannot be converted to a key" with MySql.Data

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The key was good, and MySQL had made it itself, yet the driver said The specified file cannot be converted to a key. It isn't the key, it's the wrapping: MySql.Data reads only one of the two formats that OpenSSL writes, and it isn't the one you get today by default.

What I Was Doing​

I was connecting with SslMode=VerifyFull on MySql.Data 26.10.0 and .NET 10, using the client certificate and key MySQL creates in its data folder:

Connection string
…;SslMode=VerifyFull;SslCa=/Users/you/projects/mysql-connection-test/certs/ca.pem;SslCert=/Users/you/projects/mysql-connection-test/certs/client-cert.pem;SslKey=/Users/you/projects/mysql-connection-test/certs/client-key.pem
Terminal
dotnet run test-connection.cs

What I Expected​

A connection, or a complaint about the server's name.

What I Got​

Output
Unhandled exception. MySql.Data.MySqlClient.MySqlException (0x80004005): SSL Connection error
---> System.Exception: The specified file cannot be converted to a key

The test program stopping with SSL Connection error and The specified file cannot be converted to a key.

What I Checked​

The first line of the key file:

Terminal
head -1 certs/client-key.pem
Output
-----BEGIN PRIVATE KEY-----

A key made by OpenSSL 4.1 on my computer, with openssl req -nodes: it starts the same way, and fails the same way.

The same key in the older format. That one worked, which settled it.

Why It Happens​

A private key file comes in two formats that look almost the same:

First lineFormatWho writes it
-----BEGIN PRIVATE KEY-----PKCS#8OpenSSL 3 and later by default, and MySQL's own generated files
-----BEGIN RSA PRIVATE KEY-----PKCS#1, also called "traditional"openssl rsa -traditional

The driver only accepts the second. Oracle's documentation doesn't say that, so you find out from this error.

The Fix​

Convert the key. -traditional writes the older format:

Terminal
openssl rsa -in client-key.pem -traditional -out client-rsa-key.pem
head -1 client-rsa-key.pem
Output
writing RSA key
-----BEGIN RSA PRIVATE KEY-----

No OpenSSL on your computer? For a key inside a MySQL container, the image has it:

Terminal
docker compose exec --user mysql mysql openssl rsa -in /var/lib/mysql/client-key.pem -traditional -out /var/lib/mysql/client-rsa-key.pem
docker compose cp mysql:/var/lib/mysql/client-rsa-key.pem certs/client-key.pem

After the Fix​

The key is accepted, and the driver moves on to the next check. Here, with the server's certificate carrying the right name as well:

Terminal
dotnet run test-connection.cs
Output
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.2

The test program printing Connected as inventory_app@% with TLS: TLSv1.2 under SslMode=VerifyFull, after converting the key to BEGIN RSA PRIVATE KEY.

Common Mistakes​

MistakeWhat happensDo this instead
Assuming openssl rsa converts it, as in older guidesOn OpenSSL 3 and later, it keeps BEGIN PRIVATE KEYAdd -traditional
Editing the first and last lines by handThe contents differ too, so it still failsConvert it
Pointing SslKey at the certificateSame errorCheck you have the key: it starts with BEGIN and has PRIVATE KEY

Troubleshooting​

SymptomLikely causeFix
The host name does not match the name on the certificateThe key is fine nowThe fix
Failed to read file at the specified locationThe path is wrong, or the file can't be readThe fix
Could not find or decode private key from opensslThe file isn't a key: that's what openssl rsa says when given a certificateCheck the first line

Conclusion​

The specified file cannot be converted to a key means the key starts with BEGIN PRIVATE KEY. MySql.Data needs BEGIN RSA PRIVATE KEY, and openssl rsa -traditional writes it.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.