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

Fix rawBlindRSA to ensure RSA-RAW algorithm is on the signing key #16

Merged
merged 1 commit into from
Feb 27, 2024

Conversation

thibmeu
Copy link
Contributor

@thibmeu thibmeu commented Feb 21, 2024

Certain platforms do not trigger an error when the algorithm name is not updated, while the setter is called.
This commit improves this behaviour by checking if the name is actually set.
If it's a silent fail, copy the key and import it with a new algorithm.

The commit also improves the test function.

@thibmeu thibmeu added the bug Something isn't working label Feb 21, 2024
@thibmeu thibmeu requested a review from armfazh February 21, 2024 14:31
@thibmeu thibmeu self-assigned this Feb 21, 2024
Object.assign(global, { crypto: webcrypto });
// webcrypto calls crypto, which is mocked. We need to restore the original implementation.
crypto.subtle.sign = parentSign;
const res = crypto.subtle.sign(algorithm, key, data);

Choose a reason for hiding this comment

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

Does this have the desired behavior? sign can be overwritten before the promise is resolved.

Should you do something like this?

res.finally(() => {
    crypto.subtile.sign = mockSign;
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

src/blindrsa.ts Outdated
@@ -188,6 +188,17 @@ export class BlindRSA {
): Promise<Uint8Array> {
const algorithmName = privateKey.algorithm.name;
privateKey.algorithm.name = BlindRSA.NATIVE_SUPPORT_NAME;
// only creates a copy if the private key algorithm name cannot be overwritten
if (privateKey.algorithm.name !== BlindRSA.NATIVE_SUPPORT_NAME) {
const privateKeyCopy = await crypto.subtle.exportKey('pkcs8', privateKey);

Choose a reason for hiding this comment

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

Where does this key come from? It might fail if privateKey.extractable is false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is passed by the caller. the error will propagate if the key is not extractable

src/blindrsa.ts Outdated
@@ -188,6 +188,17 @@ export class BlindRSA {
): Promise<Uint8Array> {
const algorithmName = privateKey.algorithm.name;
privateKey.algorithm.name = BlindRSA.NATIVE_SUPPORT_NAME;
// only creates a copy if the private key algorithm name cannot be overwritten
Copy link
Contributor

Choose a reason for hiding this comment

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

Additionally to pwu's comments: note that the spec establishes that the fields of a key are readonly.

interface CryptoKey {
  readonly attribute [KeyType];
  readonly attribute boolean [extractable];
  readonly attribute object [algorithm];
  readonly attribute object [usages];
};

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 does not seem to be strictly enforced on multiple runtime for what I've experience.
nevertheless, I've updated the logic to support keys passed with RSA-RAW (avoiding copies), and otherwise copying

Certain platforms do not trigger an error when the algorithm name is
not updated, while the setter is called.
This commit improves this behaviour by checking if the name is actually
set.
If it's a silent fail, copy the key and import it with a new algorithm.
@thibmeu thibmeu merged commit adc7572 into cloudflare:main Feb 27, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants