Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PKCS7-internal BIO_f_md #1886

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

WillChilds-Klein
Copy link
Contributor

Issues:

Addresses CryptoAlg-2494

Description of changes:

This change introduces a new filter BIO, BIO_f_md for use in PR 1816.

Call-outs:

  • n/a

Testing:

  • new unit tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@codecov-commenter
Copy link

codecov-commenter commented Sep 27, 2024

Codecov Report

Attention: Patch coverage is 96.25668% with 7 lines in your changes missing coverage. Please review.

Project coverage is 78.71%. Comparing base (460a9dd) to head (81637c1).

Files with missing lines Patch % Lines
crypto/pkcs7/bio/md.c 91.86% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1886      +/-   ##
==========================================
+ Coverage   78.67%   78.71%   +0.03%     
==========================================
  Files         585      588       +3     
  Lines      100849   101037     +188     
  Branches    14299    14314      +15     
==========================================
+ Hits        79347    79535     +188     
- Misses      20868    20869       +1     
+ Partials      634      633       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@WillChilds-Klein WillChilds-Klein changed the title Initial impl, basic test [DRAFT] Add PKCS7-internal BIO_f_md Sep 29, 2024
@WillChilds-Klein WillChilds-Klein changed the title [DRAFT] Add PKCS7-internal BIO_f_md Add PKCS7-internal BIO_f_md Sep 30, 2024
@WillChilds-Klein WillChilds-Klein marked this pull request as ready for review September 30, 2024 16:57
@WillChilds-Klein WillChilds-Klein requested a review from a team as a code owner September 30, 2024 16:57
#include "../crypto/bio/internal.h"
#include "../internal.h"

// BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this a leaked note? It doesn't seem like BIO_f_md has a puts method or am I overlooking something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, should be "md_write and md_read both contribute to the digest, md_gets returns digest contents"

crypto/pkcs7/bio/md.c Show resolved Hide resolved
Comment on lines +22 to +25
static const BIO_METHOD methods_md = {
BIO_TYPE_MD, "message digest", md_write, md_read, /*puts*/ NULL,
md_gets, md_ctrl, md_new, md_free, /*callback_ctrl*/ NULL,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP: This would be easier to read if each field was on a separate line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but this was clang-format's doing. I've been using it for formatting stuff in the pkcs7/ module. If there's some set of flags/options that we prefer to use I'm happy to specify them.

Copy link
Contributor

@justsmth justsmth Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP: In other places, we have a comment next to each value indicating the field name it corresponds to:

static const BIO_METHOD methods_md = {
    BIO_TYPE_MD,       // type
    "message digest",  // name
    md_write,          // bwrite
    md_read,           // bread
    NULL,              // bputs
    md_gets,           // bgets
    md_ctrl,           // ctrl
    md_new,            // create
    md_free,           // destroy
    NULL,              // callback_ctrl
};

Comment on lines 65 to 66
ret = BIO_read(next, out, outl);
if (BIO_get_init(b)) {
Copy link
Contributor

@justsmth justsmth Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to support the use-case of leaving this BIO uninitialized while still reading data from next. In this case, data is returned that has not been digested. Is this use-case tested as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, good point. this use-case does appear to be supported as far back as OpenSSL 1.1.1. I didn't consider this, I can add a test case.

I do wonder -- should we support this? For compatibility, I'm leaning towards "yes" but it seems like a sharp edge that could cause subtle issues with digest calculation (see also response below)...

OpenSSL's own documentation indicates that the caller should re-initialize after DigestFinal (although it doesn't say anything about initial initialization):

After the digest has been retrieved from a digest BIO it must be reinitialized by calling BIO_reset(), or BIO_set_md() before any more data is passed through it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion here, we now require the BIO to be initialized before it can do any IO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, at any rate, it looks like IO on uninitialized BIO's is prevented higher in the call stack in bio.c.

BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If b was not initialized, should we still be calling BIO_clear_retry_flags and BIO_copy_next_retry?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the answer to this is "yes". It would be needed to support its use prior to (or without) initialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is addressed by requiring b to be initialized before it can do any IO.

Comment on lines 68 to 69
if (EVP_DigestUpdate(ctx, (unsigned char *)out, ret) <= 0) {
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this ever returns an error, should we somehow mark this BIO and being not OK for subsequent use? In this case, next has provided new data that can not be reprocessed on any subsequent call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenSSL doesn't do this, but i agree philosophically that we should fail loudly and not silently corrupt the digest.

EVP_DigestUpdate 's documentation stipulates an interface contract of "It returns one". Looking at the implementation, the only case where it can break that contract is if an update function pointer isn't configured on ctx. I believe we can trust that to always be populated as long as ctx has been initialized by EVP_DigestInit_ex.

So, in other words, it looks like EVP_DigestUpdate can only fail if ctx hasn't been initialized. This makes me consider... should we fail b's reads/writes before next's IO if b hasn't been initialized? this would address your concern here and make it harder to misuse the BIO_f_md.

crypto/pkcs7/bio/md.c Show resolved Hide resolved
crypto/pkcs7/bio/md.c Show resolved Hide resolved
crypto/pkcs7/bio/md.c Show resolved Hide resolved
Comment on lines +12 to +19
// |md_write| and |md_read| both contribute to the digest, |md_gets| returns
// digest contents
static int md_write(BIO *h, char const *buf, int num);
static int md_read(BIO *h, char *buf, int size);
static int md_gets(BIO *h, char *str, int size);
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int md_new(BIO *h);
static int md_free(BIO *data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP: I would prefer having methods_md and BIO_f_md at the bottom of this source file. With that change, these static function declarations can be deleted.

Comment on lines +22 to +25
static const BIO_METHOD methods_md = {
BIO_TYPE_MD, "message digest", md_write, md_read, /*puts*/ NULL,
md_gets, md_ctrl, md_new, md_free, /*callback_ctrl*/ NULL,
};
Copy link
Contributor

@justsmth justsmth Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP: In other places, we have a comment next to each value indicating the field name it corresponds to:

static const BIO_METHOD methods_md = {
    BIO_TYPE_MD,       // type
    "message digest",  // name
    md_write,          // bwrite
    md_read,           // bread
    NULL,              // bputs
    md_gets,           // bgets
    md_ctrl,           // ctrl
    md_new,            // create
    md_free,           // destroy
    NULL,              // callback_ctrl
};

Comment on lines +93 to +96
if (!EVP_DigestUpdate(ctx, (const unsigned char *)in, ret)) {
BIO_clear_retry_flags(b);
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP: Since this is a non-recoverable error, shouldn't the return value be negative? The BIO_write documentation seems to say that.

// returns the value from calling the |callback_ex|, otherwise |BIO_write|
// returns the number of bytes written, or a negative number on error.

I see OpenSSL returns 0 for this, so compatibility might dictate we do the same. (I'm also bothered that the we're unable to inform the caller about the actual number of bytes written to next b/c we can't return a value indicating success. But that's just the nature of this API.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants