diff options
author | DebXWoody <stefan@debxwoody.de> | 2020-07-05 09:28:51 +0200 |
---|---|---|
committer | Michael Vetter <jubalh@iodoru.org> | 2020-07-06 13:19:18 +0200 |
commit | 5a179572535805022580b264a99df60a47cb935d (patch) | |
tree | b4236a482866d76fb435a40e74104c7ea878ce25 /src/pgp | |
parent | 5e87b0dc51c941bcc819736c93ef06b9e24517a6 (diff) | |
download | profani-tty-5a179572535805022580b264a99df60a47cb935d.tar.gz |
OX: Announce public key on PEP
src/pgp/gpg.c:p_ox_gpg_readkey Used to read a public key from a file. The function will return the fingerprint of the file and the base64 encoded key. src/xmpp/ox.[hc] ox_announce_public_key(const char* const filename) can be called from the /ox announce <filename> command. The key within the file will be pushed on PEP and the Metadata node will be set. Issue: #1331
Diffstat (limited to 'src/pgp')
-rw-r--r-- | src/pgp/gpg.c | 103 | ||||
-rw-r--r-- | src/pgp/gpg.h | 2 |
2 files changed, 105 insertions, 0 deletions
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index 6fe2e858..b9be1d71 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -1190,4 +1190,107 @@ p_ox_gpg_decrypt(char* base64) return result; } +/*! + * \brief Read public key from file. + * + * This function is used the read a public key from a file. + * + * This function is used to read a key and push it on PEP. There are some checks + * in this function: + * + * Key is not + * - gkey->revoked + * - gkey->expired + * - gkey->disabled + * - gkey->invalid + * - gkey->secret + * + * Only one key in the file. + * + * \param filename filname to read the file. + * \param key result with base64 encode key or NULL + * \param fp result with the fingerprint or NULL + * + */ + +void +p_ox_gpg_readkey(const char* const filename, char** key, char** fp){ + + log_info("Read OpenPGP Key from file %s", filename); + + GError* error = NULL; + gchar* data = NULL; + gsize size = -1; + + gboolean success = g_file_get_contents (filename, + &data, + &size, + &error); + 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("Read OpenPGP key from file: gpgme_new failed: %s", gpgme_strerror(error)); + return; + } + + error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP); + if( error != GPG_ERR_NO_ERROR ) { + log_error("Read OpenPGP key from file: set GPGME_PROTOCOL_OPENPGP: %s", gpgme_strerror(error)); + return; + } + + gpgme_set_armor(ctx,0); + gpgme_set_textmode(ctx,0); + 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("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("Read OpenPGP key from file: gpgme_data_new_from_mem %s", gpgme_strerror(error)); + return; + } + error = gpgme_op_keylist_from_data_start ( ctx, gpgme_data, 0); + if ( error != GPG_ERR_NO_ERROR ) { + log_error("Read OpenPGP key from file: gpgme_op_keylist_from_data_start %s", gpgme_strerror(error)); + return; + } + gpgme_key_t gkey; + error = gpgme_op_keylist_next (ctx, &gkey); + if ( error != GPG_ERR_NO_ERROR ) { + log_error("Read OpenPGP key from file: gpgme_op_keylist_next %s", gpgme_strerror(error)); + return; + } + + gpgme_key_t end; + error = gpgme_op_keylist_next (ctx, &end); + if( error == GPG_ERR_NO_ERROR ) { + log_error("Read OpenPGP key from file: ambiguous key"); + return; + } + if(gkey->revoked || gkey->expired || gkey->disabled || gkey->invalid || gkey->secret ) { + log_error("Read OpenPGP key from file: Key is not valid"); + return; + } + + gchar* keybase64 = g_base64_encode( (const guchar*) data, size ); + + *key = strdup(keybase64); + *fp = strdup(gkey->fpr); + } else { + log_error("Read OpenPGP key from file: Unable to read file: %s", error->message); + } + +} diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h index 3eae6032..b3202505 100644 --- a/src/pgp/gpg.h +++ b/src/pgp/gpg.h @@ -76,6 +76,8 @@ char* p_ox_gpg_signcrypt(const char* const sender_barejid, const char* const rec char* p_ox_gpg_decrypt(char* base64); +void p_ox_gpg_readkey(const char* const filename, char** key, char** fp); + /*! * \brief List of public keys with xmpp-URI. * |