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

Retry password request during decryption. #2018

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/librepgp/stream-parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2107,8 +2107,17 @@ init_encrypted_src(pgp_parse_handler_t *handler, pgp_source_t *src, pgp_source_t
}
/* Decrypt key */
rnp::KeyLocker seclock(*seckey);
if (!seckey->unlock(*handler->password_provider, PGP_OP_DECRYPT)) {
errcode = RNP_ERROR_BAD_PASSWORD;
int attempts = 3;
while (attempts--) {
errcode = RNP_ERROR_NO_SUITABLE_KEY;
if (!seckey->unlock(*handler->password_provider, PGP_OP_DECRYPT)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here and below we should take in account case when there are multiple recipients of the message, and user want to enter password only to the particular one. This may be checked by returning result of password provider call - false or true.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also we should make number of password request tries configurable.

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 may be checked by returning result of password provider call - false or true.

In case of a rnp cli tool and password provider that reads from the pass-fd, it does not check the keyid, so the password and true result would be returned every time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Otherwise this code should still succeed when one of the passwords matches one of the keys, just many "Bad password" messages will be printed on stderr as a side effect.

Copy link
Contributor

Choose a reason for hiding this comment

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

In case of a rnp cli tool and password provider that reads from the pass-fd, it does not check the keyid, so the password and true result would be returned every time.

These changes are mostly not for CLI, but for other FFI users (like TB), to let them repeat the password request if needed.

errcode = RNP_ERROR_BAD_PASSWORD;
RNP_LOG("Bad password, try again.");
continue;
}
break;
}
if (errcode == RNP_ERROR_BAD_PASSWORD) {
continue;
}

Expand All @@ -2129,20 +2138,28 @@ init_encrypted_src(pgp_parse_handler_t *handler, pgp_source_t *src, pgp_source_t
if (!have_key && !param->symencs.empty()) {
rnp::secure_array<char, MAX_PASSWORD_LENGTH> password;
pgp_password_ctx_t pass_ctx(PGP_OP_DECRYPT_SYM);
if (!pgp_request_password(
handler->password_provider, &pass_ctx, password.data(), password.size())) {
errcode = RNP_ERROR_BAD_PASSWORD;
goto finish;
}
int attempts = 3;

int intres = encrypted_try_password(param, password.data());
if (intres > 0) {
have_key = true;
} else if (intres < 0) {
errcode = RNP_ERROR_NOT_SUPPORTED;
} else {
errcode = RNP_ERROR_BAD_PASSWORD;
}
while (attempts--) {
errcode = RNP_ERROR_NO_SUITABLE_KEY;
if (!pgp_request_password(
handler->password_provider, &pass_ctx, password.data(), password.size())) {
errcode = RNP_ERROR_BAD_PASSWORD;
goto finish;
}

int intres = encrypted_try_password(param, password.data());
if (intres > 0) {
have_key = true;
break;
} else if (intres < 0) {
errcode = RNP_ERROR_NOT_SUPPORTED;
break;
} else {
errcode = RNP_ERROR_BAD_PASSWORD;
RNP_LOG("Bad password, try again.");
}
}
}

/* report decryption start to the handler */
Expand Down