ssltls1.2mbedtls

How can I verify with mbedtls, that a cert validates a key?


Mbedtls can validate an x509 cert with its mbedtls_x509_crt_verify(...) function (link).

However, what I have:

There is no problem with the validation of the certificate.

However, what if that certificate validates a different key? (What can be the result of a software problem and also a crack attempt.) Of course, such a key/cert pair will be unable for a tls handshake, but I don't think that I would need to build up a tcp connection for that.

This source (although it is for openssl scripting) makes likely, that certificate-key matching validation can happen with simply a modulus match.

There is also an mbedtls_pk_verify(...) function (ref), but it seems to me that it plays mostly with signatures. But I have no signatures, I have a cert (got in a pem format), and my key (I have also that in a pem format). Processing them into internal mbedtls data structures (mbedtls_x509_crt and mbedtls_pk_context) is not a problem, but how could I verify that they match?


Solution

  • I know this is an older question, but perhaps mbedtls_pk_check_pair is what you are looking for. Pass it your private/public key pair and the certificates public key.

    /**
     * \brief           Check if a public-private pair of keys matches.
     *
     * \param pub       Context holding a public key.
     * \param prv       Context holding a private (and public) key.
     *
     * \return          \c 0 on success (keys were checked and match each other).
     * \return          #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
     *                  be checked - in that case they may or may not match.
     * \return          #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
     * \return          Another non-zero value if the keys do not match.
     */
    int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );