Calculating RSA Key Fingerprints in Ruby
I regularly find myself working on projects that involve the manipulation and storage of RSA keys. In the past I've never had to worry about identification or presentation of these keys. Normally I've only got one too three pairs at most that I'm manipulating (server, certificate authority, client).
I've not found myself working on a project that involves presenting the certificates to users for selection and comparison. The obvious way too do this is take a page out of other developer's books and present the key's fingerprint.
For those unfamiliar with key fingerprints, they are a condensed way to compare differing RSA with a high probability that if the fingerprints match, so do the keys. These are generally based on a cryptographic digest function such as SHA1 and MD5, and you'll see them most commonly when connecting to a new SSH host and will look like the following:.
The string of 32 hex characters presented there can be compared with another known value to make sure you're connecting to the correct SSH server and will always be the same length regardless of the bit-strength of the keys used. Without the fingerprint, users would have to compare 256 hex characters for a 1024 bit key, which is a very low security key.
You can calculate the SSH fingerprint for your SSH key or a SSH host key using the ssh-keygen command like so:
It will work when the path is either a private RSA key or a public key formatted for SSH authorized key files.
X509 certificates also use a key fingerprint to help identify a certificate's signing authority. What I rapidly learned through this investigation was that they are calculated slightly differently from SSH fingerprints even if they're in the same format.
I couldn't find any good Ruby code that calculated either, and the alternatives were some dense C++. Luckily SSH fingerprints are pretty documented in RFC4253 and RFC4716. Fingerprints on RSA keys for use with OpenSSL are less clear, and there is a different method for calculating the fingerprints of certificates.
Slowly working through the undocumented bits of Ruby's OpenSSL wrapper, the RFCs and a couple of C++ implementations I finally got a set of working implementations that calculate the following fingerprints in Ruby:
- MD5 & SHA1 fingerprints for RSA SSH keys
- Fingerprints of RSA keys for use with x509 certificates
- Fingerprints of x509 certificates
The easiest being a regular x509 certificate:
You can compare the output of the above code with OpenSSL's implementation with the following command:
|
|
Please note that case sensitivity doesn't matter here (OpenSSL will return upper case hex codes).
The next one I got working was the SSH fingerprints thanks to the RFCs mentioned earlier.
Please note: The above only works for RSA SSH keys.
Calculating a SHA1 fingerprint for SSH hosts is as simple as replacing the 'MD5' class with 'SHA1' or any of the other support digest algorithms.
The last one was the hardest to track down and implement, eventually I found the answer in RFC3279 under section 2.3.1 for the format of the public key I would need to generate before performing a digest calculation on it.
|
|
bd9e1ab3 @ 2024-07-14