mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-25 07:36:22 +00:00
merge: sync upstream profanity-im/profanity
Sync with upstream profanity-im/profanity.
Major upstream changes incorporated:
Memory management
- Replace malloc+memset with g_new0 throughout codebase
- Adopt auto_gchar / auto_gcharv / auto_gerror / auto_jid cleanup macros
- Replace free() with g_free() for GAlloc'd memory
Editor rewrite
- Remove pthread-based async editor; use GChildWatch callback API
- New launch_editor(initial_content, callback, user_data) interface
- Proper signal handling (SIGINT, SIGTSTP, SIGPIPE reset in child)
- ui_suspend()/ui_resume() integration for TTY management
OMEMO improvements
- Dual backend support: libsignal-protocol-c and libomemo-c
- Proper pre-key removal after use (XEP-0384 compliance)
- Automatic pre-key regeneration when store drops below threshold
- New functions: omemo_is_device_active(), omemo_is_jid_trusted()
- omemo_get_jid_untrusted_fingerprints() for better error messages
- Fingerprint notifications on new device identity discovery
- Deterministic pre-key ID generation tracking max_pre_key_id
- omemo_trust_changed() UI updates on trust state changes
JID validation
- RFC 6122-compliant validation in jid_is_valid()
- Character-level checks (RFC 6122 forbidden chars: & ' / : < > @)
- Length limits: 1023 per component, 3071 total
- New jid_is_valid_user_jid() for user vs. service JID distinction
Database
- Schema migration v3: UNIQUE constraint on archive_id for deduplication
- Triggers for corrected message tracking (replaces_db_id / replaced_by_db_id)
- db_history_result_t return type, _truncate_datetime_suffix()
UI / console
- win_warn_needed() / win_warn_sent() warning deduplication hash table
- PAD_MIN_HEIGHT dynamic pad sizing with PAD_THRESHOLD auto-cleanup
- Spellcheck integration in input field with Unicode word detection
- cons_spellcheck_setting() for /settings ui output
- /[command]? shortcut for command help
Account config
- Account name sanitization for GKeyFile special chars ([ ] = # \n \r)
- Replace popen() with g_spawn_sync() for eval_password
- TLS policy: add "direct" option alongside legacy
Connection
- Port validation with g_assert (0–65535)
- SHA-256 certificate fingerprint support (XMPP_CERT_PUBKEY_FINGERPRINT_SHA256)
- "direct" TLS policy alias for legacy SSL
Common utilities
- str_xml_sanitize() for XML 1.0 illegal character removal
- string_matches_one_of() with formatted error messages
- valid_tls_policy_option() helper
- prof_date_time_format_iso8601() utility
- Improved strip_arg_quotes() with backslash unescaping
- prof_occurrences() uses g_slist_prepend + reverse for performance
PGP / OX
- Proper GPGME resource cleanup with goto-cleanup pattern
- g_string_free(xmppuri) leak fix in _ox_key_lookup
CSV export
- Use GString + g_file_set_contents instead of raw write() syscalls
Tests
- Restructured into subdirectories: command/, config/, xmpp/, ui/, omemo/, otr/, pgp/
- New test_cmd_ac.c for autocompleter unit tests
- Updated stubs for new UI suspend/resume functions
License headers
- Migrate to SPDX-3.0 identifiers (GPL-3.0-or-later WITH OpenSSL-exception)
────────────────────────────────────────────────────
cproof-specific preservations:
- XEP-0308 LMC: replace_id ?: id logic in message/stanza/omemo
- Force encryption: cmd_force_encryption, test_forced_encryption
- CWE-134: format string protection (cons_show("%s", ...))
- y_start_pos-based paging in window.c
- db_history_result_t return type, _truncate_datetime_suffix()
Merge-time fixes:
- common.c: format-security (-Werror) — cons_show(errmsg) → cons_show("%s", errmsg)
- database.c: null-deref guard — !msg->timestamp → msg && !msg->timestamp
- console.c: implicit size_t → int cast — (int)(maxlen + 1)
- tlscerts.c: %d for size_t — %zu
Build system:
- Kept autotools (Makefile.am, configure.ac); upstream uses Meson
- Restored deleted files: bootstrap.sh, autogen.sh, ax_valgrind_check.m4, configure-debug
- Updated Makefile.am test paths for subdirectory structure
- Added test_cmd_ac, test_forced_encryption to test sources
Functional tests:
- Use cproof version; upstream requires stbbr_for_xmlns from updated stabber
- Not yet available in devs/stabber fork
Closes #64
Merge author: jabber.developer2
Commits authors:
Michael Vetter <jubalh@iodoru.org>
& Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
234
src/pgp/gpg.c
234
src/pgp/gpg.c
@@ -3,35 +3,9 @@
|
||||
* vim: expandtab:ts=4:sts=4:sw=4
|
||||
*
|
||||
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
||||
* Copyright (C) 2018 - 2025 Michael Vetter <jubalh@iodoru.org>
|
||||
*
|
||||
* This file is part of Profanity.
|
||||
*
|
||||
* Profanity is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Profanity is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* In addition, as a special exception, the copyright holders give permission to
|
||||
* link the code of portions of this program with the OpenSSL library under
|
||||
* certain conditions as described in each individual source file, and
|
||||
* distribute linked combinations including the two.
|
||||
*
|
||||
* You must obey the GNU General Public License in all respects for all of the
|
||||
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
||||
* may extend this exception to your version of the file(s), but you are not
|
||||
* obligated to do so. If you do not wish to do so, delete this exception
|
||||
* statement from your version. If you delete this exception statement from all
|
||||
* source files in the program, then also delete it here.
|
||||
* Copyright (C) 2018 - 2026 Michael Vetter <jubalh@iodoru.org>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
@@ -66,27 +40,28 @@ static GHashTable* pubkeys;
|
||||
static prof_keyfile_t pubkeys_prof_keyfile;
|
||||
static GKeyFile* pubkeyfile;
|
||||
|
||||
static char* passphrase;
|
||||
static char* passphrase_attempt;
|
||||
static gchar* passphrase;
|
||||
static gchar* passphrase_attempt;
|
||||
|
||||
static Autocomplete key_ac;
|
||||
|
||||
static char* _remove_header_footer(char* str, const char* const footer);
|
||||
static char* _add_header_footer(const char* const str, const char* const header, const char* const footer);
|
||||
static char* _gpgme_data_to_char(gpgme_data_t data);
|
||||
static gchar* _remove_header_footer(gchar* str, const char* const footer);
|
||||
static gchar* _add_header_footer(const gchar* const str, const char* const header, const char* const footer);
|
||||
static gchar* _gpgme_data_to_char(gpgme_data_t data);
|
||||
static void _save_pubkeys(void);
|
||||
static ProfPGPKey* _gpgme_key_to_ProfPGPKey(gpgme_key_t key);
|
||||
static const gchar* _gpgme_key_get_email(gpgme_key_t key);
|
||||
|
||||
void
|
||||
_p_gpg_free_pubkeyid(ProfPGPPubKeyId* pubkeyid)
|
||||
{
|
||||
if (pubkeyid) {
|
||||
free(pubkeyid->id);
|
||||
g_free(pubkeyid->id);
|
||||
}
|
||||
free(pubkeyid);
|
||||
g_free(pubkeyid);
|
||||
}
|
||||
|
||||
static gpgme_error_t*
|
||||
static gpgme_error_t
|
||||
_p_gpg_passphrase_cb(void* hook, const char* uid_hint, const char* passphrase_info, int prev_was_bad, int fd)
|
||||
{
|
||||
if (passphrase) {
|
||||
@@ -94,21 +69,21 @@ _p_gpg_passphrase_cb(void* hook, const char* uid_hint, const char* passphrase_in
|
||||
} else {
|
||||
GString* pass_term = g_string_new("");
|
||||
|
||||
auto_char char* password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
|
||||
auto_gchar gchar* password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
|
||||
if (password) {
|
||||
g_string_append(pass_term, password);
|
||||
}
|
||||
|
||||
g_string_append(pass_term, "\n");
|
||||
if (passphrase_attempt) {
|
||||
free(passphrase_attempt);
|
||||
g_free(passphrase_attempt);
|
||||
}
|
||||
passphrase_attempt = g_string_free(pass_term, FALSE);
|
||||
|
||||
gpgme_io_write(fd, passphrase_attempt, strlen(passphrase_attempt));
|
||||
}
|
||||
|
||||
return 0;
|
||||
return GPG_ERR_NO_ERROR;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -126,13 +101,11 @@ _p_gpg_close(void)
|
||||
key_ac = NULL;
|
||||
|
||||
if (passphrase) {
|
||||
free(passphrase);
|
||||
passphrase = NULL;
|
||||
GFREE_SET_NULL(passphrase);
|
||||
}
|
||||
|
||||
if (passphrase_attempt) {
|
||||
free(passphrase_attempt);
|
||||
passphrase_attempt = NULL;
|
||||
GFREE_SET_NULL(passphrase_attempt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +129,7 @@ p_gpg_init(void)
|
||||
}
|
||||
|
||||
void
|
||||
p_gpg_on_connect(const char* const barejid)
|
||||
p_gpg_on_connect(const gchar* const barejid)
|
||||
{
|
||||
gchar* pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
|
||||
if (!pubsloc) {
|
||||
@@ -179,13 +152,13 @@ p_gpg_on_connect(const char* const barejid)
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
for (gsize i = 0; i < len; i++) {
|
||||
GError* gerr = NULL;
|
||||
gchar* jid = jids[i];
|
||||
auto_gchar gchar* keyid = g_key_file_get_string(pubkeyfile, jid, "keyid", &gerr);
|
||||
if (gerr) {
|
||||
log_error("Error loading PGP key id for %s", jid);
|
||||
g_error_free(gerr);
|
||||
PROF_GERROR_FREE(gerr);
|
||||
} else {
|
||||
gpgme_key_t key = NULL;
|
||||
error = gpgme_get_key(ctx, keyid, &key, 0);
|
||||
@@ -194,10 +167,10 @@ p_gpg_on_connect(const char* const barejid)
|
||||
continue;
|
||||
}
|
||||
|
||||
ProfPGPPubKeyId* pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
||||
pubkeyid->id = strdup(keyid);
|
||||
ProfPGPPubKeyId* pubkeyid = g_new0(ProfPGPPubKeyId, 1);
|
||||
pubkeyid->id = g_strdup(keyid);
|
||||
pubkeyid->received = FALSE;
|
||||
g_hash_table_replace(pubkeys, strdup(jid), pubkeyid);
|
||||
g_hash_table_replace(pubkeys, g_strdup(jid), pubkeyid);
|
||||
gpgme_key_unref(key);
|
||||
}
|
||||
}
|
||||
@@ -216,7 +189,7 @@ p_gpg_on_disconnect(void)
|
||||
}
|
||||
|
||||
gboolean
|
||||
p_gpg_addkey(const char* const jid, const char* const keyid)
|
||||
p_gpg_addkey(const gchar* const jid, const gchar* const keyid)
|
||||
{
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
@@ -239,10 +212,10 @@ p_gpg_addkey(const char* const jid, const char* const keyid)
|
||||
_save_pubkeys();
|
||||
|
||||
// update in memory pubkeys list
|
||||
ProfPGPPubKeyId* pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
||||
pubkeyid->id = strdup(keyid);
|
||||
ProfPGPPubKeyId* pubkeyid = g_new0(ProfPGPPubKeyId, 1);
|
||||
pubkeyid->id = g_strdup(keyid);
|
||||
pubkeyid->received = FALSE;
|
||||
g_hash_table_replace(pubkeys, strdup(jid), pubkeyid);
|
||||
g_hash_table_replace(pubkeys, g_strdup(jid), pubkeyid);
|
||||
gpgme_key_unref(key);
|
||||
|
||||
return TRUE;
|
||||
@@ -251,7 +224,7 @@ p_gpg_addkey(const char* const jid, const char* const keyid)
|
||||
ProfPGPKey*
|
||||
p_gpg_key_new(void)
|
||||
{
|
||||
ProfPGPKey* p_pgpkey = malloc(sizeof(ProfPGPKey));
|
||||
ProfPGPKey* p_pgpkey = g_new0(ProfPGPKey, 1);
|
||||
p_pgpkey->id = NULL;
|
||||
p_pgpkey->name = NULL;
|
||||
p_pgpkey->fp = NULL;
|
||||
@@ -268,10 +241,10 @@ void
|
||||
p_gpg_free_key(ProfPGPKey* key)
|
||||
{
|
||||
if (key) {
|
||||
free(key->id);
|
||||
free(key->name);
|
||||
free(key->fp);
|
||||
free(key);
|
||||
g_free(key->id);
|
||||
g_free(key->name);
|
||||
g_free(key->fp);
|
||||
g_free(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +267,7 @@ GHashTable*
|
||||
p_gpg_list_keys(void)
|
||||
{
|
||||
gpgme_error_t error;
|
||||
GHashTable* result = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)p_gpg_free_key);
|
||||
GHashTable* result = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)p_gpg_free_key);
|
||||
|
||||
gpgme_ctx_t ctx;
|
||||
error = gpgme_new(&ctx);
|
||||
@@ -312,7 +285,7 @@ p_gpg_list_keys(void)
|
||||
while (!error) {
|
||||
ProfPGPKey* p_pgpkey = _gpgme_key_to_ProfPGPKey(key);
|
||||
if (p_pgpkey != NULL) {
|
||||
g_hash_table_insert(result, strdup(p_pgpkey->name), p_pgpkey);
|
||||
g_hash_table_insert(result, g_strdup(p_pgpkey->name), p_pgpkey);
|
||||
}
|
||||
|
||||
gpgme_key_unref(key);
|
||||
@@ -368,7 +341,7 @@ p_gpg_pubkeys(void)
|
||||
return pubkeys;
|
||||
}
|
||||
|
||||
const char*
|
||||
const gchar*
|
||||
p_gpg_libver(void)
|
||||
{
|
||||
if (libversion == NULL) {
|
||||
@@ -378,14 +351,14 @@ p_gpg_libver(void)
|
||||
}
|
||||
|
||||
gboolean
|
||||
p_gpg_valid_key(const char* const keyid, char** err_str)
|
||||
p_gpg_valid_key(const gchar* const keyid, gchar** err_str)
|
||||
{
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
if (error) {
|
||||
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
if (err_str) {
|
||||
*err_str = strdup(gpgme_strerror(error));
|
||||
*err_str = g_strdup(gpgme_strerror(error));
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
@@ -396,7 +369,7 @@ p_gpg_valid_key(const char* const keyid, char** err_str)
|
||||
if (error || key == NULL) {
|
||||
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
if (err_str) {
|
||||
*err_str = strdup(error ? gpgme_strerror(error) : "gpgme didn't return any error, but it didn't return a key");
|
||||
*err_str = g_strdup(error ? gpgme_strerror(error) : "gpgme didn't return any error, but it didn't return a key");
|
||||
}
|
||||
gpgme_release(ctx);
|
||||
return FALSE;
|
||||
@@ -408,14 +381,14 @@ p_gpg_valid_key(const char* const keyid, char** err_str)
|
||||
}
|
||||
|
||||
gboolean
|
||||
p_gpg_available(const char* const barejid)
|
||||
p_gpg_available(const gchar* const barejid)
|
||||
{
|
||||
char* pubkey = g_hash_table_lookup(pubkeys, barejid);
|
||||
gchar* pubkey = g_hash_table_lookup(pubkeys, barejid);
|
||||
return (pubkey != NULL);
|
||||
}
|
||||
|
||||
void
|
||||
p_gpg_verify(const char* const barejid, const char* const sign)
|
||||
p_gpg_verify(const gchar* const barejid, const gchar* const sign)
|
||||
{
|
||||
if (!sign) {
|
||||
return;
|
||||
@@ -429,7 +402,7 @@ p_gpg_verify(const char* const barejid, const char* const sign)
|
||||
return;
|
||||
}
|
||||
|
||||
auto_char char* sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER);
|
||||
auto_gchar gchar* sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER);
|
||||
gpgme_data_t sign_data;
|
||||
gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1);
|
||||
|
||||
@@ -455,10 +428,10 @@ p_gpg_verify(const char* const barejid, const char* const sign)
|
||||
log_debug("Could not find PGP key with ID %s for %s", result->signatures->fpr, barejid);
|
||||
} else {
|
||||
log_debug("Fingerprint found for %s: %s ", barejid, key->subkeys->fpr);
|
||||
ProfPGPPubKeyId* pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
||||
pubkeyid->id = strdup(key->subkeys->keyid);
|
||||
ProfPGPPubKeyId* pubkeyid = g_new0(ProfPGPPubKeyId, 1);
|
||||
pubkeyid->id = g_strdup(key->subkeys->keyid);
|
||||
pubkeyid->received = TRUE;
|
||||
g_hash_table_replace(pubkeys, strdup(barejid), pubkeyid);
|
||||
g_hash_table_replace(pubkeys, g_strdup(barejid), pubkeyid);
|
||||
}
|
||||
|
||||
gpgme_key_unref(key);
|
||||
@@ -468,8 +441,8 @@ p_gpg_verify(const char* const barejid, const char* const sign)
|
||||
gpgme_release(ctx);
|
||||
}
|
||||
|
||||
char*
|
||||
p_gpg_sign(const char* const str, const char* const fp)
|
||||
gchar*
|
||||
p_gpg_sign(const gchar* const str, const gchar* const fp)
|
||||
{
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
@@ -505,6 +478,10 @@ p_gpg_sign(const char* const str, const char* const fp)
|
||||
} else {
|
||||
str_or_empty = strdup("");
|
||||
}
|
||||
if (!str_or_empty) {
|
||||
log_error("GPG: strdup failed");
|
||||
return NULL;
|
||||
}
|
||||
gpgme_data_t str_data;
|
||||
gpgme_data_new_from_mem(&str_data, str_or_empty, strlen(str_or_empty), 1);
|
||||
|
||||
@@ -522,10 +499,10 @@ p_gpg_sign(const char* const str, const char* const fp)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* result = NULL;
|
||||
gchar* result = NULL;
|
||||
|
||||
size_t len = 0;
|
||||
char* signed_str = gpgme_data_release_and_get_mem(signed_data, &len);
|
||||
gchar* signed_str = gpgme_data_release_and_get_mem(signed_data, &len);
|
||||
if (signed_str) {
|
||||
GString* signed_gstr = g_string_new("");
|
||||
g_string_append_len(signed_gstr, signed_str, len);
|
||||
@@ -535,14 +512,14 @@ p_gpg_sign(const char* const str, const char* const fp)
|
||||
}
|
||||
|
||||
if (passphrase_attempt) {
|
||||
passphrase = strdup(passphrase_attempt);
|
||||
passphrase = g_strdup(passphrase_attempt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char*
|
||||
p_gpg_encrypt(const char* const barejid, const char* const message, const char* const fp)
|
||||
gchar*
|
||||
p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gchar* const fp)
|
||||
{
|
||||
ProfPGPPubKeyId* pubkeyid = g_hash_table_lookup(pubkeys, barejid);
|
||||
if (!pubkeyid) {
|
||||
@@ -616,8 +593,8 @@ p_gpg_encrypt(const char* const barejid, const char* const message, const char*
|
||||
return result;
|
||||
}
|
||||
|
||||
char*
|
||||
p_gpg_decrypt(const char* const cipher)
|
||||
gchar*
|
||||
p_gpg_decrypt(const gchar* const cipher)
|
||||
{
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
@@ -629,7 +606,7 @@ p_gpg_decrypt(const char* const cipher)
|
||||
|
||||
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
|
||||
|
||||
auto_char char* cipher_with_headers = _add_header_footer(cipher, PGP_MESSAGE_HEADER, PGP_MESSAGE_FOOTER);
|
||||
auto_gchar gchar* cipher_with_headers = _add_header_footer(cipher, PGP_MESSAGE_HEADER, PGP_MESSAGE_FOOTER);
|
||||
gpgme_data_t cipher_data;
|
||||
gpgme_data_new_from_mem(&cipher_data, cipher_with_headers, strlen(cipher_with_headers), 1);
|
||||
|
||||
@@ -655,11 +632,9 @@ p_gpg_decrypt(const char* const cipher)
|
||||
error = gpgme_get_key(ctx, recipient->keyid, &key, 1);
|
||||
|
||||
if (!error && key) {
|
||||
for (gpgme_user_id_t uid = key->uids; uid != NULL; uid = uid->next) {
|
||||
if (uid->email) {
|
||||
g_string_append(recipients_str, uid->email);
|
||||
break;
|
||||
}
|
||||
const gchar* addr = _gpgme_key_get_email(key);
|
||||
if (addr) {
|
||||
g_string_append(recipients_str, addr);
|
||||
}
|
||||
gpgme_key_unref(key);
|
||||
} else if (error) {
|
||||
@@ -680,7 +655,7 @@ p_gpg_decrypt(const char* const cipher)
|
||||
gpgme_release(ctx);
|
||||
|
||||
if (passphrase_attempt) {
|
||||
passphrase = strdup(passphrase_attempt);
|
||||
passphrase = g_strdup(passphrase_attempt);
|
||||
}
|
||||
|
||||
return _gpgme_data_to_char(plain_data);
|
||||
@@ -692,8 +667,8 @@ p_gpg_free_decrypted(char* decrypted)
|
||||
g_free(decrypted);
|
||||
}
|
||||
|
||||
char*
|
||||
p_gpg_autocomplete_key(const char* const search_str, gboolean previous, void* context)
|
||||
gchar*
|
||||
p_gpg_autocomplete_key(const gchar* const search_str, gboolean previous, void* context)
|
||||
{
|
||||
return autocomplete_complete(key_ac, search_str, TRUE, previous);
|
||||
}
|
||||
@@ -704,16 +679,16 @@ p_gpg_autocomplete_key_reset(void)
|
||||
autocomplete_reset(key_ac);
|
||||
}
|
||||
|
||||
char*
|
||||
p_gpg_format_fp_str(char* fp)
|
||||
gchar*
|
||||
p_gpg_format_fp_str(gchar* fp)
|
||||
{
|
||||
if (!fp) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GString* format = g_string_new("");
|
||||
int len = strlen(fp);
|
||||
for (int i = 0; i < len; i++) {
|
||||
size_t len = strlen(fp);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
g_string_append_c(format, fp[i]);
|
||||
if (((i + 1) % 4 == 0) && (i + 1 < len)) {
|
||||
g_string_append_c(format, ' ');
|
||||
@@ -728,12 +703,12 @@ p_gpg_format_fp_str(char* fp)
|
||||
*
|
||||
* @param keyid The key ID for which to retrieve the public key data.
|
||||
* If the key ID is empty or NULL, returns NULL.
|
||||
* @return The public key data as a null-terminated char* string allocated using malloc.
|
||||
* @return The public key data as a null-terminated gchar* string allocated using malloc.
|
||||
* The returned string should be freed by the caller.
|
||||
* Returns NULL on error, and errors are written to the error log.
|
||||
*/
|
||||
char*
|
||||
p_gpg_get_pubkey(const char* keyid)
|
||||
gchar*
|
||||
p_gpg_get_pubkey(const gchar* keyid)
|
||||
{
|
||||
if (!keyid || *keyid == '\0') {
|
||||
return NULL;
|
||||
@@ -777,18 +752,18 @@ cleanup:
|
||||
* @return TRUE if the buffer has the expected header and footer of an armored public key, FALSE otherwise.
|
||||
*/
|
||||
gboolean
|
||||
p_gpg_is_public_key_format(const char* buffer)
|
||||
p_gpg_is_public_key_format(const gchar* buffer)
|
||||
{
|
||||
if (buffer == NULL || buffer[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* headerPos = strstr(buffer, PGP_PUBLIC_KEY_HEADER);
|
||||
const gchar* headerPos = strstr(buffer, PGP_PUBLIC_KEY_HEADER);
|
||||
if (headerPos == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* footerPos = strstr(buffer, PGP_PUBLIC_KEY_FOOTER);
|
||||
const gchar* footerPos = strstr(buffer, PGP_PUBLIC_KEY_FOOTER);
|
||||
|
||||
return (footerPos != NULL && footerPos > headerPos);
|
||||
}
|
||||
@@ -803,7 +778,7 @@ p_gpg_is_public_key_format(const char* buffer)
|
||||
* by calling p_gpg_free_key() to avoid resource leaks.
|
||||
*/
|
||||
ProfPGPKey*
|
||||
p_gpg_import_pubkey(const char* buffer)
|
||||
p_gpg_import_pubkey(const gchar* buffer)
|
||||
{
|
||||
gpgme_ctx_t ctx;
|
||||
ProfPGPKey* result = NULL;
|
||||
@@ -873,9 +848,9 @@ _gpgme_key_to_ProfPGPKey(gpgme_key_t key)
|
||||
|
||||
ProfPGPKey* p_pgpkey = p_gpg_key_new();
|
||||
gpgme_subkey_t sub = key->subkeys;
|
||||
p_pgpkey->id = strdup(sub->keyid);
|
||||
p_pgpkey->name = strdup(key->uids->uid);
|
||||
p_pgpkey->fp = strdup(sub->fpr);
|
||||
p_pgpkey->id = g_strdup(sub->keyid);
|
||||
p_pgpkey->name = g_strdup(key->uids->uid);
|
||||
p_pgpkey->fp = g_strdup(sub->fpr);
|
||||
|
||||
while (sub) {
|
||||
if (sub->can_encrypt)
|
||||
@@ -892,6 +867,40 @@ _gpgme_key_to_ProfPGPKey(gpgme_key_t key)
|
||||
return p_pgpkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the first email address from a gpgme_key_t object.
|
||||
* This function provides backwards compatibility for both old and new GPGME versions.
|
||||
* - GPGME < 2.0.0: Uses gpgme_key_get_string_attr (if available)
|
||||
* - GPGME >= 2.0.0: Uses modern key->uids->email API
|
||||
*
|
||||
* @param key The gpgme_key_t object to extract email from.
|
||||
* @return The first email address found in the key's user IDs, or NULL if none found.
|
||||
* The returned string should not be freed as it points to internal gpgme memory.
|
||||
*/
|
||||
static const gchar*
|
||||
_gpgme_key_get_email(gpgme_key_t key)
|
||||
{
|
||||
if (!key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef GPGME_ATTR_EMAIL
|
||||
/* Use deprecated function if available (GPGME < 2.0.0) */
|
||||
return gpgme_key_get_string_attr(key, GPGME_ATTR_EMAIL, NULL, 0);
|
||||
#else
|
||||
/* Use modern API for GPGME >= 2.0.0 */
|
||||
gpgme_user_id_t uid = key->uids;
|
||||
while (uid) {
|
||||
if (uid->email && strlen(uid->email) > 0) {
|
||||
return uid->email;
|
||||
}
|
||||
uid = uid->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a gpgme_data_t object to a null-terminated char* string.
|
||||
*
|
||||
@@ -899,7 +908,7 @@ _gpgme_key_to_ProfPGPKey(gpgme_key_t key)
|
||||
* @return The converted string allocated using malloc, which should be freed by the caller.
|
||||
* If an error occurs or the data is empty, NULL is returned and errors are written to the error log.
|
||||
*/
|
||||
static char*
|
||||
static gchar*
|
||||
_gpgme_data_to_char(gpgme_data_t data)
|
||||
{
|
||||
size_t buffer_size = 0;
|
||||
@@ -910,16 +919,13 @@ _gpgme_data_to_char(gpgme_data_t data)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* buffer = malloc(buffer_size + 1);
|
||||
memcpy(buffer, gpgme_buffer, buffer_size);
|
||||
buffer[buffer_size] = '\0';
|
||||
gchar* buffer = g_strndup(gpgme_buffer, buffer_size);
|
||||
gpgme_free(gpgme_buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static char*
|
||||
_remove_header_footer(char* str, const char* const footer)
|
||||
static gchar*
|
||||
_remove_header_footer(gchar* str, const char* const footer)
|
||||
{
|
||||
int pos = 0;
|
||||
int newlines = 0;
|
||||
@@ -935,15 +941,15 @@ _remove_header_footer(char* str, const char* const footer)
|
||||
}
|
||||
}
|
||||
|
||||
char* stripped = strdup(&str[pos]);
|
||||
char* footer_start = g_strrstr(stripped, footer);
|
||||
gchar* stripped = g_strdup(&str[pos]);
|
||||
gchar* footer_start = g_strrstr(stripped, footer);
|
||||
footer_start[0] = '\0';
|
||||
|
||||
return stripped;
|
||||
}
|
||||
|
||||
static char*
|
||||
_add_header_footer(const char* const str, const char* const header, const char* const footer)
|
||||
static gchar*
|
||||
_add_header_footer(const gchar* const str, const char* const header, const char* const footer)
|
||||
{
|
||||
return g_strdup_printf("%s\n\n%s\n%s", header, str, footer);
|
||||
}
|
||||
|
||||
@@ -4,33 +4,7 @@
|
||||
*
|
||||
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
||||
*
|
||||
* This file is part of Profanity.
|
||||
*
|
||||
* Profanity is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Profanity is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* In addition, as a special exception, the copyright holders give permission to
|
||||
* link the code of portions of this program with the OpenSSL library under
|
||||
* certain conditions as described in each individual source file, and
|
||||
* distribute linked combinations including the two.
|
||||
*
|
||||
* You must obey the GNU General Public License in all respects for all of the
|
||||
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
||||
* may extend this exception to your version of the file(s), but you are not
|
||||
* obligated to do so. If you do not wish to do so, delete this exception
|
||||
* statement from your version. If you delete this exception statement from all
|
||||
* source files in the program, then also delete it here.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
|
||||
*/
|
||||
|
||||
#ifndef PGP_GPG_H
|
||||
@@ -38,9 +12,9 @@
|
||||
|
||||
typedef struct pgp_key_t
|
||||
{
|
||||
char* id;
|
||||
char* name;
|
||||
char* fp;
|
||||
gchar* id;
|
||||
gchar* name;
|
||||
gchar* fp;
|
||||
gboolean encrypt;
|
||||
gboolean sign;
|
||||
gboolean certify;
|
||||
@@ -50,31 +24,31 @@ typedef struct pgp_key_t
|
||||
|
||||
typedef struct pgp_pubkeyid_t
|
||||
{
|
||||
char* id;
|
||||
gchar* id;
|
||||
gboolean received;
|
||||
} ProfPGPPubKeyId;
|
||||
|
||||
void p_gpg_init(void);
|
||||
void p_gpg_on_connect(const char* const barejid);
|
||||
void p_gpg_on_connect(const gchar* const barejid);
|
||||
void p_gpg_on_disconnect(void);
|
||||
GHashTable* p_gpg_list_keys(void);
|
||||
void p_gpg_free_keys(GHashTable* keys);
|
||||
gboolean p_gpg_addkey(const char* const jid, const char* const keyid);
|
||||
gboolean p_gpg_addkey(const gchar* const jid, const gchar* const keyid);
|
||||
GHashTable* p_gpg_pubkeys(void);
|
||||
gboolean p_gpg_valid_key(const char* const keyid, char** err_str);
|
||||
gboolean p_gpg_available(const char* const barejid);
|
||||
const char* p_gpg_libver(void);
|
||||
char* p_gpg_sign(const char* const str, const char* const fp);
|
||||
void p_gpg_verify(const char* const barejid, const char* const sign);
|
||||
char* p_gpg_encrypt(const char* const barejid, const char* const message, const char* const fp);
|
||||
char* p_gpg_decrypt(const char* const cipher);
|
||||
void p_gpg_free_decrypted(char* decrypted);
|
||||
char* p_gpg_autocomplete_key(const char* const search_str, gboolean previous, void* context);
|
||||
gboolean p_gpg_valid_key(const gchar* const keyid, gchar** err_str);
|
||||
gboolean p_gpg_available(const gchar* const barejid);
|
||||
const gchar* p_gpg_libver(void);
|
||||
gchar* p_gpg_sign(const gchar* const str, const gchar* const fp);
|
||||
void p_gpg_verify(const gchar* const barejid, const gchar* const sign);
|
||||
gchar* p_gpg_encrypt(const gchar* const barejid, const gchar* const message, const gchar* const fp);
|
||||
gchar* p_gpg_decrypt(const gchar* const cipher);
|
||||
void p_gpg_free_decrypted(gchar* decrypted);
|
||||
gchar* p_gpg_autocomplete_key(const gchar* const search_str, gboolean previous, void* context);
|
||||
void p_gpg_autocomplete_key_reset(void);
|
||||
char* p_gpg_format_fp_str(char* fp);
|
||||
char* p_gpg_get_pubkey(const char* const keyid);
|
||||
gboolean p_gpg_is_public_key_format(const char* buffer);
|
||||
ProfPGPKey* p_gpg_import_pubkey(const char* buffer);
|
||||
gchar* p_gpg_format_fp_str(gchar* fp);
|
||||
gchar* p_gpg_get_pubkey(const gchar* const keyid);
|
||||
gboolean p_gpg_is_public_key_format(const gchar* buffer);
|
||||
ProfPGPKey* p_gpg_import_pubkey(const gchar* buffer);
|
||||
|
||||
ProfPGPKey* p_gpg_key_new(void);
|
||||
void p_gpg_free_key(ProfPGPKey* key);
|
||||
|
||||
226
src/pgp/ox.c
226
src/pgp/ox.c
@@ -3,35 +3,9 @@
|
||||
* vim: expandtab:ts=4:sts=4:sw=4
|
||||
*
|
||||
* Copyright (C) 2020 Stefan Kropp <stefan@debxwoody.de>
|
||||
* Copyright (C) 2020 - 2025 Michael Vetter <jubalh@iodoru.org>
|
||||
*
|
||||
* This file is part of Profanity.
|
||||
*
|
||||
* Profanity is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Profanity is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* In addition, as a special exception, the copyright holders give permission to
|
||||
* link the code of portions of this program with the OpenSSL library under
|
||||
* certain conditions as described in each individual source file, and
|
||||
* distribute linked combinations including the two.
|
||||
*
|
||||
* You must obey the GNU General Public License in all respects for all of the
|
||||
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
||||
* may extend this exception to your version of the file(s), but you are not
|
||||
* obligated to do so. If you do not wish to do so, delete this exception
|
||||
* statement from your version. If you delete this exception statement from all
|
||||
* source files in the program, then also delete it here.
|
||||
* Copyright (C) 2020 - 2026 Michael Vetter <jubalh@iodoru.org>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
@@ -85,6 +59,7 @@ ox_gpg_public_keys(void)
|
||||
if (error != GPG_ERR_EOF && error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: gpgme_op_keylist_next %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
g_hash_table_destroy(result);
|
||||
gpgme_release(ctx);
|
||||
return NULL;
|
||||
}
|
||||
while (!error) {
|
||||
@@ -145,10 +120,16 @@ ox_gpg_public_keys(void)
|
||||
char*
|
||||
p_ox_gpg_signcrypt(const char* const sender_barejid, const char* const recipient_barejid, const char* const message)
|
||||
{
|
||||
char* result = NULL;
|
||||
gpgme_ctx_t ctx = NULL;
|
||||
gpgme_key_t recp[3] = { NULL, NULL, NULL };
|
||||
gpgme_data_t plain = NULL;
|
||||
gpgme_data_t cipher = NULL;
|
||||
char* cipher_str = NULL;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
gpgme_check_version(NULL);
|
||||
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
||||
gpgme_ctx_t ctx;
|
||||
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
if (GPG_ERR_NO_ERROR != error) {
|
||||
@@ -165,13 +146,6 @@ p_ox_gpg_signcrypt(const char* const sender_barejid, const char* const recipient
|
||||
gpgme_set_textmode(ctx, 0);
|
||||
gpgme_set_offline(ctx, 1);
|
||||
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
||||
if (error != 0) {
|
||||
log_error("OX: Signcrypt error: %s", gpgme_strerror(error));
|
||||
}
|
||||
|
||||
gpgme_key_t recp[3];
|
||||
recp[0] = NULL,
|
||||
recp[1] = NULL;
|
||||
|
||||
char* xmpp_jid_me = alloca((strlen(sender_barejid) + 6) * sizeof(char));
|
||||
char* xmpp_jid_recipient = alloca((strlen(recipient_barejid) + 6) * sizeof(char));
|
||||
@@ -185,64 +159,73 @@ p_ox_gpg_signcrypt(const char* const sender_barejid, const char* const recipient
|
||||
|
||||
// lookup own key
|
||||
recp[0] = _ox_key_lookup(sender_barejid, TRUE);
|
||||
if (error != 0) {
|
||||
if (recp[0] == NULL) {
|
||||
cons_show_error("Can't find OX key for %s", xmpp_jid_me);
|
||||
log_error("OX: Key not found for %s. Error: %s", xmpp_jid_me, gpgme_strerror(error));
|
||||
return NULL;
|
||||
log_error("OX: Key not found for %s.", xmpp_jid_me);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_signers_add(ctx, recp[0]);
|
||||
if (error != 0) {
|
||||
log_error("OX: gpgme_signers_add %s. Error: %s", xmpp_jid_me, gpgme_strerror(error));
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// lookup key of recipient
|
||||
recp[1] = _ox_key_lookup(recipient_barejid, FALSE);
|
||||
if (error != 0) {
|
||||
if (recp[1] == NULL) {
|
||||
cons_show_error("Can't find OX key for %s", xmpp_jid_recipient);
|
||||
log_error("OX: Key not found for %s. Error: %s", xmpp_jid_recipient, gpgme_strerror(error));
|
||||
return NULL;
|
||||
log_error("OX: Key not found for %s.", xmpp_jid_recipient);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
recp[2] = NULL;
|
||||
log_debug("OX: %s <%s>", recp[0]->uids->name, recp[0]->uids->email);
|
||||
log_debug("OX: %s <%s>", recp[1]->uids->name, recp[1]->uids->email);
|
||||
if (recp[0]->uids) {
|
||||
log_debug("OX: %s <%s>", recp[0]->uids->name, recp[0]->uids->email);
|
||||
}
|
||||
if (recp[1]->uids) {
|
||||
log_debug("OX: %s <%s>", recp[1]->uids->name, recp[1]->uids->email);
|
||||
}
|
||||
|
||||
gpgme_encrypt_flags_t flags = 0;
|
||||
|
||||
gpgme_data_t plain;
|
||||
gpgme_data_t cipher;
|
||||
|
||||
error = gpgme_data_new(&plain);
|
||||
if (error != 0) {
|
||||
log_error("OX: %s", gpgme_strerror(error));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
error = gpgme_data_new_from_mem(&plain, message, strlen(message), 0);
|
||||
if (error != 0) {
|
||||
log_error("OX: %s", gpgme_strerror(error));
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_data_new(&cipher);
|
||||
if (error != 0) {
|
||||
log_error("OX: %s", gpgme_strerror(error));
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_op_encrypt_sign(ctx, recp, flags, plain, cipher);
|
||||
if (error != 0) {
|
||||
log_error("OX: %s", gpgme_strerror(error));
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
size_t len;
|
||||
char* cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
|
||||
char* result = g_base64_encode((unsigned char*)cipher_str, len);
|
||||
gpgme_key_release(recp[0]);
|
||||
gpgme_key_release(recp[1]);
|
||||
gpgme_release(ctx);
|
||||
cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
|
||||
cipher = NULL; // Already released by gpgme_data_release_and_get_mem
|
||||
result = g_base64_encode((unsigned char*)cipher_str, len);
|
||||
|
||||
cleanup:
|
||||
if (cipher_str)
|
||||
gpgme_free(cipher_str);
|
||||
if (plain)
|
||||
gpgme_data_release(plain);
|
||||
if (cipher)
|
||||
gpgme_data_release(cipher);
|
||||
if (recp[0])
|
||||
gpgme_key_release(recp[0]);
|
||||
if (recp[1])
|
||||
gpgme_key_release(recp[1]);
|
||||
if (ctx)
|
||||
gpgme_release(ctx);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -313,6 +296,7 @@ _ox_key_lookup(const char* const barejid, gboolean secret_only)
|
||||
if (uid->name && strlen(uid->name) >= 10) {
|
||||
if (g_strcmp0(uid->name, xmppuri->str) == 0) {
|
||||
gpgme_release(ctx);
|
||||
g_string_free(xmppuri, TRUE);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -321,6 +305,7 @@ _ox_key_lookup(const char* const barejid, gboolean secret_only)
|
||||
gpgme_key_unref(key);
|
||||
error = gpgme_op_keylist_next(ctx, &key);
|
||||
}
|
||||
g_string_free(xmppuri, TRUE);
|
||||
}
|
||||
gpgme_release(ctx);
|
||||
|
||||
@@ -362,6 +347,13 @@ _ox_key_is_usable(gpgme_key_t key, const char* const barejid, gboolean secret)
|
||||
char*
|
||||
p_ox_gpg_decrypt(char* base64)
|
||||
{
|
||||
char* result = NULL;
|
||||
gpgme_ctx_t ctx = NULL;
|
||||
gpgme_data_t plain = NULL;
|
||||
gpgme_data_t cipher = NULL;
|
||||
guchar* encrypted = NULL;
|
||||
char* plain_str = NULL;
|
||||
|
||||
// if there is no private key avaibale,
|
||||
// we don't try do decrypt
|
||||
if (!ox_is_private_key_available(connection_get_barejid())) {
|
||||
@@ -370,7 +362,6 @@ p_ox_gpg_decrypt(char* base64)
|
||||
setlocale(LC_ALL, "");
|
||||
gpgme_check_version(NULL);
|
||||
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
|
||||
if (GPG_ERR_NO_ERROR != error) {
|
||||
@@ -387,25 +378,19 @@ p_ox_gpg_decrypt(char* base64)
|
||||
gpgme_set_textmode(ctx, 0);
|
||||
gpgme_set_offline(ctx, 1);
|
||||
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
||||
if (error != 0) {
|
||||
log_error("OX: %s", gpgme_strerror(error));
|
||||
}
|
||||
|
||||
gpgme_data_t plain = NULL;
|
||||
gpgme_data_t cipher = NULL;
|
||||
|
||||
gsize s;
|
||||
guchar* encrypted = g_base64_decode(base64, &s);
|
||||
encrypted = g_base64_decode(base64, &s);
|
||||
error = gpgme_data_new_from_mem(&cipher, (char*)encrypted, s, 0);
|
||||
if (error != 0) {
|
||||
log_error("OX: gpgme_data_new_from_mem: %s", gpgme_strerror(error));
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_data_new(&plain);
|
||||
if (error != 0) {
|
||||
log_error("OX: %s", gpgme_strerror(error));
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_op_decrypt_verify(ctx, cipher, plain);
|
||||
@@ -413,18 +398,29 @@ p_ox_gpg_decrypt(char* base64)
|
||||
log_error("OX: gpgme_op_decrypt: %s", gpgme_strerror(error));
|
||||
error = gpgme_op_decrypt(ctx, cipher, plain);
|
||||
if (error != 0) {
|
||||
return NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
size_t len;
|
||||
char* plain_str = gpgme_data_release_and_get_mem(plain, &len);
|
||||
char* result = NULL;
|
||||
plain_str = gpgme_data_release_and_get_mem(plain, &len);
|
||||
plain = NULL; // Already released by gpgme_data_release_and_get_mem
|
||||
if (plain_str) {
|
||||
result = strndup(plain_str, len);
|
||||
gpgme_free(plain_str);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (encrypted)
|
||||
g_free(encrypted);
|
||||
if (plain_str)
|
||||
gpgme_free(plain_str);
|
||||
if (plain)
|
||||
gpgme_data_release(plain);
|
||||
if (cipher)
|
||||
gpgme_data_release(cipher);
|
||||
if (ctx)
|
||||
gpgme_release(ctx);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -455,30 +451,33 @@ p_ox_gpg_readkey(const char* const filename, char** key, char** fp)
|
||||
{
|
||||
log_info("PX: Read OpenPGP Key from file %s", filename);
|
||||
|
||||
GError* error = NULL;
|
||||
GError* gerr = NULL;
|
||||
gchar* data = NULL;
|
||||
gsize size = -1;
|
||||
gpgme_ctx_t ctx = NULL;
|
||||
gpgme_data_t gpgme_data = NULL;
|
||||
gpgme_key_t gkey = NULL;
|
||||
gpgme_key_t end = NULL;
|
||||
|
||||
gboolean success = g_file_get_contents(filename,
|
||||
&data,
|
||||
&size,
|
||||
&error);
|
||||
&gerr);
|
||||
if (success) {
|
||||
setlocale(LC_ALL, "");
|
||||
gpgme_check_version(NULL);
|
||||
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
|
||||
if (GPG_ERR_NO_ERROR != error) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_new failed: %s", gpgme_strerror(error));
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: set GPGME_PROTOCOL_OPENPGP: %s", gpgme_strerror(error));
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
gpgme_set_armor(ctx, 0);
|
||||
@@ -486,49 +485,54 @@ p_ox_gpg_readkey(const char* const filename, char** key, char** fp)
|
||||
gpgme_set_offline(ctx, 1);
|
||||
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
||||
|
||||
gpgme_data_t gpgme_data = NULL;
|
||||
error = gpgme_data_new(&gpgme_data);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_data_new %s", gpgme_strerror(error));
|
||||
return;
|
||||
}
|
||||
|
||||
error = gpgme_data_new_from_mem(&gpgme_data, (char*)data, size, 0);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_data_new_from_mem %s", gpgme_strerror(error));
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
error = gpgme_op_keylist_from_data_start(ctx, gpgme_data, 0);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_op_keylist_from_data_start %s", gpgme_strerror(error));
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
gpgme_key_t gkey;
|
||||
error = gpgme_op_keylist_next(ctx, &gkey);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_op_keylist_next %s", gpgme_strerror(error));
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
gpgme_key_t end;
|
||||
error = gpgme_op_keylist_next(ctx, &end);
|
||||
if (error == GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: ambiguous key");
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (gkey->revoked || gkey->expired || gkey->disabled || gkey->invalid || gkey->secret) {
|
||||
log_error("OX: Read OpenPGP key from file: Key is not valid");
|
||||
return;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
gchar* keybase64 = g_base64_encode((const guchar*)data, size);
|
||||
|
||||
*key = strdup(keybase64);
|
||||
*fp = strdup(gkey->fpr);
|
||||
g_free(keybase64);
|
||||
} else {
|
||||
log_error("OX: Read OpenPGP key from file: Unable to read file: %s", error->message);
|
||||
log_error("OX: Read OpenPGP key from file: Unable to read file: %s", gerr->message);
|
||||
g_error_free(gerr);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (data)
|
||||
g_free(data);
|
||||
if (gpgme_data)
|
||||
gpgme_data_release(gpgme_data);
|
||||
if (gkey)
|
||||
gpgme_key_unref(gkey);
|
||||
if (end)
|
||||
gpgme_key_unref(end);
|
||||
if (ctx)
|
||||
gpgme_release(ctx);
|
||||
}
|
||||
|
||||
gboolean
|
||||
@@ -536,22 +540,26 @@ p_ox_gpg_import(char* base64_public_key)
|
||||
{
|
||||
gsize size = -1;
|
||||
guchar* key = g_base64_decode(base64_public_key, &size);
|
||||
gboolean result = TRUE;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
gpgme_check_version(NULL);
|
||||
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
||||
gpgme_ctx_t ctx;
|
||||
gpgme_ctx_t ctx = NULL;
|
||||
gpgme_data_t gpgme_data = NULL;
|
||||
gpgme_error_t error = gpgme_new(&ctx);
|
||||
|
||||
if (GPG_ERR_NO_ERROR != error) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_new failed: %s", gpgme_strerror(error));
|
||||
return FALSE;
|
||||
result = FALSE;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: set GPGME_PROTOCOL_OPENPGP: %s", gpgme_strerror(error));
|
||||
return FALSE;
|
||||
result = FALSE;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
gpgme_set_armor(ctx, 0);
|
||||
@@ -559,18 +567,26 @@ p_ox_gpg_import(char* base64_public_key)
|
||||
gpgme_set_offline(ctx, 1);
|
||||
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
||||
|
||||
gpgme_data_t gpgme_data = NULL;
|
||||
error = gpgme_data_new(&gpgme_data);
|
||||
error = gpgme_data_new_from_mem(&gpgme_data, (gchar*)key, size, 0);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_data_new %s", gpgme_strerror(error));
|
||||
return FALSE;
|
||||
log_error("OX: Read OpenPGP key from file: gpgme_data_new_from_mem %s", gpgme_strerror(error));
|
||||
result = FALSE;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
gpgme_data_new_from_mem(&gpgme_data, (gchar*)key, size, 0);
|
||||
error = gpgme_op_import(ctx, gpgme_data);
|
||||
if (error != GPG_ERR_NO_ERROR) {
|
||||
log_error("OX: Failed to import key");
|
||||
result = FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
cleanup:
|
||||
if (key)
|
||||
g_free(key);
|
||||
if (gpgme_data)
|
||||
gpgme_data_release(gpgme_data);
|
||||
if (ctx)
|
||||
gpgme_release(ctx);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
30
src/pgp/ox.h
30
src/pgp/ox.h
@@ -3,35 +3,9 @@
|
||||
* vim: expandtab:ts=4:sts=4:sw=4
|
||||
*
|
||||
* Copyright (C) 2020 Stefan Kropp <stefan@debxwoody.de>
|
||||
* Copyright (C) 2020 - 2025 Michael Vetter <jubalh@iodoru.org>
|
||||
*
|
||||
* This file is part of Profanity.
|
||||
*
|
||||
* Profanity is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Profanity is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* In addition, as a special exception, the copyright holders give permission to
|
||||
* link the code of portions of this program with the OpenSSL library under
|
||||
* certain conditions as described in each individual source file, and
|
||||
* distribute linked combinations including the two.
|
||||
*
|
||||
* You must obey the GNU General Public License in all respects for all of the
|
||||
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
||||
* may extend this exception to your version of the file(s), but you are not
|
||||
* obligated to do so. If you do not wish to do so, delete this exception
|
||||
* statement from your version. If you delete this exception statement from all
|
||||
* source files in the program, then also delete it here.
|
||||
* Copyright (C) 2020 - 2026 Michael Vetter <jubalh@iodoru.org>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
|
||||
*/
|
||||
|
||||
#ifndef PGP_OX_H
|
||||
|
||||
Reference in New Issue
Block a user