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 key hashing for v5 / v6 signatures #2261

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/lib/fingerprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ try {
{
auto halg = key.version == PGP_V4 ? PGP_HASH_SHA1 : PGP_HASH_SHA256;
auto hash = rnp::Hash::create(halg);
signature_hash_key(key, *hash);
signature_hash_key(key, *hash, key.version);
fp.length = hash->finish(fp.fingerprint);
return RNP_SUCCESS;
}
Expand Down
20 changes: 12 additions & 8 deletions src/librepgp/stream-sig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,36 @@
#include "stream-armor.h"
#include "pgp-key.h"
#include "crypto/signatures.h"
#include <cassert>

#include <time.h>

void
signature_hash_key(const pgp_key_pkt_t &key, rnp::Hash &hash)
signature_hash_key(const pgp_key_pkt_t &key, rnp::Hash &hash, pgp_version_t pgpver)
{
if (!key.hashed_data) {
/* call self recursively if hashed data is not filled, to overcome const restriction */
pgp_key_pkt_t keycp(key, true);
keycp.fill_hashed_data();
signature_hash_key(keycp, hash);
signature_hash_key(keycp, hash, pgpver);
return;
}

switch (key.version) {
switch (pgpver) {
case PGP_V2:
FALLTHROUGH_STATEMENT;
case PGP_V3:
FALLTHROUGH_STATEMENT;
case PGP_V4: {
assert(key.hashed_len < ((size_t) 1 << 16));
uint8_t hdr[3] = {0x99, 0x00, 0x00};
write_uint16(hdr + 1, key.hashed_len);
hash.add(hdr, 3);
hash.add(key.hashed_data, key.hashed_len);
break;
}
case PGP_V5: {
assert(key.hashed_len < ((size_t) 1 << 32));
uint8_t hdr[5] = {0x9A, 0x00, 0x00, 0x00, 0x00};
write_uint32(hdr + 1, key.hashed_len);
hash.add(&hdr, 5);
Expand All @@ -77,6 +80,7 @@
}
#if defined(ENABLE_CRYPTO_REFRESH)
case PGP_V6: {
assert(key.hashed_len < ((size_t) 1 << 32));
uint8_t hdr[5] = {0x9b, 0x00, 0x00, 0x00, 0x00};
write_uint32(hdr + 1, key.hashed_len);
hash.add(hdr, sizeof(hdr));
Expand All @@ -85,7 +89,7 @@
}
#endif
default:
RNP_LOG("unknown key version: %d", (int) key.version);
RNP_LOG("unknown key/sig version: %d", (int) pgpver);

Check warning on line 92 in src/librepgp/stream-sig.cpp

View check run for this annotation

Codecov / codecov/patch

src/librepgp/stream-sig.cpp#L92

Added line #L92 was not covered by tests
throw rnp::rnp_exception(RNP_ERROR_OUT_OF_MEMORY);
}
}
Expand Down Expand Up @@ -121,7 +125,7 @@
const pgp_userid_pkt_t &userid)
{
auto hash = signature_init(key, sig);
signature_hash_key(key, *hash);
signature_hash_key(key, *hash, sig.version);
signature_hash_userid(userid, *hash, sig.version);
return hash;
}
Expand All @@ -132,16 +136,16 @@
const pgp_key_pkt_t & subkey)
{
auto hash = signature_init(key, sig);
signature_hash_key(key, *hash);
signature_hash_key(subkey, *hash);
signature_hash_key(key, *hash, sig.version);
signature_hash_key(subkey, *hash, sig.version);
return hash;
}

std::unique_ptr<rnp::Hash>
signature_hash_direct(const pgp_signature_t &sig, const pgp_key_pkt_t &key)
{
auto hash = signature_init(key, sig);
signature_hash_key(key, *hash);
signature_hash_key(key, *hash, sig.version);
return hash;
}

Expand Down
4 changes: 3 additions & 1 deletion src/librepgp/stream-sig.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,10 @@ typedef struct pgp_signature_info_t {
* Throws exception on error.
* @param key key packet, must be populated
* @param hash initialized hash context
* @param pgpver for fingerprint calculation, the key version is required,
* otherwise the signature version is required
*/
void signature_hash_key(const pgp_key_pkt_t &key, rnp::Hash &hash);
void signature_hash_key(const pgp_key_pkt_t &key, rnp::Hash &hash, pgp_version_t pgpver);

void signature_hash_userid(const pgp_userid_pkt_t &uid, rnp::Hash &hash, pgp_version_t sigver);

Expand Down
Loading