Skip to content

EVP_PKEY_derive() no longer accepts buffers larger than expected output #18517

Closed

Description

@jeredfloyd

Commit #6674 changed the behavior of EVP_PKEY_derive() to require the output buffer be the exact size of the output data, throwing KDF_R_WRONG_OUTPUT_BUFFER_SIZE if there is a mismatch. This is a regression against OpenSSL 1.1 and can cause existing software to fail.

The documentation (https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_derive.html) still states:

If key is not NULL then before the call the keylen parameter should contain the length of the key buffer, if the call is successful the shared secret is written to key and the amount of data written to keylen.

However the code now says:

    if (prk_len != (size_t)EVP_MD_size(evp_md)) {
        KDFerr(KDF_F_HKDF_EXTRACT, KDF_R_WRONG_OUTPUT_BUFFER_SIZE);
        return 0;
    }

Activity

  1. added
    issue: bug report
    on Jun 9, 2022
  2. added
    triaged: bug
    and removed
    issue: bug report
    on Jun 10, 2022
  3. mattcaswell commented on Jun 10, 2022

    @mattcaswell
    Member

    This does seem to be a bug. For clarity this looks like HKDF only and the check in question looks a little different now to what it looked like when it was first introduced. The check is now here:

    int sz = EVP_MD_get_size(evp_md);
    if (sz < 0)
    return 0;
    if (prk_len != (size_t)sz) {
    ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
    return 0;
    }

    Simply changing != to <= would seem sufficient, although we should also have a test for this.

  4. added
    help wanted
    on Jun 10, 2022
  5. added
    branch: master
    branch: 3.0
    on Jun 10, 2022
  6. jeredfloyd commented on Jun 10, 2022

    @jeredfloyd
    Author

    Simply changing != to <= would seem sufficient, although we should also have a test for this.

    Unfortunately, I do not believe this is sufficient. The new signature for HKDF_derive() has prk_len as an in-variable only, and it is not (and cannot be) updated here to the actual output length.

  7. levitte commented on Jun 11, 2022

    @levitte
    Member

    @mattcaswell was unfortunately looking in the wrong place. The KDF implementation "HKDF" is called with EVP_KDF_derive(), not EVP_PKEY_derive(), and the implementation is behaving correctly according to documentation:

    EVP_KDF_derive() processes any parameters in Params and then derives
    keylen bytes of key material and places it in the key buffer. If the
    algorithm produces a fixed amount of output then an error will occur
    unless the keylen parameter is equal to that output size, as returned
    by EVP_KDF_CTX_get_kdf_size()

    EVP_PKEY_derive() calls a KEX implementation, so the spot to look would be this:

    static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
    size_t outlen)
    {
    PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
    if (!ossl_prov_is_running())
    return 0;
    if (secret == NULL) {
    *secretlen = EVP_KDF_CTX_get_kdf_size(pkdfctx->kdfctx);
    return 1;
    }
    return EVP_KDF_derive(pkdfctx->kdfctx, secret, outlen, NULL);
    }

    Notably, you do find a EVP_KDF_derive() call here... but it seems that the code doesn't do enough to adapt the data to fit the requirements when calling EPV_KDF_derive(), so it ends up calling it with an outlen that's too large.

  8. levitte commented on Jun 11, 2022

    @levitte
    Member

    @jeredfloyd, would you mind testing the modification from #18533?

  9. jeredfloyd commented on Jun 12, 2022

    @jeredfloyd
    Author

    @levitte Thanks! I'll try this patch against the RHEL 9 package and report back; if it works I will also submit it internally as a proposed backport.

  10. levitte commented on Jun 12, 2022

    @levitte
    Member

    @jeredfloyd, hold on... that patch isn't quite right

  11. levitte commented on Jun 12, 2022

    @levitte
    Member

    I've added a test in a separate PR, #18536

  12. levitte commented on Jun 12, 2022

    @levitte
    Member

    @jeredfloyd, hold on... that patch isn't quite right

    I think I got it right, now

  13. added a commit that references this issue on Jun 12, 2022
    51ed3c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    branch: 3.0branch: masterhelp wantedtriaged: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions