Verified Commit 3fcd5fa9 authored by Karel Koci's avatar Karel Koci 🤘
Browse files

src/lib/signature: allow more descriptive debug messages

This adds more descriptive debug messages to check for cause of
signature verification fail.
parent e1465bff
Pipeline #64660 failed with stages
in 5 minutes and 17 seconds
......@@ -31,6 +31,16 @@
THREAD_LOCAL enum sign_errors sign_errno;
static const char *error_strings[] = {
[SIGN_NO_ERROR] = NULL,
[SIGN_ERR_KEY_FORMAT] = "Public key has invalid format",
[SIGN_ERR_SIG_FORMAT] = "Signature has invalid format",
[SIGN_ERR_KEY_UNKNOWN] = "Public key is invalid or has unknown type",
[SIGN_ERR_SIG_UNKNOWN] = "Signature is invalid or has unknown type",
[SIGN_ERR_NO_MATHING_KEY] = "No public key with matching signature was provided",
[SIGN_ERR_VERIFY_FAIL] = "Data or signature are corrupted",
};
struct sign_pubkey {
char pkalg[2];
uint8_t fingerprint[FINGERPRINT_SIZE];
......@@ -94,10 +104,14 @@ static bool openssl_error() {
}
bool sign_verify(const void *data, size_t data_len, const void *sign,
size_t sign_len, const struct sign_pubkey **pubkeys) {
size_t sign_len, const struct sign_pubkey *const *pubkeys) {
struct sig sig;
if (!key_load_generic(sign, sign_len, &sig, sizeof sig))
return false; // sign_errno is already set by key_load_generic
if (!key_load_generic(sign, sign_len, &sig, sizeof sig)) {
sign_errno = sign_errno == SIGN_ERR_KEY_FORMAT ? SIGN_ERR_SIG_FORMAT :
sign_errno == SIGN_ERR_KEY_UNKNOWN ? SIGN_ERR_SIG_UNKNOWN :
sign_errno;
return false;
}
// Locate appropriate key by comparing fingerprint
while (*pubkeys && memcmp(sig.fingerprint, (*pubkeys)->fingerprint, FINGERPRINT_SIZE))
......@@ -133,3 +147,7 @@ cleanup:
EVP_PKEY_free(pkey);
return res;
}
const char *sign_strerror(enum sign_errors number) {
return error_strings[number];
}
......@@ -22,8 +22,11 @@
#include "util.h"
enum sign_errors {
SIGN_NO_ERROR = 0,
SIGN_ERR_KEY_FORMAT, // Loaded key has invalid format (size does not match)
SIGN_ERR_SIG_FORMAT, // KEY_FORMAT error variant for signatures
SIGN_ERR_KEY_UNKNOWN, // Key loaded but it has unknown format or type
SIGN_ERR_SIG_UNKNOWN, // KEY_UNKNOWN error variant for signatures
SIGN_ERR_NO_MATHING_KEY, // Non of provided keys was used to sign provided message
SIGN_ERR_VERIFY_FAIL, // Provided message was corrupted (signature does not match)
};
......@@ -53,6 +56,12 @@ void sign_pubkey_free(struct sign_pubkey*);
// Possible errors: SIGN_ERR_NO_MATHING_KEY, SIGN_ERR_VERIFY_FAIL
bool sign_verify(const void *data, size_t data_len,
const void *sign, size_t sign_len,
const struct sign_pubkey**);
const struct sign_pubkey* const*);
// Provides string describing signature error
// number: signature error number
// Returns string with message describing error. You should not modify this
// message.
const char *sign_strerror(enum sign_errors number);
#endif
......@@ -414,7 +414,7 @@ static bool uri_finish_data(struct uri *uri) {
static bool verify_signature(struct uri *uri) {
if (!uri->pubkey) // no keys means no verification
return true;
ASSERT_MSG(uri->sig_uri, "Signature uri should be set if public keys are provided");
ASSERT_MSG(uri->sig_uri, "Signature uri should be set if public keys are provided (URI: %s)", uri->uri);
const uint8_t *sign;
size_t sign_len;
if (!uri_finish(uri->sig_uri, &sign, &sign_len)) {
......@@ -433,11 +433,15 @@ static bool verify_signature(struct uri *uri) {
data_len = uri->data_len;
} else {
data_len = ftell(uri->output);
// TODO additional error in assert?
ASSERT((data = mmap(NULL, data_len, PROT_READ, MAP_PRIVATE, fileno(uri->output), 0)) != MAP_FAILED);
}
bool verified = sign_verify(data, data_len, sign, sign_len, pubkeys);
bool verified = sign_verify(data, data_len, sign, sign_len,
(const struct sign_pubkey* const*)pubkeys);
if (!verified) {
DBG("URI (%s) verify failed; %s", uri->uri, sign_strerror(sign_errno));
uri_errno = URI_E_VERIFY_FAIL;
}
if (!uri->data)
munmap(data, data_len);
......@@ -445,8 +449,6 @@ static bool verify_signature(struct uri *uri) {
uri_free(uri->sig_uri);
uri->sig_uri = NULL;
if (!verified)
uri_errno = URI_E_VERIFY_FAIL;
return verified;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment