[openssl-users] Programmatically check private key and public key cert?

Viktor Dukhovni openssl-users at dukhovni.org
Thu Jan 11 17:45:18 UTC 2018



> On Jan 11, 2018, at 10:28 AM, pratyush parimal <pratyush.parimal at gmail.com> wrote:
> 
> After googling, it seems that I may be able to verify that by comparing the modulus
> from the key and the cert. Does anyone know if that's sufficient, and how to do it
> programmatically?

It may be useful to note that ECDSA keys don't have a modulus, that's RSA-specific,
so a more general approach is to compare public keys.  A more broadly applicatble
command-line test is:

   #! /bin/sh
   certfile=$1; shift
   keyfile=$1; shift

   certid=$(openssl x509 -in "$certfile" -noout -pubkey |
            openssl pkey -pubin -outform DER |
            openssl dgst -sha256 -binary |
            hexencode -ve '/1 "%02x"')
   keyid=$(openssl pkey -in "$keyfile" -pubout -outform DER |
           openssl dgst -sha256 -binary |
           hexencode -ve '/1 "%02x"')
   if [ "$certid" != "$keyid" ]; then
      echo "Certificate in $certfile does not match key in $keyfile" >&2
      exit 1
   fi

Karl Denninger <karl at denninger.net> already explained how key/cert correspondence
can be checked when loading the key and cert into an SSL_CTX.

The certificate should have appropriate an appropriate keyUsage and/or
extendedKeyUsage for the purpose at hand (TLS Server Authentication?).

-- 
	Viktor.



More information about the openssl-users mailing list