In Keychain Access on OS X, Find matching public and private keys

keychainkeychain-accesspublic-key

I have a couple keys that seem to have been generated with the same names. I would like to know which public key match up with which private keys so I can rename/delete them. Is this something that is important (keeping around the public key) or does a public key get generated each time you request a certificate?

Best Answer

I guess you have been able to get around your problem as this is an old thread, but I am just writing a reply for any future reference.

The basic idea is to export your private and public keys, and use openssl to view their modulus. Matching private/public keys will have the same modulus.

Here is how to see the modulus of a private key:

  1. In Keychain Access export your private key and select "Personal Information Exchange (.p12)" file format. This will create .p12 file.

  2. Launch a terminal and use openssl to convert your .p12 file to a .pem file:

    openssl pkcs12 -in key.p12 -out key.pem -nodes
    
  3. Use openssl to view the modulus of the pem private key:

    openssl rsa -in key.pem -modulus -noout
    

Here is how to see the modulus of a public key:

  1. In Keychain Access export your public key and select "Privacy Enhanced Mail (.pem)" file format. This will create .pem file.

  2. This .pem file is a PKCS#1 PEM file (with a header -----BEGIN RSA PUBLIC KEY-----), while openssl can only read PKCS#8 PEM (with a header -----BEGIN PUBLIC KEY-----). So open up your exported public key in TextEdit and remove the RSA bit from the header and the footer, and save the changes.

  3. Use openssl to view the modulus of the pem public key:

    openssl rsa -pubin -in pubkey.pem -modulus -noout
    

Please also note that in fact, you could also delete your public keys and re-create them from the private keys (that way you could be certain of your matching pairs). To create the matching public key from a private key use the following openssl command:

openssl rsa -in key.pem -pubout -out pubkey.pem
Related Question