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

Merged
merged 9 commits into from
Oct 30, 2024
Merged

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.75676% with 6 lines in your changes missing coverage. Please review.

Project coverage is 78.75%. Comparing base (318c34c) to head (a1f145e).

Files with missing lines Patch % Lines
crypto/pkcs7/bio/md.c 92.94% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1886      +/-   ##
==========================================
+ Coverage   78.71%   78.75%   +0.04%     
==========================================
  Files         587      590       +3     
  Lines      101218   101404     +186     
  Branches    14361    14380      +19     
==========================================
+ Hits        79669    79856     +187     
+ Misses      20913    20911       -2     
- Partials      636      637       +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.

Comment on lines +73 to +58
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
justsmth
justsmth previously approved these changes Oct 24, 2024
crypto/pkcs7/bio/md.c Outdated 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

@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 80
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.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed elswhere, EVP_DigestUpdate can only fail if ctx is uninitialized here, but greed that we should return -1 and be consistent with md_read above.

justsmth
justsmth previously approved these changes Oct 28, 2024
justsmth
justsmth previously approved these changes Oct 29, 2024
samuel40791765
samuel40791765 previously approved these changes Oct 29, 2024
Comment on lines +208 to +211
#define BIO_get_md_ctx(b, mdcp) BIO_ctrl(b, BIO_C_GET_MD_CTX, 0, mdcp)

// BIO_set_md set's |b|'s EVP_MD* to |md|
#define BIO_set_md(b, md) BIO_ctrl(b, BIO_C_SET_MD, 0, md)
Copy link
Contributor

@samuel40791765 samuel40791765 Oct 29, 2024

Choose a reason for hiding this comment

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

Nit: Not really a priority since it's an internal header, but doing a direct function call instead of macros might be better. Similar to what

aws-lc/crypto/bio/bio.c

Lines 503 to 505 in 8d9809e

int BIO_set_close(BIO *bio, int close_flag) {
return (int)BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
}
is doing.

Macros allow for potential type mismatches when passing parameters.

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, that's a good point. i'll adjust this in my next PR.

@WillChilds-Klein WillChilds-Klein merged commit 11d7f49 into aws:main Oct 30, 2024
115 of 116 checks passed
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