Added pgpkeyid account setting, send signed presence
This commit is contained in:
@@ -42,8 +42,12 @@
|
||||
#include "pgp/gpg.h"
|
||||
#include "log.h"
|
||||
|
||||
#define PGP_FOOTER "-----END PGP SIGNATURE-----"
|
||||
|
||||
static const char *libversion;
|
||||
|
||||
static char* _remove_header_footer(char *str);
|
||||
|
||||
void
|
||||
p_gpg_init(void)
|
||||
{
|
||||
@@ -62,7 +66,7 @@ p_gpg_list_keys(void)
|
||||
|
||||
error = gpgme_new(&ctx);
|
||||
if (error) {
|
||||
log_error("GPG: Could not list keys: %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -84,7 +88,7 @@ p_gpg_list_keys(void)
|
||||
gpgme_key_release(key);
|
||||
}
|
||||
} else {
|
||||
log_error("GPG: Could not list keys: %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
}
|
||||
|
||||
gpgme_release(ctx);
|
||||
@@ -107,4 +111,89 @@ p_gpg_free_key(ProfPGPKey *key)
|
||||
free(key->fp);
|
||||
free(key);
|
||||
}
|
||||
}
|
||||
|
||||
char*
|
||||
p_gpg_sign_str(const char * const str, const char * const fp)
|
||||
{
|
||||
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));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gpgme_key_t key = NULL;
|
||||
error = gpgme_get_key(ctx, fp, &key, 1);
|
||||
if (error || key == NULL) {
|
||||
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
gpgme_release (ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gpgme_signers_clear(ctx);
|
||||
error = gpgme_signers_add(ctx, key);
|
||||
if (error) {
|
||||
log_error("GPG: Failed to load signer. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
gpgme_release(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gpgme_data_t str_data;
|
||||
gpgme_data_t signed_data;
|
||||
char *str_or_empty = NULL;
|
||||
if (str) {
|
||||
str_or_empty = strdup(str);
|
||||
} else {
|
||||
str_or_empty = strdup("");
|
||||
}
|
||||
gpgme_data_new_from_mem(&str_data, str_or_empty, strlen(str_or_empty), 1);
|
||||
gpgme_data_new(&signed_data);
|
||||
|
||||
gpgme_set_armor(ctx,1);
|
||||
error = gpgme_op_sign(ctx,str_data,signed_data,GPGME_SIG_MODE_DETACH);
|
||||
if (error) {
|
||||
log_error("GPG: Failed to sign string. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
||||
gpgme_release(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *result = NULL;
|
||||
gpgme_data_release(str_data);
|
||||
|
||||
size_t len = 0;
|
||||
char *signed_str = gpgme_data_release_and_get_mem(signed_data, &len);
|
||||
if (signed_str != NULL) {
|
||||
signed_str[len] = 0;
|
||||
result = _remove_header_footer(signed_str);
|
||||
}
|
||||
gpgme_free(signed_str);
|
||||
gpgme_release(ctx);
|
||||
free(str_or_empty);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static char*
|
||||
_remove_header_footer(char *str)
|
||||
{
|
||||
char *pointer = str;
|
||||
|
||||
int newlines = 0;
|
||||
while (newlines < 3) {
|
||||
if (pointer[0] == '\n') {
|
||||
newlines++;
|
||||
}
|
||||
pointer++;
|
||||
|
||||
if (strlen(pointer) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
char *stripped = malloc(strlen(pointer)+1-strlen(PGP_FOOTER));
|
||||
strncpy(stripped,pointer,strlen(pointer)-strlen(PGP_FOOTER));
|
||||
stripped[strlen(pointer)-strlen(PGP_FOOTER)] = '\0';
|
||||
|
||||
return stripped;
|
||||
}
|
||||
@@ -45,5 +45,6 @@ void p_gpg_init(void);
|
||||
GSList* p_gpg_list_keys(void);
|
||||
const char* p_gpg_libver(void);
|
||||
void p_gpg_free_key(ProfPGPKey *key);
|
||||
char* p_gpg_sign_str(const char * const str, const char * const fp);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user