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

Improve Webcrypto error handling, resolve several internal errors and add clarifying comments #382

Merged
merged 5 commits into from
Feb 21, 2023

Conversation

fhanau
Copy link
Collaborator

@fhanau fhanau commented Feb 15, 2023

Commit messages and comments should explain the changes reasonably well, please let me know if you find anything unclear.
One issue that merits discussion is whether to validate imported RSA keys. Calling validateRsaParams() should only reject keys that are unreasonably large or possibly insecure based on the modulus size or exponent, but may break existing scripts that import such keys. Fortunately, the other changes add some key size checks during signing and should be sufficient to prevent internal errors even if unreasonably small keys are imported.
For signing with RSASSA-PKCS1-v1_5, in very few cases errors will be reported where there previously was no error (e.g. when importing a key of exactly 62 or 63 bytes, using SHA256 as the hash digest and trying to use it for signing). I think the change – requiring the key to be at least 32 bytes larger than the hash digest when 26 or 30 would be sufficient depending on which hash is used – is sensible since there is no good reason to use keys of these specific sizes, boringssl doesn't generate keys of that size already and it is unlikely anyone is using this exact configuration.

@fhanau fhanau changed the title Improve Webcrypto error handling, resolve several internal errors and rewrite. Improve Webcrypto error handling, resolve several internal errors and clarify documentation Feb 15, 2023
@fhanau fhanau changed the title Improve Webcrypto error handling, resolve several internal errors and clarify documentation Improve Webcrypto error handling, resolve several internal errors and add clarifying comments Feb 15, 2023
@jasnell
Copy link
Member

jasnell commented Feb 16, 2023

Needs an internal PR to run through CI.

// TODO: boringssl silently uses (modulusLength & ~127) for the key size, i.e. it rounds down to
// the closest multiple of 128 bits. This can easily cause confusion when non-standard key sizes
// are requested. Ideally we would throw an error when trying to create keys where the size would
// be rounded down, but this would likely break existing scripts.
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could emit a log message to confirm whether we'd break anyone, like here:

static bool logOnce KJ_UNUSED = ([] {
KJ_LOG(WARNING, "reportStartupError() customer-specific SyntaxError hack "
"is still relevant.");
return true;
})();

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could also do the same for the other cases where you identify that we could theoretically be introducing a breaking change, e.g. when calling validateRsaParams() during RSA key import, and 62/63-byte RSASSA-PKCS1-v1_5 keys used with SHA-256.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good, I'll try doing that. Can you explain why there's a lambda expression around this instance of KJ_LOG – is it related to how the warning is reported?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Never mind, it's used to only report the warning once instead of repeating it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switched to log messages to detect potential breakage for key signing and validating imported keys. For the key size issue I'd expect that there likely are scripts using sizes other than multiples of 128, so I left the TODO message as-is for now.

@fhanau
Copy link
Collaborator Author

fhanau commented Feb 16, 2023

Needs an internal PR to run through CI.

There is an internal branch (felix/crypto-errors2), would that generally be enough or is a PR preferred? The internal tests seem to be failing for some reason though – I'll take care of the suggested changes, rebase and see if that fixes it.

@jasnell
Copy link
Member

jasnell commented Feb 16, 2023

A branch is fine so long as CI shows that internal changes aren't also needed for this to land, and that nothing breaks there of course :-). The label can be removed once that's clear :-)

@fhanau
Copy link
Collaborator Author

fhanau commented Feb 17, 2023

Internal branch felix/crypto-errors2 now passes all test cases.

static bool logOnce KJ_UNUSED = ([v] {
KJ_LOG(WARNING, "Imported RSA key has invalid publicExponent ", *v,".");
return true;
})();
Copy link
Member

Choose a reason for hiding this comment

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

No action for this PR but just noting... this is a common enough pattern now that we could probably justify adding a new LOG_WARNING_ONCE("...") macro to simplify.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good suggestion! Looks like this approach is not used that much elsewhere, but if I use it again in the future I'll try adding a macro

@panva
Copy link
Contributor

panva commented Feb 21, 2023

While there are some eyes on subtle crypto 🙏 #138, #48, and of course at least Ed25519 from https://wicg.github.io/webcrypto-secure-curves/

@fhanau fhanau merged commit c417d75 into main Feb 21, 2023
@fhanau
Copy link
Collaborator Author

fhanau commented Feb 21, 2023

@panva I'll work on the secure curves and take a look at the other issues in the coming days.

@fhanau fhanau deleted the felix/crypto-errors2 branch March 23, 2023 01:48
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