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

Implement rnp_signature_get_features and rnp_key_set_features. #2179

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
clang-format
  • Loading branch information
kaie committed Jan 15, 2024
commit ecf055eb986310ce4e14d50b5c09b094b406ed39
13 changes: 5 additions & 8 deletions src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,19 @@
}

static bool
update_sig_features(pgp_signature_t * dst,
const pgp_signature_t *src,
pgp_key_feature_t flags)
update_sig_features(pgp_signature_t *dst, const pgp_signature_t *src, pgp_key_feature_t flags)

Check warning on line 292 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L292

Added line #L292 was not covered by tests
{
try {
*dst = *src;
pgp_sig_subpkt_t *oldFeatures =
dst->get_subpkt(PGP_SIG_SUBPKT_FEATURES);
pgp_sig_subpkt_t *oldFeatures = dst->get_subpkt(PGP_SIG_SUBPKT_FEATURES);
if (oldFeatures) {
dst->remove_subpkt(oldFeatures);
dst->remove_subpkt(oldFeatures);

Check warning on line 298 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L295-L298

Added lines #L295 - L298 were not covered by tests
}
dst->set_key_features(flags);
return true;
} catch (const std::exception &e) {
RNP_LOG("%s", e.what());
return false;

Check warning on line 304 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L300-L304

Added lines #L300 - L304 were not covered by tests
}
}

Expand Down Expand Up @@ -455,90 +452,90 @@

/* Based on the code from pgp_key_set_expiration. */
bool
pgp_key_set_features(pgp_key_t * key,

Check warning on line 455 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L455

Added line #L455 was not covered by tests
pgp_key_t * seckey,
pgp_key_feature_t flags,
const pgp_password_provider_t &prov,
rnp::SecurityContext & ctx)
{
if (!key->is_primary()) {
RNP_LOG("Not a primary key");
return false;

Check warning on line 463 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L461-L463

Added lines #L461 - L463 were not covered by tests
}

std::vector<pgp_sig_id_t> sigs;

Check warning on line 466 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L466

Added line #L466 was not covered by tests
/* update features for the latest direct-key signature and self-signature for each userid
*/
pgp_subsig_t *sig = key->latest_selfsig(PGP_UID_NONE);
if (sig) {
sigs.push_back(sig->sigid);

Check warning on line 471 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L469-L471

Added lines #L469 - L471 were not covered by tests
}
for (size_t uid = 0; uid < key->uid_count(); uid++) {
sig = key->latest_selfsig(uid);
if (sig) {
sigs.push_back(sig->sigid);

Check warning on line 476 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L473-L476

Added lines #L473 - L476 were not covered by tests
}
}
if (sigs.empty()) {
RNP_LOG("No valid self-signature(s)");
return false;

Check warning on line 481 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L479-L481

Added lines #L479 - L481 were not covered by tests
}

rnp::KeyLocker seclock(*seckey);
for (const auto &sigid : sigs) {
pgp_subsig_t &sig = key->get_sig(sigid);

Check warning on line 486 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L484-L486

Added lines #L484 - L486 were not covered by tests
/* update signature and re-sign it */

if (sig.is_cert() && !key->is_self_cert(sig)) {

Check warning on line 489 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L489

Added line #L489 was not covered by tests
// Features subpacket appears only in self-signatures.
continue;
// Features subpacket appears only in self-signatures.
continue;

Check warning on line 491 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L491

Added line #L491 was not covered by tests
}

/* unlock secret key if needed */
if (seckey->is_locked() && !seckey->unlock(prov)) {
RNP_LOG("Failed to unlock secret key");
return false;

Check warning on line 497 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L495-L497

Added lines #L495 - L497 were not covered by tests
}

pgp_signature_t newsig;
pgp_sig_id_t oldsigid = sigid;
if (!update_sig_features(&newsig, &sig.sig, flags)) {
return false;

Check warning on line 503 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L500-L503

Added lines #L500 - L503 were not covered by tests
}

try {
if (sig.is_cert()) {
if (sig.uid >= key->uid_count()) {
RNP_LOG("uid not found");
return false;

Check warning on line 510 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L507-L510

Added lines #L507 - L510 were not covered by tests
}
seckey->sign_cert(key->pkt(), key->get_uid(sig.uid).pkt, newsig, ctx);

Check warning on line 512 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L512

Added line #L512 was not covered by tests
} else {
/* direct-key signature case */
seckey->sign_direct(key->pkt(), newsig, ctx);

Check warning on line 515 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L515

Added line #L515 was not covered by tests
}
/* replace signature, first for secret key since it may be replaced in public */
if (seckey->has_sig(oldsigid)) {
seckey->replace_sig(oldsigid, newsig);

Check warning on line 519 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L518-L519

Added lines #L518 - L519 were not covered by tests
}
if (key != seckey) {
key->replace_sig(oldsigid, newsig);

Check warning on line 522 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L521-L522

Added lines #L521 - L522 were not covered by tests
}
} catch (const std::exception &e) {
RNP_LOG("failed to calculate or add signature: %s", e.what());
return false;

Check warning on line 526 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L524-L526

Added lines #L524 - L526 were not covered by tests
}
}

if (!seckey->refresh_data(ctx)) {
RNP_LOG("Failed to refresh seckey data.");
return false;

Check warning on line 532 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L530-L532

Added lines #L530 - L532 were not covered by tests
}
if ((key != seckey) && !key->refresh_data(ctx)) {
RNP_LOG("Failed to refresh key data.");
return false;

Check warning on line 536 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L534-L536

Added lines #L534 - L536 were not covered by tests
}
return true;

Check warning on line 538 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L538

Added line #L538 was not covered by tests
}

pgp_key_t *
Expand Down
7 changes: 5 additions & 2 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6301,18 +6301,18 @@
FFI_GUARD

rnp_result_t
rnp_signature_get_features(rnp_signature_handle_t handle, uint64_t *features)

Check warning on line 6304 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6304

Added line #L6304 was not covered by tests
try {
if (!handle || !features) {
return RNP_ERROR_NULL_POINTER;

Check warning on line 6307 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6306-L6307

Added lines #L6306 - L6307 were not covered by tests
}
if (!handle->sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6310 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6309-L6310

Added lines #L6309 - L6310 were not covered by tests
}
*features = handle->sig->sig.key_get_features();
return RNP_SUCCESS;

Check warning on line 6313 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6312-L6313

Added lines #L6312 - L6313 were not covered by tests
}
FFI_GUARD

Check warning on line 6315 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6315

Added line #L6315 was not covered by tests

rnp_result_t
rnp_signature_get_keyid(rnp_signature_handle_t handle, char **result)
Expand Down Expand Up @@ -6956,38 +6956,41 @@
FFI_GUARD

rnp_result_t
rnp_key_set_features(rnp_key_handle_t key, uint64_t features)

Check warning on line 6959 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6959

Added line #L6959 was not covered by tests
try {
if (!key) {
return RNP_ERROR_NULL_POINTER;

Check warning on line 6962 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6961-L6962

Added lines #L6961 - L6962 were not covered by tests
}

pgp_key_t *pkey = get_key_prefer_public(key);
if (!pkey) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6967 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6965-L6967

Added lines #L6965 - L6967 were not covered by tests
}
pgp_key_t *skey = get_key_require_secret(key);
if (!skey) {
FFI_LOG(key->ffi, "Secret key required.");
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6972 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6969-L6972

Added lines #L6969 - L6972 were not covered by tests
}

if (!pkey->is_primary()) {
FFI_LOG(key->ffi, "Function only usable with primary keys.");
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6977 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6975-L6977

Added lines #L6975 - L6977 were not covered by tests
}

if (!pgp_key_set_features(
pkey, skey, (pgp_key_feature_t)features, key->ffi->pass_provider, key->ffi->context)) {
if (!pgp_key_set_features(pkey,

Check warning on line 6980 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6980

Added line #L6980 was not covered by tests
skey,
(pgp_key_feature_t) features,
key->ffi->pass_provider,
key->ffi->context)) {
return RNP_ERROR_GENERIC;

Check warning on line 6985 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6983-L6985

Added lines #L6983 - L6985 were not covered by tests
}
pkey->revalidate(*key->ffi->pubring);
if (pkey != skey) {
skey->revalidate(*key->ffi->secring);

Check warning on line 6989 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6987-L6989

Added lines #L6987 - L6989 were not covered by tests
}
return RNP_SUCCESS;

Check warning on line 6991 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6991

Added line #L6991 was not covered by tests
}
FFI_GUARD

Check warning on line 6993 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6993

Added line #L6993 was not covered by tests

rnp_result_t
rnp_key_set_expiration(rnp_key_handle_t key, uint32_t expiry)
Expand Down
Loading