diff options
author | James Booth <boothj5@gmail.com> | 2015-03-23 23:38:06 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2015-03-23 23:38:06 +0000 |
commit | 475dfebd97b8adace3ee56683b968da752a3ae02 (patch) | |
tree | dac7c25cdb51c8b562ad9543b422709cbf7a658f /src/pgp | |
parent | 8a5d1fef2930036d34bf02d0b15d0b6239725ffd (diff) | |
download | profani-tty-475dfebd97b8adace3ee56683b968da752a3ae02.tar.gz |
Added pgpkeyid account setting, send signed presence
Diffstat (limited to 'src/pgp')
-rw-r--r-- | src/pgp/gpg.c | 93 | ||||
-rw-r--r-- | src/pgp/gpg.h | 1 |
2 files changed, 92 insertions, 2 deletions
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index 67ea8794..0d0cae5a 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -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; } \ No newline at end of file diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h index f5271596..f017f14b 100644 --- a/src/pgp/gpg.h +++ b/src/pgp/gpg.h @@ -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 |